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


TypeScript index-next.insertBefore函數代碼示例

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


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

示例1: addCustomElementsEs5Adapter

export function addCustomElementsEs5Adapter(html: string): string {
  // Only modify this file if we find a web components polyfill. This is a
  // heuristic to identify the entry point HTML file. Ultimately we should
  // explicitly transform only the entry point by having the project config.
  if (!webcomponentsLoaderRegex.test(html)) {
    return html;
  }
  const parsed = parse5.parse(html, {locationInfo: true});
  const script = dom5.query(parsed, webcomponentsLoaderMatcher);
  if (!script) {
    return html;
  }

  // Collect important dom references & create fragments for injection.
  const loaderScriptUrl = dom5.getAttribute(script, 'src')!;
  const adapterScriptUrl =
      url.resolve(loaderScriptUrl, 'custom-elements-es5-adapter.js');
  const es5AdapterFragment = parse5.parseFragment(`
    <script>if (!window.customElements) { document.write('<!--'); }</script>
    <script type="text/javascript" src="${adapterScriptUrl}"></script>
    <!--! do not remove -->
`);

  dom5.insertBefore(script.parentNode!, script, es5AdapterFragment);
  return parse5.serialize(parsed);
}
開發者ID:Polymer,項目名稱:polymer-build,代碼行數:26,代碼來源:custom-elements-es5-adapter.ts

示例2:

  const injectScript = (js: string) => {
    const fragment = parse5.parseFragment('<script></script>\n');
    dom5.setTextContent(fragment.childNodes![0], js);

    const firstJsScriptOrHtmlImport =
        dom5.query(document, isJsScriptOrHtmlImport);
    if (firstJsScriptOrHtmlImport) {
      dom5.insertBefore(
          firstJsScriptOrHtmlImport.parentNode!,
          firstJsScriptOrHtmlImport,
          fragment);

    } else {
      const headOrDocument =
          dom5.query(document, dom5.predicates.hasTagName('head')) || document;
      dom5.append(headOrDocument, fragment);
    }
  };
開發者ID:MehdiRaash,項目名稱:tools,代碼行數:18,代碼來源:html-transform.ts

示例3: htmlTransform

