Simplify interpolation handling in ngNonBindable (#20115)

This commit is contained in:
fisker Cheung authored and GitHub committed 2026-09-20 21:52:10 +08:00
1 parent 0292df6bf3
commit 51ed2cbbf3
9 files changed
+68 -41

No files matched your search

+1 -1
View File
@@ -1,4 +1,4 @@
#### Preserve interpolations inside elements with `ngNonBindable` directive (#20044 by @splincode)
#### Preserve interpolations inside elements with `ngNonBindable` directive (#20044 by @splincode, #20115 by @fisker)
<!-- prettier-ignore -->
```html
+1 -5
View File
@@ -23,7 +23,6 @@ import {
isScriptLikeTag,
isVueNonHtmlBlock,
} from "./utilities/index.js";
import { isInNgNonBindable } from "./utilities/is-in-ng-non-bindable.js";
import isVueSfcWithTypescriptScript from "./utilities/is-vue-sfc-with-typescript-script.js";
const embeddedAngularControlFlowBlocks = new Set([
@@ -110,10 +109,7 @@ function embed(path, options) {
];
};
}
} else if (
node.parent.kind === "interpolation" &&
!isInNgNonBindable(path, options)
) {
} else if (node.parent.kind === "interpolation") {
return async (textToDoc) => {
const textToDocOptions = {
__isInHtmlInterpolation: true, // to avoid unexpected `}}`
+10 -1
View File
@@ -18,6 +18,7 @@ import {
CSS_WHITE_SPACE_TAGS,
} from "../constants.evaluate.js";
import isUnknownNamespace from "./is-unknown-namespace.js";
import { hasOrInNgNonBindable } from "./ng-non-bindable.js";
const htmlTrimLeadingBlankLines = (string) =>
string.replaceAll(/^[\t\f\r ]*\n/g, "");
@@ -121,7 +122,15 @@ function isScriptLikeTag(node, options) {
}
function canHaveInterpolation(node, options) {
return node.children && !isScriptLikeTag(node, options);
if (!node.children || isScriptLikeTag(node, options)) {
return false;
}
if (hasOrInNgNonBindable(node, options)) {
return false;
}
return true;
}
function isWhitespaceSensitiveNode(node, options) {
@@ -1,29 +0,0 @@
import { getOrInsertComputed } from "../../utilities/get-or-insert.js";
const angularNonBindableCache = new WeakMap();
function hasOrInNgNonBindable(node) {
return getOrInsertComputed(angularNonBindableCache, node, (node) => {
if (
node.kind === "element" &&
Object.hasOwn(node.attrMap, "ngNonBindable")
) {
return true;
}
if (!node.parent) {
return false;
}
return hasOrInNgNonBindable(node.parent);
});
}
function isInNgNonBindable(path, options) {
if (options.parser !== "angular") {
return false;
}
return hasOrInNgNonBindable(path.node.parent);
}
export { isInNgNonBindable };
@@ -0,0 +1,20 @@
import { getOrInsertComputed } from "../../utilities/get-or-insert.js";
const isElementWithNgNonBindable = (node) =>
node?.kind === "element" && Object.hasOwn(node.attrMap, "ngNonBindable");
const angularNonBindableCache = new WeakMap();
const hasOrInNgNonBindableInternal = (node) =>
Boolean(node) &&
getOrInsertComputed(
angularNonBindableCache,
node,
(node) =>
isElementWithNgNonBindable(node) ||
hasOrInNgNonBindableInternal(node.parent),
);
const hasOrInNgNonBindable = (node, options) =>
options.parser === "angular" && hasOrInNgNonBindableInternal(node);
export { hasOrInNgNonBindable };
@@ -78,24 +78,38 @@ parsers: ["angular"]
<pre ngNonBindable>{{foo +bar}}</pre>
<div ngNonBindable> {{ foo + bar }} </div>
<div ngNonBindable> [[ foo + bar ]] </div>
<span ngNonBindable> {{ foo + bar }} </span>
<span ngNonBindable> [[ foo + bar ]] </span>
<pre ngNonBindable> {{ foo + bar }} </pre>
<pre ngNonBindable> [[ foo + bar ]] </pre>
=====================================output=====================================
<span ngNonBindable>{{foo +bar}}</span>
<span ngNonBindable>{{foo +bar}}</span>
<div ngNonBindable>
{{foo +bar}}
<span>{{baz +qux}}</span>
{{foo +bar}}
<span>{{baz +qux}}</span>
</div>
<span>{{ foo + bar }}</span>
<span ngNonBindable>
@if (A) {
<span>{{foo +bar}}</span>
<span>{{foo +bar}}</span>
}
</span>
<pre ngNonBindable>{{foo +bar}}</pre>
<div ngNonBindable>{{ foo + bar }}</div>
<div ngNonBindable>[[ foo + bar ]]</div>
<span ngNonBindable> {{ foo + bar }} </span>
<span ngNonBindable> [[ foo + bar ]] </span>
<pre ngNonBindable> {{ foo + bar }} </pre>
<pre ngNonBindable> [[ foo + bar ]] </pre>
================================================================================
`;
@@ -7,9 +7,15 @@ parsers: ["angular"]
printWidth: 80 (default) |
=====================================input======================================
<span ngNonBindable>{{foo +bar}}</span>
<pre ngNonBindable>{{foo +bar}}</pre>
<span ngNonBindable>{{foo
.bar(
)}}</span>
=====================================output=====================================
<span ngNonBindable>{{foo +bar}}</span>
<span ngNonBindable>{{foo +bar}}</span>
<pre ngNonBindable>{{foo +bar}}</pre>
<span ngNonBindable>{{foo .bar( )}}</span>
================================================================================
`;
@@ -1 +1,5 @@
<span ngNonBindable>{{foo +bar}}</span>
<pre ngNonBindable>{{foo +bar}}</pre>
<span ngNonBindable>{{foo
.bar(
)}}</span>
@@ -14,3 +14,10 @@
</span>
<pre ngNonBindable>{{foo +bar}}</pre>
<div ngNonBindable> {{ foo + bar }} </div>
<div ngNonBindable> [[ foo + bar ]] </div>
<span ngNonBindable> {{ foo + bar }} </span>
<span ngNonBindable> [[ foo + bar ]] </span>
<pre ngNonBindable> {{ foo + bar }} </pre>
<pre ngNonBindable> [[ foo + bar ]] </pre>