wp/wp-includes/js/dist/autop.js
changeset 22 8c2e4d02f4ef
parent 21 48c4eec2b7e6
equal deleted inserted replaced
21:48c4eec2b7e6 22:8c2e4d02f4ef
    39 /* harmony export */   autop: () => (/* binding */ autop),
    39 /* harmony export */   autop: () => (/* binding */ autop),
    40 /* harmony export */   removep: () => (/* binding */ removep)
    40 /* harmony export */   removep: () => (/* binding */ removep)
    41 /* harmony export */ });
    41 /* harmony export */ });
    42 /**
    42 /**
    43  * The regular expression for an HTML element.
    43  * The regular expression for an HTML element.
    44  *
       
    45  * @type {RegExp}
       
    46  */
    44  */
    47 const htmlSplitRegex = (() => {
    45 const htmlSplitRegex = (() => {
    48   /* eslint-disable no-multi-spaces */
    46   /* eslint-disable no-multi-spaces */
    49   const comments = '!' +
    47   const comments = '!' +
    50   // Start of comment, after the <.
    48   // Start of comment, after the <.
    95 })();
    93 })();
    96 
    94 
    97 /**
    95 /**
    98  * Separate HTML elements and comments from the text.
    96  * Separate HTML elements and comments from the text.
    99  *
    97  *
   100  * @param {string} input The text which has to be formatted.
    98  * @param input The text which has to be formatted.
   101  *
    99  *
   102  * @return {string[]} The formatted text.
   100  * @return The formatted text.
   103  */
   101  */
   104 function htmlSplit(input) {
   102 function htmlSplit(input) {
   105   const parts = [];
   103   const parts = [];
   106   let workingInput = input;
   104   let workingInput = input;
   107   let match;
   105   let match;
   108   while (match = workingInput.match(htmlSplitRegex)) {
   106   while (match = workingInput.match(htmlSplitRegex)) {
   109     // The `match` result, when invoked on a RegExp with the `g` flag (`/foo/g`) will not include `index`.
   107     // The `match` result, when invoked on a RegExp with the `g` flag (`/foo/g`) will not include `index`.
   110     // If the `g` flag is omitted, `index` is included.
   108     // If the `g` flag is omitted, `index` is included.
   111     // `htmlSplitRegex` does not have the `g` flag so we can assert it will have an index number.
   109     // `htmlSplitRegex` does not have the `g` flag so we can assert it will have an index number.
   112     // Assert `match.index` is a number.
   110     // Assert `match.index` is a number.
   113     const index = /** @type {number} */match.index;
   111     const index = match.index;
   114     parts.push(workingInput.slice(0, index));
   112     parts.push(workingInput.slice(0, index));
   115     parts.push(match[0]);
   113     parts.push(match[0]);
   116     workingInput = workingInput.slice(index + match[0].length);
   114     workingInput = workingInput.slice(index + match[0].length);
   117   }
   115   }
   118   if (workingInput.length) {
   116   if (workingInput.length) {
   122 }
   120 }
   123 
   121 
   124 /**
   122 /**
   125  * Replace characters or phrases within HTML elements only.
   123  * Replace characters or phrases within HTML elements only.
   126  *
   124  *
   127  * @param {string}                haystack     The text which has to be formatted.
   125  * @param haystack     The text which has to be formatted.
   128  * @param {Record<string,string>} replacePairs In the form {from: 'to', …}.
   126  * @param replacePairs In the form {from: 'to', …}.
   129  *
   127  *
   130  * @return {string} The formatted text.
   128  * @return The formatted text.
   131  */
   129  */
   132 function replaceInHtmlTags(haystack, replacePairs) {
   130 function replaceInHtmlTags(haystack, replacePairs) {
   133   // Find all elements.
   131   // Find all elements.
   134   const textArr = htmlSplit(haystack);
   132   const textArr = htmlSplit(haystack);
   135   let changed = false;
   133   let changed = false;
   160  *
   158  *
   161  * A group of regex replaces used to identify text formatted with newlines and
   159  * A group of regex replaces used to identify text formatted with newlines and
   162  * replace double line-breaks with HTML paragraph tags. The remaining line-
   160  * replace double line-breaks with HTML paragraph tags. The remaining line-
   163  * breaks after conversion become `<br />` tags, unless br is set to 'false'.
   161  * breaks after conversion become `<br />` tags, unless br is set to 'false'.
   164  *
   162  *
   165  * @param {string}  text The text which has to be formatted.
   163  * @param text The text which has to be formatted.
   166  * @param {boolean} br   Optional. If set, will convert all remaining line-
   164  * @param br   Optional. If set, will convert all remaining line-
   167  *                       breaks after paragraphing. Default true.
   165  *             breaks after paragraphing. Default true.
   168  *
   166  *
   169  * @example
   167  * @example
   170  *```js
   168  *```js
   171  * import { autop } from '@wordpress/autop';
   169  * import { autop } from '@wordpress/autop';
   172  * autop( 'my text' ); // "<p>my text</p>"
   170  * autop( 'my text' ); // "<p>my text</p>"
   173  * ```
   171  * ```
   174  *
   172  *
   175  * @return {string} Text which has been converted into paragraph tags.
   173  * @return Text which has been converted into paragraph tags.
   176  */
   174  */
   177 function autop(text, br = true) {
   175 function autop(text, br = true) {
   178   const preTags = [];
   176   const preTags = [];
   179   if (text.trim() === '') {
   177   if (text.trim() === '') {
   180     return '';
   178     return '';
   331  * Replaces `<p>` tags with two line breaks. "Opposite" of autop().
   329  * Replaces `<p>` tags with two line breaks. "Opposite" of autop().
   332  *
   330  *
   333  * Replaces `<p>` tags with two line breaks except where the `<p>` has attributes.
   331  * Replaces `<p>` tags with two line breaks except where the `<p>` has attributes.
   334  * Unifies whitespace. Indents `<li>`, `<dt>` and `<dd>` for better readability.
   332  * Unifies whitespace. Indents `<li>`, `<dt>` and `<dd>` for better readability.
   335  *
   333  *
   336  * @param {string} html The content from the editor.
   334  * @param html The content from the editor.
   337  *
   335  *
   338  * @example
   336  * @example
   339  * ```js
   337  * ```js
   340  * import { removep } from '@wordpress/autop';
   338  * import { removep } from '@wordpress/autop';
   341  * removep( '<p>my text</p>' ); // "my text"
   339  * removep( '<p>my text</p>' ); // "my text"
   342  * ```
   340  * ```
   343  *
   341  *
   344  * @return {string} The content with stripped paragraph tags.
   342  * @return The content with stripped paragraph tags.
   345  */
   343  */
   346 function removep(html) {
   344 function removep(html) {
   347   const blocklist = 'blockquote|ul|ol|li|dl|dt|dd|table|thead|tbody|tfoot|tr|th|td|h[1-6]|fieldset|figure';
   345   const blocklist = 'blockquote|ul|ol|li|dl|dt|dd|table|thead|tbody|tfoot|tr|th|td|h[1-6]|fieldset|figure';
   348   const blocklist1 = blocklist + '|div|p';
   346   const blocklist1 = blocklist + '|div|p';
   349   const blocklist2 = blocklist + '|pre';
   347   const blocklist2 = blocklist + '|pre';
   350   /** @type {string[]} */
       
   351   const preserve = [];
   348   const preserve = [];
   352   let preserveLinebreaks = false;
   349   let preserveLinebreaks = false;
   353   let preserveBr = false;
   350   let preserveBr = false;
   354   if (!html) {
   351   if (!html) {
   355     return '';
   352     return '';
   456   }
   453   }
   457 
   454 
   458   // Restore preserved tags.
   455   // Restore preserved tags.
   459   if (preserve.length) {
   456   if (preserve.length) {
   460     html = html.replace(/<wp-preserve>/g, () => {
   457     html = html.replace(/<wp-preserve>/g, () => {
   461       return /** @type {string} */preserve.shift();
   458       return preserve.shift();
   462     });
   459     });
   463   }
   460   }
   464   return html;
   461   return html;
   465 }
   462 }
   466 
   463