mirror of
https://github.com/marko-js/marko.git
synced 2025-12-08 19:26:05 +00:00
fix: add validation for dynamic tag names
This commit is contained in:
parent
1e4bdad7e5
commit
d25a5a57c5
5
.changeset/rude-berries-tan.md
Normal file
5
.changeset/rude-berries-tan.md
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
"@marko/runtime-tags": patch
|
||||
---
|
||||
|
||||
Add dev mode validation for dynamic tag names.
|
||||
@ -0,0 +1,5 @@
|
||||
{
|
||||
"vars": {
|
||||
"props": {}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1 @@
|
||||
Invalid tag name: "hello world". Tag names must start with a letter and contain only letters, numbers, periods, hyphens, and underscores.
|
||||
@ -0,0 +1 @@
|
||||
Invalid tag name: "hello world". Tag names must start with a letter and contain only letters, numbers, periods, hyphens, and underscores.
|
||||
@ -0,0 +1 @@
|
||||
// size: 0
|
||||
@ -0,0 +1,9 @@
|
||||
export const $template = "<!><!><!>";
|
||||
export const $walks = /* over(1), replace, over(2) */"b%c";
|
||||
const tagName = "hello world";
|
||||
import * as _ from "@marko/runtime-tags/debug/dom";
|
||||
const $dynamicTag = /* @__PURE__ */_._dynamic_tag("#text/0");
|
||||
export function $setup($scope) {
|
||||
$dynamicTag($scope, tagName);
|
||||
}
|
||||
export default /* @__PURE__ */_._template("__tests__/template.marko", $template, $walks, $setup);
|
||||
@ -0,0 +1,6 @@
|
||||
const tagName = "hello world";
|
||||
import * as _ from "@marko/runtime-tags/debug/html";
|
||||
export default _._template("__tests__/template.marko", input => {
|
||||
const $scope0_id = _._scope_id();
|
||||
_._dynamic_tag($scope0_id, "#text/0", tagName, {}, 0, 0, 0);
|
||||
});
|
||||
@ -0,0 +1 @@
|
||||
Invalid tag name: "hello world". Tag names must start with a letter and contain only letters, numbers, periods, hyphens, and underscores.
|
||||
@ -0,0 +1 @@
|
||||
Invalid tag name: "hello world". Tag names must start with a letter and contain only letters, numbers, periods, hyphens, and underscores.
|
||||
@ -0,0 +1 @@
|
||||
Invalid tag name: "hello world". Tag names must start with a letter and contain only letters, numbers, periods, hyphens, and underscores.
|
||||
@ -0,0 +1 @@
|
||||
Invalid tag name: "hello world". Tag names must start with a letter and contain only letters, numbers, periods, hyphens, and underscores.
|
||||
@ -0,0 +1,3 @@
|
||||
static const tagName = "hello world";
|
||||
${tagName}
|
||||
|
||||
@ -0,0 +1 @@
|
||||
export const error_runtime = true;
|
||||
@ -57,6 +57,14 @@ export function assertExclusiveAttrs(
|
||||
}
|
||||
}
|
||||
|
||||
export function assertValidTagName(tagName: string) {
|
||||
if (!/^[a-z][a-z0-9._-]*$/i.test(tagName)) {
|
||||
throw new Error(
|
||||
`Invalid tag name: "${tagName}". Tag names must start with a letter and contain only letters, numbers, periods, hyphens, and underscores.`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function throwErr(msg: string) {
|
||||
throw new Error(msg);
|
||||
}
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
import { assertValidTagName } from "../common/errors";
|
||||
import { forIn, forOf, forTo, forUntil } from "../common/for";
|
||||
import { normalizeDynamicRenderer } from "../common/helpers";
|
||||
import { DYNAMIC_TAG_SCRIPT_REGISTER_ID } from "../common/meta";
|
||||
@ -522,6 +523,10 @@ function createBranchWithTagNameOrRenderer(
|
||||
parentScope: Scope,
|
||||
parentNode: ParentNode,
|
||||
) {
|
||||
if (MARKO_DEBUG && typeof tagNameOrRenderer === "string") {
|
||||
assertValidTagName(tagNameOrRenderer);
|
||||
}
|
||||
|
||||
const branch = createBranch(
|
||||
$global,
|
||||
tagNameOrRenderer,
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
import { assertValidTagName } from "../common/errors";
|
||||
import { normalizeDynamicRenderer } from "../common/helpers";
|
||||
import { DYNAMIC_TAG_SCRIPT_REGISTER_ID } from "../common/meta";
|
||||
import { type Accessor, AccessorPrefix, ResumeSymbol } from "../common/types";
|
||||
@ -53,6 +54,10 @@ export let _dynamic_tag = (
|
||||
let result: unknown;
|
||||
|
||||
if (typeof renderer === "string") {
|
||||
if (MARKO_DEBUG) {
|
||||
assertValidTagName(renderer);
|
||||
}
|
||||
|
||||
const input = ((inputIsArgs
|
||||
? (inputOrArgs as unknown[])[0]
|
||||
: inputOrArgs) || {}) as Record<string, unknown>;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user