nokogiri-html5-inference 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rdoc_options +27 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/LICENSE.txt +21 -0
- data/README.md +167 -0
- data/Rakefile +12 -0
- data/lib/nokogiri/html5/inference/version.rb +9 -0
- data/lib/nokogiri/html5/inference.rb +193 -0
- metadata +72 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: e862de1922c82b7b1186b7d40c8beaf652cac4333d2a40f6f74a90e0b2b42874
|
4
|
+
data.tar.gz: 727d4d8bbfac766198a2491c315938c02c4fb37df371c10c90218d15fc083433
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 003aabbb5e086f862e57e16ed6bd013dc30c021bab85c09226d2d78d0fe4a38d931ffcee62c64673234fa970aa44b89d16cd2b0fc4ea2c54e8b77598085b7edd
|
7
|
+
data.tar.gz: 0d07a1bb3b505d181f0f6e00ab909b41ebb192b75153d00c4e49bd079dd0f08a5521c559793e246b087d3e2e3e339cacc6b268e7f7177dec97c7d709f3c7b52c
|
data/.rdoc_options
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
---
|
2
|
+
encoding: UTF-8
|
3
|
+
static_path: []
|
4
|
+
rdoc_include: []
|
5
|
+
page_dir:
|
6
|
+
charset: UTF-8
|
7
|
+
exclude:
|
8
|
+
- "~\\z"
|
9
|
+
- "\\.orig\\z"
|
10
|
+
- "\\.rej\\z"
|
11
|
+
- "\\.bak\\z"
|
12
|
+
- "\\.gemspec\\z"
|
13
|
+
hyperlink_all: false
|
14
|
+
line_numbers: false
|
15
|
+
locale:
|
16
|
+
locale_dir: locale
|
17
|
+
locale_name:
|
18
|
+
main_page: README.md
|
19
|
+
markup: rdoc
|
20
|
+
output_decoration: true
|
21
|
+
show_hash: false
|
22
|
+
skip_tests: true
|
23
|
+
tab_width: 8
|
24
|
+
template_stylesheets: []
|
25
|
+
title:
|
26
|
+
visibility: :protected
|
27
|
+
webcvs:
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at mike.dalessio@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2024 Mike Dalessio
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,167 @@
|
|
1
|
+
# Nokogiri::Html5::Inference
|
2
|
+
|
3
|
+
Given HTML5 input, make a reasonable guess at how to parse it correctly.
|
4
|
+
|
5
|
+
Infer from the HTML5 input whether it's a fragment or a document, and if it's a fragment what the proper context node should be. This is useful for parsing trusted content like view snippets, particularly for morphing cases like StimulusReflex.
|
6
|
+
|
7
|
+
## The problem this library solves
|
8
|
+
|
9
|
+
The [HTML5 Spec](https://html.spec.whatwg.org/multipage/parsing.html) defines some very precise
|
10
|
+
context-dependent parsing rules which can make it challenging to "just parse" a fragment of HTML
|
11
|
+
without knowing the parent node -- also called the "context node" -- in which it will be inserted.
|
12
|
+
|
13
|
+
Most content in an HTML5 document can be parsed assuming the parser's mode will be in the
|
14
|
+
["in body" insertion mode](https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-inbody),
|
15
|
+
but there are some notable exceptions. Perhaps the most problematic to web developers are the
|
16
|
+
table-related tags, which will not be parsed properly unless the parser is in the
|
17
|
+
["in table" insertion mode](https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-intable).
|
18
|
+
|
19
|
+
For example:
|
20
|
+
|
21
|
+
``` ruby
|
22
|
+
Nokogiri::HTML5::DocumentFragment.parse("<td>foo</td>").to_html
|
23
|
+
# => "foo"
|
24
|
+
```
|
25
|
+
|
26
|
+
In the default "in body" mode, the parser will log an error, "Start tag 'td' isn't allowed here",
|
27
|
+
and drop the tag. This fragment must be parsed "in the context" of a table in order to parse
|
28
|
+
properly. Thankfully, libgumbo and Nokogiri allow us to do this:
|
29
|
+
|
30
|
+
``` ruby
|
31
|
+
Nokogiri::HTML5::DocumentFragment.new(
|
32
|
+
Nokogiri::HTML5::Document.new,
|
33
|
+
"<td>foo</td>",
|
34
|
+
"table" # this is the context node
|
35
|
+
).to_html
|
36
|
+
# => "<tbody><tr><td>foo</td></tr></tbody>"
|
37
|
+
```
|
38
|
+
|
39
|
+
This is _almost_ correct, but we're seeing another HTML5 parsing rule in action: there may be
|
40
|
+
_intermediate parent tags_ that the HTML5 spec requires to be inserted by the parser. In this case,
|
41
|
+
the `<td>` tag must be wrapped in `<tbody><tr>` tags.
|
42
|
+
|
43
|
+
We can narrow down the result set with an XPath query to get back only the intended tags:
|
44
|
+
|
45
|
+
``` ruby
|
46
|
+
Nokogiri::HTML5::DocumentFragment.new(
|
47
|
+
Nokogiri::HTML5::Document.new,
|
48
|
+
"<td>foo</td>",
|
49
|
+
"table" # this is the context node
|
50
|
+
).xpath("tbody/tr/*").to_html
|
51
|
+
# => "<td>foo</td>"
|
52
|
+
```
|
53
|
+
|
54
|
+
Hurrah! This is precisely what Nokogiri::HTML5::Inference.parse does: make reasonable inferences
|
55
|
+
that work for both HTML5 documents and HTML5 fragments, and for all the different HTML5 tags that a
|
56
|
+
web developer might need in a view library.
|
57
|
+
|
58
|
+
|
59
|
+
## Usage
|
60
|
+
|
61
|
+
Given an input String containing HTML5, infer the best way to parse it by calling `Nokogiri::HTML5::Inference.parse`.
|
62
|
+
|
63
|
+
If the input is a document, you'll get a Nokogiri::HTML5::Document back:
|
64
|
+
|
65
|
+
``` ruby
|
66
|
+
html = <<~HTML
|
67
|
+
<!doctype html>
|
68
|
+
<html lang="en">
|
69
|
+
<head>
|
70
|
+
<meta encoding="UTF-8">
|
71
|
+
</head>
|
72
|
+
<body>
|
73
|
+
<h1>Hello, world!</h1>
|
74
|
+
</body>
|
75
|
+
</html>
|
76
|
+
HTML
|
77
|
+
|
78
|
+
Nokogiri::HTML5::Inference.parse(html)
|
79
|
+
# => #(Document:0x1f04 {
|
80
|
+
# name = "document",
|
81
|
+
# children = [
|
82
|
+
# #(DTD:0x2030 { name = "html" }),
|
83
|
+
# #(Element:0x2134 {
|
84
|
+
# name = "html",
|
85
|
+
# attribute_nodes = [ #(Attr:0x2260 { name = "lang", value = "en" })],
|
86
|
+
# ...
|
87
|
+
# #(Element:0x2a44 {
|
88
|
+
# name = "body",
|
89
|
+
# children = [
|
90
|
+
# #(Text "\n "),
|
91
|
+
# #(Element:0x2bd4 { name = "h1", children = [ #(Text "Hello, world!")] }),
|
92
|
+
# #(Text "\n \n\n")]
|
93
|
+
# })]
|
94
|
+
# })]
|
95
|
+
# })
|
96
|
+
```
|
97
|
+
|
98
|
+
If the input is a fragment that is parsed normally, you'll either get a Nokogiri::HTML5::DocumentFragment back:
|
99
|
+
|
100
|
+
``` ruby
|
101
|
+
Nokogiri::HTML5::Inference.parse("<div>hello,</div><div>world!</div>")
|
102
|
+
# => #(DocumentFragment:0x34f8 {
|
103
|
+
# name = "#document-fragment",
|
104
|
+
# children = [
|
105
|
+
# #(Element:0x3624 { name = "div", children = [ #(Text "hello,")] }),
|
106
|
+
# #(Element:0x3804 { name = "div", children = [ #(Text "world!")] })]
|
107
|
+
# })
|
108
|
+
```
|
109
|
+
|
110
|
+
or, if there are intermediate parent tags that need to be removed, you'll get a Nokogiri::XML::NodeSet:
|
111
|
+
|
112
|
+
``` ruby
|
113
|
+
Nokogiri::HTML5::Inference.parse("<tr><td>hello</td><td>world!</td></tr>")
|
114
|
+
# => [
|
115
|
+
# #<Nokogiri::XML::Element:0x4074 name="tr"
|
116
|
+
# children=[
|
117
|
+
# #<Nokogiri::XML::Element:0x4038 name="td" children=[#<Nokogiri::XML::Text:0x4024 "hello">]>,
|
118
|
+
# #<Nokogiri::XML::Element:0x4060 name="td" children=[#<Nokogiri::XML::Text:0x404c "world!">]>
|
119
|
+
# ]>
|
120
|
+
# ]
|
121
|
+
```
|
122
|
+
|
123
|
+
All of these return types respond to the same query methods like `#css` and `#xpath`, tree-traversal
|
124
|
+
methods like `#children`, and serialization methods like `#to_html`.
|
125
|
+
|
126
|
+
|
127
|
+
## Caveats
|
128
|
+
|
129
|
+
The implementation is currently pretty hacky and only looks at the first tag in the input to make
|
130
|
+
decisions. Nonetheless, it is a step forward from what Nokogiri and libgumbo do out-of-the-box.
|
131
|
+
|
132
|
+
The implementation also is almost certainly incomplete, meaning there are HTML5 tags that aren't handled by this library as you might expect.
|
133
|
+
|
134
|
+
We would welcome bug reports and pull requests improving this library!
|
135
|
+
|
136
|
+
|
137
|
+
## Installation
|
138
|
+
|
139
|
+
Install the gem and add to the application's Gemfile by executing:
|
140
|
+
|
141
|
+
$ bundle add nokgiri-html5-inference
|
142
|
+
|
143
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
144
|
+
|
145
|
+
$ gem install nokgiri-html5-inference
|
146
|
+
|
147
|
+
|
148
|
+
## Development
|
149
|
+
|
150
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
151
|
+
|
152
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
153
|
+
|
154
|
+
|
155
|
+
## Contributing
|
156
|
+
|
157
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/flavorjones/nokogiri-html5-inference. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/flavorjones/nokogiri-html5-inference/blob/main/CODE_OF_CONDUCT.md).
|
158
|
+
|
159
|
+
|
160
|
+
## License
|
161
|
+
|
162
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
163
|
+
|
164
|
+
|
165
|
+
## Code of Conduct
|
166
|
+
|
167
|
+
Everyone interacting in the Nokogiri::Html5::Inference project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/flavorjones/nokogiri-html5-inference/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,193 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "inference/version"
|
4
|
+
|
5
|
+
require "nokogiri"
|
6
|
+
|
7
|
+
if defined?(Nokogiri::HTML5::Inference) && Nokogiri::HTML5::Inference.respond_to?(:parse)
|
8
|
+
# keep option open of merging into Nokogiri some day
|
9
|
+
warn "NOTE: nokogiri-html5-inference is already loaded. Check for pinned issues at https://github.com/flavorjones/nokogiri-html5-inference/issues for more information."
|
10
|
+
else
|
11
|
+
module Nokogiri
|
12
|
+
module HTML5
|
13
|
+
# :markup: markdown
|
14
|
+
#
|
15
|
+
# The [HTML5 Spec](https://html.spec.whatwg.org/multipage/parsing.html) defines some very precise
|
16
|
+
# context-dependent parsing rules which can make it challenging to "just parse" a fragment of HTML
|
17
|
+
# without knowing the parent node -- also called the "context node" -- in which it will be inserted.
|
18
|
+
#
|
19
|
+
# Most content in an HTML5 document can be parsed assuming the parser's mode will be in the
|
20
|
+
# ["in body" insertion mode](https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-inbody),
|
21
|
+
# but there are some notable exceptions. Perhaps the most problematic to web developers are the
|
22
|
+
# table-related tags, which will not be parsed properly unless the parser is in the
|
23
|
+
# ["in table" insertion mode](https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-intable).
|
24
|
+
#
|
25
|
+
# For example:
|
26
|
+
#
|
27
|
+
# ``` ruby
|
28
|
+
# Nokogiri::HTML5::DocumentFragment.parse("<td>foo</td>").to_html
|
29
|
+
# # => "foo"
|
30
|
+
# ```
|
31
|
+
#
|
32
|
+
# In the default "in body" mode, the parser will log an error, "Start tag 'td' isn't allowed here",
|
33
|
+
# and drop the tag. This fragment must be parsed "in the context" of a table in order to parse
|
34
|
+
# properly. Thankfully, libgumbo and Nokogiri allow us to do this:
|
35
|
+
#
|
36
|
+
# ``` ruby
|
37
|
+
# Nokogiri::HTML5::DocumentFragment.new(
|
38
|
+
# Nokogiri::HTML5::Document.new,
|
39
|
+
# "<td>foo</td>",
|
40
|
+
# "table" # this is the context node
|
41
|
+
# ).to_html
|
42
|
+
# # => "<tbody><tr><td>foo</td></tr></tbody>"
|
43
|
+
# ```
|
44
|
+
#
|
45
|
+
# This is _almost_ correct, but we're seeing another HTML5 parsing rule in action: there may be
|
46
|
+
# _intermediate parent tags_ that the HTML5 spec requires to be inserted by the parser. In this case,
|
47
|
+
# the `<td>` tag must be wrapped in `<tbody><tr>` tags.
|
48
|
+
#
|
49
|
+
# We can narrow down the result set with an XPath query to get back only the intended tags:
|
50
|
+
#
|
51
|
+
# ``` ruby
|
52
|
+
# Nokogiri::HTML5::DocumentFragment.new(
|
53
|
+
# Nokogiri::HTML5::Document.new,
|
54
|
+
# "<td>foo</td>",
|
55
|
+
# "table" # this is the context node
|
56
|
+
# ).xpath("tbody/tr/*").to_html
|
57
|
+
# # => "<td>foo</td>"
|
58
|
+
# ```
|
59
|
+
#
|
60
|
+
# Hurrah! This is precisely what Nokogiri::HTML5::Inference.parse does: make reasonable inferences
|
61
|
+
# that work for both HTML5 documents and HTML5 fragments, and for all the different HTML5 tags that a
|
62
|
+
# web developer might need in a view library.
|
63
|
+
#
|
64
|
+
module Inference
|
65
|
+
# Tags that must be parsed in a specific HTML5 insertion mode, for which we must use a
|
66
|
+
# context node.
|
67
|
+
module ContextTags # :nodoc:
|
68
|
+
TABLE = %w[thead tbody tfoot tr td th col colgroup caption].freeze
|
69
|
+
HTML = %w[head body].freeze
|
70
|
+
end
|
71
|
+
|
72
|
+
# Regular expressions used to determine if we need to use a context node.
|
73
|
+
module ContextRegexp # :nodoc:
|
74
|
+
DOCUMENT = /\A\s*(<!doctype\s+html\b|<html\b)/i
|
75
|
+
TABLE = /\A\s*<(#{ContextTags::TABLE.join("|")})\b/i
|
76
|
+
HTML = /\A\s*<(#{ContextTags::HTML.join("|")})\b/i
|
77
|
+
end
|
78
|
+
|
79
|
+
# Tags that get an intermediate parent created for them according to the HTML5 spec.
|
80
|
+
module PluckTags # :nodoc:
|
81
|
+
TBODY = %w[tr].freeze
|
82
|
+
TBODY_TR = %w[td th].freeze
|
83
|
+
COLGROUP = %w[col].freeze
|
84
|
+
end
|
85
|
+
|
86
|
+
# Regular expressions used to determine if we will need to skip an intermediate parent or
|
87
|
+
# otherwise narrow the fragment DOM that is returned.
|
88
|
+
module PluckRegexp # :nodoc:
|
89
|
+
TBODY = /\A\s*<(#{PluckTags::TBODY.join("|")})\b/i
|
90
|
+
TBODY_TR = /\A\s*<(#{PluckTags::TBODY_TR.join("|")})\b/i
|
91
|
+
COLGROUP = /\A\s*<(#{PluckTags::COLGROUP.join("|")})\b/i
|
92
|
+
HEAD_OUTER = /\A\s*<(head)\b/i
|
93
|
+
BODY_OUTER = /\A\s*<(body)\b/i
|
94
|
+
end
|
95
|
+
|
96
|
+
class << self
|
97
|
+
#
|
98
|
+
# call-seq:
|
99
|
+
# parse(input, pluck: true) => (Nokogiri::HTML5::Document | Nokogiri::HTML5::DocumentFragment | Nokogiri::XML::NodeSet)
|
100
|
+
#
|
101
|
+
# Based on the start of the input HTML5 string, guess whether it's a full document or a
|
102
|
+
# fragment and, using the fragment context node if necessary, parse it properly and
|
103
|
+
# return the correct set of nodes.
|
104
|
+
#
|
105
|
+
# The keyword parameter +pluck+ can be set to +false+ to disable the narrowing of a
|
106
|
+
# parsed fragment to omit any intermediate parent nodes. This "plucking" is necessary,
|
107
|
+
# for example, when the input fragment begins with "<td>", which the HTML5 spec requires
|
108
|
+
# to be wrapped in <tt><tbody><tr>...</tr></tbody></tt> tags. By default, this method
|
109
|
+
# will return only the children of <tt><tbody><tr></tt>, but setting this flag to +false+
|
110
|
+
# will return the +tbody+ tag and its children.
|
111
|
+
#
|
112
|
+
# [Parameters]
|
113
|
+
# - +input+ (String) The input HTML5 string, which may represent a document or a fragment.
|
114
|
+
#
|
115
|
+
# [Keyword Parameters]
|
116
|
+
# - +pluck+ (Boolean) Default: +true+. Set to +false+ if you want the method to always
|
117
|
+
# return <tt>DocumentFragment</tt>s as-parsed, without attempting to remove
|
118
|
+
# intermediate parent nodes. This shouldn't be necessary if the library is working
|
119
|
+
# properly, but may be useful to allow user to work around a bad guess.
|
120
|
+
#
|
121
|
+
# [Returns]
|
122
|
+
# - A +Nokogiri::HTML5::Document+ if the input appears to represent a full document.
|
123
|
+
# - A +Nokogiri::HTML5::DocumentFragment+ or a +Nokogiri::XML::NodeSet+ if the input
|
124
|
+
# appears to be a fragment.
|
125
|
+
#
|
126
|
+
def parse(input, pluck: true)
|
127
|
+
context = Nokogiri::HTML5::Inference.context(input)
|
128
|
+
if context.nil?
|
129
|
+
Nokogiri::HTML5::Document.parse(input)
|
130
|
+
else
|
131
|
+
fragment = Nokogiri::HTML5::DocumentFragment.new(Nokogiri::HTML5::Document.new, input, context)
|
132
|
+
if pluck && (path = pluck_path(input))
|
133
|
+
fragment.xpath(path)
|
134
|
+
else
|
135
|
+
fragment
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
#
|
141
|
+
# call-seq: context(input) => (String | nil)
|
142
|
+
#
|
143
|
+
# Based on the start of the input HTML5 string, make a guess about whether it's a full
|
144
|
+
# document or a document fragment; and if it's a fragment, whether we need to parse it
|
145
|
+
# within a specific context node.
|
146
|
+
#
|
147
|
+
# [Parameters]
|
148
|
+
# - +input+ (String) The input HTML5 string, which may represent a document or a fragment.
|
149
|
+
#
|
150
|
+
# [Returns]
|
151
|
+
# The String name of the context node required to parse the fragment, or +nil+ if the
|
152
|
+
# input represents a full document.
|
153
|
+
#
|
154
|
+
def context(input) # :nodoc:
|
155
|
+
case input
|
156
|
+
when ContextRegexp::DOCUMENT then nil
|
157
|
+
when ContextRegexp::TABLE then "table"
|
158
|
+
when ContextRegexp::HTML then "html"
|
159
|
+
else "body"
|
160
|
+
end
|
161
|
+
end
|
162
|
+
protected :context
|
163
|
+
|
164
|
+
#
|
165
|
+
# call-seq: pluck_path(input) => (String | nil)
|
166
|
+
#
|
167
|
+
# Based on the start of the input HTML5 fragment string, determine whether the fragment
|
168
|
+
# will need to be selected out of a parent node. This is necessary, for example, when the
|
169
|
+
# fragment begins with "<td>", a tag which the HTML5 spec requires to be wrapped in
|
170
|
+
# "<tbody><tr>...</tr></tbody>".
|
171
|
+
#
|
172
|
+
# [Parameters]
|
173
|
+
# - +input+ (String) The input HTML5 string, which should represent a fragment (not a full document).
|
174
|
+
#
|
175
|
+
# [Returns]
|
176
|
+
# The String XPath query of the context node required to parse the fragment, or +nil+
|
177
|
+
# if no plucking is necessary.
|
178
|
+
#
|
179
|
+
def pluck_path(input) # :nodoc:
|
180
|
+
case input
|
181
|
+
when PluckRegexp::TBODY then "tbody/*"
|
182
|
+
when PluckRegexp::TBODY_TR then "tbody/tr/*"
|
183
|
+
when PluckRegexp::COLGROUP then "colgroup/*"
|
184
|
+
when PluckRegexp::HEAD_OUTER then "head"
|
185
|
+
when PluckRegexp::BODY_OUTER then "body"
|
186
|
+
end
|
187
|
+
end
|
188
|
+
protected :pluck_path
|
189
|
+
end
|
190
|
+
end
|
191
|
+
end
|
192
|
+
end
|
193
|
+
end
|
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nokogiri-html5-inference
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mike Dalessio
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-04-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: nokogiri
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.14'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.14'
|
27
|
+
description: |
|
28
|
+
Infer from the HTML5 input whether it's a fragment or a document, and if it's a fragment what
|
29
|
+
the proper context node should be. This is useful for parsing trusted content like view
|
30
|
+
snippets, particularly for morphing cases like StimulusReflex.
|
31
|
+
email:
|
32
|
+
- mike.dalessio@gmail.com
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- ".rdoc_options"
|
38
|
+
- CHANGELOG.md
|
39
|
+
- CODE_OF_CONDUCT.md
|
40
|
+
- LICENSE.txt
|
41
|
+
- README.md
|
42
|
+
- Rakefile
|
43
|
+
- lib/nokogiri/html5/inference.rb
|
44
|
+
- lib/nokogiri/html5/inference/version.rb
|
45
|
+
homepage: https://github.com/flavorjones/nokogiri-html5-inference
|
46
|
+
licenses:
|
47
|
+
- MIT
|
48
|
+
metadata:
|
49
|
+
homepage_uri: https://github.com/flavorjones/nokogiri-html5-inference
|
50
|
+
source_code_uri: https://github.com/flavorjones/nokogiri-html5-inference
|
51
|
+
bug_tracker_uri: https://github.com/flavorjones/nokogiri-html5-inference/issues
|
52
|
+
changelog_uri: https://github.com/flavorjones/nokogiri-html5-inference/blob/main/CHANGELOG.md
|
53
|
+
post_install_message:
|
54
|
+
rdoc_options: []
|
55
|
+
require_paths:
|
56
|
+
- lib
|
57
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 3.0.0
|
62
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
67
|
+
requirements: []
|
68
|
+
rubygems_version: 3.5.9
|
69
|
+
signing_key:
|
70
|
+
specification_version: 4
|
71
|
+
summary: Given HTML5 input, make a reasonable guess at how to parse it correctly.
|
72
|
+
test_files: []
|