本文整理汇总了TypeScript中parse5.parse函数的典型用法代码示例。如果您正苦于以下问题:TypeScript parse函数的具体用法?TypeScript parse怎么用?TypeScript parse使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了parse函数的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: test
test('two fragments', async () => {
await setupTest({
root: defaultRoot,
entrypoint: 'entrypoint-a.html',
fragments: ['shell.html', 'entrypoint-a.html'],
});
// shell doesn't import framework
const shellDoc = parse5(getFileOrDie('shell.html'));
assert.isFalse(hasMarker(shellDoc, 'framework'));
assert.isFalse(hasImport(shellDoc, 'framework.html'));
// entrypoint doesn't import framework
const entrypointDoc = parse5(getFileOrDie('entrypoint-a.html'));
assert.isFalse(hasMarker(entrypointDoc, 'framework'));
assert.isFalse(hasImport(entrypointDoc, 'framework.html'));
// No shared-bundle bundles framework
const sharedDoc = parse5(getFileOrDie('shared_bundle_1.html'));
assert.isTrue(hasMarker(sharedDoc, 'framework'));
assert.isFalse(hasImport(sharedDoc, 'framework.html'));
// fragments import shared-bundle
assert.isTrue(hasImport(entrypointDoc, 'shared_bundle_1.html'));
assert.isTrue(hasImport(shellDoc, 'shared_bundle_1.html'));
});
示例2: async
test.skip('entrypoint and fragments', async () => {
await setupTest({
entrypoint: 'entrypoint-a.html',
fragments: [
'shell.html',
'entrypoint-b.html',
'entrypoint-c.html',
],
});
// shared bundle was emitted
const bundle = getFileOrDie('shared_bundle_1.html');
assert.ok(bundle);
const bundleDoc = parse5(bundle);
// shared-bundle bundles framework
assert.isTrue(hasMarker(bundleDoc, 'framework'));
assert.isFalse(hasImport(bundleDoc, 'framework.html'));
// shared-bundle bundles commonDep
assert.isTrue(hasMarker(bundleDoc, 'commonDep'));
assert.isFalse(hasImport(bundleDoc, 'common-dependency.html'));
// entrypoint doesn't import framework
const entrypointDoc = parse5(getFileOrDie('entrypoint-a.html'));
assert.isFalse(hasMarker(entrypointDoc, 'framework'));
assert.isFalse(hasImport(entrypointDoc, 'framework.html'));
// shell doesn't import framework
const shellDoc = parse5(getFileOrDie('entrypoint-a.html'));
assert.isFalse(hasMarker(shellDoc, 'framework'));
assert.isFalse(hasImport(shellDoc, 'framework.html'));
// entrypoint B doesn't import commonDep
const entrypointBDoc = parse5(getFileOrDie('entrypoint-b.html'));
assert.isFalse(hasMarker(entrypointBDoc, 'commonDep'));
assert.isFalse(hasImport(entrypointBDoc, 'common-dependency.html'));
// entrypoint C doesn't import commonDep
const entrypointCDoc = parse5(getFileOrDie('entrypoint-c.html'));
assert.isFalse(hasMarker(entrypointCDoc, 'commonDep'));
assert.isFalse(hasImport(entrypointCDoc, 'common-dependency.html'));
// entrypoint and fragments import shared-bundle
assert.isTrue(hasImport(entrypointDoc, 'shared_bundle_1.html'));
assert.isTrue(hasImport(entrypointBDoc, 'shared_bundle_1.html'));
assert.isTrue(hasImport(entrypointCDoc, 'shared_bundle_1.html'));
assert.isTrue(hasImport(shellDoc, 'shared_bundle_1.html'));
});
示例3: 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<any>): ParsedHtmlDocument {
const ast = parseHtml(contents, {locationInfo: true});
// There should be at most one <base> tag and it must be inside <head> tag.
const baseTag = query(
ast,
p.AND(
p.parentMatches(p.hasTagName('head')),
p.hasTagName('base'),
p.hasAttr('href')));
let baseUrl;
if (baseTag) {
const baseHref = getAttribute(baseTag, 'href')! as FileRelativeUrl;
baseUrl = urlResolver.resolve(baseHref, url, undefined);
} else {
baseUrl = url;
}
const isInline = !!inlineInfo;
inlineInfo = inlineInfo || {};
return new ParsedHtmlDocument({
url,
baseUrl,
contents,
ast,
locationOffset: inlineInfo.locationOffset,
astNode: inlineInfo.astNode,
isInline,
});
}
示例4: processFile
private async processFile(file: File): Promise<File> {
if (file.path !== this.entrypoint) {
return file;
}
const contents = await getFileContents(file);
const document = parse5.parse(contents);
const babelHelpersFragment =
parse5.parseFragment('\n\n<script></script>\n\n');
dom5.setTextContent(
babelHelpersFragment.childNodes![1]!,
await fs.readFile(
path.join(__dirname, 'babel-helpers.min.js'), 'utf-8'));
const firstScriptOrImport = dom5.nodeWalk(document, scriptOrImport);
if (firstScriptOrImport) {
dom5.insertBefore(
firstScriptOrImport.parentNode!,
firstScriptOrImport,
babelHelpersFragment);
} else {
const head =
dom5.query(document, dom5.predicates.hasTagName('head')) || document;
dom5.append(head, babelHelpersFragment);
}
const newFile = file.clone();
newFile.contents = new Buffer(parse5.serialize(document), 'utf-8');
return newFile;
}
示例5: 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);
}
示例6: createLinks
export function createLinks(
html: string,
baseUrl: string,
deps: Set<string>,
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(dep);
} else {
href = relativeUrl(absUrl(baseUrl), absUrl(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);
}
示例7: test
test('append to node with no children', () => {
const emptyBody = parse5.parse('<head></head><body></body>');
const body = emptyBody.childNodes![0].childNodes![1];
const span = dom5.constructors.element('span');
dom5.append(body, span);
assert.equal(body.childNodes!.length, 1);
});
示例8: html
export function html(text: string) {
const ast = parse5.parse(text);
const styleNodes = dom5.queryAll(
ast, isInlineStyle, dom5.childNodesIncludeTemplate);
for (const styleNode of styleNodes) {
const text = dom5.getTextContent(styleNode);
dom5.setTextContent(styleNode, css(text));
}
return parse5.serialize(ast);
}