Skip to content

Commit 213f199

Browse files
committed
Refactor to improve bundle size
1 parent 06b55c6 commit 213f199

File tree

1 file changed

+22
-38
lines changed

1 file changed

+22
-38
lines changed

index.js

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ var s = require('hastscript/svg')
77
var zwitch = require('zwitch')
88
var Parser = require('css-selector-parser').CssSelectorParser
99

10-
var compile = zwitch('type')
11-
var handlers = compile.handlers
12-
13-
handlers.selectors = selectors
14-
handlers.ruleSet = ruleSet
15-
handlers.rule = rule
10+
var compile = zwitch('type', {
11+
handlers: {
12+
selectors: selectors,
13+
ruleSet: ruleSet,
14+
rule: rule
15+
}
16+
})
1617

1718
var parser = new Parser()
1819

@@ -21,11 +22,9 @@ parser.registerNestingOperators('>', '+', '~')
2122
parser.registerAttrEqualityMods('~', '|', '^', '$', '*')
2223

2324
function fromSelector(selector, space) {
24-
var options = (typeof space === 'string' ? {space: space} : space) || {}
25-
var result = parser.parse(selector || '')
26-
var config = {space: options.space || 'html', root: true}
25+
var config = {space: (space && space.space) || space || 'html', root: true}
2726

28-
return compile(result, config) || build(config.space)()
27+
return compile(parser.parse(selector || ''), config) || build(config.space)()
2928
}
3029

3130
function selectors() {
@@ -37,20 +36,15 @@ function ruleSet(query, config) {
3736
}
3837

3938
function rule(query, config) {
40-
var subrule = query.rule
41-
var name = query.tagName
4239
var parentSpace = config.space
43-
var space = parentSpace
44-
var sibling = false
40+
var name = query.tagName === '*' ? '' : query.tagName
41+
var space = parentSpace === 'html' && name === 'svg' ? 'svg' : parentSpace
42+
var sibling
4543
var operator
4644
var node
4745

48-
if (name === '*') {
49-
name = ''
50-
}
51-
52-
if (subrule) {
53-
operator = subrule.nestingOperator
46+
if (query.rule) {
47+
operator = query.rule.nestingOperator
5448
sibling = operator === '+' || operator === '~'
5549

5650
if (sibling && config.root) {
@@ -60,22 +54,17 @@ function rule(query, config) {
6054
}
6155
}
6256

63-
// Switch to SVG when needed.
64-
if (space === 'html' && name === 'svg') {
65-
space = 'svg'
66-
}
67-
6857
node = build(space)(
6958
name,
7059
Object.assign(
7160
{id: query.id, className: query.classNames},
7261
pseudosToHast(query.pseudos || []),
7362
attrsToHast(query.attrs || [])
7463
),
75-
!subrule || sibling ? [] : compile(subrule, {space: space})
64+
!query.rule || sibling ? [] : compile(query.rule, {space: space})
7665
)
7766

78-
return sibling ? [node, compile(subrule, {space: parentSpace})] : node
67+
return sibling ? [node, compile(query.rule, {space: parentSpace})] : node
7968
}
8069

8170
function pseudosToHast(pseudos) {
@@ -94,27 +83,22 @@ function pseudosToHast(pseudos) {
9483

9584
function attrsToHast(attrs) {
9685
var props = {}
97-
var length = attrs.length
9886
var index = -1
9987
var attr
100-
var name
101-
var operator
10288

103-
while (++index < length) {
89+
while (++index < attrs.length) {
10490
attr = attrs[index]
105-
name = attr.name
106-
operator = attr.operator
10791

108-
if (operator) {
109-
if (operator === '=') {
110-
props[name] = attr.value
92+
if (attr.operator) {
93+
if (attr.operator === '=') {
94+
props[attr.name] = attr.value
11195
} else {
11296
throw new Error(
113-
'Cannot handle attribute equality modifier `' + operator + '`'
97+
'Cannot handle attribute equality modifier `' + attr.operator + '`'
11498
)
11599
}
116100
} else {
117-
props[name] = true
101+
props[attr.name] = true
118102
}
119103
}
120104

0 commit comments

Comments
 (0)