Skip to content

Commit

Permalink
Refactor to improve bundle size
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Nov 2, 2020
1 parent 06b55c6 commit 213f199
Showing 1 changed file with 22 additions and 38 deletions.
60 changes: 22 additions & 38 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ var s = require('hastscript/svg')
var zwitch = require('zwitch')
var Parser = require('css-selector-parser').CssSelectorParser

var compile = zwitch('type')
var handlers = compile.handlers

handlers.selectors = selectors
handlers.ruleSet = ruleSet
handlers.rule = rule
var compile = zwitch('type', {
handlers: {
selectors: selectors,
ruleSet: ruleSet,
rule: rule
}
})

var parser = new Parser()

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

function fromSelector(selector, space) {
var options = (typeof space === 'string' ? {space: space} : space) || {}
var result = parser.parse(selector || '')
var config = {space: options.space || 'html', root: true}
var config = {space: (space && space.space) || space || 'html', root: true}

return compile(result, config) || build(config.space)()
return compile(parser.parse(selector || ''), config) || build(config.space)()
}

function selectors() {
Expand All @@ -37,20 +36,15 @@ function ruleSet(query, config) {
}

function rule(query, config) {
var subrule = query.rule
var name = query.tagName
var parentSpace = config.space
var space = parentSpace
var sibling = false
var name = query.tagName === '*' ? '' : query.tagName
var space = parentSpace === 'html' && name === 'svg' ? 'svg' : parentSpace
var sibling
var operator
var node

if (name === '*') {
name = ''
}

if (subrule) {
operator = subrule.nestingOperator
if (query.rule) {
operator = query.rule.nestingOperator
sibling = operator === '+' || operator === '~'

if (sibling && config.root) {
Expand All @@ -60,22 +54,17 @@ function rule(query, config) {
}
}

// Switch to SVG when needed.
if (space === 'html' && name === 'svg') {
space = 'svg'
}

node = build(space)(
name,
Object.assign(
{id: query.id, className: query.classNames},
pseudosToHast(query.pseudos || []),
attrsToHast(query.attrs || [])
),
!subrule || sibling ? [] : compile(subrule, {space: space})
!query.rule || sibling ? [] : compile(query.rule, {space: space})
)

return sibling ? [node, compile(subrule, {space: parentSpace})] : node
return sibling ? [node, compile(query.rule, {space: parentSpace})] : node
}

function pseudosToHast(pseudos) {
Expand All @@ -94,27 +83,22 @@ function pseudosToHast(pseudos) {

function attrsToHast(attrs) {
var props = {}
var length = attrs.length
var index = -1
var attr
var name
var operator

while (++index < length) {
while (++index < attrs.length) {
attr = attrs[index]
name = attr.name
operator = attr.operator

if (operator) {
if (operator === '=') {
props[name] = attr.value
if (attr.operator) {
if (attr.operator === '=') {
props[attr.name] = attr.value
} else {
throw new Error(
'Cannot handle attribute equality modifier `' + operator + '`'
'Cannot handle attribute equality modifier `' + attr.operator + '`'
)
}
} else {
props[name] = true
props[attr.name] = true
}
}

Expand Down

0 comments on commit 213f199

Please sign in to comment.