How to use the parse5.treeAdapters 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 / tools / src / document-processor.ts View on Github external
}
      claimedDomModules.add(domModule);
      const template = dom5.query(domModule, (e) => e.tagName === 'template');
      if (template === null) {
        continue;
      }

      // It's ok to tag templates with the expression `Polymer.html` without
      // adding an import because `Polymer.html` is re-exported by both
      // polymer.html and polymer-element.html and, crucially, template
      // inlining happens before rewriting references.
      const templateLiteral = jsc.taggedTemplateExpression(
          jsc.memberExpression(
              jsc.identifier('Polymer'), jsc.identifier('html')),
          serializeNodeToTemplateLiteral(
              parse5.treeAdapters.default.getTemplateContent(template)));
      const nodePath = getNodePathInProgram(program, element.astNode);

      if (nodePath === undefined) {
        console.warn(
            new Warning({
              code: 'not-found',
              message: `Can't find recast node for element ${element.tagName}`,
              parsedDocument: this.document.parsedDocument,
              severity: Severity.WARNING,
              sourceRange: element.sourceRange!
            }).toString());
        continue;
      }

      const node = nodePath.node;
      if (node.type === 'ClassDeclaration' || node.type === 'ClassExpression') {
github Polymer / tools / packages / editor-service / src / ast-from-source-position.ts View on Github external
if (startTagRange && isPositionInsideRange(position, startTagRange, false)) {
    if (position.line === startTagRange.start.line) {
      // If the cursor is in the "
github Polymer / tools / packages / editor-service / src / ast-from-source-position.ts View on Github external
startTagRange.start.column + (node.tagName || '').length + 1) {
        return {kind: 'tagName', element: node};
      }
    }
    // Otherwise we're in the start tag, but not in the tag name or any
    // particular attribute, but definitely in the attributes section.
    return {kind: 'attribute', attribute: null, element: node};
  }

  // The edges of a comment aren't part of the comment.
  if (parse5.treeAdapters.default.isCommentNode(node) &&
      isPositionInsideRange(position, sourceRange, false)) {
    return {kind: 'comment', commentNode: node};
  }

  if (parse5.treeAdapters.default.isTextNode(node)) {
    const parent = node.parentNode;
    if (parent && parent.tagName === 'script') {
      return {kind: 'scriptTagContents', textNode: node};
    }
    if (parent && parent.tagName === 'style') {
      return {kind: 'styleTagContents', textNode: node};
    }
    return {kind: 'text', textNode: node};
  }


  if (isPositionInsideRange(position, sourceRange, false)) {
    /**
     * This is tricky. Consider the position inside an empty element, i.e.
     * here:
     *    .
github nrwl / nx / packages / web / src / utils / third-party / cli-files / utilities / index-file / augment-index-html.ts View on Github external
continue;
      }

      switch (extension) {
        case '.js':
          scripts.add(file);
          break;
        case '.css':
          stylesheets.add(file);
          break;
      }
    }
  }

  // Find the head and body elements
  const treeAdapter = parse5.treeAdapters.default;
  const document = parse5.parse(params.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;
        } else if (htmlChild.tagName === 'body') {
          bodyElement = htmlChild;
        }
      }
    }
github pageobject-io / pageobject-generator / test / protractor / protractor-locator-generator.spec.js View on Github external
function getDocument(source) {
    return parse5.parse(source, {treeAdapter: parse5.treeAdapters.htmlparser2});
  }
});
github pageobject-io / pageobject-generator / test / protractor / protractor-es5-emitter.spec.js View on Github external
function getDocument(source) {
    return parse5.parse(source, {treeAdapter: parse5.treeAdapters.htmlparser2});
  }
github Promact / md2 / vendor / @angular / platform-server / bundles / platform-server.umd.js View on Github external
Parse5DomAdapter.makeCurrent = function () {
            treeAdapter = parse5.treeAdapters.htmlparser2;
            setRootDomAdapter(new Parse5DomAdapter());
        };
        Parse5DomAdapter.prototype.hasProperty = function (element, name) {
github Promact / md2 / libs / @angular / platform-server / src / parse5_adapter.js View on Github external
Parse5DomAdapter.makeCurrent = function () {
        treeAdapter = parse5.treeAdapters.htmlparser2;
        setRootDomAdapter(new Parse5DomAdapter());
    };
    /**
github wellcometrust / wellcomecollection.org / server / utils / body-parser.js View on Github external
function serializeNode(node) {
  const treeAdapter = parse.treeAdapters.default;
  const frag = treeAdapter.createDocumentFragment();

  treeAdapter.appendChild(frag, node);

  return parse.serialize(frag);
}
github Polymer / tools / packages / linter / src / polymer / unbalanced-delimiters.ts View on Github external
private _getWarningsForTemplate(
      parsedHtml: ParsedHtmlDocument, template: parse5.ASTNode): Warning[] {
    let warnings: Warning[] = [];
    const content = parse5.treeAdapters.default.getTemplateContent(template);

    for (const node of dom5.depthFirst(content)) {
      if (dom5.isElement(node) && node.attrs.length > 0) {
        warnings =
            warnings.concat(this._getWarningsForElementAttrs(parsedHtml, node));
      } else if (
          dom5.isTextNode(node) && typeof node.value === 'string' &&
          this._extractBadBindingExpression(node.value)) {
        warnings.push(new Warning({
          parsedDocument: parsedHtml,
          code: 'unbalanced-delimiters',
          message: this._getMessageForBadBindingExpression(node.value),
          severity: Severity.ERROR,
          sourceRange: parsedHtml.sourceRangeForNode(node)!
        }));
      }