当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript dom5.nodeWalkAll函数代码示例

本文整理汇总了TypeScript中dom5.nodeWalkAll函数的典型用法代码示例。如果您正苦于以下问题:TypeScript nodeWalkAll函数的具体用法?TypeScript nodeWalkAll怎么用?TypeScript nodeWalkAll使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了nodeWalkAll函数的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。

示例1: stripComments

export function stripComments(document: ASTNode) {
  const uniqueLicenseTexts = new Set<string>();
  const licenseComments: ASTNode[] = [];
  for (const comment of dom5.nodeWalkAll(
           document,
           dom5.isCommentNode,
           undefined,
           dom5.childNodesIncludeTemplate)) {
    if (isImportantComment(comment) || isServerSideIncludeComment(comment)) {
      continue;
    }

    // Make whitespace uniform so we can deduplicate based on actual content.
    const commentText = (comment.data || '').replace(/\s+/g, ' ').trim();

    if (isLicenseComment(comment) && !uniqueLicenseTexts.has(commentText)) {
      uniqueLicenseTexts.add(commentText);
      licenseComments.push(comment);
    }

    removeElementAndNewline(comment);
  }
  const prependTarget = dom5.query(document, matchers.head) || document;
  for (const comment of licenseComments.reverse()) {
    prepend(prependTarget, comment);
  }
}
开发者ID:MehdiRaash,项目名称:tools,代码行数:27,代码来源:parse5-utils.ts

示例2: domModuleTemplates

function domModuleTemplates(analyzer, id) {
  var domModules = getDomModules(analyzer,id);
  if (!domModules[0]) {
    return [];
  }
  var templates = [];
  var isOuterTemplate = p.AND(
      isTemplate,
      p.NOT(parentIsTemplate)
  );
  return dom5.nodeWalkAll(domModules[0], isOuterTemplate);
}
开发者ID:PolymerLabs,项目名称:polylint,代码行数:12,代码来源:linters.ts

示例3: test

 test('Excluded comments are removed', async () => {
   const options = {stripComments: true};
   const {ast: doc} = await bundle('comments.html', options);
   const comments = dom5.nodeWalkAll(
       doc, dom5.isCommentNode, undefined, dom5.childNodesIncludeTemplate);
   const commentsExpected = [
     '#important server-side include business',
     '# this could be a server-side include too',
     '@license common',
     '@license main',
     '@license import 1',
     '@license import 2'
   ];
   const commentsActual = comments.map((c) => dom5.getTextContent(c).trim());
   assert.deepEqual(commentsActual, commentsExpected);
 });
开发者ID:Polymer,项目名称:tools,代码行数:16,代码来源:bundler_test.ts

示例4: test

    test('works for comments', async () => {
      const comments = dom5.nodeWalkAll(
          document.ast, parse5.treeAdapters.default.isCommentNode);

      assert.equal(comments.length, 2);
      assert.deepEqual(
          await underliner.underline(document.sourceRangeForNode(comments![0])),
          `
    <!-- Single Line Comment -->
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~`);

      assert.deepEqual(
          await underliner.underline(document.sourceRangeForNode(comments![1])),
          `
    <!-- Multiple
    ~~~~~~~~~~~~~
         Line
~~~~~~~~~~~~~
         Comment -->
~~~~~~~~~~~~~~~~~~~~`);
    });
开发者ID:asdfg9822,项目名称:polymer-analyzer,代码行数:21,代码来源:html-document_test.ts

示例5: convertStylesToScriptsThatInsertThem

  private *
      convertStylesToScriptsThatInsertThem(htmlDocument: ParsedHtmlDocument):
          Iterable<Edit> {
    const p = dom5.predicates;
    const head = dom5.nodeWalk(htmlDocument.ast, p.hasTagName('head'));
    const body = dom5.nodeWalk(htmlDocument.ast, p.hasTagName('body'));
    if (head === null || body === null) {
      throw new Error(`HTML Parser error, got a document without a head/body?`);
    }

    const tagsToInsertImperatively = [
      ...dom5.nodeWalkAll(
          head,
          p.OR(
              p.hasTagName('custom-style'),
              p.AND(
                  p.hasTagName('style'),
                  p.NOT(p.parentMatches(p.hasTagName('custom-style')))))),
    ];

    const apology = `<!-- FIXME(polymer-modulizer):
        These imperative modules that innerHTML your HTML are
        a hacky way to be sure that any mixins in included style
        modules are ready before any elements that reference them are
        instantiated, otherwise the CSS @apply mixin polyfill won't be
        able to expand the underlying CSS custom properties.
        See: https://github.com/Polymer/polymer-modulizer/issues/154
        -->
    `.split('\n').join(EOL);
    let first = true;
    for (const tag of tagsToInsertImperatively) {
      const offsets = htmlDocument.sourceRangeToOffsets(
          htmlDocument.sourceRangeForNode(tag)!);
      const scriptTag = parse5.parseFragment(`<script type="module"></script>`)
                            .childNodes![0];
      const program = jsc.program(createDomNodeInsertStatements([tag]));
      dom5.setTextContent(
          scriptTag,
          EOL +
              recast
                  .print(
                      program, {quote: 'single', wrapColumn: 80, tabWidth: 2})
                  .code +
              EOL);
      let replacementText = serializeNode(scriptTag);
      if (first) {
        replacementText = apology + replacementText;
        first = false;
      }
      yield {offsets, replacementText};
    }

    for (const bodyNode of body.childNodes || []) {
      if (bodyNode.nodeName.startsWith('#') || bodyNode.tagName === 'script') {
        continue;
      }
      const offsets = htmlDocument.sourceRangeToOffsets(
          htmlDocument.sourceRangeForNode(bodyNode)!);
      const scriptTag = parse5.parseFragment(`<script type="module"></script>`)
                            .childNodes![0];
      const program =
          jsc.program(createDomNodeInsertStatements([bodyNode], true));
      dom5.setTextContent(
          scriptTag,
          EOL +
              recast
                  .print(
                      program, {quote: 'single', wrapColumn: 80, tabWidth: 2})
                  .code +
              EOL);
      let replacementText = serializeNode(scriptTag);
      if (first) {
        replacementText = apology + replacementText;
        first = false;
      }
      yield {offsets, replacementText};
    }
  }
