How to use the parse5.treeAdapters.default 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 / packages / linter / src / polymer / elements / iron-flex-layout-classes.ts View on Github external
function searchUsedModulesAndIncludes(
    rootNode: dom5.Node,
    modules: Map = new Map(),
    includes: string[] =
        []): {modules: Map, includes: string[]} {
  for (const node of dom5.depthFirst(rootNode)) {
    if (!dom5.isElement(node)) {
      continue;
    }
    // Ensure we don't search into dom-module's templates.
    if (p.hasTagName('template')(node) &&
        !p.hasTagName('dom-module')(node.parentNode!)) {
      const templateContent = treeAdapters.default.getTemplateContent(node);
      searchUsedModulesAndIncludes(templateContent, modules, includes);
    } else if (isStyleInclude(node)) {
      dom5.getAttribute(node, 'include')!.split(' ').forEach((include) => {
        if (includes.indexOf(include) === -1) {
          includes.push(include);
        }
      });
    } else {
      styleModules.forEach((m) => {
        if (m.selector(node)) {
          if (!modules.has(m.module)) {
            modules.set(m.module, [node]);
          } else {
            modules.get(m.module)!.push(node);
          }
        }
github Polymer / tools / packages / analyzer / src / html / html-element-reference-scanner.ts View on Github external
value: attr.value,
              sourceRange: document.sourceRangeForAttribute(node, attr.name)!,
              nameSourceRange:
                  document.sourceRangeForAttributeName(node, attr.name)!,
              valueSourceRange:
                  document.sourceRangeForAttributeValue(node, attr.name)
            });
          }
        }

        elements.push(element);
      }

      // Descend into templates.
      if (node.tagName === 'template') {
        const content = treeAdapters.default.getTemplateContent(node);
        if (content) {
          for (const n of dom5.depthFirst(content)) {
            visitor(n);
          }
        }
      }
    };
github Polymer / tools / packages / bundler / src / html-bundler.ts View on Github external
template, dom5.constructors.fragment());
      prepend(domModule, template);
    }
    removeElementAndNewline(style);

    // Ignore the refStyle object if it is contained within a different
    // dom-module.
    if (refStyle &&
        !dom5.query(
            domModule, (n) => n === refStyle, dom5.childNodesIncludeTemplate)) {
      refStyle = undefined;
    }

    // keep ordering if previding with a reference style
    if (!refStyle) {
      prepend(treeAdapters.default.getTemplateContent(template), style);
    } else {
      insertAfter(refStyle, style);
    }
  }
