Skip to content

Commit

Permalink
Escape HTML: Always Escape Ampersand
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Oct 17, 2019
1 parent d7cd263 commit 70dfa53
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 28 deletions.
25 changes: 0 additions & 25 deletions packages/block-library/src/code/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { flow } from 'lodash';
*/
export function escape( content ) {
return flow(
escapeAmpersands,
escapeOpeningSquareBrackets,
escapeProtocolInIsolatedUrls
)( content || '' );
Expand All @@ -27,33 +26,9 @@ export function unescape( content ) {
return flow(
unescapeProtocolInIsolatedUrls,
unescapeOpeningSquareBrackets,
unescapeAmpersands
)( content || '' );
}

/**
* Returns the given content with all its ampersand characters converted
* into their HTML entity counterpart (i.e. & => &)
*
* @param {string} content The content of a code block.
* @return {string} The given content with its ampersands converted into
* their HTML entity counterpart (i.e. & => &)
*/
function escapeAmpersands( content ) {
return content.replace( /&/g, '&' );
}

/**
* Returns the given content with all & HTML entities converted into &.
*
* @param {string} content The content of a code block.
* @return {string} The given content with all & HTML entities
* converted into &.
*/
function unescapeAmpersands( content ) {
return content.replace( /&/g, '&' );
}

/**
* Returns the given content with all opening shortcode characters converted
* into their HTML entity counterpart (i.e. [ => [). For instance, a
Expand Down
2 changes: 1 addition & 1 deletion packages/element/src/test/serialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ describe( 'renderElement()', () => {
it( 'renders escaped string element', () => {
const result = renderElement( 'hello & world &amp; friends <img/>' );

expect( result ).toBe( 'hello &amp; world &amp; friends &lt;img/>' );
expect( result ).toBe( 'hello &amp; world &amp;amp; friends &lt;img/>' );
} );

it( 'renders numeric element as string', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/escape-html/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const REGEXP_INVALID_ATTRIBUTE_NAME = /[\u007F-\u009F "'>/="\uFDD0-\uFDEF]/;
* @return {string} Escaped string.
*/
export function escapeAmpersand( value ) {
return value.replace( /&(?!([a-z0-9]+|#[0-9]+|#x[a-f0-9]+);)/gi, '&amp;' );
return value.replace( /&/gi, '&amp;' );
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/escape-html/src/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function testEscapeAmpersand( implementation ) {
it( 'should escape ampersand', () => {
const result = implementation( 'foo & bar &amp; &AMP; baz &#931; &#bad; &#x3A3; &#X3a3; &#xevil;' );

expect( result ).toBe( 'foo &amp; bar &amp; &AMP; baz &#931; &amp;#bad; &#x3A3; &#X3a3; &amp;#xevil;' );
expect( result ).toBe( 'foo &amp; bar &amp;amp; &amp;AMP; baz &amp;#931; &amp;#bad; &amp;#x3A3; &amp;#X3a3; &amp;#xevil;' );
} );
}

Expand Down

0 comments on commit 70dfa53

Please sign in to comment.