Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/draftjs_exporter/html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ module DraftjsExporter
class HTML
attr_reader :block_map, :style_map, :entity_decorators

def initialize(block_map:, style_map:, entity_decorators:)
def initialize(block_map:, style_map:, entity_decorators:, style_options: {})
@block_map = block_map
@style_map = style_map
@entity_decorators = entity_decorators
@style_options = style_options
end

def call(content_state, options = {})
Expand All @@ -28,7 +29,7 @@ def call(content_state, options = {})
private

def block_contents(element, block, entity_map)
style_state = StyleState.new(style_map)
style_state = StyleState.new(style_map, **@style_options)
entity_state = EntityState.new(element, entity_decorators, entity_map)
build_command_groups(block).each do |text, commands|
commands.each do |command|
Expand Down
35 changes: 27 additions & 8 deletions lib/draftjs_exporter/style_state.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# frozen_string_literal: true
module DraftjsExporter
class StyleState
attr_reader :styles, :style_map
attr_reader :styles, :style_map, :unknown_styles

def initialize(style_map)
def initialize(style_map, unknown_styles: :warn)
@styles = []
@style_map = style_map
@unknown_styles = unknown_styles
end

def apply(command)
Expand All @@ -22,16 +23,34 @@ def text?
end

def element_attributes
return {} unless styles.any?
{ style: styles_css }
css_attributes = calculate_styles
if css_attributes.any?
{ style: css_attributes.join }
else
{}
end
end

def styles_css
private

def calculate_styles
styles.map { |style|
style_map.fetch(style)
}.inject({}, :merge).map { |key, value|
fetch_style(style)
}.compact.inject({}, :merge).map { |key, value|
"#{hyphenize(key)}: #{value};"
}.join
}
end

def fetch_style(style)
style_map.fetch(style) { |style|
case unknown_styles
when :warn, :ignore
warn("Missing definition for style: #{style}") if unknown_styles == :warn
nil
else
raise KeyError, "Cannot find style #{style}"
end
}
end

def hyphenize(string)
Expand Down
2 changes: 1 addition & 1 deletion lib/draftjs_exporter/version.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literal: true
module DraftjsExporter
VERSION = '0.0.7'.freeze
VERSION = '0.1.0'.freeze
end
49 changes: 47 additions & 2 deletions spec/integrations/html_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
},
style_map: {
'ITALIC' => { fontStyle: 'italic' }
}
},
style_options: style_options
)
end
let(:style_options) { {} }

describe '#call' do
context 'with different blocks' do
Expand Down Expand Up @@ -267,11 +269,54 @@
}

expected_output = <<-OUTPUT.strip
<ul class=\"public-DraftStyleDefault-ul\">\n<li>Russian: Привет, мир!</li>\n<li>Japanese: 曖昧さ回避</li>\n</ul>
<ul class=\"public-DraftStyleDefault-ul\">\n<li>Russian: Привет, мир!</li>\n<li>Japanese: 曖昧さ回避</li>\n</ul>
OUTPUT

expect(mapper.call(input, encoding: 'UTF-8')).to eq(expected_output)
end
end

context 'strange styles' do
let(:input) {
input = {
entityMap: {},
blocks: [
{
key: 'dem5p',
text: 'some paragraph text',
type: 'unstyled',
depth: 0,
inlineStyleRanges: [
{
offset: 0,
length: 4,
style: 'rgba(0.5,0.5,0.5,0.1)'
}
],
entityRanges: []
}
]
}
}

let(:expected_output) {
<<-OUTPUT.strip
<div>
<span>some</span> paragraph text</div>
OUTPUT
}
it 'generates warnings for the missing styles.' do
expect(mapper.call(input)).to eq(expected_output)
end

context "style options set to :error" do
let(:style_options) { { unknown_styles: :error } }
it 'generates errors for the missing styles.' do
expect {
mapper.call(input)
}.to raise_error(KeyError, "Cannot find style rgba(0.5,0.5,0.5,0.1)")
end
end
end
end
end