htx 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6bb09e36ecc0e6a70df4fed4552ae20a9512eadd3ac57d86bd88fa278c8d6029
4
- data.tar.gz: e45e67362bc5422faa7df5330c5bd4940d4973c8ab0303cdd8207f0d7149efed
3
+ metadata.gz: dc1adc5f7b7b12da438f6c96c5a81b3585a8f9a8b761d54eee3895b5195898ec
4
+ data.tar.gz: cfc75dd48759fdb2151db74de71ce9c7ae07a73e07d20af415e96a0a37c07bda
5
5
  SHA512:
6
- metadata.gz: 9eaea052f3b131de348f9ad20de712d55e180820d184a54356935aeacc3f2a7116266f2a4a6ff6e725ca44052a6c15350debcacd250b0f60be5ed0bb586aca26
7
- data.tar.gz: f0f3ca8fd975d7bbf3b8d26c65d62cda86caaf245dd8143be571a5a9b28088e2274fb89a04a5fba9c567ef549f1d9d5dbaad8fcbc9db1ee346f395cd863097df
6
+ metadata.gz: 81660a171c9eb0a384b483f5391bd929bec1833aceef2128ecefcfba1190ea4343efbc02f286f241fcf435dcd3fd860fd7f9844a6c29358d84e5005b10e2a62b
7
+ data.tar.gz: c8f764200a3e0f5fb19d1c19cc8f8e5a71209b4d0e8e87f7cf27941fbf0595090395ee141a136c67670be865de8dba50235d7f10ce8fc0ccd4a2cd060809af09
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.6
1
+ 0.0.7
data/lib/htx/template.rb CHANGED
@@ -4,13 +4,13 @@ require('nokogiri')
4
4
 
5
5
  module HTX
6
6
  class Template
7
- CHILDLESS = 0b001
8
- TEXT_NODE = 0b010
9
- XMLNS_NODE = 0b100
10
- FLAG_BITS = 3
7
+ ELEMENT = 0b001
8
+ CHILDLESS = 0b010
9
+ XMLNS = 0b100
10
+ FLAG_BITS = 3
11
11
 
12
12
  INDENT_DEFAULT = ' '
13
- TEXT_NODE_TAG = 'htx-text'
13
+ CONTENT_TAG = 'htx-content'
14
14
  DYNAMIC_KEY_ATTR = 'htx-key'
15
15
 
