本文整理汇总了TypeScript中dom5/lib/index-next.query函数的典型用法代码示例。如果您正苦于以下问题:TypeScript query函数的具体用法?TypeScript query怎么用?TypeScript query使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了query函数的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: createLinks
export function createLinks(
urlResolver: UrlResolver,
html: string,
baseUrl: ResolvedUrl,
deps: Set<ResolvedUrl>,
absolute: boolean = false): string {
const ast = parse5.parse(html, {locationInfo: true});
const baseTag = dom5.query(ast, dom5.predicates.hasTagName('base'));
const baseTagHref = baseTag ? dom5.getAttribute(baseTag, 'href') : '';
// parse5 always produces a <head> element.
const head = dom5.query(ast, dom5.predicates.hasTagName('head'))!;
for (const dep of deps) {
let href;
if (absolute && !baseTagHref) {
href = absUrl(urlResolver.relative(dep));
} else {
href = urlResolver.relative(baseUrl, dep);
}
const link = dom5.constructors.element('link');
dom5.setAttribute(link, 'rel', 'prefetch');
dom5.setAttribute(link, 'href', href);
dom5.append(head, link);
}
dom5.removeFakeRootElements(ast);
return parse5.serialize(ast);
}
示例2: 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);
}
示例3:
const hasMarker = (doc: ASTNode, id: string) => {
const marker = dom5.query(
doc,
dom5.predicates.AND(
dom5.predicates.hasTagName('div'),
dom5.predicates.hasAttrValue('id', id)));
return marker != null;
};
示例4:
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);
}
};
示例5: test
test(testName, async () => {
const url = analyzer.resolveUrl(`unclosed-tag-attributes.html`)!;
const document = parser.parse(
await analyzer.load(url),
url,
new PackageUrlResolver({packageDir: fixtureDir}));
const tag = dom5.query(document.ast, dom5.predicates.hasTagName('tag'))!;
assert.deepEqual(
await underliner.underline(document.sourceRangeForNode(tag)), `
<tag attr>
~~~~~~~~~~`);
});
示例6: _transformIter
protected async *
_transformIter(files: AsyncIterable<File>): AsyncIterable<File> {
for await (const file of files) {
if (file.path !== this.filePath) {
yield file;
continue;
}
const contents = await getFileContents(file);
const parsed = parse5.parse(contents, {locationInfo: true});
const base = dom5.query(parsed, baseMatcher);
if (!base || dom5.getAttribute(base, 'href') === this.newHref) {
yield file;
continue;
}
dom5.setAttribute(base, 'href', this.newHref);
dom5.removeFakeRootElements(parsed);
const updatedFile = file.clone();
updatedFile.contents = Buffer.from(parse5.serialize(parsed), 'utf-8');
yield updatedFile;
}
}
示例7: parse
/**
* Parse html into ASTs.
*
* @param {string} htmlString an HTML document.
* @param {string} href is the path of the document.
*/
parse(
contents: string, url: ResolvedUrl, urlResolver: UrlResolver,
inlineInfo?: InlineDocInfo): ParsedHtmlDocument {
const ast = parseHtml(contents, {locationInfo: true});
// There should be at most one <base> tag and it must be inside <head> tag.
const baseTag = dom5.query(
ast,
p.AND(
p.parentMatches(p.hasTagName('head')),
p.hasTagName('base'),
p.hasAttr('href')));
const isInline = !!inlineInfo;
inlineInfo = inlineInfo || {};
let baseUrl: ResolvedUrl =
inlineInfo.baseUrl !== undefined ? inlineInfo.baseUrl : url;
if (baseTag) {
const baseTagHref = getAttribute(baseTag, 'href')! as FileRelativeUrl;
const resolvedBaseTagHref =
urlResolver.resolve(url, baseTagHref, undefined);
if (resolvedBaseTagHref !== undefined) {
baseUrl = resolvedBaseTagHref;
}
}
return new ParsedHtmlDocument({
url,
baseUrl,
contents,
ast,
locationOffset: inlineInfo.locationOffset,
astNode: inlineInfo.astNode,
isInline,
});
}
示例8: 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);
}
//.........这里部分代码省略.........