[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/js/dist/ -> block-serialization-default-parser.js (source)

   1  (function() {
   2  "use strict";
   3  var wp;
   4  (wp ||= {}).blockSerializationDefaultParser = (() => {
   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/block-serialization-default-parser/build-module/index.mjs
  24    var index_exports = {};
  25    __export(index_exports, {
  26      parse: () => parse
  27    });
  28    var document;
  29    var offset;
  30    var output;
  31    var stack;
  32    var delimiterPrefixPattern = /[ \t\f\r\n]+(\/)?wp:([a-z][a-z0-9_-]*\/)?([a-z][a-z0-9_-]*)[ \t\f\r\n]+/y;
  33    var attributeAreaClosingPattern = /}[ \t\f\r\n]+$/g;
  34    var commentStartPattern = /<!--/g;
  35    var onlyDashesCommentEndPattern = /-*>/y;
  36    var commentEndPattern = /--!?>/g;
  37    function Block(blockName, attrs, innerBlocks, innerHTML, innerContent) {
  38      return {
  39        blockName,
  40        attrs,
  41        innerBlocks,
  42        innerHTML,
  43        innerContent
  44      };
  45    }
  46    function Freeform(innerHTML) {
  47      return Block(null, {}, [], innerHTML, [innerHTML]);
  48    }
  49    function Frame(block, tokenStart, tokenLength, prevOffset, leadingHtmlStart) {
  50      return {
  51        block,
  52        tokenStart,
  53        tokenLength,
  54        prevOffset: prevOffset || tokenStart + tokenLength,
  55        leadingHtmlStart
  56      };
  57    }
  58    var parse = (doc) => {
  59      document = doc;
  60      offset = 0;
  61      output = [];
  62      stack = [];
  63      commentStartPattern.lastIndex = 0;
  64      do {
  65      } while (proceed());
  66      return output;
  67    };
  68    function proceed() {
  69      const stackDepth = stack.length;
  70      const next = nextToken();
  71      const [tokenType, blockName, attrs, startOffset, tokenLength] = next;
  72      const leadingHtmlStart = startOffset > offset ? offset : null;
  73      switch (tokenType) {
  74        case "no-more-tokens":
  75          if (0 === stackDepth) {
  76            addFreeform();
  77            return false;
  78          }
  79          implicitlyCloseAllOpenBlocks();
  80          return false;
  81        case "void-block":
  82          if (0 === stackDepth) {
  83            if (null !== leadingHtmlStart) {
  84              output.push(
  85                Freeform(
  86                  document.substr(
  87                    leadingHtmlStart,
  88                    startOffset - leadingHtmlStart
  89                  )
  90                )
  91              );
  92            }
  93            output.push(Block(blockName, attrs, [], "", []));
  94            offset = startOffset + tokenLength;
  95            return true;
  96          }
  97          addInnerBlock(
  98            Block(blockName, attrs, [], "", []),
  99            startOffset,
 100            tokenLength
 101          );
 102          offset = startOffset + tokenLength;
 103          return true;
 104        case "block-opener":
 105          stack.push(
 106            Frame(
 107              Block(blockName, attrs, [], "", []),
 108              startOffset,
 109              tokenLength,
 110              startOffset + tokenLength,
 111              leadingHtmlStart
 112            )
 113          );
 114          offset = startOffset + tokenLength;
 115          return true;
 116        case "block-closer":
 117          if (0 === stackDepth) {
 118            addFreeform();
 119            return false;
 120          }
 121          if (1 === stackDepth) {
 122            addBlockFromStack(startOffset);
 123            offset = startOffset + tokenLength;
 124            return true;
 125          }
 126          const stackTop = stack.pop();
 127          const html = document.substr(
 128            stackTop.prevOffset,
 129            startOffset - stackTop.prevOffset
 130          );
 131          if (html) {
 132            stackTop.block.innerHTML += html;
 133            stackTop.block.innerContent.push(html);
 134          }
 135          stackTop.prevOffset = startOffset + tokenLength;
 136          addInnerBlock(
 137            stackTop.block,
 138            stackTop.tokenStart,
 139            stackTop.tokenLength,
 140            startOffset + tokenLength
 141          );
 142          offset = startOffset + tokenLength;
 143          return true;
 144        default:
 145          addFreeform();
 146          return false;
 147      }
 148    }
 149    function parseJSON(input) {
 150      try {
 151        return JSON.parse(input);
 152      } catch {
 153        return null;
 154      }
 155    }
 156    function nextToken() {
 157      while (commentStartPattern.lastIndex < document.length) {
 158        const nextComment = commentStartPattern.exec(document);
 159        if (null === nextComment) {
 160          commentStartPattern.lastIndex = document.length;
 161          break;
 162        }
 163        onlyDashesCommentEndPattern.lastIndex = commentStartPattern.lastIndex;
 164        const abruptlyClosedEnding = onlyDashesCommentEndPattern.exec(document);
 165        if (null !== abruptlyClosedEnding) {
 166          commentStartPattern.lastIndex = onlyDashesCommentEndPattern.lastIndex;
 167          continue;
 168        }
 169        commentEndPattern.lastIndex = commentStartPattern.lastIndex;
 170        const commentEnd = commentEndPattern.exec(document);
 171        if (null === commentEnd) {
 172          commentStartPattern.lastIndex = document.length;
 173          break;
 174        }
 175        if ("!" === document[commentEndPattern.lastIndex - 2]) {
 176          commentStartPattern.lastIndex = commentEndPattern.lastIndex;
 177          continue;
 178        }
 179        if (commentEnd.index < commentStartPattern.lastIndex + 6) {
 180          commentStartPattern.lastIndex = commentEndPattern.lastIndex;
 181          continue;
 182        }
 183        delimiterPrefixPattern.lastIndex = commentStartPattern.lastIndex;
 184        const delimiterPrefix = delimiterPrefixPattern.exec(document);
 185        if (null === delimiterPrefix) {
 186          commentStartPattern.lastIndex = commentEndPattern.lastIndex;
 187          continue;
 188        }
 189        const isVoid = "/" === document[commentEnd.index - 1];
 190        const voidBackup = isVoid ? 1 : 0;
 191        const attributeAreaLength = commentEnd.index - voidBackup - delimiterPrefixPattern.lastIndex;
 192        const hasAttrs = attributeAreaLength > 0;
 193        if (hasAttrs && "{" !== document[delimiterPrefixPattern.lastIndex]) {
 194          commentStartPattern.lastIndex = commentEndPattern.lastIndex;
 195          continue;
 196        }
 197        const attributeArea = document.slice(
 198          delimiterPrefixPattern.lastIndex,
 199          commentEnd.index - voidBackup
 200        );
 201        attributeAreaClosingPattern.lastIndex = 1;
 202        const attributeClosing = hasAttrs ? attributeAreaClosingPattern.exec(attributeArea) : null;
 203        if (hasAttrs && null === attributeClosing) {
 204          commentStartPattern.lastIndex = commentEndPattern.lastIndex;
 205          continue;
 206        }
 207        const jsonAttributes = attributeClosing ? attributeArea.slice(0, attributeClosing.index + 1) : null;
 208        const startedAt = nextComment.index;
 209        commentStartPattern.lastIndex = commentEndPattern.lastIndex;
 210        const length = commentEndPattern.lastIndex - nextComment.index;
 211        const isCloser = "/" === delimiterPrefix[1];
 212        const namespace = delimiterPrefix[2] || "core/";
 213        const name = namespace + delimiterPrefix[3];
 214        const attrs = jsonAttributes ? parseJSON(jsonAttributes) : {};
 215        if (isCloser && (isVoid || hasAttrs)) {
 216        }
 217        if (isVoid) {
 218          return ["void-block", name, attrs, startedAt, length];
 219        }
 220        if (isCloser) {
 221          return ["block-closer", name, null, startedAt, length];
 222        }
 223        return ["block-opener", name, attrs, startedAt, length];
 224      }
 225      return ["no-more-tokens", "", null, 0, 0];
 226    }
 227    function addFreeform(rawLength) {
 228      const length = rawLength ? rawLength : document.length - offset;
 229      if (0 === length) {
 230        return;
 231      }
 232      output.push(Freeform(document.substr(offset, length)));
 233    }
 234    function addInnerBlock(block, tokenStart, tokenLength, lastOffset) {
 235      const parent = stack[stack.length - 1];
 236      parent.block.innerBlocks.push(block);
 237      const html = document.substr(
 238        parent.prevOffset,
 239        tokenStart - parent.prevOffset
 240      );
 241      if (html) {
 242        parent.block.innerHTML += html;
 243        parent.block.innerContent.push(html);
 244      }
 245      parent.block.innerContent.push(null);
 246      parent.prevOffset = lastOffset ? lastOffset : tokenStart + tokenLength;
 247    }
 248    function addBlockFromStack(endOffset) {
 249      const { block, leadingHtmlStart, prevOffset, tokenStart } = stack.pop();
 250      const html = endOffset ? document.substr(prevOffset, endOffset - prevOffset) : document.substr(prevOffset);
 251      if (html) {
 252        block.innerHTML += html;
 253        block.innerContent.push(html);
 254      }
 255      if (null !== leadingHtmlStart) {
 256        output.push(
 257          Freeform(
 258            document.substr(
 259              leadingHtmlStart,
 260              tokenStart - leadingHtmlStart
 261            )
 262          )
 263        );
 264      }
 265      output.push(block);
 266    }
 267    function implicitlyCloseAllOpenBlocks() {
 268      let lastClosedBlock = null;
 269      let lastOpenerAt = document.length;
 270      let lastLeadingHtml = null;
 271      while (stack.length > 0) {
 272        const stackTop = stack.pop();
 273        let html;
 274        const htmlLength = lastOpenerAt - stackTop.prevOffset;
 275        if (htmlLength > 0) {
 276          html = document.substr(stackTop.prevOffset, htmlLength);
 277          stackTop.block.innerHTML += html;
 278          stackTop.block.innerContent.push(html);
 279        }
 280        if (lastClosedBlock !== null) {
 281          stackTop.block.innerContent.push(null);
 282          stackTop.block.innerBlocks.push(lastClosedBlock);
 283        }
 284        lastClosedBlock = stackTop.block;
 285        lastOpenerAt = stackTop.tokenStart;
 286        lastLeadingHtml = stackTop.leadingHtmlStart;
 287      }
 288      if (null !== lastLeadingHtml && lastOpenerAt > lastLeadingHtml) {
 289        const html = document.substr(
 290          lastLeadingHtml,
 291          lastOpenerAt - lastLeadingHtml
 292        );
 293        output.push(Freeform(html));
 294      }
 295      if (lastClosedBlock !== null) {
 296        output.push(lastClosedBlock);
 297      }
 298      offset = document.length;
 299    }
 300    return __toCommonJS(index_exports);
 301  })();
 302  (window.wp ||= {}).blockSerializationDefaultParser = wp.blockSerializationDefaultParser;
 303  })();


Generated : Sat Nov 28 08:20:01 2026 Cross-referenced by PHPXref