aboutsummaryrefslogtreecommitdiffstats
path: root/vanilla/node_modules/css-tree/lib/tokenizer/OffsetToLocation.js
diff options
context:
space:
mode:
authorAdam Mathes <adam@adammathes.com>2026-02-13 21:34:48 -0800
committerAdam Mathes <adam@adammathes.com>2026-02-13 21:34:48 -0800
commit76cb9c2a39d477a64824a985ade40507e3bbade1 (patch)
tree41e997aa9c6f538d3a136af61dae9424db2005a9 /vanilla/node_modules/css-tree/lib/tokenizer/OffsetToLocation.js
parent819a39a21ac992b1393244a4c283bbb125208c69 (diff)
downloadneko-76cb9c2a39d477a64824a985ade40507e3bbade1.tar.gz
neko-76cb9c2a39d477a64824a985ade40507e3bbade1.tar.bz2
neko-76cb9c2a39d477a64824a985ade40507e3bbade1.zip
feat(vanilla): add testing infrastructure and tests (NK-wjnczv)
Diffstat (limited to 'vanilla/node_modules/css-tree/lib/tokenizer/OffsetToLocation.js')
-rw-r--r--vanilla/node_modules/css-tree/lib/tokenizer/OffsetToLocation.js87
1 files changed, 87 insertions, 0 deletions
diff --git a/vanilla/node_modules/css-tree/lib/tokenizer/OffsetToLocation.js b/vanilla/node_modules/css-tree/lib/tokenizer/OffsetToLocation.js
new file mode 100644
index 0000000..cc584c0
--- /dev/null
+++ b/vanilla/node_modules/css-tree/lib/tokenizer/OffsetToLocation.js
@@ -0,0 +1,87 @@
+import { adoptBuffer } from './adopt-buffer.js';
+import { isBOM } from './char-code-definitions.js';
+
+const N = 10;
+const F = 12;
+const R = 13;
+
+function computeLinesAndColumns(host) {
+ const source = host.source;
+ const sourceLength = source.length;
+ const startOffset = source.length > 0 ? isBOM(source.charCodeAt(0)) : 0;
+ const lines = adoptBuffer(host.lines, sourceLength);
+ const columns = adoptBuffer(host.columns, sourceLength);
+ let line = host.startLine;
+ let column = host.startColumn;
+
+ for (let i = startOffset; i < sourceLength; i++) {
+ const code = source.charCodeAt(i);
+
+ lines[i] = line;
+ columns[i] = column++;
+
+ if (code === N || code === R || code === F) {
+ if (code === R && i + 1 < sourceLength && source.charCodeAt(i + 1) === N) {
+ i++;
+ lines[i] = line;
+ columns[i] = column;
+ }
+
+ line++;
+ column = 1;
+ }
+ }
+
+ lines[sourceLength] = line;
+ columns[sourceLength] = column;
+
+ host.lines = lines;
+ host.columns = columns;
+ host.computed = true;
+}
+
+export class OffsetToLocation {
+ constructor(source, startOffset, startLine, startColumn) {
+ this.setSource(source, startOffset, startLine, startColumn);
+ this.lines = null;
+ this.columns = null;
+ }
+ setSource(source = '', startOffset = 0, startLine = 1, startColumn = 1) {
+ this.source = source;
+ this.startOffset = startOffset;
+ this.startLine = startLine;
+ this.startColumn = startColumn;
+ this.computed = false;
+ }
+ getLocation(offset, filename) {
+ if (!this.computed) {
+ computeLinesAndColumns(this);
+ }
+
+ return {
+ source: filename,
+ offset: this.startOffset + offset,
+ line: this.lines[offset],
+ column: this.columns[offset]
+ };
+ }
+ getLocationRange(start, end, filename) {
+ if (!this.computed) {
+ computeLinesAndColumns(this);
+ }
+
+ return {
+ source: filename,
+ start: {
+ offset: this.startOffset + start,
+ line: this.lines[start],
+ column: this.columns[start]
+ },
+ end: {
+ offset: this.startOffset + end,
+ line: this.lines[end],
+ column: this.columns[end]
+ }
+ };
+ }
+};