phlex 2.4.0 → 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 +44 -14
- data/lib/phlex/sgml/attributes.rb +114 -21
- data/lib/phlex/svg.rb +37 -7
- data/lib/phlex/version.rb +1 -1
- data/lib/phlex.rb +1 -1
- metadata +3 -2
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
|
|
|
58
|
-
|
|
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
|
+
|
|
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,7 +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"
|
|
16
|
+
NAMED_CHARACTER_REFERENCES = {
|
|
17
|
+
"colon" => ":",
|
|
18
|
+
"tab" => "\t",
|
|
19
|
+
"newline" => "\n",
|
|
20
|
+
"semi" => ";",
|
|
21
|
+
}.freeze
|
|
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
|
|
8
28
|
|
|
9
29
|
def generate_attributes(attributes, buffer = +"")
|
|
10
30
|
attributes.each do |k, v|
|
|
@@ -20,9 +40,9 @@ module Phlex::SGML::Attributes
|
|
|
20
40
|
when true
|
|
21
41
|
true
|
|
22
42
|
when String
|
|
23
|
-
v.gsub(
|
|
43
|
+
v.gsub(UNSAFE_VALUE_CHARS, VALUE_ESCAPES)
|
|
24
44
|
when Symbol
|
|
25
|
-
v.name.tr("_", "-").gsub(
|
|
45
|
+
v.name.tr("_", "-").gsub(UNSAFE_VALUE_CHARS, VALUE_ESCAPES)
|
|
26
46
|
when Integer, Float
|
|
27
47
|
v.to_s
|
|
28
48
|
when Date
|
|
@@ -32,26 +52,26 @@ module Phlex::SGML::Attributes
|
|
|
32
52
|
when Hash
|
|
33
53
|
case k
|
|
34
54
|
when :style
|
|
35
|
-
generate_styles(v).gsub(
|
|
55
|
+
generate_styles(v).gsub(UNSAFE_VALUE_CHARS, VALUE_ESCAPES)
|
|
36
56
|
else
|
|
37
57
|
generate_nested_attributes(v, "#{name}-", buffer)
|
|
38
58
|
end
|
|
39
59
|
when Array
|
|
40
60
|
case k
|
|
41
61
|
when :style
|
|
42
|
-
generate_styles(v).gsub(
|
|
62
|
+
generate_styles(v).gsub(UNSAFE_VALUE_CHARS, VALUE_ESCAPES)
|
|
43
63
|
else
|
|
44
64
|
generate_nested_tokens(v)
|
|
45
65
|
end
|
|
46
66
|
when Set
|
|
47
67
|
case k
|
|
48
68
|
when :style
|
|
49
|
-
generate_styles(v).gsub(
|
|
69
|
+
generate_styles(v).gsub(UNSAFE_VALUE_CHARS, VALUE_ESCAPES)
|
|
50
70
|
else
|
|
51
71
|
generate_nested_tokens(v.to_a)
|
|
52
72
|
end
|
|
53
73
|
when Phlex::SGML::SafeObject
|
|
54
|
-
v.to_s.gsub(
|
|
74
|
+
v.to_s.gsub(UNSAFE_VALUE_CHARS, VALUE_ESCAPES)
|
|
55
75
|
else
|
|
56
76
|
if v.respond_to?(:to_h)
|
|
57
77
|
generate_nested_attributes(v.to_h, "#{name}-", buffer)
|
|
@@ -62,18 +82,29 @@ module Phlex::SGML::Attributes
|
|
|
62
82
|
|
|
63
83
|
lower_name = name.downcase
|
|
64
84
|
|
|
65
|
-
|
|
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
|
|
66
88
|
normalized_name = lower_name.delete("^a-z-")
|
|
67
89
|
|
|
68
|
-
|
|
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)
|
|
69
95
|
case value
|
|
70
96
|
when String
|
|
71
|
-
|
|
97
|
+
unsafe = REF_LIST_ATTRIBUTES.include?(reference_name) ? unsafe_reference_list?(value) : unsafe_reference?(value)
|
|
98
|
+
|
|
99
|
+
if unsafe
|
|
72
100
|
# We just ignore these because they were likely not specified by the developer.
|
|
73
101
|
next
|
|
74
102
|
end
|
|
75
103
|
else
|
|
76
|
-
|
|
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
|
|
77
108
|
end
|
|
78
109
|
end
|
|
79
110
|
|
|
@@ -86,7 +117,7 @@ module Phlex::SGML::Attributes
|
|
|
86
117
|
end
|
|
87
118
|
end
|
|
88
119
|
|
|
89
|
-
if name.match?(
|
|
120
|
+
if name.match?(UNSAFE_ATTRIBUTE_NAME_CHARS)
|
|
90
121
|
raise Phlex::ArgumentError.new("Unsafe attribute name detected: #{k}.")
|
|
91
122
|
end
|
|
92
123
|
|
|
@@ -122,32 +153,33 @@ module Phlex::SGML::Attributes
|
|
|
122
153
|
else raise Phlex::ArgumentError.new("Attribute keys should be Strings or Symbols")
|
|
123
154
|
end
|
|
124
155
|
|
|
125
|
-
if name.match?(
|
|
156
|
+
if name.match?(UNSAFE_ATTRIBUTE_NAME_CHARS)
|
|
126
157
|
raise Phlex::ArgumentError.new("Unsafe attribute name detected: #{k}.")
|
|
127
158
|
end
|
|
128
159
|
end
|
|
129
160
|
|
|
130
161
|
case v
|
|
131
162
|
when true
|
|
132
|
-
buffer << " " << base_name
|
|
163
|
+
buffer << " " << checked_nested_name(base_name, name)
|
|
133
164
|
when String
|
|
134
|
-
buffer << " " << base_name
|
|
165
|
+
buffer << " " << checked_nested_name(base_name, name) << '="' << v.gsub(UNSAFE_VALUE_CHARS, VALUE_ESCAPES) << '"'
|
|
135
166
|
when Symbol
|
|
136
|
-
buffer << " " << base_name
|
|
167
|
+
buffer << " " << checked_nested_name(base_name, name) << '="' << v.name.tr("_", "-").gsub(UNSAFE_VALUE_CHARS, VALUE_ESCAPES) << '"'
|
|
137
168
|
when Integer, Float
|
|
138
|
-
buffer << " " << base_name
|
|
169
|
+
buffer << " " << checked_nested_name(base_name, name) << '="' << v.to_s << '"'
|
|
139
170
|
when Hash
|
|
140
171
|
generate_nested_attributes(v, "#{base_name}#{name}-", buffer)
|
|
141
172
|
when Array
|
|
142
173
|
if (value = generate_nested_tokens(v))
|
|
143
|
-
buffer << " " << base_name
|
|
174
|
+
buffer << " " << checked_nested_name(base_name, name) << '="' << value << '"'
|
|
144
175
|
end
|
|
145
176
|
when Set
|
|
146
177
|
if (value = generate_nested_tokens(v.to_a))
|
|
147
|
-
buffer << " " << base_name
|
|
178
|
+
buffer << " " << checked_nested_name(base_name, name) << '="' << value << '"'
|
|
148
179
|
end
|
|
149
180
|
when Phlex::SGML::SafeObject
|
|
150
|
-
|
|
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) << '"'
|
|
151
183
|
else
|
|
152
184
|
raise Phlex::ArgumentError.new("Invalid attribute value #{v.inspect}.")
|
|
153
185
|
end
|
|
@@ -160,6 +192,67 @@ module Phlex::SGML::Attributes
|
|
|
160
192
|
end
|
|
161
193
|
end
|
|
162
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
|
+
|
|
232
|
+
def decode_html_character_references(value)
|
|
233
|
+
value
|
|
234
|
+
.gsub(/&#x([0-9a-f]+);?/i) { numeric_character_reference($1, 16, 6) }
|
|
235
|
+
.gsub(/&#(\d+);?/) { numeric_character_reference($1, 10, 7) }
|
|
236
|
+
.gsub(/&([a-z][a-z0-9]+);?/i) {
|
|
237
|
+
NAMED_CHARACTER_REFERENCES[$1.downcase] || ""
|
|
238
|
+
}
|
|
239
|
+
end
|
|
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
|
+
|
|
163
256
|
def generate_nested_tokens(tokens, sep = " ", gsub_from = nil, gsub_to = "")
|
|
164
257
|
buffer = +""
|
|
165
258
|
|
|
@@ -215,7 +308,7 @@ module Phlex::SGML::Attributes
|
|
|
215
308
|
|
|
216
309
|
return if buffer.empty?
|
|
217
310
|
|
|
218
|
-
buffer.gsub(
|
|
311
|
+
buffer.gsub(UNSAFE_VALUE_CHARS, VALUE_ESCAPES)
|
|
219
312
|
end
|
|
220
313
|
|
|
221
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
|
|
|
44
|
-
|
|
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
|
+
|
|
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,7 +1,7 @@
|
|
|
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
|
|
@@ -48,6 +48,7 @@ files:
|
|
|
48
48
|
- LICENSE.txt
|
|
49
49
|
- README.md
|
|
50
50
|
- lib/phlex.rb
|
|
51
|
+
- lib/phlex/attribute_cache.rb
|
|
51
52
|
- lib/phlex/compiler.rb
|
|
52
53
|
- lib/phlex/compiler/class_compiler.rb
|
|
53
54
|
- lib/phlex/compiler/compactor.rb
|
|
@@ -99,7 +100,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
99
100
|
- !ruby/object:Gem::Version
|
|
100
101
|
version: '0'
|
|
101
102
|
requirements: []
|
|
102
|
-
rubygems_version: 4.0.
|
|
103
|
+
rubygems_version: 4.0.20
|
|
103
104
|
specification_version: 4
|
|
104
105
|
summary: Object-oriented views in Ruby.
|
|
105
106
|
test_files: []
|