Skip to content

Commit

Permalink
Update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Nov 19, 2024
1 parent 4fe79e6 commit 947c255
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 74 deletions.
119 changes: 49 additions & 70 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @import {AstAttribute, AstRule} from 'css-selector-parser'
* @import {AstRule} from 'css-selector-parser'
* @import {Element, Properties} from 'hast'
*/

Expand Down Expand Up @@ -79,38 +79,59 @@ export function fromSelector(selector, options) {
* One or more elements.
*/
function rule(query, state) {
const space =
state.space === 'html' &&
query.tag &&
query.tag.type === 'TagName' &&
query.tag.name === 'svg'
? 'svg'
: state.space

const pseudoClass = query.pseudoClasses ? query.pseudoClasses[0] : undefined

if (pseudoClass) {
if (pseudoClass.name) {
throw new Error('Cannot handle pseudo class `' + pseudoClass.name + '`')
/* c8 ignore next 4 -- types say this can occur, but I don’t understand how */
}
let space = state.space
/** @type {Properties} */
const properties = {}
/** @type {Array<string> | undefined} */
let className
/** @type {Array<string>} */
const ids = []
/** @type {Array<string>} */
const names = []

for (const item of query.items) {
if (item.type === 'Attribute') {
if ('operator' in item) {
if (item.operator === '=') {
const value = item.value

throw new Error('Cannot handle empty pseudo class')
// eslint-disable-next-line max-depth
if (value) {
assert(value.type === 'String', 'substitution are not enabled')
properties[item.name] = value.value
}
} else {
throw new Error(
'Cannot handle attribute equality modifier `' + item.operator + '`'
)
}
} else {
properties[item.name] = true
}
} else if (item.type === 'ClassName') {
if (!className) className = []
className.push(item.name)
} else if (item.type === 'Id') {
ids.push(item.name)
} else if (item.type === 'PseudoClass') {
throw new Error('Cannot handle pseudo class `' + item.name + '`')
} else if (item.type === 'PseudoElement') {
throw new Error('Cannot handle pseudo element `' + item.name + '`')
} else if (item.type === 'TagName') {
names.push(item.name)
} else {
assert(item.type === 'WildcardTag')
// Ignore.
}
}

if (query.pseudoElement) {
throw new Error(
'Cannot handle pseudo element `' + query.pseudoElement + '`'
)
const id = ids[ids.length - 1]
const name = names[names.length - 1] || ''
if (state.space === 'html' && name === 'svg') {
space = 'svg'
}

const name = query.tag && query.tag.type === 'TagName' ? query.tag.name : ''

const node = build(space)(name, {
id: query.ids ? query.ids[query.ids.length - 1] : undefined,
className: query.classNames,
...attributesToHast(query.attributes)
})
const node = build(space)(name, {id, className, ...properties}, [])
const results = [node]

if (query.nestedRule) {
Expand All @@ -130,48 +151,6 @@ function rule(query, state) {
return results
}

/**
* Turn attribute selectors into properties.
*
* @param {Array<AstAttribute> | undefined} attributes
* Attribute selectors.
* @returns {Properties}
* Properties.
*/
function attributesToHast(attributes) {
/** @type {Properties} */
const properties = {}
let index = -1

if (attributes) {
while (++index < attributes.length) {
const attribute = attributes[index]

if ('operator' in attribute) {
if (attribute.operator === '=') {
const value = attribute.value

// eslint-disable-next-line max-depth
if (value) {
assert(value.type === 'String', 'substitution are not enabled')
properties[attribute.name] = value.value
}
} else {
throw new Error(
'Cannot handle attribute equality modifier `' +
attribute.operator +
'`'
)
}
} else {
properties[attribute.name] = true
}
}
}

return properties
}

/**
* @param {Space} space
* Space.
Expand Down
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
],
"dependencies": {
"@types/hast": "^3.0.0",
"css-selector-parser": "^2.0.0",
"css-selector-parser": "^3.0.0",
"devlop": "^1.0.0",
"hastscript": "^8.0.0"
"hastscript": "^9.0.0"
},
"description": "hast utility to parse CSS selectors to hast nodes",
"devDependencies": {
Expand Down Expand Up @@ -79,7 +79,9 @@
"xo": {
"prettier": true,
"rules": {
"unicorn/prefer-at": "off"
"logical-assignment-operators": "off",
"unicorn/prefer-at": "off",
"unicorn/prefer-switch": "off"
}
}
}
2 changes: 1 addition & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ test('fromSelector', async function (t) {
await t.test('should support space (#5)', async function () {
assert.deepEqual(
fromSelector(
'p svg[viewbox="0 0 10 10"] circle[cx=10][cy=10][r=10] altGlyph'
'p svg[viewbox="0 0 10 10"] circle[cx="10"][cy="10"][r="10"] altGlyph'
),
h('p', [
s('svg', {viewBox: '0 0 10 10'}, [
Expand Down

0 comments on commit 947c255

Please sign in to comment.