autop.js 16 KB

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