Public: anyone can read this; the crew can change it.

Merge scanJsxTag and scanHtmlAttributes into one tag-attribute scanner #17088

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

mason/forge:mason/16802-merge-scanjsxtag-and-scanhtmlattributes → main 0 commits fork

  • Merged. mason/16802-merge-scanjsxtag-and-scanhtmlattributes was merged into main as 821c831.

Review

Sign in to review this change.
3 files changed +76 −100
src/web/highlight/generic.ts +2 −47
@@ -1,6 +1,7 @@
11 /** The table-driven scanner most languages use. */
22 import { isAlphaCh, isDigitCh, isIdentCh, isSpaceCh, isUpperCh, set } from "./chars.ts";
33 import { consumeStringBody, pushString, scanNumber, spaceRun } from "./lexing.ts";
4 import { scanTagAttributes } from "./tagattrs.ts";
45 import { type LangSpec } from "./specs.ts";
56 import { type Heredoc, type ScanState, type Scanned } from "./state.ts";
67 import { Out, type TokenClass, type TokenKind } from "./tokens.ts";
@@ -446,53 +447,7 @@ export function scanJsxTag(line: string, at: number, out: Out): number {
446447 out.push("punctuation", line.slice(at, start));
447448 out.push("keyword", line.slice(start, i), "tag");
448449
449 while (i < n) {
450 const c = line.charCodeAt(i);
451 if (isSpaceCh(c)) {
452 const j = spaceRun(line, i);
453 out.push("plain", line.slice(i, j));
454 i = j;
455 continue;
456 }
457 if (c === 62 /* > */) {
458 out.push("punctuation", ">");
459 return i + 1;
460 }
461 if (c === 47 && line.charCodeAt(i + 1) === 62) {
462 out.push("punctuation", "/>");
463 return i + 2;
464 }
465 if (c === 123 /* { */) return i; // an expression: back to the code scanner
466 if (c === 61 /* = */) {
467 out.push("operator", "=");
468 i++;
469 continue;
470 }
471 if (c === 34 || c === 39) {
472 const quote = line[i]!;
473 const consumed = consumeStringBody(line, i + 1, {
474 close: quote,
475 escape: true,
476 });
477 out.push("string", line.slice(i, consumed.end));
478 i = consumed.end;
479 continue;
480 }
481 if (isAlphaCh(c) || c === 95) {
482 let j = i;
483 while (
484 j < n &&
485 (isIdentCh(line.charCodeAt(j)) || line[j] === "-" || line[j] === ":")
486 )
487 j++;
488 out.push("type", line.slice(i, j), "attribute");
489 i = j;
490 continue;
491 }
492 out.push("punctuation", line[i]!);
493 i++;
494 }
495 return n;
450 return scanTagAttributes(line, i, out, true);
496451 }
497452
498453 // ---- Markdown ----
src/web/highlight/html.ts +3 −53
@@ -1,7 +1,6 @@
11 /** HTML scanner. */
22 import { scanOneLine } from "./scanners.ts";
3 import { isSpaceCh } from "./chars.ts";
4 import { consumeStringBody, spaceRun } from "./lexing.ts";
3 import { scanTagAttributes } from "./tagattrs.ts";
54 import { INITIAL_SCAN, type ScanState, type Scanned } from "./state.ts";
65 import { Out } from "./tokens.ts";
76
@@ -43,7 +42,7 @@ export function scanHtml(line: string, state: ScanState): Scanned {
4342
4443 while (i < n) {
4544 if (state.tag) {
46 i = scanHtmlAttributes(line, i, out);
45 i = scanTagAttributes(line, i, out, false);
4746 if (i < n || line.endsWith(">")) state = { ...state, tag: false };
4847 continue;
4948 }
@@ -83,7 +82,7 @@ export function scanHtml(line: string, state: ScanState): Scanned {
8382 out.push("punctuation", line.slice(i, i + lead));
8483 out.push("keyword", name[1]!, "tag");
8584 i += name[0].length;
86 const after = scanHtmlAttributes(line, i, out);
85 const after = scanTagAttributes(line, i, out, false);
8786 const tag = name[1]!.toLowerCase();
8887 const closed = line.slice(i, after).includes(">");
8988 i = after;
@@ -138,53 +137,4 @@ export function pushHtmlText(out: Out, text: string): void {
138137 out.push("plain", text.slice(run));
139138 }
140139
141 /** Attributes up to and including the `>` that closes the tag. */
142 export function scanHtmlAttributes(line: string, from: number, out: Out): number {
143 const n = line.length;
144 let i = from;
145 while (i < n) {
146 const c = line.charCodeAt(i);
147 if (isSpaceCh(c)) {
148 const j = spaceRun(line, i);
149 out.push("plain", line.slice(i, j));
150 i = j;
151 continue;
152 }
153 if (c === 62 /* > */) {
154 out.push("punctuation", ">");
155 return i + 1;
156 }
157 if (c === 47 && line.charCodeAt(i + 1) === 62) {
158 out.push("punctuation", "/>");
159 return i + 2;
160 }
161 if (c === 61) {
162 out.push("operator", "=");
163 i++;
164 continue;
165 }
166 if (c === 34 || c === 39) {
167 const consumed = consumeStringBody(line, i + 1, {
168 close: line[i]!,
169 escape: false,
170 });
171 out.push("string", line.slice(i, consumed.end));
172 i = consumed.end;
173 continue;
174 }
175 let j = i;
176 while (
177 j < n &&
178 !isSpaceCh(line.charCodeAt(j)) &&
179 line[j] !== "=" &&
180 line[j] !== ">"
181 )
182 j++;
183 if (j === i) j++;
184 out.push("type", line.slice(i, j), "attribute");
185 i = j;
186 }
187 return n;
188 }
189
190140 // ---- CSS (and SCSS/LESS) ----
src/web/highlight/tagattrs.ts added +71
@@ -0,0 +1,71 @@
1 /** Attributes of an HTML or JSX tag, shared by both scanners. */
2 import { isAlphaCh, isIdentCh, isSpaceCh } from "./chars.ts";
3 import { consumeStringBody, spaceRun } from "./lexing.ts";
4 import { Out } from "./tokens.ts";
5
6 /**
7 * Scan attributes from `from` up to and including the `>` or `/>` that closes
8 * the tag; returns where scanning stopped (the end of the line if the tag
9 * stays open). In JSX a `{` stops the scan so the code scanner can take the
10 * expression, strings honour backslash escapes, and an attribute name is an
11 * identifier with `-` and `:`; in HTML a name is any run up to a space, `=`
12 * or `>`.
13 */
14 export function scanTagAttributes(
15 line: string,
16 from: number,
17 out: Out,
18 jsx: boolean,
19 ): number {
20 const n = line.length;
21 let i = from;
22 while (i < n) {
23 const c = line.charCodeAt(i);
24 if (isSpaceCh(c)) {
25 const j = spaceRun(line, i);
26 out.push("plain", line.slice(i, j));
27 i = j;
28 continue;
29 }
30 if (c === 62 /* > */) {
31 out.push("punctuation", ">");
32 return i + 1;
33 }
34 if (c === 47 && line.charCodeAt(i + 1) === 62) {
35 out.push("punctuation", "/>");
36 return i + 2;
37 }
38 if (jsx && c === 123 /* { */) return i; // an expression: back to the code scanner
39 if (c === 61 /* = */) {
40 out.push("operator", "=");
41 i++;
42 continue;
43 }
44 if (c === 34 || c === 39) {
45 const consumed = consumeStringBody(line, i + 1, {
46 close: line[i]!,
47 escape: jsx,
48 });
49 out.push("string", line.slice(i, consumed.end));
50 i = consumed.end;
51 continue;
52 }
53 if (jsx && !(isAlphaCh(c) || c === 95)) {
54 out.push("punctuation", line[i]!);
55 i++;
56 continue;
57 }
58 let j = i;
59 while (
60 j < n &&
61 (jsx
62 ? isIdentCh(line.charCodeAt(j)) || line[j] === "-" || line[j] === ":"
63 : !isSpaceCh(line.charCodeAt(j)) && line[j] !== "=" && line[j] !== ">")
64 )
65 j++;
66 if (j === i) j++;
67 out.push("type", line.slice(i, j), "attribute");
68 i = j;
69 }
70 return n;
71 }

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs