2
2
3
3
module . exports = match
4
4
5
- var zwitch = require ( 'zwitch ' )
5
+ var commas = require ( 'comma-separated-tokens ' )
6
6
var has = require ( 'hast-util-has-property' )
7
7
var find = require ( 'property-information/find' )
8
- var spaceSeparated = require ( 'space-separated-tokens' ) . stringify
9
- var commaSeparated = require ( 'comma-separated-tokens' ) . stringify
10
-
11
- var handle = zwitch ( 'operator' )
12
- var handlers = handle . handlers
8
+ var spaces = require ( 'space-separated-tokens' )
9
+ var zwitch = require ( 'zwitch' )
13
10
14
- handle . unknown = unknownOperator
15
- handle . invalid = exists
16
- handlers [ '=' ] = exact
17
- handlers [ '~=' ] = spaceSeparatedList
18
- handlers [ '|=' ] = exactOrPrefix
19
- handlers [ '^=' ] = begins
20
- handlers [ '$=' ] = ends
21
- handlers [ '*=' ] = contains
11
+ var handle = zwitch ( 'operator' , {
12
+ unknown : unknownOperator ,
13
+ invalid : exists ,
14
+ handlers : {
15
+ '=' : exact ,
16
+ '~=' : spaceSeparatedList ,
17
+ '|=' : exactOrPrefix ,
18
+ '^=' : begins ,
19
+ '$=' : ends ,
20
+ '*=' : contains
21
+ }
22
+ } )
22
23
23
24
function match ( query , node , schema ) {
24
25
var attrs = query . attrs
25
- var length = attrs . length
26
26
var index = - 1
27
- var info
28
- var attr
29
-
30
- while ( ++ index < length ) {
31
- attr = attrs [ index ]
32
- info = find ( schema , attr . name )
33
27
34
- if ( ! handle ( attr , node , info ) ) {
35
- return false
36
- }
28
+ while ( ++ index < attrs . length ) {
29
+ if ( ! handle ( attrs [ index ] , node , find ( schema , attrs [ index ] . name ) ) ) return
37
30
}
38
31
39
32
return true
@@ -46,75 +39,56 @@ function exists(query, node, info) {
46
39
47
40
// `[attr=value]`
48
41
function exact ( query , node , info ) {
49
- if ( ! has ( node , info . property ) ) {
50
- return false
51
- }
52
-
53
- return normalizeValue ( node . properties [ info . property ] , info ) === query . value
42
+ return (
43
+ has ( node , info . property ) &&
44
+ normalizeValue ( node . properties [ info . property ] , info ) === query . value
45
+ )
54
46
}
55
47
56
48
// `[attr~=value]`
57
49
function spaceSeparatedList ( query , node , info ) {
58
- var value
50
+ var value = node . properties [ info . property ]
59
51
60
- if ( ! has ( node , info . property ) ) {
61
- return false
62
- }
63
-
64
- value = node . properties [ info . property ]
65
-
66
- // If this is a comma-separated list, and the query is contained in it, return
67
- // true.
68
- if (
69
- typeof value === 'object' &&
70
- ! info . commaSeparated &&
71
- value . indexOf ( query . value ) !== - 1
72
- ) {
73
- return true
74
- }
75
-
76
- // For all other values (including comma-separated lists), return whether this
77
- // is an exact match.
78
- return normalizeValue ( value , info ) === query . value
52
+ return (
53
+ // If this is a comma-separated list, and the query is contained in it, return
54
+ // true.
55
+ ( ! info . commaSeparated &&
56
+ value &&
57
+ typeof value === 'object' &&
58
+ value . indexOf ( query . value ) > - 1 ) ||
59
+ // For all other values (including comma-separated lists), return whether this
60
+ // is an exact match.
61
+ ( has ( node , info . property ) && normalizeValue ( value , info ) === query . value )
62
+ )
79
63
}
80
64
81
65
// `[attr|=value]`
82
66
function exactOrPrefix ( query , node , info ) {
83
- var value
84
-
85
- if ( ! has ( node , info . property ) ) {
86
- return false
87
- }
67
+ var value = normalizeValue ( node . properties [ info . property ] , info )
88
68
89
- value = normalizeValue ( node . properties [ info . property ] , info )
90
-
91
- return Boolean (
92
- value === query . value ||
69
+ return (
70
+ has ( node , info . property ) &&
71
+ ( value === query . value ||
93
72
( value . slice ( 0 , query . value . length ) === query . value &&
94
- value . charAt ( query . value . length ) === '-' )
73
+ value . charAt ( query . value . length ) === '-' ) )
95
74
)
96
75
}
97
76
98
77
// `[attr^=value]`
99
78
function begins ( query , node , info ) {
100
- var value
101
-
102
- if ( ! has ( node , info . property ) ) {
103
- return false
104
- }
105
-
106
- value = normalizeValue ( node . properties [ info . property ] , info )
107
-
108
- return value . slice ( 0 , query . value . length ) === query . value
79
+ return (
80
+ has ( node , info . property ) &&
81
+ normalizeValue ( node . properties [ info . property ] , info ) . slice (
82
+ 0 ,
83
+ query . value . length
84
+ ) === query . value
85
+ )
109
86
}
110
87
111
88
// `[attr$=value]`
112
89
function ends ( query , node , info ) {
113
- if ( ! has ( node , info . property ) ) {
114
- return false
115
- }
116
-
117
90
return (
91
+ has ( node , info . property ) &&
118
92
normalizeValue ( node . properties [ info . property ] , info ) . slice (
119
93
- query . value . length
120
94
) === query . value
@@ -123,14 +97,10 @@ function ends(query, node, info) {
123
97
124
98
// `[attr*=value]`
125
99
function contains ( query , node , info ) {
126
- if ( ! has ( node , info . property ) ) {
127
- return false
128
- }
129
-
130
100
return (
131
- normalizeValue ( node . properties [ info . property ] , info ) . indexOf (
132
- query . value
133
- ) !== - 1
101
+ has ( node , info . property ) &&
102
+ normalizeValue ( node . properties [ info . property ] , info ) . indexOf ( query . value ) >
103
+ - 1
134
104
)
135
105
}
136
106
@@ -142,11 +112,15 @@ function unknownOperator(query) {
142
112
// Stringify a hast value back to its HTML form.
143
113
function normalizeValue ( value , info ) {
144
114
if ( typeof value === 'number' ) {
145
- value = String ( value )
146
- } else if ( typeof value === 'boolean' ) {
147
- value = info . attribute
148
- } else if ( typeof value === 'object' && 'length' in value ) {
149
- value = ( info . commaSeparated ? commaSeparated : spaceSeparated ) ( value )
115
+ return String ( value )
116
+ }
117
+
118
+ if ( typeof value === 'boolean' ) {
119
+ return info . attribute
120
+ }
121
+
122
+ if ( typeof value === 'object' && 'length' in value ) {
123
+ return ( info . commaSeparated ? commas . stringify : spaces . stringify ) ( value )
150
124
}
151
125
152
126
return value
0 commit comments