@@ -8,64 +8,61 @@ const search = /[#.]/g
8
8
/**
9
9
* Create a hast element from a simple CSS selector.
10
10
*
11
- * @param selector
11
+ * @template {string} Selector
12
+ * Type of selector.
13
+ * @template {string} [DefaultTagName='div']
14
+ * Type of default tag name.
15
+ * @param {Selector | null | undefined } [selector]
12
16
* A simple CSS selector.
17
+ *
13
18
* Can contain a tag-name (`foo`), classes (`.bar`), and an ID (`#baz`).
14
19
* Multiple classes are allowed.
15
20
* Uses the last ID if multiple IDs are found.
16
- * @param [defaultTagName='div']
17
- * Tag name to use if `selector` does not specify one.
21
+ * @param {DefaultTagName | null | undefined } [defaultTagName='div']
22
+ * Tag name to use if `selector` does not specify one (default: `'div'`).
23
+ * @returns {Element & {tagName: import('./extract.js').ExtractTagName<Selector, DefaultTagName>} }
24
+ * Built element.
18
25
*/
19
- export const parseSelector =
20
- /**
21
- * @type {(
22
- * <Selector extends string, DefaultTagName extends string = 'div'>(selector?: Selector|null, defaultTagName?: DefaultTagName) => Element & {tagName: import('./extract.js').ExtractTagName<Selector, DefaultTagName>}
23
- * )}
24
- */
25
- (
26
- /**
27
- * @param {string|null } [selector]
28
- * @param {string } [defaultTagName='div']
29
- * @returns {Element }
30
- */
31
- function ( selector , defaultTagName = 'div' ) {
32
- const value = selector || ''
33
- /** @type {Properties } */
34
- const props = { }
35
- let start = 0
36
- /** @type {string|undefined } */
37
- let previous
38
-
39
- while ( start < value . length ) {
40
- search . lastIndex = start
41
- const match = search . exec ( value )
42
- const subvalue = value . slice ( start , match ? match . index : value . length )
26
+ export function parseSelector ( selector , defaultTagName ) {
27
+ const value = selector || ''
28
+ /** @type {Properties } */
29
+ const props = { }
30
+ let start = 0
31
+ /** @type {string | undefined } */
32
+ let previous
33
+ /** @type {string | undefined } */
34
+ let tagName
43
35
44
- if ( subvalue ) {
45
- if ( ! previous ) {
46
- defaultTagName = subvalue
47
- } else if ( previous === '#' ) {
48
- props . id = subvalue
49
- } else if ( Array . isArray ( props . className ) ) {
50
- props . className . push ( subvalue )
51
- } else {
52
- props . className = [ subvalue ]
53
- }
36
+ while ( start < value . length ) {
37
+ search . lastIndex = start
38
+ const match = search . exec ( value )
39
+ const subvalue = value . slice ( start , match ? match . index : value . length )
54
40
55
- start += subvalue . length
56
- }
57
-
58
- if ( match ) {
59
- previous = match [ 0 ]
60
- start ++
61
- }
41
+ if ( subvalue ) {
42
+ if ( ! previous ) {
43
+ tagName = subvalue
44
+ } else if ( previous === '#' ) {
45
+ props . id = subvalue
46
+ } else if ( Array . isArray ( props . className ) ) {
47
+ props . className . push ( subvalue )
48
+ } else {
49
+ props . className = [ subvalue ]
62
50
}
63
51
64
- return {
65
- type : 'element' ,
66
- tagName : defaultTagName ,
67
- properties : props ,
68
- children : [ ]
69
- }
52
+ start += subvalue . length
70
53
}
71
- )
54
+
55
+ if ( match ) {
56
+ previous = match [ 0 ]
57
+ start ++
58
+ }
59
+ }
60
+
61
+ return {
62
+ type : 'element' ,
63
+ // @ts -expect-error: fine.
64
+ tagName : tagName || defaultTagName || 'div' ,
65
+ properties : props ,
66
+ children : [ ]
67
+ }
68
+ }
0 commit comments