Skip to content

Commit 966b423

Browse files
committed
Preserve parsed styles in the instance
1 parent 9a93376 commit 966b423

3 files changed

Lines changed: 24 additions & 4 deletions

File tree

examples/instance.pdf

1.41 KB
Binary file not shown.

lib/prawn_html/html_parser.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,17 @@ def initialize(renderer, ignore_content_tags: %i[script style])
1515
@ignore = false
1616
@ignore_content_tags = ignore_content_tags
1717
@renderer = renderer
18-
@styles = {}
18+
@raw_styles = {}
1919
end
2020

2121
# Processes HTML and renders it
2222
#
2323
# @param html [String] The HTML content to process
2424
def process(html)
25+
@styles = {}
2526
@processing = !html.include?('<body')
2627
@document = Oga.parse_html(html)
28+
process_styles # apply previously loaded styles
2729
traverse_nodes(document.children)
2830
renderer.flush
2931
end
@@ -59,9 +61,9 @@ def init_element(node)
5961
end
6062
end
6163

62-
def process_styles(text_styles)
63-
hash_styles = text_styles.scan(REGEXP_STYLES).to_h
64-
hash_styles.each do |selector, rule|
64+
def process_styles(text_styles = nil)
65+
@raw_styles = text_styles.scan(REGEXP_STYLES).to_h if text_styles
66+
@raw_styles.each do |selector, rule|
6567
document.css(selector).each do |node|
6668
styles[node] = rule
6769
end

spec/features/instance_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe 'Instance' do
4+
it 'preserves the styles in the instance' do
5+
pdf = Prawn::Document.new(page_size: 'A4')
6+
phtml = PrawnHtml::Instance.new(pdf)
7+
phtml.append(html: '<style>h1 { color: green }</style>')
8+
phtml.append(html: '<h1>Some HTML before</h1>')
9+
pdf.text 'Some Prawn text'
10+
phtml.append(html: '<h1>Some HTML after</h1>')
11+
12+
output_file = File.expand_path('../../examples/instance.pdf', __dir__)
13+
pdf.render_file(output_file) unless File.exist?(output_file)
14+
15+
expected_pdf = File.read(output_file)
16+
expect(Zlib.crc32(pdf.render)).to eq Zlib.crc32(expected_pdf)
17+
end
18+
end

0 commit comments

Comments
 (0)