开发者ID:,项目名称:,代码行数:78,代码来源:

示例6: convertTopLevelHtmlDocument

  /**
   * Convert a document to a top-level HTML document.
   */
  convertTopLevelHtmlDocument(namespacedExports: Map<string, JsExport>):
      ConversionResult {
    const htmlDocument = this.document.parsedDocument as ParsedHtmlDocument;
    const p = dom5.predicates;

    const edits: Array<Edit> = [];
    for (const script of this.document.getFeatures({kind: 'js-document'})) {
      if (!script.astNode ||
          !isLegacyJavaScriptTag(script.astNode.node as parse5.ASTNode)) {
        continue;  // ignore unknown script tags and preexisting modules
      }
      const astNode = script.astNode.node as parse5.ASTNode;
      const sourceRange =
          script.astNode ? htmlDocument.sourceRangeForNode(astNode) : undefined;
      if (!sourceRange) {
        continue;  // nothing we can do about scripts without known positions
      }
      const offsets = htmlDocument.sourceRangeToOffsets(sourceRange);

      const file = recast.parse(script.parsedDocument.contents);
      const program = this.rewriteInlineScript(file.program, namespacedExports);

      if (program === undefined) {
        continue;
      }

      const newScriptTag =
          parse5.treeAdapters.default.createElement('script', '', []);
      dom5.setAttribute(newScriptTag, 'type', 'module');
      dom5.setTextContent(
          newScriptTag,
          EOL +
              recast
                  .print(
                      program, {quote: 'single', wrapColumn: 80, tabWidth: 2})
                  .code +
              EOL);
      const replacementText = serializeNode(newScriptTag);
      edits.push({offsets, replacementText});
    }

    const demoSnippetTemplates = dom5.nodeWalkAll(
        htmlDocument.ast,
        p.AND(
            p.hasTagName('template'),
            p.parentMatches(p.hasTagName('demo-snippet'))));
    const scriptsToConvert = [];
    for (const demoSnippetTemplate of demoSnippetTemplates) {
      scriptsToConvert.push(...dom5.nodeWalkAll(
          demoSnippetTemplate,
          p.hasTagName('script'),
          [],
          dom5.childNodesIncludeTemplate));
    }

    for (const astNode of scriptsToConvert) {
      if (!isLegacyJavaScriptTag(astNode)) {
        continue;
      }
      const sourceRange =
          astNode ? htmlDocument.sourceRangeForNode(astNode) : undefined;
      if (!sourceRange) {
        continue;  // nothing we can do about scripts without known positions
      }
      const offsets = htmlDocument.sourceRangeToOffsets(sourceRange);

      const file = recast.parse(dom5.getTextContent(astNode));
      const program = this.rewriteInlineScript(file.program, namespacedExports);

      if (program === undefined) {
        continue;
      }

      const newScriptTag =
          parse5.treeAdapters.default.createElement('script', '', []);
      dom5.setAttribute(newScriptTag, 'type', 'module');
      dom5.setTextContent(
          newScriptTag,
          EOL +
              recast
                  .print(
                      program, {quote: 'single', wrapColumn: 80, tabWidth: 2})
                  .code +
              EOL);
      const replacementText = serializeNode(newScriptTag);
      edits.push({offsets, replacementText});
    }

    for (const htmlImport of this.getHtmlImports()) {
      // Only replace imports that are actually in the document.
      if (!htmlImport.sourceRange) {
        continue;
      }
      const offsets = htmlDocument.sourceRangeToOffsets(htmlImport.sourceRange);

      const htmlDocumentUrl =
          this.urlHandler.getDocumentUrl(htmlImport.document);
//.........这里部分代码省略.........
开发者ID:,项目名称:,代码行数:101,代码来源:

示例7:

 templates.forEach(function(template){
   textNodes = textNodes.concat(dom5.nodeWalkAll(template, p.AND(
     dom5.isTextNode,
     p.NOT(twoTemplateAncestors))));
 });
开发者ID:PolymerLabs,项目名称:polylint,代码行数:5,代码来源:linters.ts


注:本文中的dom5.nodeWalkAll函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。