How to use the parse5.parse function in parse5

To help you get started, we’ve selected a few parse5 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Polymer / polymer-analyzer / src / html / html-parser.ts View on Github external
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;
github johandb / svg-drawing-tool / node_modules / @angular-devkit / build-angular / src / angular-cli-files / plugins / index-html-webpack-plugin.js View on Github external
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');
github Polymer / tools / packages / build / src / html-transform.ts View on Github external
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) {
github open-wc / open-wc / packages / building-utils / index-html / extract-resources.js View on Github external
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;
    }
github tensorflow / tensorboard / tensorboard / tools / migration / src / transfer-import.ts View on Github external
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) => {
github exokitxr / exokit / src / Document.js View on Github external
const _parseDocument = (s, window) => {
  const ast = parse5.parse(s, {
    sourceCodeLocationInfo: true,
  });
  ast.tagName = 'document';
  return _parseDocumentAst(ast, window, true);
};
module.exports._parseDocument = _parseDocument;
github open-wc / open-wc / packages / webpack-index-html-plugin / src / emit-index-html.js View on Github external
/** @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 },
github ng-matero / ng-matero / schematics / utils / html-element.ts View on Github external
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;
}
github junkerm / specmate / bundles / specmate-ui-core / webcontent / lib / js / @angular / platform-server / bundles / platform-server.umd.js View on Github external
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;
}
/**
github material-components / material-components-web-components / scripts / build / rollup-demos.js View on Github external
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'));