-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathpaste-handler.js
242 lines (214 loc) · 7.47 KB
/
paste-handler.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
/**
* External dependencies
*/
import { flatMap, compact } from 'lodash';
/**
* WordPress dependencies
*/
import { getPhrasingContentSchema, removeInvalidHTML } from '@wordpress/dom';
/**
* Internal dependencies
*/
import { htmlToBlocks } from './html-to-blocks';
import { hasBlockSupport } from '../registration';
import { getBlockInnerHTML } from '../serializer';
import { parseWithGrammar } from '../parser';
import normaliseBlocks from './normalise-blocks';
import specialCommentConverter from './special-comment-converter';
import commentRemover from './comment-remover';
import isInlineContent from './is-inline-content';
import phrasingContentReducer from './phrasing-content-reducer';
import headRemover from './head-remover';
import msListConverter from './ms-list-converter';
import listReducer from './list-reducer';
import imageCorrector from './image-corrector';
import blockquoteNormaliser from './blockquote-normaliser';
import figureContentReducer from './figure-content-reducer';
import shortcodeConverter from './shortcode-converter';
import markdownConverter from './markdown-converter';
import iframeRemover from './iframe-remover';
import googleDocsUIDRemover from './google-docs-uid-remover';
import htmlFormattingRemover from './html-formatting-remover';
import brRemover from './br-remover';
import { deepFilterHTML, isPlain, getBlockContentSchema } from './utils';
import emptyParagraphRemover from './empty-paragraph-remover';
/**
* Browser dependencies
*/
const { console } = window;
/**
* Filters HTML to only contain phrasing content.
*
* @param {string} HTML The HTML to filter.
* @param {boolean} preserveWhiteSpace Whether or not to preserve consequent white space.
*
* @return {string} HTML only containing phrasing content.
*/
function filterInlineHTML( HTML, preserveWhiteSpace ) {
HTML = deepFilterHTML( HTML, [
googleDocsUIDRemover,
phrasingContentReducer,
commentRemover,
] );
HTML = removeInvalidHTML( HTML, getPhrasingContentSchema( 'paste' ), {
inline: true,
} );
if ( ! preserveWhiteSpace ) {
HTML = deepFilterHTML( HTML, [ htmlFormattingRemover, brRemover ] );
}
// Allows us to ask for this information when we get a report.
console.log( 'Processed inline HTML:\n\n', HTML );
return HTML;
}
/**
* Converts an HTML string to known blocks. Strips everything else.
*
* @param {Object} options
* @param {string} [options.HTML] The HTML to convert.
* @param {string} [options.plainText] Plain text version.
* @param {string} [options.mode] Handle content as blocks or inline content.
* * 'AUTO': Decide based on the content passed.
* * 'INLINE': Always handle as inline content, and return string.
* * 'BLOCKS': Always handle as blocks, and return array of blocks.
* @param {Array} [options.tagName] The tag into which content will be inserted.
* @param {boolean} [options.preserveWhiteSpace] Whether or not to preserve consequent white space.
*
* @return {Array|string} A list of blocks or a string, depending on `handlerMode`.
*/
export function pasteHandler( {
HTML = '',
plainText = '',
mode = 'AUTO',
tagName,
preserveWhiteSpace,
} ) {
// First of all, strip any meta tags.
HTML = HTML.replace( /<meta[^>]+>/g, '' );
// Strip Windows markers.
HTML = HTML.replace(
/^\s*<html[^>]*>\s*<body[^>]*>(?:\s*<!--\s*StartFragment\s*-->)?/i,
''
);
HTML = HTML.replace(
/(?:<!--\s*EndFragment\s*-->\s*)?<\/body>\s*<\/html>\s*$/i,
''
);
// If we detect block delimiters in HTML, parse entirely as blocks.
if ( mode !== 'INLINE' ) {
// Check plain text if there is no HTML.
const content = HTML ? HTML : plainText;
if ( content.indexOf( '<!-- wp:' ) !== -1 ) {
return parseWithGrammar( content );
}
}
// Normalize unicode to use composed characters.
// This is unsupported in IE 11 but it's a nice-to-have feature, not mandatory.
// Not normalizing the content will only affect older browsers and won't
// entirely break the app.
// See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/normalize
// See: https://core.trac.wordpress.org/ticket/30130
// See: https://github.com/WordPress/gutenberg/pull/6983#pullrequestreview-125151075
if ( String.prototype.normalize ) {
HTML = HTML.normalize();
}
// Parse Markdown (and encoded HTML) if:
// * There is a plain text version.
// * There is no HTML version, or it has no formatting.
if ( plainText && ( ! HTML || isPlain( HTML ) ) ) {
HTML = plainText;
// The markdown converter (Showdown) trims whitespace.
if ( ! /^\s+$/.test( plainText ) ) {
HTML = markdownConverter( HTML );
}
// Switch to inline mode if:
// * The current mode is AUTO.
// * The original plain text had no line breaks.
// * The original plain text was not an HTML paragraph.
// * The converted text is just a paragraph.
if (
mode === 'AUTO' &&
plainText.indexOf( '\n' ) === -1 &&
plainText.indexOf( '<p>' ) !== 0 &&
HTML.indexOf( '<p>' ) === 0
) {
mode = 'INLINE';
}
}
if ( mode === 'INLINE' ) {
return filterInlineHTML( HTML, preserveWhiteSpace );
}
// An array of HTML strings and block objects. The blocks replace matched
// shortcodes.
const pieces = shortcodeConverter( HTML );
// The call to shortcodeConverter will always return more than one element
// if shortcodes are matched. The reason is when shortcodes are matched
// empty HTML strings are included.
const hasShortcodes = pieces.length > 1;
if (
mode === 'AUTO' &&
! hasShortcodes &&
isInlineContent( HTML, tagName )
) {
return filterInlineHTML( HTML, preserveWhiteSpace );
}
const phrasingContentSchema = getPhrasingContentSchema( 'paste' );
const blockContentSchema = getBlockContentSchema( 'paste' );
const blocks = compact(
flatMap( pieces, ( piece ) => {
// Already a block from shortcode.
if ( typeof piece !== 'string' ) {
return piece;
}
const filters = [
googleDocsUIDRemover,
msListConverter,
headRemover,
listReducer,
imageCorrector,
phrasingContentReducer,
specialCommentConverter,
commentRemover,
iframeRemover,
figureContentReducer,
blockquoteNormaliser,
];
const schema = {
...blockContentSchema,
// Keep top-level phrasing content, normalised by `normaliseBlocks`.
...phrasingContentSchema,
};
piece = deepFilterHTML( piece, filters, blockContentSchema );
piece = removeInvalidHTML( piece, schema );
piece = normaliseBlocks( piece );
piece = deepFilterHTML(
piece,
[ htmlFormattingRemover, brRemover, emptyParagraphRemover ],
blockContentSchema
);
// Allows us to ask for this information when we get a report.
console.log( 'Processed HTML piece:\n\n', piece );
return htmlToBlocks( piece );
} )
);
// If we're allowed to return inline content, and there is only one
// inlineable block, and the original plain text content does not have any
// line breaks, then treat it as inline paste.
if (
mode === 'AUTO' &&
blocks.length === 1 &&
hasBlockSupport( blocks[ 0 ].name, '__unstablePasteTextInline', false )
) {
// Don't catch line breaks at the start or end.
const trimmedPlainText = plainText.replace( /^[\n]+|[\n]+$/g, '' );
if (
trimmedPlainText !== '' &&
trimmedPlainText.indexOf( '\n' ) === -1
) {
return removeInvalidHTML(
getBlockInnerHTML( blocks[ 0 ] ),
phrasingContentSchema
);
}
}
return blocks;
}