@projectwallace/css-parser 0.11.2 → 0.11.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/css-node.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { DECLARATION, DIMENSION, NUMBER, URL, STRING, AT_RULE, AT_RULE_PRELUDE, STYLE_RULE, ATTRIBUTE_SELECTOR, FLAG_IMPORTANT, FLAG_BROWSERHACK, IDENTIFIER, FUNCTION, PSEUDO_ELEMENT_SELECTOR, PSEUDO_CLASS_SELECTOR, FLAG_HAS_ERROR, FLAG_HAS_BLOCK, FLAG_HAS_DECLARATIONS, BLOCK, COMMENT, FLAG_HAS_PARENS, NTH_SELECTOR, NTH_OF_SELECTOR, SELECTOR_LIST, FEATURE_RANGE, PRELUDE_OPERATOR, LAYER_NAME, SUPPORTS_QUERY, CONTAINER_QUERY, MEDIA_TYPE, MEDIA_FEATURE, MEDIA_QUERY, LANG_SELECTOR, NESTING_SELECTOR, UNIVERSAL_SELECTOR, COMBINATOR, ID_SELECTOR, CLASS_SELECTOR, TYPE_SELECTOR, VALUE, PARENTHESIS, OPERATOR, HASH, SELECTOR, STYLESHEET } from './arena.js';
2
2
  import { str_starts_with, is_vendor_prefixed, is_whitespace, CHAR_MINUS_HYPHEN, CHAR_PLUS } from './string-utils.js';
3
- import { parse_dimension } from './parse-utils.js';
3
+ import { parse_dimension } from './parse-dimension.js';
4
4
 
