PluginProbe
Block Animations, Motion & Scroll Effects – Ghost Kit / 2.25.0
Block Animations, Motion & Scroll Effects – Ghost Kit v2.25.0
3.7.2 3.7.1 3.7.0 3.6.1 trunk 1.6.3 2.25.0 3.3.0 3.3.1 3.3.2 3.3.3 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.6 3.5.0 3.5.1 3.6.0
ghostkit / gutenberg / utils / encode-decode / index.js

index.js in Block Animations, Motion & Scroll Effects – Ghost Kit 2.25.0, at gutenberg/utils/encode-decode/index.js

93 lines 2.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 const eCache = {};
2 const dCache = {};
3
4 /**
5 * Encode URI component with `try {} catch` and caching.
6 *
7 * @param {string} str - decoded string.
8 * @return {string} - new encoded string.
9 */
10 export function maybeEncode(str) {
11 // return cached string.
12 if (eCache[str]) {
13 return eCache[str];
14 }
15
16 let result = {};
17
18 // Object
19 if (typeof str === 'object') {
20 Object.keys(str).forEach((k) => {
21 result[maybeEncode(k)] = maybeEncode(str[k]);
22 });
23
24 return result;
25 }
26
27 // String
28 result = str;
29
30 if (typeof result === 'string') {
31 try {
32 // Because of these replacements, some attributes can't be exported to XML without being broken. So, we need to replace it manually with something safe.
33 // https://github.com/WordPress/gutenberg/blob/88645e4b268acf5746e914159e3ce790dcb1665a/packages/blocks/src/api/serializer.js#L246-L271
34 result = result.replace(/--/gm, '_u002d__u002d_');
35
36 result = encodeURIComponent(result);
37 } catch (e) {
38 // eslint-disable-next-line
39 console.warn(e);
40 }
41 }
42
43 // save to cache.
44 eCache[str] = result;
45
46 return result;
47 }
48
49 /**
50 * Encode URI component with `try {} catch` and caching.
51 *
52 * @param {string} str - decoded string.
53 * @return {string} - new encoded string.
54 */
55 export function maybeDecode(str) {
56 // return cached string.
57 if (dCache[str]) {
58 return dCache[str];
59 }
60
61 let result = {};
62
63 // Object
64 if (typeof str === 'object') {
65 Object.keys(str).forEach((k) => {
66 result[maybeDecode(k)] = maybeDecode(str[k]);
67 });
68
69 return result;
70 }
71
72 // String
73 result = str;
74
75 if (typeof result === 'string') {
76 try {
77 result = decodeURIComponent(result);
78
79 // Because of these replacements, some attributes can't be exported to XML without being broken. So, we need to replace it manually with something safe.
80 // https://github.com/WordPress/gutenberg/blob/88645e4b268acf5746e914159e3ce790dcb1665a/packages/blocks/src/api/serializer.js#L246-L271
81 result = result.replace(/_u002d__u002d_/gm, '--');
82 } catch (e) {
83 // eslint-disable-next-line
84 console.warn(e);
85 }
86 }
87
88 // save to cache.
89 dCache[str] = result;
90
91 return result;
92 }
93