diff --git a/README.md b/README.md index 75ac033..f515b4b 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`, `walk_declarations`, `walk_selectors`, `walk_values`, `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`, `strip_value_hack`, `str_equals`, `str_starts_with`, `str_index_of` +- `/string-utils` — `is_custom`, `is_vendor_prefixed`, `strip_vendor_prefix`, `strip_value_hack`, `property_basename`, `str_equals`, `str_starts_with`, `str_index_of` - `/token-types` — token type constants (`TOKEN_*`) ### `parse(source, options?)` diff --git a/src/index.ts b/src/index.ts index b15c221..cb1f096 100644 --- a/src/index.ts +++ b/src/index.ts @@ -13,6 +13,7 @@ export { is_custom, is_vendor_prefixed, strip_vendor_prefix, + property_basename, strip_value_hack, str_equals, str_starts_with, diff --git a/src/string-helpers.ts b/src/string-helpers.ts index 318caeb..ebea2ad 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, strip_value_hack, str_equals, str_starts_with, diff --git a/src/string-utils.test.ts b/src/string-utils.test.ts index a3b07c9..4a224ae 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, strip_value_hack, CHAR_SPACE, CHAR_TAB, @@ -270,6 +271,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('strip_value_hack', () => { test.each([ ['10px\\9', '10px'], diff --git a/src/string-utils.ts b/src/string-utils.ts index 4234292..72f7708 100644 --- a/src/string-utils.ts +++ b/src/string-utils.ts @@ -231,6 +231,39 @@ export function strip_vendor_prefix(str: string): string { 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() +} + const CHAR_BACKSLASH = 0x5c // \ const CHAR_ZERO = 0x30 // 0 const CHAR_NINE = 0x39 // 9