Module: Dommy

Defined in:
lib/dommy/window.rb,
lib/dommy.rb,
lib/dommy/css.rb,
lib/dommy/url.rb,
lib/dommy/attr.rb,
lib/dommy/blob.rb,
lib/dommy/node.rb,
lib/dommy/event.rb,
lib/dommy/fetch.rb,
lib/dommy/range.rb,
lib/dommy/bridge.rb,
lib/dommy/crypto.rb,
lib/dommy/parser.rb,
lib/dommy/worker.rb,
lib/dommy/element.rb,
lib/dommy/history.rb,
lib/dommy/promise.rb,
lib/dommy/storage.rb,
lib/dommy/streams.rb,
lib/dommy/version.rb,
lib/dommy/document.rb,
lib/dommy/location.rb,
lib/dommy/animation.rb,
lib/dommy/form_data.rb,
lib/dommy/navigator.rb,
lib/dommy/scheduler.rb,
lib/dommy/dom_parser.rb,
lib/dommy/text_codec.rb,
lib/dommy/web_socket.rb,
lib/dommy/file_reader.rb,
lib/dommy/performance.rb,
lib/dommy/shadow_root.rb,
lib/dommy/tree_walker.rb,
lib/dommy/url_pattern.rb,
lib/dommy/cookie_store.rb,
lib/dommy/event_source.rb,
lib/dommy/notification.rb,
lib/dommy/svg_elements.rb,
lib/dommy/test_helpers.rb,
lib/dommy/data_transfer.rb,
lib/dommy/dom_exception.rb,
lib/dommy/html_elements.rb,
lib/dommy/internal/idna.rb,
lib/dommy/rspec/matchers.rb,
lib/dommy/custom_elements.rb,
lib/dommy/html_collection.rb,
lib/dommy/message_channel.rb,
lib/dommy/resize_observer.rb,
lib/dommy/media_query_list.rb,
lib/dommy/xml_http_request.rb,
lib/dommy/internal/punycode.rb,
lib/dommy/mutation_observer.rb,
lib/dommy/internal/idna_data.rb,
lib/dommy/compression_streams.rb,
lib/dommy/internal/cookie_jar.rb,
lib/dommy/minitest/assertions.rb,
lib/dommy/internal/ipv4_parser.rb,
lib/dommy/performance_observer.rb,
lib/dommy/internal/dom_matching.rb,
lib/dommy/intersection_observer.rb,
lib/dommy/internal/node_traversal.rb,
lib/dommy/internal/observer_manager.rb,
lib/dommy/internal/observer_matcher.rb,
lib/dommy/internal/scope_resolution.rb,
lib/dommy/rspec/capy_style_matchers.rb,
lib/dommy/internal/node_wrapper_cache.rb,
lib/dommy/internal/observable_callback.rb,
lib/dommy/internal/mutation_coordinator.rb,
lib/dommy/internal/reflected_attributes.rb,
lib/dommy/internal/shadow_root_registry.rb,
lib/dommy/internal/range_text_serializer.rb,
lib/dommy/internal/template_content_registry.rb

Overview

Generated by script/build_idna_tables.rb from Unicode 16.0.0 source files in vendor/unicode/. Re-run after upgrading.

