prawn-svg 0.36.2 → 0.37.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +2 -2
- data/lib/prawn/svg/css/stylesheets.rb +0 -7
- data/lib/prawn/svg/elements/base.rb +15 -8
- data/lib/prawn/svg/properties.rb +9 -4
- data/lib/prawn/svg/version.rb +1 -1
- data/spec/prawn/svg/css/stylesheets_spec.rb +0 -2
- data/spec/prawn/svg/elements/base_spec.rb +2 -2
- data/spec/sample_svg/important.svg +14 -0
- data/spec/sample_svg/presentation_attribute_precedence.svg +12 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d5c28b2e706923d04785979a68f0e457f14df7c71b519d16db6e0bb45e3458bc
|
4
|
+
data.tar.gz: 3ade59453c7005f624b4dcd36d9930f27285291c410b00fe57b3d3d278673e4c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 250c187430ff8042898b9ee55a595162824613e13a9760a8e32dd21e5a026dfabe012a80f3c19442b3a97635ab020aa1eb113cd70dcc86ffef888f6bc2d39fa1
|
7
|
+
data.tar.gz: 1d79258d401547d57dd694878e08d9a50d0166cce4fefd9351cfda0ef6a7c3096910a88a29650993f0eeadd44d7f2fba21d25f438e31660750cc4b847cacc760
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -114,9 +114,9 @@ prawn-svg supports CSS, both in `<style>` blocks and `style` attributes.
|
|
114
114
|
In CSS selectors you can use element names, IDs, classes, attributes (existence, `=`, `^=`, `$=`, `*=`, `~=`, `|=`)
|
115
115
|
and all combinators (` `, `>`, `+`, `~`).
|
116
116
|
The pseudo-classes `:first-child`, `:last-child` and `:nth-child(n)` (where n is a number) also work.
|
117
|
+
`!important` is supported.
|
117
118
|
|
118
|
-
Pseudo-elements and the other pseudo-classes are not supported.
|
119
|
-
implemented, but `!important` is not.
|
119
|
+
Pseudo-elements and the other pseudo-classes are not supported.
|
120
120
|
|
121
121
|
## Not supported
|
122
122
|
|
@@ -1,7 +1,5 @@
|
|
1
1
|
module Prawn::SVG::CSS
|
2
2
|
class Stylesheets
|
3
|
-
USER_AGENT_STYLESHEET = 'svg, symbol, image, marker, pattern, foreignObject { overflow: hidden }'.freeze
|
4
|
-
|
5
3
|
attr_reader :css_parser, :root, :media
|
6
4
|
|
7
5
|
def initialize(css_parser, root, media = :all)
|
@@ -11,7 +9,6 @@ module Prawn::SVG::CSS
|
|
11
9
|
end
|
12
10
|
|
13
11
|
def load
|
14
|
-
load_user_agent_stylesheet
|
15
12
|
load_style_elements
|
16
13
|
xpath_styles = gather_xpath_styles
|
17
14
|
associate_xpath_styles_with_elements(xpath_styles)
|
@@ -19,10 +16,6 @@ module Prawn::SVG::CSS
|
|
19
16
|
|
20
17
|
private
|
21
18
|
|
22
|
-
def load_user_agent_stylesheet
|
23
|
-
css_parser.add_block!(USER_AGENT_STYLESHEET)
|
24
|
-
end
|
25
|
-
|
26
19
|
def load_style_elements
|
27
20
|
REXML::XPath.match(root, '//style').each do |source|
|
28
21
|
data = source.texts.map(&:value).join
|
@@ -142,7 +142,7 @@ class Prawn::SVG::Elements::Base
|
|
142
142
|
source.elements.select do |elem|
|
143
143
|
# To be strict, we shouldn't treat namespace-less elements as SVG, but for
|
144
144
|
# backwards compatibility, and because it doesn't hurt, we will.
|
145
|
-
|
145
|
+
[SVG_NAMESPACE, ''].include?(elem.namespace)
|
146
146
|
end
|
147
147
|
end
|
148
148
|
|
@@ -203,20 +203,27 @@ class Prawn::SVG::Elements::Base
|
|
203
203
|
end
|
204
204
|
|
205
205
|
def extract_attributes_and_properties
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
@properties.set(name, value)
|
210
|
-
end
|
206
|
+
# Apply user agent stylesheet
|
207
|
+
if %w[svg symbol image marker pattern foreignObject].include?(source.name)
|
208
|
+
@properties.set('overflow', 'hidden')
|
211
209
|
end
|
212
210
|
|
213
|
-
|
214
|
-
|
211
|
+
# Apply presentation attributes, and set attributes that aren't presentation attributes
|
215
212
|
source.attributes.each do |name, value|
|
216
213
|
# Properties#set returns nil if it's not a recognised property name
|
217
214
|
@properties.set(name, value) or @attributes[name] = value
|
218
215
|
end
|
219
216
|
|
217
|
+
# Apply stylesheet styles
|
218
|
+
if (styles = document.element_styles[source])
|
219
|
+
styles.each do |name, value, important|
|
220
|
+
@properties.set(name, value, important: important)
|
221
|
+
end
|
222
|
+
end
|
223
|
+
|
224
|
+
# Apply inline styles
|
225
|
+
@properties.load_hash(parse_css_declarations(source.attributes['style'] || ''))
|
226
|
+
|
220
227
|
state.computed_properties.compute_properties(@properties)
|
221
228
|
end
|
222
229
|
|
data/lib/prawn/svg/properties.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
module Prawn::SVG
|
2
2
|
class Properties
|
3
|
-
Config = Struct.new(:default, :inheritable?, :valid_values, :attr, :ivar)
|
3
|
+
Config = Struct.new(:default, :inheritable?, :valid_values, :attr, :ivar, :id)
|
4
4
|
|
5
5
|
EM = 16
|
6
6
|
FONT_SIZES = {
|
@@ -49,6 +49,7 @@ module Prawn::SVG
|
|
49
49
|
|
50
50
|
PROPERTIES.each do |name, value|
|
51
51
|
value.attr = name.gsub('-', '_')
|
52
|
+
value.id = value.attr.to_sym
|
52
53
|
value.ivar = "@#{value.attr}"
|
53
54
|
end
|
54
55
|
|
@@ -57,9 +58,11 @@ module Prawn::SVG
|
|
57
58
|
ATTR_NAMES = PROPERTIES.keys.map { |name| name.gsub('-', '_') }
|
58
59
|
|
59
60
|
attr_accessor(*ATTR_NAMES)
|
61
|
+
attr_reader :important_ids
|
60
62
|
|
61
63
|
def initialize
|
62
64
|
@numeric_font_size = EM
|
65
|
+
@important_ids = []
|
63
66
|
end
|
64
67
|
|
65
68
|
def load_default_stylesheet
|
@@ -70,10 +73,11 @@ module Prawn::SVG
|
|
70
73
|
self
|
71
74
|
end
|
72
75
|
|
73
|
-
def set(name, value)
|
76
|
+
def set(name, value, important: false)
|
74
77
|
name = name.to_s.downcase
|
75
78
|
if (config = PROPERTIES[name])
|
76
|
-
if (value = parse_value(config, value.strip))
|
79
|
+
if (value = parse_value(config, value.strip)) && (important || !@important_ids.include?(config.id))
|
80
|
+
@important_ids << config.id if important
|
77
81
|
instance_variable_set(config.ivar, value)
|
78
82
|
end
|
79
83
|
elsif name == 'font'
|
@@ -99,7 +103,7 @@ module Prawn::SVG
|
|
99
103
|
PROPERTY_CONFIGS.each do |config|
|
100
104
|
value = other.send(config.attr)
|
101
105
|
|
102
|
-
if value && value != 'inherit'
|
106
|
+
if value && value != 'inherit' && (!@important_ids.include?(config.id) || other.important_ids.include?(config.id))
|
103
107
|
instance_variable_set(config.ivar, value)
|
104
108
|
|
105
109
|
elsif value.nil? && !config.inheritable?
|
@@ -107,6 +111,7 @@ module Prawn::SVG
|
|
107
111
|
end
|
108
112
|
end
|
109
113
|
|
114
|
+
@important_ids += other.important_ids
|
110
115
|
@numeric_font_size = calculate_numeric_font_size
|
111
116
|
nil
|
112
117
|
end
|
data/lib/prawn/svg/version.rb
CHANGED
@@ -62,7 +62,6 @@ RSpec.describe Prawn::SVG::CSS::Stylesheets do
|
|
62
62
|
width_and_styles = result.map { |k, v| [k.attributes['width'].to_i, v] }.sort_by(&:first)
|
63
63
|
|
64
64
|
expected = [
|
65
|
-
[0, [['overflow', 'hidden', false]]],
|
66
65
|
[1, [['fill', '#ff0000', false]]],
|
67
66
|
[2,
|
68
67
|
[['fill', '#ff0000', false], ['fill', '#330000', false], ['fill', '#440000', false],
|
@@ -121,7 +120,6 @@ RSpec.describe Prawn::SVG::CSS::Stylesheets do
|
|
121
120
|
it 'scans the document for style tags and adds the style information to the css parser' do
|
122
121
|
css_parser = instance_double(CssParser::Parser)
|
123
122
|
|
124
|
-
expect(css_parser).to receive(:add_block!).with('svg, symbol, image, marker, pattern, foreignObject { overflow: hidden }')
|
125
123
|
expect(css_parser).to receive(:add_block!).with("a\n before>\n x y\n inside <>>\n k j\n after\nz")
|
126
124
|
expect(css_parser).to receive(:add_block!).with('hello')
|
127
125
|
allow(css_parser).to receive(:each_rule_set)
|
@@ -175,8 +175,8 @@ describe Prawn::SVG::Elements::Base do
|
|
175
175
|
</style>
|
176
176
|
<rect width="100" height="100"></rect>
|
177
177
|
<g class="special">
|
178
|
-
<rect width="100" height="100"></rect>
|
179
|
-
<rect width="100" height="100" style="fill: yellow;"></rect>
|
178
|
+
<rect width="100" fill="blue" height="100"></rect>
|
179
|
+
<rect width="100" height="100" fill="blue" style="fill: yellow;"></rect>
|
180
180
|
</g>
|
181
181
|
</svg>
|
182
182
|
SVG
|
@@ -0,0 +1,14 @@
|
|
1
|
+
<?xml version="1.0" encoding="utf-8"?>
|
2
|
+
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="320" height="320">
|
3
|
+
<rect x="10" y="10" width="100" height="100" class="green" id="id" />
|
4
|
+
<text x="10" y="130" fill="green">The square above should be green</text>
|
5
|
+
|
6
|
+
<rect x="10" y="170" width="100" height="100" class="green" style="fill: blue;" />
|
7
|
+
<text x="10" y="290" fill="green">The square above should be also be green</text>
|
8
|
+
|
9
|
+
<style>
|
10
|
+
.green { fill: green !important; }
|
11
|
+
rect.green { fill: red; }
|
12
|
+
#id { fill: orange; }
|
13
|
+
</style>
|
14
|
+
</svg>
|
@@ -0,0 +1,12 @@
|
|
1
|
+
<?xml version="1.0" encoding="utf-8"?>
|
2
|
+
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="320" height="320">
|
3
|
+
<rect x="10" y="10" width="100" height="100" fill="red" />
|
4
|
+
<text x="10" y="130" fill="green">The square above should be green</text>
|
5
|
+
|
6
|
+
<rect x="10" y="170" width="100" height="100" fill="red" style="fill: blue;" />
|
7
|
+
<text x="10" y="290" fill="blue">The square above should be blue</text>
|
8
|
+
|
9
|
+
<style>
|
10
|
+
rect { fill: green; }
|
11
|
+
</style>
|
12
|
+
</svg>
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: prawn-svg
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.37.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mog Nesbitt
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2025-
|
11
|
+
date: 2025-03-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: css_parser
|
@@ -227,6 +227,7 @@ files:
|
|
227
227
|
- spec/sample_svg/image02_base64.svg
|
228
228
|
- spec/sample_svg/image03.svg
|
229
229
|
- spec/sample_svg/image_svg.svg
|
230
|
+
- spec/sample_svg/important.svg
|
230
231
|
- spec/sample_svg/line01.svg
|
231
232
|
- spec/sample_svg/links.svg
|
232
233
|
- spec/sample_svg/marker.svg
|
@@ -244,6 +245,7 @@ files:
|
|
244
245
|
- spec/sample_svg/pie_piece.svg
|
245
246
|
- spec/sample_svg/polygon01.svg
|
246
247
|
- spec/sample_svg/polyline01.svg
|
248
|
+
- spec/sample_svg/presentation_attribute_precedence.svg
|
247
249
|
- spec/sample_svg/preserve-space.svg
|
248
250
|
- spec/sample_svg/quad01.svg
|
249
251
|
- spec/sample_svg/radgrad01-bounding.svg
|