Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/css-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ export class CSSNode {

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

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

if (type === OPERATOR) {
return this.get_content()
}

// For other nodes, return as string
let start = this.arena.get_value_start(this.index)
let length = this.arena.get_value_length(this.index)
Expand Down
62 changes: 37 additions & 25 deletions src/parse-value.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -607,36 +607,48 @@ describe('Value Node Types', () => {
})

describe('OPERATOR', () => {
it('should parse comma operator', () => {
const root = parse('body { font-family: Arial, sans-serif; }')
const decl = root.first_child?.first_child?.next_sibling?.first_child

expect(decl?.first_child!.children[1].type).toBe(OPERATOR)
expect(decl?.first_child!.children[1].text).toBe(',')
})
it('should parse comma operator', () => {
const root = parse('body { font-family: Arial, sans-serif; }')
const decl = root.first_child?.first_child?.next_sibling?.first_child

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

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

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

const operators = func?.children.filter((n) => n.type === OPERATOR)
expect(operators).toHaveLength(4)
expect(operators?.[0].text).toBe('+')
expect(operators?.[1].text).toBe('*')
expect(operators?.[2].text).toBe('/')
expect(operators?.[3].text).toBe('-')
})
it('should parse all calc operators', () => {
const root = parse('body { width: calc(1px + 2px * 3px / 4px - 5px); }')
const decl = root.first_child?.first_child?.next_sibling?.first_child
const func = decl?.first_child!.children[0]

const operators = func?.children.filter((n) => n.type === OPERATOR)
expect(operators).toHaveLength(4)
expect(operators?.[0].text).toBe('+')
expect(operators?.[0].name).toBe(undefined)
expect(operators?.[0].value).toBe('+')
expect(operators?.[1].text).toBe('*')
expect(operators?.[1].name).toBe(undefined)
expect(operators?.[1].value).toBe('*')
expect(operators?.[2].text).toBe('/')
expect(operators?.[2].name).toBe(undefined)
expect(operators?.[2].value).toBe('/')
expect(operators?.[3].text).toBe('-')
expect(operators?.[3].name).toBe(undefined)
expect(operators?.[3].value).toBe('-')
})
})

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