Tables are sorted, non-overlapping [start, end, value…] rows. ‘IDNAData.lookup(table, cp)` does a binary search and returns the matching row (or nil).

Defined Under Namespace

Modules: Bridge, EventTarget, Internal, Minitest, Node, NodeFilter, Parser, RSpec, TestHelpers, TreeTraversalCore Classes: AbortController, AbortSignal, Animation, Attr, BatteryManager, BeforeUnloadEvent, Blob, BroadcastChannel, CSSRule, CSSRuleList, CSSStyleSheet, CharacterDataNode, ClassList, Clipboard, ClipboardEvent, CloseEvent, CommentNode, CompositionEvent, CompressionStream, CookieChangeEvent, CookieStore, Crypto, CryptoKey, CustomElementRegistry, CustomEvent, DOMException, DOMParser, DOMRect, DataTransfer, DatasetMap, DecompressionStream, Document, DocumentType, DragEvent, Element, ErrorValue, Event, EventSource, FetchFn, File, FileList, FileReader, FocusEvent, FormData, Fragment, Geolocation, HTMLAnchorElement, HTMLAreaElement, HTMLAudioElement, HTMLBRElement, HTMLBaseElement, HTMLBodyElement, HTMLButtonElement, HTMLCollection, HTMLDataElement, HTMLDetailsElement, HTMLDialogElement, HTMLDivElement, HTMLElement, HTMLEmbedElement, HTMLFieldsetElement, HTMLFormElement, HTMLHRElement, HTMLHeadElement, HTMLHeadingElement, HTMLHtmlElement, HTMLIFrameElement, HTMLImageElement, HTMLInputElement, HTMLLIElement, HTMLLabelElement, HTMLLegendElement, HTMLLinkElement, HTMLMapElement, HTMLMediaElement, HTMLMetaElement, HTMLMeterElement, HTMLModElement, HTMLOListElement, HTMLObjectElement, HTMLOptGroupElement, HTMLOptionElement, HTMLOptionsCollection, HTMLOutputElement, HTMLParagraphElement, HTMLPictureElement, HTMLPreElement, HTMLProgressElement, HTMLQuoteElement, HTMLScriptElement, HTMLSelectElement, HTMLSlotElement, HTMLSourceElement, HTMLSpanElement, HTMLStyleElement, HTMLTableCaptionElement, HTMLTableCellElement, HTMLTableElement, HTMLTableRowElement, HTMLTableSectionElement, HTMLTemplateElement, HTMLTextAreaElement, HTMLTimeElement, HTMLTitleElement, HTMLTrackElement, HTMLUListElement, HTMLVideoElement, Headers, History, InputEvent, IntersectionObserver, KeyboardEvent, KeyframeEffect, LiveNodeList, Location, Lock, LockManager, MediaQueryList, MediaQueryListEvent, MessageChannel, MessageEvent, MessagePort, MouseEvent, MutationObserver, MutationRecord, NamedNodeMap, Navigator, NodeIterator, NodeList, Notification, Performance, PerformanceEntry, PerformanceObserver, PermissionStatus, Permissions, PointerEvent, ProgressEvent, PromiseValue, Range, ReadableStream, ReadableStreamDefaultController, ReadableStreamDefaultReader, Request, ResizeObserver, Response, SVGAElement, SVGAnimateElement, SVGAnimateMotionElement, SVGAnimateTransformElement, SVGAnimationElement, SVGCircleElement, SVGClipPathElement, SVGComponentTransferFunctionElement, SVGDefsElement, SVGDescElement, SVGDiscardElement, SVGElement, SVGEllipseElement, SVGFEBlendElement, SVGFEColorMatrixElement, SVGFEComponentTransferElement, SVGFECompositeElement, SVGFEConvolveMatrixElement, SVGFEDiffuseLightingElement, SVGFEDisplacementMapElement, SVGFEDistantLightElement, SVGFEDropShadowElement, SVGFEFloodElement, SVGFEFuncAElement, SVGFEFuncBElement, SVGFEFuncGElement, SVGFEFuncRElement, SVGFEGaussianBlurElement, SVGFEImageElement, SVGFEMergeElement, SVGFEMergeNodeElement, SVGFEMorphologyElement, SVGFEOffsetElement, SVGFEPointLightElement, SVGFESpecularLightingElement, SVGFESpotLightElement, SVGFETileElement, SVGFETurbulenceElement, SVGFilterElement, SVGFilterPrimitiveElement, SVGForeignObjectElement, SVGGElement, SVGImageElement, SVGLineElement, SVGLinearGradientElement, SVGMPathElement, SVGMarkerElement, SVGMaskElement, SVGMetadataElement, SVGPathElement, SVGPatternElement, SVGPolygonElement, SVGPolylineElement, SVGRadialGradientElement, SVGRectElement, SVGSVGElement, SVGSetElement, SVGStopElement, SVGSwitchElement, SVGSymbolElement, SVGTSpanElement, SVGTextElement, SVGTextPathElement, SVGTitleElement, SVGUseElement, SVGViewElement, Scheduler, Selection, ShadowRoot, StandaloneEventTarget, Storage, StorageManager, StyleDeclaration, SubtleCrypto, TextDecoder, TextDecoderStream, TextEncoder, TextEncoderStream, TextNode, Touch, TouchEvent, TouchList, TransformStream, TransformStreamDefaultController, TreeWalker, URL, URLPattern, URLSearchParams, ValidityState, ViewTransition, WakeLock, WakeLockSentinel, WebSocket, WheelEvent, Window, Worker, WritableStream, WritableStreamDefaultWriter, XMLHttpRequest, XMLHttpRequestUpload, XMLSerializer

Constant Summary collapse

VERSION =
"0.6.0"
SVG_ELEMENT_CLASSES =

Tag-name → class lookup. Keys are the lowercased form Nokogiri::HTML5 stores in the tree. Tags absent from this table fall through to ‘Dommy::SVGElement` via `element_class_for`.

