Skip to content

Commit 76fedf5

Browse files
committed
Refactor to improve bundle size
1 parent 40ba301 commit 76fedf5

File tree

12 files changed

+864
-957
lines changed

12 files changed

+864
-957
lines changed

lib/any.js

Lines changed: 42 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,34 @@
22

33
module.exports = match
44

5-
var zwitch = require('zwitch')
65
var html = require('property-information/html')
76
var svg = require('property-information/svg')
8-
var needsIndex = require('./pseudo').needsIndex
9-
var test = require('./test')
10-
var nest = require('./nest')
7+
var zwitch = require('zwitch')
118
var enter = require('./enter-state')
9+
var nest = require('./nest')
10+
var pseudo = require('./pseudo')
11+
var test = require('./test')
1212

13-
var type = zwitch('type')
14-
var handlers = type.handlers
15-
16-
type.unknown = unknownType
17-
type.invalid = invalidType
18-
handlers.selectors = selectors
19-
handlers.ruleSet = ruleSet
20-
handlers.rule = rule
13+
var type = zwitch('type', {
14+
unknown: unknownType,
15+
invalid: invalidType,
16+
handlers: {
17+
selectors: selectors,
18+
ruleSet: ruleSet,
19+
rule: rule
20+
}
21+
})
2122

2223
function match(query, node, state) {
2324
return query && node ? type(query, node, state) : []
2425
}
2526

2627
function selectors(query, node, state) {
2728
var collect = collector(state.one)
28-
var ruleSets = query.selectors
29-
var length = ruleSets.length
3029
var index = -1
3130

32-
while (++index < length) {
33-
collect(ruleSet(ruleSets[index], node, state))
31+
while (++index < query.selectors.length) {
32+
collect(ruleSet(query.selectors[index], node, state))
3433
}
3534

3635
return collect.result
@@ -42,26 +41,31 @@ function ruleSet(query, node, state) {
4241

4342
function rule(query, tree, state) {
4443
var collect = collector(state.one)
45-
var options = {
46-
schema: state.space === 'svg' ? svg : html,
47-
language: undefined,
48-
direction: 'ltr',
49-
editableOrEditingHost: false,
50-
scopeElements: tree.type === 'root' ? tree.children : [tree],
51-
iterator: match,
52-
one: state.one,
53-
shallow: state.shallow
54-
}
5544

5645
if (state.shallow && query.rule) {
5746
throw new Error('Expected selector without nesting')
5847
}
5948

60-
nest(query, tree, 0, null, configure(query, options))
49+
nest(
50+
query,
51+
tree,
52+
0,
53+
null,
54+
configure(query, {
55+
schema: state.space === 'svg' ? svg : html,
56+
language: null,
57+
direction: 'ltr',
58+
editableOrEditingHost: false,
59+
scopeElements: tree.type === 'root' ? tree.children : [tree],
60+
iterator: iterator,
61+
one: state.one,
62+
shallow: state.shallow
63+
})
64+
)
6165

6266
return collect.result
6367

64-
function match(query, node, index, parent, state) {
68+
function iterator(query, node, index, parent, state) {
6569
var exit = enter(state, node)
6670

6771
if (test(query, node, index, parent, state)) {
@@ -77,12 +81,11 @@ function rule(query, tree, state) {
7781
}
7882

7983
function configure(query, state) {
80-
var pseudos = query.pseudos
81-
var length = pseudos && pseudos.length
84+
var pseudos = query.pseudos || []
8285
var index = -1
8386

84-
while (++index < length) {
85-
if (needsIndex.indexOf(pseudos[index].name) !== -1) {
87+
while (++index < pseudos.length) {
88+
if (pseudo.needsIndex.indexOf(pseudos[index].name) > -1) {
8689
state.index = true
8790
break
8891
}
@@ -112,34 +115,24 @@ function collector(one) {
112115

113116
// Append elements to array, filtering out duplicates.
114117
function collect(source) {
115-
if ('length' in source) {
116-
collectAll()
117-
} else {
118-
collectOne(source)
119-
}
120-
121-
function collectAll() {
122-
var length = source.length
123-
var index = -1
118+
var index = -1
124119

125-
while (++index < length) {
120+
if ('length' in source) {
121+
while (++index < source.length) {
126122
collectOne(source[index])
127123
}
124+
} else {
125+
collectOne(source)
128126
}
129127

130128
function collectOne(element) {
131129
if (one) {
132130
/* istanbul ignore if - shouldn’t happen, safeguards performance problems. */
133-
if (found) {
134-
throw new Error('Cannot collect multiple nodes')
135-
}
136-
131+
if (found) throw new Error('Cannot collect multiple nodes')
137132
found = true
138133
}
139134

140-
if (result.indexOf(element) === -1) {
141-
result.push(element)
142-
}
135+
if (result.indexOf(element) < 0) result.push(element)
143136
}
144137
}
145138
}

lib/attribute.js

Lines changed: 58 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,31 @@
22

33
module.exports = match
44

5-
var zwitch = require('zwitch')
5+
var commas = require('comma-separated-tokens')
66
var has = require('hast-util-has-property')
77
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')
1310

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+
})
2223

2324
function match(query, node, schema) {
2425
var attrs = query.attrs
25-
var length = attrs.length
2626
var index = -1
27-
var info
28-
var attr
29-
30-
while (++index < length) {
31-
attr = attrs[index]
32-
info = find(schema, attr.name)
3327

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
3730
}
3831

3932
return true
@@ -46,75 +39,56 @@ function exists(query, node, info) {
4639

4740
// `[attr=value]`
4841
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+
)
5446
}
5547

5648
// `[attr~=value]`
5749
function spaceSeparatedList(query, node, info) {
58-
var value
50+
var value = node.properties[info.property]
5951

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+
)
7963
}
8064

8165
// `[attr|=value]`
8266
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)
8868

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 ||
9372
(value.slice(0, query.value.length) === query.value &&
94-
value.charAt(query.value.length) === '-')
73+
value.charAt(query.value.length) === '-'))
9574
)
9675
}
9776

9877
// `[attr^=value]`
9978
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+
)
10986
}
11087

11188
// `[attr$=value]`
11289
function ends(query, node, info) {
113-
if (!has(node, info.property)) {
114-
return false
115-
}
116-
11790
return (
91+
has(node, info.property) &&
11892
normalizeValue(node.properties[info.property], info).slice(
11993
-query.value.length
12094
) === query.value
@@ -123,14 +97,10 @@ function ends(query, node, info) {
12397

12498
// `[attr*=value]`
12599
function contains(query, node, info) {
126-
if (!has(node, info.property)) {
127-
return false
128-
}
129-
130100
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
134104
)
135105
}
136106

@@ -142,11 +112,15 @@ function unknownOperator(query) {
142112
// Stringify a hast value back to its HTML form.
143113
function normalizeValue(value, info) {
144114
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)
150124
}
151125

152126
return value

lib/class-name.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,11 @@
33
module.exports = match
44

55
function match(query, node) {
6-
var prop = node.properties.className || []
7-
var classNames = query.classNames
8-
var length = classNames.length
6+
var value = node.properties.className || []
97
var index = -1
108

11-
while (++index < length) {
12-
if (prop.indexOf(classNames[index]) === -1) {
13-
return false
14-
}
9+
while (++index < query.classNames.length) {
10+
if (value.indexOf(query.classNames[index]) < 0) return
1511
}
1612

1713
return true

0 commit comments

Comments
 (0)