Skip to content

Keep Markup: preserve empty last-child markup - #4121

Open
dyk1454683243-sudo wants to merge 1 commit into
PrismJS:v2from
dyk1454683243-sudo:cursor/fix-keep-markup-last-empty-1618-8695
Open

dyk1454683243-sudo wants to merge 1 commit into
PrismJS:v2from
dyk1454683243-sudo:cursor/fix-keep-markup-last-empty-1618-8695

Conversation

@dyk1454683243-sudo

Copy link
Copy Markdown

Fixes #1618

Keep Markup dropped empty markup when it was the last child of the highlighted code element. The same empty element was kept when it was not last.

Cause

before-highlight records posOpen/posClose from text offsets. An empty last child has posOpen === posClose at the end of the text. after-highlight walks highlighted text looking for pos + length > posOpen, so a text node that ends exactly at that offset never matches, and there is no later text to match. The original element is never reinserted.

Widening the start predicate to >= (as in closed #1622) finds that text, but a zero-width Range#insertNode at the end of the node lands before following siblings. That reverses adjacent empty tags and pulls them into a preceding kept element.

Fix

If the walk does not find a start for an empty range (posOpen === posClose), append the original element to the code element. Mid-stream empty markup, contentful last markup, order, double-highlight, and no-clone behavior are unchanged.

Tests

Fixes PrismJS#1618

Empty keep-markup elements at the end of the highlighted code have
posOpen === posClose at the last text offset. The after-highlight walk
never selects that zero-width range, so the original element is dropped.

Reinsert those empty nodes as the last child of the code element instead
of widening the start predicate to >=, which reverses adjacent empty
tags and pulls them into a preceding kept element.

Co-authored-by: David <dyk1454683243-sudo@users.noreply.github.com>
@netlify

netlify Bot commented Sep 20, 2026

Copy link
Copy Markdown

Deploy Preview for dev-prismjs-com ready!

Name Link
🔨 Latest commit c06b038
🔍 Latest deploy log https://app.netlify.com/projects/dev-prismjs-com/deploys/6aaf59a9483017000809e9ea
😎 Deploy Preview https://deploy-preview-4121--dev-prismjs-com.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

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.

Keep Markup: the markup is removed if it's the last element

2 participants