{
  "svg" => SVGSVGElement,
  "g" => SVGGElement,
  "circle" => SVGCircleElement,
  "rect" => SVGRectElement,
  "ellipse" => SVGEllipseElement,
  "line" => SVGLineElement,
  "polygon" => SVGPolygonElement,
  "polyline" => SVGPolylineElement,
  "path" => SVGPathElement,
  "text" => SVGTextElement,
  "tspan" => SVGTSpanElement,
  "defs" => SVGDefsElement,
  "use" => SVGUseElement,
  "image" => SVGImageElement,
  "symbol" => SVGSymbolElement,
  "foreignobject" => SVGForeignObjectElement,
  "title" => SVGTitleElement,
  "desc" => SVGDescElement,
  "mask" => SVGMaskElement,
  "clippath" => SVGClipPathElement,
  "pattern" => SVGPatternElement,
  "lineargradient" => SVGLinearGradientElement,
  "radialgradient" => SVGRadialGradientElement,
  "stop" => SVGStopElement,
  "filter" => SVGFilterElement,
  "marker" => SVGMarkerElement,

  # Recommended additions
  "a" => SVGAElement,
  "textpath" => SVGTextPathElement,
  "view" => SVGViewElement,
  "switch" => SVGSwitchElement,
  "metadata" => SVGMetadataElement,

  # Filter primitives (common ones)
  "fegaussianblur" => SVGFEGaussianBlurElement,
  "feoffset" => SVGFEOffsetElement,
  "feblend" => SVGFEBlendElement,
  "fecolormatrix" => SVGFEColorMatrixElement,
  "feflood" => SVGFEFloodElement,
  "fecomposite" => SVGFECompositeElement,
  "femerge" => SVGFEMergeElement,
  "femergenode" => SVGFEMergeNodeElement,
  "fecomponenttransfer" => SVGFEComponentTransferElement,
  "fefuncr" => SVGFEFuncRElement,
  "fefuncg" => SVGFEFuncGElement,
  "fefuncb" => SVGFEFuncBElement,
  "fefunca" => SVGFEFuncAElement,
  "fetile" => SVGFETileElement,
  "femorphology" => SVGFEMorphologyElement,
  "feimage" => SVGFEImageElement,
  "fedropshadow" => SVGFEDropShadowElement,
  "feturbulence" => SVGFETurbulenceElement,
  "fedisplacementmap" => SVGFEDisplacementMapElement,
  "fedistantlight" => SVGFEDistantLightElement,
  "fepointlight" => SVGFEPointLightElement,
  "fespotlight" => SVGFESpotLightElement,
  "feconvolvematrix" => SVGFEConvolveMatrixElement,
  "fediffuselighting" => SVGFEDiffuseLightingElement,
  "fespecularlighting" => SVGFESpecularLightingElement,

  # SMIL animation
  "animate" => SVGAnimateElement,
  "animatetransform" => SVGAnimateTransformElement,
  "animatemotion" => SVGAnimateMotionElement,
  "set" => SVGSetElement,
  "mpath" => SVGMPathElement,
  "discard" => SVGDiscardElement
}.freeze
HTML_ELEMENT_CLASSES =

Look up the subclass for a given HTML tag. Document#wrap_node consults this map; defaults to plain Element.

{
  "a" => HTMLAnchorElement,
  "form" => HTMLFormElement,
  "input" => HTMLInputElement,
  "button" => HTMLButtonElement,
  "img" => HTMLImageElement,
  "script" => HTMLScriptElement,
  "link" => HTMLLinkElement,
  "select" => HTMLSelectElement,
  "option" => HTMLOptionElement,
  "optgroup" => HTMLOptGroupElement,
  "textarea" => HTMLTextAreaElement,
  "label" => HTMLLabelElement,
  "fieldset" => HTMLFieldsetElement,
  "output" => HTMLOutputElement,
  "legend" => HTMLLegendElement,
  "slot" => HTMLSlotElement,
  "table" => HTMLTableElement,
  "thead" => HTMLTableSectionElement,
  "tbody" => HTMLTableSectionElement,
  "tfoot" => HTMLTableSectionElement,
  "tr" => HTMLTableRowElement,
  "td" => HTMLTableCellElement,
  "th" => HTMLTableCellElement,
  "caption" => HTMLTableCaptionElement,
  "dialog" => HTMLDialogElement,
  "details" => HTMLDetailsElement,
  "meter" => HTMLMeterElement,
  "progress" => HTMLProgressElement,
  "template" => HTMLTemplateElement,
  "audio" => HTMLAudioElement,
  "video" => HTMLVideoElement,
  "source" => HTMLSourceElement,
  "track" => HTMLTrackElement,
  "iframe" => HTMLIFrameElement,
  "picture" => HTMLPictureElement,
  "ol" => HTMLOListElement,
  "ul" => HTMLUListElement,
  "li" => HTMLLIElement,
  "time" => HTMLTimeElement,
  "data" => HTMLDataElement,
  "area" => HTMLAreaElement,
  "map" => HTMLMapElement,
  "object" => HTMLObjectElement,
  "embed" => HTMLEmbedElement,
  "base" => HTMLBaseElement,
  "meta" => HTMLMetaElement,
  "style" => HTMLStyleElement,
  "title" => HTMLTitleElement,
  "q" => HTMLQuoteElement,
  "blockquote" => HTMLQuoteElement,
  "ins" => HTMLModElement,
  "del" => HTMLModElement,
  "div" => HTMLDivElement,
  "span" => HTMLSpanElement,
  "p" => HTMLParagraphElement,
  "h1" => HTMLHeadingElement,
  "h2" => HTMLHeadingElement,
  "h3" => HTMLHeadingElement,
  "h4" => HTMLHeadingElement,
  "h5" => HTMLHeadingElement,
  "h6" => HTMLHeadingElement,
  "br" => HTMLBRElement,
  "hr" => HTMLHRElement,
  "pre" => HTMLPreElement,
  "body" => HTMLBodyElement,
  "head" => HTMLHeadElement,
  "html" => HTMLHtmlElement
}.freeze
SVG_NAMESPACE_URI =
"http://www.w3.org/2000/svg"

