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