[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/js/dist/ -> autop.js (source)

   1  "use strict";
   2  var wp;
   3  (wp ||= {}).autop = (() => {
   4    var __defProp = Object.defineProperty;
   5    var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
   6    var __getOwnPropNames = Object.getOwnPropertyNames;
   7    var __hasOwnProp = Object.prototype.hasOwnProperty;
   8    var __export = (target, all) => {
   9      for (var name in all)
  10        __defProp(target, name, { get: all[name], enumerable: true });
  11    };
  12    var __copyProps = (to, from, except, desc) => {
  13      if (from && typeof from === "object" || typeof from === "function") {
  14        for (let key of __getOwnPropNames(from))
  15          if (!__hasOwnProp.call(to, key) && key !== except)
  16            __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  17      }
  18      return to;
  19    };
  20    var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  21  
  22    // packages/autop/build-module/index.js
  23    var index_exports = {};
  24    __export(index_exports, {
  25      autop: () => autop,
  26      removep: () => removep
  27    });
  28    var htmlSplitRegex = (() => {
  29      const comments = "!(?:-(?!->)[^\\-]*)*(?:-->)?";
  30      const cdata = "!\\[CDATA\\[[^\\]]*(?:](?!]>)[^\\]]*)*?(?:]]>)?";
  31      const escaped = "(?=!--|!\\[CDATA\\[)((?=!-)" + // If yes, which type?
  32      comments + "|" + cdata + ")";
  33      const regex = "(<(" + // Conditional expression follows.
  34      escaped + // Find end of escaped element.
  35      "|[^>]*>?))";
  36      return new RegExp(regex);
  37    })();
  38    function htmlSplit(input) {
  39      const parts = [];
  40      let workingInput = input;
  41      let match;
  42      while (match = workingInput.match(htmlSplitRegex)) {
  43        const index = match.index;
  44        parts.push(workingInput.slice(0, index));
  45        parts.push(match[0]);
  46        workingInput = workingInput.slice(index + match[0].length);
  47      }
  48      if (workingInput.length) {
  49        parts.push(workingInput);
  50      }
  51      return parts;
  52    }
  53    function replaceInHtmlTags(haystack, replacePairs) {
  54      const textArr = htmlSplit(haystack);
  55      let changed = false;
  56      const needles = Object.keys(replacePairs);
  57      for (let i = 1; i < textArr.length; i += 2) {
  58        for (let j = 0; j < needles.length; j++) {
  59          const needle = needles[j];
  60          if (-1 !== textArr[i].indexOf(needle)) {
  61            textArr[i] = textArr[i].replace(
  62              new RegExp(needle, "g"),
  63              replacePairs[needle]
  64            );
  65            changed = true;
  66            break;
  67          }
  68        }
  69      }
  70      if (changed) {
  71        haystack = textArr.join("");
  72      }
  73      return haystack;
  74    }
  75    function autop(text, br = true) {
  76      const preTags = [];
  77      if (text.trim() === "") {
  78        return "";
  79      }
  80      text = text + "\n";
  81      if (text.indexOf("<pre") !== -1) {
  82        const textParts = text.split("</pre>");
  83        const lastText = textParts.pop();
  84        text = "";
  85        for (let i = 0; i < textParts.length; i++) {
  86          const textPart = textParts[i];
  87          const start = textPart.indexOf("<pre");
  88          if (start === -1) {
  89            text += textPart;
  90            continue;
  91          }
  92          const name = "<pre wp-pre-tag-" + i + "></pre>";
  93          preTags.push([name, textPart.substr(start) + "</pre>"]);
  94          text += textPart.substr(0, start) + name;
  95        }
  96        text += lastText;
  97      }
  98      text = text.replace(/<br\s*\/?>\s*<br\s*\/?>/g, "\n\n");
  99      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)";
 100      text = text.replace(
 101        new RegExp("(<" + allBlocks + "[\\s/>])", "g"),
 102        "\n\n$1"
 103      );
 104      text = text.replace(
 105        new RegExp("(</" + allBlocks + ">)", "g"),
 106        "$1\n\n"
 107      );
 108      text = text.replace(/\r\n|\r/g, "\n");
 109      text = replaceInHtmlTags(text, { "\n": " <!-- wpnl --> " });
 110      if (text.indexOf("<option") !== -1) {
 111        text = text.replace(/\s*<option/g, "<option");
 112        text = text.replace(/<\/option>\s*/g, "</option>");
 113      }
 114      if (text.indexOf("</object>") !== -1) {
 115        text = text.replace(/(<object[^>]*>)\s*/g, "$1");
 116        text = text.replace(/\s*<\/object>/g, "</object>");
 117        text = text.replace(/\s*(<\/?(?:param|embed)[^>]*>)\s*/g, "$1");
 118      }
 119      if (text.indexOf("<source") !== -1 || text.indexOf("<track") !== -1) {
 120        text = text.replace(/([<\[](?:audio|video)[^>\]]*[>\]])\s*/g, "$1");
 121        text = text.replace(/\s*([<\[]\/(?:audio|video)[>\]])/g, "$1");
 122        text = text.replace(/\s*(<(?:source|track)[^>]*>)\s*/g, "$1");
 123      }
 124      if (text.indexOf("<figcaption") !== -1) {
 125        text = text.replace(/\s*(<figcaption[^>]*>)/, "$1");
 126        text = text.replace(/<\/figcaption>\s*/, "</figcaption>");
 127      }
 128      text = text.replace(/\n\n+/g, "\n\n");
 129      const texts = text.split(/\n\s*\n/).filter(Boolean);
 130      text = "";
 131      texts.forEach((textPiece) => {
 132        text += "<p>" + textPiece.replace(/^\n*|\n*$/g, "") + "</p>\n";
 133      });
 134      text = text.replace(/<p>\s*<\/p>/g, "");
 135      text = text.replace(
 136        /<p>([^<]+)<\/(div|address|form)>/g,
 137        "<p>$1</p></$2>"
 138      );
 139      text = text.replace(
 140        new RegExp("<p>\\s*(</?" + allBlocks + "[^>]*>)\\s*</p>", "g"),
 141        "$1"
 142      );
 143      text = text.replace(/<p>(<li.+?)<\/p>/g, "$1");
 144      text = text.replace(/<p><blockquote([^>]*)>/gi, "<blockquote$1><p>");
 145      text = text.replace(/<\/blockquote><\/p>/g, "</p></blockquote>");
 146      text = text.replace(
 147        new RegExp("<p>\\s*(</?" + allBlocks + "[^>]*>)", "g"),
 148        "$1"
 149      );
 150      text = text.replace(
 151        new RegExp("(</?" + allBlocks + "[^>]*>)\\s*</p>", "g"),
 152        "$1"
 153      );
 154      if (br) {
 155        text = text.replace(
 156          /<(script|style).*?<\/\\1>/g,
 157          (match) => match[0].replace(/\n/g, "<WPPreserveNewline />")
 158        );
 159        text = text.replace(/<br>|<br\/>/g, "<br />");
 160        text = text.replace(
 161          /(<br \/>)?\s*\n/g,
 162          (a, b) => b ? a : "<br />\n"
 163        );
 164        text = text.replace(/<WPPreserveNewline \/>/g, "\n");
 165      }
 166      text = text.replace(
 167        new RegExp("(</?" + allBlocks + "[^>]*>)\\s*<br />", "g"),
 168        "$1"
 169      );
 170      text = text.replace(
 171        /<br \/>(\s*<\/?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)/g,
 172        "$1"
 173      );
 174      text = text.replace(/\n<\/p>$/g, "</p>");
 175      preTags.forEach((preTag) => {
 176        const [name, original] = preTag;
 177        text = text.replace(name, () => original);
 178      });
 179      if (-1 !== text.indexOf("<!-- wpnl -->")) {
 180        text = text.replace(/\s?<!-- wpnl -->\s?/g, "\n");
 181      }
 182      return text;
 183    }
 184    function removep(html) {
 185      const blocklist = "blockquote|ul|ol|li|dl|dt|dd|table|thead|tbody|tfoot|tr|th|td|h[1-6]|fieldset|figure";
 186      const blocklist1 = blocklist + "|div|p";
 187      const blocklist2 = blocklist + "|pre";
 188      const preserve = [];
 189      let preserveLinebreaks = false;
 190      let preserveBr = false;
 191      if (!html) {
 192        return "";
 193      }
 194      if (html.indexOf("<script") !== -1 || html.indexOf("<style") !== -1) {
 195        html = html.replace(
 196          /<(script|style)[^>]*>[\s\S]*?<\/\1>/g,
 197          (match) => {
 198            preserve.push(match);
 199            return "<wp-preserve>";
 200          }
 201        );
 202      }
 203      if (html.indexOf("<pre") !== -1) {
 204        preserveLinebreaks = true;
 205        html = html.replace(/<pre[^>]*>[\s\S]+?<\/pre>/g, (a) => {
 206          a = a.replace(/<br ?\/?>(\r\n|\n)?/g, "<wp-line-break>");
 207          a = a.replace(/<\/?p( [^>]*)?>(\r\n|\n)?/g, "<wp-line-break>");
 208          return a.replace(/\r?\n/g, "<wp-line-break>");
 209        });
 210      }
 211      if (html.indexOf("[caption") !== -1) {
 212        preserveBr = true;
 213        html = html.replace(/\[caption[\s\S]+?\[\/caption\]/g, (a) => {
 214          return a.replace(/<br([^>]*)>/g, "<wp-temp-br$1>").replace(/[\r\n\t]+/, "");
 215        });
 216      }
 217      html = html.replace(
 218        new RegExp("\\s*</(" + blocklist1 + ")>\\s*", "g"),
 219        "</$1>\n"
 220      );
 221      html = html.replace(
 222        new RegExp("\\s*<((?:" + blocklist1 + ")(?: [^>]*)?)>", "g"),
 223        "\n<$1>"
 224      );
 225      html = html.replace(/(<p [^>]+>[\s\S]*?)<\/p>/g, "$1</p#>");
 226      html = html.replace(/<div( [^>]*)?>\s*<p>/gi, "<div$1>\n\n");
 227      html = html.replace(/\s*<p>/gi, "");
 228      html = html.replace(/\s*<\/p>\s*/gi, "\n\n");
 229      html = html.replace(/\n[\s\u00a0]+\n/g, "\n\n");
 230      html = html.replace(/(\s*)<br ?\/?>\s*/gi, (_, space) => {
 231        if (space && space.indexOf("\n") !== -1) {
 232          return "\n\n";
 233        }
 234        return "\n";
 235      });
 236      html = html.replace(/\s*<div/g, "\n<div");
 237      html = html.replace(/<\/div>\s*/g, "</div>\n");
 238      html = html.replace(
 239        /\s*\[caption([^\[]+)\[\/caption\]\s*/gi,
 240        "\n\n[caption$1[/caption]\n\n"
 241      );
 242      html = html.replace(/caption\]\n\n+\[caption/g, "caption]\n\n[caption");
 243      html = html.replace(
 244        new RegExp("\\s*<((?:" + blocklist2 + ")(?: [^>]*)?)\\s*>", "g"),
 245        "\n<$1>"
 246      );
 247      html = html.replace(
 248        new RegExp("\\s*</(" + blocklist2 + ")>\\s*", "g"),
 249        "</$1>\n"
 250      );
 251      html = html.replace(/<((li|dt|dd)[^>]*)>/g, "     <$1>");
 252      if (html.indexOf("<option") !== -1) {
 253        html = html.replace(/\s*<option/g, "\n<option");
 254        html = html.replace(/\s*<\/select>/g, "\n</select>");
 255      }
 256      if (html.indexOf("<hr") !== -1) {
 257        html = html.replace(/\s*<hr( [^>]*)?>\s*/g, "\n\n<hr$1>\n\n");
 258      }
 259      if (html.indexOf("<object") !== -1) {
 260        html = html.replace(/<object[\s\S]+?<\/object>/g, (a) => {
 261          return a.replace(/[\r\n]+/g, "");
 262        });
 263      }
 264      html = html.replace(/<\/p#>/g, "</p>\n");
 265      html = html.replace(/\s*(<p [^>]+>[\s\S]*?<\/p>)/g, "\n$1");
 266      html = html.replace(/^\s+/, "");
 267      html = html.replace(/[\s\u00a0]+$/, "");
 268      if (preserveLinebreaks) {
 269        html = html.replace(/<wp-line-break>/g, "\n");
 270      }
 271      if (preserveBr) {
 272        html = html.replace(/<wp-temp-br([^>]*)>/g, "<br$1>");
 273      }
 274      if (preserve.length) {
 275        html = html.replace(/<wp-preserve>/g, () => {
 276          return preserve.shift();
 277        });
 278      }
 279      return html;
 280    }
 281    return __toCommonJS(index_exports);
 282  })();


Generated : Wed May 6 08:20:15 2026 Cross-referenced by PHPXref