Skip to content

Conversation

@obenland
Copy link
Member

@obenland obenland commented Dec 5, 2025

Fixes #2592

Proposed changes:

  • Add Sanitize::strip_whitespace() method that strips newlines, carriage returns, and tabs from content while preserving them inside <pre> elements.
  • Update Shortcodes::content() and Post::get_content() to use the new sanitization method.

Code blocks in federated posts were losing their newline characters, making them unreadable. The fix uses a regex with preg_replace_callback to preserve whitespace inside <pre> tags while stripping it from the rest of the content.

Other information:

  • Have you written new tests for your changes, if applicable?

Testing instructions:

  1. Create a post with a code block containing multiple lines
  2. Publish the post
  3. View the federated content on a remote instance (or inspect the ActivityPub JSON at /wp-json/activitypub/1.0/posts/{id})
  4. Verify the code block preserves newlines and formatting

Changelog entry

  • Automatically create a changelog entry from the details below.
Changelog Entry Details

Significance

  • Patch
  • Minor
  • Major

Type

  • Added - for new features
  • Changed - for changes in existing functionality
  • Deprecated - for soon-to-be removed features
  • Removed - for now removed features
  • Fixed - for any bug fixes
  • Security - in case of vulnerabilities

Message

Preserve newlines inside pre elements when federating content.

Copilot AI review requested due to automatic review settings December 5, 2025 17:55
@obenland obenland self-assigned this Dec 5, 2025
@obenland obenland requested a review from a team December 5, 2025 17:55
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes whitespace handling in federated content by preserving formatting inside <pre> elements while stripping unnecessary whitespace elsewhere. Previously, code blocks were losing their newlines during federation, making them unreadable.

Key Changes:

  • Added Sanitize::strip_whitespace() method to handle selective whitespace stripping
  • Updated content processing in Post::get_content() and Shortcodes::content() to use the new method
  • Added comprehensive test coverage for both isolated code blocks and mixed content scenarios

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
includes/class-sanitize.php Introduces the new strip_whitespace() method that uses regex callback to preserve whitespace in <pre> tags
includes/transformer/class-post.php Replaces direct regex whitespace stripping with the new sanitization method
includes/class-shortcodes.php Updates shortcode content processing to use the new sanitization method
tests/phpunit/tests/includes/transformer/class-test-post.php Adds two test cases validating whitespace preservation in code blocks
.github/changelog/2595-from-description Changelog entry documenting the fix

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Newlines should be preserved when federating code blocks

3 participants