Skip to content

Remove block markers as nodes so <style>/raw-text content isn't corrupted - #1357

Open
jeremy wants to merge 1 commit into
mainfrom
fix/block-marker-serialization
Open

Remove block markers as nodes so <style>/raw-text content isn't corrupted#1357
jeremy wants to merge 1 commit into
mainfrom
fix/block-marker-serialization

Conversation

@jeremy

@jeremy jeremy commented Sep 10, 2026

Copy link
Copy Markdown
Member

Problem

serializeToContentType(..., "text/html") strips Trix's <!--block--> boundary markers with a context-free global string replace over the already-serialized HTML:

return element.innerHTML.replace(blockCommentPattern, "")

That replace can't distinguish Trix's own comment nodes (created in BlockView via document.createComment("block")) from the identical byte sequence <!--block--> sitting in a text node. So it corrupts content that merely happens to contain those bytes.

The clearest case is a raw-text element like <style>, whose contents are text, not parsed markup:

  • <style>x<!--block-->y</style> serializes to <style>xy</style> — the inner text is silently mangled.
  • The bytes can also be planted so that removing the marker fuses adjacent text into real markup, e.g. </st<!--block-->yle> (one inert text node) collapses into a real </style> end tag that wasn't in the sanitized DOM.

Fix

Remove the markers structurally, as comment nodes, instead of as text. Walk the serializable element for comment nodes and remove exactly Trix's own block markers:

const walker = document.createTreeWalker(element, NodeFilter.SHOW_COMMENT)
const markers = []
while (walker.nextNode()) {
  if (walker.currentNode.data === "block") markers.push(walker.currentNode)
}
markers.forEach((node) => node.remove())
return element.innerHTML

element here is always the serializer's own working copy — a cloneNode(true) of the input element, or a freshly DocumentView.rendered tree — never the live editor DOM, so mutating it in place is safe. The now-unused blockCommentPattern constant is removed.

Tests

Added regression coverage in serialization_test.js:

  • A literal <!--block--> byte sequence in a <style> text node round-trips verbatim (it's not a comment node, so it must be preserved).
  • The </st<!--block-->yle> shape does not splice into a real </style> end tag — the serialized value re-parses to the same single <style> element with intact text.
  • Real <!--block--> comment nodes are still removed (existing behavior), and non-block comment nodes are left alone.

The two corruption tests fail against the old string-replace implementation and pass with this change. Full suite green locally; the vendored Action Text bundle is regenerated via yarn build.

Serialization stripped Trix's <!--block--> boundary markers with a
context-free string replace over the already-serialized HTML. That
replace can't tell Trix's own comment nodes from the identical byte
sequence living in a text node, so it silently mangles benign content:
<style>x<!--block-->y</style> serializes as <style>xy</style>. Inside a
raw-text element it can also fuse otherwise-inert text into real markup.

Walk the serializable element for comment nodes and remove only Trix's
own 'block' markers, leaving text content untouched.
Copilot AI balanced review requested due to automatic review settings September 10, 2026 17:43
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 10, 2026

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review Completed 2026-09-10T17:46:28.783827Z ca31e08 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approved

The structural fix is correct, safely scoped to working copies, and adequately covered by regression tests.

Pull request overview

Replaces unsafe serialized-string marker removal with structural comment-node removal, preserving raw-text content.

Changes:

  • Removes only block comment nodes via TreeWalker.
  • Adds regression tests for marker removal and raw-text preservation.
  • Regenerates the Action Text bundle.

[!TIP]
If you aren't ready for review, convert to a draft PR.
Click "Convert to draft" or run gh pr ready --undo.
Click "Ready for review" or run gh pr ready to reengage.

File summaries
File Description
src/trix/core/serialization.js Structurally removes block markers.
src/test/unit/serialization_test.js Covers comments and raw-text edge cases.
action_text-trix/app/assets/javascripts/trix.js Updates the vendored bundle.
Review details
  • Files reviewed: 3/3 changed files
  • Comments generated: 0
  • Review effort level: Balanced

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants