papercraft 0.26 → 0.27

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2b64cff374a2d8fdf50de978d3a4b0ceb6fb939741c00ed7cf63154af7cf6732
4
- data.tar.gz: 67c0fc2aa65a1056f04c378ebe50a0a433bb1cabba75a1fbd522d60604ddcd1d
3
+ metadata.gz: bd2f4f4d1ebc2cfc5718079d68c9393b3d95def9c02b057107425ddd5b45eaf3
4
+ data.tar.gz: f3f82f29227e22e5e31b0d685bcab211ebbef0ecf8a4433d251cb1f204875cf7
5
5
  SHA512:
6
- metadata.gz: 690eaec575b4ad635fc913609197d166841536811b1e1d4314d763c6c37fb520b9fc8f2f19d50f4a0cfd242a98b69d59aebf16eaa16f4298bfc74ac0040b516a
7
- data.tar.gz: 66a468eeee29e5c2b9b9e7a2f3adc605fa2f9648ade7a0aa444a798d896de3eb8c5aa02c2e79bf27780769f86d2b1960b8a111be1d8ed6604af739cab5931adb
6
+ metadata.gz: 7389861bea04a92e06ce1a53a682ef3b4db1bf975bb7f5e63cef0ed6c1a4ebb4b92920a0bb4ea04eba24dc72b41c70ba8fac0d8aa605a2b5a720d7860198b7e0
7
+ data.tar.gz: 2a617260deef291c56cd0c3fc1b275d47dd1dc95bdeeafc69289d630260480e49126c29f0ec77c177fd6c1032c633ba2568be1ffb767e13f91e51b5a9c298831
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 0.27 2023-01-19
2
+
3
+ - Fix rendering of HTML void element tags
4
+
1
5
  ## 0.26 2023-01-13
2
6
 
3
7
  - Add support for namespaced local extensions using `#extend`
@@ -88,6 +88,21 @@ module Papercraft
88
88
 
89
89
  private
90
90
 
91
+ # Returns true if the given tag is a void element, in order to render a self
92
+ # closing tag. See spec: https://html.spec.whatwg.org/multipage/syntax.html#void-elements.
93
+ #
94
+ # @param text [String] tag
95
+ # @return [Bool] is it a void element
96
+ def is_void_element_tag?(tag)
97
+ case tag
98
+ #
99
+ when 'area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'source', 'track', 'wbr'
100
+ true
101
+ else
102
+ false
103
+ end
104
+ end
105
+
91
106
  # Escapes the given text using HTML entities.
92
107
  #
93
108
  # @param text [String] text
@@ -10,6 +10,7 @@ module Papercraft
10
10
  S_LT_SLASH = '</'
11
11
  S_SPACE_LT_SLASH = ' </'
12
12
  S_SLASH_GT = '/>'
13
+ S_GT_LT_SLASH = '></'
13
14
  S_SPACE = ' '
14
15
  S_EQUAL_QUOTE = '="'
15
16
  S_QUOTE = '"'
@@ -21,6 +22,36 @@ module Papercraft
21
22
  S_TAG_%<TAG>s_PRE = %<tag_pre>s
22
23
  S_TAG_%<TAG>s_CLOSE = %<tag_close>s
23
24
 
25
+ def %<tag>s(text = nil, **props, &block)
26
+ if text.is_a?(Hash) && props.empty?
27
+ props = text
28
+ text = nil
29
+ end
30
+
31
+ @buffer << S_TAG_%<TAG>s_PRE
32
+ emit_props(props) unless props.empty?
33
+
34
+ if block
35
+ @buffer << S_GT
36
+ instance_eval(&block)
37
+ @buffer << S_TAG_%<TAG>s_CLOSE
38
+ elsif Proc === text
39
+ @buffer << S_GT
40
+ emit(text)
41
+ @buffer << S_TAG_%<TAG>s_CLOSE
42
+ elsif text
43
+ @buffer << S_GT << escape_text(text.to_s) << S_TAG_%<TAG>s_CLOSE
44
+ else
45
+ @buffer << S_GT << S_TAG_%<TAG>s_CLOSE
46
+ end
47
+ end
48
+ EOF
49
+
50
+ S_VOID_TAG_METHOD_LINE = __LINE__ + 2
51
+ S_VOID_TAG_METHOD = <<~EOF
52
+ S_TAG_%<TAG>s_PRE = %<tag_pre>s
53
+ S_TAG_%<TAG>s_CLOSE = %<tag_close>s
54
+
24
55
  def %<tag>s(text = nil, **props, &block)
25
56
  if text.is_a?(Hash) && props.empty?
26
57
  props = text
@@ -149,8 +180,10 @@ module Papercraft
149
180
  @buffer << S_LT_SLASH << tag << S_GT
150
181
  elsif text
151
182
  @buffer << S_GT << escape_text(text.to_s) << S_LT_SLASH << tag << S_GT
152
- else
183
+ elsif is_void_element_tag?(sym)
153
184
  @buffer << S_SLASH_GT
185
+ else
186
+ @buffer << S_GT_LT_SLASH << tag << S_GT
154
187
  end
155
188
  end
156
189
 
@@ -262,13 +295,20 @@ module Papercraft
262
295
  # @return [void]
263
296
  def define_tag_method(tag)
264
297
  repr = tag_repr(tag)
265
- code = S_TAG_METHOD % {
298
+ if is_void_element_tag?(tag)
299
+ tmpl = S_VOID_TAG_METHOD
300
+ line = S_VOID_TAG_METHOD_LINE
301
+ else
302
+ tmpl = S_TAG_METHOD
303
+ line = S_TAG_METHOD_LINE
304
+ end
305
+ code = tmpl % {
266
306
  tag: tag,
267
307
  TAG: tag.upcase,
268
308
  tag_pre: "<#{repr}".inspect,
269
309
  tag_close: "</#{repr}>".inspect
270
310
  }
271
- self.class.class_eval(code, __FILE__, S_TAG_METHOD_LINE)
311
+ self.class.class_eval(code, __FILE__, line)
272
312
  end
273
313
 
274
314
  # Defines a namespace referring to the given module.
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Papercraft
4
- VERSION = '0.26'
4
+ VERSION = '0.27'
5
5
  end
@@ -10,7 +10,15 @@ module Papercraft
10
10
 
11
11
  private
12
12
 
13
- # Converts a tag to its string representation. Underscores will be converted
13
+ # Returns false (no void elements in XML)
14
+ #
15
+ # @param tag [String] tag
16
+ # @return [false] false
17
+ def is_void_element_tag?(tag)
18
+ false
19
+ end
20
+
21
+ # Converts a tag to its string representation. Underscores will be converted
14
22
  # to dashes, double underscores will be converted to colon.
15
23
  #
16
24
  # @param tag [Symbol, String] tag
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: papercraft
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.26'
4
+ version: '0.27'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sharon Rosner
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-01-13 00:00:00.000000000 Z
11
+ date: 2023-01-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: escape_utils