lobzique 0.1.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 +7 -0
- data/CHANGELOG.md +5 -0
- data/Cargo.lock +520 -0
- data/Cargo.toml +11 -0
- data/LICENSE-NOKOGIRI.md +15 -0
- data/LICENSE.txt +21 -0
- data/README.md +144 -0
- data/crates/lobzique-core/Cargo.toml +14 -0
- data/crates/lobzique-core/src/css/mod.rs +127 -0
- data/crates/lobzique-core/src/css/parser.rs +805 -0
- data/crates/lobzique-core/src/css/tables.rs +282 -0
- data/crates/lobzique-core/src/css/tests.rs +476 -0
- data/crates/lobzique-core/src/css/tokenizer.rs +215 -0
- data/crates/lobzique-core/src/css/translate.rs +806 -0
- data/crates/lobzique-core/src/css/write.rs +370 -0
- data/crates/lobzique-core/src/dom/attribute.rs +144 -0
- data/crates/lobzique-core/src/dom/check.rs +160 -0
- data/crates/lobzique-core/src/dom/copy.rs +229 -0
- data/crates/lobzique-core/src/dom/mod.rs +298 -0
- data/crates/lobzique-core/src/dom/namespace.rs +134 -0
- data/crates/lobzique-core/src/dom/node.rs +129 -0
- data/crates/lobzique-core/src/dom/path.rs +116 -0
- data/crates/lobzique-core/src/dom/reparent.rs +308 -0
- data/crates/lobzique-core/src/dom/tests.rs +1057 -0
- data/crates/lobzique-core/src/dom/text.rs +159 -0
- data/crates/lobzique-core/src/dom/tree.rs +446 -0
- data/crates/lobzique-core/src/html5/mod.rs +28 -0
- data/crates/lobzique-core/src/html5/parse.rs +838 -0
- data/crates/lobzique-core/src/html5/serialize.rs +181 -0
- data/crates/lobzique-core/src/html5/tests.rs +548 -0
- data/crates/lobzique-core/src/lib.rs +12 -0
- data/crates/lobzique-core/src/work.rs +26 -0
- data/crates/lobzique-core/src/xml/mod.rs +8 -0
- data/crates/lobzique-core/src/xml/serialize.rs +681 -0
- data/crates/lobzique-core/src/xml/tests.rs +174 -0
- data/crates/lobzique-core/src/xpath/ast.rs +415 -0
- data/crates/lobzique-core/src/xpath/chars.rs +139 -0
- data/crates/lobzique-core/src/xpath/eval.rs +1305 -0
- data/crates/lobzique-core/src/xpath/mod.rs +118 -0
- data/crates/lobzique-core/src/xpath/parse.rs +729 -0
- data/crates/lobzique-core/src/xpath/tests.rs +921 -0
- data/ext/lobzique/Cargo.toml +22 -0
- data/ext/lobzique/build.rs +5 -0
- data/ext/lobzique/clippy.toml +15 -0
- data/ext/lobzique/extconf.rb +6 -0
- data/ext/lobzique/src/css/mod.rs +9 -0
- data/ext/lobzique/src/css/parser.rs +44 -0
- data/ext/lobzique/src/css/selector.rs +101 -0
- data/ext/lobzique/src/html4/document.rs +31 -0
- data/ext/lobzique/src/html4/mod.rs +7 -0
- data/ext/lobzique/src/html5/mod.rs +16 -0
- data/ext/lobzique/src/html5ever.rs +278 -0
- data/ext/lobzique/src/lib.rs +45 -0
- data/ext/lobzique/src/support.rs +156 -0
- data/ext/lobzique/src/xml/attr.rs +27 -0
- data/ext/lobzique/src/xml/cdata.rs +33 -0
- data/ext/lobzique/src/xml/comment.rs +34 -0
- data/ext/lobzique/src/xml/document.rs +199 -0
- data/ext/lobzique/src/xml/document_fragment.rs +27 -0
- data/ext/lobzique/src/xml/dtd.rs +37 -0
- data/ext/lobzique/src/xml/mod.rs +44 -0
- data/ext/lobzique/src/xml/namespace.rs +56 -0
- data/ext/lobzique/src/xml/node.rs +944 -0
- data/ext/lobzique/src/xml/node_set.rs +20 -0
- data/ext/lobzique/src/xml/syntax_error.rs +33 -0
- data/ext/lobzique/src/xml/text.rs +44 -0
- data/ext/lobzique/src/xml/xpath_context.rs +305 -0
- data/lib/lobzique/action_text.rb +29 -0
- data/lib/lobzique/class_resolver.rb +33 -0
- data/lib/lobzique/css/selector_cache.rb +36 -0
- data/lib/lobzique/css/syntax_error.rb +12 -0
- data/lib/lobzique/css/xpath_visitor.rb +60 -0
- data/lib/lobzique/css.rb +57 -0
- data/lib/lobzique/extension.rb +31 -0
- data/lib/lobzique/html.rb +14 -0
- data/lib/lobzique/html4/document.rb +235 -0
- data/lib/lobzique/html4/document_fragment.rb +75 -0
- data/lib/lobzique/html4/encoding_reader.rb +374 -0
- data/lib/lobzique/html4.rb +35 -0
- data/lib/lobzique/html5/document.rb +106 -0
- data/lib/lobzique/html5/document_fragment.rb +113 -0
- data/lib/lobzique/html5/node.rb +66 -0
- data/lib/lobzique/html5.rb +92 -0
- data/lib/lobzique/html5ever.rb +14 -0
- data/lib/lobzique/rails_dom_testing.rb +95 -0
- data/lib/lobzique/sanitizer/concerns.rb +110 -0
- data/lib/lobzique/sanitizer/document.rb +14 -0
- data/lib/lobzique/sanitizer/document_fragment.rb +12 -0
- data/lib/lobzique/sanitizer/escape_raw_text_in_select.rb +31 -0
- data/lib/lobzique/sanitizer/scrubbers.rb +46 -0
- data/lib/lobzique/sanitizer.rb +96 -0
- data/lib/lobzique/syntax_error.rb +8 -0
- data/lib/lobzique/version.rb +6 -0
- data/lib/lobzique/xml/attr.rb +23 -0
- data/lib/lobzique/xml/cdata.rb +13 -0
- data/lib/lobzique/xml/character_data.rb +11 -0
- data/lib/lobzique/xml/document.rb +149 -0
- data/lib/lobzique/xml/document_fragment.rb +109 -0
- data/lib/lobzique/xml/dtd.rb +28 -0
- data/lib/lobzique/xml/namespace.rb +19 -0
- data/lib/lobzique/xml/node/save_options.rb +52 -0
- data/lib/lobzique/xml/node.rb +629 -0
- data/lib/lobzique/xml/node_set.rb +329 -0
- data/lib/lobzique/xml/parse_options.rb +89 -0
- data/lib/lobzique/xml/pp/character_data.rb +22 -0
- data/lib/lobzique/xml/pp/node.rb +73 -0
- data/lib/lobzique/xml/pp.rb +6 -0
- data/lib/lobzique/xml/searchable.rb +123 -0
- data/lib/lobzique/xml/syntax_error.rb +77 -0
- data/lib/lobzique/xml/text.rb +14 -0
- data/lib/lobzique/xml/xpath/syntax_error.rb +20 -0
- data/lib/lobzique/xml/xpath.rb +16 -0
- data/lib/lobzique/xml/xpath_context.rb +27 -0
- data/lib/lobzique/xml.rb +22 -0
- data/lib/lobzique.rb +15 -0
- metadata +177 -0
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Adapted from Loofah 2.25.3, lib/loofah/scrubbers.rb (MIT): the scrubbers
|
|
4
|
+
# that build nodes with Nokogiri::XML::Text.new, which takes only a Nokogiri
|
|
5
|
+
# document. The others run on lobzique nodes as they are.
|
|
6
|
+
|
|
7
|
+
module Lobzique
|
|
8
|
+
module Sanitizer
|
|
9
|
+
module Scrubbers
|
|
10
|
+
class Escape < Loofah::Scrubbers::Escape
|
|
11
|
+
def scrub(node)
|
|
12
|
+
return CONTINUE if html5lib_sanitize(node) == CONTINUE
|
|
13
|
+
|
|
14
|
+
# lobzique: create_text_node, not Nokogiri::XML::Text.new.
|
|
15
|
+
node.add_next_sibling(node.document.create_text_node(node.to_s))
|
|
16
|
+
node.remove
|
|
17
|
+
STOP
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
class NewlineBlockElements < Loofah::Scrubbers::NewlineBlockElements # :nodoc:
|
|
22
|
+
def scrub(node)
|
|
23
|
+
return CONTINUE unless Loofah::Elements::LINEBREAKERS.include?(node.name)
|
|
24
|
+
|
|
25
|
+
replacement = if Loofah::Elements::INLINE_LINE_BREAK.include?(node.name)
|
|
26
|
+
"\n"
|
|
27
|
+
else
|
|
28
|
+
"\n#{node.content}\n"
|
|
29
|
+
end
|
|
30
|
+
# lobzique: create_text_node, not Nokogiri::XML::Text.new.
|
|
31
|
+
node.add_next_sibling(node.document.create_text_node(replacement))
|
|
32
|
+
node.remove
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Overrides of Loofah::Scrubbers::MAP.
|
|
37
|
+
MAP = {
|
|
38
|
+
escape: Escape,
|
|
39
|
+
newline_block_elements: NewlineBlockElements
|
|
40
|
+
}.freeze
|
|
41
|
+
|
|
42
|
+
# The Loofah classes that they port. Their instances hold no state.
|
|
43
|
+
PORTS = MAP.values.to_h { |port| [port.superclass, port] }.freeze
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# A rails-html-sanitizer vendor that parses, scrubs and serializes with
|
|
4
|
+
# lobzique (plan.md phase 3):
|
|
5
|
+
#
|
|
6
|
+
# config.action_view.sanitizer_vendor = Lobzique::Sanitizer
|
|
7
|
+
# config.action_text.sanitizer_vendor = Lobzique::Sanitizer
|
|
8
|
+
#
|
|
9
|
+
# The scrubbers of Loofah and Rails run unchanged on lobzique nodes. Only
|
|
10
|
+
# the documents and scrub! of Loofah, and the scrubbers that build Nokogiri
|
|
11
|
+
# nodes, are ported.
|
|
12
|
+
|
|
13
|
+
require "lobzique"
|
|
14
|
+
require "loofah"
|
|
15
|
+
require "action_view/helpers" if defined?(ActionView) && ActionView.autoload?(:Helpers)
|
|
16
|
+
require "rails-html-sanitizer"
|
|
17
|
+
|
|
18
|
+
module Lobzique
|
|
19
|
+
module Sanitizer
|
|
20
|
+
# The vendor classes include the concerns of rails-html-sanitizer,
|
|
21
|
+
# which are internals. Only the versions that the tests ran on are accepted.
|
|
22
|
+
RAILS_HTML_SANITIZER_REQUIREMENT = Gem::Requirement.new(">= 1.7.1", "< 1.8")
|
|
23
|
+
|
|
24
|
+
unless RAILS_HTML_SANITIZER_REQUIREMENT.satisfied_by?(Gem::Version.new(Rails::HTML::Sanitizer::VERSION))
|
|
25
|
+
raise "#{self} needs rails-html-sanitizer #{RAILS_HTML_SANITIZER_REQUIREMENT}, " \
|
|
26
|
+
"not #{Rails::HTML::Sanitizer::VERSION}"
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
require_relative "sanitizer/escape_raw_text_in_select"
|
|
32
|
+
require_relative "sanitizer/concerns"
|
|
33
|
+
require_relative "sanitizer/scrubbers"
|
|
34
|
+
require_relative "sanitizer/document"
|
|
35
|
+
require_relative "sanitizer/document_fragment"
|
|
36
|
+
|
|
37
|
+
module Lobzique
|
|
38
|
+
module Sanitizer
|
|
39
|
+
# Rails::HTML::Concern::Parser::HTML5, with the fragment of lobzique
|
|
40
|
+
# in place of the fragment of Loofah.
|
|
41
|
+
module Parser
|
|
42
|
+
def parse_fragment(html)
|
|
43
|
+
DocumentFragment.parse(html)
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# The vendor stands in for Rails::HTML5, so its safe list sanitizer reads
|
|
48
|
+
# and writes the lists of Rails::HTML5. Apps and gems edit them on the class
|
|
49
|
+
# of Rails by name.
|
|
50
|
+
module RailsAllowedLists
|
|
51
|
+
%i[allowed_tags allowed_tags= allowed_attributes allowed_attributes=].each do |name|
|
|
52
|
+
define_method(name) { |*args| Rails::HTML5::SafeListSanitizer.public_send(name, *args) }
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
class FullSanitizer < Rails::HTML::Sanitizer
|
|
57
|
+
include Rails::HTML::Concern::ComposedSanitize
|
|
58
|
+
include Parser
|
|
59
|
+
include Rails::HTML::Concern::Scrubber::Full
|
|
60
|
+
include Rails::HTML::Concern::Serializer::UTF8Encode
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
class LinkSanitizer < Rails::HTML::Sanitizer
|
|
64
|
+
include Rails::HTML::Concern::ComposedSanitize
|
|
65
|
+
include Parser
|
|
66
|
+
include Rails::HTML::Concern::Scrubber::Link
|
|
67
|
+
include Rails::HTML::Concern::Serializer::UTF8Encode
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
class SafeListSanitizer < Rails::HTML::Sanitizer
|
|
71
|
+
include Rails::HTML::Concern::ComposedSanitize
|
|
72
|
+
include Parser
|
|
73
|
+
include Rails::HTML::Concern::Scrubber::SafeList
|
|
74
|
+
include Rails::HTML::Concern::Serializer::UTF8Encode
|
|
75
|
+
singleton_class.prepend(RailsAllowedLists)
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
class << self
|
|
79
|
+
def full_sanitizer
|
|
80
|
+
FullSanitizer
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def link_sanitizer
|
|
84
|
+
LinkSanitizer
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def safe_list_sanitizer
|
|
88
|
+
SafeListSanitizer
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def white_list_sanitizer # :nodoc:
|
|
92
|
+
safe_list_sanitizer
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Adapted from Nokogiri 1.19.7, lib/nokogiri/xml/attr.rb (MIT, see LICENSE-NOKOGIRI.md).
|
|
4
|
+
|
|
5
|
+
module Lobzique
|
|
6
|
+
module XML
|
|
7
|
+
class Attr < Node
|
|
8
|
+
alias_method :value, :content
|
|
9
|
+
alias_method :to_s, :content
|
|
10
|
+
alias_method :content=, :value=
|
|
11
|
+
|
|
12
|
+
def deconstruct_keys(keys)
|
|
13
|
+
{name: name, value: value, namespace: namespace}
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
private
|
|
17
|
+
|
|
18
|
+
def inspect_attributes
|
|
19
|
+
[:name, :namespace, :value]
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Adapted from Nokogiri 1.19.7, lib/nokogiri/xml/document.rb (MIT, see LICENSE-NOKOGIRI.md).
|
|
4
|
+
|
|
5
|
+
module Lobzique
|
|
6
|
+
module XML
|
|
7
|
+
class Document < Node
|
|
8
|
+
# The xmlns keys #create_element treats as namespace definitions.
|
|
9
|
+
NCNAME_START_CHAR = "A-Za-z_"
|
|
10
|
+
NCNAME_CHAR = NCNAME_START_CHAR + "\\-\\.0-9"
|
|
11
|
+
NCNAME_RE = /^xmlns(?::([#{NCNAME_START_CHAR}][#{NCNAME_CHAR}]*))?$/
|
|
12
|
+
|
|
13
|
+
OBJECT_DUP_METHOD = Object.instance_method(:dup)
|
|
14
|
+
OBJECT_CLONE_METHOD = Object.instance_method(:clone)
|
|
15
|
+
private_constant :OBJECT_DUP_METHOD, :OBJECT_CLONE_METHOD
|
|
16
|
+
|
|
17
|
+
attr_accessor :errors
|
|
18
|
+
attr_accessor :namespace_inheritance
|
|
19
|
+
|
|
20
|
+
# lobzique: native in Nokogiri (URL of libxml2). The HTML parsers keep
|
|
21
|
+
# it in @url, as the HTML5 parser of Nokogiri does.
|
|
22
|
+
attr_reader :url
|
|
23
|
+
|
|
24
|
+
# Every way of making a document runs this, parsing included,
|
|
25
|
+
# so that subclasses (the ones of Loofah) can extend it with `super`.
|
|
26
|
+
def initialize(*args) # rubocop:disable Lint/MissingSuper
|
|
27
|
+
@errors = []
|
|
28
|
+
@decorators = nil
|
|
29
|
+
@namespace_inheritance = false
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# A copy of the document, with its whole tree unless +level+ is 0.
|
|
33
|
+
def dup(level = 1)
|
|
34
|
+
copy = OBJECT_DUP_METHOD.bind_call(self)
|
|
35
|
+
copy.initialize_copy_with_args(self, level)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def clone(level = 1)
|
|
39
|
+
copy = OBJECT_CLONE_METHOD.bind_call(self)
|
|
40
|
+
copy.initialize_copy_with_args(self, level)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# An element named +name+. Each argument is a Hash of attributes or
|
|
44
|
+
# the element's content; the block gets the element first.
|
|
45
|
+
def create_element(name, *contents_or_attrs, &block)
|
|
46
|
+
elm = XML::Element.new(name, self, &block)
|
|
47
|
+
contents_or_attrs.each do |arg|
|
|
48
|
+
case arg
|
|
49
|
+
when Hash
|
|
50
|
+
arg.each do |k, v|
|
|
51
|
+
key = k.to_s
|
|
52
|
+
if NCNAME_RE.match?(key)
|
|
53
|
+
# lobzique: Nokogiri adds a namespace definition here, and makes
|
|
54
|
+
# an unprefixed one the namespace of the element. Adding
|
|
55
|
+
# definitions is out of scope.
|
|
56
|
+
raise NotImplementedError, "namespace definitions can't be added"
|
|
57
|
+
else
|
|
58
|
+
elm[k.to_s] = v.to_s
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
else
|
|
62
|
+
elm.content = arg
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
elm
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def create_text_node(string, &block)
|
|
69
|
+
Text.new(string.to_s, self, &block)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def create_cdata(string, &block)
|
|
73
|
+
CDATA.new(self, string.to_s, &block)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def create_comment(string, &block)
|
|
77
|
+
Comment.new(self, string.to_s, &block)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def name
|
|
81
|
+
"document"
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def document
|
|
85
|
+
self
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# The modules extended into every node and node set of this document,
|
|
89
|
+
# by the class they apply to.
|
|
90
|
+
def decorators(key)
|
|
91
|
+
@decorators ||= {}
|
|
92
|
+
@decorators[key] ||= []
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def decorate(node)
|
|
96
|
+
return unless @decorators
|
|
97
|
+
|
|
98
|
+
@decorators.each do |klass, list|
|
|
99
|
+
next unless node.is_a?(klass)
|
|
100
|
+
|
|
101
|
+
list.each { |mod| node.extend(mod) }
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
alias_method :to_xml, :serialize
|
|
106
|
+
|
|
107
|
+
def namespaces
|
|
108
|
+
root ? root.namespaces : {}
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
# lobzique: Nokogiri also undefines default_namespace=,
|
|
112
|
+
# add_namespace_definition, line and add_namespace, which are not defined
|
|
113
|
+
# here.
|
|
114
|
+
undef_method :swap, :parent, :namespace, :attributes, :namespace_definitions
|
|
115
|
+
|
|
116
|
+
def add_child(node_or_tags)
|
|
117
|
+
raise "A document may not have multiple root nodes." if root && root.name != "nokogiri_text_wrapper" && !(node_or_tags.comment? || node_or_tags.processing_instruction?)
|
|
118
|
+
|
|
119
|
+
node_or_tags = coerce(node_or_tags)
|
|
120
|
+
if node_or_tags.is_a?(XML::NodeSet)
|
|
121
|
+
raise "A document may not have multiple root nodes." if node_or_tags.size > 1
|
|
122
|
+
|
|
123
|
+
super(node_or_tags.first)
|
|
124
|
+
else
|
|
125
|
+
super
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
alias_method :<<, :add_child
|
|
129
|
+
|
|
130
|
+
# lobzique: Nokogiri returns CSS::XPathVisitor::DoctypeConfig::XML,
|
|
131
|
+
# which is this symbol. CSS::Selector takes it.
|
|
132
|
+
def xpath_doctype
|
|
133
|
+
:xml
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
def deconstruct_keys(keys)
|
|
137
|
+
{root: root}
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
private
|
|
141
|
+
|
|
142
|
+
IMPLIED_XPATH_CONTEXTS = ["//"].freeze # :nodoc:
|
|
143
|
+
|
|
144
|
+
def inspect_attributes
|
|
145
|
+
[:name, :children]
|
|
146
|
+
end
|
|
147
|
+
end
|
|
148
|
+
end
|
|
149
|
+
end
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Adapted from Nokogiri 1.19.7, lib/nokogiri/xml/document_fragment.rb (MIT, see LICENSE-NOKOGIRI.md).
|
|
4
|
+
|
|
5
|
+
module Lobzique
|
|
6
|
+
module XML
|
|
7
|
+
class DocumentFragment < Node
|
|
8
|
+
attr_reader :parse_options
|
|
9
|
+
|
|
10
|
+
class << self
|
|
11
|
+
# The native node is made, wrapped and decorated first. `initialize`
|
|
12
|
+
# then fills it. Subclasses override `initialize`.
|
|
13
|
+
def new(document, ...) # :nodoc:
|
|
14
|
+
instance = native_new(document)
|
|
15
|
+
instance.send(:initialize, document, ...)
|
|
16
|
+
instance
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def initialize(
|
|
21
|
+
document, tags = nil,
|
|
22
|
+
context_ = nil, options_ = ParseOptions::DEFAULT_XML,
|
|
23
|
+
context: context_, options: options_
|
|
24
|
+
) # rubocop:disable Lint/MissingSuper
|
|
25
|
+
return self unless tags # rubocop:disable Lint/ReturnInVoidContext
|
|
26
|
+
|
|
27
|
+
options = ParseOptions.new(options) if Integer === options
|
|
28
|
+
@parse_options = options
|
|
29
|
+
yield options if block_given?
|
|
30
|
+
|
|
31
|
+
children = if context
|
|
32
|
+
context.parse(tags, options)
|
|
33
|
+
else
|
|
34
|
+
# lobzique: XML parsing is out of scope (plan.md decision 4).
|
|
35
|
+
raise NotImplementedError, "#{XML::Document}.parse isn't supported"
|
|
36
|
+
end
|
|
37
|
+
children.each { |child| child.parent = self }
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# A copy in a copy of the document.
|
|
41
|
+
def dup
|
|
42
|
+
new_document = document.dup
|
|
43
|
+
new_fragment = self.class.new(new_document)
|
|
44
|
+
children.each do |child|
|
|
45
|
+
child.dup(1, new_document).parent = new_fragment
|
|
46
|
+
end
|
|
47
|
+
new_fragment
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def name
|
|
51
|
+
"#document-fragment"
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def to_s
|
|
55
|
+
children.to_s
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def to_html(*args)
|
|
59
|
+
children.to_html(*args)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def to_xhtml(*args)
|
|
63
|
+
children.to_xhtml(*args)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def to_xml(*args)
|
|
67
|
+
children.to_xml(*args)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def css(*args)
|
|
71
|
+
if children.any?
|
|
72
|
+
children.css(*args) # 'children' is a smell here
|
|
73
|
+
else
|
|
74
|
+
NodeSet.new(document)
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def search(*rules)
|
|
79
|
+
rules, handler, ns, binds = extract_params(rules)
|
|
80
|
+
|
|
81
|
+
rules.inject(NodeSet.new(document)) do |set, rule|
|
|
82
|
+
set + if Searchable::LOOKS_LIKE_XPATH.match?(rule)
|
|
83
|
+
xpath(*[rule, ns, handler, binds].compact)
|
|
84
|
+
else
|
|
85
|
+
children.css(*[rule, ns, handler].compact) # 'children' is a smell here
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
alias_method :serialize, :to_s
|
|
91
|
+
|
|
92
|
+
def errors
|
|
93
|
+
document.errors
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def errors=(things) # :nodoc:
|
|
97
|
+
document.errors = things
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def fragment(data)
|
|
101
|
+
document.fragment(data)
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def deconstruct
|
|
105
|
+
children.to_a
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Adapted from Nokogiri 1.19.7, lib/nokogiri/xml/dtd.rb (MIT, see LICENSE-NOKOGIRI.md).
|
|
4
|
+
|
|
5
|
+
module Lobzique
|
|
6
|
+
module XML
|
|
7
|
+
class DTD < Node
|
|
8
|
+
undef_method :attribute_nodes
|
|
9
|
+
undef_method :values
|
|
10
|
+
undef_method :content
|
|
11
|
+
undef_method :namespace
|
|
12
|
+
undef_method :namespace_definitions
|
|
13
|
+
# lobzique: Nokogiri also undefines line, which is not defined yet,
|
|
14
|
+
# and defines keys and each over the attribute declarations of the DTD,
|
|
15
|
+
# which are out of scope.
|
|
16
|
+
|
|
17
|
+
def html_dtd?
|
|
18
|
+
name.casecmp("html").zero?
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def html5_dtd?
|
|
22
|
+
html_dtd? &&
|
|
23
|
+
external_id.nil? &&
|
|
24
|
+
(system_id.nil? || system_id == "about:legacy-compat")
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Adapted from Nokogiri 1.19.7, lib/nokogiri/xml/namespace.rb (MIT, see LICENSE-NOKOGIRI.md).
|
|
4
|
+
|
|
5
|
+
module Lobzique
|
|
6
|
+
module XML
|
|
7
|
+
class Namespace
|
|
8
|
+
include PP::Node
|
|
9
|
+
|
|
10
|
+
attr_reader :document
|
|
11
|
+
|
|
12
|
+
private
|
|
13
|
+
|
|
14
|
+
def inspect_attributes
|
|
15
|
+
[:prefix, :href]
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Adapted from Nokogiri 1.19.7, lib/nokogiri/xml/node/save_options.rb (MIT, see LICENSE-NOKOGIRI.md).
|
|
4
|
+
|
|
5
|
+
module Lobzique
|
|
6
|
+
module XML
|
|
7
|
+
class Node
|
|
8
|
+
class SaveOptions
|
|
9
|
+
FORMAT = 1
|
|
10
|
+
NO_DECLARATION = 2
|
|
11
|
+
NO_EMPTY_TAGS = 4
|
|
12
|
+
NO_XHTML = 8
|
|
13
|
+
AS_XHTML = 16
|
|
14
|
+
AS_XML = 32
|
|
15
|
+
AS_HTML = 64
|
|
16
|
+
|
|
17
|
+
DEFAULT_XML = FORMAT | AS_XML
|
|
18
|
+
DEFAULT_HTML = FORMAT | NO_DECLARATION | NO_EMPTY_TAGS | AS_HTML
|
|
19
|
+
DEFAULT_XHTML = FORMAT | NO_DECLARATION | AS_XHTML
|
|
20
|
+
|
|
21
|
+
attr_reader :options
|
|
22
|
+
|
|
23
|
+
def initialize(options = 0)
|
|
24
|
+
@options = options
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
constants.each do |constant|
|
|
28
|
+
class_eval <<~RUBY, __FILE__, __LINE__ + 1
|
|
29
|
+
def #{constant.downcase}
|
|
30
|
+
@options |= #{constant}
|
|
31
|
+
self
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def #{constant.downcase}?
|
|
35
|
+
#{constant} & @options == #{constant}
|
|
36
|
+
end
|
|
37
|
+
RUBY
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
alias_method :to_i, :options
|
|
41
|
+
|
|
42
|
+
def inspect
|
|
43
|
+
options = []
|
|
44
|
+
self.class.constants.each do |k|
|
|
45
|
+
options << k.downcase if send(:"#{k.downcase}?")
|
|
46
|
+
end
|
|
47
|
+
super.sub(/>$/, " " + options.join(", ") + ">")
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|