-
Notifications
You must be signed in to change notification settings - Fork 10.8k
/
Copy pathutils.js
104 lines (93 loc) · 2.89 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/**
* External dependencies
*/
import { isArray, isNumber, isString } from 'lodash';
import deprecated from '@wordpress/deprecated';
import { createInterpolateElement } from '@wordpress/element';
/**
* DOM Node.textContent for React components
* See: https://github.com/rwu823/react-addons-text-content/blob/master/src/index.js
*
* @param {Array<string|Node>} components array of components
*
* @return {string} concatenated text content of all nodes
*/
export function textContent( components ) {
let text = '';
const toText = ( component ) => {
if ( isString( component ) || isNumber( component ) ) {
text += component;
} else if ( isArray( component ) ) {
component.forEach( toText );
} else if ( component && component.props ) {
const { children } = component.props;
if ( isArray( children ) ) {
children.forEach( toText );
} else {
toText( children );
}
}
};
toText( components );
return text;
}
/**
* This function processes an input string, checks for deprecated interpolation formatting, and
* modifies it to conform to the new standard.
* The deprecated interpolation formatting is `{{element}}...{{/element}}`, and the new standard
* formatting is `<element>...</element>`.
*
* @param {string} interpolatedString The interpolation string to be parsed.
*
* @return {string} Fixed interpolation string.
*/
export function getInterpolatedString( interpolatedString ) {
const regex = /(\{\{)(\/?\s*\w+\s*\/?)(\}\})/g;
const replacedString = interpolatedString.replaceAll(
regex,
( match, p1, p2 ) => {
const inner = p2.trim();
let replacement;
if ( inner.startsWith( '/' ) ) {
// Closing tag
replacement = `</${ inner.slice( 1 ) }>`;
} else if ( inner.endsWith( '/' ) ) {
// Self-closing tag
replacement = `<${ inner.slice( 0, -1 ) }/>`;
} else {
// Opening tag
replacement = `<${ inner }>`;
}
return replacement;
}
);
if ( replacedString !== interpolatedString ) {
deprecated(
'Old interpolation string format `{{element}}...{{/element}}` or `{{element/}}`',
{
since: '7.8',
alternative:
'new interpolation string format `<element>...</element>` or `<element/>`',
link: 'https://github.com/woocommerce/woocommerce/tree/trunk/packages/js/components/src/advanced-filters/README.md',
}
);
}
return replacedString;
}
/**
* This function creates an interpolation element that is backwards compatible.
*
* @param {string} interpolatedString The interpolation string to be parsed and transformed.
* @param {Object} conversionMap The map used for the conversion to create the interpolate element.
*
* @return {Element} A React element that is the result of applying the transformation.
*/
export function backwardsCompatibleCreateInterpolateElement(
interpolatedString,
conversionMap
) {
return createInterpolateElement(
getInterpolatedString( interpolatedString ),
conversionMap
);
}