Module: Dommy::Parser

Defined in:
lib/dommy/parser.rb

Overview

Thin wrapper around the backend's HTML5 fragment parser. Delegates to Dommy::Backend.fragment so backends can supply their own implementation.

Known quirks (vary by backend):

  • Nokogiri (libxml2): <table>-only fragments wrap children in an implicit <tbody>; <select> reparents non-option children.
  • Makiri (Lexbor): similar behavior, slightly different edge cases for malformed input.

owner_doc is critical: when a node parsed via a detached fragment gets add_child'd into a Document with a different owner, libxml2 silently copies the node (new object_id) instead of moving it. That breaks identity-dependent caches (e.g. Document#wrap_node and any reconciler that keys off node identity). Always pass the destination document.

Class Method Summary collapse

Class Method Details

.fragment(html, owner_doc: nil) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/dommy/parser.rb', line 21

def self.fragment(html, owner_doc: nil)
  if owner_doc
    owner_doc.fragment(html.to_s)
  else
    Backend.fragment(html.to_s, owner_doc: nil)
  end
end