-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't re-encode already encoded post titles (#2407)
* Added an entities module with reusable 'decodeEntities'
- Loading branch information
1 parent
5125ee1
commit f08c5f4
Showing
4 changed files
with
48 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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,22 @@ | ||
let _decodeTextArea; | ||
|
||
export function decodeEntities( html ) { | ||
// not a string, or no entities to decode | ||
if ( 'string' !== typeof html || -1 === html.indexOf( '&' ) ) { | ||
return html; | ||
} | ||
|
||
// create a textarea for decoding entities, that we can reuse | ||
if ( undefined === _decodeTextArea ) { | ||
if ( document.implementation && document.implementation.createHTMLDocument ) { | ||
_decodeTextArea = document.implementation.createHTMLDocument( '' ).createElement( 'textarea' ); | ||
} else { | ||
_decodeTextArea = document.createElement( 'textarea' ); | ||
} | ||
} | ||
|
||
_decodeTextArea.innerHTML = html; | ||
const decoded = _decodeTextArea.textContent; | ||
_decodeTextArea.innerHTML = ''; | ||
return decoded; | ||
} |
This file contains 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 |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import * as keycodes from './keycodes'; | ||
import * as nodetypes from './nodetypes'; | ||
import { decodeEntities } from './entities'; | ||
|
||
export { keycodes }; | ||
export { nodetypes }; | ||
export { decodeEntities }; | ||
|
||
export * from './mediaupload'; |
This file contains 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,22 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { decodeEntities } from '../entities'; | ||
|
||
describe( 'decodeEntities', () => { | ||
it( 'should not change html with no entities', () => { | ||
const html = '<h1>A noble tag embiggens the smallest text.</h1>'; | ||
const expected = '<h1>A noble tag embiggens the smallest text.</h1>'; | ||
expect( decodeEntities( html ) ).toEqual( expected ); | ||
} ); | ||
it( 'should decode entities', () => { | ||
const html = '<h1>This post’s title.</h1>'; | ||
const expected = '<h1>This post’s title.</h1>'; | ||
expect( decodeEntities( html ) ).toEqual( expected ); | ||
} ); | ||
it( 'should not double decode entities', () => { | ||
const html = 'This post&rsquo;s title.'; | ||
const expected = 'This post’s title.'; | ||
expect( decodeEntities( html ) ).toEqual( expected ); | ||
} ); | ||
} ); |