phlex 2.4.2 → 2.4.3
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/csv.rb +13 -3
- data/lib/phlex/fifo_cache_store.rb +3 -1
- data/lib/phlex/html.rb +5 -1
- data/lib/phlex/sgml/attributes.rb +21 -16
- data/lib/phlex/sgml/state.rb +38 -5
- data/lib/phlex/sgml.rb +62 -29
- data/lib/phlex/svg.rb +3 -3
- data/lib/phlex/version.rb +1 -1
- data/lib/phlex.rb +1 -0
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 67700cf68c240368c4f85e21097026e4a42ca8462a4bb21766538d84d7cc21dd
|
|
4
|
+
data.tar.gz: a52a807f50d250770f7ef4cd2e7ab467dcd0417548ba50ba3a435c3e0de97228
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: fb543c361b2decd7ac21371abb01232e1f3d2e95d5d99a1569f163dcb92c6ecefd0aee910f8b19b88a183d70ae23ee1923aaa66a37e6bbf006b1e5a03d9de335
|
|
7
|
+
data.tar.gz: '08e15befb80047aa61c82beac64cfd94391d8d882b04162a7a55be676cf55a858d6ef73486e0879c3cb62569df7032a396d062b33c55943010f9a26b9a78d04f'
|
data/lib/phlex/csv.rb
CHANGED
|
@@ -34,9 +34,9 @@ class Phlex::CSV
|
|
|
34
34
|
end
|
|
35
35
|
|
|
36
36
|
if strip_whitespace
|
|
37
|
-
escape_regex = /[\n"#{delimiter}]/
|
|
37
|
+
escape_regex = /[\r\n"#{Regexp.escape(delimiter)}]/
|
|
38
38
|
else
|
|
39
|
-
escape_regex = /^\s|\s$|[\n"#{delimiter}]/
|
|
39
|
+
escape_regex = /^\s|\s$|[\r\n"#{Regexp.escape(delimiter)}]/
|
|
40
40
|
end
|
|
41
41
|
|
|
42
42
|
if has_yielder
|
|
@@ -55,6 +55,7 @@ class Phlex::CSV
|
|
|
55
55
|
|
|
56
56
|
if first_row
|
|
57
57
|
first_row = false
|
|
58
|
+
headers.clear
|
|
58
59
|
|
|
59
60
|
i = 0
|
|
60
61
|
number_of_columns = row.length
|
|
@@ -83,6 +84,11 @@ class Phlex::CSV
|
|
|
83
84
|
number_of_columns = row.length
|
|
84
85
|
first_col = true
|
|
85
86
|
|
|
87
|
+
unless number_of_columns == headers.length
|
|
88
|
+
row_buffer.clear
|
|
89
|
+
raise Phlex::RuntimeError.new("Column count mismatch: expected #{headers.length}, got #{number_of_columns}.")
|
|
90
|
+
end
|
|
91
|
+
|
|
86
92
|
while i < number_of_columns
|
|
87
93
|
header, value = row[i]
|
|
88
94
|
|
|
@@ -188,7 +194,11 @@ class Phlex::CSV
|
|
|
188
194
|
buffer << value
|
|
189
195
|
end
|
|
190
196
|
else # not escaping CSV injection
|
|
191
|
-
|
|
197
|
+
if value.match?(escape_regex)
|
|
198
|
+
buffer << '"' << value.gsub('"', '""') << '"'
|
|
199
|
+
else
|
|
200
|
+
buffer << value
|
|
201
|
+
end
|
|
192
202
|
end
|
|
193
203
|
else # not stripping whitespace
|
|
194
204
|
if escape_csv_injection
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "json"
|
|
4
|
+
|
|
3
5
|
# An extremely fast in-memory cache store that evicts keys on a first-in-first-out basis.
|
|
4
6
|
class Phlex::FIFOCacheStore
|
|
5
7
|
def initialize(max_bytesize: 2 ** 20)
|
|
@@ -18,7 +20,7 @@ class Phlex::FIFOCacheStore
|
|
|
18
20
|
else
|
|
19
21
|
result = yield
|
|
20
22
|
|
|
21
|
-
fifo[key] = JSON.
|
|
23
|
+
fifo[key] = JSON.generate(result)
|
|
22
24
|
|
|
23
25
|
result
|
|
24
26
|
end
|
data/lib/phlex/html.rb
CHANGED
|
@@ -64,7 +64,7 @@ class Phlex::HTML < Phlex::SGML
|
|
|
64
64
|
raise Phlex::ArgumentError.new("`tag` can't render <#{unsafe_tag}>. Call `#{unsafe_tag}` directly instead.")
|
|
65
65
|
end
|
|
66
66
|
|
|
67
|
-
|
|
67
|
+
if (tag = StandardElements.__registered_elements__[name]) || ((tag = name.name.tr("_", "-")).include?("-") && tag.match?(/\A[a-z0-9-]+\z/))
|
|
68
68
|
if attributes.length > 0 # with attributes
|
|
69
69
|
if block_given # with content block
|
|
70
70
|
buffer << "<#{tag}"
|
|
@@ -106,6 +106,10 @@ class Phlex::HTML < Phlex::SGML
|
|
|
106
106
|
buffer << "<#{tag}></#{tag}>"
|
|
107
107
|
end
|
|
108
108
|
end
|
|
109
|
+
|
|
110
|
+
flush if "head" == tag
|
|
111
|
+
|
|
112
|
+
nil
|
|
109
113
|
elsif (tag = VoidElements.__registered_elements__[name])
|
|
110
114
|
if block_given
|
|
111
115
|
raise Phlex::ArgumentError.new("Void elements cannot have content blocks.")
|
|
@@ -74,7 +74,12 @@ module Phlex::SGML::Attributes
|
|
|
74
74
|
v.to_s.gsub(UNSAFE_VALUE_CHARS, VALUE_ESCAPES)
|
|
75
75
|
else
|
|
76
76
|
if v.respond_to?(:to_h)
|
|
77
|
-
|
|
77
|
+
case k
|
|
78
|
+
when :style
|
|
79
|
+
generate_styles(v.to_h).gsub(UNSAFE_VALUE_CHARS, VALUE_ESCAPES)
|
|
80
|
+
else
|
|
81
|
+
generate_nested_attributes(v.to_h, "#{name}-", buffer)
|
|
82
|
+
end
|
|
78
83
|
else
|
|
79
84
|
raise Phlex::ArgumentError.new("Invalid attribute value for #{k}: #{v.inspect}.")
|
|
80
85
|
end
|
|
@@ -264,37 +269,37 @@ module Phlex::SGML::Attributes
|
|
|
264
269
|
case token
|
|
265
270
|
when String
|
|
266
271
|
token = token.gsub(gsub_from, gsub_to) if gsub_from
|
|
267
|
-
if
|
|
268
|
-
buffer << sep << token
|
|
269
|
-
else
|
|
272
|
+
if buffer.empty?
|
|
270
273
|
buffer << token
|
|
274
|
+
else
|
|
275
|
+
buffer << sep << token
|
|
271
276
|
end
|
|
272
277
|
when Symbol
|
|
273
|
-
if
|
|
274
|
-
buffer << sep << token.name.tr("_", "-")
|
|
275
|
-
else
|
|
278
|
+
if buffer.empty?
|
|
276
279
|
buffer << token.name.tr("_", "-")
|
|
280
|
+
else
|
|
281
|
+
buffer << sep << token.name.tr("_", "-")
|
|
277
282
|
end
|
|
278
283
|
when Integer, Float, Phlex::SGML::SafeObject
|
|
279
|
-
if
|
|
280
|
-
buffer << sep << token.to_s
|
|
281
|
-
else
|
|
284
|
+
if buffer.empty?
|
|
282
285
|
buffer << token.to_s
|
|
286
|
+
else
|
|
287
|
+
buffer << sep << token.to_s
|
|
283
288
|
end
|
|
284
289
|
when Array
|
|
285
290
|
if token.length > 0 && (value = generate_nested_tokens(token, sep, gsub_from, gsub_to))
|
|
286
|
-
if
|
|
287
|
-
buffer << sep << value
|
|
288
|
-
else
|
|
291
|
+
if buffer.empty?
|
|
289
292
|
buffer << value
|
|
293
|
+
else
|
|
294
|
+
buffer << sep << value
|
|
290
295
|
end
|
|
291
296
|
end
|
|
292
297
|
when Set
|
|
293
298
|
if token.length > 0 && (value = generate_nested_tokens(token.to_a, sep, gsub_from, gsub_to))
|
|
294
|
-
if
|
|
295
|
-
buffer << sep << value
|
|
296
|
-
else
|
|
299
|
+
if buffer.empty?
|
|
297
300
|
buffer << value
|
|
301
|
+
else
|
|
302
|
+
buffer << sep << value
|
|
298
303
|
end
|
|
299
304
|
end
|
|
300
305
|
when nil
|
data/lib/phlex/sgml/state.rb
CHANGED
|
@@ -7,6 +7,7 @@ class Phlex::SGML::State
|
|
|
7
7
|
@user_context = user_context
|
|
8
8
|
@fragments = fragments
|
|
9
9
|
@fragment_depth = 0
|
|
10
|
+
@rendering_fragments = nil
|
|
10
11
|
@cache_stack = []
|
|
11
12
|
@halt_signal = nil
|
|
12
13
|
@output_buffer = output_buffer
|
|
@@ -34,12 +35,16 @@ class Phlex::SGML::State
|
|
|
34
35
|
end
|
|
35
36
|
|
|
36
37
|
def begin_fragment(id)
|
|
37
|
-
|
|
38
|
+
# A selected fragment ends at the first end of its name, so count it once however deeply the name is nested.
|
|
39
|
+
if @fragments&.include?(id) && (@rendering_fragments ||= Set.new).add?(id)
|
|
40
|
+
@fragment_depth += 1
|
|
41
|
+
end
|
|
38
42
|
|
|
39
43
|
if caching?
|
|
40
44
|
current_byte_offset = 0 # Start tracking the byte offset of this fragment from the start of the cache buffer
|
|
41
45
|
@cache_stack.reverse_each do |(cache_buffer, fragment_map)| # We'll iterate deepest to shallowest
|
|
42
46
|
current_byte_offset += cache_buffer.bytesize # Add the length of the cache buffer to the current byte offset
|
|
47
|
+
next if fragment_map.key?(id) # Only the first fragment with a given name can be selected
|
|
43
48
|
fragment_map[id] = [current_byte_offset, nil, []] # Record the byte offset, length, and store a list of the nested fragments
|
|
44
49
|
|
|
45
50
|
fragment_map.each do |name, (_offset, length, nested_fragments)| # Iterate over the other fragments
|
|
@@ -52,10 +57,12 @@ class Phlex::SGML::State
|
|
|
52
57
|
|
|
53
58
|
def end_fragment(id)
|
|
54
59
|
if caching?
|
|
55
|
-
|
|
60
|
+
current_byte_offset = 0
|
|
56
61
|
@cache_stack.reverse_each do |(cache_buffer, fragment_map)| # We'll iterate deepest to shallowest
|
|
57
|
-
|
|
58
|
-
fragment_map[id]
|
|
62
|
+
current_byte_offset += cache_buffer.bytesize
|
|
63
|
+
fragment = fragment_map[id]
|
|
64
|
+
next if fragment[1] # An earlier fragment with this name has already ended
|
|
65
|
+
fragment[1] = current_byte_offset - fragment[0] # The byte length is the difference between the current byte offset and the byte offset of the fragment
|
|
59
66
|
end
|
|
60
67
|
end
|
|
61
68
|
|
|
@@ -63,6 +70,10 @@ class Phlex::SGML::State
|
|
|
63
70
|
|
|
64
71
|
@fragments.delete(id)
|
|
65
72
|
@fragment_depth -= 1
|
|
73
|
+
halt_if_all_fragments_found
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def halt_if_all_fragments_found
|
|
66
77
|
throw @halt_signal if @fragments.length == 0
|
|
67
78
|
end
|
|
68
79
|
|
|
@@ -71,10 +82,32 @@ class Phlex::SGML::State
|
|
|
71
82
|
|
|
72
83
|
@cache_stack.reverse_each do |(cache_buffer, fragment_map)|
|
|
73
84
|
offset += cache_buffer.bytesize
|
|
74
|
-
|
|
85
|
+
|
|
86
|
+
if (fragment = fragment_map[id])
|
|
87
|
+
# An open fragment with this name ends where the recorded one does.
|
|
88
|
+
unless fragment[1]
|
|
89
|
+
fragment[1] = offset + length - fragment[0]
|
|
90
|
+
fragment[2].concat(nested_fragments)
|
|
91
|
+
end
|
|
92
|
+
else
|
|
93
|
+
fragment_map.each_value do |(_offset, open_length, open_nested_fragments)|
|
|
94
|
+
open_nested_fragments << id unless open_length
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
fragment_map[id] = [offset, length, nested_fragments]
|
|
98
|
+
end
|
|
75
99
|
end
|
|
76
100
|
end
|
|
77
101
|
|
|
102
|
+
# Captured output isn't written to the cache buffer, so its fragments have no offsets in it.
|
|
103
|
+
def suspend_caching
|
|
104
|
+
original_cache_stack = @cache_stack
|
|
105
|
+
@cache_stack = []
|
|
106
|
+
yield
|
|
107
|
+
ensure
|
|
108
|
+
@cache_stack = original_cache_stack
|
|
109
|
+
end
|
|
110
|
+
|
|
78
111
|
def caching(&)
|
|
79
112
|
result = nil
|
|
80
113
|
|
data/lib/phlex/sgml.rb
CHANGED
|
@@ -60,30 +60,32 @@ class Phlex::SGML
|
|
|
60
60
|
|
|
61
61
|
block ||= @_content_block
|
|
62
62
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
63
|
+
begin
|
|
64
|
+
previous_phlex_component = Thread.current[:__phlex_component__]
|
|
65
|
+
Thread.current[:__phlex_component__] = self
|
|
66
|
+
|
|
67
|
+
state.around_render(self) do
|
|
68
|
+
before_template(&block)
|
|
69
|
+
|
|
70
|
+
around_template do
|
|
71
|
+
if block
|
|
72
|
+
view_template do |*args|
|
|
73
|
+
if args.length > 0
|
|
74
|
+
__yield_content_with_args__(*args, &block)
|
|
75
|
+
else
|
|
76
|
+
__yield_content__(&block)
|
|
77
|
+
end
|
|
76
78
|
end
|
|
79
|
+
else
|
|
80
|
+
view_template
|
|
77
81
|
end
|
|
78
|
-
else
|
|
79
|
-
view_template
|
|
80
82
|
end
|
|
81
|
-
end
|
|
82
83
|
|
|
83
|
-
|
|
84
|
+
after_template(&block)
|
|
85
|
+
end
|
|
86
|
+
ensure
|
|
87
|
+
Thread.current[:__phlex_component__] = previous_phlex_component
|
|
84
88
|
end
|
|
85
|
-
ensure
|
|
86
|
-
Thread.current[:__phlex_component__] = previous_phlex_component
|
|
87
89
|
end
|
|
88
90
|
|
|
89
91
|
def context
|
|
@@ -164,10 +166,14 @@ class Phlex::SGML
|
|
|
164
166
|
def capture(*args, &block)
|
|
165
167
|
return "" unless block
|
|
166
168
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
169
|
+
state = @_state
|
|
170
|
+
|
|
171
|
+
state.suspend_caching do
|
|
172
|
+
if args.length > 0
|
|
173
|
+
state.capture { __yield_content_with_args__(*args, &block) }
|
|
174
|
+
else
|
|
175
|
+
state.capture { __yield_content__(&block) }
|
|
176
|
+
end
|
|
171
177
|
end
|
|
172
178
|
end
|
|
173
179
|
|
|
@@ -273,20 +279,47 @@ class Phlex::SGML
|
|
|
273
279
|
end
|
|
274
280
|
state.buffer << cached_buffer
|
|
275
281
|
else
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
282
|
+
ranges = []
|
|
283
|
+
|
|
284
|
+
fragment_map.each do |fragment_name, (offset, length, _nested_fragments)|
|
|
285
|
+
if state.fragments.delete?(fragment_name)
|
|
286
|
+
ranges << [offset, offset + length]
|
|
281
287
|
end
|
|
282
288
|
end
|
|
289
|
+
|
|
290
|
+
unless ranges.empty?
|
|
291
|
+
# Selected fragments can nest or overlap, so render the union of their ranges in order, as uncached rendering would.
|
|
292
|
+
ranges.sort!
|
|
293
|
+
start, finish = ranges.shift
|
|
294
|
+
|
|
295
|
+
ranges.each do |(next_start, next_finish)|
|
|
296
|
+
if next_start > finish
|
|
297
|
+
state.buffer << cached_buffer.byteslice(start, finish - start)
|
|
298
|
+
start, finish = next_start, next_finish
|
|
299
|
+
elsif next_finish > finish
|
|
300
|
+
finish = next_finish
|
|
301
|
+
end
|
|
302
|
+
end
|
|
303
|
+
|
|
304
|
+
state.buffer << cached_buffer.byteslice(start, finish - start)
|
|
305
|
+
state.halt_if_all_fragments_found
|
|
306
|
+
end
|
|
283
307
|
end
|
|
284
308
|
|
|
285
309
|
nil
|
|
286
310
|
end
|
|
287
311
|
|
|
288
312
|
def json_escape(string)
|
|
289
|
-
|
|
313
|
+
# Active Support's version keeps an `html_safe` string `html_safe`.
|
|
314
|
+
return ERB::Util.json_escape(string) if ERB::Util.respond_to?(:json_escape)
|
|
315
|
+
|
|
316
|
+
string.gsub(/[&<>\u2028\u2029]/, {
|
|
317
|
+
"&" => '\u0026',
|
|
318
|
+
"<" => '\u003c',
|
|
319
|
+
">" => '\u003e',
|
|
320
|
+
"\u2028" => '\u2028',
|
|
321
|
+
"\u2029" => '\u2029',
|
|
322
|
+
})
|
|
290
323
|
end
|
|
291
324
|
|
|
292
325
|
# Override this method to use a different deployment key.
|
data/lib/phlex/svg.rb
CHANGED
|
@@ -26,9 +26,9 @@ class Phlex::SVG < Phlex::SGML
|
|
|
26
26
|
return unless state.should_render?
|
|
27
27
|
|
|
28
28
|
if !block && String === content
|
|
29
|
-
state.buffer << "<![CDATA[" << content.gsub("]]>", "]]
|
|
29
|
+
state.buffer << "<![CDATA[" << content.gsub("]]>", "]]]]><![CDATA[>") << "]]>"
|
|
30
30
|
elsif block && nil == content
|
|
31
|
-
state.buffer << "<![CDATA[" << capture(&block).gsub("]]>", "]]
|
|
31
|
+
state.buffer << "<![CDATA[" << capture(&block).gsub("]]>", "]]]]><![CDATA[>") << "]]>"
|
|
32
32
|
else
|
|
33
33
|
|
|
34
34
|
raise Phlex::ArgumentError.new("Expected a String or block.")
|
|
@@ -55,7 +55,7 @@ class Phlex::SVG < Phlex::SGML
|
|
|
55
55
|
raise Phlex::ArgumentError.new("`tag` can't render <#{unsafe_tag}>. Call `#{unsafe_tag}` directly instead.")
|
|
56
56
|
end
|
|
57
57
|
|
|
58
|
-
|
|
58
|
+
if (tag = StandardElements.__registered_elements__[name]) || ((tag = name.name.tr("_", "-")).include?("-") && tag.match?(/\A[a-z0-9-]+\z/))
|
|
59
59
|
if attributes.length > 0 # with attributes
|
|
60
60
|
if block_given # with content block
|
|
61
61
|
buffer << "<#{tag}"
|
data/lib/phlex/version.rb
CHANGED
data/lib/phlex.rb
CHANGED