diff --git a/README.md b/README.md index 6ba8acb..61fe430 100644 --- a/README.md +++ b/README.md @@ -119,6 +119,7 @@ parse_selector_list('h1, h2').children.map((s) => s.text) // ["h1", "h2"] const decl = parse_declaration('color: red !important') decl.property // "color" decl.is_important // true +decl.important // "!important" for (const node of parse_value('10px 20px')) { node.type_name // "Dimension" diff --git a/src/api.test.ts b/src/api.test.ts index 81bfef5..f0616d5 100644 --- a/src/api.test.ts +++ b/src/api.test.ts @@ -1124,6 +1124,7 @@ describe('CSSNode', () => { const clone = decl.clone() expect(clone.is_important).toBe(true) + expect(clone.important).toBe('!important') }) test('is_important is false', () => { diff --git a/src/arena.ts b/src/arena.ts index 850f5fa..c0040cf 100644 --- a/src/arena.ts +++ b/src/arena.ts @@ -115,6 +115,7 @@ export class CSSDataArena { private count: number // Number of nodes currently allocated private growth_count: number // Number of times the arena has grown private overflow_lengths: Map // Stores actual lengths for nodes > 65535 chars + private important_ranges: Map // Source range of "!important" per declaration; only !important declarations have an entry, so nodes stay 32 bytes private parents: Uint32Array | null = null // Built on first get_parent() call; nothing is stored per node while parsing // Growth multiplier when capacity is exceeded @@ -145,6 +146,7 @@ export class CSSDataArena { this.buffer = new ArrayBuffer(initial_capacity * BYTES_PER_NODE) this.view = new DataView(this.buffer) this.overflow_lengths = new Map() + this.important_ranges = new Map() } // Calculate recommended initial capacity based on CSS source size @@ -297,6 +299,17 @@ export class CSSDataArena { this.overflow_lengths.set(node_index, length) } + // Mark a declaration as important and record where its "!important" text sits in the source + set_important(node_index: number, start: number, end: number): void { + this.set_flag(node_index, FLAG_IMPORTANT) + this.important_ranges.set(node_index, [start, end]) + } + + // Source range [start, end) of the "!important" text, undefined if the declaration isn't important + get_important(node_index: number): [number, number] | undefined { + return this.important_ranges.get(node_index) + } + // Write content start delta (offset from startOffset) set_content_start_delta(node_index: number, delta: number): void { this.view.setUint16(this.node_offset(node_index) + 16, delta, true) diff --git a/src/css-node.ts b/src/css-node.ts index ff3ca8b..177c6a6 100644 --- a/src/css-node.ts +++ b/src/css-node.ts @@ -210,6 +210,7 @@ export type PlainCSSNode = { // Flags (only when true) is_important?: boolean + important?: string | null is_vendor_prefixed?: boolean is_browserhack?: boolean has_error?: boolean @@ -286,6 +287,7 @@ const enumerable_properties = [ 'is_vendor_prefixed', 'has_error', 'is_important', + 'important', 'left', 'right', ] as const @@ -709,6 +711,15 @@ export class CSSNode { return this.arena.has_flag(this.index, FLAG_IMPORTANT) } + // Exact source text of the important annotation ("!important", "! IE"), null if absent + get important(): string | null | undefined { + if (this.type !== DECLARATION) { + return undefined + } + const range = this.arena.get_important(this.index) + return range ? this.source.substring(range[0], range[1]) : null + } + // Check if this declaration has a browser hack prefix get is_browserhack(): boolean | undefined { if (this.type !== DECLARATION) { diff --git a/src/node-types.test.ts b/src/node-types.test.ts index b1e84c3..b8b904d 100644 --- a/src/node-types.test.ts +++ b/src/node-types.test.ts @@ -177,11 +177,23 @@ describe('type narrowing — compile-time', () => { expectTypeOf(decl).toExtend() expectTypeOf(decl.property).toEqualTypeOf() expectTypeOf(decl.is_important).toEqualTypeOf() + expectTypeOf(decl.important).toEqualTypeOf() expectTypeOf(decl.is_browserhack).toEqualTypeOf() // `name`, `prelude`, `block` are absent on Declaration — verified by tsc } }) + test('Declaration narrows important to string / null via is_important', () => { + const decl = parse_declaration('color: red !important') + if (decl.is_important) { + expectTypeOf(decl.important).toEqualTypeOf() + expect(decl.important).toBe('!important') + } else { + expectTypeOf(decl.important).toEqualTypeOf() + } + expect(parse_declaration('color: red').important).toBeNull() + }) + test('is_block narrows is_empty to boolean', () => { const root = parse('a { color: red }') const rule = root.first_child! as Rule diff --git a/src/node-types.ts b/src/node-types.ts index e0f0a27..4a4f045 100644 --- a/src/node-types.ts +++ b/src/node-types.ts @@ -202,6 +202,10 @@ export type Atrule = WithClone< } > +/** + * When `is_important` is true, `important` is the exact text as written + * (e.g. "!important", "! IE"); otherwise it is null. + */ export type Declaration = Leaf< typeof DECLARATION, 'Declaration', @@ -210,9 +214,8 @@ export type Declaration = Leaf< readonly property: string /** VALUE node (parse_values=true), RAW node (parse_values=false), or null */ readonly value: Value | Raw | null - readonly is_important: boolean readonly is_browserhack: boolean - } + } & Toggle<'is_important', 'important', string> > export type SelectorNode = diff --git a/src/parse-declaration.test.ts b/src/parse-declaration.test.ts index 075fc59..faa1f3c 100644 --- a/src/parse-declaration.test.ts +++ b/src/parse-declaration.test.ts @@ -145,6 +145,35 @@ describe('parse_declaration', () => { }) }) + describe('important text', () => { + test.each([ + ['color: red !important', '!important'], + ['color: red !important;', '!important'], + ['color: red!important', '!important'], + ['color: red ! important', '! important'], + ['color: red !IMPORTANT', '!IMPORTANT'], + ['color: red !ie', '!ie'], + ['color: red !important /* c */;', '!important'], + ['color: red !important/* c */;', '!important'], + ['color: red /* ! */ !important', '!important'], + ['color: red /* c */ ! /* c */ important;', '! /* c */ important'], + ['color: !important', '!important'], + ])('%s', (source, expected) => { + expect(parse_declaration(source).important).toBe(expected) + }) + + test('null when not important', () => { + expect(parse_declaration('color: red').important).toBeNull() + expect(parse_declaration('color: red;').important).toBeNull() + expect(parse_declaration('color: red !').important).toBeNull() + }) + + test('undefined on non-declaration nodes', () => { + const value = parse_declaration('color: red !important').first_child! + expect((value as { important?: unknown }).important).toBeUndefined() + }) + }) + describe('Vendor Prefixes', () => { test('-webkit- vendor prefix', () => { const node = parse_declaration('-webkit-transform: rotate(45deg)') diff --git a/src/parse-declaration.ts b/src/parse-declaration.ts index 83f8664..4c1a74f 100644 --- a/src/parse-declaration.ts +++ b/src/parse-declaration.ts @@ -1,6 +1,6 @@ // Declaration Parser - Parses CSS declarations into structured AST nodes import { Lexer, type LexerPosition } from './tokenize.ts' -import { CSSDataArena, DECLARATION, RAW, FLAG_IMPORTANT, FLAG_BROWSERHACK } from './arena.ts' +import { CSSDataArena, DECLARATION, RAW, FLAG_BROWSERHACK } from './arena.ts' import { ValueParser } from './parse-value.ts' import { is_vendor_prefixed, @@ -160,7 +160,8 @@ export class DeclarationParser { let value_end = value_start // Parse value (everything until ';' or EOF) - let has_important = false + let important_start = 0 + let important_end = 0 // stays 0 unless !important is found let last_end = value_start // Track parenthesis depth to handle semicolons inside functions (e.g., url(data:image/png;base64,...)) let paren_depth = 0 @@ -216,12 +217,12 @@ export class DeclarationParser { if (stop_ch === CHAR_EXCLAMATION) { // Mark end of value before !important value_end = lexer.pos + important_start = value_end lexer.pos++ // consume '!' // Check if next token is an identifier (doesn't verify it's literally "important") const next_type = lexer.next_token(true) // skip whitespace if (next_type === TOKEN_IDENT) { - has_important = true - last_end = lexer.token_end + last_end = important_end = lexer.token_end lexer.next_token(true) // Advance to next token after "important", skip whitespace break } @@ -291,9 +292,9 @@ export class DeclarationParser { } } - // Set !important flag if found - if (has_important) { - this.arena.set_flag(declaration, FLAG_IMPORTANT) + // Set !important flag and record its text range if found + if (important_end > 0) { + this.arena.set_important(declaration, important_start, important_end) } // Set browser hack flag if found