web_components_rails 2.1.2 → 2.3.0

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
- SHA1:
3
- metadata.gz: b09f5c09128952911e23c7d01d9c32a15646d442
4
- data.tar.gz: 02876ac28c8bcb83da2530d22e814bf613675e7a
2
+ SHA256:
3
+ metadata.gz: 29026bd8a5c3ae2ade72b7978e60ea6a1905dd8ff66ab438b89faacd85f4a2a8
4
+ data.tar.gz: b05ab464110938ec3b1a70fe2bbca66b1d8f6e9c753800980e36fb4629e4e223
5
5
  SHA512:
6
- metadata.gz: 84aa01e3227cf376f61dc8c4682ccbec450325b014b42946701ea7c9049ba07894ddcc2e4ee9cc776091f3af5407d5991ce0a17d6ab9e371e847587f5ae50990
7
- data.tar.gz: be43d982b2c169077db220d449f0be06a91d33ddcb6e3f11a6ff009baab445a9274dc4ab2ce28a02828ac22ad1886aea5509fdb768df91aae5f41f36f4ee44dd
6
+ metadata.gz: fb1d5a6107ebef9a5e74ca444bb50f652d1538f3b949a71c11463e6c5bae903a8baf135ea61cee43b548befdc59b07c41e67606385823057b051ec837b3825f1
7
+ data.tar.gz: f16c3b8a29a34774b232144bde31a5bae0665c658723475f8e606cb9d25e89485078cbb8354098fd912588b873322904eb38f0426c19472ab58f84578fceaccd
@@ -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.3.0'
3
3
  end
@@ -1,5 +1,5 @@
1
1
  require 'active_support'
2
- require 'nokogumbo'
2
+ require 'nokogiri'
3
3
  require 'sprockets'
4
4
 
5
5
  module WebComponentsRails
metadata CHANGED
@@ -1,71 +1,77 @@
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.3.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: 2022-11-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: nokogumbo
14
+ name: nokogiri
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 1.4.5
19
+ version: '1.12'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '2.0'
20
23
  type: :runtime
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
23
26
  requirements:
24
27
  - - ">="
25
28
  - !ruby/object:Gem::Version
26
- version: 1.4.5
29
+ version: '1.12'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '2.0'
27
33
  - !ruby/object:Gem::Dependency
28
34
  name: rails
29
35
  requirement: !ruby/object:Gem::Requirement
30
36
  requirements:
31
37
  - - ">="
32
38
  - !ruby/object:Gem::Version
33
- version: 4.0.0
39
+ version: '6.0'
34
40
  type: :runtime
35
41
  prerelease: false
36
42
  version_requirements: !ruby/object:Gem::Requirement
37
43
  requirements:
38
44
  - - ">="
39
45
  - !ruby/object:Gem::Version
40
- version: 4.0.0
46
+ version: '6.0'
41
47
  - !ruby/object:Gem::Dependency
42
48
  name: sprockets-rails
43
49
  requirement: !ruby/object:Gem::Requirement
44
50
  requirements:
45
51
  - - ">="
46
52
  - !ruby/object:Gem::Version
47
- version: 2.0.0
53
+ version: '3.2'
48
54
  type: :runtime
49
55
  prerelease: false
50
56
  version_requirements: !ruby/object:Gem::Requirement
51
57
  requirements:
52
58
  - - ">="
53
59
  - !ruby/object:Gem::Version
54
- version: 2.0.0
60
+ version: '3.2'
55
61
  - !ruby/object:Gem::Dependency
56
62
  name: uglifier
57
63
  requirement: !ruby/object:Gem::Requirement
58
64
  requirements:
59
65
  - - ">="
60
66
  - !ruby/object:Gem::Version
61
- version: 2.0.0
67
+ version: '3.2'
62
68
  type: :runtime
63
69
  prerelease: false
64
70
  version_requirements: !ruby/object:Gem::Requirement
65
71
  requirements:
66
72
  - - ">="
67
73
  - !ruby/object:Gem::Version
68
- version: 2.0.0
74
+ version: '3.2'
69
75
  - !ruby/object:Gem::Dependency
70
76
  name: rspec
71
77
  requirement: !ruby/object:Gem::Requirement
@@ -118,8 +124,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
118
124
  - !ruby/object:Gem::Version
119
125
  version: '0'
120
126
  requirements: []
121
- rubyforge_project:
122
- rubygems_version: 2.4.8
127
+ rubygems_version: 3.1.6
123
128
  signing_key:
124
129
  specification_version: 4
125
130
  summary: Web components utils for rails