|
| 1 | +'use strict' |
| 2 | + |
| 3 | +module.exports = fromSelector |
| 4 | + |
| 5 | +var h = require('hastscript') |
| 6 | +var s = require('hastscript/svg') |
| 7 | +var zwitch = require('zwitch') |
| 8 | +var Parser = require('css-selector-parser').CssSelectorParser |
| 9 | + |
| 10 | +var compile = zwitch('type') |
| 11 | +var handlers = compile.handlers |
| 12 | + |
| 13 | +handlers.selectors = selectors |
| 14 | +handlers.ruleSet = ruleSet |
| 15 | +handlers.rule = rule |
| 16 | + |
| 17 | +var parser = new Parser() |
| 18 | + |
| 19 | +parser.registerNestingOperators('>', '+', '~') |
| 20 | +// Register these so we can throw nicer errors. |
| 21 | +parser.registerAttrEqualityMods('~', '|', '^', '$', '*') |
| 22 | + |
| 23 | +function fromSelector(selector, space) { |
| 24 | + var opts = (typeof space === 'string' ? {space: space} : space) || {} |
| 25 | + var result = parser.parse(selector || '') |
| 26 | + var config = {space: opts.space || 'html', root: true} |
| 27 | + |
| 28 | + return compile(result, config) || build(config.space)() |
| 29 | +} |
| 30 | + |
| 31 | +function selectors() { |
| 32 | + throw new Error('Cannot handle selector list') |
| 33 | +} |
| 34 | + |
| 35 | +function ruleSet(query, config) { |
| 36 | + return compile(query.rule, config) |
| 37 | +} |
| 38 | + |
| 39 | +function rule(query, config) { |
| 40 | + var subrule = query.rule |
| 41 | + var name = query.tagName |
| 42 | + var parentSpace = config.space |
| 43 | + var space = parentSpace |
| 44 | + var sibling = false |
| 45 | + var operator |
| 46 | + var node |
| 47 | + |
| 48 | + if (name === '*') { |
| 49 | + name = '' |
| 50 | + } |
| 51 | + |
| 52 | + if (subrule) { |
| 53 | + operator = subrule.nestingOperator |
| 54 | + sibling = operator === '+' || operator === '~' |
| 55 | + |
| 56 | + if (sibling && config.root) { |
| 57 | + throw new Error( |
| 58 | + 'Cannot handle sibling combinator `' + operator + '` at root' |
| 59 | + ) |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + // Switch to SVG when needed. |
| 64 | + if (space === 'html' && name === 'svg') { |
| 65 | + space = 'svg' |
| 66 | + } |
| 67 | + |
| 68 | + node = build(space)( |
| 69 | + name, |
| 70 | + Object.assign( |
| 71 | + {id: query.id, className: query.classNames}, |
| 72 | + pseudosToHast(query.pseudos || []), |
| 73 | + attrsToHast(query.attrs || []) |
| 74 | + ), |
| 75 | + !subrule || sibling ? [] : compile(subrule, {space: space}) |
| 76 | + ) |
| 77 | + |
| 78 | + return sibling ? [node, compile(subrule, {space: parentSpace})] : node |
| 79 | +} |
| 80 | + |
| 81 | +function pseudosToHast(pseudos) { |
| 82 | + var props = {} |
| 83 | + var length = pseudos.length |
| 84 | + var index = -1 |
| 85 | + var pseudo |
| 86 | + var name |
| 87 | + |
| 88 | + while (++index < length) { |
| 89 | + pseudo = pseudos[index] |
| 90 | + name = pseudo.name |
| 91 | + |
| 92 | + if (name) { |
| 93 | + throw new Error('Cannot handle pseudo-selector `' + name + '`') |
| 94 | + } else { |
| 95 | + throw new Error('Cannot handle pseudo-element or empty pseudo-class') |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return props |
| 100 | +} |
| 101 | + |
| 102 | +function attrsToHast(attrs) { |
| 103 | + var props = {} |
| 104 | + var length = attrs.length |
| 105 | + var index = -1 |
| 106 | + var attr |
| 107 | + var name |
| 108 | + var operator |
| 109 | + |
| 110 | + while (++index < length) { |
| 111 | + attr = attrs[index] |
| 112 | + name = attr.name |
| 113 | + operator = attr.operator |
| 114 | + |
| 115 | + if (operator) { |
| 116 | + if (operator === '=') { |
| 117 | + props[name] = attr.value |
| 118 | + } else { |
| 119 | + throw new Error( |
| 120 | + 'Cannot handle attribute equality modifier `' + operator + '`' |
| 121 | + ) |
| 122 | + } |
| 123 | + } else { |
| 124 | + props[name] = true |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + return props |
| 129 | +} |
| 130 | + |
| 131 | +function build(space) { |
| 132 | + return space === 'html' ? h : s |
| 133 | +} |
0 commit comments