-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathvalidation.js
391 lines (356 loc) · 10 KB
/
validation.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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/**
* External dependencies
*/
import { tokenize } from 'simple-html-tokenizer';
import { xor, fromPairs, isEqual, includes } from 'lodash';
/**
* Internal dependencies
*/
import { getSaveContent } from './serializer';
/**
* Globally matches any consecutive whitespace
*
* @type {RegExp}
*/
const REGEXP_WHITESPACE = /[\t\n\r\v\f ]+/g;
/**
* Matches a string containing only whitespace
*
* @type {RegExp}
*/
const REGEXP_ONLY_WHITESPACE = /^[\t\n\r\v\f ]*$/;
/**
* Matches a CSS URL type value
*
* @type {RegExp}
*/
const REGEXP_STYLE_URL_TYPE = /^url\s*\(['"\s]*(.*?)['"\s]*\)$/;
/**
* Boolean attributes are attributes whose presence as being assigned is
* meaningful, even if only empty.
*
* See: https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#boolean-attributes
* Extracted from: https://html.spec.whatwg.org/multipage/indices.html#attributes-3
*
* [ ...document.querySelectorAll( '#attributes-1 > tbody > tr' ) ]
* .filter( ( tr ) => tr.lastChild.textContent.indexOf( 'Boolean attribute' ) !== -1 )
* .map( ( tr ) => tr.firstChild.textContent.trim() )
*
* @type {Array}
*/
const BOOLEAN_ATTRIBUTES = [
'allowfullscreen',
'allowpaymentrequest',
'allowusermedia',
'async',
'autofocus',
'autoplay',
'checked',
'controls',
'default',
'defer',
'disabled',
'formnovalidate',
'hidden',
'ismap',
'itemscope',
'loop',
'multiple',
'muted',
'nomodule',
'novalidate',
'open',
'open',
'playsinline',
'readonly',
'required',
'reversed',
'selected',
'typemustmatch',
];
/**
* Enumerated attributes are attributes which must be of a specific value form.
* Like boolean attributes, these are meaningful if specified, even if not of a
* valid enumerated value.
*
* See: https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#enumerated-attribute
* Extracted from: https://html.spec.whatwg.org/multipage/indices.html#attributes-3
*
* [ ...document.querySelectorAll( '#attributes-1 > tbody > tr' ) ]
* filter( ( tr ) => /("(.+?)";?\s*)+/.test( tr.lastChild.textContent ) )
* .map( ( tr ) => tr.firstChild.textContent.trim() )
*
* @type {Array}
*/
const ENUMERATED_ATTRIBUTES = [
'autocomplete',
'contenteditable',
'crossorigin',
'dir',
'dir',
'draggable',
'enctype',
'formenctype',
'formmethod',
'inputmode',
'kind',
'method',
'preload',
'sandbox',
'scope',
'shape',
'spellcheck',
'step',
'translate',
'type',
'type',
'workertype',
'wrap',
];
/**
* Meaningful attributes are those who cannot be safely ignored when omitted in
* one HTML markup string and not another.
*
* @type {Array}
*/
const MEANINGFUL_ATTRIBUTES = [
...BOOLEAN_ATTRIBUTES,
...ENUMERATED_ATTRIBUTES,
];
/**
* Given a specified string, returns an array of strings split by consecutive
* whitespace, ignoring leading or trailing whitespace.
*
* @param {String} text Original text
* @return {String[]} Text pieces split on whitespace
*/
export function getTextPiecesSplitOnWhitespace( text ) {
return text.trim().split( REGEXP_WHITESPACE );
}
/**
* Given a specified string, returns a new trimmed string where all consecutive
* whitespace is collapsed to a single space.
*
* @param {String} text Original text
* @return {String} Trimmed text with consecutive whitespace collapsed
*/
export function getTextWithCollapsedWhitespace( text ) {
return getTextPiecesSplitOnWhitespace( text ).join( ' ' );
}
/**
* Returns attribute pairs of the given StartTag token, including only pairs
* where the value is non-empty or the attribute is a boolean attribute, an
* enumerated attribute, or a custom data- attribute.
*
* @see MEANINGFUL_ATTRIBUTES
*
* @param {Object} token StartTag token
* @return {Array[]} Attribute pairs
*/
export function getMeaningfulAttributePairs( token ) {
return token.attributes.filter( ( pair ) => {
const [ key, value ] = pair;
return (
value ||
key.indexOf( 'data-' ) === 0 ||
includes( MEANINGFUL_ATTRIBUTES, key )
);
} );
}
/**
* Returns true if two text tokens (with `chars` property) are equivalent, or
* false otherwise.
*
* @param {Object} a First token
* @param {Object} b Second token
* @return {Boolean} Whether two text tokens are equivalent
*/
export function isEqualTextTokensWithCollapsedWhitespace( a, b ) {
// This is an overly simplified whitespace comparison. The specification is
// more prescriptive of whitespace behavior in inline and block contexts.
//
// See: https://medium.com/@patrickbrosset/when-does-white-space-matter-in-html-b90e8a7cdd33
return isEqual( ...[ a.chars, b.chars ].map( getTextWithCollapsedWhitespace ) );
}
/**
* Given a style value, returns a normalized style value for strict equality
* comparison.
*
* @param {String} value Style value
* @return {String} Normalized style value
*/
export function getNormalizedStyleValue( value ) {
return value
// Normalize URL type to omit whitespace or quotes
.replace( REGEXP_STYLE_URL_TYPE, 'url($1)' );
}
/**
* Given a style attribute string, returns an object of style properties.
*
* @param {String} text Style attribute
* @return {Object} Style properties
*/
export function getStyleProperties( text ) {
const pairs = text
// Trim ending semicolon (avoid including in split)
.replace( /;?\s*$/, '' )
// Split on property assignment
.split( ';' )
// For each property assignment...
.map( ( style ) => {
// ...split further into key-value pairs
const [ key, ...valueParts ] = style.split( ':' );
const value = valueParts.join( ':' );
return [
key.trim(),
getNormalizedStyleValue( value.trim() ),
];
} );
return fromPairs( pairs );
}
/**
* Attribute-specific equality handlers
*
* @type {Object}
*/
export const isEqualAttributesOfName = {
class: ( a, b ) => {
// Class matches if members are the same, even if out of order or
// superfluous whitespace between.
return ! xor( ...[ a, b ].map( getTextPiecesSplitOnWhitespace ) ).length;
},
style: ( a, b ) => {
return isEqual( ...[ a, b ].map( getStyleProperties ) );
},
};
/**
* Given two sets of attribute tuples, returns true if the attribute sets are
* equivalent
*
* @param {Array[]} a First attributes tuples
* @param {Array[]} b Second attributes tuples
* @return {Boolean} Whether attributes are equivalent
*/
export function isEqualTagAttributePairs( a, b ) {
// Attributes is tokenized as tuples. Their lengths should match. This also
// avoids us needing to check both attributes sets, since if A has any keys
// which do not exist in B, we know the sets to be different.
if ( a.length !== b.length ) {
return false;
}
// Convert tuples to object for ease of lookup
const [ aAttributes, bAttributes ] = [ a, b ].map( fromPairs );
for ( const name in aAttributes ) {
// As noted above, if missing member in B, assume different
if ( ! bAttributes.hasOwnProperty( name ) ) {
return false;
}
const aValue = aAttributes[ name ];
const bValue = bAttributes[ name ];
const isEqualAttributes = isEqualAttributesOfName[ name ];
if ( isEqualAttributes ) {
// Defer custom attribute equality handling
if ( ! isEqualAttributes( aValue, bValue ) ) {
return false;
}
} else if ( aValue !== bValue ) {
// Otherwise strict inequality should bail
return false;
}
}
return true;
}
/**
* Token-type-specific equality handlers
*
* @type {Object}
*/
export const isEqualTokensOfType = {
StartTag: ( a, b ) => {
if ( a.tagName !== b.tagName ) {
return false;
}
return isEqualTagAttributePairs(
...[ a, b ].map( getMeaningfulAttributePairs )
);
},
Chars: isEqualTextTokensWithCollapsedWhitespace,
Comment: isEqualTextTokensWithCollapsedWhitespace,
};
/**
* Given an array of tokens, returns the first token which is not purely
* whitespace.
*
* Mutates the tokens array.
*
* @param {Object[]} tokens Set of tokens to search
* @return {Object} Next non-whitespace token
*/
export function getNextNonWhitespaceToken( tokens ) {
let token;
while ( ( token = tokens.shift() ) ) {
if ( token.type !== 'Chars' ) {
return token;
}
if ( ! REGEXP_ONLY_WHITESPACE.test( token.chars ) ) {
return token;
}
}
}
/**
* Returns true if there is given HTML strings are effectively equivalent, or
* false otherwise.
*
* @param {String} a First HTML string
* @param {String} b Second HTML string
* @return {Boolean} Whether HTML strings are equivalent
*/
export function isEquivalentHTML( a, b ) {
// Tokenize input content and reserialized save content
const [ actualTokens, expectedTokens ] = [ a, b ].map( tokenize );
let actualToken, expectedToken;
while ( ( actualToken = getNextNonWhitespaceToken( actualTokens ) ) ) {
expectedToken = getNextNonWhitespaceToken( expectedTokens );
// Inequal if exhausted all expected tokens
if ( ! expectedToken ) {
return false;
}
// Inequal if next non-whitespace token of each set are not same type
if ( actualToken.type !== expectedToken.type ) {
return false;
}
// Defer custom token type equality handling, otherwise continue and
// assume as equal
const isEqualTokens = isEqualTokensOfType[ actualToken.type ];
if ( isEqualTokens && ! isEqualTokens( actualToken, expectedToken ) ) {
return false;
}
}
while ( ( expectedToken = getNextNonWhitespaceToken( expectedTokens ) ) ) {
// If any non-whitespace tokens remain in expected token set, this
// indicates inequality
return false;
}
return true;
}
/**
* Returns true if the parsed block is valid given the input content. A block
* is considered valid if, when serialized with assumed attributes, the content
* matches the original value.
*
* Logs to console in development environments when invalid.
*
* @param {String} rawContent Original block content
* @param {String} blockType Block type
* @param {Object} attributes Parsed block attributes
* @return {Boolean} Whether block is valid
*/
export function isValidBlock( rawContent, blockType, attributes ) {
let saveContent;
try {
saveContent = getSaveContent( blockType, attributes );
} catch ( error ) {
return false;
}
return isEquivalentHTML( rawContent, saveContent );
}