Keep Markup: preserve empty last-child markup - #4121
Open
dyk1454683243-sudo wants to merge 1 commit into
Open
dyk1454683243-sudo wants to merge 1 commit into
dyk1454683243-sudo wants to merge 1 commit into
Conversation
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>
✅ Deploy Preview for dev-prismjs-com ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
This was referenced Sep 20, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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-highlightrecordsposOpen/posClosefrom text offsets. An empty last child hasposOpen === posCloseat the end of the text.after-highlightwalks highlighted text looking forpos + 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-widthRange#insertNodeat 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
<span></span>,<span class="keep"></span>, and<a></a><div class="keep"></div>built with DOM APIs so the emptydivis actually the last child of<code>