Module: Canon::XmlBackend

Defined in:
lib/canon/xml_backend.rb

Class Method Summary collapse

Class Method Details

.activeObject



6
7
8
# File 'lib/canon/xml_backend.rb', line 6

def active
  @active ||= detect
end

.document_fragment?(node) ⇒ Boolean

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

Returns:

  • (Boolean)


23
24
25
26
27
28
29
30
31
# File 'lib/canon/xml_backend.rb', line 23

def document_fragment?(node)
  if nokogiri?
    node.is_a?(Nokogiri::XML::DocumentFragment) ||
      node.is_a?(Nokogiri::HTML4::DocumentFragment) ||
      node.is_a?(Nokogiri::HTML5::DocumentFragment)
  else
    false
  end
end

.html_document?(node) ⇒ Boolean

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

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
42
# File 'lib/canon/xml_backend.rb', line 34

def html_document?(node)
  if nokogiri?
    node.is_a?(Nokogiri::HTML::Document) ||
      node.is_a?(Nokogiri::HTML4::Document) ||
      node.is_a?(Nokogiri::HTML5::Document)
  else
    false
  end
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.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/canon/xml_backend.rb', line 46

def html_version_from_node(node)
  if 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
  else
    :html5
  end
end

.moxml?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/canon/xml_backend.rb', line 14

def moxml?
  active == :moxml
end

.nokogiri?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/canon/xml_backend.rb', line 10

def nokogiri?
  active == :nokogiri
end

.reset!Object



18
19
20
# File 'lib/canon/xml_backend.rb', line 18

def reset!
  @active = nil
end

.xml_fragment(html_string) ⇒ Object

Parse an HTML string into an XML fragment.



63
64
65
66
67
68
69
70
# File 'lib/canon/xml_backend.rb', line 63

def xml_fragment(html_string)
  if nokogiri?
    Nokogiri::XML.fragment(html_string)
  else
    raise Canon::Error,
          "HTML fragment parsing requires the Nokogiri backend"
  end
end