-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Expand file tree
/
Copy pathutils.js
More file actions
263 lines (241 loc) · 5.97 KB
/
utils.js
File metadata and controls
263 lines (241 loc) · 5.97 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/**
* External dependencies
*/
import deepFreeze from 'deep-freeze';
/**
* WordPress dependencies
*/
import { registerBlockType, unregisterBlockType } from '@wordpress/blocks';
/**
* Internal dependencies
*/
import { getBlockContentSchemaFromTransforms, isPlain } from '../utils';
describe( 'isPlain', () => {
it( 'should return true for plain text', () => {
expect( isPlain( 'test' ) ).toBe( true );
} );
it( 'should return true for only line breaks', () => {
expect( isPlain( 'test<br>test' ) ).toBe( true );
expect( isPlain( 'test<br/>test' ) ).toBe( true );
expect( isPlain( 'test<br />test' ) ).toBe( true );
expect( isPlain( 'test<br data-test>test' ) ).toBe( true );
} );
it( 'should return false for formatted text', () => {
expect( isPlain( '<strong>test</strong>' ) ).toBe( false );
expect( isPlain( '<strong>test<br></strong>' ) ).toBe( false );
expect( isPlain( 'test<br-custom>test' ) ).toBe( false );
} );
it( 'should return true for single non-semantic wrapper elements with only text', () => {
expect( isPlain( '<span>test</span>' ) ).toBe( true );
} );
it( 'should return true for single wrapper with styled content but no semantic tags', () => {
expect( isPlain( '<span style="color: red;">test</span>' ) ).toBe(
true
);
} );
it( 'should return true for single wrapper with line breaks', () => {
expect( isPlain( '<span>test<br>test</span>' ) ).toBe( true );
} );
it( 'should return false for wrapper with semantic child elements', () => {
expect( isPlain( '<div><strong>test</strong></div>' ) ).toBe( false );
expect( isPlain( '<span><em>test</em></span>' ) ).toBe( false );
expect( isPlain( '<p>Some <a href="#">link</a></p>' ) ).toBe( false );
} );
it( 'should return false for multiple wrapper elements', () => {
expect( isPlain( '<span>test</span><span>test</span>' ) ).toBe( false );
} );
it( 'should return false for semantic wrapper elements', () => {
expect( isPlain( '<h1>test</h1>' ) ).toBe( false );
expect( isPlain( '<ul><li>test</li></ul>' ) ).toBe( false );
expect( isPlain( '<article>test</article>' ) ).toBe( false );
} );
it( 'should return true for a single div wrapper with no semantic attributes', () => {
expect( isPlain( '<div>test</div>' ) ).toBe( true );
expect( isPlain( '<div style="color: red;">test</div>' ) ).toBe( true );
} );
it( 'should return true for nested div/span wrappers with only styles', () => {
expect(
isPlain(
'<div style="color: red;"><div><span style="color: blue;">test</span></div><br><div><span>more</span></div></div>'
)
).toBe( true );
} );
[ 'div', 'span' ].forEach( ( tag ) => {
it( `should return false for ${ tag } wrappers with semantic attributes`, () => {
[
'class="box"',
'id="content"',
'role="alert"',
'data-token="abc"',
'aria-label="content"',
].forEach( ( attr ) => {
expect( isPlain( `<${ tag } ${ attr }>test</${ tag }>` ) ).toBe(
false
);
} );
} );
} );
it( 'should return false when a descendant has semantic attributes', () => {
expect( isPlain( '<div><div class="box">test</div></div>' ) ).toBe(
false
);
expect(
isPlain( '<div><span data-token="abc">test</span></div>' )
).toBe( false );
} );
} );
describe( 'getBlockContentSchema', () => {
beforeAll( () => {
registerBlockType( 'core/paragraph', {
apiVersion: 3,
title: 'Paragraph',
supports: {
anchor: true,
},
} );
} );
afterAll( () => {
unregisterBlockType( 'core/paragraph' );
} );
const myContentSchema = {
strong: {},
em: {},
};
it( 'should handle a single raw transform', () => {
const transforms = deepFreeze( [
{
blockName: 'core/paragraph',
type: 'raw',
selector: 'p',
schema: {
p: {
children: myContentSchema,
},
},
},
] );
const output = {
p: {
children: myContentSchema,
attributes: [ 'id' ],
isMatch: undefined,
},
};
expect( getBlockContentSchemaFromTransforms( transforms ) ).toEqual(
output
);
} );
it( 'should handle multiple raw transforms', () => {
const preformattedIsMatch = ( input ) => {
return input === 4;
};
const transforms = deepFreeze( [
{
blockName: 'core/paragraph',
type: 'raw',
schema: {
p: {
children: myContentSchema,
},
},
},
{
blockName: 'core/preformatted',
type: 'raw',
isMatch: preformattedIsMatch,
schema: {
pre: {
children: myContentSchema,
},
},
},
] );
const output = {
p: {
children: myContentSchema,
attributes: [ 'id' ],
isMatch: undefined,
},
pre: {
children: myContentSchema,
attributes: [],
isMatch: preformattedIsMatch,
},
};
expect( getBlockContentSchemaFromTransforms( transforms ) ).toEqual(
output
);
} );
it( 'should correctly merge the children', () => {
const transforms = deepFreeze( [
{
blockName: 'my/preformatted',
type: 'raw',
schema: {
pre: {
children: {
sub: {},
sup: {},
strong: {},
},
},
},
},
{
blockName: 'core/preformatted',
type: 'raw',
schema: {
pre: {
children: myContentSchema,
},
},
},
] );
const output = {
pre: {
children: {
strong: {},
em: {},
sub: {},
sup: {},
},
},
};
expect( getBlockContentSchemaFromTransforms( transforms ) ).toEqual(
output
);
} );
it( 'should correctly merge the attributes', () => {
const transforms = deepFreeze( [
{
blockName: 'my/preformatted',
type: 'raw',
schema: {
pre: {
attributes: [ 'data-chicken' ],
children: myContentSchema,
},
},
},
{
blockName: 'core/preformatted',
type: 'raw',
schema: {
pre: {
attributes: [ 'data-ribs' ],
children: myContentSchema,
},
},
},
] );
const output = {
pre: {
children: myContentSchema,
attributes: [ 'data-chicken', 'data-ribs' ],
},
};
expect( getBlockContentSchemaFromTransforms( transforms ) ).toEqual(
output
);
} );
} );