@wordpress/autop 3.39.0 → 3.40.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +2 -0
- package/build/index.js +149 -122
- package/build/index.js.map +1 -1
- package/build-module/index.js +149 -120
- package/build-module/index.js.map +1 -1
- package/package.json +2 -2
- package/tsconfig.tsbuildinfo +1 -1
package/build-module/index.js
CHANGED
|
@@ -5,34 +5,54 @@
|
|
|
5
5
|
*/
|
|
6
6
|
const htmlSplitRegex = (() => {
|
|
7
7
|
/* eslint-disable no-multi-spaces */
|
|
8
|
-
const comments = '!' +
|
|
9
|
-
|
|
10
|
-
'
|
|
11
|
-
|
|
12
|
-
')
|
|
8
|
+
const comments = '!' +
|
|
9
|
+
// Start of comment, after the <.
|
|
10
|
+
'(?:' +
|
|
11
|
+
// Unroll the loop: Consume everything until --> is found.
|
|
12
|
+
'-(?!->)' +
|
|
13
|
+
// Dash not followed by end of comment.
|
|
14
|
+
'[^\\-]*' +
|
|
15
|
+
// Consume non-dashes.
|
|
16
|
+
')*' +
|
|
17
|
+
// Loop possessively.
|
|
13
18
|
'(?:-->)?'; // End of comment. If not found, match all input.
|
|
14
19
|
|
|
15
|
-
const cdata = '!\\[CDATA\\[' +
|
|
16
|
-
|
|
17
|
-
'
|
|
18
|
-
|
|
19
|
-
'
|
|
20
|
-
|
|
20
|
+
const cdata = '!\\[CDATA\\[' +
|
|
21
|
+
// Start of comment, after the <.
|
|
22
|
+
'[^\\]]*' +
|
|
23
|
+
// Consume non-].
|
|
24
|
+
'(?:' +
|
|
25
|
+
// Unroll the loop: Consume everything until ]]> is found.
|
|
26
|
+
'](?!]>)' +
|
|
27
|
+
// One ] not followed by end of comment.
|
|
28
|
+
'[^\\]]*' +
|
|
29
|
+
// Consume non-].
|
|
30
|
+
')*?' +
|
|
31
|
+
// Loop possessively.
|
|
21
32
|
'(?:]]>)?'; // End of comment. If not found, match all input.
|
|
22
33
|
|
|
23
|
-
const escaped = '(?=' +
|
|
24
|
-
|
|
34
|
+
const escaped = '(?=' +
|
|
35
|
+
// Is the element escaped?
|
|
36
|
+
'!--' + '|' + '!\\[CDATA\\[' + ')' + '((?=!-)' +
|
|
37
|
+
// If yes, which type?
|
|
25
38
|
comments + '|' + cdata + ')';
|
|
26
|
-
const regex = '(' +
|
|
27
|
-
|
|
28
|
-
'
|
|
29
|
-
|
|
30
|
-
'
|
|
31
|
-
|
|
39
|
+
const regex = '(' +
|
|
40
|
+
// Capture the entire match.
|
|
41
|
+
'<' +
|
|
42
|
+
// Find start of element.
|
|
43
|
+
'(' +
|
|
44
|
+
// Conditional expression follows.
|
|
45
|
+
escaped +
|
|
46
|
+
// Find end of escaped element.
|
|
47
|
+
'|' +
|
|
48
|
+
// ... else ...
|
|
49
|
+
'[^>]*>?' +
|
|
50
|
+
// Find end of normal element.
|
|
32
51
|
')' + ')';
|
|
33
52
|
return new RegExp(regex);
|
|
34
53
|
/* eslint-enable no-multi-spaces */
|
|
35
54
|
})();
|
|
55
|
+
|
|
36
56
|
/**
|
|
37
57
|
* Separate HTML elements and comments from the text.
|
|
38
58
|
*
|
|
@@ -40,32 +60,26 @@ const htmlSplitRegex = (() => {
|
|
|
40
60
|
*
|
|
41
61
|
* @return {string[]} The formatted text.
|
|
42
62
|
*/
|
|
43
|
-
|
|
44
|
-
|
|
45
63
|
function htmlSplit(input) {
|
|
46
64
|
const parts = [];
|
|
47
65
|
let workingInput = input;
|
|
48
66
|
let match;
|
|
49
|
-
|
|
50
67
|
while (match = workingInput.match(htmlSplitRegex)) {
|
|
51
68
|
// The `match` result, when invoked on a RegExp with the `g` flag (`/foo/g`) will not include `index`.
|
|
52
69
|
// If the `g` flag is omitted, `index` is included.
|
|
53
70
|
// `htmlSplitRegex` does not have the `g` flag so we can assert it will have an index number.
|
|
54
71
|
// Assert `match.index` is a number.
|
|
55
|
-
const index =
|
|
56
|
-
/** @type {number} */
|
|
57
|
-
match.index;
|
|
72
|
+
const index = /** @type {number} */match.index;
|
|
58
73
|
parts.push(workingInput.slice(0, index));
|
|
59
74
|
parts.push(match[0]);
|
|
60
75
|
workingInput = workingInput.slice(index + match[0].length);
|
|
61
76
|
}
|
|
62
|
-
|
|
63
77
|
if (workingInput.length) {
|
|
64
78
|
parts.push(workingInput);
|
|
65
79
|
}
|
|
66
|
-
|
|
67
80
|
return parts;
|
|
68
81
|
}
|
|
82
|
+
|
|
69
83
|
/**
|
|
70
84
|
* Replace characters or phrases within HTML elements only.
|
|
71
85
|
*
|
|
@@ -74,34 +88,32 @@ function htmlSplit(input) {
|
|
|
74
88
|
*
|
|
75
89
|
* @return {string} The formatted text.
|
|
76
90
|
*/
|
|
77
|
-
|
|
78
|
-
|
|
79
91
|
function replaceInHtmlTags(haystack, replacePairs) {
|
|
80
92
|
// Find all elements.
|
|
81
93
|
const textArr = htmlSplit(haystack);
|
|
82
|
-
let changed = false;
|
|
94
|
+
let changed = false;
|
|
83
95
|
|
|
84
|
-
|
|
96
|
+
// Extract all needles.
|
|
97
|
+
const needles = Object.keys(replacePairs);
|
|
85
98
|
|
|
99
|
+
// Loop through delimiters (elements) only.
|
|
86
100
|
for (let i = 1; i < textArr.length; i += 2) {
|
|
87
101
|
for (let j = 0; j < needles.length; j++) {
|
|
88
102
|
const needle = needles[j];
|
|
89
|
-
|
|
90
103
|
if (-1 !== textArr[i].indexOf(needle)) {
|
|
91
104
|
textArr[i] = textArr[i].replace(new RegExp(needle, 'g'), replacePairs[needle]);
|
|
92
|
-
changed = true;
|
|
93
|
-
|
|
105
|
+
changed = true;
|
|
106
|
+
// After one strtr() break out of the foreach loop and look at next element.
|
|
94
107
|
break;
|
|
95
108
|
}
|
|
96
109
|
}
|
|
97
110
|
}
|
|
98
|
-
|
|
99
111
|
if (changed) {
|
|
100
112
|
haystack = textArr.join('');
|
|
101
113
|
}
|
|
102
|
-
|
|
103
114
|
return haystack;
|
|
104
115
|
}
|
|
116
|
+
|
|
105
117
|
/**
|
|
106
118
|
* Replaces double line-breaks with paragraph elements.
|
|
107
119
|
*
|
|
@@ -121,145 +133,159 @@ function replaceInHtmlTags(haystack, replacePairs) {
|
|
|
121
133
|
*
|
|
122
134
|
* @return {string} Text which has been converted into paragraph tags.
|
|
123
135
|
*/
|
|
124
|
-
|
|
125
|
-
|
|
126
136
|
export function autop(text, br = true) {
|
|
127
137
|
const preTags = [];
|
|
128
|
-
|
|
129
138
|
if (text.trim() === '') {
|
|
130
139
|
return '';
|
|
131
|
-
}
|
|
132
|
-
|
|
140
|
+
}
|
|
133
141
|
|
|
142
|
+
// Just to make things a little easier, pad the end.
|
|
134
143
|
text = text + '\n';
|
|
144
|
+
|
|
135
145
|
/*
|
|
136
146
|
* Pre tags shouldn't be touched by autop.
|
|
137
147
|
* Replace pre tags with placeholders and bring them back after autop.
|
|
138
148
|
*/
|
|
139
|
-
|
|
140
149
|
if (text.indexOf('<pre') !== -1) {
|
|
141
150
|
const textParts = text.split('</pre>');
|
|
142
151
|
const lastText = textParts.pop();
|
|
143
152
|
text = '';
|
|
144
|
-
|
|
145
153
|
for (let i = 0; i < textParts.length; i++) {
|
|
146
154
|
const textPart = textParts[i];
|
|
147
|
-
const start = textPart.indexOf('<pre');
|
|
155
|
+
const start = textPart.indexOf('<pre');
|
|
148
156
|
|
|
157
|
+
// Malformed html?
|
|
149
158
|
if (start === -1) {
|
|
150
159
|
text += textPart;
|
|
151
160
|
continue;
|
|
152
161
|
}
|
|
153
|
-
|
|
154
162
|
const name = '<pre wp-pre-tag-' + i + '></pre>';
|
|
155
163
|
preTags.push([name, textPart.substr(start) + '</pre>']);
|
|
156
164
|
text += textPart.substr(0, start) + name;
|
|
157
165
|
}
|
|
158
|
-
|
|
159
166
|
text += lastText;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
|
|
167
|
+
}
|
|
168
|
+
// Change multiple <br>s into two line breaks, which will turn into paragraphs.
|
|
163
169
|
text = text.replace(/<br\s*\/?>\s*<br\s*\/?>/g, '\n\n');
|
|
164
|
-
const allBlocks = '(?:table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|form|map|area|blockquote|address|math|style|p|h[1-6]|hr|fieldset|legend|section|article|aside|hgroup|header|footer|nav|figure|figcaption|details|menu|summary)';
|
|
170
|
+
const allBlocks = '(?:table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|form|map|area|blockquote|address|math|style|p|h[1-6]|hr|fieldset|legend|section|article|aside|hgroup|header|footer|nav|figure|figcaption|details|menu|summary)';
|
|
165
171
|
|
|
166
|
-
|
|
172
|
+
// Add a double line break above block-level opening tags.
|
|
173
|
+
text = text.replace(new RegExp('(<' + allBlocks + '[\\s/>])', 'g'), '\n\n$1');
|
|
167
174
|
|
|
168
|
-
|
|
175
|
+
// Add a double line break below block-level closing tags.
|
|
176
|
+
text = text.replace(new RegExp('(</' + allBlocks + '>)', 'g'), '$1\n\n');
|
|
169
177
|
|
|
170
|
-
|
|
178
|
+
// Standardize newline characters to "\n".
|
|
179
|
+
text = text.replace(/\r\n|\r/g, '\n');
|
|
171
180
|
|
|
181
|
+
// Find newlines in all elements and add placeholders.
|
|
172
182
|
text = replaceInHtmlTags(text, {
|
|
173
183
|
'\n': ' <!-- wpnl --> '
|
|
174
|
-
});
|
|
184
|
+
});
|
|
175
185
|
|
|
186
|
+
// Collapse line breaks before and after <option> elements so they don't get autop'd.
|
|
176
187
|
if (text.indexOf('<option') !== -1) {
|
|
177
188
|
text = text.replace(/\s*<option/g, '<option');
|
|
178
189
|
text = text.replace(/<\/option>\s*/g, '</option>');
|
|
179
190
|
}
|
|
191
|
+
|
|
180
192
|
/*
|
|
181
193
|
* Collapse line breaks inside <object> elements, before <param> and <embed> elements
|
|
182
194
|
* so they don't get autop'd.
|
|
183
195
|
*/
|
|
184
|
-
|
|
185
|
-
|
|
186
196
|
if (text.indexOf('</object>') !== -1) {
|
|
187
197
|
text = text.replace(/(<object[^>]*>)\s*/g, '$1');
|
|
188
198
|
text = text.replace(/\s*<\/object>/g, '</object>');
|
|
189
199
|
text = text.replace(/\s*(<\/?(?:param|embed)[^>]*>)\s*/g, '$1');
|
|
190
200
|
}
|
|
201
|
+
|
|
191
202
|
/*
|
|
192
203
|
* Collapse line breaks inside <audio> and <video> elements,
|
|
193
204
|
* before and after <source> and <track> elements.
|
|
194
205
|
*/
|
|
195
|
-
|
|
196
|
-
|
|
197
206
|
if (text.indexOf('<source') !== -1 || text.indexOf('<track') !== -1) {
|
|
198
207
|
text = text.replace(/([<\[](?:audio|video)[^>\]]*[>\]])\s*/g, '$1');
|
|
199
208
|
text = text.replace(/\s*([<\[]\/(?:audio|video)[>\]])/g, '$1');
|
|
200
209
|
text = text.replace(/\s*(<(?:source|track)[^>]*>)\s*/g, '$1');
|
|
201
|
-
}
|
|
202
|
-
|
|
210
|
+
}
|
|
203
211
|
|
|
212
|
+
// Collapse line breaks before and after <figcaption> elements.
|
|
204
213
|
if (text.indexOf('<figcaption') !== -1) {
|
|
205
214
|
text = text.replace(/\s*(<figcaption[^>]*>)/, '$1');
|
|
206
215
|
text = text.replace(/<\/figcaption>\s*/, '</figcaption>');
|
|
207
|
-
}
|
|
208
|
-
|
|
216
|
+
}
|
|
209
217
|
|
|
210
|
-
|
|
218
|
+
// Remove more than two contiguous line breaks.
|
|
219
|
+
text = text.replace(/\n\n+/g, '\n\n');
|
|
211
220
|
|
|
212
|
-
|
|
221
|
+
// Split up the contents into an array of strings, separated by double line breaks.
|
|
222
|
+
const texts = text.split(/\n\s*\n/).filter(Boolean);
|
|
213
223
|
|
|
214
|
-
|
|
224
|
+
// Reset text prior to rebuilding.
|
|
225
|
+
text = '';
|
|
215
226
|
|
|
227
|
+
// Rebuild the content as a string, wrapping every bit with a <p>.
|
|
216
228
|
texts.forEach(textPiece => {
|
|
217
229
|
text += '<p>' + textPiece.replace(/^\n*|\n*$/g, '') + '</p>\n';
|
|
218
|
-
});
|
|
230
|
+
});
|
|
219
231
|
|
|
220
|
-
|
|
232
|
+
// Under certain strange conditions it could create a P of entirely whitespace.
|
|
233
|
+
text = text.replace(/<p>\s*<\/p>/g, '');
|
|
221
234
|
|
|
222
|
-
|
|
235
|
+
// Add a closing <p> inside <div>, <address>, or <form> tag if missing.
|
|
236
|
+
text = text.replace(/<p>([^<]+)<\/(div|address|form)>/g, '<p>$1</p></$2>');
|
|
223
237
|
|
|
224
|
-
|
|
238
|
+
// If an opening or closing block element tag is wrapped in a <p>, unwrap it.
|
|
239
|
+
text = text.replace(new RegExp('<p>\\s*(</?' + allBlocks + '[^>]*>)\\s*</p>', 'g'), '$1');
|
|
225
240
|
|
|
226
|
-
|
|
241
|
+
// In some cases <li> may get wrapped in <p>, fix them.
|
|
242
|
+
text = text.replace(/<p>(<li.+?)<\/p>/g, '$1');
|
|
227
243
|
|
|
244
|
+
// If a <blockquote> is wrapped with a <p>, move it inside the <blockquote>.
|
|
228
245
|
text = text.replace(/<p><blockquote([^>]*)>/gi, '<blockquote$1><p>');
|
|
229
|
-
text = text.replace(/<\/blockquote><\/p>/g, '</p></blockquote>');
|
|
246
|
+
text = text.replace(/<\/blockquote><\/p>/g, '</p></blockquote>');
|
|
230
247
|
|
|
231
|
-
|
|
248
|
+
// If an opening or closing block element tag is preceded by an opening <p> tag, remove it.
|
|
249
|
+
text = text.replace(new RegExp('<p>\\s*(</?' + allBlocks + '[^>]*>)', 'g'), '$1');
|
|
232
250
|
|
|
233
|
-
|
|
251
|
+
// If an opening or closing block element tag is followed by a closing <p> tag, remove it.
|
|
252
|
+
text = text.replace(new RegExp('(</?' + allBlocks + '[^>]*>)\\s*</p>', 'g'), '$1');
|
|
234
253
|
|
|
254
|
+
// Optionally insert line breaks.
|
|
235
255
|
if (br) {
|
|
236
256
|
// Replace newlines that shouldn't be touched with a placeholder.
|
|
237
|
-
text = text.replace(/<(script|style).*?<\/\\1>/g, match => match[0].replace(/\n/g, '<WPPreserveNewline />'));
|
|
257
|
+
text = text.replace(/<(script|style).*?<\/\\1>/g, match => match[0].replace(/\n/g, '<WPPreserveNewline />'));
|
|
238
258
|
|
|
239
|
-
|
|
259
|
+
// Normalize <br>
|
|
260
|
+
text = text.replace(/<br>|<br\/>/g, '<br />');
|
|
240
261
|
|
|
241
|
-
|
|
262
|
+
// Replace any new line characters that aren't preceded by a <br /> with a <br />.
|
|
263
|
+
text = text.replace(/(<br \/>)?\s*\n/g, (a, b) => b ? a : '<br />\n');
|
|
242
264
|
|
|
265
|
+
// Replace newline placeholders with newlines.
|
|
243
266
|
text = text.replace(/<WPPreserveNewline \/>/g, '\n');
|
|
244
|
-
}
|
|
245
|
-
|
|
267
|
+
}
|
|
246
268
|
|
|
247
|
-
|
|
269
|
+
// If a <br /> tag is after an opening or closing block tag, remove it.
|
|
270
|
+
text = text.replace(new RegExp('(</?' + allBlocks + '[^>]*>)\\s*<br />', 'g'), '$1');
|
|
248
271
|
|
|
272
|
+
// If a <br /> tag is before a subset of opening or closing block tags, remove it.
|
|
249
273
|
text = text.replace(/<br \/>(\s*<\/?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)/g, '$1');
|
|
250
|
-
text = text.replace(/\n<\/p>$/g, '</p>');
|
|
274
|
+
text = text.replace(/\n<\/p>$/g, '</p>');
|
|
251
275
|
|
|
276
|
+
// Replace placeholder <pre> tags with their original content.
|
|
252
277
|
preTags.forEach(preTag => {
|
|
253
278
|
const [name, original] = preTag;
|
|
254
279
|
text = text.replace(name, original);
|
|
255
|
-
});
|
|
280
|
+
});
|
|
256
281
|
|
|
282
|
+
// Restore newlines in all elements.
|
|
257
283
|
if (-1 !== text.indexOf('<!-- wpnl -->')) {
|
|
258
284
|
text = text.replace(/\s?<!-- wpnl -->\s?/g, '\n');
|
|
259
285
|
}
|
|
260
|
-
|
|
261
286
|
return text;
|
|
262
287
|
}
|
|
288
|
+
|
|
263
289
|
/**
|
|
264
290
|
* Replaces `<p>` tags with two line breaks. "Opposite" of autop().
|
|
265
291
|
*
|
|
@@ -276,30 +302,27 @@ export function autop(text, br = true) {
|
|
|
276
302
|
*
|
|
277
303
|
* @return {string} The content with stripped paragraph tags.
|
|
278
304
|
*/
|
|
279
|
-
|
|
280
305
|
export function removep(html) {
|
|
281
306
|
const blocklist = 'blockquote|ul|ol|li|dl|dt|dd|table|thead|tbody|tfoot|tr|th|td|h[1-6]|fieldset|figure';
|
|
282
307
|
const blocklist1 = blocklist + '|div|p';
|
|
283
308
|
const blocklist2 = blocklist + '|pre';
|
|
284
309
|
/** @type {string[]} */
|
|
285
|
-
|
|
286
310
|
const preserve = [];
|
|
287
311
|
let preserveLinebreaks = false;
|
|
288
312
|
let preserveBr = false;
|
|
289
|
-
|
|
290
313
|
if (!html) {
|
|
291
314
|
return '';
|
|
292
|
-
}
|
|
293
|
-
|
|
315
|
+
}
|
|
294
316
|
|
|
317
|
+
// Protect script and style tags.
|
|
295
318
|
if (html.indexOf('<script') !== -1 || html.indexOf('<style') !== -1) {
|
|
296
319
|
html = html.replace(/<(script|style)[^>]*>[\s\S]*?<\/\1>/g, match => {
|
|
297
320
|
preserve.push(match);
|
|
298
321
|
return '<wp-preserve>';
|
|
299
322
|
});
|
|
300
|
-
}
|
|
301
|
-
|
|
323
|
+
}
|
|
302
324
|
|
|
325
|
+
// Protect pre tags.
|
|
303
326
|
if (html.indexOf('<pre') !== -1) {
|
|
304
327
|
preserveLinebreaks = true;
|
|
305
328
|
html = html.replace(/<pre[^>]*>[\s\S]+?<\/pre>/g, a => {
|
|
@@ -307,91 +330,97 @@ export function removep(html) {
|
|
|
307
330
|
a = a.replace(/<\/?p( [^>]*)?>(\r\n|\n)?/g, '<wp-line-break>');
|
|
308
331
|
return a.replace(/\r?\n/g, '<wp-line-break>');
|
|
309
332
|
});
|
|
310
|
-
}
|
|
311
|
-
|
|
333
|
+
}
|
|
312
334
|
|
|
335
|
+
// Remove line breaks but keep <br> tags inside image captions.
|
|
313
336
|
if (html.indexOf('[caption') !== -1) {
|
|
314
337
|
preserveBr = true;
|
|
315
338
|
html = html.replace(/\[caption[\s\S]+?\[\/caption\]/g, a => {
|
|
316
339
|
return a.replace(/<br([^>]*)>/g, '<wp-temp-br$1>').replace(/[\r\n\t]+/, '');
|
|
317
340
|
});
|
|
318
|
-
}
|
|
319
|
-
|
|
341
|
+
}
|
|
320
342
|
|
|
343
|
+
// Normalize white space characters before and after block tags.
|
|
321
344
|
html = html.replace(new RegExp('\\s*</(' + blocklist1 + ')>\\s*', 'g'), '</$1>\n');
|
|
322
|
-
html = html.replace(new RegExp('\\s*<((?:' + blocklist1 + ')(?: [^>]*)?)>', 'g'), '\n<$1>');
|
|
345
|
+
html = html.replace(new RegExp('\\s*<((?:' + blocklist1 + ')(?: [^>]*)?)>', 'g'), '\n<$1>');
|
|
323
346
|
|
|
324
|
-
|
|
347
|
+
// Mark </p> if it has any attributes.
|
|
348
|
+
html = html.replace(/(<p [^>]+>[\s\S]*?)<\/p>/g, '$1</p#>');
|
|
325
349
|
|
|
326
|
-
|
|
350
|
+
// Preserve the first <p> inside a <div>.
|
|
351
|
+
html = html.replace(/<div( [^>]*)?>\s*<p>/gi, '<div$1>\n\n');
|
|
327
352
|
|
|
353
|
+
// Remove paragraph tags.
|
|
328
354
|
html = html.replace(/\s*<p>/gi, '');
|
|
329
|
-
html = html.replace(/\s*<\/p>\s*/gi, '\n\n');
|
|
355
|
+
html = html.replace(/\s*<\/p>\s*/gi, '\n\n');
|
|
330
356
|
|
|
331
|
-
|
|
357
|
+
// Normalize white space chars and remove multiple line breaks.
|
|
358
|
+
html = html.replace(/\n[\s\u00a0]+\n/g, '\n\n');
|
|
332
359
|
|
|
360
|
+
// Replace <br> tags with line breaks.
|
|
333
361
|
html = html.replace(/(\s*)<br ?\/?>\s*/gi, (_, space) => {
|
|
334
362
|
if (space && space.indexOf('\n') !== -1) {
|
|
335
363
|
return '\n\n';
|
|
336
364
|
}
|
|
337
|
-
|
|
338
365
|
return '\n';
|
|
339
|
-
});
|
|
366
|
+
});
|
|
340
367
|
|
|
368
|
+
// Fix line breaks around <div>.
|
|
341
369
|
html = html.replace(/\s*<div/g, '\n<div');
|
|
342
|
-
html = html.replace(/<\/div>\s*/g, '</div>\n');
|
|
370
|
+
html = html.replace(/<\/div>\s*/g, '</div>\n');
|
|
343
371
|
|
|
372
|
+
// Fix line breaks around caption shortcodes.
|
|
344
373
|
html = html.replace(/\s*\[caption([^\[]+)\[\/caption\]\s*/gi, '\n\n[caption$1[/caption]\n\n');
|
|
345
|
-
html = html.replace(/caption\]\n\n+\[caption/g, 'caption]\n\n[caption');
|
|
374
|
+
html = html.replace(/caption\]\n\n+\[caption/g, 'caption]\n\n[caption');
|
|
346
375
|
|
|
376
|
+
// Pad block elements tags with a line break.
|
|
347
377
|
html = html.replace(new RegExp('\\s*<((?:' + blocklist2 + ')(?: [^>]*)?)\\s*>', 'g'), '\n<$1>');
|
|
348
|
-
html = html.replace(new RegExp('\\s*</(' + blocklist2 + ')>\\s*', 'g'), '</$1>\n');
|
|
378
|
+
html = html.replace(new RegExp('\\s*</(' + blocklist2 + ')>\\s*', 'g'), '</$1>\n');
|
|
349
379
|
|
|
350
|
-
|
|
380
|
+
// Indent <li>, <dt> and <dd> tags.
|
|
381
|
+
html = html.replace(/<((li|dt|dd)[^>]*)>/g, ' \t<$1>');
|
|
351
382
|
|
|
383
|
+
// Fix line breaks around <select> and <option>.
|
|
352
384
|
if (html.indexOf('<option') !== -1) {
|
|
353
385
|
html = html.replace(/\s*<option/g, '\n<option');
|
|
354
386
|
html = html.replace(/\s*<\/select>/g, '\n</select>');
|
|
355
|
-
}
|
|
356
|
-
|
|
387
|
+
}
|
|
357
388
|
|
|
389
|
+
// Pad <hr> with two line breaks.
|
|
358
390
|
if (html.indexOf('<hr') !== -1) {
|
|
359
391
|
html = html.replace(/\s*<hr( [^>]*)?>\s*/g, '\n\n<hr$1>\n\n');
|
|
360
|
-
}
|
|
361
|
-
|
|
392
|
+
}
|
|
362
393
|
|
|
394
|
+
// Remove line breaks in <object> tags.
|
|
363
395
|
if (html.indexOf('<object') !== -1) {
|
|
364
396
|
html = html.replace(/<object[\s\S]+?<\/object>/g, a => {
|
|
365
397
|
return a.replace(/[\r\n]+/g, '');
|
|
366
398
|
});
|
|
367
|
-
}
|
|
368
|
-
|
|
399
|
+
}
|
|
369
400
|
|
|
370
|
-
|
|
401
|
+
// Unmark special paragraph closing tags.
|
|
402
|
+
html = html.replace(/<\/p#>/g, '</p>\n');
|
|
371
403
|
|
|
372
|
-
|
|
404
|
+
// Pad remaining <p> tags whit a line break.
|
|
405
|
+
html = html.replace(/\s*(<p [^>]+>[\s\S]*?<\/p>)/g, '\n$1');
|
|
373
406
|
|
|
407
|
+
// Trim.
|
|
374
408
|
html = html.replace(/^\s+/, '');
|
|
375
409
|
html = html.replace(/[\s\u00a0]+$/, '');
|
|
376
|
-
|
|
377
410
|
if (preserveLinebreaks) {
|
|
378
411
|
html = html.replace(/<wp-line-break>/g, '\n');
|
|
379
412
|
}
|
|
380
|
-
|
|
381
413
|
if (preserveBr) {
|
|
382
414
|
html = html.replace(/<wp-temp-br([^>]*)>/g, '<br$1>');
|
|
383
|
-
}
|
|
384
|
-
|
|
415
|
+
}
|
|
385
416
|
|
|
417
|
+
// Restore preserved tags.
|
|
386
418
|
if (preserve.length) {
|
|
387
419
|
html = html.replace(/<wp-preserve>/g, () => {
|
|
388
|
-
return (
|
|
389
|
-
/** @type {string} */
|
|
390
|
-
preserve.shift()
|
|
420
|
+
return (/** @type {string} */preserve.shift()
|
|
391
421
|
);
|
|
392
422
|
});
|
|
393
423
|
}
|
|
394
|
-
|
|
395
424
|
return html;
|
|
396
425
|
}
|
|
397
426
|
//# sourceMappingURL=index.js.map
|