Class: Dommy::DOMParser
- Inherits:
-
Object
- Object
- Dommy::DOMParser
- Includes:
- Bridge::Methods
- 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
- #__js_call__(method, args) ⇒ Object
- #__js_get__(_key) ⇒ Object
- #parse_from_string(string, mime_type = "text/html") ⇒ Object (also: #parseFromString)
Methods included from Bridge::Methods
Instance Method Details
#__js_call__(method, args) ⇒ Object
41 42 43 44 45 46 |
# File 'lib/dommy/dom_parser.rb', line 41 def __js_call__(method, args) case method when "parseFromString" parse_from_string(args[0], args[1]) end end |
#__js_get__(_key) ⇒ Object
35 36 37 |
# File 'lib/dommy/dom_parser.rb', line 35 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 30 31 |
# 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, mime_type.to_s.downcase) else # `type` is a WebIDL enum (DOMParserSupportedType); an out-of-enum value # is a TypeError, not a DOMException. raise Bridge::TypeError, "The provided value '#{mime_type}' is not a valid enum value of type DOMParserSupportedType." end end |