Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions lib/DOMParser.js
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;
2 changes: 2 additions & 0 deletions lib/Window.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var EventTarget = require('./EventTarget');
var Location = require('./Location');
var sloppy = require('./sloppy');
var utils = require('./utils');
var DOMParserWrapper = require("./DOMParser");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this DOMParserWrapper and not just DOMParser? None of the other classes have ...Wrapper suffixes.


module.exports = Window;

Expand All @@ -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);
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 utils.expose in the last line of this file.

}

Window.prototype = Object.create(EventTarget.prototype, {
Expand Down
18 changes: 5 additions & 13 deletions test/web-platform-blacklist.json
Original file line number Diff line number Diff line change
Expand Up @@ -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')",
Expand Down Expand Up @@ -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"
],
Copy link
Collaborator

Choose a reason for hiding this comment

The 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"
],
Expand Down Expand Up @@ -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"
Copy link
Collaborator

Choose a reason for hiding this comment

The 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",
Expand Down Expand Up @@ -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 <"
Expand Down