You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.3 KiB
JavaScript

'use strict';
const {DOM_PARSER, GLOBALS} = require('../shared/symbols.js');
const {parseFromString} = require('../shared/parse-from-string.js');
const {HTMLDocument} = require('../html/document.js');
const {SVGDocument} = require('../svg/document.js');
const {XMLDocument} = require('../xml/document.js');
/**
* @implements globalThis.DOMParser
*/
class DOMParser {
/** @typedef {{ "text/html": HTMLDocument, "image/svg+xml": SVGDocument, "text/xml": XMLDocument }} MimeToDoc */
/**
* @template {keyof MimeToDoc} MIME
* @param {string} markupLanguage
* @param {MIME} mimeType
* @returns {MimeToDoc[MIME]}
*/
parseFromString(markupLanguage, mimeType, globals = null) {
let isHTML = false, document;
if (mimeType === 'text/html') {
isHTML = true;
document = new HTMLDocument;
}
else if (mimeType === 'image/svg+xml')
document = new SVGDocument;
else
document = new XMLDocument;
document[DOM_PARSER] = DOMParser;
if (globals)
document[GLOBALS] = globals;
if (isHTML && markupLanguage === '...')
markupLanguage = '<!doctype html><html><head></head><body></body></html>';
return markupLanguage ?
parseFromString(document, isHTML, markupLanguage) :
document;
}
}
exports.DOMParser = DOMParser