Skip to content
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

convert escape-html package to TS #62586

Merged
merged 4 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, '>' );
}
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, '&' );
}

/**
* 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, '"' );
}

/**
* 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
Loading