當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript dom5.parse函數代碼示例

本文整理匯總了TypeScript中dom5.parse函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript parse函數的具體用法?TypeScript parse怎麽用?TypeScript parse使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了parse函數的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: _addSharedImportsToShell

  _addSharedImportsToShell(bundles: Map<string, string[]>): string {
    console.assert(this.shell != null);
    let shellDeps = bundles.get(this.shell)
        .map((d) => path.relative(path.dirname(this.shell), d));

    let file = this.analyzer.files.get(this.shell);
    console.assert(file != null);
    let contents = file.contents.toString();
    let doc = dom5.parse(contents);
    let imports = dom5.queryAll(doc, dom5.predicates.AND(
      dom5.predicates.hasTagName('link'),
      dom5.predicates.hasAttrValue('rel', 'import')
    ));

    // Remove all imports that are in the shared deps list so that we prefer
    // the ordering or shared deps. Any imports left should be independent of
    // ordering of shared deps.
    let shellDepsSet = new Set(shellDeps);
    for (let _import of imports) {
      if (shellDepsSet.has(dom5.getAttribute(_import, 'href'))) {
        dom5.remove(_import);
      }
    }

    // Append all shared imports to the end of <head>
    let head = dom5.query(doc, dom5.predicates.hasTagName('head'));
    for (let dep of shellDeps) {
      let newImport = dom5.constructors.element('link');
      dom5.setAttribute(newImport, 'rel', 'import');
      dom5.setAttribute(newImport, 'href', dep);
      dom5.append(head, newImport);
    }
    let newContents = dom5.serialize(doc);
    return newContents;
  }
開發者ID:LostInBrittany,項目名稱:polymer-cli,代碼行數:35,代碼來源:bundle.ts

示例2: _addSharedImportsToShell

  _addSharedImportsToShell(bundles: Map<string, string[]>): string {
    console.assert(this.shell != null);
    let shellUrl = urlFromPath(this.root, this.shell);
    let shellUrlDir = posixPath.dirname(shellUrl);
    let shellDeps = bundles.get(shellUrl)
      .map((d) => posixPath.relative(shellUrlDir, d));
    logger.debug('found shell dependencies', {
      shellUrl: shellUrl,
      shellUrlDir: shellUrlDir,
      shellDeps: shellDeps,
    });

    let file = this.analyzer.getFile(this.shell);
    console.assert(file != null);
    let contents = file.contents.toString();
    let doc = dom5.parse(contents);
    let imports = dom5.queryAll(doc, dom5.predicates.AND(
      dom5.predicates.hasTagName('link'),
      dom5.predicates.hasAttrValue('rel', 'import')
    ));
    logger.debug('found html import elements', {
      imports: imports.map((el) => dom5.getAttribute(el, 'href')),
    });

    // Remove all imports that are in the shared deps list so that we prefer
    // the ordering or shared deps. Any imports left should be independent of
    // ordering of shared deps.
    let shellDepsSet = new Set(shellDeps);
    for (let _import of imports) {
      let importHref = dom5.getAttribute(_import, 'href');
      if (shellDepsSet.has(importHref)) {
        logger.debug(`removing duplicate import element "${importHref}"...`);
        dom5.remove(_import);
      }
    }

    // Append all shared imports to the end of <head>
    let head = dom5.query(doc, dom5.predicates.hasTagName('head'));
    for (let dep of shellDeps) {
      let newImport = dom5.constructors.element('link');
      dom5.setAttribute(newImport, 'rel', 'import');
      dom5.setAttribute(newImport, 'href', dep);
      dom5.append(head, newImport);
    }
    let newContents = dom5.serialize(doc);
    return newContents;
  }
開發者ID:augint,項目名稱:polymer-cli,代碼行數:47,代碼來源:bundle.ts

