Skip to content

Fix: Don't process v-html injected content as Vue template expressions - #52

Merged
mahmoud-abdelsattar-wmde merged 1 commit into
masterfrom
hotfix/v-html-content-reprocessed-as-template-expressions
Mar 2, 2026
Merged

mahmoud-abdelsattar-wmde merged 1 commit into
masterfrom
hotfix/v-html-content-reprocessed-as-template-expressions

Conversation

@mahmoud-abdelsattar-wmde

Copy link
Copy Markdown
Contributor

Problem
When a snak value contained text like {{P}}, the server-side renderer crashed with:
RuntimeException: Undefined variable 'P'

This happened because after handleRawHtml injected HTML via appendHTML, the child traversal in handleNode would recurse into the newly-injected nodes and call replaceMustacheVariables on them .. treating user content like {{P}} as a Vue template expression.

HTML-entity-encoding the curly braces ({) was not a viable workaround because HtmlParser uses DOMDocument::loadHTML which decodes entities back to { before creating text nodes.

Fix
In handleNode, take a snapshot of the node's children before handleRawHtml runs, then use that snapshot in the foreach traversal instead of the node's current children.

This means only the original template children are walked and processed for Vue directives. Any HTML injected by v-html is appended to the DOM but never passed to handleNode .. matching real Vue.js behaviour, where v-html content is treated as raw HTML and is never compiled as template expressions.

Bug: T418104

@lucaswerkmeister lucaswerkmeister left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be some tests for this change.

Comment thread src/Component.php Resolved
@mahmoud-abdelsattar-wmde
mahmoud-abdelsattar-wmde force-pushed the hotfix/v-html-content-reprocessed-as-template-expressions branch from a80e4ea to 401324e Compare March 2, 2026 14:24

@lucaswerkmeister lucaswerkmeister left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(There should still be some tests for this change.)

@mahmoud-abdelsattar-wmde
mahmoud-abdelsattar-wmde force-pushed the hotfix/v-html-content-reprocessed-as-template-expressions branch from 401324e to 64884d0 Compare March 2, 2026 15:23
@mahmoud-abdelsattar-wmde

Copy link
Copy Markdown
Contributor Author

Ok, I added two test cases for the {{P}} value, as text string and HTML string.

@lucaswerkmeister lucaswerkmeister left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, one optional suggestion. Haven’t tried it out locally yet.

Comment thread tests/php/TemplatingTest.php Resolved
@mahmoud-abdelsattar-wmde
mahmoud-abdelsattar-wmde force-pushed the hotfix/v-html-content-reprocessed-as-template-expressions branch from 64884d0 to 04b55c8 Compare March 2, 2026 16:00

@lucaswerkmeister lucaswerkmeister left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, one comment for potential follow-up but in the meantime I think this is okay to merge.

Comment thread src/Component.php
@@ -59,7 +60,7 @@ private function handleNode( DOMNode $node, array $data ) {
$this->handleAttributeBinding( $node, $data );
$this->handleConditionalNodes( $node->childNodes, $data );

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this part should also be changed… what if the v-html returns markup with v-if conditions inside?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please give an example so I can also study and fix a solution as a follow-up PR?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t have an example yet, I haven’t checked this at all. I’m just wondering if there’s a bug here.

But I guess it would be something like: template <div v-html="html"></div> and [ 'html' => '<span v-if="false">content</span> ] should evaluate to <div><span v-if="false">content</span></div> and not <div></div>.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @lucaswerkmeister , coming back to this possible bug issue, in order to evaluate it, I created a simple test locally:

'v-if in v-html content is not evaluated' => [
	'<div v-html="html"></div>',
	[ 'html' => '<span v-if="false">content</span>' ],
	'<div><span v-if="false">content</span></div>',
],

And it failed:
image
That looks like a bug to me.

@mahmoud-abdelsattar-wmde
mahmoud-abdelsattar-wmde merged commit 5dd7fec into master Mar 2, 2026
8 checks passed
@lucaswerkmeister
lucaswerkmeister deleted the hotfix/v-html-content-reprocessed-as-template-expressions branch March 2, 2026 16:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Development

Successfully merging this pull request may close these issues.

2 participants