export function htmlTransform(
    html: string, options: HtmlTransformOptions): string {
  if (options.js && options.js.moduleResolution === 'node' &&
      !options.js.filePath) {
    throw new Error('Cannot perform node module resolution without filePath.');
  }

  const document = parse5.parse(html, {
    locationInfo: true,  // Required for removeFakeNodes.
  });
  removeFakeNodes(document);
  const allScripts = [...dom5.queryAll(document, isJsScript)];

  const shouldTransformEsModuleToAmd = options.js &&
      options.js.transformModulesToAmd &&
      // Assume that if this document has a nomodule script, the author is
      // already handling browsers that don't support modules, and we don't
      // need to transform them (even if the configuration was set).
      // TODO(aomarks) Check this for HtmlSplitter case too.
      !allScripts.some((node) => dom5.hasAttribute(node, 'nomodule'));

  let wctScript, firstModuleScript, finalModuleScript;
  let moduleScriptIdx = 0;

  for (const script of allScripts) {
    const isModule = dom5.getAttribute(script, 'type') === 'module';
    if (isModule) {
      if (firstModuleScript === undefined) {
        firstModuleScript = script;
      }
      finalModuleScript = script;
      if (shouldTransformEsModuleToAmd) {
        transformEsModuleToAmd(script, moduleScriptIdx++, options.js);
        continue;  // Bypass the standard jsTransform below.
      }
    }

    const isInline = !dom5.hasAttribute(script, 'src');
    if (isInline) {
      // Note that scripts split by HtmlSplitter are always external, so we
      // don't have to check for that case here.
      const newJs = jsTransform(
          dom5.getTextContent(script),
          {...options.js, transformModulesToAmd: false});
      dom5.setTextContent(script, newJs);

    } else if (wctScript === undefined) {
      const src = dom5.getAttribute(script, 'src') || '';
      if (src.includes('web-component-tester/browser.js') ||
          src.includes('wct-browser-legacy/browser.js')) {
        wctScript = script;
      }
    }
  }

  if (shouldTransformEsModuleToAmd && finalModuleScript !== undefined) {
    // We've defined a bunch of modules and chained them together. Now we need
    // to initiate loading the chain by requiring the final one.
    const finalModuleName = generateModuleName(moduleScriptIdx - 1);
    const fragment = parse5.parseFragment(
        `<script>require(['${finalModuleName}']);</script>`);
    dom5.insertAfter(
        finalModuleScript.parentNode!, finalModuleScript, fragment);
  }

  if (options.injectAmdLoader && shouldTransformEsModuleToAmd &&
      firstModuleScript !== undefined) {
    const fragment = parse5.parseFragment('<script></script>\n');
    dom5.setTextContent(fragment.childNodes![0], getMinifiedRequireJs());
    const requireJsScript = fragment.childNodes![0];

    // Inject as late as possible (just before the first module is declared, if
    // there is one) because there may be some UMD dependencies that we want to
    // continue to load in global mode instead of AMD mode (which is detected by
    // the presence of the `require` global).
    dom5.insertBefore(
        firstModuleScript.parentNode!, firstModuleScript, fragment);

    if (wctScript !== undefined) {
      addWctTimingHack(wctScript, requireJsScript);
    }
  }

  if (options.injectBabelHelpers) {
    const fragment = parse5.parseFragment('<script></script>\n');
    dom5.setTextContent(fragment.childNodes![0], getMinifiedBabelHelpers());

    const firstJsScriptOrHtmlImport =
        dom5.query(document, isJsScriptOrHtmlImport);
    if (firstJsScriptOrHtmlImport) {
      dom5.insertBefore(
          firstJsScriptOrHtmlImport.parentNode!,
          firstJsScriptOrHtmlImport,
          fragment);

    } else {
      const headOrDocument =
          dom5.query(document, dom5.predicates.hasTagName('head')) || document;
      dom5.append(headOrDocument, fragment);
    }
//.........這裏部分代碼省略.........
開發者ID:Polymer,項目名稱:polymer-build,代碼行數:101,代碼來源:html-transform.ts

示例4: addWctTimingHack

function addWctTimingHack(wctScript: dom5.Node, requireJsScript: dom5.Node) {
  // This looks like a Web Component Tester script, and we have converted ES
  // modules to AMD. Converting a module to AMD means that `DOMContentLoaded`
  // will fire before RequireJS resolves and executes the modules. Since WCT
  // listens for `DOMContentLoaded`, this means test suites in modules will
  // not have been registered by the time WCT starts running tests.
  //
  // To address this, we inject a block of JS that uses WCT's `waitFor` hook
  // to defer running tests until our AMD modules have loaded. If WCT finds a
  // `waitFor`, it passes it a callback that will run the tests, instead of
  // running tests immediately.
  //
  // Note we must do this as late as possible, before the WCT script, because
  // users may be setting their own `waitFor` that musn't clobber ours.
  // Likewise we must call theirs if we find it.
  dom5.insertBefore(wctScript.parentNode!, wctScript, parse5.parseFragment(`
<script>
  // Injected by polymer-build to defer WCT until all AMD modules are loaded.
  (function() {
    window.WCT = window.WCT || {};
    var originalWaitFor = window.WCT.waitFor;
    window.WCT.waitFor = function(cb) {
      window._wctCallback = function() {
        if (originalWaitFor) {
          originalWaitFor(cb);
        } else {
          cb();
        }
      };
    };
  }());
</script>
`));

  // Monkey patch `require` to keep track of loaded AMD modules. Note this
  // assumes that all modules are registered before `DOMContentLoaded`, but
  // that's an assumption WCT normally makes anyway. Do this right after
  // RequireJS is loaded, and hence before the first module is registered.
  dom5.insertAfter(
      requireJsScript.parentNode!, requireJsScript, parse5.parseFragment(`
<script>
  // Injected by polymer-build to defer WCT until all AMD modules are loaded.
  (function() {
    var originalRequire = window.require;
    var moduleCount = 0;
    window.require = function(deps, factory) {
      moduleCount++;
      originalRequire(deps, function() {
        if (factory) {
          factory.apply(undefined, arguments);
        }
        moduleCount--;
        if (moduleCount === 0) {
          window._wctCallback();
        }
      });
    };
  })();
</script>
`));
}
開發者ID:Polymer,項目名稱:polymer-build,代碼行數:61,代碼來源:html-transform.ts

示例5: htmlTransform

export function htmlTransform(
    html: string, options: HtmlTransformOptions): string {
  if (options.js && options.js.moduleResolution === 'node' &&
      !options.js.filePath) {
    throw new Error('Cannot perform node module resolution without filePath.');
  }

  const document = parse5.parse(html, {
    locationInfo: true,  // Required for removeFakeNodes.
  });
  removeFakeNodes(document);
  const allScripts = [...dom5.queryAll(document, isJsScript)];

  const shouldTransformEsModuleToAmd = options.js &&
      options.js.transformModulesToAmd &&
      // Assume that if this document has a nomodule script, the author is
      // already handling browsers that don't support modules, and we don't
      // need to transform them (even if the configuration was set).
      // TODO(aomarks) Check this for HtmlSplitter case too.
      !allScripts.some((node) => dom5.hasAttribute(node, 'nomodule'));

  let wctScript, firstModuleScript;

  for (const script of allScripts) {
    const isModule = dom5.getAttribute(script, 'type') === 'module';
    if (isModule) {
      if (firstModuleScript === undefined) {
        firstModuleScript = script;
      }
      if (shouldTransformEsModuleToAmd) {
        transformEsModuleToAmd(script, options.js);
        continue;  // Bypass the standard jsTransform below.
      }
    }

    const isInline = !dom5.hasAttribute(script, 'src');
    if (isInline) {
      // Note that scripts split by HtmlSplitter are always external, so we
      // don't have to check for that case here.
      const newJs = jsTransform(
          dom5.getTextContent(script),
          {...options.js, transformModulesToAmd: false});
      dom5.setTextContent(script, newJs);

    } else if (wctScript === undefined) {
      const src = dom5.getAttribute(script, 'src') || '';
      if (src.includes('web-component-tester/browser.js') ||
          src.includes('wct-browser-legacy/browser.js') ||
          src.includes('wct-mocha/wct-mocha.js')) {
        wctScript = script;
      }
    }
  }

  if (options.injectAmdLoader && shouldTransformEsModuleToAmd &&
      firstModuleScript !== undefined) {
    const fragment = parse5.parseFragment('<script></script>\n');
    dom5.setTextContent(fragment.childNodes![0], externalJs.getAmdLoader());
    const amdLoaderScript = fragment.childNodes![0];

    // Inject as late as possible (just before the first module is declared, if
    // there is one) because there may be some UMD dependencies that we want to
    // continue to load in global mode instead of AMD mode (which is detected by
    // the presence of the `require` global).
    // TODO(aomarks) If we don't define require, we can inject earlier.
    dom5.insertBefore(
        firstModuleScript.parentNode!, firstModuleScript, fragment);

    if (wctScript !== undefined) {
      addWctTimingHack(wctScript, amdLoaderScript);
    }
  }

  const injectScript = (js: string) => {
    const fragment = parse5.parseFragment('<script></script>\n');
    dom5.setTextContent(fragment.childNodes![0], js);

    const firstJsScriptOrHtmlImport =
        dom5.query(document, isJsScriptOrHtmlImport);
    if (firstJsScriptOrHtmlImport) {
      dom5.insertBefore(
          firstJsScriptOrHtmlImport.parentNode!,
          firstJsScriptOrHtmlImport,
          fragment);

    } else {
      const headOrDocument =
          dom5.query(document, dom5.predicates.hasTagName('head')) || document;
      dom5.append(headOrDocument, fragment);
    }
  };

  let babelHelpers;
  switch (options.injectBabelHelpers) {
    case undefined:
    case 'none':
      break;
    case 'full':
      babelHelpers = externalJs.getBabelHelpersFull();
      break;
//.........這裏部分代碼省略.........
開發者ID:Polymer,項目名稱:tools,代碼行數:101,代碼來源:html-transform.ts


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