2
2
3
3
module . exports = match
4
4
5
- var camelcase = require ( 'camelcase' )
6
5
var zwitch = require ( 'zwitch' )
7
6
var has = require ( 'hast-util-has-property' )
8
- var information = require ( 'property-information' )
9
- var spaceSeparated = require ( 'space-separated-tokens' )
10
- var commaSeparated = require ( 'comma-separated-tokens' )
7
+ var find = require ( 'property-information/find ' )
8
+ var spaceSeparated = require ( 'space-separated-tokens' ) . stringify
9
+ var commaSeparated = require ( 'comma-separated-tokens' ) . stringify
11
10
12
11
var handle = zwitch ( 'operator' )
13
12
var handlers = handle . handlers
@@ -23,7 +22,7 @@ handlers['^='] = begins
23
22
handlers [ '$=' ] = ends
24
23
handlers [ '*=' ] = contains
25
24
26
- function match ( query , node ) {
25
+ function match ( query , node , schema ) {
27
26
var attrs = query . attrs
28
27
var length = attrs . length
29
28
var index = - 1
@@ -32,8 +31,7 @@ function match(query, node) {
32
31
33
32
while ( ++ index < length ) {
34
33
attr = attrs [ index ]
35
- info = information ( attr . name ) || { }
36
- attr . propertyName = info . propertyName || camelcase ( attr . name )
34
+ info = find ( schema , attr . name )
37
35
38
36
if ( ! handle ( attr , node , info ) ) {
39
37
return false
@@ -44,30 +42,28 @@ function match(query, node) {
44
42
}
45
43
46
44
/* [attr] */
47
- function exists ( query , node ) {
48
- return has ( node , query . propertyName )
45
+ function exists ( query , node , info ) {
46
+ return has ( node , info . property )
49
47
}
50
48
51
49
/* [attr=value] */
52
50
function exact ( query , node , info ) {
53
- if ( ! has ( node , query . propertyName ) ) {
51
+ if ( ! has ( node , info . property ) ) {
54
52
return false
55
53
}
56
54
57
- return (
58
- normalizeValue ( node . properties [ query . propertyName ] , info ) === query . value
59
- )
55
+ return normalizeValue ( node . properties [ info . property ] , info ) === query . value
60
56
}
61
57
62
58
/* [attr~=value] */
63
59
function spaceSeparatedList ( query , node , info ) {
64
60
var val
65
61
66
- if ( ! has ( node , query . propertyName ) ) {
62
+ if ( ! has ( node , info . property ) ) {
67
63
return false
68
64
}
69
65
70
- val = node . properties [ query . propertyName ]
66
+ val = node . properties [ info . property ]
71
67
72
68
/* If this is a comma-separated list, and the query is contained in it,
73
69
* return true. */
@@ -88,11 +84,11 @@ function spaceSeparatedList(query, node, info) {
88
84
function exactOrPrefix ( query , node , info ) {
89
85
var value
90
86
91
- if ( ! has ( node , query . propertyName ) ) {
87
+ if ( ! has ( node , info . property ) ) {
92
88
return false
93
89
}
94
90
95
- value = normalizeValue ( node . properties [ query . propertyName ] , info )
91
+ value = normalizeValue ( node . properties [ info . property ] , info )
96
92
97
93
return Boolean (
98
94
value === query . value ||
@@ -103,39 +99,38 @@ function exactOrPrefix(query, node, info) {
103
99
104
100
/* [attr^=value] */
105
101
function begins ( query , node , info ) {
106
- if ( ! has ( node , query . propertyName ) ) {
102
+ var value
103
+
104
+ if ( ! has ( node , info . property ) ) {
107
105
return false
108
106
}
109
107
110
- return (
111
- normalizeValue ( node . properties [ query . propertyName ] , info ) . slice (
112
- 0 ,
113
- query . value . length
114
- ) === query . value
115
- )
108
+ value = normalizeValue ( node . properties [ info . property ] , info )
109
+
110
+ return value . slice ( 0 , query . value . length ) === query . value
116
111
}
117
112
118
113
/* [attr$=value] */
119
114
function ends ( query , node , info ) {
120
- if ( ! has ( node , query . propertyName ) ) {
115
+ if ( ! has ( node , info . property ) ) {
121
116
return false
122
117
}
123
118
124
119
return (
125
- normalizeValue ( node . properties [ query . propertyName ] , info ) . slice (
120
+ normalizeValue ( node . properties [ info . property ] , info ) . slice (
126
121
- query . value . length
127
122
) === query . value
128
123
)
129
124
}
130
125
131
126
/* [attr*=value] */
132
127
function contains ( query , node , info ) {
133
- if ( ! has ( node , query . propertyName ) ) {
128
+ if ( ! has ( node , info . property ) ) {
134
129
return false
135
130
}
136
131
137
132
return (
138
- normalizeValue ( node . properties [ query . propertyName ] , info ) . indexOf (
133
+ normalizeValue ( node . properties [ info . property ] , info ) . indexOf (
139
134
query . value
140
135
) !== - 1
141
136
)
@@ -148,19 +143,12 @@ function unknownOperator(query) {
148
143
149
144
/* Stringify a HAST value back to its HTML form. */
150
145
function normalizeValue ( value , info ) {
151
- if ( info . commaSeparated ) {
152
- value = commaSeparated . stringify ( value )
153
- } else if ( info . spaceSeparated ) {
154
- value = spaceSeparated . stringify ( value )
155
- } else if ( info . boolean ) {
156
- /* `false` is ignored by `has()`. */
157
- value = info . name
158
- } else if ( info . overloadedBoolean ) {
159
- if ( value === true ) {
160
- value = info . name
161
- }
162
- } else if ( info . positiveNumeric || info . numeric ) {
146
+ if ( typeof value === 'number' ) {
163
147
value = String ( value )
148
+ } else if ( typeof value === 'boolean' ) {
149
+ value = info . attribute
150
+ } else if ( typeof value === 'object' && 'length' in value ) {
151
+ value = ( info . commaSeparated ? commaSeparated : spaceSeparated ) ( value )
164
152
}
165
153
166
154
return value
0 commit comments