JAVASCRIPT   90

autop.js

Guest on 5th August 2021 05:16:16 PM

  1. this["wp"] = this["wp"] || {}; this["wp"]["autop"] =
  2. /******/ (function(modules) { // webpackBootstrap
  3. /******/        // The module cache
  4. /******/        var installedModules = {};
  5. /******/
  6. /******/        // The require function
  7. /******/        function __webpack_require__(moduleId) {
  8. /******/
  9. /******/                // Check if module is in cache
  10. /******/                if(installedModules[moduleId]) {
  11. /******/                        return installedModules[moduleId].exports;
  12. /******/                }
  13. /******/                // Create a new module (and put it into the cache)
  14. /******/                var module = installedModules[moduleId] = {
  15. /******/                        i: moduleId,
  16. /******/                        l: false,
  17. /******/                        exports: {}
  18. /******/                };
  19. /******/
  20. /******/                // Execute the module function
  21. /******/                modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
  22. /******/
  23. /******/                // Flag the module as loaded
  24. /******/                module.l = true;
  25. /******/
  26. /******/                // Return the exports of the module
  27. /******/                return module.exports;
  28. /******/        }
  29. /******/
  30. /******/
  31. /******/        // expose the modules object (__webpack_modules__)
  32. /******/        __webpack_require__.m = modules;
  33. /******/
  34. /******/        // expose the module cache
  35. /******/        __webpack_require__.c = installedModules;
  36. /******/
  37. /******/        // define getter function for harmony exports
  38. /******/        __webpack_require__.d = function(exports, name, getter) {
  39. /******/                if(!__webpack_require__.o(exports, name)) {
  40. /******/                        Object.defineProperty(exports, name, { enumerable: true, get: getter });
  41. /******/                }
  42. /******/        };
  43. /******/
  44. /******/        // define __esModule on exports
  45. /******/        __webpack_require__.r = function(exports) {
  46. /******/                if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
  47. /******/                        Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
  48. /******/                }
  49. /******/                Object.defineProperty(exports, '__esModule', { value: true });
  50. /******/        };
  51. /******/
  52. /******/        // create a fake namespace object
  53. /******/        // mode & 1: value is a module id, require it
  54. /******/        // mode & 2: merge all properties of value into the ns
  55. /******/        // mode & 4: return value when already ns object
  56. /******/        // mode & 8|1: behave like require
  57. /******/        __webpack_require__.t = function(value, mode) {
  58. /******/                if(mode & 1) value = __webpack_require__(value);
  59. /******/                if(mode & 8) return value;
  60. /******/                if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
  61. /******/                var ns = Object.create(null);
  62. /******/                __webpack_require__.r(ns);
  63. /******/                Object.defineProperty(ns, 'default', { enumerable: true, value: value });
  64. /******/                if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
  65. /******/                return ns;
  66. /******/        };
  67. /******/
  68. /******/        // getDefaultExport function for compatibility with non-harmony modules
  69. /******/        __webpack_require__.n = function(module) {
  70. /******/                var getter = module && module.__esModule ?
  71. /******/                        function getDefault() { return module['default']; } :
  72. /******/                        function getModuleExports() { return module; };
  73. /******/                __webpack_require__.d(getter, 'a', getter);
  74. /******/                return getter;
  75. /******/        };
  76. /******/
  77. /******/        // Object.prototype.hasOwnProperty.call
  78. /******/        __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
  79. /******/
  80. /******/        // __webpack_public_path__
  81. /******/        __webpack_require__.p = "";
  82. /******/
  83. /******/
  84. /******/        // Load entry module and return exports
  85. /******/        return __webpack_require__(__webpack_require__.s = "zbAn");
  86. /******/ })
  87. /************************************************************************/
  88. /******/ ({
  89.  
  90. /***/ "zbAn":
  91. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  92.  
  93. "use strict";
  94. __webpack_require__.r(__webpack_exports__);
  95. /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "autop", function() { return autop; });
  96. /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "removep", function() { return removep; });
  97. /**
  98.  * The regular expression for an HTML element.
  99.  *
  100.  * @type {RegExp}
  101.  */
  102. const htmlSplitRegex = (() => {
  103.   /* eslint-disable no-multi-spaces */
  104.   const comments = '!' + // Start of comment, after the <.
  105.   '(?:' + // Unroll the loop: Consume everything until --> is found.
  106.   '-(?!->)' + // Dash not followed by end of comment.
  107.   '[^\\-]*' + // Consume non-dashes.
  108.   ')*' + // Loop possessively.
  109.   '(?:-->)?'; // End of comment. If not found, match all input.
  110.  
  111.   const cdata = '!\\[CDATA\\[' + // Start of comment, after the <.
  112.   '[^\\]]*' + // Consume non-].
  113.   '(?:' + // Unroll the loop: Consume everything until ]]> is found.
  114.   '](?!]>)' + // One ] not followed by end of comment.
  115.   '[^\\]]*' + // Consume non-].
  116.   ')*?' + // Loop possessively.
  117.   '(?:]]>)?'; // End of comment. If not found, match all input.
  118.  
  119.   const escaped = '(?=' + // Is the element escaped?
  120.   '!--' + '|' + '!\\[CDATA\\[' + ')' + '((?=!-)' + // If yes, which type?
  121.   comments + '|' + cdata + ')';
  122.   const regex = '(' + // Capture the entire match.
  123.   '<' + // Find start of element.
  124.   '(' + // Conditional expression follows.
  125.   escaped + // Find end of escaped element.
  126.   '|' + // ... else ...
  127.   '[^>]*>?' + // Find end of normal element.
  128.   ')' + ')';
  129.   return new RegExp(regex);
  130.   /* eslint-enable no-multi-spaces */
  131. })();
  132. /**
  133.  * Separate HTML elements and comments from the text.
  134.  *
  135.  * @param  {string} input The text which has to be formatted.
  136.  * @return {string[]}        The formatted text.
  137.  */
  138.  
  139.  
  140. function htmlSplit(input) {
  141.   const parts = [];
  142.   let workingInput = input;
  143.   let match;
  144.  
  145.   while (match = workingInput.match(htmlSplitRegex)) {
  146.     // The `match` result, when invoked on a RegExp with the `g` flag (`/foo/g`) will not include `index`.
  147.     // If the `g` flag is omitted, `index` is included.
  148.     // `htmlSplitRegex` does not have the `g` flag so we can assert it will have an index number.
  149.     // Assert `match.index` is a number.
  150.     const index =
  151.     /** @type {number} */
  152.     match.index;
  153.     parts.push(workingInput.slice(0, index));
  154.     parts.push(match[0]);
  155.     workingInput = workingInput.slice(index + match[0].length);
  156.   }
  157.  
  158.   if (workingInput.length) {
  159.     parts.push(workingInput);
  160.   }
  161.  
  162.   return parts;
  163. }
  164. /**
  165.  * Replace characters or phrases within HTML elements only.
  166.  *
  167.  * @param  {string}                haystack     The text which has to be formatted.
  168.  * @param  {Record<string,string>} replacePairs In the form {from: 'to', ā€¦}.
  169.  * @return {string}                             The formatted text.
  170.  */
  171.  */
  172.  
  173.  
  174. function replaceInHtmlTags(haystack, replacePairs) // Find all elements.
  175. ts.
  176.   const textArr = htmlSplit(haystack);
  177.   let changed = fal// Extract all needles.
  178. es.
  179.  
  180.   const needles = Object.keys(replacePair// Loop through delimiters (elements) only.
  181. ly.
  182.  
  183.   for (let i = 1; i < textArr.length; i += 2) {
  184.     for (let j = 0; j < needles.length; j++) {
  185.       const needle = needles[j];
  186.  
  187.       if (-1 !== textArr[i].indexOf(needle)) {
  188.         textArr[i] = textArr[i].replace(new RegExp(needle, 'g'), replacePairs[needle]);
  189.         changed = tr// After one strtr() break out of the foreach loop and look at next element.
  190. nt.
  191.  
  192.         break;
  193.       }
  194.     }
  195.   }
  196.  
  197.   if (changed) {
  198.     haystack = textArr.join('');
  199.   }
  200.  
  201.   return haystack/**
  202.  * Replaces double line-breaks with paragraph elements.
  203.  *
  204.  * A group of regex replaces used to identify text formatted with newlines and
  205.  * replace double line-breaks with HTML paragraph tags. The remaining line-
  206.  * breaks after conversion become `<br />` tags, unless br is set to 'false'.
  207.  *
  208.  * @param  {string}    text The text which has to be formatted.
  209.  * @param  {boolean}   br   Optional. If set, will convert all remaining line-
  210.  *                          breaks after paragraphing. Default true.
  211.  *
  212.  * @example
  213.  *```js
  214.  * import { autop } from '@wordpress/autop';
  215.  * autop( 'my text' ); // "<p>my text</p>"
  216.  * ```
  217.  *
  218.  * @return {string}         Text which has been converted into paragraph tags.
  219.  */
  220.  */
  221.  
  222.  
  223. function autop(text, br = true) {
  224.   const preTags = [];
  225.  
  226.   if (text.trim() === '') {
  227.     return '';
  228. // Just to make things a little easier, pad the end.
  229. nd.
  230.  
  231.  
  232.   text = text + '\n'/*
  233.    * Pre tags shouldn't be touched by autop.
  234.    * Replace pre tags with placeholders and bring them back after autop.
  235.    */  */
  236.  
  237.   if (text.indexOf('<pre') !== -1) {
  238.     const textParts = text.split('</pre>');
  239.     const lastText = textParts.pop();
  240.     text = '';
  241.  
  242.     for (let i = 0; i < textParts.length; i++) {
  243.       const textPart = textParts[i];
  244.       const start = textPart.indexOf('<pre// Malformed html?
  245. ml?
  246.  
  247.       if (start === -1) {
  248.         text += textPart;
  249.         continue;
  250.       }
  251.  
  252.       const name = '<pre wp-pre-tag-' + i + '></pre>';
  253.       preTags.push([name, textPart.substr(start) + '</pre>']);
  254.       text += textPart.substr(0, start) + name;
  255.     }
  256.  
  257.     text += lastText;
  258. // Change multiple <br>s into two line breaks, which will turn into paragraphs.
  259. hs.
  260.  
  261.  
  262.   text = text.replace(\s*\/?>\s*<br\s*\/?>/g, '\, '\n\n');
  263.   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.
  264. gs.
  265.  
  266.   text = text.replace(new RegExp('(<' + allBlocks + '[\\s/>])', 'g'\,\'$1'n$1// Add a double line break below block-level closing tags.
  267. gs.
  268.  
  269.   text = text.replace(new RegExp(' + allBlocks + ' + '>)', 'g'$1\'\1'n\n// Standardize newline characters to "\n".
  270. n".
  271.  
  272.   text = text.replace(n|\r/g, '\, '\n// Find newlines in all elements and add placeholders.
  273. rs.
  274.  
  275.   text = replaceInHtmlTags(text, {
  276.     '\n': ' <!-- wpnl --> '
  277.   // Collapse line breaks before and after <option> elements so they don't get autop'd.
  278. 'd.
  279.  
  280.   if (text.indexOf('<option') !== -1) {
  281.     text = text.replace(<option/g, '<, '<option');
  282.     text = text.replace(option>\s*/g, '<, '</option>');
  283.   /*
  284.    * Collapse line breaks inside <object> elements, before <param> and <embed> elements
  285.    * so they don't get autop'd.
  286.    */  */
  287.  
  288.  
  289.   if (text.indexOf('</object>') !== -1) {
  290.     text = text.replace(bject[^>]*>)\s*/g, '$, '$1');
  291.     text = text.replace(<\/object>/g, '<, '</object>');
  292.     text = text.replace((<\/?(?:param|embed)[^>]*>)\s*/g, '$, '$1');
  293.   /*
  294.    * Collapse line breaks inside <audio> and <video> elements,
  295.    * before and after <source> and <track> elements.
  296.    */  */
  297.  
  298.  
  299.   if (text.indexOf('<source') !== -1 || text.indexOf('<track') !== -1) {
  300.     text = text.replace(\[](?:audio|video)[^>\]]*[>\]])\s*/g, '$, '$1');
  301.     text = text.replace(([<\[]\/(?:audio|video)[>\]])/g, '$, '$1');
  302.     text = text.replace((<(?:source|track)[^>]*>)\s*/g, '$, '$1');
  303. // Collapse line breaks before and after <figcaption> elements.
  304. ts.
  305.  
  306.  
  307.   if (text.indexOf('<figcaption') !== -1) {
  308.     text = text.replace((<figcaption[^>]*>)/, '$, '$1');
  309.     text = text.replace(figcaption>\s*/, '<, '</figcaption>');
  310. // Remove more than two contiguous line breaks.
  311. ks.
  312.  
  313.  
  314.   text = text.replace(n+/g, '\, '\n\n// Split up the contents into an array of strings, separated by double line breaks.
  315. ks.
  316.  
  317.   const texts = text.split(s*\n/).fi).filter(Boolea// Reset text prior to rebuilding.
  318. ng.
  319.  
  320.   text = // Rebuild the content as a string, wrapping every bit with a <p>.
  321. p>.
  322.  
  323.   texts.forEach(textPiece => {
  324.     text += ' + textPiece.replace(/^\(*|\n*$/g, 'g, '') + '</p>\n';
  325.   // Under certain strange conditions it could create a P of entirely whitespace.
  326. ce.
  327.  
  328.   text = text.replace(\s*<\/p>/g, '', '// Add a closing <p> inside <div>, <address>, or <form> tag if missing.
  329. ng.
  330.  
  331.   text = text.replace(([^<]+)<\/(div|address|form)>/g, '<, '<p>$1</p></$2>// If an opening or closing block element tag is wrapped in a <p>, unwrap it.
  332. it.
  333.  
  334.   text = text.replace(new RegExp('<p>\\s*(</?' + allBlocks + '[^>]*>)\\s*</p>', 'g'$1''$1// In some cases <li> may get wrapped in <p>, fix them.
  335. em.
  336.  
  337.   text = text.replace((<li.+?)<\/p>/g, '$, '$1// If a <blockquote> is wrapped with a <p>, move it inside the <blockquote>.
  338. e>.
  339.  
  340.   text = text.replace(<blockquote([^>]*)>/gi, '<, '<blockquote$1><p>');
  341.   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.
  342. it.
  343.  
  344.   text = text.replace(new RegExp('<p>\\s*(</?' + allBlocks + '[^>]*>)', 'g'$1''$1// If an opening or closing block element tag is followed by a closing <p> tag, remove it.
  345. it.
  346.  
  347.   text = text.replace(new RegExp('(</?' + allBlocks + '[^>]*>)\\s*</p>', 'g'$1''$1// Optionally insert line breaks.
  348. ks.
  349.  
  350.   if (br) {
  351. // Replace newlines that shouldn't be touched with a placeholder.
  352. er.
  353.     text = text.replace(cript|style).*?<\/\\1>/g, ma, match => match[0].replace(g, '<, '<WPPreserveNewline />'// Normalize <br>
  354. br>
  355.  
  356.     text = text.replace(>|<br\/>/g, '<, '<br />// Replace any new line characters that aren't preceded by a <br /> with a <br />.
  357. />.
  358.  
  359.     text = text.replace(r \/>)?\s*\n/g, (a, (a, b) => b ? a : '<br />\n// Replace newline placeholders with newlines.
  360. es.
  361.  
  362.     text = text.replace(PreserveNewline \/>/g, '\, '\n');
  363. // If a <br /> tag is after an opening or closing block tag, remove it.
  364. it.
  365.  
  366.  
  367.   text = text.replace(new RegExp('(</?' + allBlocks + '[^>]*>)\\s*<br />', 'g'$1''$1// If a <br /> tag is before a subset of opening or closing block tags, remove it.
  368. it.
  369.  
  370.   text = text.replace( \/>(\s*<\/?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)/g, '$, '$1');
  371.   text = text.replace(\/p>$/g, '<, '</p>// Replace placeholder <pre> tags with their original content.
  372. nt.
  373.  
  374.   preTags.forEach(preTag => {
  375.     const [name, original] = preTag;
  376.     text = text.replace(name, original);
  377.   // Restore newlines in all elements.
  378. ts.
  379.  
  380.   if (-1 !== text.indexOf('<!-- wpnl -->')) {
  381.     text = text.replace(<!-- wpnl -->\s?/g, '\, '\n');
  382.   }
  383.  
  384.   return text/**
  385.  * Replaces `<p>` tags with two line breaks. "Opposite" of autop().
  386.  *
  387.  * Replaces `<p>` tags with two line breaks except where the `<p>` has attributes.
  388.  * Unifies whitespace. Indents `<li>`, `<dt>` and `<dd>` for better readability.
  389.  *
  390.  * @param  {string} html The content from the editor.
  391.  *
  392.  * @example
  393.  * ```js
  394.  * import { removep } from '@wordpress/autop';
  395.  * removep( '<p>my text</p>' ); // "my text"
  396.  * ```
  397.  *
  398.  * @return {string}      The content with stripped paragraph tags.
  399.  */
  400.  */
  401.  
  402. function removep(html) {
  403.   const blocklist = 'blockquote|ul|ol|li|dl|dt|dd|table|thead|tbody|tfoot|tr|th|td|h[1-6]|fieldset|figure';
  404.   const blocklist1 = blocklist + '|div|p';
  405.   const blocklist2 = blocklist + '|pre'/** @type {string[]} */} */
  406.  
  407.   const preserve = [];
  408.   let preserveLinebreaks = false;
  409.   let preserveBr = false;
  410.  
  411.   if (!html) {
  412.     return '';
  413. // Protect script and style tags.
  414. gs.
  415.  
  416.  
  417.   if (html.indexOf('<script') !== -1 || html.indexOf('<style') !== -1) {
  418.     html = html.replace(cript|style)[^>]*>[\s\S]*?<\/\1>/g, ma, match => {
  419.       preserve.push(match);
  420.       return '<wp-preserve>';
  421.     });
  422. // Protect pre tags.
  423. gs.
  424.  
  425.  
  426.   if (html.indexOf('<pre') !== -1) {
  427.     preserveLinebreaks = true;
  428.     html = html.replace(e[^>]*>[\s\S]+?<\/pre>/g, a , a => {
  429.       a = a.replace( ?\/?>(\r\n|\n)?/g, '<, '<wp-line-break>');
  430.       a = a.replace(?p( [^>]*)?>(\r\n|\n)?/g, '<, '<wp-line-break>');
  431.       return a.replace(\n/g, '<, '<wp-line-break>');
  432.     });
  433. // Remove line breaks but keep <br> tags inside image captions.
  434. ns.
  435.  
  436.  
  437.   if (html.indexOf('[caption') !== -1) {
  438.     preserveBr = true;
  439.     html = html.replace(aption[\s\S]+?\[\/caption\]/g, a , a => {
  440.       return a.replace(([^>]*)>/g, '<, '<wp-temp-br$1>').replace(\n\t]+/, '', '');
  441.     });
  442. // Normalize white space characters before and after block tags.
  443. gs.
  444.  
  445.  
  446.   html = html.replace(new RegExp('\\s*</(' + blocklist1 + ')>\\s*', 'g'</$1>\$'>\n');
  447.   html = html.replace(new RegExp('\\s*<((?:' + blocklist1 + ')(?: [^>]*)?)>', 'g'\,<$1>'$1>// Mark </p> if it has any attributes.
  448. es.
  449.  
  450.   html = html.replace( [^>]+>[\s\S]*?)<\/p>/g, '$, '$1</p#>// Preserve the first <p> inside a <div>.
  451. v>.
  452.  
  453.   html = html.replace(v( [^>]*)?>\s*<p>/gi, '<, '<div$1>\n\n// Remove paragraph tags.
  454. gs.
  455.  
  456.   html = html.replace(<p>/gi, '', '');
  457.   html = html.replace(<\/p>\s*/gi, '\, '\n\n// Normalize white space chars and remove multiple line breaks.
  458. ks.
  459.  
  460.   html = html.replace(\s\u00a0]+\n/g, '\, '\n\n// Replace <br> tags with line breaks.
  461. ks.
  462.  
  463.   html = html.replace(*)<br ?\/?>\s*/gi, (_, (_, space) => {
  464.     if (space && space.indexOf('\n') !== -1) {
  465.       return '\n\n';
  466.     }
  467.  
  468.     return '\n';
  469.   // Fix line breaks around <div>.
  470. v>.
  471.  
  472.   html = html.replace(<div/g, '\, '\n<div');
  473.   html = html.replace(div>\s*/g, '<, '</div>\n// Fix line breaks around caption shortcodes.
  474. es.
  475.  
  476.   html = html.replace(\[caption([^\[]+)\[\/caption\]\s*/gi, '\, '\n\n[caption$1[/caption]\n\n');
  477.   html = html.replace(tion\]\n\n+\[caption/g, 'c, 'caption]\n\n[caption// Pad block elements tags with a line break.
  478. ak.
  479.  
  480.   html = html.replace(new RegExp('\\s*<((?:' + blocklist2 + ')(?: [^>]*)?)\\s*>', 'g'\,<$1>'$1>');
  481.   html = html.replace(new RegExp('\\s*</(' + blocklist2 + ')>\\s*', 'g'</$1>\$'>\n// Indent <li>, <dt> and <dd> tags.
  482. gs.
  483.  
  484.   html = html.replace(li|dt|dd)[^>]*)>/g, ' , ' \t<$1>// Fix line breaks around <select> and <option>.
  485. n>.
  486.  
  487.   if (html.indexOf('<option') !== -1) {
  488.     html = html.replace(<option/g, '\, '\n<option');
  489.     html = html.replace(<\/select>/g, '\, '\n</select>');
  490. // Pad <hr> with two line breaks.
  491. ks.
  492.  
  493.  
  494.   if (html.indexOf(') !== -1) {
  495.    html = html.replace(/\e*<hr( [^>]*)?>\)*/g, 'g, '\n\n<hr$1>\n\n');
  496. // Remove line breaks in <object> tags.
  497. gs.
  498.  
  499.  
  500.   if (html.indexOf('<object') !== -1) {
  501.     html = html.replace(ject[\s\S]+?<\/object>/g, a , a => {
  502.       return a.replace(\n]+/g, '', '');
  503.     });
  504. // Unmark special paragraph closing tags.
  505. gs.
  506.  
  507.  
  508.   html = html.replace(p#>/g, '<, '</p>\n// Pad remaining <p> tags whit a line break.
  509. ak.
  510.  
  511.   html = html.replace((<p [^>]+>[\s\S]*?<\/p>)/g, '\, '\n$1// Trim.
  512. im.
  513.  
  514.   html = html.replace(+/, '', '');
  515.   html = html.replace(\u00a0]+$/, '', '');
  516.  
  517.   if (preserveLinebreaks) {
  518.     html = html.replace(-line-break>/g, '\, '\n');
  519.   }
  520.  
  521.   if (preserveBr) {
  522.     html = html.replace(-temp-br([^>]*)>/g, '<, '<br$1>');
  523. // Restore preserved tags.
  524. gs.
  525.  
  526.  
  527.   if (preserve.length) {
  528.     html = html.replace(-preserve>/g, (), () => {
  529.       return (
  530.     /** @type {string} */} */
  531.         preserve.shift()
  532.       );
  533.     });
  534.   }
  535.  
  536.   return html;
  537. /***/***/ /******/***/

Raw Paste


Login or Register to edit or fork this paste. It's free.