[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

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

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


Generated : Sat Aug 8 08:20:21 2026 Cross-referenced by PHPXref