Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Refactor: Replace custom HTML entity decoder with `@wordpress/html-en…
Browse files Browse the repository at this point in the history
…tities`

Rationale:
- The shift to `@wordpress/html-entities` aligns with standard WordPress practices, ensuring consistency across the platform.
- Enhances maintainability by relying on a well-supported library rather than custom code.
- Simplifies the codebase by removing a redundant utility function.

This change enhances the robustness of our code and aligns with best practices in WordPress development.
  • Loading branch information
imanish003 committed Nov 24, 2023
1 parent f17fbea commit 70ea5f1
Showing 1 changed file with 3 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import { getProducts } from '@woocommerce/editor-components/utils';
import { ProductResponseItem } from '@woocommerce/types';
import { decodeEntities } from '@wordpress/html-entities';
import { useState, useEffect, useCallback, useMemo } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import {
Expand All @@ -17,25 +18,6 @@ import {
*/
import { QueryControlProps } from '../types';

/**
* Decodes HTML entities in a string.
* Example:
* decodeHTMLEntities( 'foo & bar' ) // 'foo & bar'
* decodeHTMLEntities( 'Hoodie – Black' ) // 'Hoodie – Black'
*
* @param {string} str - The string containing HTML entities.
* @return {string} - The decoded string.
*/
function decodeHTMLEntities( str?: string ) {
if ( ! str ) {
return '';
}

const txt = document.createElement( 'textarea' );
txt.innerHTML = str;
return txt.value;
}

/**
* Returns:
* - productsMap: Map of products by id and name.
Expand Down Expand Up @@ -119,12 +101,12 @@ const HandPickedProductsControl = ( {
const parsedToken = Number( token );

if ( Number.isNaN( parsedToken ) ) {
return decodeHTMLEntities( token );
return decodeEntities( token ) || '';
}

const product = productsMap.get( parsedToken );

return decodeHTMLEntities( product?.name );
return decodeEntities( product?.name ) || '';
};

return (
Expand Down

0 comments on commit 70ea5f1

Please sign in to comment.