Module: Dommy::Parser

Defined in:
lib/dommy/parser.rb

Overview

Thin wrapper around Nokogiri’s HTML5 fragment parser. Pinned to ‘max_errors: 0` for silent recovery on malformed HTML (matching browser behavior).

Known quirks: ‘<table>`-only fragments wrap children in an implicit `<tbody>`; `<select>` reparents non-option children outside itself.

‘owner_doc` is critical: when a node parsed via a detached fragment gets `add_child`’d into a Document with a different Nokogiri 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
    Nokogiri::HTML5.fragment(html.to_s, max_errors: 0)
  end
end