Skip to content

Commit 083c8d2

Browse files
authored
Fix make_clickable corrupting existing anchor tags (#2517)
1 parent 0b15563 commit 083c8d2

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: patch
2+
Type: fixed
3+
4+
Fix make_clickable corrupting existing anchor tags in ActivityPub content

includes/class-sanitize.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,11 @@ public static function webfinger( $value ) {
191191
* @return string The converted content.
192192
*/
193193
public static function content( $content ) {
194-
$content = \make_clickable( $content );
194+
// Only make URLs clickable if no anchor tags exist, to avoid corrupting existing links.
195+
if ( false === \strpos( $content, '<a ' ) ) {
196+
$content = \make_clickable( $content );
197+
}
198+
195199
$content = \wpautop( $content );
196200
$content = \wp_kses_post( $content );
197201

tests/phpunit/tests/includes/class-test-sanitize.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,20 @@ public function test_content_urls() {
231231
$this->assertStringContainsString( '<a href="https://example.com"', $result );
232232
}
233233

234+
/**
235+
* Test content sanitization preserves existing links with Mastodon-style spans.
236+
*
237+
* @covers ::content
238+
*/
239+
public function test_content_preserves_existing_links() {
240+
$content = '<p><a href="https://www.example.com/path/to/article?param=value&amp;utm_source=mastodon" target="_blank" rel="nofollow noopener" translate="no"><span class="invisible">https://www.</span><span class="ellipsis">example.com/path/to/art</span><span class="invisible">icle?param=value&amp;utm_source=mastodon</span></a></p>';
241+
$result = Sanitize::content( $content );
242+
243+
// Should preserve existing link structure without double-linking.
244+
$this->assertSame( 1, \substr_count( $result, '<a ' ), 'Should have exactly one anchor tag' );
245+
$this->assertStringContainsString( 'href="https://www.example.com/path/', $result );
246+
}
247+
234248
/**
235249
* Test content sanitization with empty content.
236250
*

0 commit comments

Comments
 (0)