|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Tests that inline note markers are unwrapped in rendered block output via the |
| 5 | + * render_block filter, while raw post content is left untouched. |
| 6 | + * |
| 7 | + * The `<mark class="wp-note">` wrapper is removed entirely - both the open tag |
| 8 | + * and its matching closer - so no note marker or metadata reaches the public |
| 9 | + * HTML, while the marked text (and any nested formatting) is preserved. |
| 10 | + * |
| 11 | + * @group comment |
| 12 | + * @group notes |
| 13 | + * |
| 14 | + * @covers ::wp_strip_inline_note_markers |
| 15 | + */ |
| 16 | +class Tests_Comment_StripInlineNoteMarkers extends WP_UnitTestCase { |
| 17 | + |
| 18 | + /** |
| 19 | + * @ticket 65482 |
| 20 | + */ |
| 21 | + public function test_strip_unwraps_marker_from_mark(): void { |
| 22 | + $html = '<p>Hello <mark class="wp-note" data-id="7">marked</mark> world</p>'; |
| 23 | + $stripped = wp_strip_inline_note_markers( $html ); |
| 24 | + |
| 25 | + $this->assertSame( '<p>Hello marked world</p>', $stripped ); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * @ticket 65482 |
| 30 | + */ |
| 31 | + public function test_strip_handles_multiple_markers_in_one_block(): void { |
| 32 | + $html = '<p><mark class="wp-note" data-id="1">a</mark> and <mark class="wp-note" data-id="2">b</mark></p>'; |
| 33 | + $stripped = wp_strip_inline_note_markers( $html ); |
| 34 | + |
| 35 | + $this->assertSame( '<p>a and b</p>', $stripped ); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @ticket 65482 |
| 40 | + */ |
| 41 | + public function test_strip_passes_through_block_content_without_markers(): void { |
| 42 | + $html = '<p>Plain text with no notes here.</p>'; |
| 43 | + $stripped = wp_strip_inline_note_markers( $html ); |
| 44 | + |
| 45 | + $this->assertSame( $html, $stripped ); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @ticket 65482 |
| 50 | + */ |
| 51 | + public function test_strip_keeps_other_classes_when_removing_wp_note(): void { |
| 52 | + // The whole wrapper is removed, so any companion classes go with it. |
| 53 | + $html = '<p><mark class="custom wp-note other" data-id="3">x</mark></p>'; |
| 54 | + $stripped = wp_strip_inline_note_markers( $html ); |
| 55 | + |
| 56 | + $this->assertSame( '<p>x</p>', $stripped ); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @ticket 65482 |
| 61 | + */ |
| 62 | + public function test_strip_leaves_unrelated_marks_untouched(): void { |
| 63 | + // A user highlight (`core/text-color`) serializes as a plain `<mark>` and |
| 64 | + // must survive untouched. |
| 65 | + $html = '<p><mark style="background-color:#ff0">keep me</mark></p>'; |
| 66 | + $stripped = wp_strip_inline_note_markers( $html ); |
| 67 | + |
| 68 | + $this->assertSame( $html, $stripped ); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * @ticket 65482 |
| 73 | + */ |
| 74 | + public function test_strip_does_not_match_partial_class_names(): void { |
| 75 | + // `wp-note-foo` is a different class and must not be treated as a marker; |
| 76 | + // a regex word boundary would incorrectly match it. |
| 77 | + $html = '<p><mark class="wp-note-foo">keep me</mark></p>'; |
| 78 | + $stripped = wp_strip_inline_note_markers( $html ); |
| 79 | + |
| 80 | + $this->assertSame( $html, $stripped ); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @ticket 65482 |
| 85 | + */ |
| 86 | + public function test_strip_preserves_user_mark_attributes_next_to_note(): void { |
| 87 | + // A user/plugin `<mark>` with several attributes sitting beside a note |
| 88 | + // marker must be returned byte-for-byte; only the `wp-note` wrapper goes. |
| 89 | + $html = '<p><mark class="highlight" style="background-color:#ff0" data-id="99" title="kept">user</mark> and <mark class="wp-note" data-id="4">noted</mark></p>'; |
| 90 | + $stripped = wp_strip_inline_note_markers( $html ); |
| 91 | + |
| 92 | + $this->assertSame( '<p><mark class="highlight" style="background-color:#ff0" data-id="99" title="kept">user</mark> and noted</p>', $stripped ); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * @ticket 65482 |
| 97 | + */ |
| 98 | + public function test_strip_preserves_nested_formatting(): void { |
| 99 | + // A note wrapping already-formatted text (e.g. coloured text) serializes |
| 100 | + // with nested inline elements. The wrapper is removed while the inner |
| 101 | + // markup is preserved intact. |
| 102 | + $html = '<p><mark class="wp-note" data-id="1">a <span style="color:red">red</span> b</mark></p>'; |
| 103 | + $stripped = wp_strip_inline_note_markers( $html ); |
| 104 | + |
| 105 | + $this->assertSame( '<p>a <span style="color:red">red</span> b</p>', $stripped ); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * @ticket 65482 |
| 110 | + */ |
| 111 | + public function test_strip_unwraps_note_but_keeps_inner_highlight_mark(): void { |
| 112 | + // A note wrapping a user highlight nests `<mark>` inside `<mark>`. Only the |
| 113 | + // note wrapper is removed; the inner highlight `<mark>` is preserved, and |
| 114 | + // the closer pairing must not unbalance. |
| 115 | + $html = '<p><mark class="wp-note" data-id="1">a <mark style="background-color:#ff0">hi</mark> b</mark></p>'; |
| 116 | + $stripped = wp_strip_inline_note_markers( $html ); |
| 117 | + |
| 118 | + $this->assertSame( '<p>a <mark style="background-color:#ff0">hi</mark> b</p>', $stripped ); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * @ticket 65482 |
| 123 | + */ |
| 124 | + public function test_strip_handles_overlapping_nested_note_markers(): void { |
| 125 | + // Two notes anchored on overlapping text serialize as nested `<mark>`s. |
| 126 | + // Both wrappers are removed and the text survives. |
| 127 | + $html = '<p><mark class="wp-note" data-id="1">a<mark class="wp-note" data-id="2">b</mark>c</mark></p>'; |
| 128 | + $stripped = wp_strip_inline_note_markers( $html ); |
| 129 | + |
| 130 | + $this->assertSame( '<p>abc</p>', $stripped ); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * @ticket 65482 |
| 135 | + */ |
| 136 | + public function test_strip_ignores_mark_like_text_inside_a_comment(): void { |
| 137 | + // A `</mark>` sequence inside an HTML comment is text, not a tag. Walking |
| 138 | + // the parsed token stream ignores it; a raw regex over the string would |
| 139 | + // mistake it for the note's closer, unbalance the pairing, and corrupt |
| 140 | + // both the comment and the real wrapper. |
| 141 | + $html = '<p><mark class="wp-note" data-id="1">a<!-- </mark> -->b</mark>tail</p>'; |
| 142 | + $stripped = wp_strip_inline_note_markers( $html ); |
| 143 | + |
| 144 | + $this->assertSame( '<p>a<!-- </mark> -->btail</p>', $stripped ); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * A note marker left unclosed (e.g. by a hand edit in the code editor) still |
| 149 | + * has its opener stripped, so no `wp-note` metadata leaks to the front end. |
| 150 | + * |
| 151 | + * @ticket 65482 |
| 152 | + */ |
| 153 | + public function test_strip_unwraps_unclosed_note_marker(): void { |
| 154 | + $html = '<p><mark class="wp-note" data-id="1">a'; |
| 155 | + $stripped = wp_strip_inline_note_markers( $html ); |
| 156 | + |
| 157 | + $this->assertSame( '<p>a', $stripped ); |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * A stray `</mark>` closer with no matching opener is not a note marker, so it |
| 162 | + * is left exactly as it was rather than corrupting the surrounding markup. |
| 163 | + * |
| 164 | + * @ticket 65482 |
| 165 | + */ |
| 166 | + public function test_strip_leaves_stray_mark_closer_untouched(): void { |
| 167 | + $html = '<p>a</mark>b</p>'; |
| 168 | + $stripped = wp_strip_inline_note_markers( $html ); |
| 169 | + |
| 170 | + $this->assertSame( $html, $stripped ); |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * Note markers can be crossed with other inline formatting in hand-edited or |
| 175 | + * otherwise ill-formed content. The full HTML tree builder would abort on this |
| 176 | + * nesting and leave the marker (and its metadata) in place; scanning tokens |
| 177 | + * strips the `wp-note` marker regardless and keeps the rest of the markup. |
| 178 | + * |
| 179 | + * @ticket 65482 |
| 180 | + */ |
| 181 | + public function test_strip_unwraps_note_marker_with_improper_nesting(): void { |
| 182 | + $html = '<p><mark class="wp-note" data-id="1">a<i>b</mark>c</i></p>'; |
| 183 | + $stripped = wp_strip_inline_note_markers( $html ); |
| 184 | + |
| 185 | + $this->assertSame( '<p>a<i>bc</i></p>', $stripped ); |
| 186 | + } |
| 187 | + |
| 188 | + /** |
| 189 | + * @ticket 65482 |
| 190 | + */ |
| 191 | + public function test_strip_filter_is_registered_on_render_block(): void { |
| 192 | + // Guards against future hook rewiring that would silently leave |
| 193 | + // inline-note markers in rendered output. |
| 194 | + $this->assertNotFalse( |
| 195 | + has_filter( 'render_block', 'wp_strip_inline_note_markers' ) |
| 196 | + ); |
| 197 | + } |
| 198 | +} |
0 commit comments