From 743e5d1d2a8f649f452834122cf5b4dae6016a7e Mon Sep 17 00:00:00 2001 From: Mattia Roccoberton Date: Tue, 7 Sep 2021 08:50:00 +0200 Subject: [PATCH 1/2] Handle tag extra_styles Extra styles are additional attributes per tag that need to be merged in the styles. For example: href for A tag. --- lib/prawn_html/tag.rb | 1 + spec/units/prawn_html/tag_spec.rb | 43 +++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/lib/prawn_html/tag.rb b/lib/prawn_html/tag.rb index d098700..66679ef 100644 --- a/lib/prawn_html/tag.rb +++ b/lib/prawn_html/tag.rb @@ -45,6 +45,7 @@ def process_styles(element_styles: nil) attrs.merge_text_styles!(tag_styles, options: options) if respond_to?(:tag_styles) attrs.merge_text_styles!(element_styles, options: options) if element_styles attrs.merge_text_styles!(attrs.style, options: options) + attrs.merge_text_styles!(extra_styles, options: options) if respond_to?(:extra_styles) end # Styles to apply on tag closing diff --git a/spec/units/prawn_html/tag_spec.rb b/spec/units/prawn_html/tag_spec.rb index 7c30828..73b271f 100644 --- a/spec/units/prawn_html/tag_spec.rb +++ b/spec/units/prawn_html/tag_spec.rb @@ -41,6 +41,49 @@ end end + describe '#process_styles' do + subject(:process_styles) { tag.process_styles(element_styles: element_styles) } + + let(:element_styles) { nil } + + before do + allow(tag.attrs).to receive(:merge_text_styles!) + end + + it 'merges the inline styles' do + process_styles + expect(tag.attrs).to have_received(:merge_text_styles!).with('color: #0088ff', options: {}) + end + + context 'with some additional styles' do + let(:some_tag_class) do + Class.new(described_class) do + def extra_styles + 'color: green; text-decoration: underline' + end + + def tag_styles + 'color: yellow; font-style: italic' + end + end + end + + let(:element_styles) { 'color: red; font-weight: bold' } + let(:tag) { some_tag_class.new(:some_tag, attributes: attributes) } + + it 'merges the tag styles', :aggregate_failures do + process_styles + + expected_styles = 'color: yellow; font-style: italic' + expect(tag.attrs).to have_received(:merge_text_styles!).with(expected_styles, options: {}).ordered + expect(tag.attrs).to have_received(:merge_text_styles!).with(element_styles, options: {}).ordered + expect(tag.attrs).to have_received(:merge_text_styles!).with('color: #0088ff', options: {}).ordered + expected_styles = 'color: green; text-decoration: underline' + expect(tag.attrs).to have_received(:merge_text_styles!).with(expected_styles, options: {}).ordered + end + end + end + describe '#styles' do subject(:styles) { tag.styles } From ebf4836d293b6c5f6df48da37d4e8b61f628d6b1 Mon Sep 17 00:00:00 2001 From: Mattia Roccoberton Date: Tue, 7 Sep 2021 08:55:07 +0200 Subject: [PATCH 2/2] Improve A tag --- lib/prawn_html/tags/a.rb | 7 ++++--- spec/units/prawn_html/tags/a_spec.rb | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/prawn_html/tags/a.rb b/lib/prawn_html/tags/a.rb index ae70fd8..cb7c629 100644 --- a/lib/prawn_html/tags/a.rb +++ b/lib/prawn_html/tags/a.rb @@ -5,12 +5,13 @@ module Tags class A < Tag ELEMENTS = [:a].freeze - def tag_styles - return unless attrs.href + def extra_styles + attrs.href ? "href: #{attrs.href}" : nil + end + def tag_styles <<~STYLES color: #00E; - href: #{attrs.href}; text-decoration: underline; STYLES end diff --git a/spec/units/prawn_html/tags/a_spec.rb b/spec/units/prawn_html/tags/a_spec.rb index c13fcd2..39d3334 100644 --- a/spec/units/prawn_html/tags/a_spec.rb +++ b/spec/units/prawn_html/tags/a_spec.rb @@ -11,7 +11,7 @@ end it "styles doesn't include the link property" do - expect(a.styles).to eq(color: 'ffbb11') + expect(a.styles).to eq(color: 'ffbb11', styles: [:underline]) end end