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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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?)`
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export {
is_custom,
is_vendor_prefixed,
strip_vendor_prefix,
property_basename,
strip_value_hack,
str_equals,
str_starts_with,
Expand Down
1 change: 1 addition & 0 deletions src/string-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export {
is_custom,
is_vendor_prefixed,
strip_vendor_prefix,
property_basename,
strip_value_hack,
str_equals,
str_starts_with,
Expand Down
22 changes: 22 additions & 0 deletions src/string-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
str_index_of,
is_vendor_prefixed,
is_custom,
property_basename,
strip_value_hack,
CHAR_SPACE,
CHAR_TAB,
Expand Down Expand Up @@ -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'],
Expand Down
33 changes: 33 additions & 0 deletions src/string-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading