From 09e7615bd5305e4450d80ccec57a1debf2a1187e Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 8 Nov 2026 19:24:15 +0000 Subject: [PATCH 1/2] feat: add basename and property_basename for normalized property names basename (declarations, media features, @supports declarations) is the property with browser hack and vendor prefix removed, lowercased unless it is a custom property. property_basename() is exported as a string helper. --- README.md | 4 +++- src/css-node.ts | 8 ++++++++ src/index.ts | 1 + src/node-types.ts | 7 +++++++ src/parse-declaration.test.ts | 6 ++++++ src/string-helpers.ts | 1 + src/string-utils.test.ts | 22 ++++++++++++++++++++++ src/string-utils.ts | 33 +++++++++++++++++++++++++++++++++ 8 files changed, 81 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9dc7ca1..e951358 100644 --- a/README.md +++ b/README.md @@ -81,7 +81,7 @@ Everything is exported from the package root. Each part is also available from i - `/walker` — `walk`, `traverse`, `SKIP`, `BREAK` - `/nodes` — node type constants (`STYLE_RULE`, `DECLARATION`, …), `NODE_TYPES` - `/node-types` — type guards (`is_declaration`, `is_rule`, …) and node types (`CSSNode`, `Rule`, …) -- `/string-utils` — `is_custom`, `is_vendor_prefixed`, `strip_vendor_prefix`, `str_equals`, `str_starts_with`, `str_index_of` +- `/string-utils` — `is_custom`, `is_vendor_prefixed`, `strip_vendor_prefix`, `property_basename`, `str_equals`, `str_starts_with`, `str_index_of` - `/token-types` — token type constants (`TOKEN_*`) ### `parse(source, options?)` @@ -95,6 +95,8 @@ rule.block.first_child.property // "color" rule.block.first_child.value.text // "red" ``` +Declarations (and media features) also expose `basename`: the property name with browser hack and vendor prefix removed, lowercased (custom properties keep their case), so `*Color` and `-WEBKIT-Color` both give `"color"`. + Options: - `parse_values` (default `true`) — parse declaration values into nodes diff --git a/src/css-node.ts b/src/css-node.ts index ebfcb52..6dafa49 100644 --- a/src/css-node.ts +++ b/src/css-node.ts @@ -67,6 +67,7 @@ import { CHAR_ASTERISK, is_whitespace, is_vendor_prefixed, + property_basename, str_starts_with, str_equals, } from './string-utils' @@ -460,6 +461,13 @@ export class CSSNode { return this.get_content() } + // Normalized `property`: hack and vendor prefix removed, lowercased (custom properties keep their case) + get basename(): string | undefined { + const { type } = this + if (type !== DECLARATION && type !== MEDIA_FEATURE && type !== SUPPORTS_DECLARATION) return + return property_basename(this.get_content()) + } + // Declarations: the value text ("1px solid blue"). Dimension/number: the numeric value. // String: content without quotes. URL: quoted string keeps quotes, unquoted URL doesn't. get value(): CSSNode | string | number | null | undefined { diff --git a/src/index.ts b/src/index.ts index d26499e..58a7e2c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -13,6 +13,7 @@ export { is_custom, is_vendor_prefixed, strip_vendor_prefix, + property_basename, str_equals, str_starts_with, str_index_of, diff --git a/src/node-types.ts b/src/node-types.ts index 7672d6a..f16ea68 100644 --- a/src/node-types.ts +++ b/src/node-types.ts @@ -138,6 +138,7 @@ export type ToPlain = PlainCSSNode & { type: T['type'] } & { | symbol | 'attr_operator' | 'attr_flags' + | 'basename' | 'has_children' | 'child_count' | 'children' @@ -187,6 +188,8 @@ export type Declaration = Leaf< { /** Property name, e.g. "color" */ readonly property: string + /** Normalized property name: hack and vendor prefix removed, lowercased (not for custom properties), e.g. "color" for "*Color" */ + readonly basename: string /** VALUE node (parse_values=true), RAW node (parse_values=false), or null */ readonly value: Value | Raw | null readonly is_important: boolean @@ -534,6 +537,8 @@ export type MediaFeature = WithClone< readonly type_name: 'Feature' /** Feature name, e.g. "min-width" */ readonly property: string + /** Normalized feature name: vendor prefix removed, lowercased, e.g. "min-device-pixel-ratio" for "-webkit-min-device-pixel-ratio" */ + readonly basename: string /** Feature value node (Dimension, Number, Identifier or Ratio), or null for boolean features like (hover). Hacks like `0\0` add further children after it. */ readonly value: Identifier | Number | Dimension | Ratio | null } @@ -575,6 +580,8 @@ export type SupportsDeclaration = WithClone< readonly type_name: 'SupportsDeclaration' /** Property name, e.g. "display" from "(display: flex)" */ readonly property: string + /** Normalized property name: hack and vendor prefix removed, lowercased */ + readonly basename: string /** VALUE node for the declaration's value, e.g. "flex" from "(display: flex)" */ readonly value: Value | null } diff --git a/src/parse-declaration.test.ts b/src/parse-declaration.test.ts index 1b1e19f..87088fb 100644 --- a/src/parse-declaration.test.ts +++ b/src/parse-declaration.test.ts @@ -198,6 +198,12 @@ describe('parse_declaration', () => { expect(node.is_browserhack).toBe(false) }) + test('basename', () => { + expect(parse_declaration('*Color: red').basename).toBe('color') + expect(parse_declaration('-WEBKIT-Transform: none').basename).toBe('transform') + expect(parse_declaration('--Foo: 1').basename).toBe('--Foo') + }) + test('normal property is not a browserhack', () => { const node = parse_declaration('color: red') expect(node.is_browserhack).toBe(false) diff --git a/src/string-helpers.ts b/src/string-helpers.ts index 8eddc34..1f44ac0 100644 --- a/src/string-helpers.ts +++ b/src/string-helpers.ts @@ -4,6 +4,7 @@ export { is_custom, is_vendor_prefixed, strip_vendor_prefix, + property_basename, str_equals, str_starts_with, str_index_of, diff --git a/src/string-utils.test.ts b/src/string-utils.test.ts index f51bcad..38304bd 100644 --- a/src/string-utils.test.ts +++ b/src/string-utils.test.ts @@ -6,6 +6,7 @@ import { str_index_of, is_vendor_prefixed, is_custom, + property_basename, CHAR_SPACE, CHAR_TAB, CHAR_NEWLINE, @@ -269,6 +270,27 @@ describe('string-utils', () => { }) }) + describe('property_basename', () => { + test.each([ + ['color', 'color'], + ['COLOR', 'color'], + ['*color', 'color'], + ['_Color', 'color'], + ['* color', 'color'], + ['#color', 'color'], + ['@color', 'color'], + ['-color', 'color'], + ['-webkit-transform', 'transform'], + ['-WEBKIT-Transform', 'transform'], + ['*-webkit-transform', 'transform'], + ['--Custom-Prop', '--Custom-Prop'], + ['-moz-', ''], + ['', ''], + ])('%s → %s', (input, expected) => { + expect(property_basename(input)).toBe(expected) + }) + }) + describe('is_vendor_prefixed', () => { test('should detect -webkit- vendor prefix', () => { const source = '-webkit-transform' diff --git a/src/string-utils.ts b/src/string-utils.ts index 25e1f41..2d65be1 100644 --- a/src/string-utils.ts +++ b/src/string-utils.ts @@ -230,3 +230,36 @@ export function strip_vendor_prefix(str: string): string { // Should never reach here if is_vendor_prefixed returned true return str } + +/** + * Normalized property name: browser hack prefix (`*`, `_`, `#`, …) and vendor prefix removed, + * lowercased. Custom properties (`--Foo`) keep their case. `*Color` → `color`, `-WEBKIT-Transform` → `transform`. + */ +export function property_basename(str: string): string { + let start = 0 + const length = str.length + // Skip hack prefix characters until something that can start a property name + while (start < length) { + const ch = str.charCodeAt(start) + if ( + (ch >= 0x61 && ch <= 0x7a) || // a-z + (ch >= 0x41 && ch <= 0x5a) || // A-Z + ch === CHAR_MINUS_HYPHEN || + ch === 0x5c || // escape + ch > 0x7f + ) { + break + } + start++ + } + if (str.charCodeAt(start) === CHAR_MINUS_HYPHEN) { + if (str.charCodeAt(start + 1) === CHAR_MINUS_HYPHEN) { + return start === 0 ? str : str.substring(start) + } + // `-webkit-foo` → `foo`; a lone `-` (`-color`) is a browser hack + const name = start === 0 ? str : str.substring(start) + if (is_vendor_prefixed(name)) return strip_vendor_prefix(name).toLowerCase() + return name.substring(1).toLowerCase() + } + return (start === 0 ? str : str.substring(start)).toLowerCase() +} From 1c4ccbc616ec949dbfdefbedb035e2221f9a3409 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 8 Nov 2026 19:41:11 +0000 Subject: [PATCH 2/2] refactor: drop basename node property, keep property_basename helper Per review: only the string helper is exposed, not an extra CSSNode property. --- README.md | 2 -- src/css-node.ts | 8 -------- src/node-types.ts | 7 ------- src/parse-declaration.test.ts | 6 ------ 4 files changed, 23 deletions(-) diff --git a/README.md b/README.md index e951358..45d7ab7 100644 --- a/README.md +++ b/README.md @@ -95,8 +95,6 @@ rule.block.first_child.property // "color" rule.block.first_child.value.text // "red" ``` -Declarations (and media features) also expose `basename`: the property name with browser hack and vendor prefix removed, lowercased (custom properties keep their case), so `*Color` and `-WEBKIT-Color` both give `"color"`. - Options: - `parse_values` (default `true`) — parse declaration values into nodes diff --git a/src/css-node.ts b/src/css-node.ts index 6dafa49..ebfcb52 100644 --- a/src/css-node.ts +++ b/src/css-node.ts @@ -67,7 +67,6 @@ import { CHAR_ASTERISK, is_whitespace, is_vendor_prefixed, - property_basename, str_starts_with, str_equals, } from './string-utils' @@ -461,13 +460,6 @@ export class CSSNode { return this.get_content() } - // Normalized `property`: hack and vendor prefix removed, lowercased (custom properties keep their case) - get basename(): string | undefined { - const { type } = this - if (type !== DECLARATION && type !== MEDIA_FEATURE && type !== SUPPORTS_DECLARATION) return - return property_basename(this.get_content()) - } - // Declarations: the value text ("1px solid blue"). Dimension/number: the numeric value. // String: content without quotes. URL: quoted string keeps quotes, unquoted URL doesn't. get value(): CSSNode | string | number | null | undefined { diff --git a/src/node-types.ts b/src/node-types.ts index f16ea68..7672d6a 100644 --- a/src/node-types.ts +++ b/src/node-types.ts @@ -138,7 +138,6 @@ export type ToPlain = PlainCSSNode & { type: T['type'] } & { | symbol | 'attr_operator' | 'attr_flags' - | 'basename' | 'has_children' | 'child_count' | 'children' @@ -188,8 +187,6 @@ export type Declaration = Leaf< { /** Property name, e.g. "color" */ readonly property: string - /** Normalized property name: hack and vendor prefix removed, lowercased (not for custom properties), e.g. "color" for "*Color" */ - readonly basename: string /** VALUE node (parse_values=true), RAW node (parse_values=false), or null */ readonly value: Value | Raw | null readonly is_important: boolean @@ -537,8 +534,6 @@ export type MediaFeature = WithClone< readonly type_name: 'Feature' /** Feature name, e.g. "min-width" */ readonly property: string - /** Normalized feature name: vendor prefix removed, lowercased, e.g. "min-device-pixel-ratio" for "-webkit-min-device-pixel-ratio" */ - readonly basename: string /** Feature value node (Dimension, Number, Identifier or Ratio), or null for boolean features like (hover). Hacks like `0\0` add further children after it. */ readonly value: Identifier | Number | Dimension | Ratio | null } @@ -580,8 +575,6 @@ export type SupportsDeclaration = WithClone< readonly type_name: 'SupportsDeclaration' /** Property name, e.g. "display" from "(display: flex)" */ readonly property: string - /** Normalized property name: hack and vendor prefix removed, lowercased */ - readonly basename: string /** VALUE node for the declaration's value, e.g. "flex" from "(display: flex)" */ readonly value: Value | null } diff --git a/src/parse-declaration.test.ts b/src/parse-declaration.test.ts index 87088fb..1b1e19f 100644 --- a/src/parse-declaration.test.ts +++ b/src/parse-declaration.test.ts @@ -198,12 +198,6 @@ describe('parse_declaration', () => { expect(node.is_browserhack).toBe(false) }) - test('basename', () => { - expect(parse_declaration('*Color: red').basename).toBe('color') - expect(parse_declaration('-WEBKIT-Transform: none').basename).toBe('transform') - expect(parse_declaration('--Foo: 1').basename).toBe('--Foo') - }) - test('normal property is not a browserhack', () => { const node = parse_declaration('color: red') expect(node.is_browserhack).toBe(false)