deno.land / x / deno_temme@v1.0.0 / temme / utils.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import type { cheerio } from '../deps.ts'import type { Section, Qualifier, AttributeQualifier, Capture,} from '../parser/mod.ts'
import invariant from './invariant.ts'import { msg } from './check.ts'
/** Generator standard css selector according to temme sections. */export function makeNormalCssSelector(sections: Section[]) { const result: string[] = [] for (const section of sections) { result.push(section.combinator) result.push(section.element) for (const qualifier of section.qualifiers) { if (qualifier.type === 'id-qualifier') { result.push(`#${qualifier.id}`) } else if (qualifier.type === 'class-qualifier') { result.push(`.${qualifier.className}`) } else if (qualifier.type === 'attribute-qualifier') { const { attribute, operator, value } = qualifier if (operator == null && value == null) { // existence result.push(`[${attribute}]`) } else if (isCapture(value)) { // Here we does not handle captures, but simply check if the operator is `=` invariant(operator === '=', msg.valueCaptureWithOtherOperator()) } else { // Normal css attribute qualifier result.push(`[${attribute}${operator}"${value}"]`) } } else { // pseudo-qualifier const { name, content } = qualifier if (content) { result.push(`:${name}(${content})`) } else { result.push(`:${name}`) } } } } return result.join('').trim()}
export function isEmptyObject(x: any) { return ( x !== null && typeof x === 'object' && Object.getPrototypeOf(x) === Object.prototype && Object.keys(x).length === 0 )}
export function isCheerioStatic( arg: cheerio.CheerioAPI | cheerio.Element,): arg is cheerio.CheerioAPI { return typeof (<cheerio.CheerioAPI>arg).root === 'function'}
export function isAttributeQualifier( qualifier: Qualifier,): qualifier is AttributeQualifier { return qualifier.type === 'attribute-qualifier'}
export function isCapture(x: any): x is Capture { return ( x != null && typeof x === 'object' && typeof x.name === 'string' && Array.isArray(x.filterList) )}
export function last<T>(arr: T[]): T { return arr[arr.length - 1]}
deno_temme

Version Info

Tagged at
a year ago