Numeric values are missing from NumberLiterals with Units #15

Closed
opened 2025-01-24 13:28:39 +01:00 by SirPepe · 2 comments
SirPepe commented 2025-01-24 13:28:39 +01:00 (Migrated from github.com)
import { parser } from "@lezer/css";
import { printTree } from "@lezer-unofficial/printer";
const source = "a { width: 0px }";
const tree = parser.parse(source);
console.log(printTree(tree, source));

Results in the NumberLiteral containing only the unit, but not the actual numeric value:

StyleSheet [1:0..1:16]
┗━  RuleSet [1:0..1:16]
    ┣━  TagSelector [1:0..1:1]
    ┃   ┗━  TagName [1:0..1:1]: "a"
    ┗━  Block [1:2..1:16]
        ┣━  { [1:2..1:3]: "{"
        ┣━  Declaration [1:4..1:14]
        ┃   ┣━  PropertyName [1:4..1:9]: "width"
        ┃   ┣━  : [1:9..1:10]: ":"
        ┃   ┗━  NumberLiteral [1:11..1:14]
        ┃       ┗━  Unit [1:12..1:14]: "px"
        ┗━  } [1:15..1:16]: "}"

Compared to just width:0:

StyleSheet [1:0..1:14]
┗━  RuleSet [1:0..1:14]
    ┣━  TagSelector [1:0..1:1]
    ┃   ┗━  TagName [1:0..1:1]: "a"
    ┗━  Block [1:2..1:14]
        ┣━  { [1:2..1:3]: "{"
        ┣━  Declaration [1:4..1:12]
        ┃   ┣━  PropertyName [1:4..1:9]: "width"
        ┃   ┣━  : [1:9..1:10]: ":"
        ┃   ┗━  NumberLiteral [1:11..1:12]: "0"
        ┗━  } [1:13..1:14]: "}"
```js import { parser } from "@lezer/css"; import { printTree } from "@lezer-unofficial/printer"; const source = "a { width: 0px }"; const tree = parser.parse(source); console.log(printTree(tree, source)); ``` Results in the NumberLiteral containing only the unit, but not the actual numeric value: ``` StyleSheet [1:0..1:16] ┗━ RuleSet [1:0..1:16] ┣━ TagSelector [1:0..1:1] ┃ ┗━ TagName [1:0..1:1]: "a" ┗━ Block [1:2..1:16] ┣━ { [1:2..1:3]: "{" ┣━ Declaration [1:4..1:14] ┃ ┣━ PropertyName [1:4..1:9]: "width" ┃ ┣━ : [1:9..1:10]: ":" ┃ ┗━ NumberLiteral [1:11..1:14] ┃ ┗━ Unit [1:12..1:14]: "px" ┗━ } [1:15..1:16]: "}" ``` Compared to just `width:0`: ``` StyleSheet [1:0..1:14] ┗━ RuleSet [1:0..1:14] ┣━ TagSelector [1:0..1:1] ┃ ┗━ TagName [1:0..1:1]: "a" ┗━ Block [1:2..1:14] ┣━ { [1:2..1:3]: "{" ┣━ Declaration [1:4..1:12] ┃ ┣━ PropertyName [1:4..1:9]: "width" ┃ ┣━ : [1:9..1:10]: ":" ┃ ┗━ NumberLiteral [1:11..1:12]: "0" ┗━ } [1:13..1:14]: "}" ```
marijnh commented 2025-01-24 14:14:45 +01:00 (Migrated from github.com)

What is the problem, though? The NumberLiteral node exactly covers the entire literal. If you want the part without the unit, subtract the Unit node's extent.

What is the problem, though? The `NumberLiteral` node exactly covers the entire literal. If you want the part without the unit, subtract the `Unit` node's extent.
SirPepe commented 2025-01-24 14:23:28 +01:00 (Migrated from github.com)

Ok, I just thought that this might be unintended :)

Ok, I just thought that this might be unintended :)
Sign in to join this conversation.
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
lezer/css#15
No description provided.