Class Method Summary collapse

Class Method Details

.element_class_for(tag_name, namespace_uri = nil) ⇒ Object



4443
4444
4445
4446
4447
4448
4449
# File 'lib/dommy/html_elements.rb', line 4443

def self.element_class_for(tag_name, namespace_uri = nil)
  if namespace_uri == SVG_NAMESPACE_URI
    SVG_ELEMENT_CLASSES[tag_name.to_s.downcase] || SVGElement
  else
    HTML_ELEMENT_CLASSES[tag_name.to_s.downcase] || Element
  end
end

.new_windowObject

Build a fresh, empty Window (no host). Equivalent to opening a blank document.



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

def self.new_window
  Window.new
end

.parse(html) ⇒ Object

Parse an HTML string and return a fresh ‘Window`.

When the input starts with ‘<!doctype` or `<html>`, it is parsed as a full HTML document (preserving <head>, <title>, etc.). Otherwise the input is treated as a body fragment and inserted into a fresh document’s <body>.

The Window has no host (standalone CRuby usage); embedders that need bridge callbacks (e.g. a wasm host) pass a host instead.



73
74
75
76
77
78
79
80
81
82
# File 'lib/dommy.rb', line 73

def self.parse(html)
  s = html.to_s
  if s.match?(/\A\s*(<!doctype|<html\b)/i)
    Window.new(nil, nokogiri_doc: Nokogiri::HTML5(s))
  else
    window = Window.new
    window.document.body.inner_html = s
    window
  end
end

.structured_clone(value, memo = {}) ⇒ Object

‘structuredClone(value)` — deep clone for plain Ruby values and DOM nodes (via `cloneNode(true)`). Mirrors the JS structured-clone algorithm for the subset we support:

- primitives (String / Numeric / Boolean / nil)         → copied
- Array / Hash / Set                                    → deep-cloned
- DOM nodes (anything responding to `clone_node`)       → deep-cloned
- cyclic structures                                     → preserved
- Proc / Method / Class / Module / IO                   → DataCloneError

Symbols are passed through unchanged (Ruby has no ‘Symbol` clone concept; JS treats Symbols as DataCloneError, but Ruby code uses them as hash keys so this is the pragmatic choice).



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/dommy.rb', line 103

def self.structured_clone(value, memo = {})
  return memo[value.object_id] if memo.key?(value.object_id)

  case value
  when nil, true, false, Numeric, Symbol
    value
  when String
    value.dup
  when Array
    arr = []
    memo[value.object_id] = arr
    value.each { |v| arr << structured_clone(v, memo) }
    arr
  when Hash
    h = {}
    memo[value.object_id] = h
    value.each { |k, v| h[structured_clone(k, memo)] = structured_clone(v, memo) }
    h
  when Set
    s = Set.new
    memo[value.object_id] = s
    value.each { |v| s << structured_clone(v, memo) }
    s
  when Time
    value.dup
  when Regexp
    value.dup
  when Proc, Method, UnboundMethod, IO, Class, Module
    raise DOMException::DataCloneError, "#{value.class} cannot be cloned"
  else
    if value.respond_to?(:clone_node)
      value.clone_node(true)
    else
      # Fallback: rely on the object's own `dup` (which handles
      # custom classes the user might pass through).
      value.dup
    end
  end
end

.structuredClone(value) ⇒ Object

JS-style global alias for symmetry with ‘window.structuredClone(…)`.



145
146
147
# File 'lib/dommy.rb', line 145

def Dommy.structuredClone(value)
  structured_clone(value)
end