Module: Canon::Html::NokogiriSupport

Defined in:
lib/canon/html/nokogiri_support.rb

Overview

Nokogiri node utilities for the HTML pipeline.

HTML parsing is Nokogiri's job on every CRuby runtime regardless of the XML engine (XmlBackend): moxml has no HTML adapter and leptris no HTML parser. Under Opal Nokogiri is unavailable and these helpers degrade the same way the moxml backend always did (false / nil / raise), which keeps HTML comparison unsupported there.

Class Method Summary collapse

Class Method Details

.document_fragment?(node) ⇒ Boolean

Whether the node is a document fragment (any variant).

Returns:

  • (Boolean)


15
16
17
18
19
20
21
# File 'lib/canon/html/nokogiri_support.rb', line 15

def document_fragment?(node)
  return false unless defined?(Nokogiri)

  node.is_a?(Nokogiri::XML::DocumentFragment) ||
    node.is_a?(Nokogiri::HTML4::DocumentFragment) ||
    node.is_a?(Nokogiri::HTML5::DocumentFragment)
end

.html_document?(node) ⇒ Boolean

Whether the node is an HTML document (any variant).

Returns:

  • (Boolean)


24
25
26
27
28
29
30
# File 'lib/canon/html/nokogiri_support.rb', line 24

def html_document?(node)
  return false unless defined?(Nokogiri)

  node.is_a?(Nokogiri::HTML::Document) ||
    node.is_a?(Nokogiri::HTML4::Document) ||
    node.is_a?(Nokogiri::HTML5::Document)
end

.html_version_from_node(node) ⇒ Object

Detect HTML version from a Nokogiri node. Returns :html5 or :html4. Defaults to :html5 for non-Nokogiri nodes.



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/canon/html/nokogiri_support.rb', line 34

def html_version_from_node(node)
  return :html5 unless defined?(Nokogiri)

  if node.is_a?(Nokogiri::HTML5::Document) ||
      node.is_a?(Nokogiri::HTML5::DocumentFragment)
    :html5
  elsif node.is_a?(Nokogiri::HTML4::Document) ||
      node.is_a?(Nokogiri::HTML4::DocumentFragment)
    :html4
  else
    :html5
  end
end

.xml_fragment(html_string) ⇒ Object

Parse an HTML string into an XML fragment for the XML comparator.



49
50
51
52
53
54
55
56
# File 'lib/canon/html/nokogiri_support.rb', line 49

def xml_fragment(html_string)
  unless defined?(Nokogiri)
    raise Canon::Error,
          "HTML fragment parsing requires Nokogiri"
  end

  Nokogiri::XML.fragment(html_string)
end