source: node_modules/hast-util-parse-selector/index.js@ 65b6638

main
Last change on this file since 65b6638 was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 913 bytes
RevLine 
[d24f17c]1'use strict'
2
3module.exports = parse
4
5var search = /[#.]/g
6
7// Create a hast element from a simple CSS selector.
8function parse(selector, defaultTagName) {
9 var value = selector || ''
10 var name = defaultTagName || 'div'
11 var props = {}
12 var start = 0
13 var subvalue
14 var previous
15 var match
16
17 while (start < value.length) {
18 search.lastIndex = start
19 match = search.exec(value)
20 subvalue = value.slice(start, match ? match.index : value.length)
21
22 if (subvalue) {
23 if (!previous) {
24 name = subvalue
25 } else if (previous === '#') {
26 props.id = subvalue
27 } else if (props.className) {
28 props.className.push(subvalue)
29 } else {
30 props.className = [subvalue]
31 }
32
33 start += subvalue.length
34 }
35
36 if (match) {
37 previous = match[0]
38 start++
39 }
40 }
41
42 return {type: 'element', tagName: name, properties: props, children: []}
43}
Note: See TracBrowser for help on using the repository browser.