16
16
  DEFAULT_XMLNS = {
@@ -35,11 +35,12 @@ module HTX
35
35
  HTML_ENTITY = /&([a-zA-Z]+|#\d+|x[0-9a-fA-F]+);/.freeze
36
36
  NON_CONTROL_STATEMENT = /#{INTERPOLATION}|(#{HTML_ENTITY})/.freeze
37
37
  CONTROL_STATEMENT = /[{}();]/.freeze
38
+ UNESCAPED_BACKTICK = /(?<!\\)((\\\\)*)`/.freeze
38
39
  CLOSE_STATEMENT = /;?\s*htx\.close\((\d*)\);?(\s*)\z/.freeze
39
40
 
40
41
  ##
41
42
  # Returns false. In the near future when support for the <:> tag has been dropped (in favor of
42
- # <htx-text>), will return true if Nokogiri's HTML5 parser is available. To use it now, monkey patch
43
+ # <htx-content>), will return true if Nokogiri's HTML5 parser is available. To use it now, monkey patch
43
44
  # this method to return true.
44
45
  #
45
46
  def self.html5_parser?
@@ -116,20 +117,25 @@ module HTX
116
117
  #
117
118
  # * +base+ - Base Nokogiri node to start from.
118
119
  #
119
- def process(base)
120
+ def process(base, xmlns: false)
120
121
  base.children.each do |node|
121
122
  next unless node.element? || node.text?
122
123
 
123
124
  dynamic_key = process_value(node.attr(DYNAMIC_KEY_ATTR), :attr)
124
125
 
125
- if node.text? || node.name == TEXT_NODE_TAG || node.name == ':'
126
- if node.name == ':'
127
- warn("#{@name}:#{node.line}: The <:> tag has been deprecated. Please use <#{TEXT_NODE_TAG}> "\
128
- 'for identical functionality.')
126
+ if node.text? || node.name == CONTENT_TAG || node.name == 'htx-text' || node.name == ':'
127
+ if !node.text? && node.name != CONTENT_TAG
128
+ warn("#{@name}:#{node.line}: The <#{node.name}> tag has been deprecated. Please use "\
129
+ "<#{CONTENT_TAG}> for identical functionality.")
130
+ end
131
+
132
+ if (node.attributes.size - (dynamic_key ? 1 : 0)) != 0
133
+ raise(MalformedTemplateError.new("<#{node.name}> tags may not have attributes other than "\
134
+ "#{DYNAMIC_KEY_ATTR}", @name, node))
129
135
  end
130
136
 
131
137
  if (non_text_node = node.children.find { |n| !n.text? })
132
- raise(MalformedTemplateError.new('text node tags may not contain child tags', @name,
138
+ raise(MalformedTemplateError.new("<#{node.name}> tags may not contain child tags", @name,
133
139
  non_text_node))
134
140
  end
135
141
 
@@ -141,7 +147,7 @@ module HTX
141
147
  "htx.node(#{[
142
148
  value,
143
149
  dynamic_key,
144
- ((@static_key += 1) << FLAG_BITS) | TEXT_NODE,
150
+ (@static_key += 1) << FLAG_BITS,
145
151
  ].compact.join(', ')})"\
146
152
  "#{indent(text[TRAILING_WHITESPACE])}"
147
153
  )
@@ -150,17 +156,18 @@ module HTX
150
156
  end
151
157
  else
152
158
  childless = node.children.empty? || (node.children.size == 1 && node.children[0].text.strip == '')
153
- attrs, xmlns = process_attrs(node)
159
+ attrs, explicit_xmlns = process_attrs(node)
160
+ xmlns ||= explicit_xmlns
154
161
 
155
162
  append("htx.node(#{[
156
163
  "'#{tag_name(node.name)}'",
157
164
  attrs,
158
165
  dynamic_key,
159
- ((@static_key += 1) << FLAG_BITS) | (childless ? CHILDLESS : 0) | (xmlns ? XMLNS_NODE : 0),
166
+ ((@static_key += 1) << FLAG_BITS) | ELEMENT | (childless ? CHILDLESS : 0) | (xmlns ? XMLNS : 0),
160
167
  ].compact.flatten.join(', ')})")
161
168
 
162
169
  unless childless
163
- process(node)
170
+ process(node, xmlns: xmlns)
164
171
 
165
172
  count = ''
166
173
  @compiled.sub!(CLOSE_STATEMENT) do
@@ -220,13 +227,16 @@ module HTX
220
227
  # Entire text is enclosed in ${...}.
221
228
  value.strip!
222
229
  quote = false
230
+ escape_quotes = false
223
231
  elsif (value = text[TEMPLATE_STRING, 1])
224
232
  # Entire text is enclosed in backticks (template string).
225
233
  quote = true
234
+ escape_quotes = false
226
235
  elsif is_attr || text.gsub(NON_CONTROL_STATEMENT, '') !~ CONTROL_STATEMENT
227
236
  # Text is an attribute value or doesn't match control statement pattern.
228
237
  value = text.dup
229
238
  quote = true
239
+ escape_quotes = true
230
240
  else
231
241
  return nil
232
242
  end
@@ -235,6 +245,7 @@ module HTX
235
245
  # calculation ignores everything before the first newline in its search for the least-indented line.
236
246
  outdent = value.scan(NON_BLANK_NON_FIRST_LINE).min
237
247
  value.gsub!(/#{LEADING_WHITESPACE}|#{TRAILING_WHITESPACE}|^#{outdent}/, '')
248
+ value.gsub!(UNESCAPED_BACKTICK, '\1\\\`') if escape_quotes
238
249
  value.insert(0, '`').insert(-1, '`') if quote
239
250
 
240
251
  # Ensure any Unicode characters get converted to Unicode escape sequences. Also note that since
@@ -300,9 +311,9 @@ module HTX
300
311
  #
301
312
  # Note: Nokogiri's newer HTML5 parser resulting from the Nokogumbo merge fixes this issue, but it is
302
313
  # currently not available for JRuby. It also does not parse <:> as a tag, which is why it's been
303
- # deprecated in favor of <htx-text>. Once support for <:> has been completely removed, the HTML5 parser
304
- # will be used for regular Ruby and this tag and attribute mapping hack reserved for JRuby (and any
305
- # other potential environments where the HTML5 parser is not available).
314
+ # deprecated in favor of <htx-content>. Once support for <:> has been completely removed, the HTML5
315
+ # parser will be used for regular Ruby and this tag and attribute mapping hack reserved for JRuby (and
316
+ # any other potential environments where the HTML5 parser is not available).
306
317
 
307
318
  # Source: https://developer.mozilla.org/en-US/docs/Web/SVG/Element
308
319
  TAG_MAP = %w[
data/lib/htx/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module HTX
4
- VERSION = '0.0.6'
4
+ VERSION = '0.0.7'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: htx
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nate Pickens
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-01-21 00:00:00.000000000 Z
11
+ date: 2022-02-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri