From fa94ea89c2b63ca48976e9a33b2f9d9528bdc153 Mon Sep 17 00:00:00 2001 From: bramski Date: Sun, 15 Apr 2018 18:47:23 -0700 Subject: [PATCH] Adds ability to ignore unknown styles and generate either warnings or simply ignore the problems. --- lib/draftjs_exporter/html.rb | 5 +-- lib/draftjs_exporter/style_state.rb | 35 ++++++++++++++++----- lib/draftjs_exporter/version.rb | 2 +- spec/integrations/html_spec.rb | 49 +++++++++++++++++++++++++++-- 4 files changed, 78 insertions(+), 13 deletions(-) diff --git a/lib/draftjs_exporter/html.rb b/lib/draftjs_exporter/html.rb index fdcb730..5e3b9c9 100644 --- a/lib/draftjs_exporter/html.rb +++ b/lib/draftjs_exporter/html.rb @@ -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 = {}) @@ -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| diff --git a/lib/draftjs_exporter/style_state.rb b/lib/draftjs_exporter/style_state.rb index 1556f16..74f9ccb 100644 --- a/lib/draftjs_exporter/style_state.rb +++ b/lib/draftjs_exporter/style_state.rb @@ -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) @@ -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) diff --git a/lib/draftjs_exporter/version.rb b/lib/draftjs_exporter/version.rb index 45f1cf1..daa2910 100644 --- a/lib/draftjs_exporter/version.rb +++ b/lib/draftjs_exporter/version.rb @@ -1,4 +1,4 @@ # frozen_string_literal: true module DraftjsExporter - VERSION = '0.0.7'.freeze + VERSION = '0.1.0'.freeze end diff --git a/spec/integrations/html_spec.rb b/spec/integrations/html_spec.rb index 995a8f4..66114dd 100644 --- a/spec/integrations/html_spec.rb +++ b/spec/integrations/html_spec.rb @@ -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 @@ -267,11 +269,54 @@ } expected_output = <<-OUTPUT.strip - + 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 +
+some paragraph text
+ 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