5
5
  const TYPE_NAMES = {
6
6
  [STYLESHEET]: "StyleSheet",
package/dist/index.d.ts CHANGED
@@ -3,6 +3,7 @@ export { parse_selector } from './parse-selector';
3
3
  export { parse_atrule_prelude } from './parse-atrule-prelude';
4
4
  export { parse_declaration } from './parse-declaration';
5
5
  export { parse_value } from './parse-value';
6
+ export { parse_dimension } from './parse-dimension';
6
7
  export { tokenize } from './tokenize';
7
8
  export { walk, traverse, SKIP, BREAK } from './walk';
8
9
  export { is_custom, is_vendor_prefixed, str_equals, str_starts_with, str_index_of } from './string-utils';
package/dist/index.js CHANGED
@@ -3,6 +3,7 @@ export { parse_selector } from './parse-selector.js';
3
3
  export { parse_atrule_prelude } from './parse-atrule-prelude.js';
4
4
  export { parse_declaration } from './parse-declaration.js';
5
5
  export { parse_value } from './parse-value.js';
6
+ export { parse_dimension } from './parse-dimension.js';
6
7
  export { tokenize } from './tokenize.js';
7
8
  export { BREAK, SKIP, traverse, walk } from './walk.js';
8
9
  export { is_custom, is_vendor_prefixed, str_equals, str_index_of, str_starts_with } from './string-utils.js';
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Parse a dimension string into numeric value and unit
3
+ *
4
+ * @param text - Dimension text like "100px", "50%", "1.5em"
5
+ * @returns Object with value (number) and unit (string)
6
+ *
7
+ * Examples:
8
+ * - "100px" → { value: 100, unit: "px" }
9
+ * - "50%" → { value: 50, unit: "%" }
10
+ * - "1.5em" → { value: 1.5, unit: "em" }
11
+ * - "-10rem" → { value: -10, unit: "rem" }
12
+ */
13
+ export declare function parse_dimension(text: string): {
14
+ value: number;
15
+ unit: string;
16
+ };
@@ -0,0 +1,36 @@
1
+ import { is_digit, CHAR_PERIOD, CHAR_MINUS_HYPHEN, CHAR_PLUS } from './string-utils.js';
2
+
3
+ function parse_dimension(text) {
4
+ let num_end = 0;
5
+ for (let i = 0; i < text.length; i++) {
6
+ let ch = text.charCodeAt(i);
7
+ if (ch === 101 || ch === 69) {
8
+ if (i + 1 < text.length) {
9
+ let next_ch = text.charCodeAt(i + 1);
10
+ if (is_digit(next_ch)) {
11
+ num_end = i + 1;
12
+ continue;
13
+ }
14
+ if ((next_ch === 43 || next_ch === 45) && i + 2 < text.length) {
15
+ let afterSign = text.charCodeAt(i + 2);
16
+ if (is_digit(afterSign)) {
17
+ num_end = i + 1;
18
+ continue;
19
+ }
20
+ }
21
+ }
22
+ break;
23
+ }
24
+ if (is_digit(ch) || ch === CHAR_PERIOD || ch === CHAR_MINUS_HYPHEN || ch === CHAR_PLUS) {
25
+ num_end = i + 1;
26
+ } else {
27
+ break;
28
+ }
29
+ }
30
+ let num_str = text.substring(0, num_end);
31
+ let unit = text.substring(num_end);
32
+ let value = num_str ? parseFloat(num_str) : 0;
33
+ return { value, unit };
34
+ }
35
+
36
+ export { parse_dimension };
@@ -1,16 +1 @@
1
- /**
2
- * Parse a dimension string into numeric value and unit
3
- *
4
- * @param text - Dimension text like "100px", "50%", "1.5em"
5
- * @returns Object with value (number) and unit (string)
6
- *
7
- * Examples:
8
- * - "100px" → { value: 100, unit: "px" }
9
- * - "50%" → { value: 50, unit: "%" }
10
- * - "1.5em" → { value: 1.5, unit: "em" }
11
- * - "-10rem" → { value: -10, unit: "rem" }
12
- */
13
- export declare function parse_dimension(text: string): {
14
- value: number;
15
- unit: string;
16
- };
1
+ export {};
@@ -1,37 +1,5 @@
1
- import { is_digit, CHAR_PERIOD, CHAR_MINUS_HYPHEN, CHAR_PLUS, is_whitespace, CHAR_FORWARD_SLASH, CHAR_ASTERISK } from './string-utils.js';
1
+ import { is_whitespace, CHAR_FORWARD_SLASH, CHAR_ASTERISK } from './string-utils.js';
2
2
 
3
- function parse_dimension(text) {
4
- let num_end = 0;
5
- for (let i = 0; i < text.length; i++) {
6
- let ch = text.charCodeAt(i);
7
- if (ch === 101 || ch === 69) {
8
- if (i + 1 < text.length) {
9
- let next_ch = text.charCodeAt(i + 1);
10
- if (is_digit(next_ch)) {
11
- num_end = i + 1;
12
- continue;
13
- }
14
- if ((next_ch === 43 || next_ch === 45) && i + 2 < text.length) {
15
- let afterSign = text.charCodeAt(i + 2);
16
- if (is_digit(afterSign)) {
17
- num_end = i + 1;
18
- continue;
19
- }
20
- }
21
- }
22
- break;
23
- }
24
- if (is_digit(ch) || ch === CHAR_PERIOD || ch === CHAR_MINUS_HYPHEN || ch === CHAR_PLUS) {
25
- num_end = i + 1;
26
- } else {
27
- break;
28
- }
29
- }
30
- let num_str = text.substring(0, num_end);
31
- let unit = text.substring(num_end);
32
- let value = num_str ? parseFloat(num_str) : 0;
33
- return { value, unit };
34
- }
35
3
  function skip_whitespace_forward(source, pos, end) {
36
4
  while (pos < end && is_whitespace(source.charCodeAt(pos))) {
37
5
  pos++;
@@ -89,4 +57,4 @@ function trim_boundaries(source, start, end) {
89
57
  return [start, end];
90
58
  }
91
59
 
92
- export { parse_dimension, skip_whitespace_and_comments_backward, skip_whitespace_and_comments_forward, skip_whitespace_forward, trim_boundaries };
60
+ export { skip_whitespace_and_comments_backward, skip_whitespace_and_comments_forward, skip_whitespace_forward, trim_boundaries };
package/dist/walk.d.ts CHANGED
@@ -7,7 +7,8 @@ type WalkCallback = (node: CSSNode, depth: number) => void | typeof SKIP | typeo
7
7
  * Return SKIP to skip children, BREAK to stop traversal. See API.md for examples.
8
8
  *
9
9
  * @param node - The root node to start walking from
10
- * @param callback - Function called for each node. Receives the node and its depth (0 for root).
10
+ * @param callback - Function called for each node. Receives the node and its nesting depth.
11
+ * Depth increments only for STYLE_RULE and AT_RULE nodes (tracks rule nesting, not tree depth).
11
12
  * @param depth - Starting depth (default: 0)
12
13
  */
13
14
  export declare function walk(node: CSSNode, callback: WalkCallback, depth?: number): boolean;
package/dist/walk.js CHANGED
@@ -1,3 +1,5 @@
1
+ import { STYLE_RULE, AT_RULE } from './arena.js';
2
+
1
3
  const SKIP = /* @__PURE__ */ Symbol("SKIP");
2
4
  const BREAK = /* @__PURE__ */ Symbol("BREAK");
3
5
  function walk(node, callback, depth = 0) {
@@ -8,9 +10,13 @@ function walk(node, callback, depth = 0) {
8
10
  if (result === SKIP) {
9
11
  return true;
10
12
  }
13
+ let child_depth = depth;
14
+ if (node.type === STYLE_RULE || node.type === AT_RULE) {
15
+ child_depth = depth + 1;
16
+ }
11
17
  let child = node.first_child;
12
18
  while (child) {
13
- const should_continue = walk(child, callback, depth + 1);
19
+ const should_continue = walk(child, callback, child_depth);
14
20
  if (!should_continue) {
15
21
  return false;
16
22
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@projectwallace/css-parser",
3
- "version": "0.11.2",
3
+ "version": "0.11.4",
4
4
  "description": "High-performance CSS lexer and parser, optimized for CSS inspection and analysis",
5
5
  "author": "Bart Veneman <bart@projectwallace.com>",
6
6
  "license": "MIT",
@@ -46,6 +46,10 @@
46
46
  "./parse-anplusb": {
47
47
  "types": "./dist/parse-anplusb.d.ts",
48
48
  "import": "./dist/parse-anplusb.js"
49
+ },
50
+ "./parse-dimension": {
51
+ "types": "./dist/parse-dimension.d.ts",
52
+ "import": "./dist/parse-dimension.js"
49
53
  }
50
54
  },
51
55
  "files": [