示例3: pullUpDeps

 pullUpDeps(
   file: File,
   deps: string[],
   type: 'import' | 'prefetch'
 ) {
   let contents = file.contents.toString();
   let ast = dom5.parse(contents);
   let head = dom5.query(ast, dom5.predicates.hasTagName('head'));
   for (let dep of deps) {
     if (dep.startsWith(this.root)) {
       dep = path.relative(file.dirname, dep);
     }
     // prefetched deps should be absolute, as they will be in the main file
     if (type === 'prefetch') {
       dep = path.join('/', dep);
     }
     let link = dom5.constructors.element('link');
     dom5.setAttribute(link, 'rel', type);
     dom5.setAttribute(link, 'href', dep);
     dom5.append(head, link);
   }
   contents = dom5.serialize(ast);
   file.contents = new Buffer(contents);
 }
開發者ID:NorthernLogic,項目名稱:polymer-cli,代碼行數:24,代碼來源:prefetch.ts

示例4: addShadowDomTests

async function addShadowDomTests(element: ElementRepo): Promise<void> {
  const testDir = path.join(element.dir, 'test');
  if (!existsSync(testDir) || !fs.statSync(testDir).isDirectory()) {
    return;  // nothing to do
  }
  const testIndexFile = path.join(testDir, 'index.html');
  if (!existsSync(testIndexFile)) {
    return;  // nothing to do
  }

  const contents = fs.readFileSync(testIndexFile, 'utf8');
  const domTree = dom5.parse(contents);
  const scripts = dom5.queryAll(domTree, (n) => n.tagName === 'script');
  let updateNeeded = false;
  for (const script of scripts) {
    const data = script.childNodes[0];
    if (!data || data.nodeName !== '#text') {
      continue;
    }
    const program = espree.parse(data.value, {attachComment: true});
    estree_walker.walk(program, {
      enter(n) {
        if (!(n.type === 'CallExpression' &&
              n.callee.type === 'MemberExpression')) {
          return;
        }
        if (!(n.callee.object && n.callee.property)) {
          return;
        }
        if (!(n.callee.object.name === 'WCT' &&
              n.callee.property.name === 'loadSuites')) {
          return;
        }
        if (!(n.arguments && n.arguments.length === 1 &&
              n.arguments[0].type === 'ArrayExpression')) {
          return;
        }
        const testFilenameExpressions = n.arguments[0];
        const shadyFilenames = new Set<string>();
        const shadowFilenames = new Set<string>();
        for (const filenameExpression of testFilenameExpressions.elements) {
          if (/dom=shadow/.test(filenameExpression.value)) {
            shadowFilenames.add(filenameExpression.value);
          } else {
            shadyFilenames.add(filenameExpression.value);
          }
        }
        for (const shadyFilename of shadyFilenames) {
          if (!shadowFilenames.has(shadyFilename + '?dom=shadow')) {
            updateNeeded = true;
          }
        }
        testFilenameExpressions.elements = [];
        for (const shadyFilename of shadyFilenames) {
          testFilenameExpressions.elements.push(
              {type: 'Literal', value: shadyFilename});
        }
        for (const shadyFilename of shadyFilenames) {
          testFilenameExpressions.elements.push(
              {type: 'Literal', value: shadyFilename + '?dom=shadow'});
        }
      }
    });
    // Try to infer indentation
    let indentation = '  ';
    const parent = script.parentNode;
    const scriptIndex = parent.childNodes.indexOf(script);
    if (scriptIndex >= 0 &&
        parent.childNodes[scriptIndex - 1].nodeName === '#text') {
      const textJustBefore = parent.childNodes[scriptIndex - 1].value;
      indentation = textJustBefore.match(/( +)$/)[1];
    }
    data.value = '\n' + escodegen.generate(program, {
      comment: true,
      format: {
        indent: {
          style: '  ',
          base: (indentation.length / 2) + 1,
          adjustMultilineComment: true
        }
      }
    }) + '\n' + indentation;
  }

  if (updateNeeded) {
    fs.writeFileSync(testIndexFile, dom5.serialize(domTree) + '\n', 'utf8');
    element.needsReview = true;
    await makeCommit(
        element, ['test/index.html'],
        'Add shadow dom test configurations.');
  }
}
開發者ID:BruceZu,項目名稱:tedium,代碼行數:92,代碼來源:tests.ts


注:本文中的dom5.parse函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。