Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
parse(
contents: string, url: ResolvedUrl, urlResolver: UrlResolver,
inlineInfo?: InlineDocInfo): ParsedHtmlDocument {
const ast = parseHtml(contents, {locationInfo: true});
// There should be at most one tag and it must be inside tag.
const baseTag = dom5.query(
ast,
p.AND(
p.parentMatches(p.hasTagName('head')),
p.hasTagName('base'),
p.hasAttr('href')));
const isInline = !!inlineInfo;
inlineInfo = inlineInfo || {};
let baseUrl: ResolvedUrl =
inlineInfo.baseUrl !== undefined ? inlineInfo.baseUrl : url;
if (baseTag) {
const baseTagHref = getAttribute(baseTag, 'href')! as FileRelativeUrl;
const scripts = [];
for (const file of unfilteredSortedFiles) {
if (existingFiles.has(file)) {
continue;
}
existingFiles.add(file);
if (file.endsWith('.js')) {
scripts.push(file);
}
else if (file.endsWith('.css')) {
stylesheets.push(file);
}
}
// Find the head and body elements
const treeAdapter = parse5.treeAdapters.default;
const document = parse5.parse(inputContent, { treeAdapter, locationInfo: true });
let headElement;
let bodyElement;
for (const docChild of document.childNodes) {
if (docChild.tagName === 'html') {
for (const htmlChild of docChild.childNodes) {
if (htmlChild.tagName === 'head') {
headElement = htmlChild;
}
if (htmlChild.tagName === 'body') {
bodyElement = htmlChild;
}
}
}
}
if (!headElement || !bodyElement) {
throw new Error('Missing head and/or body elements');
export function htmlTransform(
html: string, options: HtmlTransformOptions): string {
if (options.js && options.js.moduleResolution === 'node' &&
!options.js.filePath) {
throw new Error('Cannot perform node module resolution without filePath.');
}
const document = parse5.parse(html, {
locationInfo: true, // Required for removeFakeNodes.
});
removeFakeNodes(document);
const allScripts = [...dom5.queryAll(document, isJsScript)];
const shouldTransformEsModuleToAmd = options.js &&
options.js.transformModulesToAmd &&
// Assume that if this document has a nomodule script, the author is
// already handling browsers that don't support modules, and we don't
// need to transform them (even if the configuration was set).
// TODO(aomarks) Check this for HtmlSplitter case too.
!allScripts.some((node) => dom5.hasAttribute(node, 'nomodule'));
let wctScript, firstModuleScript;
for (const script of allScripts) {
function extractResources(htmlString, options = {}) {
const { removeModules = true, removeImportMaps = true } = options;
const indexHTML = parse(htmlString);
const scripts = queryAll(indexHTML, predicates.hasTagName('script'));
const moduleScripts = scripts.filter(script => getAttribute(script, 'type') === 'module');
const importMapScripts = scripts.filter(script => getAttribute(script, 'type') === 'importmap');
const inlineModules = [];
const jsModules = [];
const inlineImportMaps = [];
const importMapPaths = [];
moduleScripts.forEach(moduleScript => {
const src = getAttribute(moduleScript, 'src');
// don't touch scripts which reference external resources
if (isUri(src)) {
return;
}
function getDependencies(filePath: string, sourceContent: string) {
const node = parse5.parse(sourceContent);
const html = node.childNodes.find((node: Node) => node.tagName === 'html');
const head = html.childNodes.find((node: Node) => node.tagName === 'head');
const links = (head ? head.childNodes : [])
.filter((node: Node) => {
return (
node.tagName === 'link' &&
node.attrs.find((attr) => {
return attr.name === 'rel' && attr.value === 'import';
})
);
})
.map((node: Node) => {
return node.attrs.find((attr) => attr.name === 'href').value;
});
const scriptNodes = findScripts(html);
const externalScripts = scriptNodes.filter((node: Node) => {
const _parseDocument = (s, window) => {
const ast = parse5.parse(s, {
sourceCodeLocationInfo: true,
});
ast.tagName = 'document';
return _parseDocumentAst(ast, window, true);
};
module.exports._parseDocument = _parseDocument;
/** @type {import('parse5').ASTNode} */
let localBaseIndex;
/** If there is a user defined template, use that as the base to inject the output into. */
if (config.template) {
if (typeof config.template !== 'function') {
throw new Error('config.template should be a function.');
}
const templateString = config.template({
assets: compilation.assets,
entries,
legacyEntries,
variation: variation && variation.toString(),
});
localBaseIndex = parse(templateString);
} else {
/**
* If there is no user defined template the entrypoint was an index.html, and we use that as the base
* for our output. We need to clone it to avoid mutating the baseIndex variable.
*/
localBaseIndex = cloneAST(baseIndex);
}
/** @type {string} */
let finalIndexHTML;
/** Inject generated output into index.html */
if (config.inject) {
const generateResult = createIndexHTML(localBaseIndex, {
...config,
entries: { type: 'script', files: entries },
export function getHtmlTagElement(htmlContent: string, tag: string): DefaultTreeElement | null {
const document = parseHtml(htmlContent, { sourceCodeLocationInfo: true }) as DefaultTreeDocument;
const nodeQueue = [...document.childNodes];
while (nodeQueue.length) {
const node = nodeQueue.shift() as DefaultTreeElement;
if (node.nodeName.toLowerCase() === tag) {
return node;
} else if (node.childNodes) {
nodeQueue.push(...node.childNodes);
}
}
return null;
}
function parseDocument(html) {
var /** @type {?} */ doc = parse5$1.parse(html, { treeAdapter: parse5$1.treeAdapters.htmlparser2 });
var /** @type {?} */ docElement = _getElement(doc, 'html');
doc['head'] = _getElement(docElement, 'head');
doc['body'] = _getElement(docElement, 'body');
doc['_window'] = {};
return doc;
}
/**
async function separateScripts() {
const content = await readFile(file, 'utf-8');
const imports = [];
const scripts = [];
const doc = parse5.parse(content);
const nodes = dom5.queryAll(doc, moduleFinder);
for (const node of nodes) {
const src = dom5.getAttribute(node, 'src');
if (src) {
const namePath = src.replace(removeNpmPath, '');
imports.push(`import "${namePath}";`);
} else {
scripts.push(dom5.getTextContent(node));
}
dom5.remove(node);
}
const newScript = dom5.constructors.element('script');
const moduleText = `${imports.join('\n')}\n${scripts.join(';\n')}`;
const newScriptContent = await build(moduleText);
dom5.setTextContent(newScript, newScriptContent);
const body = dom5.query(doc, pred.hasTagName('body'));