-
Notifications
You must be signed in to change notification settings - Fork 123
Implement DOMParser interface #137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| "use strict"; | ||
| var HTMLParser = require("./HTMLParser"); | ||
| var utils = require("./utils"); | ||
|
|
||
| // https://www.w3.org/TR/DOM-Parsing/#the-domparser-interface | ||
|
|
||
| var supportedTypes = [ | ||
| "text/html", | ||
| "text/xml", | ||
| "application/xml", | ||
| "application/xhtml+xml", | ||
| "image/svg+xml" | ||
| ]; | ||
|
|
||
| function DOMParserWrapper(win) { | ||
| function DOMParser() {} | ||
|
|
||
| DOMParser.prototype = { | ||
| parseFromString: function(str, type) { | ||
| if (arguments.length < 2) { | ||
| throw new TypeError( | ||
| "Not enough arguments to DOMParser.parseFromString." | ||
| ); | ||
| } | ||
|
|
||
| if (supportedTypes.indexOf(type) === -1) { | ||
| throw new TypeError( | ||
| "Argument 2 of DOMParser.parseFromString '" + type + | ||
| "' is not a valid value for enumeration SupportedType." | ||
| ); | ||
| } | ||
|
|
||
| var isHTML = /html$/.test(type); | ||
| var doc = null; | ||
| var address = win.document._address; | ||
| var parserOptions = {scripting_enabled: false}; | ||
|
|
||
| if (isHTML) { | ||
| // As per spec, set the address to the active document URL and disable | ||
| // the scripting flag, so "noscript" tags are parsed correctly. | ||
| var parser = new HTMLParser(address, undefined, parserOptions); | ||
|
|
||
| parser.parse(str || "", true); | ||
|
|
||
| doc = parser.document(); | ||
| } else { | ||
| // TODO: XML parsing code | ||
| utils.nyi(); | ||
| } | ||
|
|
||
| if (doc) { | ||
| // Set the correct content type: | ||
| doc._contentType = type; | ||
|
|
||
| // Set the location to null: | ||
| // (currently it throws, because the location setter is not yet | ||
| // implemented, however, by default, it should already be null) | ||
| /*doc.location = null;*/ | ||
| } else { | ||
| throw new Error("This should never happen"); | ||
| } | ||
|
|
||
| return doc; | ||
| } | ||
| }; | ||
|
|
||
| return DOMParser; | ||
| } | ||
|
|
||
| module.exports = DOMParserWrapper; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ var EventTarget = require('./EventTarget'); | |
| var Location = require('./Location'); | ||
| var sloppy = require('./sloppy'); | ||
| var utils = require('./utils'); | ||
| var DOMParserWrapper = require("./DOMParser"); | ||
|
|
||
| module.exports = Window; | ||
|
|
||
|
|
@@ -12,6 +13,7 @@ function Window(document) { | |
| this.document._scripting_enabled = true; | ||
| this.document.defaultView = this; | ||
| this.location = new Location(this, this.document._address || 'about:blank'); | ||
| this.DOMParser = DOMParserWrapper(this); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, this isn't the right way to export it. You should instead add this to lib/impl.js, which then gets merged into Window in via |
||
| } | ||
|
|
||
| Window.prototype = Object.create(EventTarget.prototype, { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2774,7 +2774,6 @@ | |
| "createComment(undefined)" | ||
| ], | ||
| "dom/nodes/Document-createElement-namespace.html": [ | ||
| "Created element's namespace in created HTML document by DOMParser ('text/html')", | ||
| "Created element's namespace in created XML document by DOMParser ('text/xml')", | ||
| "Created element's namespace in created XML document by DOMParser ('application/xml')", | ||
| "Created element's namespace in created XHTML document by DOMParser ('application/xhtml+xml')", | ||
|
|
@@ -3036,6 +3035,9 @@ | |
| "dom/nodes/Element-siblingElement-null-xhtml.xhtml": [ | ||
| "Uncaught: Unexpected token <" | ||
| ], | ||
| "dom/nodes/Element-tagName.html": [ | ||
| "tagName should be updated when changing ownerDocument" | ||
| ], | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a new test failure. Why? |
||
| "dom/nodes/Element-webkitMatchesSelector.html": [ | ||
| "Selectors-API Level 2 Test Suite: HTML with Selectors Level 3" | ||
| ], | ||
|
|
@@ -4529,16 +4531,7 @@ | |
| "XHR - retrieve HTML document: document.contentType === 'application/xml'" | ||
| ], | ||
| "domparsing/DOMParser-parseFromString-html.html": [ | ||
| "Parsing of id attribute", | ||
| "contentType", | ||
| "characterSet", | ||
| "inputEncoding", | ||
| "charset", | ||
| "URL value", | ||
| "baseURI value", | ||
| "Location value", | ||
| "DOMParser parses HTML tag soup with no problems", | ||
| "DOMParser throws on an invalid enum value" | ||
| "baseURI value" | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice that we pass these tests now! |
||
| ], | ||
| "domparsing/DOMParser-parseFromString-xml-doctype.html": [ | ||
| "Doctype parsing of System Id must fail on ommitted value", | ||
|
|
@@ -4657,8 +4650,7 @@ | |
| "domparsing/style_attribute_html.html": [ | ||
| "Parsing of initial style attribute", | ||
| "Parsing of invalid style attribute", | ||
| "Parsing of style attribute", | ||
| "Update style.backgroundColor" | ||
| "Parsing of style attribute" | ||
| ], | ||
| "domparsing/xml-serialization.xhtml": [ | ||
| "Uncaught: Unexpected token <" | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this
DOMParserWrapperand not justDOMParser? None of the other classes have...Wrappersuffixes.