Skip to content

Commit b765620

Browse files
committed
make value-parser -> parse-value
1 parent eb98e58 commit b765620

File tree

7 files changed

+30
-4
lines changed

7 files changed

+30
-4
lines changed

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
"types": "./dist/parse-atrule-prelude.d.ts",
3636
"import": "./dist/parse-atrule-prelude.js"
3737
},
38+
"./parse-value": {
39+
"types": "./dist/parse-value.d.ts",
40+
"import": "./dist/parse-value.js"
41+
},
3842
"./parse-anplusb": {
3943
"types": "./dist/parse-anplusb.d.ts",
4044
"import": "./dist/parse-anplusb.js"

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
export { parse } from './parse'
55
export { parse_selector } from './parse-selector'
66
export { parse_atrule_prelude } from './parse-atrule-prelude'
7+
export { parse_value } from './parse-value'
78
export { tokenize } from './tokenize'
89
export { walk, traverse } from './walk'
910

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Value Parser - Parses CSS declaration values into structured AST nodes
22
import { Lexer } from './lexer'
3-
import type { CSSDataArena } from './arena'
43
import {
4+
CSSDataArena,
55
NODE_VALUE_KEYWORD,
66
NODE_VALUE_NUMBER,
77
NODE_VALUE_DIMENSION,
@@ -25,6 +25,7 @@ import {
2525
TOKEN_RIGHT_PAREN,
2626
} from './token-types'
2727
import { is_whitespace, CHAR_MINUS_HYPHEN, CHAR_PLUS, CHAR_ASTERISK, CHAR_FORWARD_SLASH } from './string-utils'
28+
import { CSSNode } from './css-node'
2829

2930
export class ValueParser {
3031
private lexer: Lexer
@@ -216,3 +217,22 @@ export class ValueParser {
216217
return node
217218
}
218219
}
220+
221+
/**
222+
* Parse a CSS declaration value string and return an array of value AST nodes
223+
* @param value_string - The CSS value to parse (e.g., "1px solid red")
224+
* @returns An array of CSSNode objects representing the parsed value
225+
*/
226+
export function parse_value(value_string: string): CSSNode[] {
227+
// Create an arena for the value nodes
228+
const arena = new CSSDataArena(CSSDataArena.capacity_for_source(value_string.length))
229+
230+
// Create value parser
231+
const value_parser = new ValueParser(arena, value_string)
232+
233+
// Parse the entire source as a value
234+
const node_indices = value_parser.parse_value(0, value_string.length)
235+
236+
// Wrap each node index in a CSSNode
237+
return node_indices.map((index) => new CSSNode(arena, value_string, index))
238+
}

src/parse.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
FLAG_HAS_DECLARATIONS,
1616
} from './arena'
1717
import { CSSNode } from './css-node'
18-
import { ValueParser } from './value-parser'
18+
import { ValueParser } from './parse-value'
1919
import { SelectorParser } from './parse-selector'
2020
import { AtRulePreludeParser } from './parse-atrule-prelude'
2121
import {

test/build/exports.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import { NODE_AT_RULE, NODE_STYLE_RULE, NODE_STYLESHEET } from '../../dist/index
33

44
describe('Package exports', () => {
55
test('should export main exports from main entry', async () => {
6-
let { CSSNode, walk, walk_enter_leave, parse, parse_atrule_prelude, parse_selector } = await import('../../dist/index.js')
6+
let { CSSNode, walk, traverse, parse, parse_atrule_prelude, parse_selector } = await import('../../dist/index.js')
77

88
expect(typeof CSSNode).toBe('function')
99
expect(typeof walk).toBe('function')
10-
expect(typeof walk_enter_leave).toBe('function')
10+
expect(typeof traverse).toBe('function')
1111
expect(typeof parse_atrule_prelude).toBe('function')
1212
expect(typeof parse_selector).toBe('function')
1313
expect(typeof parse).toBe('function')

vite.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export default defineConfig({
99
parse: './src/parse.ts',
1010
'parse-selector': './src/parse-selector.ts',
1111
'parse-atrule-prelude': './src/parse-atrule-prelude.ts',
12+
'parse-value': './src/parse-value.ts',
1213
'parse-anplusb': './src/parse-anplusb.ts',
1314
},
1415
formats: ['es'],

0 commit comments

Comments
 (0)