|
| 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; |
0 commit comments