當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。