Skip to content

Commit

Permalink
Escape-html: Convert package to TypeScript (#62586)
Browse files Browse the repository at this point in the history
Co-authored-by: jpstevens <[email protected]>
Co-authored-by: sirreal <[email protected]>
Co-authored-by: up1512001 <[email protected]>
  • Loading branch information
4 people authored Jun 14, 2024
1 parent 33c2fb7 commit eb924d4
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 32 deletions.
4 changes: 4 additions & 0 deletions packages/escape-html/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Internal

- Refactor to TypeScript ([#62586](https://github.com/WordPress/gutenberg/pull/62586)).

## 3.0.0 (2024-05-31)

### Breaking Changes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
*
* See: https://core.trac.wordpress.org/ticket/45387
*
* @param {string} value Original string.
* @param value Original string.
*
* @return {string} Escaped string.
* @return Escaped string.
*/
export default function __unstableEscapeGreaterThan( value ) {
export default function __unstableEscapeGreaterThan( value: string ): string {
return value.replace( />/g, '&gt;' );
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ import __unstableEscapeGreaterThan from './escape-greater';
* and noncharacters."
*
* @see https://html.spec.whatwg.org/multipage/syntax.html#attributes-2
*
* @type {RegExp}
*/
const REGEXP_INVALID_ATTRIBUTE_NAME = /[\u007F-\u009F "'>/="\uFDD0-\uFDEF]/;
const REGEXP_INVALID_ATTRIBUTE_NAME: RegExp =
/[\u007F-\u009F "'>/="\uFDD0-\uFDEF]/;

/**
* Returns a string with ampersands escaped. Note that this is an imperfect
Expand All @@ -26,33 +25,33 @@ const REGEXP_INVALID_ATTRIBUTE_NAME = /[\u007F-\u009F "'>/="\uFDD0-\uFDEF]/;
* @see https://w3c.github.io/html/syntax.html#ambiguous-ampersand
* @see https://w3c.github.io/html/syntax.html#named-character-references
*
* @param {string} value Original string.
* @param value Original string.
*
* @return {string} Escaped string.
* @return Escaped string.
*/
export function escapeAmpersand( value ) {
export function escapeAmpersand( value: string ): string {
return value.replace( /&(?!([a-z0-9]+|#[0-9]+|#x[a-f0-9]+);)/gi, '&amp;' );
}

/**
* Returns a string with quotation marks replaced.
*
* @param {string} value Original string.
* @param value Original string.
*
* @return {string} Escaped string.
* @return Escaped string.
*/
export function escapeQuotationMark( value ) {
export function escapeQuotationMark( value: string ): string {
return value.replace( /"/g, '&quot;' );
}

/**
* Returns a string with less-than sign replaced.
*
* @param {string} value Original string.
* @param value Original string.
*
* @return {string} Escaped string.
* @return Escaped string.
*/
export function escapeLessThan( value ) {
export function escapeLessThan( value: string ): string {
return value.replace( /</g, '&lt;' );
}

Expand All @@ -72,11 +71,11 @@ export function escapeLessThan( value ) {
*
* See: https://core.trac.wordpress.org/ticket/45387
*
* @param {string} value Attribute value.
* @param value Attribute value.
*
* @return {string} Escaped attribute value.
* @return Escaped attribute value.
*/
export function escapeAttribute( value ) {
export function escapeAttribute( value: string ): string {
return __unstableEscapeGreaterThan(
escapeQuotationMark( escapeAmpersand( value ) )
);
Expand All @@ -90,11 +89,11 @@ export function escapeAttribute( value ) {
* "the text must not contain the character U+003C LESS-THAN SIGN (<) or an
* ambiguous ampersand."
*
* @param {string} value Element value.
* @param value Element value.
*
* @return {string} Escaped HTML element value.
* @return Escaped HTML element value.
*/
export function escapeHTML( value ) {
export function escapeHTML( value: string ): string {
return escapeLessThan( escapeAmpersand( value ) );
}

Expand All @@ -103,21 +102,21 @@ export function escapeHTML( value ) {
* `escapeHTML`, because for editable HTML, ALL ampersands must be escaped in
* order to render the content correctly on the page.
*
* @param {string} value Element value.
* @param value Element value.
*
* @return {string} Escaped HTML element value.
* @return Escaped HTML element value.
*/
export function escapeEditableHTML( value ) {
export function escapeEditableHTML( value: string ): string {
return escapeLessThan( value.replace( /&/g, '&amp;' ) );
}

/**
* Returns true if the given attribute name is valid, or false otherwise.
*
* @param {string} name Attribute name to test.
* @param name Attribute name to test.
*
* @return {boolean} Whether attribute is valid.
* @return Whether attribute is valid.
*/
export function isValidAttributeName( name ) {
export function isValidAttributeName( name: string ): boolean {
return ! REGEXP_INVALID_ATTRIBUTE_NAME.test( name );
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@ import {
escapeHTML,
isValidAttributeName,
escapeEditableHTML,
} from '../';
} from '..';
import __unstableEscapeGreaterThan from '../escape-greater';

function testUnstableEscapeGreaterThan( implementation ) {
type Implementation = ( str: string ) => string;

function testUnstableEscapeGreaterThan( implementation: Implementation ) {
it( 'should escape greater than', () => {
const result = implementation( 'Chicken > Ribs' );
expect( result ).toBe( 'Chicken &gt; Ribs' );
} );
}

function testEscapeAmpersand( implementation ) {
function testEscapeAmpersand( implementation: Implementation ) {
it( 'should escape ampersand', () => {
const result = implementation(
'foo & bar &amp; &AMP; baz &#931; &#bad; &#x3A3; &#X3a3; &#xevil;'
Expand All @@ -31,15 +33,15 @@ function testEscapeAmpersand( implementation ) {
} );
}

function testEscapeQuotationMark( implementation ) {
function testEscapeQuotationMark( implementation: Implementation ) {
it( 'should escape quotation mark', () => {
const result = implementation( '"Be gone!"' );

expect( result ).toBe( '&quot;Be gone!&quot;' );
} );
}

function testEscapeLessThan( implementation ) {
function testEscapeLessThan( implementation: Implementation ) {
it( 'should escape less than', () => {
const result = implementation( 'Chicken < Ribs' );

Expand Down

1 comment on commit eb924d4

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected in eb924d4.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/9519078053
📝 Reported issues:

Please sign in to comment.