-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Formatting: Add strip_html_newlines() function
#11450
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
himanshupathak95
wants to merge
11
commits into
WordPress:trunk
Choose a base branch
from
himanshupathak95:fix/5678
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a539746
Formatting: Add `strip_html_newlines()` function to preserve newlines…
himanshupathak95 d938814
Formatting: Add documentation for `strip_html_newlines()` function
himanshupathak95 58407b1
Tests: Add initial test class
himanshupathak95 b8a380f
Tests: Add documentation
himanshupathak95 46eb161
Tests: Verify if it strips newlines from the nodes
himanshupathak95 91ca8a4
Tests: Add documentation
himanshupathak95 2c6305c
Tests: Verify preservation of newlines in preformatted elements
himanshupathak95 9eeed37
Tests: Add documentation for newline preservation test
himanshupathak95 c53a2b7
Ensure strip_html_newlines only preserves tags that exactly match the…
himanshupathak95 504ab8b
Add unit test coverage for strip_html_newlines with non-preserved tags
himanshupathak95 b74bae5
Remove empty docblock comment from stripHtmlNewlines test case
himanshupathak95 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * Tests for strip_html_newlines(). | ||
| * | ||
| * @group formatting | ||
| * | ||
| * @covers ::strip_html_newlines | ||
| */ | ||
| class Tests_Formatting_StripHtmlNewlines extends WP_UnitTestCase { | ||
|
|
||
| /** | ||
| * Verifies that newlines and carriage returns in text nodes are replaced | ||
| * with spaces, including across inline elements like anchors. | ||
| * | ||
| * @ticket 5678 | ||
| */ | ||
| public function test_strips_newlines_from_text_nodes() { | ||
| $this->assertSame( '', strip_html_newlines( '' ), 'Empty string should be returned as-is.' ); | ||
| $this->assertSame( '<p>No newlines here.</p>', strip_html_newlines( '<p>No newlines here.</p>' ), 'Text without newlines should be returned as-is.' ); | ||
| $this->assertSame( '<p>Line one Line two Line three</p>', strip_html_newlines( "<p>Line one\n\nLine two\r\nLine three</p>" ), 'Multiple newlines and carriage returns should be collapsed to a single space.' ); | ||
| $this->assertSame( | ||
| '<p>This is a paragraph in which the wpautop() <a href="#elsewhere">wrapping will happen in the middle</a> of an anchor, which is an inline element.</p>', | ||
| strip_html_newlines( "<p>This is a paragraph in which the\nwpautop() <a href=\"#elsewhere\">wrapping will\nhappen in the middle</a> of an\nanchor, which is an inline element.</p>" ), | ||
| 'Newlines within and around inline elements should be stripped.' | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * @ticket 5678 | ||
| */ | ||
| public function test_preserves_newlines_in_preformatted_elements() { | ||
| $input = "<p>Normal\ntext</p>\n<pre>\nPreformatted\nlines\n</pre>\n<p>More\ntext</p>"; | ||
| $result = strip_html_newlines( $input ); | ||
|
|
||
| $this->assertStringContainsString( 'Normal text', $result, 'Newlines in normal text should be stripped.' ); | ||
| $this->assertStringContainsString( 'More text', $result, 'Newlines in trailing paragraph should be stripped.' ); | ||
| $this->assertStringContainsString( "\nPreformatted\nlines\n", $result, 'Newlines inside <pre> should be preserved.' ); | ||
|
|
||
| $preserved_cases = array( | ||
| 'code' => "<p>A\nB</p><code>x\ny</code>", | ||
| 'kbd' => "<p>A\nB</p><kbd>x\ny</kbd>", | ||
| 'script' => "<p>A\nB</p><script>x\ny</script>", | ||
| 'style' => "<p>A\nB</p><style>x\ny</style>", | ||
| ); | ||
|
|
||
| foreach ( $preserved_cases as $tag => $html ) { | ||
| $out = strip_html_newlines( $html ); | ||
| $this->assertStringContainsString( 'A B', $out, "Text node newline should be stripped around <{$tag}>." ); | ||
| $this->assertStringContainsString( "x\ny", $out, "Newline inside <{$tag}> should be preserved." ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @ticket 5678 | ||
| */ | ||
| public function test_does_not_preserve_newlines_in_tags_prefixed_with_preserved_name() { | ||
| $cases = array( | ||
| 'preload' => "<preload>some\ncontent</preload><p>normal\ntext</p>", | ||
| 'codeblock' => "<codeblock>x\ny</codeblock>", | ||
| 'keyboard' => "<keyboard>a\nb</keyboard>", | ||
| ); | ||
|
|
||
| foreach ( $cases as $tag => $html ) { | ||
| $out = strip_html_newlines( $html ); | ||
| $this->assertStringNotContainsString( "\n", $out, "Newlines inside <{$tag}> should be stripped (not a preserved element)." ); | ||
| } | ||
| } | ||
| } | ||
|
himanshupathak95 marked this conversation as resolved.
|
||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.