Repository navigation
Expand file tree
/
Copy pathclean-node-list.ts
More file actions
185 lines (171 loc) · 4.78 KB
/
Copy pathclean-node-list.ts
File metadata and controls
185 lines (171 loc) · 4.78 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
import isEmpty from './is-empty';
import remove from './remove';
import unwrap from './unwrap';
import { isPhrasingContent } from '../phrasing-content';
import insertAfter from './insert-after';
import isElement from './is-element';
import type { Schema } from './types';
const noop = () => {};
/**
* Given a schema, unwraps or removes nodes, attributes and classes on a node
* list.
*
* @param nodeList The nodeList to filter.
* @param doc The document of the nodeList.
* @param schema Schema for the HTML.
* @param [inline] Whether to clean for inline mode.
*/
export default function cleanNodeList(
nodeList: NodeList,
doc: Document,
schema: Schema,
inline?: boolean
) {
Array.from( nodeList ).forEach(
( node: Node & { nextElementSibling?: unknown } ) => {
const tag = node.nodeName.toLowerCase();
// It's a valid child, if the tag exists in the schema without an isMatch
// function, or with an isMatch function that matches the node.
if (
schema.hasOwnProperty( tag ) &&
( ! schema[ tag ].isMatch || schema[ tag ].isMatch?.( node ) )
) {
if ( isElement( node ) ) {
const {
attributes = [],
classes = [],
children,
require = [],
allowEmpty,
} = schema[ tag ];
// If the node is empty and it's supposed to have children,
// remove the node. For phrasing content nodes that contain
// only whitespace, unwrap instead to preserve the text.
// See https://github.com/WordPress/gutenberg/issues/50898
if ( children && ! allowEmpty && isEmpty( node ) ) {
if (
isPhrasingContent( node ) &&
node.hasChildNodes()
) {
unwrap( node );
} else {
remove( node );
}
return;
}
if ( node.hasAttributes() ) {
// Strip invalid attributes.
Array.from( node.attributes ).forEach( ( { name } ) => {
if (
name !== 'class' &&
! attributes.includes( name )
) {
node.removeAttribute( name );
}
} );
// Strip invalid classes.
// In jsdom-jscore, 'node.classList' can be undefined.
// TODO: Explore patching this in jsdom-jscore.
if ( node.classList && node.classList.length ) {
const mattchers = classes.map( ( item ) => {
if ( item === '*' ) {
// Keep all classes.
return () => true;
} else if ( typeof item === 'string' ) {
return ( className: string ) =>
className === item;
} else if ( item instanceof RegExp ) {
return ( className: string ) =>
item.test( className );
}
return noop;
} );
Array.from( node.classList ).forEach( ( name ) => {
if (
! mattchers.some( ( isMatch ) =>
isMatch( name )
)
) {
node.classList.remove( name );
}
} );
if ( ! node.classList.length ) {
node.removeAttribute( 'class' );
}
}
}
if ( node.hasChildNodes() ) {
// Do not filter any content.
if ( children === '*' ) {
return;
}
// Continue if the node is supposed to have children.
if ( children ) {
// If a parent requires certain children, but it does
// not have them, drop the parent and continue.
if (
require.length &&
! node.querySelector( require.join( ',' ) )
) {
cleanNodeList(
node.childNodes,
doc,
schema,
inline
);
unwrap( node );
// If the node is at the top, phrasing content, and
// contains children that are block content, unwrap
// the node because it is invalid.
} else if (
node.parentNode &&
node.parentNode.nodeName === 'BODY' &&
isPhrasingContent( node )
) {
cleanNodeList(
node.childNodes,
doc,
schema,
inline
);
if (
Array.from( node.childNodes ).some(
( child ) =>
! isPhrasingContent( child )
)
) {
unwrap( node );
}
} else {
cleanNodeList(
node.childNodes,
doc,
children,
inline
);
}
// Remove children if the node is not supposed to have any.
} else {
while ( node.firstChild ) {
remove( node.firstChild );
}
}
}
}
// Invalid child. Continue with schema at the same place and unwrap.
} else {
cleanNodeList( node.childNodes, doc, schema, inline );
// For inline mode, insert a line break when unwrapping nodes that
// are not phrasing content.
if (
inline &&
! isPhrasingContent( node ) &&
node.nextElementSibling
) {
insertAfter( doc.createElement( 'br' ), node );
}
unwrap( node );
}
}
);
}