Skip to content

Commit 6709f4c

Browse files
committed
fix: move textarea normalization to pre-analyze
1 parent f9cf89f commit 6709f4c

File tree

5 files changed

+34
-37
lines changed

5 files changed

+34
-37
lines changed

.changeset/three-cobras-wonder.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@marko/runtime-tags": patch
3+
---
4+
5+
Move textarea normalization to be pre-analyze.

packages/runtime-tags/src/translator/core/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import ScriptTag from "./script";
2222
import ServerTag from "./server";
2323
import StaticTag from "./static";
2424
import StyleTag from "./style";
25-
import TextAreaTag from "./textarea";
2625
import TitleTag from "./title";
2726
import TryTag from "./try";
2827

@@ -53,7 +52,6 @@ export default {
5352
"<server>": ServerTag,
5453
"<static>": StaticTag,
5554
"<style>": StyleTag,
56-
"<textarea>": TextAreaTag,
5755
"<title>": TitleTag,
5856
"<try>": TryTag,
5957
};

packages/runtime-tags/src/translator/core/script.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ export default {
148148
text: true,
149149
preserveWhitespace: true,
150150
},
151-
attributes: {},
152151
autocomplete: [
153152
{
154153
description: "Use to create a side effects.",
Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,31 @@
11
import { types as t } from "@marko/compiler";
2-
import type { Tag } from "@marko/compiler/babel-utils";
32

43
import normalizeStringExpression from "../util/normalize-string-expression";
54

6-
export default {
7-
parse(tag) {
8-
if (tag.node.body.body.length) {
9-
// convert textarea body into a static value attribute.
10-
const parts: (string | t.Expression)[] = [];
11-
for (const child of tag.node.body.body) {
12-
if (
13-
child.type === "MarkoText" ||
14-
(child.type === "MarkoPlaceholder" && child.escape)
15-
) {
16-
parts.push(child.value);
17-
} else {
18-
throw tag.hub.file.hub.buildError(
19-
child,
20-
"Unexpected content in textarea, only text and placeholders are supported.",
21-
SyntaxError,
22-
);
23-
}
5+
export function preAnalyze(tag: t.NodePath<t.MarkoTag>) {
6+
if (tag.node.body.body.length) {
7+
// convert textarea body into a static value attribute.
8+
const parts: (string | t.Expression)[] = [];
9+
for (const child of tag.node.body.body) {
10+
if (
11+
child.type === "MarkoText" ||
12+
(child.type === "MarkoPlaceholder" && child.escape)
13+
) {
14+
parts.push(child.value);
15+
} else {
16+
throw tag.hub.file.hub.buildError(
17+
child,
18+
"Unexpected content in textarea, only text and placeholders are supported.",
19+
SyntaxError,
20+
);
2421
}
25-
tag.node.attributes.push(
26-
t.markoAttribute(
27-
"value",
28-
normalizeStringExpression(parts) || buildUndefined(),
29-
),
30-
);
22+
}
3123

32-
tag.node.body.body = [];
24+
const textValue = normalizeStringExpression(parts);
25+
if (textValue) {
26+
tag.node.attributes.push(t.markoAttribute("value", textValue));
3327
}
34-
},
35-
parseOptions: {
36-
text: true,
37-
preserveWhitespace: true,
38-
},
39-
} as Tag;
4028

41-
function buildUndefined() {
42-
return t.unaryExpression("void", t.numericLiteral(0));
29+
tag.node.body.body = [];
30+
}
4331
}

packages/runtime-tags/src/translator/visitors/program/pre-analyze.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { types as t } from "@marko/compiler";
22

3+
import { preAnalyze as preAnalyzeTextarea } from "../../core/textarea";
34
import { generateUid, generateUidIdentifier } from "../../util/generate-uid";
45
import { getMarkoRoot, isMarko } from "../../util/get-root";
56
import withPreviousLocation from "../../util/with-previous-location";
@@ -78,6 +79,12 @@ function normalizeTag(state: State, tag: t.NodePath<t.MarkoTag>) {
7879
// <MyTag> --> <${MyTag}>
7980
state.crawl = true;
8081
node.name = withPreviousLocation(t.identifier(tagName), name);
82+
} else {
83+
switch (tagName) {
84+
case "textarea":
85+
preAnalyzeTextarea(tag);
86+
break;
87+
}
8188
}
8289
}
8390

0 commit comments

Comments
 (0)