-
Notifications
You must be signed in to change notification settings - Fork 4.7k
stripHTML - preserve leading and trailing spaces and strip script and on* attributes #35539
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
Merged
ellatrix
merged 6 commits into
trunk
from
try/strip-html-preserve-leading-trailing-spaces
Oct 22, 2021
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c87dae7
DOM: stripHTML: preserve leading and trailing space
ellatrix 76ca4f9
Add tests from #35457
ellatrix 92c34be
Fix typo
talldan 3245da1
Tests to cover stripping scripts and on attributes
getdave 55d9756
Use safeHTML to strip scripts and on attributes
getdave dca5a3b
Remove obsolette tests
getdave 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,64 @@ | ||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import stripHTML from '../strip-html'; | ||
|
|
||
| describe( 'stripHTML', () => { | ||
| it( 'should strip valid HTML, scripts and on attributes', () => { | ||
| const input = `<strong onClick="alert('and on attributes')">Here is some text</strong> that contains <em>HTML markup</em><script>alert("and scripts")</script>.`; | ||
| const output = 'Here is some text that contains HTML markup.'; | ||
| expect( stripHTML( input ) ).toBe( output ); | ||
| } ); | ||
|
|
||
| it( 'should strip invalid HTML, scripts and on attributes', () => { | ||
| const input = `<strong onClick="alert('and on attributes')">Here is some text</em> <p></div>that contains HTML markup</p><script>alert("and scripts")</script>.`; | ||
| const output = 'Here is some text that contains HTML markup.'; | ||
| expect( stripHTML( input ) ).toBe( output ); | ||
| } ); | ||
|
|
||
| describe( 'whitespace preservation', () => { | ||
| it( 'should preserve leading spaces', () => { | ||
| const input = | ||
| ' <strong>Here is some text</strong> with <em>leading spaces</em>.'; | ||
| const output = ' Here is some text with leading spaces.'; | ||
| expect( stripHTML( input ) ).toBe( output ); | ||
| } ); | ||
|
|
||
| it( 'should preserve leading spaces with HTML', () => { | ||
| const input = | ||
| '<strong> Here is some text</strong> with <em>leading spaces</em>.'; | ||
| const output = ' Here is some text with leading spaces.'; | ||
| expect( stripHTML( input ) ).toBe( output ); | ||
| } ); | ||
|
|
||
| it( 'should preserve trailing spaces with HTML', () => { | ||
| const input = | ||
| '<strong>Here is some text</strong> with <em>trailing spaces</em>. '; | ||
| const output = 'Here is some text with trailing spaces. '; | ||
| expect( stripHTML( input ) ).toBe( output ); | ||
| } ); | ||
|
|
||
| it( 'should preserve consecutive spaces within string', () => { | ||
| const input = | ||
| '<strong>Here is some text</strong> with <em>a lot of spaces inside</em>.'; | ||
| const output = | ||
| 'Here is some text with a lot of spaces inside.'; | ||
| expect( stripHTML( input ) ).toBe( output ); | ||
| } ); | ||
|
|
||
| it( 'should preserve new lines in multi-line HTML string', () => { | ||
| const input = `<div> | ||
| Here is some | ||
| <em>text</em> | ||
| with new lines | ||
| </div>`; | ||
|
|
||
| const output = ` | ||
| Here is some | ||
| text | ||
| with new lines | ||
| `; | ||
| expect( stripHTML( input ) ).toBe( output ); | ||
| } ); | ||
| } ); | ||
| } ); |
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
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.