Class: Dommy::DOMParser

Inherits:
Object
  • Object
show all
Defined in:
lib/dommy/dom_parser.rb

Overview

‘DOMParser` — public-facing parser entry point. Parses an HTML or XML string into a fresh `Dommy::Document`. Per spec, JS code often does:

const doc = new DOMParser().parseFromString(html, "text/html");

Supported mime types:

- `text/html`            (full HTML page)
- `application/xhtml+xml`/`application/xml`/`text/xml`/`image/svg+xml`
  all delegate to Nokogiri's XML parser

The returned Document has no ‘defaultView` (not attached to a Window). Useful for fragment parsing where you want a Document without spinning up a Window.

Instance Method Summary collapse

Instance Method Details

#__js_call__(method, args) ⇒ Object



37
38
39
40
41
42
# File 'lib/dommy/dom_parser.rb', line 37

def __js_call__(method, args)
  case method
  when "parseFromString"
    parse_from_string(args[0], args[1])
  end
end

#__js_get__(_key) ⇒ Object



33
34
35
# File 'lib/dommy/dom_parser.rb', line 33

def __js_get__(_key)
  nil
end

#parse_from_string(string, mime_type = "text/html") ⇒ Object Also known as: parseFromString



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/dommy/dom_parser.rb', line 19

def parse_from_string(string, mime_type = "text/html")
  str = string.to_s
  case mime_type.to_s.downcase
  when "text/html", ""
    parse_html(str)
  when "application/xhtml+xml", "application/xml", "text/xml", "image/svg+xml"
    parse_xml(str)
  else
    raise DOMException::TypeMismatchError, "Unsupported mime type: #{mime_type}"
  end
end