Skip to content

Commit 0b2a1ff

Browse files
committed
Add DOMParser interface (WIP)
This implementation seems to work correctly when the content type is set to "text/html", however the XML types fall through the HTMLParser since there's no XMLSerializer interface.
1 parent e418d16 commit 0b2a1ff

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

lib/DOMParser.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
"use strict";
2+
var HTMLParser = require("./HTMLParser");
3+
4+
// https://www.w3.org/TR/DOM-Parsing/#the-domparser-interface
5+
6+
var supportedTypes = [
7+
"text/html",
8+
"text/xml",
9+
"application/xml",
10+
"application/xhtml+xml",
11+
"image/svg+xml"
12+
];
13+
14+
function DOMParserWrapper(win) {
15+
function DOMParser() {}
16+
17+
DOMParser.prototype = {
18+
parseFromString: function(str, type) {
19+
if (arguments.length < 2) {
20+
throw new TypeError(
21+
"Not enough arguments to DOMParser.parseFromString."
22+
);
23+
}
24+
25+
if (supportedTypes.indexOf(type) === -1) {
26+
throw new TypeError(
27+
"Argument 2 of DOMParser.parseFromString '" + type +
28+
"' is not a valid value for enumeration SupportedType."
29+
);
30+
}
31+
32+
// FIX ME: without the XMLSerializer interface, the document is always
33+
// parsed as HTML.
34+
var isHTML = true || /html$/.test(type);
35+
var doc = null;
36+
var address = win.document._address;
37+
var parserOptions = {scripting_enabled: false};
38+
39+
if (isHTML) {
40+
// As per spec, set the address to the active document URL and disable
41+
// the scripting flag, so "noscript" tags are parsed correctly.
42+
var parser = new HTMLParser(address, undefined, parserOptions);
43+
44+
parser.parse(str || "", true);
45+
46+
doc = parser.document();
47+
} else {
48+
// TODO: XML parsing code
49+
}
50+
51+
if (doc) {
52+
// Set the correct content type:
53+
doc._contentType = type;
54+
55+
// Set the location to null:
56+
// (currently it throws, because the location setter is not yet
57+
// implemented, however, by default it should already be null)
58+
/*doc.location = null;*/
59+
} else {
60+
throw new Error("This should never happen");
61+
}
62+
63+
return doc;
64+
}
65+
};
66+
67+
return DOMParser;
68+
}
69+
70+
module.exports = DOMParserWrapper;

lib/Window.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var EventTarget = require('./EventTarget');
44
var Location = require('./Location');
55
var sloppy = require('./sloppy');
66
var utils = require('./utils');
7+
var DOMParserWrapper = require("./DOMParser");
78

89
module.exports = Window;
910

@@ -12,6 +13,7 @@ function Window(document) {
1213
this.document._scripting_enabled = true;
1314
this.document.defaultView = this;
1415
this.location = new Location(this, this.document._address || 'about:blank');
16+
this.DOMParser = DOMParserWrapper(this);
1517
}
1618

1719
Window.prototype = Object.create(EventTarget.prototype, {

0 commit comments

Comments
 (0)