web_components_rails 2.1.2 → 2.2.0

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
- SHA1:
3
- metadata.gz: b09f5c09128952911e23c7d01d9c32a15646d442
4
- data.tar.gz: 02876ac28c8bcb83da2530d22e814bf613675e7a
2
+ SHA256:
3
+ metadata.gz: 6f52ec4b08fcca1e496159e385dee89b3b65e191c4004f1dff6366df870d9f8f
4
+ data.tar.gz: d9aa536fc574386e5316a0e601a163809c8d8e4286583361b51a376d1e5548d4
5
5
  SHA512:
6
- metadata.gz: 84aa01e3227cf376f61dc8c4682ccbec450325b014b42946701ea7c9049ba07894ddcc2e4ee9cc776091f3af5407d5991ce0a17d6ab9e371e847587f5ae50990
7
- data.tar.gz: be43d982b2c169077db220d449f0be06a91d33ddcb6e3f11a6ff009baab445a9274dc4ab2ce28a02828ac22ad1886aea5509fdb768df91aae5f41f36f4ee44dd
6
+ metadata.gz: 9fad69eb81f98d615b601c29da13dacbd97a62119203f1d0c23b020a6e38593f5762e7163c2646ec532ea0267dfd711214523c3bede432e2ae64fb1240ab340b
7
+ data.tar.gz: 4c826159637070c3c8c9dd94f230953541a15f506c68a7fe8fce56e2d8dcf659b8296032a521c3f09c1f18788c87054690cd563142a1cc2c9f418aca525e3d09
@@ -6,7 +6,56 @@ require 'securerandom'
6
6
  # https://github.com/rails/sprockets/blob/3.x/lib/sprockets/directive_processor.rb
7
7
  class WebComponentsRails::HTMLImportProcessor
8
8
 
9
- VERSION = '9'
9
+ VERSION = '10'
10
+ XML_SAVE_OPTIONS = {
11
+ save_with: ::Nokogiri::XML::Node::SaveOptions::DEFAULT_XML | ::Nokogiri::XML::Node::SaveOptions::NO_EMPTY_TAGS
12
+ }
13
+
14
+ # URI attributes are determined by using the attributes list at https://html.spec.whatwg.org/#attributes-3
15
+ # and looking for attributes that specify "Valid URL" as their values
16
+ URI_ATTRIBUTES = %w(
17
+ action
18
+ cite
19
+ data
20
+ formaction
21
+ href
22
+ itemid
23
+ manifest
24
+ ping
25
+ poster
26
+ src
27
+ ).freeze
28
+
29
+ # URI attributes are determined by using the attributes list at https://html.spec.whatwg.org/#attributes-3
30
+ # and looking for attributes that specify "Boolean attribute" as their values
31
+ BOOLEAN_ATTRIBUTES = %w(
32
+ allowfullscreen
33
+ allowpaymentrequest
34
+ allowusermedia
35
+ async
36
+ autofocus
37
+ autoplay
38
+ checked
39
+ controls
40
+ default
41
+ defer
42
+ disabled
43
+ formnovalidate
44
+ hidden
45
+ ismap
46
+ itemscope
47
+ loop
48
+ multiple
49
+ muted
50
+ nomodule
51
+ novalidate
52
+ open
53
+ playsinline
54
+ readonly
55
+ required
56
+ reversed
57
+ selected
58
+ ).freeze
10
59
 
11
60
  def self.instance
12
61
  @instance ||= new
@@ -21,25 +70,30 @@ class WebComponentsRails::HTMLImportProcessor
21
70
  end
22
71
 
23
72
  def self.doc_to_html(doc)
24
- # Nokogiri/Nokogumbo are hard-coded to URI-escape certain attributes (src, href, action, and a[name]),
25
- # so we have to put in placeholders, and fix the values in the HTML string output afterwards
26
- # This doesn't work so well with framework-specific syntax (eg. <foo src="{{bar}}">)
27
- placeholder_mapping = {}
28
- %w(src href action).each do |name|
29
- doc.css("[#{name}]").each do |node|
30
- # The placeholders are just random strings
31
- placeholder = SecureRandom.hex(40)
32
- attr = node.attributes[name]
33
- placeholder_mapping[placeholder] = attr.value
34
- attr.value = placeholder
35
- end
36
- end
37
- new_html = doc.to_html
38
- placeholder_mapping.each do |placeholder, value|
39
- new_html.sub!(placeholder, value)
73
+ doc_html = doc.to_html(encoding: 'UTF-8')
74
+
75
+ uri_regex = Regexp.union(URI_ATTRIBUTES)
76
+ square_brackets_regex = Regexp.new("(#{uri_regex})=\"%5B%5B(.+?)%5D%5D\"", 'i')
77
+ curly_braces_regex = Regexp.new("(#{uri_regex})=\"%7B%7B(.+?)%7D%7D\"", 'i')
78
+
79
+ selectors = (URI_ATTRIBUTES + BOOLEAN_ATTRIBUTES).map { |attribute| "*[#{attribute}]"}
80
+ doc.css(selectors.join(',')).each do |node|
81
+ # Nokogiri only writes out valid HTML so this outputs some nodes that would not be valid HTML
82
+ # (e.g. nodes with boolean attributes that have values, or nodes that have unescaped URI attributes)
83
+ # as both HTML and XML, and uses swap out the HTML for XML in the complete document HTML.
84
+ pattern = node.to_html
85
+ replacement = node.to_xml(XML_SAVE_OPTIONS)
86
+
87
+ # Nokogiri/Nokogumbo are hard-coded to URI-escape certain attributes (src, href, action, and a[name]),
88
+ # so we have to put in placeholders, and fix the values in the HTML string output afterwards
89
+ # This doesn't work so well with framework-specific syntax (eg. <foo src="{{bar}}">)
90
+ replacement.gsub!(square_brackets_regex, '\1="[[\2]]"')
91
+ replacement.gsub!(curly_braces_regex, '\1="{{\2}}"')
92
+
93
+ doc_html.gsub!(pattern, replacement)
40
94
  end
41
95
 
42
- new_html
96
+ doc_html
43
97
  end
44
98
 
45
99
 
@@ -1,3 +1,3 @@
1
1
  module WebComponentsRails
2
- VERSION = '2.1.2'
2
+ VERSION = '2.2.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: web_components_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.2
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jon Botelho
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-05-04 00:00:00.000000000 Z
11
+ date: 2018-01-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogumbo
@@ -119,7 +119,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
119
119
  version: '0'
120
120
  requirements: []
121
121
  rubyforge_project:
122
- rubygems_version: 2.4.8
122
+ rubygems_version: 2.7.4
123
123
  signing_key:
124
124
  specification_version: 4
125
125
  summary: Web components utils for rails