本文整理汇总了TypeScript中dom5/lib/index-next.predicates类的典型用法代码示例。如果您正苦于以下问题:TypeScript predicates类的具体用法?TypeScript predicates怎么用?TypeScript predicates使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了predicates类的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: simpleSelectorToPredicate
function simpleSelectorToPredicate(
selector: cssWhat.Simple, isPolymerTemplate: boolean) {
switch (selector.type) {
case 'adjacent':
case 'child':
case 'descendant':
case 'parent':
case 'sibling':
case 'pseudo':
throw new Error(`Unsupported CSS operator: ${selector.type}`);
case 'attribute':
if (isPolymerTemplate) {
return dom5.predicates.OR(
attributeSelectorToPredicate(selector),
attributeSelectorToPredicate(
{...selector, name: selector.name + '$'}));
}
return attributeSelectorToPredicate(selector);
case 'tag':
return dom5.predicates.hasTagName(selector.name);
case 'universal':
return () => true;
}
const never: never = selector;
throw new Error(`Unexpected node type from css parser: ${never}`);
}
示例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: 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);
}
示例4:
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;
};
示例5: test
test('works for elements', async () => {
const liTags =
[...dom5.queryAll(document.ast, dom5.predicates.hasTagName('li'))];
assert.equal(liTags.length, 4);
assert.deepEqual(
await underliner.underline(document.sourceRangeForNode(liTags[0]!)!),
`
<li>1
~~~~~
<li>2</li>
~~~~~~~~`);
assert.deepEqual(
await underliner.underline(document.sourceRangeForNode(liTags[1]!)!),
`
<li>2</li>
~~~~~~~~~~`);
assert.deepEqual(
await underliner.underline(document.sourceRangeForNode(liTags[2]!)), `
<li><li>
~~~~`);
assert.deepEqual(
await underliner.underline(document.sourceRangeForNode(liTags[3]!)), `
<li><li>
~~~~
</ul>
~~~~~~`);
const pTags =
[...dom5.queryAll(document.ast, dom5.predicates.hasTagName('p'))];
assert.equal(pTags.length, 2);
assert.deepEqual(
await underliner.underline(document.sourceRangeForNode(pTags[0]!)), `
<p>
~~~
This is a paragraph without a closing tag.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<p>This is a paragraph with a closing tag.</p>
~~~~`);
assert.deepEqual(
await underliner.underline(document.sourceRangeForNode(pTags[1]!)), `
<p>This is a paragraph with a closing tag.</p>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`);
});