Skip to content

Commit be9e94d

Browse files
committed
Evaluate before content on tag open
1 parent f37aa53 commit be9e94d

3 files changed

Lines changed: 11 additions & 22 deletions

File tree

lib/prawn_html/context.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ def merged_styles
6262
end
6363
end
6464

65+
# :nocov:
66+
def inspect
67+
map(&:class).map(&:to_s).join(', ')
68+
end
69+
# :nocov:
70+
6571
# Remove the last element from the context
6672
def remove_last
6773
last.on_context_remove(self) if last.respond_to?(:on_context_remove)

lib/prawn_html/document_renderer.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class DocumentRenderer
99
#
1010
# @param pdf [PdfWrapper] target PDF wrapper
1111
def initialize(pdf)
12+
@before_content = []
1213
@buffer = []
1314
@context = Context.new
1415
@last_margin = 0
@@ -42,6 +43,7 @@ def on_tag_open(tag_name, attributes:, element_styles: '')
4243
options = { width: pdf.page_width, height: pdf.page_height }
4344
tag_class.new(tag_name, attributes: attributes, options: options).tap do |element|
4445
setup_element(element, element_styles: element_styles)
46+
@before_content.push(element.before_content) if element.respond_to?(:before_content)
4547
@last_tag_open = true
4648
end
4749
end
@@ -106,8 +108,9 @@ def apply_tag_open_styles(element)
106108
end
107109

108110
def prepare_text(content)
109-
before_content = context.before_content
110-
text = before_content ? ::Oga::HTML::Entities.decode(before_content) : ''
111+
text = @before_content.any? ? ::Oga::HTML::Entities.decode(@before_content.join) : ''
112+
@before_content.clear
113+
111114
return (@last_text = text + content) if context.white_space_pre?
112115

113116
content = content.lstrip if @last_text[-1] == ' ' || @last_tag_open

spec/units/prawn_html/document_renderer_spec.rb

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -63,27 +63,7 @@
6363

6464
let(:content) { 'some content' }
6565

66-
before do
67-
allow(Oga::HTML::Entities).to receive(:decode).and_call_original
68-
end
69-
7066
it { is_expected.to be_nil }
71-
72-
it 'calls the decode method for before content' do
73-
on_text_node
74-
expect(Oga::HTML::Entities).to have_received(:decode)
75-
end
76-
77-
context 'without before content' do
78-
before do
79-
allow(context).to receive(:before_content).and_return(nil)
80-
end
81-
82-
it "doesn't call the decode method" do
83-
on_text_node
84-
expect(Oga::HTML::Entities).not_to have_received(:decode)
85-
end
86-
end
8767
end
8868

8969
describe '#render' do

0 commit comments

Comments
 (0)