1
- 'use strict'
2
-
3
- module . exports = match
4
-
5
- var commas = require ( 'comma-separated-tokens' )
6
- var has = require ( 'hast-util-has-property' )
7
- var find = require ( 'property-information/find' )
8
- var spaces = require ( 'space-separated-tokens' )
9
- var zwitch = require ( 'zwitch' )
1
+ import { stringify as commas } from 'comma-separated-tokens'
2
+ import { hasProperty } from 'hast-util-has-property'
3
+ import { find } from 'property-information'
4
+ import { stringify as spaces } from 'space-separated-tokens'
5
+ import { zwitch } from 'zwitch'
10
6
11
7
var handle = zwitch ( 'operator' , {
12
8
unknown : unknownOperator ,
@@ -21,7 +17,7 @@ var handle = zwitch('operator', {
21
17
}
22
18
} )
23
19
24
- function match ( query , node , schema ) {
20
+ export function attribute ( query , node , schema ) {
25
21
var attrs = query . attrs
26
22
var index = - 1
27
23
@@ -34,13 +30,13 @@ function match(query, node, schema) {
34
30
35
31
// `[attr]`
36
32
function exists ( query , node , info ) {
37
- return has ( node , info . property )
33
+ return hasProperty ( node , info . property )
38
34
}
39
35
40
36
// `[attr=value]`
41
37
function exact ( query , node , info ) {
42
38
return (
43
- has ( node , info . property ) &&
39
+ hasProperty ( node , info . property ) &&
44
40
normalizeValue ( node . properties [ info . property ] , info ) === query . value
45
41
)
46
42
}
@@ -55,10 +51,11 @@ function spaceSeparatedList(query, node, info) {
55
51
( ! info . commaSeparated &&
56
52
value &&
57
53
typeof value === 'object' &&
58
- value . indexOf ( query . value ) > - 1 ) ||
54
+ value . includes ( query . value ) ) ||
59
55
// For all other values (including comma-separated lists), return whether this
60
56
// is an exact match.
61
- ( has ( node , info . property ) && normalizeValue ( value , info ) === query . value )
57
+ ( hasProperty ( node , info . property ) &&
58
+ normalizeValue ( value , info ) === query . value )
62
59
)
63
60
}
64
61
@@ -67,7 +64,7 @@ function exactOrPrefix(query, node, info) {
67
64
var value = normalizeValue ( node . properties [ info . property ] , info )
68
65
69
66
return (
70
- has ( node , info . property ) &&
67
+ hasProperty ( node , info . property ) &&
71
68
( value === query . value ||
72
69
( value . slice ( 0 , query . value . length ) === query . value &&
73
70
value . charAt ( query . value . length ) === '-' ) )
@@ -77,7 +74,7 @@ function exactOrPrefix(query, node, info) {
77
74
// `[attr^=value]`
78
75
function begins ( query , node , info ) {
79
76
return (
80
- has ( node , info . property ) &&
77
+ hasProperty ( node , info . property ) &&
81
78
normalizeValue ( node . properties [ info . property ] , info ) . slice (
82
79
0 ,
83
80
query . value . length
@@ -88,7 +85,7 @@ function begins(query, node, info) {
88
85
// `[attr$=value]`
89
86
function ends ( query , node , info ) {
90
87
return (
91
- has ( node , info . property ) &&
88
+ hasProperty ( node , info . property ) &&
92
89
normalizeValue ( node . properties [ info . property ] , info ) . slice (
93
90
- query . value . length
94
91
) === query . value
@@ -98,13 +95,13 @@ function ends(query, node, info) {
98
95
// `[attr*=value]`
99
96
function contains ( query , node , info ) {
100
97
return (
101
- has ( node , info . property ) &&
102
- normalizeValue ( node . properties [ info . property ] , info ) . indexOf ( query . value ) >
103
- - 1
98
+ hasProperty ( node , info . property ) &&
99
+ normalizeValue ( node . properties [ info . property ] , info ) . includes ( query . value )
104
100
)
105
101
}
106
102
107
- /* istanbul ignore next - Shouldn’t be invoked, Parser throws an error instead. */
103
+ // Shouldn’t be called, Parser throws an error instead.
104
+ /* c8 ignore next 3 */
108
105
function unknownOperator ( query ) {
109
106
throw new Error ( 'Unknown operator `' + query . operator + '`' )
110
107
}
@@ -120,7 +117,7 @@ function normalizeValue(value, info) {
120
117
}
121
118
122
119
if ( typeof value === 'object' && 'length' in value ) {
123
- return ( info . commaSeparated ? commas . stringify : spaces . stringify ) ( value )
120
+ return ( info . commaSeparated ? commas : spaces ) ( value )
124
121
}
125
122
126
123
return value
0 commit comments