github Polymer / tools / packages / linter / src / polymer / content-to-slot-declarations.ts View on Github external
async checkDocument(parsedDocument: ParsedHtmlDocument, document: Document) {
    const warnings: Warning[] = [];

    for (const domModule of document.getFeatures({kind: 'dom-module'})) {
      const template =
          dom5.query(domModule.astNode.node, p.hasTagName('template'));
      if (!template) {
        continue;
      }
      const contentElements = dom5.queryAll(
          treeAdapters.default.getTemplateContent(template),
          p.hasTagName('content'),
          dom5.childNodesIncludeTemplate);
      const slotNames = new Set();
      for (const contentElement of contentElements) {
        const result = getSerializedSlotElement(contentElement, slotNames);
        let fix = undefined;
        let actions: undefined|Action[] = undefined;
        if (result !== undefined) {
          const {slotElementStartTagText, isSafe} = result;
          const slotElementStartTag = slotElementStartTagText.slice(
              0, -7); /* cut  off the end */
          const edit: Edit = [
            {
              replacementText: slotElementStartTag,
              range: parsedDocument.sourceRangeForStartTag(contentElement)!
            },
github Polymer / tools / packages / analyzer / src / html / html-style-scanner.ts View on Github external
const contents = dom5.getTextContent(node);
          const locationOffset =
              getLocationOffsetOfStartOfTextContent(node, document);
          const commentText = getAttachedCommentText(node) || '';
          features.push(new ScannedInlineDocument(
              'css',
              contents,
              locationOffset,
              commentText,
              document.sourceRangeForNode(node)!,
              {language: 'html', node, containingDocument: document}));
        }
      }
      // Descend into templates.
      if (node.tagName === 'template') {
        const content = treeAdapters.default.getTemplateContent(node);
        if (content) {
          for (const n of dom5.depthFirst(content)) {
            visitor(n);
          }
        }
      }
    };
github aruntk / polymer-weight / src / parser.js View on Github external
_.each(childNodes, child => {
    switch (child.tagName) {
      case 'template': {
        const content = treeAdapters.default.getTemplateContent(child);
        getLinks(content, store, filePath);
      }
        break;
      case 'link': {
        const link = processLink(child);
        pushLink(link, store, filePath);
      }
        break;
      case 'script': {
        const link = processScript(child);
        if(link) {
          pushLink(link, store, filePath);
        }
      }
        break;
        // TODO style
github Polymer / tools / packages / bundler / src / html-bundler.ts View on Github external
private _moveDomModuleStyleIntoTemplate(style: ASTNode, refStyle?: ASTNode) {
    const domModule =
        dom5.nodeWalkAncestors(style, dom5.predicates.hasTagName('dom-module'));
    if (!domModule) {
      return;
    }
    let template = dom5.query(domModule, matchers.template);
    if (!template) {
      template = dom5.constructors.element('template')!;
      treeAdapters.default.setTemplateContent(
          template, dom5.constructors.fragment());
      prepend(domModule, template);
    }
    removeElementAndNewline(style);

    // Ignore the refStyle object if it is contained within a different
    // dom-module.
    if (refStyle &&
        !dom5.query(
            domModule, (n) => n === refStyle, dom5.childNodesIncludeTemplate)) {
      refStyle = undefined;
    }

    // keep ordering if previding with a reference style
    if (!refStyle) {
      prepend(treeAdapters.default.getTemplateContent(template), style);
github forcedotcom / lightning-language-server / packages / lwc-language-server / src / template / symbols.ts View on Github external
import { parseFragment, treeAdapters, AST } from 'parse5';
import {
    TextDocument,
    SymbolInformation,
    SymbolKind,
    Location,
    Range,
} from 'vscode-languageserver';

const treeAdapter = treeAdapters.default;

export default function findSymbols(
    document: TextDocument,
): SymbolInformation[] {
    const symbols: SymbolInformation[] = [];
    const AST = parseFragment(document.getText(), {
        locationInfo: true,
        treeAdapter,
    });

    findSymbolsRecursively(document, AST, symbols);

    return symbols;
}

function findSymbolsRecursively(
github Polymer / tools / packages / linter / src / polymer / content-to-slot-declarations.ts View on Github external
}
  const attrs = [...contentElement.attrs];
  const selectorAttr = attrs.find((a) => a.name === 'select');
  const selector = selectorAttr && selectorAttr.value;
  const isSafe = selector === undefined;
  let slotName = null;
  if (selector) {
    slotName = slotNameForSelector(selector);
    while (slotNames.has(slotName)) {
      slotName += '-unique-suffix';
    }
    slotNames.add(slotName);
    attrs.unshift({name: 'name', value: slotName});
    attrs.push({name: 'old-content-selector', value: selector});
  }
  const slotElement = treeAdapters.default.createElement('slot', '', []);
  for (const {name, value} of attrs) {
    dom5.setAttribute(slotElement, name, value);
  }
  dom5.removeAttribute(slotElement, 'select');
  const fragment = parse5.treeAdapters.default.createDocumentFragment();
  dom5.append(fragment, slotElement);
  const slotElementStartTagText = parse5.serialize(fragment);
  return {slotElementStartTagText, isSafe};
}
github Polymer / tools / packages / linter / src / polymer / content-to-slot-usages.ts View on Github external
function matchChildNodes(node: dom5.Node) {
          for (const child of node.childNodes || []) {
            if (child.tagName === 'template') {
              const content = treeAdapters.default.getTemplateContent(child);
              matchChildNodes(content);
            } else if (predicate(child)) {
              matchingLightContents.push(child);
            }
          }
        }
        matchChildNodes(reference.astNode.node);