Skip to content

Commit fb67af9

Browse files
authored
breaking: Operator node provide .value, not .name (#139)
closes #132
1 parent 064616b commit fb67af9

File tree

2 files changed

+43
-26
lines changed

2 files changed

+43
-26
lines changed

src/css-node.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,8 @@ export class CSSNode {
234234

235235
/** Get the "content" text (at-rule name for at-rules, layer name for import layers) */
236236
get name(): string | undefined {
237-
if (this.type === DECLARATION) return
237+
let { type } = this
238+
if (type === DECLARATION || type === OPERATOR) return
238239
return this.get_content()
239240
}
240241

@@ -296,6 +297,10 @@ export class CSSNode {
296297
// For unquoted URLs, fall through to value delta logic below
297298
}
298299

300+
if (type === OPERATOR) {
301+
return this.get_content()
302+
}
303+
299304
// For other nodes, return as string
300305
let start = this.arena.get_value_start(this.index)
301306
let length = this.arena.get_value_length(this.index)

src/parse-value.test.ts

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -607,36 +607,48 @@ describe('Value Node Types', () => {
607607
})
608608

609609
describe('OPERATOR', () => {
610-
it('should parse comma operator', () => {
611-
const root = parse('body { font-family: Arial, sans-serif; }')
612-
const decl = root.first_child?.first_child?.next_sibling?.first_child
613-
614-
expect(decl?.first_child!.children[1].type).toBe(OPERATOR)
615-
expect(decl?.first_child!.children[1].text).toBe(',')
616-
})
610+
it('should parse comma operator', () => {
611+
const root = parse('body { font-family: Arial, sans-serif; }')
612+
const decl = root.first_child?.first_child?.next_sibling?.first_child
617613

618-
it('should parse calc operators', () => {
619-
const root = parse('body { width: calc(100% - 20px); }')
620-
const decl = root.first_child?.first_child?.next_sibling?.first_child
621-
const func = decl?.first_child!.children[0]
614+
expect(decl?.first_child!.children[1].type).toBe(OPERATOR)
615+
expect(decl?.first_child!.children[1].text).toBe(',')
616+
expect(decl?.first_child!.children[1].name).toBe(undefined)
617+
expect(decl?.first_child!.children[1].value).toBe(',')
618+
})
622619

623-
expect(func?.children[1].type).toBe(OPERATOR)
624-
expect(func?.children[1].text).toBe('-')
625-
})
620+
it('should parse calc operators', () => {
621+
const root = parse('body { width: calc(100% - 20px); }')
622+
const decl = root.first_child?.first_child?.next_sibling?.first_child
623+
const func = decl?.first_child!.children[0]
626624

627-
it('should parse all calc operators', () => {
628-
const root = parse('body { width: calc(1px + 2px * 3px / 4px - 5px); }')
629-
const decl = root.first_child?.first_child?.next_sibling?.first_child
630-
const func = decl?.first_child!.children[0]
625+
expect(func?.children[1].type).toBe(OPERATOR)
626+
expect(func?.children[1].text).toBe('-')
627+
expect(func?.children[1].name).toBe(undefined)
628+
expect(func?.children[1].value).toBe('-')
629+
})
631630

632-
const operators = func?.children.filter((n) => n.type === OPERATOR)
633-
expect(operators).toHaveLength(4)
634-
expect(operators?.[0].text).toBe('+')
635-
expect(operators?.[1].text).toBe('*')
636-
expect(operators?.[2].text).toBe('/')
637-
expect(operators?.[3].text).toBe('-')
638-
})
631+
it('should parse all calc operators', () => {
632+
const root = parse('body { width: calc(1px + 2px * 3px / 4px - 5px); }')
633+
const decl = root.first_child?.first_child?.next_sibling?.first_child
634+
const func = decl?.first_child!.children[0]
635+
636+
const operators = func?.children.filter((n) => n.type === OPERATOR)
637+
expect(operators).toHaveLength(4)
638+
expect(operators?.[0].text).toBe('+')
639+
expect(operators?.[0].name).toBe(undefined)
640+
expect(operators?.[0].value).toBe('+')
641+
expect(operators?.[1].text).toBe('*')
642+
expect(operators?.[1].name).toBe(undefined)
643+
expect(operators?.[1].value).toBe('*')
644+
expect(operators?.[2].text).toBe('/')
645+
expect(operators?.[2].name).toBe(undefined)
646+
expect(operators?.[2].value).toBe('/')
647+
expect(operators?.[3].text).toBe('-')
648+
expect(operators?.[3].name).toBe(undefined)
649+
expect(operators?.[3].value).toBe('-')
639650
})
651+
})
640652

641653
describe('PARENTHESIS', () => {
642654
it('should parse parenthesized expressions in calc()', () => {

0 commit comments

Comments
 (0)