Module: Dommy::Backend
- Defined in:
- lib/dommy/backend.rb,
lib/dommy/backend/nokogiri_adapter.rb,
lib/dommy/backend/nokolexbor_adapter.rb
Overview
‘Dommy::Backend` — pluggable HTML parser abstraction. Lets Dommy work with either Nokogiri (mature, full namespace support) or Nokolexbor (faster, HTML5-only). Internally, all DOM library code goes through this facade rather than referencing the parser directly.
Defaults to Nokogiri if available, else Nokolexbor.
Switching backends:
require "dommy"
Dommy::Backend.use(:nokolexbor)
Or set directly:
Dommy::Backend.current = Dommy::Backend::Nokolexbor
All adapters must implement the same interface — see ‘Backend::Nokogiri` for the canonical reference.
Defined Under Namespace
Modules: Nokogiri, Nokolexbor
Classes: BackendNotAvailable
Class Attribute Summary collapse
Class Method Summary
collapse
-
.add_namespace_definition(node, prefix, href) ⇒ Object
-
.attribute_nodes(node) ⇒ Object
The element’s attribute nodes (each readable via attribute_ns_info).
-
.attribute_ns_info(attr_node) ⇒ Object
Reads a backend attribute node into prefix:, local_name:, qualified_name:, value: (namespace-aware).
-
.cdata_class ⇒ Object
-
.comment_class ⇒ Object
-
.create_cdata(content, doc) ⇒ Object
CDATA section node (XML documents).
-
.create_comment(content, doc) ⇒ Object
-
.create_element(name, doc) ⇒ Object
-
.create_text(content, doc) ⇒ Object
-
.document_class ⇒ Object
-
.document_fragment_class ⇒ Object
-
.element_class ⇒ Object
Type constants — proxy through to the current backend so ‘node.is_a?(Backend::Element)` resolves dynamically.
-
.fragment(html, owner_doc:) ⇒ Object
-
.get_attribute_ns(node, namespace, local_name) ⇒ Object
Namespaced attribute access (DOM *AttributeNS).
-
.has_attribute_ns?(node, namespace, local_name) ⇒ Boolean
-
.namespace_of(node) ⇒ Object
-
.node_class ⇒ Object
-
.parse(html) ⇒ Object
Delegate calls so internal code can use ‘Backend.parse(…)`.
-
.parse_xml(xml) ⇒ Object
Parse XML input into an XML document.
-
.remove_attribute_ns(node, namespace, local_name) ⇒ Object
-
.set_attribute_ns(node, namespace, prefix, local_name, qualified_name, value) ⇒ Object
-
.text_class ⇒ Object
-
.use(name) ⇒ Object
Class Attribute Details
.current ⇒ Object
28
29
30
|
# File 'lib/dommy/backend.rb', line 28
def current
@current ||= detect_default
end
|
Class Method Details
.add_namespace_definition(node, prefix, href) ⇒ Object
88
89
90
|
# File 'lib/dommy/backend.rb', line 88
def add_namespace_definition(node, prefix, href)
current.add_namespace_definition(node, prefix, href)
end
|
.attribute_nodes(node) ⇒ Object
The element’s attribute nodes (each readable via attribute_ns_info). The single choke point so DOM code doesn’t touch parser internals.
118
119
120
|
# File 'lib/dommy/backend.rb', line 118
def attribute_nodes(node)
current.attribute_nodes(node)
end
|
.attribute_ns_info(attr_node) ⇒ Object
Reads a backend attribute node into prefix:, local_name:, qualified_name:, value: (namespace-aware).
112
113
114
|
# File 'lib/dommy/backend.rb', line 112
def attribute_ns_info(attr_node)
current.attribute_ns_info(attr_node)
end
|
.cdata_class ⇒ Object
80
81
82
|
# File 'lib/dommy/backend.rb', line 80
def cdata_class
current.respond_to?(:cdata_class) ? current.cdata_class : nil
end
|
136
137
138
|
# File 'lib/dommy/backend.rb', line 136
def
current::Comment
end
|
.create_cdata(content, doc) ⇒ Object
CDATA section node (XML documents). Backends without CDATA fall back to a text node.
76
77
78
|
# File 'lib/dommy/backend.rb', line 76
def create_cdata(content, doc)
current.respond_to?(:create_cdata) ? current.create_cdata(content, doc) : current.create_text(content, doc)
end
|
70
71
72
|
# File 'lib/dommy/backend.rb', line 70
def (content, doc)
current.(content, doc)
end
|
.create_element(name, doc) ⇒ Object
62
63
64
|
# File 'lib/dommy/backend.rb', line 62
def create_element(name, doc)
current.create_element(name, doc)
end
|
.create_text(content, doc) ⇒ Object
66
67
68
|
# File 'lib/dommy/backend.rb', line 66
def create_text(content, doc)
current.create_text(content, doc)
end
|
.document_class ⇒ Object
128
129
130
|
# File 'lib/dommy/backend.rb', line 128
def document_class
current::Document
end
|
.document_fragment_class ⇒ Object
140
141
142
|
# File 'lib/dommy/backend.rb', line 140
def document_fragment_class
current::DocumentFragment
end
|
.element_class ⇒ Object
Type constants — proxy through to the current backend so ‘node.is_a?(Backend::Element)` resolves dynamically.
124
125
126
|
# File 'lib/dommy/backend.rb', line 124
def element_class
current::Element
end
|
.fragment(html, owner_doc:) ⇒ Object
58
59
60
|
# File 'lib/dommy/backend.rb', line 58
def fragment(html, owner_doc:)
current.fragment(html, owner_doc: owner_doc)
end
|
.get_attribute_ns(node, namespace, local_name) ⇒ Object
Namespaced attribute access (DOM *AttributeNS). ‘namespace` is an href String or nil. Nokolexbor degrades to qualified-name (null-namespace).
94
95
96
|
# File 'lib/dommy/backend.rb', line 94
def get_attribute_ns(node, namespace, local_name)
current.get_attribute_ns(node, namespace, local_name)
end
|
.has_attribute_ns?(node, namespace, local_name) ⇒ Boolean
106
107
108
|
# File 'lib/dommy/backend.rb', line 106
def has_attribute_ns?(node, namespace, local_name)
current.has_attribute_ns?(node, namespace, local_name)
end
|
.namespace_of(node) ⇒ Object
84
85
86
|
# File 'lib/dommy/backend.rb', line 84
def namespace_of(node)
current.namespace_of(node)
end
|
.node_class ⇒ Object
144
145
146
|
# File 'lib/dommy/backend.rb', line 144
def node_class
current::Node
end
|
.parse(html) ⇒ Object
Delegate calls so internal code can use ‘Backend.parse(…)`.
48
49
50
|
# File 'lib/dommy/backend.rb', line 48
def parse(html)
current.parse(html)
end
|
.parse_xml(xml) ⇒ Object
Parse XML input into an XML document. Backends without a real XML parser (HTML-only, e.g. Nokolexbor) fall back to the HTML parser.
54
55
56
|
# File 'lib/dommy/backend.rb', line 54
def parse_xml(xml)
current.respond_to?(:parse_xml) ? current.parse_xml(xml) : current.parse(xml)
end
|
.remove_attribute_ns(node, namespace, local_name) ⇒ Object
102
103
104
|
# File 'lib/dommy/backend.rb', line 102
def remove_attribute_ns(node, namespace, local_name)
current.remove_attribute_ns(node, namespace, local_name)
end
|
.set_attribute_ns(node, namespace, prefix, local_name, qualified_name, value) ⇒ Object
98
99
100
|
# File 'lib/dommy/backend.rb', line 98
def set_attribute_ns(node, namespace, prefix, local_name, qualified_name, value)
current.set_attribute_ns(node, namespace, prefix, local_name, qualified_name, value)
end
|
.text_class ⇒ Object
132
133
134
|
# File 'lib/dommy/backend.rb', line 132
def text_class
current::Text
end
|
.use(name) ⇒ Object
34
35
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/dommy/backend.rb', line 34
def use(name)
@current = case name.to_sym
when :nokogiri
require_relative "backend/nokogiri_adapter"
Nokogiri
when :nokolexbor
require_relative "backend/nokolexbor_adapter"
Nokolexbor
else
raise ArgumentError, "Unknown backend: #{name.inspect}. Use :nokogiri or :nokolexbor."
end
end
|