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

Class Attribute Details

.currentObject



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



72
73
74
# File 'lib/dommy/backend.rb', line 72

def add_namespace_definition(node, prefix, href)
  current.add_namespace_definition(node, prefix, href)
end

.comment_classObject



90
91
92
# File 'lib/dommy/backend.rb', line 90

def comment_class
  current::Comment
end

.create_comment(content, doc) ⇒ Object



64
65
66
# File 'lib/dommy/backend.rb', line 64

def create_comment(content, doc)
  current.create_comment(content, doc)
end

.create_element(name, doc) ⇒ Object



56
57
58
# File 'lib/dommy/backend.rb', line 56

def create_element(name, doc)
  current.create_element(name, doc)
end

.create_text(content, doc) ⇒ Object



60
61
62
# File 'lib/dommy/backend.rb', line 60

def create_text(content, doc)
  current.create_text(content, doc)
end

.document_classObject



82
83
84
# File 'lib/dommy/backend.rb', line 82

def document_class
  current::Document
end

.document_fragment_classObject



94
95
96
# File 'lib/dommy/backend.rb', line 94

def document_fragment_class
  current::DocumentFragment
end

.element_classObject

Type constants — proxy through to the current backend so ‘node.is_a?(Backend::Element)` resolves dynamically.



78
79
80
# File 'lib/dommy/backend.rb', line 78

def element_class
  current::Element
end

.fragment(html, owner_doc:) ⇒ Object



52
53
54
# File 'lib/dommy/backend.rb', line 52

def fragment(html, owner_doc:)
  current.fragment(html, owner_doc: owner_doc)
end

.namespace_of(node) ⇒ Object



68
69
70
# File 'lib/dommy/backend.rb', line 68

def namespace_of(node)
  current.namespace_of(node)
end

.node_classObject



98
99
100
# File 'lib/dommy/backend.rb', line 98

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

.text_classObject



86
87
88
# File 'lib/dommy/backend.rb', line 86

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