Skip to content
Open
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
23 changes: 23 additions & 0 deletions src/__tests__/multiline-token-locations.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import test from "./util/runner.mjs";
import parser from "../../dist/index.js";

test("multiline comments retain their original start position", (t) => {
const input = ".a/*one\ntwo*/.b";
const root = parser().astSync(input);
const comment = root.first.at(1);
t.deepEqual(comment.source.start, { line: 1, column: 3 });
t.deepEqual(comment.source.end, { line: 2, column: 5 });
t.is(root.atPosition(1, 4), comment);
t.is(root.atPosition(2, 4), comment);
t.deepEqual(root.first.last.source.start, { line: 2, column: 6 });
t.is(String(root), input);
});

test("escaped line continuations advance following selector positions", (t) => {
const input = '[data-text="one\\\ntwo"].after';
const root = parser().astSync(input);
t.deepEqual(root.first.first.source.end, { line: 2, column: 5 });
t.deepEqual(root.first.last.source.start, { line: 2, column: 6 });
t.is(root.atPosition(2, 6), root.first.last);
t.is(String(root), input);
});
12 changes: 9 additions & 3 deletions src/tokenize.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ export default function tokenize(input) {

while (start < length) {
code = css.charCodeAt(start);
const startLine = line;

if (code === t.newline) {
offset = start;
Expand Down Expand Up @@ -226,9 +227,14 @@ export default function tokenize(input) {
} while (escaped);

tokenType = t.str;
endLine = line;
endColumn = start - offset;
end = next + 1;
content = css.slice(start, end);
lines = content.split("\n");
last = lines.length - 1;
nextOffset = last > 0 ? next - lines[last].length : offset;
line += last;
endLine = line;
endColumn = next - nextOffset;
break;

default:
Expand Down Expand Up @@ -274,7 +280,7 @@ export default function tokenize(input) {
// Ensure that the token structure remains consistent
tokens.push([
tokenType, // [0] Token type
line, // [1] Starting line
tokenType === t.comment || tokenType === t.str ? startLine : line, // [1] Starting line
start - offset, // [2] Starting column
endLine, // [3] Ending line
endColumn, // [4] Ending column
Expand Down
Loading