Editor: Strip inline note markers from rendered block output#12215
Editor: Strip inline note markers from rendered block output#12215adamsilverstein wants to merge 2 commits into
Conversation
Inline notes anchor to a text selection within a block using an in-content `<mark class="wp-note" data-id="N">` marker so the anchor survives edits. The marker is kept in raw `post_content` (and the REST `raw` view, revisions, and exports) so the editor can re-attach on reload, but it must not reach public HTML. Add `wp_strip_inline_note_markers()`, hooked on `render_block`, which unwraps each note `<mark>` and its matching closer while preserving the marked text and any nested formatting. Matching goes through the HTML API rather than a regex so only the exact `wp-note` class token is targeted: an unrelated user or plugin `<mark>` (a `core/text-color` highlight, a `wp-note-foo` class) survives byte-for-byte, and `</mark>`-looking text inside a comment or attribute value is never mistaken for a real tag. A nesting stack pairs each note opener with its own closer so overlapping notes and nested highlights resolve correctly. Backports the server-side piece of Gutenberg PR 78218.
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
|
Hi there! 👋 Thank you for your contribution to WordPress! 💖 It looks like this is your first pull request to No one monitors this repository for new pull requests. Pull requests must be attached to a Trac ticket to be considered for inclusion in WordPress Core. To attach a pull request to a Trac ticket, please include the ticket's full URL in your pull request description. Pull requests are never merged on GitHub. The WordPress codebase continues to be managed through the SVN repository that this GitHub repository mirrors. Please feel free to open pull requests to work on any contribution you are making. More information about how GitHub pull requests can be used to contribute to WordPress can be found in the Core Handbook. Please include automated tests. Including tests in your pull request is one way to help your patch be considered faster. To learn about WordPress' test suites, visit the Automated Testing page in the handbook. If you have not had a chance, please review the Contribute with Code page in the WordPress Core Handbook. The Developer Hub also documents the various coding standards that are followed:
Thank you, |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
…7.1.0. Inline notes are a 7.1 feature (trunk is 7.1-alpha and the Gutenberg companion lives under lib/compat/wordpress-7.1/), and this function is new, so the docblock should reference 7.1.0 rather than 6.9.0.
| */ | ||
| class Tests_Comment_StripInlineNoteMarkers extends WP_UnitTestCase { | ||
|
|
||
| public function test_strip_unwraps_marker_from_mark() { |
There was a problem hiding this comment.
Can you add @ticket annotation for all new tests?
| if ( true === $is_note ) { | ||
| $processor->remove_token(); | ||
| } | ||
| } |
There was a problem hiding this comment.
this code assumes well-formed markup, which is fair for this task. the HTML Processor would guarantee that assumption, but would occasionally abort in certain cases of ill-formed markup.
everything else looks fine, though WPCS is going to complain about making multi-line comments without /* and */ — it prefers polluting diffs and making a big deal out of minor edits.
Summary
Backports the server-side piece of Gutenberg PR WordPress/gutenberg#78218, which adds inline (partial-text) Notes - notes anchored to a text selection within a block rather than to the whole block.
An inline note's anchor is an in-content
<mark class="wp-note" data-id="N">…</mark>marker. Thedata-ididentifies the note and the marker's position follows edits, so no separate selection metadata is stored. The marker stays in the rawpost_content(and the RESTrawview, revisions, and exports) so the editor can re-attach the note on reload, but the public front-end HTML should not expose note metadata.This change strips that marker from rendered output only.
What changed
src/wp-includes/comment.phpwp_strip_inline_note_markers()— unwraps each note<mark>and its matching closer onrender_block, keeping the marked text and any nested formatting.src/wp-includes/default-filters.phprender_block.tests/phpunit/tests/comment/stripInlineNoteMarkers.phpWhy the HTML API, not a regex
Matching goes through
WP_HTML_Tag_Processor::has_class()so only the exactwp-noteclass token is targeted:<mark>(acore/text-colorhighlight, or awp-note-fooclass) survives byte-for-byte with all attributes intact. A\bwp-note\bregex word boundary would wrongly matchwp-note-foo.</mark>-looking sequence inside an HTML comment or attribute value is never mistaken for a real tag.<mark>s resolve correctly.The HTML API has no public token-removal method yet, so an anonymous
WP_HTML_Tag_Processorsubclass unwraps the marker directly on the parsed token stream.Testing instructions
Run the new unit tests:
Or manually: add an inline note to a paragraph in the editor, view the post on the front end, and confirm the
<mark class="wp-note">wrapper is absent while the text remains, and that any non-note<mark>highlight is untouched.Trac ticket: https://core.trac.wordpress.org/ticket/65482