本文整理匯總了TypeScript中dom5.predicates.hasAttrValue方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript predicates.hasAttrValue方法的具體用法?TypeScript predicates.hasAttrValue怎麽用?TypeScript predicates.hasAttrValue使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類dom5.predicates
的用法示例。
在下文中一共展示了predicates.hasAttrValue方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1:
const hasImport = (doc: ASTNode, url: string) => {
const link = dom5.query(
doc,
dom5.predicates.AND(
dom5.predicates.hasTagName('link'),
dom5.predicates.hasAttrValue('rel', 'import'),
dom5.predicates.hasAttrValue('href', url)));
return link != null;
};
示例2: attributeSelectorToPredicate
function attributeSelectorToPredicate(selector: cssWhat.Attribute):
dom5.Predicate {
switch (selector.action) {
case 'exists':
return dom5.predicates.hasAttr(selector.name);
case 'equals':
return dom5.predicates.hasAttrValue(selector.name, selector.value);
case 'start':
return (el) => {
const attrValue = dom5.getAttribute(el, selector.name);
return attrValue != null && attrValue.startsWith(selector.value);
};
case 'end':
return (el) => {
const attrValue = dom5.getAttribute(el, selector.name);
return attrValue != null && attrValue.endsWith(selector.value);
};
case 'element':
return dom5.predicates.hasSpaceSeparatedAttrValue(
selector.name, selector.value);
case 'any':
return (el) => {
const attrValue = dom5.getAttribute(el, selector.name);
return attrValue != null && attrValue.includes(selector.value);
};
}
const never: never = selector.action;
throw new Error(
`Unexpected type of attribute matcher from CSS parser ${never}`);
}
示例3: _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;
}
示例4: test
test('Deprecated CSS imports should inline correctly', async () => {
const {ast: doc} = await bundle('css-imports.html', {inlineCss: true});
const styleA =
fs.readFileSync('test/html/css-import/import-a.css', 'utf-8');
const styleB =
fs.readFileSync('test/html/css-import/import-b.css', 'utf-8');
const elementA =
dom5.query(doc, dom5.predicates.hasAttrValue('id', 'element-a'))!;
assert(elementA, 'element-a not found.');
assert.include(parse5.serialize(elementA), styleA);
assert.notInclude(parse5.serialize(elementA), styleB);
const elementB =
dom5.query(doc, dom5.predicates.hasAttrValue('id', 'element-b'))!;
assert(elementB, 'element-b not found.');
assert.notInclude(parse5.serialize(elementB), styleA);
assert.include(parse5.serialize(elementB), styleB);
});
示例5: test
test('with 2 entrypoints and shell, all deps in their places', async () => {
const analyzer = getAnalyzer();
const {documents} =
await bundleMultiple([shell, entrypoint1, entrypoint2], {
strategy: generateShellMergeStrategy(analyzer.resolveUrl(shell)!, 2)
});
assert.equal(documents.size, 3);
const shellDoc = documents.get(shell)!.ast as dom5.Node;
assert.isDefined(shellDoc);
const entrypoint1Doc = documents.get(entrypoint1)!.ast as dom5.Node;
assert.isDefined(entrypoint1Doc);
const entrypoint2Doc = documents.get(entrypoint2)!.ast as dom5.Node;
assert.isDefined(entrypoint2Doc);
const shellDiv = dom5.predicates.hasAttrValue('id', 'shell');
const shellImport = dom5.predicates.AND(
dom5.predicates.hasTagName('link'),
dom5.predicates.hasSpaceSeparatedAttrValue('rel', 'import'),
dom5.predicates.hasAttrValue('href', 'shell.html'));
const commonModule = domModulePredicate('common-module');
const elOne = domModulePredicate('el-one');
const elTwo = domModulePredicate('el-two');
const depOne = domModulePredicate('el-dep1');
const depTwo = domModulePredicate('el-dep2');
// Check that all the dom modules are in their expected shards
assertContainsAndExcludes(
shellDoc, [shellDiv, commonModule, depOne], [elOne, elTwo, depTwo]);
assertContainsAndExcludes(
entrypoint1Doc,
[elOne],
[commonModule, elTwo, depOne, depTwo, shellImport]);
assertContainsAndExcludes(
entrypoint2Doc,
[elTwo, depTwo],
[commonModule, elOne, depOne, shellImport]);
});
示例6: _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;
}
示例7: async
'changes the href to another bundle if strategy moved it', async () => {
const bundler = getBundler({
// This strategy moves a file to a different bundle.
strategy: (bundles: Bundle[]): Bundle[] => {
return [
new Bundle(
'html-fragment',
new Set([resolve('default.html')]),
new Set([resolve('default.html')])),
new Bundle(
'html-fragment',
new Set(), //
new Set([
resolve('imports/simple-import.html'),
resolve('imports/another-simple-import.html'),
]))
];
}
});
const manifest =
await bundler.generateManifest([resolve('default.html')]);
const {documents} = await bundler.bundle(manifest);
const document = documents.getHtmlDoc(resolve('default.html'))!;
assert(document);
// We've moved the 'imports/simple-import.html' into a shared bundle
// so a link to import it now points to the shared bundle instead.
const linkTag = dom5.query(
document.ast!,
preds.AND(
preds.hasTagName('link'),
preds.hasAttrValue('rel', 'import')))!;
assert(linkTag);
assert.equal(
dom5.getAttribute(linkTag, 'href'), 'shared_bundle_1.html');
const shared = documents.getHtmlDoc(resolve('shared_bundle_1.html'))!;
assert(shared);
assert.isOk(dom5.query(
shared.ast, dom5.predicates.hasAttrValue('id', 'my-element')));
});
示例8:
// jshint node: true
'use strict';
import constants from './constants';
import {predicates} from 'dom5';
import * as parse5 from 'parse5';
export interface Matcher { (node: parse5.ASTNode): boolean; }
// TODO(aomarks) Look at what's using this matcher. A number of code paths
// should probably not be excluding type=module scripts.
export const nonModuleScript: Matcher = predicates.AND(
predicates.hasTagName('script'),
predicates.OR(
predicates.NOT(predicates.hasAttr('type')),
predicates.hasAttrValue('type', 'text/javascript'),
predicates.hasAttrValue('type', 'application/javascript')));
export const moduleScript: Matcher = predicates.AND(
predicates.hasTagName('script'), predicates.hasAttrValue('type', 'module'));
export const externalStyle: Matcher = predicates.AND(
predicates.hasTagName('link'),
predicates.hasAttrValue('rel', 'stylesheet'));
// polymer specific external stylesheet
export const polymerExternalStyle: Matcher = predicates.AND(
predicates.hasTagName('link'),
predicates.hasAttrValue('rel', 'import'),
predicates.hasAttrValue('type', 'css'));
export const styleMatcher: Matcher = predicates.AND(