phlex 2.4.1 → 2.4.2
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/lib/phlex/attribute_cache.rb +123 -0
- data/lib/phlex/compiler/method_compiler.rb +49 -23
- data/lib/phlex/fifo.rb +10 -6
- data/lib/phlex/html.rb +43 -13
- data/lib/phlex/sgml/attributes.rb +98 -34
- data/lib/phlex/svg.rb +36 -6
- data/lib/phlex/version.rb +1 -1
- data/lib/phlex.rb +1 -1
- metadata +4 -6
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 66eaca8c2d8a69a3229f154cefa07546afa601fcf4af4a12a4f728b59f4741cd
|
|
4
|
+
data.tar.gz: d9bbd43bf680e7d7788881f855de611fc5128bb5177595d952d5660f770335ff
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 836d6289a3f300b75f4591cb65492fb30d3cf23b93f1bcd3dca69e4928f094a960fa6b05c04a64172b740f84a152b7865ffe3058fb385d804586ba2c108e58b5
|
|
7
|
+
data.tar.gz: 79a8c9896297263149608dbb374b9d7a8bf238bc7cac7f001b9464936619c269929b839fe1d1dc0920a43d87e5cb9da4706b40828801d28b945d3ebdc1dfe1a7
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# @api private
|
|
4
|
+
#
|
|
5
|
+
# Caches serialised attributes. Hash#eql? ignores order, a String's class and
|
|
6
|
+
# whether it's a safe object, any of which can change the output, so a hit
|
|
7
|
+
# must match the frozen snapshot taken when it was cached, in order and class
|
|
8
|
+
# for class. The snapshot interns its Strings, so a frozen literal matches by
|
|
9
|
+
# identity, and a safe object can't, since it can't be frozen before it's
|
|
10
|
+
# extended. Only plain Hashes, Arrays, Strings, SafeValues, Symbols, Integers,
|
|
11
|
+
# true, false and nil are cached; anything else is serialised every time.
|
|
12
|
+
#
|
|
13
|
+
# The snapshot is kept with the output, so it counts against the budget too:
|
|
14
|
+
# each value it holds costs SLOT_BYTESIZE, plus a String's bytes. Attributes
|
|
15
|
+
# whose snapshot and output together cost more than one value may aren't
|
|
16
|
+
# cached, so a large input that renders to little, such as an Array of nils,
|
|
17
|
+
# can't hold memory the budget doesn't see.
|
|
18
|
+
class Phlex::AttributeCache < Phlex::FIFO
|
|
19
|
+
Pairs = Data.define(:items)
|
|
20
|
+
SLOT_BYTESIZE = 8
|
|
21
|
+
|
|
22
|
+
def fetch(attributes)
|
|
23
|
+
digest = attributes.hash
|
|
24
|
+
|
|
25
|
+
if (entry = @store[digest])
|
|
26
|
+
snapshot, value = entry
|
|
27
|
+
return matches?(snapshot, attributes) ? value : yield
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
value = yield
|
|
31
|
+
return value if value.bytesize.zero? || value.bytesize > @max_value_bytesize
|
|
32
|
+
|
|
33
|
+
remaining = [@max_value_bytesize - value.bytesize]
|
|
34
|
+
snapshot = catch(:uncacheable) { snapshot(attributes, remaining) }
|
|
35
|
+
store(digest, snapshot, value, @max_value_bytesize - remaining[0]) unless :uncacheable == snapshot
|
|
36
|
+
value
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def [](attributes)
|
|
40
|
+
snapshot, value = @store[attributes.hash]
|
|
41
|
+
value if snapshot && matches?(snapshot, attributes)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def []=(attributes, value)
|
|
45
|
+
return if value.bytesize > @max_value_bytesize
|
|
46
|
+
|
|
47
|
+
remaining = [@max_value_bytesize - value.bytesize]
|
|
48
|
+
snapshot = catch(:uncacheable) { snapshot(attributes, remaining) }
|
|
49
|
+
store(attributes.hash, snapshot, value, @max_value_bytesize - remaining[0]) unless :uncacheable == snapshot
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# `remaining` holds what's left of the entry's budget, and running out of it
|
|
53
|
+
# stops the walk, so a large input is given up on early.
|
|
54
|
+
private def snapshot(value, remaining)
|
|
55
|
+
throw :uncacheable, :uncacheable if (remaining[0] -= SLOT_BYTESIZE) < 0
|
|
56
|
+
|
|
57
|
+
case value
|
|
58
|
+
when Symbol, Integer, true, false, nil
|
|
59
|
+
value
|
|
60
|
+
when String
|
|
61
|
+
throw :uncacheable, :uncacheable unless String.equal?(value.class) && !(Phlex::SGML::SafeObject === value)
|
|
62
|
+
throw :uncacheable, :uncacheable if (remaining[0] -= value.bytesize) < 0
|
|
63
|
+
-value
|
|
64
|
+
when Phlex::SGML::SafeValue
|
|
65
|
+
throw :uncacheable, :uncacheable unless Phlex::SGML::SafeValue.equal?(value.class) && String.equal?(value.to_s.class)
|
|
66
|
+
throw :uncacheable, :uncacheable if (remaining[0] -= value.to_s.bytesize) < 0
|
|
67
|
+
Phlex::SGML::SafeValue.new(-value.to_s)
|
|
68
|
+
when Hash
|
|
69
|
+
throw :uncacheable, :uncacheable unless Hash.equal?(value.class)
|
|
70
|
+
Pairs.new(value.flat_map { |k, v| [snapshot_key(k, remaining), snapshot(v, remaining)] }.freeze)
|
|
71
|
+
when Array
|
|
72
|
+
throw :uncacheable, :uncacheable unless Array.equal?(value.class)
|
|
73
|
+
value.map { |item| snapshot(item, remaining) }.freeze
|
|
74
|
+
else
|
|
75
|
+
throw :uncacheable, :uncacheable
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# A pair with a nil or false value is skipped when serialising, so its key
|
|
80
|
+
# can be anything, even a cyclic Array.
|
|
81
|
+
private def snapshot_key(key, remaining)
|
|
82
|
+
case key
|
|
83
|
+
when Symbol, String then snapshot(key, remaining)
|
|
84
|
+
else throw :uncacheable, :uncacheable
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
private def matches?(snapshot, value)
|
|
89
|
+
case snapshot
|
|
90
|
+
when Pairs
|
|
91
|
+
items = snapshot.items
|
|
92
|
+
return false unless Hash.equal?(value.class) && value.size * 2 == items.size
|
|
93
|
+
|
|
94
|
+
i = 0
|
|
95
|
+
value.each do |k, v|
|
|
96
|
+
key = items[i]
|
|
97
|
+
item = items[i + 1]
|
|
98
|
+
return false unless key.equal?(k) || matches?(key, k)
|
|
99
|
+
return false unless item.equal?(v) || matches?(item, v)
|
|
100
|
+
i += 2
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
true
|
|
104
|
+
when Array
|
|
105
|
+
return false unless Array.equal?(value.class) && value.size == snapshot.size
|
|
106
|
+
|
|
107
|
+
i = 0
|
|
108
|
+
while i < snapshot.size
|
|
109
|
+
item = snapshot[i]
|
|
110
|
+
return false unless item.equal?(value[i]) || matches?(item, value[i])
|
|
111
|
+
i += 1
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
true
|
|
115
|
+
when String
|
|
116
|
+
String.equal?(value.class) && snapshot.eql?(value) && snapshot.encoding == value.encoding && !(Phlex::SGML::SafeObject === value)
|
|
117
|
+
when Phlex::SGML::SafeValue
|
|
118
|
+
Phlex::SGML::SafeValue.equal?(value.class) && String.equal?(value.to_s.class) && snapshot.to_s.eql?(value.to_s) && snapshot.to_s.encoding == value.to_s.encoding
|
|
119
|
+
else
|
|
120
|
+
snapshot.eql?(value)
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
end
|
|
@@ -120,44 +120,70 @@ module Phlex::Compiler
|
|
|
120
120
|
end
|
|
121
121
|
end
|
|
122
122
|
|
|
123
|
+
# Like the element methods, the opening tag is closed even if serialising
|
|
124
|
+
# its attributes raises, and the closing tag is written even if the
|
|
125
|
+
# content raises, so output after a rescued error isn't read as part of
|
|
126
|
+
# an unclosed tag.
|
|
123
127
|
def compile_standard_element(node, tag)
|
|
124
128
|
node => Refract::CallNode
|
|
125
129
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
130
|
+
attributes = (compile_phlex_attributes(node.arguments) if node.arguments)
|
|
131
|
+
content = (compile_phlex_block(node.block) if node.block)
|
|
132
|
+
|
|
133
|
+
if content
|
|
134
|
+
Refract::StatementsNode.new(
|
|
135
|
+
body: [
|
|
136
|
+
raw("<#{tag}"),
|
|
137
|
+
*opening_tag_attributes(attributes, ">"),
|
|
138
|
+
ensuring([content], [raw("</#{tag}>")]),
|
|
139
|
+
]
|
|
140
|
+
)
|
|
141
|
+
else
|
|
142
|
+
Refract::StatementsNode.new(
|
|
143
|
+
body: [
|
|
144
|
+
raw("<#{tag}"),
|
|
145
|
+
*opening_tag_attributes(attributes, "></#{tag}>"),
|
|
146
|
+
]
|
|
147
|
+
)
|
|
148
|
+
end
|
|
143
149
|
end
|
|
144
150
|
|
|
145
151
|
def compile_void_element(node, tag)
|
|
146
152
|
node => Refract::CallNode
|
|
147
153
|
|
|
154
|
+
attributes = (compile_phlex_attributes(node.arguments) if node.arguments)
|
|
155
|
+
|
|
148
156
|
Refract::StatementsNode.new(
|
|
149
157
|
body: [
|
|
150
158
|
raw("<#{tag}"),
|
|
151
|
-
*(
|
|
152
|
-
if node.arguments
|
|
153
|
-
compile_phlex_attributes(node.arguments)
|
|
154
|
-
end
|
|
155
|
-
),
|
|
156
|
-
raw(">"),
|
|
159
|
+
*opening_tag_attributes(attributes, ">"),
|
|
157
160
|
]
|
|
158
161
|
)
|
|
159
162
|
end
|
|
160
163
|
|
|
164
|
+
# Attributes serialised now can't raise, so only a runtime call needs the ensure.
|
|
165
|
+
private def opening_tag_attributes(attributes, closing)
|
|
166
|
+
case attributes
|
|
167
|
+
when nil
|
|
168
|
+
[raw(closing)]
|
|
169
|
+
when Refract::CallNode
|
|
170
|
+
[ensuring([attributes], [raw(closing)])]
|
|
171
|
+
else
|
|
172
|
+
[attributes, raw(closing)]
|
|
173
|
+
end
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
private def ensuring(statements, ensured)
|
|
177
|
+
Refract::BeginNode.new(
|
|
178
|
+
statements: Refract::StatementsNode.new(body: statements),
|
|
179
|
+
rescue_clause: nil,
|
|
180
|
+
else_clause: nil,
|
|
181
|
+
ensure_clause: Refract::EnsureNode.new(
|
|
182
|
+
statements: Refract::StatementsNode.new(body: ensured)
|
|
183
|
+
)
|
|
184
|
+
)
|
|
185
|
+
end
|
|
186
|
+
|
|
161
187
|
def compile_phlex_attributes(node)
|
|
162
188
|
arguments = node.arguments
|
|
163
189
|
|
data/lib/phlex/fifo.rb
CHANGED
|
@@ -24,20 +24,24 @@ class Phlex::FIFO
|
|
|
24
24
|
end
|
|
25
25
|
|
|
26
26
|
def []=(key, value)
|
|
27
|
-
|
|
27
|
+
store(key.hash, key, value)
|
|
28
|
+
end
|
|
28
29
|
|
|
29
|
-
|
|
30
|
+
# `bytesize` is what the entry counts against the budget, which is the
|
|
31
|
+
# value's unless the caller also counts its key.
|
|
32
|
+
private def store(digest, key, value, bytesize = value.bytesize)
|
|
33
|
+
return if bytesize > @max_value_bytesize
|
|
30
34
|
|
|
31
35
|
@mutex.synchronize do
|
|
32
36
|
# Check the key definitely doesn't exist now we have the lock
|
|
33
37
|
return if @store[digest]
|
|
34
38
|
|
|
35
|
-
@store[digest] = [key, value].freeze
|
|
36
|
-
@bytesize +=
|
|
39
|
+
@store[digest] = [key, value, bytesize].freeze
|
|
40
|
+
@bytesize += bytesize
|
|
37
41
|
|
|
38
42
|
while @bytesize > @max_bytesize
|
|
39
|
-
_k,
|
|
40
|
-
@bytesize -=
|
|
43
|
+
_k, entry = @store.shift
|
|
44
|
+
@bytesize -= entry[2]
|
|
41
45
|
end
|
|
42
46
|
end
|
|
43
47
|
end
|
data/lib/phlex/html.rb
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
class Phlex::HTML < Phlex::SGML
|
|
4
|
+
# Elements `tag` won't render, since its name may come from a user.
|
|
5
|
+
UNSAFE_TAGS = Set.new(%w[script style base]).freeze
|
|
6
|
+
|
|
4
7
|
extend Phlex::SGML::Elements
|
|
5
8
|
include VoidElements, StandardElements
|
|
6
9
|
|
|
@@ -55,28 +58,50 @@ class Phlex::HTML < Phlex::SGML
|
|
|
55
58
|
raise Phlex::ArgumentError.new("Expected the tag name to be a Symbol.")
|
|
56
59
|
end
|
|
57
60
|
|
|
61
|
+
# The name passed to `tag` may come from a user, so it can't render an
|
|
62
|
+
# element whose content or attributes run as code or change where code loads from.
|
|
63
|
+
if (unsafe_tag = StandardElements.__registered_elements__[name] || VoidElements.__registered_elements__[name]) && UNSAFE_TAGS.include?(unsafe_tag)
|
|
64
|
+
raise Phlex::ArgumentError.new("`tag` can't render <#{unsafe_tag}>. Call `#{unsafe_tag}` directly instead.")
|
|
65
|
+
end
|
|
66
|
+
|
|
58
67
|
if (tag = StandardElements.__registered_elements__[name]) || ((tag = name.name.tr("_", "-")).include?("-") && tag.match?(/\A[a-z0-9-]+\z/))
|
|
59
68
|
if attributes.length > 0 # with attributes
|
|
60
69
|
if block_given # with content block
|
|
61
|
-
buffer << "<#{tag}"
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
70
|
+
buffer << "<#{tag}"
|
|
71
|
+
begin
|
|
72
|
+
buffer << (Phlex::ATTRIBUTE_CACHE[attributes] ||= Phlex::SGML::Attributes.generate_attributes(attributes))
|
|
73
|
+
ensure
|
|
74
|
+
buffer << ">"
|
|
75
|
+
end
|
|
76
|
+
begin
|
|
77
|
+
if tag == "svg"
|
|
78
|
+
render Phlex::SVG.new(&)
|
|
79
|
+
else
|
|
80
|
+
__yield_content__(&)
|
|
81
|
+
end
|
|
82
|
+
ensure
|
|
83
|
+
buffer << "</#{tag}>"
|
|
66
84
|
end
|
|
67
|
-
buffer << "</#{tag}>"
|
|
68
85
|
else # without content
|
|
69
|
-
buffer << "<#{tag}"
|
|
86
|
+
buffer << "<#{tag}"
|
|
87
|
+
begin
|
|
88
|
+
buffer << (::Phlex::ATTRIBUTE_CACHE[attributes] ||= Phlex::SGML::Attributes.generate_attributes(attributes))
|
|
89
|
+
ensure
|
|
90
|
+
buffer << "></#{tag}>"
|
|
91
|
+
end
|
|
70
92
|
end
|
|
71
93
|
else # without attributes
|
|
72
94
|
if block_given # with content block
|
|
73
95
|
buffer << ("<#{tag}>")
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
96
|
+
begin
|
|
97
|
+
if tag == "svg"
|
|
98
|
+
render Phlex::SVG.new(&)
|
|
99
|
+
else
|
|
100
|
+
__yield_content__(&)
|
|
101
|
+
end
|
|
102
|
+
ensure
|
|
103
|
+
buffer << "</#{tag}>"
|
|
78
104
|
end
|
|
79
|
-
buffer << "</#{tag}>"
|
|
80
105
|
else # without content
|
|
81
106
|
buffer << "<#{tag}></#{tag}>"
|
|
82
107
|
end
|
|
@@ -87,7 +112,12 @@ class Phlex::HTML < Phlex::SGML
|
|
|
87
112
|
end
|
|
88
113
|
|
|
89
114
|
if attributes.length > 0 # with attributes
|
|
90
|
-
buffer << "<#{tag}"
|
|
115
|
+
buffer << "<#{tag}"
|
|
116
|
+
begin
|
|
117
|
+
buffer << (::Phlex::ATTRIBUTE_CACHE[attributes] ||= Phlex::SGML::Attributes.generate_attributes(attributes))
|
|
118
|
+
ensure
|
|
119
|
+
buffer << ">"
|
|
120
|
+
end
|
|
91
121
|
else # without attributes
|
|
92
122
|
buffer << "<#{tag}>"
|
|
93
123
|
end
|
|
@@ -4,13 +4,27 @@ module Phlex::SGML::Attributes
|
|
|
4
4
|
extend self
|
|
5
5
|
|
|
6
6
|
UNSAFE_ATTRIBUTES = Set.new(%w[srcdoc sandbox http-equiv]).freeze
|
|
7
|
-
|
|
7
|
+
# SVG animation elements, such as `set` and `animate`, write `to`, `from`,
|
|
8
|
+
# `by` and `values` to the attribute their `attributeName` names, which can
|
|
9
|
+
# be `href`, so those are checked as references too.
|
|
10
|
+
REF_ATTRIBUTES = Set.new(%w[href src action formaction lowsrc dynsrc background ping xlinkhref to from by values]).freeze
|
|
11
|
+
# References whose value is a `;`-separated list, any entry of which can be used.
|
|
12
|
+
REF_LIST_ATTRIBUTES = Set.new(%w[values]).freeze
|
|
13
|
+
# `data` takes a URL on `object`, but a Hash given to it expands into `data-*`
|
|
14
|
+
# attributes, so it's checked as a reference only when its value doesn't expand.
|
|
15
|
+
DATA_ATTRIBUTE = "data"
|
|
8
16
|
NAMED_CHARACTER_REFERENCES = {
|
|
9
17
|
"colon" => ":",
|
|
10
18
|
"tab" => "\t",
|
|
11
19
|
"newline" => "\n",
|
|
20
|
+
"semi" => ";",
|
|
12
21
|
}.freeze
|
|
13
22
|
UNSAFE_ATTRIBUTE_NAME_CHARS = %r([<>&"'/=\s\x00])
|
|
23
|
+
# Escaped in attribute values. `"` would end the value, and `<` or `>` could
|
|
24
|
+
# end an element whose content isn't parsed as markup, such as `</noscript>`,
|
|
25
|
+
# or a comment, with `-->`. Escaping them doesn't change the value the browser reads.
|
|
26
|
+
UNSAFE_VALUE_CHARS = /["<>]/
|
|
27
|
+
VALUE_ESCAPES = { '"' => """, "<" => "<", ">" => ">" }.freeze
|
|
14
28
|
|
|
15
29
|
def generate_attributes(attributes, buffer = +"")
|
|
16
30
|
attributes.each do |k, v|
|
|
@@ -26,9 +40,9 @@ module Phlex::SGML::Attributes
|
|
|
26
40
|
when true
|
|
27
41
|
true
|
|
28
42
|
when String
|
|
29
|
-
v.gsub(
|
|
43
|
+
v.gsub(UNSAFE_VALUE_CHARS, VALUE_ESCAPES)
|
|
30
44
|
when Symbol
|
|
31
|
-
v.name.tr("_", "-").gsub(
|
|
45
|
+
v.name.tr("_", "-").gsub(UNSAFE_VALUE_CHARS, VALUE_ESCAPES)
|
|
32
46
|
when Integer, Float
|
|
33
47
|
v.to_s
|
|
34
48
|
when Date
|
|
@@ -38,26 +52,26 @@ module Phlex::SGML::Attributes
|
|
|
38
52
|
when Hash
|
|
39
53
|
case k
|
|
40
54
|
when :style
|
|
41
|
-
generate_styles(v).gsub(
|
|
55
|
+
generate_styles(v).gsub(UNSAFE_VALUE_CHARS, VALUE_ESCAPES)
|
|
42
56
|
else
|
|
43
57
|
generate_nested_attributes(v, "#{name}-", buffer)
|
|
44
58
|
end
|
|
45
59
|
when Array
|
|
46
60
|
case k
|
|
47
61
|
when :style
|
|
48
|
-
generate_styles(v).gsub(
|
|
62
|
+
generate_styles(v).gsub(UNSAFE_VALUE_CHARS, VALUE_ESCAPES)
|
|
49
63
|
else
|
|
50
64
|
generate_nested_tokens(v)
|
|
51
65
|
end
|
|
52
66
|
when Set
|
|
53
67
|
case k
|
|
54
68
|
when :style
|
|
55
|
-
generate_styles(v).gsub(
|
|
69
|
+
generate_styles(v).gsub(UNSAFE_VALUE_CHARS, VALUE_ESCAPES)
|
|
56
70
|
else
|
|
57
71
|
generate_nested_tokens(v.to_a)
|
|
58
72
|
end
|
|
59
73
|
when Phlex::SGML::SafeObject
|
|
60
|
-
v.to_s.gsub(
|
|
74
|
+
v.to_s.gsub(UNSAFE_VALUE_CHARS, VALUE_ESCAPES)
|
|
61
75
|
else
|
|
62
76
|
if v.respond_to?(:to_h)
|
|
63
77
|
generate_nested_attributes(v.to_h, "#{name}-", buffer)
|
|
@@ -68,20 +82,29 @@ module Phlex::SGML::Attributes
|
|
|
68
82
|
|
|
69
83
|
lower_name = name.downcase
|
|
70
84
|
|
|
71
|
-
|
|
85
|
+
# Only Phlex's own `safe` can use an unsafe name or a script URL. Other safe
|
|
86
|
+
# objects, such as Rails' html_safe strings, are safe as HTML, not as script.
|
|
87
|
+
unless Phlex::SGML::SafeValue === v
|
|
72
88
|
normalized_name = lower_name.delete("^a-z-")
|
|
73
89
|
|
|
74
|
-
|
|
90
|
+
# In an SVG document parsed as XML, any prefix can be bound to XLink,
|
|
91
|
+
# so whether a name is a reference depends on its local name.
|
|
92
|
+
reference_name = local_name(lower_name)
|
|
93
|
+
|
|
94
|
+
if value != true && (REF_ATTRIBUTES.include?(reference_name) || DATA_ATTRIBUTE == reference_name)
|
|
75
95
|
case value
|
|
76
96
|
when String
|
|
77
|
-
|
|
97
|
+
unsafe = REF_LIST_ATTRIBUTES.include?(reference_name) ? unsafe_reference_list?(value) : unsafe_reference?(value)
|
|
78
98
|
|
|
79
|
-
if
|
|
99
|
+
if unsafe
|
|
80
100
|
# We just ignore these because they were likely not specified by the developer.
|
|
81
101
|
next
|
|
82
102
|
end
|
|
83
103
|
else
|
|
84
|
-
|
|
104
|
+
# A `data` Hash has already expanded into `data-*` attributes.
|
|
105
|
+
unless DATA_ATTRIBUTE == reference_name
|
|
106
|
+
raise Phlex::ArgumentError.new("Invalid attribute value for #{k}: #{v.inspect}.")
|
|
107
|
+
end
|
|
85
108
|
end
|
|
86
109
|
end
|
|
87
110
|
|
|
@@ -137,25 +160,26 @@ module Phlex::SGML::Attributes
|
|
|
137
160
|
|
|
138
161
|
case v
|
|
139
162
|
when true
|
|
140
|
-
buffer << " " << base_name
|
|
163
|
+
buffer << " " << checked_nested_name(base_name, name)
|
|
141
164
|
when String
|
|
142
|
-
buffer << " " << base_name
|
|
165
|
+
buffer << " " << checked_nested_name(base_name, name) << '="' << v.gsub(UNSAFE_VALUE_CHARS, VALUE_ESCAPES) << '"'
|
|
143
166
|
when Symbol
|
|
144
|
-
buffer << " " << base_name
|
|
167
|
+
buffer << " " << checked_nested_name(base_name, name) << '="' << v.name.tr("_", "-").gsub(UNSAFE_VALUE_CHARS, VALUE_ESCAPES) << '"'
|
|
145
168
|
when Integer, Float
|
|
146
|
-
buffer << " " << base_name
|
|
169
|
+
buffer << " " << checked_nested_name(base_name, name) << '="' << v.to_s << '"'
|
|
147
170
|
when Hash
|
|
148
171
|
generate_nested_attributes(v, "#{base_name}#{name}-", buffer)
|
|
149
172
|
when Array
|
|
150
173
|
if (value = generate_nested_tokens(v))
|
|
151
|
-
buffer << " " << base_name
|
|
174
|
+
buffer << " " << checked_nested_name(base_name, name) << '="' << value << '"'
|
|
152
175
|
end
|
|
153
176
|
when Set
|
|
154
177
|
if (value = generate_nested_tokens(v.to_a))
|
|
155
|
-
buffer << " " << base_name
|
|
178
|
+
buffer << " " << checked_nested_name(base_name, name) << '="' << value << '"'
|
|
156
179
|
end
|
|
157
180
|
when Phlex::SGML::SafeObject
|
|
158
|
-
|
|
181
|
+
nested_name = (Phlex::SGML::SafeValue === v) ? "#{base_name}#{name}" : checked_nested_name(base_name, name)
|
|
182
|
+
buffer << " " << nested_name << '="' << v.to_s.gsub(UNSAFE_VALUE_CHARS, VALUE_ESCAPES) << '"'
|
|
159
183
|
else
|
|
160
184
|
raise Phlex::ArgumentError.new("Invalid attribute value #{v.inspect}.")
|
|
161
185
|
end
|
|
@@ -168,27 +192,67 @@ module Phlex::SGML::Attributes
|
|
|
168
192
|
end
|
|
169
193
|
end
|
|
170
194
|
|
|
195
|
+
# The normalised name without any namespace prefix.
|
|
196
|
+
def local_name(lower_name)
|
|
197
|
+
(index = lower_name.rindex(":")) ? lower_name[(index + 1)..].delete("^a-z-") : lower_name.delete("^a-z-")
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
# Joined to its base, a nested name can spell an unsafe one, such as `http-equiv`.
|
|
201
|
+
private def checked_nested_name(base_name, name)
|
|
202
|
+
full_name = "#{base_name}#{name}"
|
|
203
|
+
|
|
204
|
+
if UNSAFE_ATTRIBUTES.include?(full_name.downcase.delete("^a-z-"))
|
|
205
|
+
raise Phlex::ArgumentError.new("Unsafe attribute name detected: #{full_name}.")
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
# Nested values aren't checked as references, so a nested name can't be one,
|
|
209
|
+
# as `x: { "a:href" => }` would be with its prefix bound to XLink.
|
|
210
|
+
if full_name.include?(":") && REF_ATTRIBUTES.include?(local_name(full_name.downcase))
|
|
211
|
+
raise Phlex::ArgumentError.new("Unsafe attribute name detected: #{full_name}.")
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
full_name
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
# Detects `javascript:` URLs, including ones obfuscated with HTML character
|
|
218
|
+
# references, whitespace or case.
|
|
219
|
+
def unsafe_reference?(value)
|
|
220
|
+
decode_html_character_references(value).downcase.delete("^a-z:").start_with?("javascript:")
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
# Like `unsafe_reference?`, for a `;`-separated list such as `values`, where
|
|
224
|
+
# any entry can be used. The decoder removes references it doesn't know,
|
|
225
|
+
# but a browser keeps them as text, so it could see a `;` the decoder
|
|
226
|
+
# removed. Removing text can't break up an entry's `javascript:`, so the
|
|
227
|
+
# list is unsafe if `javascript:` appears anywhere in it.
|
|
228
|
+
def unsafe_reference_list?(value)
|
|
229
|
+
decode_html_character_references(value).downcase.delete("^a-z:").include?("javascript:")
|
|
230
|
+
end
|
|
231
|
+
|
|
171
232
|
def decode_html_character_references(value)
|
|
172
233
|
value
|
|
173
|
-
.gsub(/&#x([0-9a-f]+);?/i) {
|
|
174
|
-
|
|
175
|
-
[$1.to_i(16)].pack("U*")
|
|
176
|
-
rescue
|
|
177
|
-
""
|
|
178
|
-
end
|
|
179
|
-
}
|
|
180
|
-
.gsub(/&#(\d+);?/) {
|
|
181
|
-
begin
|
|
182
|
-
[$1.to_i].pack("U*")
|
|
183
|
-
rescue
|
|
184
|
-
""
|
|
185
|
-
end
|
|
186
|
-
}
|
|
234
|
+
.gsub(/&#x([0-9a-f]+);?/i) { numeric_character_reference($1, 16, 6) }
|
|
235
|
+
.gsub(/&#(\d+);?/) { numeric_character_reference($1, 10, 7) }
|
|
187
236
|
.gsub(/&([a-z][a-z0-9]+);?/i) {
|
|
188
237
|
NAMED_CHARACTER_REFERENCES[$1.downcase] || ""
|
|
189
238
|
}
|
|
190
239
|
end
|
|
191
240
|
|
|
241
|
+
# Digits past what U+10FFFF needs, once leading zeros are dropped, can only
|
|
242
|
+
# be past Unicode, so they're decoded as U+FFFD without parsing them.
|
|
243
|
+
private def numeric_character_reference(digits, base, max_length)
|
|
244
|
+
digits = digits.sub(/\A0+(?=.)/, "")
|
|
245
|
+
(digits.length > max_length) ? "\uFFFD" : character_reference(digits.to_i(base))
|
|
246
|
+
end
|
|
247
|
+
|
|
248
|
+
# Browsers decode surrogates and code points past Unicode as U+FFFD.
|
|
249
|
+
private def character_reference(code_point)
|
|
250
|
+
case code_point
|
|
251
|
+
in 0xD800..0xDFFF | 0x110000.. then "\uFFFD"
|
|
252
|
+
else code_point.chr(Encoding::UTF_8)
|
|
253
|
+
end
|
|
254
|
+
end
|
|
255
|
+
|
|
192
256
|
def generate_nested_tokens(tokens, sep = " ", gsub_from = nil, gsub_to = "")
|
|
193
257
|
buffer = +""
|
|
194
258
|
|
|
@@ -244,7 +308,7 @@ module Phlex::SGML::Attributes
|
|
|
244
308
|
|
|
245
309
|
return if buffer.empty?
|
|
246
310
|
|
|
247
|
-
buffer.gsub(
|
|
311
|
+
buffer.gsub(UNSAFE_VALUE_CHARS, VALUE_ESCAPES)
|
|
248
312
|
end
|
|
249
313
|
|
|
250
314
|
# The result is unsafe so should be escaped.
|
data/lib/phlex/svg.rb
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
class Phlex::SVG < Phlex::SGML
|
|
4
|
+
# Elements `tag` won't render, since its name may come from a user.
|
|
5
|
+
UNSAFE_TAGS = Set.new(%w[script style]).freeze
|
|
6
|
+
|
|
4
7
|
include StandardElements
|
|
5
8
|
|
|
6
9
|
# Returns the string "image/svg+xml"
|
|
@@ -13,6 +16,11 @@ class Phlex::SVG < Phlex::SGML
|
|
|
13
16
|
nil
|
|
14
17
|
end
|
|
15
18
|
|
|
19
|
+
# Output a CDATA section, whose content isn't escaped.
|
|
20
|
+
#
|
|
21
|
+
# @note A CDATA section is only safe inside an `<svg>` element, or in an SVG
|
|
22
|
+
# document. In HTML outside one, a browser reads it as a comment that ends
|
|
23
|
+
# at the first `>`, so the rest of its content would be parsed as markup.
|
|
16
24
|
def cdata(content = nil, &block)
|
|
17
25
|
state = @_state
|
|
18
26
|
return unless state.should_render?
|
|
@@ -41,20 +49,42 @@ class Phlex::SVG < Phlex::SGML
|
|
|
41
49
|
raise Phlex::ArgumentError.new("Expected the tag name to be a Symbol.")
|
|
42
50
|
end
|
|
43
51
|
|
|
52
|
+
# The name passed to `tag` may come from a user, so it can't render an
|
|
53
|
+
# element whose content or attributes run as code or change where code loads from.
|
|
54
|
+
if (unsafe_tag = StandardElements.__registered_elements__[name]) && UNSAFE_TAGS.include?(unsafe_tag)
|
|
55
|
+
raise Phlex::ArgumentError.new("`tag` can't render <#{unsafe_tag}>. Call `#{unsafe_tag}` directly instead.")
|
|
56
|
+
end
|
|
57
|
+
|
|
44
58
|
if (tag = StandardElements.__registered_elements__[name]) || ((tag = name.name.tr("_", "-")).include?("-") && tag.match?(/\A[a-z0-9-]+\z/))
|
|
45
59
|
if attributes.length > 0 # with attributes
|
|
46
60
|
if block_given # with content block
|
|
47
|
-
buffer << "<#{tag}"
|
|
48
|
-
|
|
49
|
-
|
|
61
|
+
buffer << "<#{tag}"
|
|
62
|
+
begin
|
|
63
|
+
buffer << (Phlex::ATTRIBUTE_CACHE[attributes] ||= Phlex::SGML::Attributes.generate_attributes(attributes))
|
|
64
|
+
ensure
|
|
65
|
+
buffer << ">"
|
|
66
|
+
end
|
|
67
|
+
begin
|
|
68
|
+
__yield_content__(&)
|
|
69
|
+
ensure
|
|
70
|
+
buffer << "</#{tag}>"
|
|
71
|
+
end
|
|
50
72
|
else # without content
|
|
51
|
-
buffer << "<#{tag}"
|
|
73
|
+
buffer << "<#{tag}"
|
|
74
|
+
begin
|
|
75
|
+
buffer << (::Phlex::ATTRIBUTE_CACHE[attributes] ||= Phlex::SGML::Attributes.generate_attributes(attributes))
|
|
76
|
+
ensure
|
|
77
|
+
buffer << "></#{tag}>"
|
|
78
|
+
end
|
|
52
79
|
end
|
|
53
80
|
else # without attributes
|
|
54
81
|
if block_given # with content block
|
|
55
82
|
buffer << ("<#{tag}>")
|
|
56
|
-
|
|
57
|
-
|
|
83
|
+
begin
|
|
84
|
+
__yield_content__(&)
|
|
85
|
+
ensure
|
|
86
|
+
buffer << "</#{tag}>"
|
|
87
|
+
end
|
|
58
88
|
else # without content
|
|
59
89
|
buffer << "<#{tag}></#{tag}>"
|
|
60
90
|
end
|
data/lib/phlex/version.rb
CHANGED
data/lib/phlex.rb
CHANGED
|
@@ -25,7 +25,7 @@ module Phlex
|
|
|
25
25
|
|
|
26
26
|
DEPLOYED_AT = Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond)
|
|
27
27
|
CACHED_FILES = Set.new
|
|
28
|
-
ATTRIBUTE_CACHE =
|
|
28
|
+
ATTRIBUTE_CACHE = AttributeCache.new
|
|
29
29
|
|
|
30
30
|
UNBOUND_INSTANCE_METHOD_METHOD = Module.instance_method(:instance_method)
|
|
31
31
|
|
metadata
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: phlex
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 2.4.
|
|
4
|
+
version: 2.4.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Joel Drapper
|
|
8
8
|
- Will Cosgrove
|
|
9
|
-
autorequire:
|
|
10
9
|
bindir: bin
|
|
11
10
|
cert_chain: []
|
|
12
|
-
date:
|
|
11
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
13
12
|
dependencies:
|
|
14
13
|
- !ruby/object:Gem::Dependency
|
|
15
14
|
name: zeitwerk
|
|
@@ -49,6 +48,7 @@ files:
|
|
|
49
48
|
- LICENSE.txt
|
|
50
49
|
- README.md
|
|
51
50
|
- lib/phlex.rb
|
|
51
|
+
- lib/phlex/attribute_cache.rb
|
|
52
52
|
- lib/phlex/compiler.rb
|
|
53
53
|
- lib/phlex/compiler/class_compiler.rb
|
|
54
54
|
- lib/phlex/compiler/compactor.rb
|
|
@@ -86,7 +86,6 @@ metadata:
|
|
|
86
86
|
changelog_uri: https://github.com/phlex-ruby/phlex/releases
|
|
87
87
|
funding_uri: https://github.com/sponsors/joeldrapper
|
|
88
88
|
rubygems_mfa_required: 'true'
|
|
89
|
-
post_install_message:
|
|
90
89
|
rdoc_options: []
|
|
91
90
|
require_paths:
|
|
92
91
|
- lib
|
|
@@ -101,8 +100,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
101
100
|
- !ruby/object:Gem::Version
|
|
102
101
|
version: '0'
|
|
103
102
|
requirements: []
|
|
104
|
-
rubygems_version:
|
|
105
|
-
signing_key:
|
|
103
|
+
rubygems_version: 4.0.20
|
|
106
104
|
specification_version: 4
|
|
107
105
|
summary: Object-oriented views in Ruby.
|
|
108
106
|
test_files: []
|