[ 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 tokenizer = /<!--\s+(\/)?wp:([a-z][a-z0-9_-]*\/)?([a-z][a-z0-9_-]*)\s+({(?:(?=([^}]+|}+(?=})|(?!}\s+\/?-->)[^])*)\5|[^]*?)}\s+)?(\/)?-->/g;
  33    function Block(blockName, attrs, innerBlocks, innerHTML, innerContent) {
  34      return {
  35        blockName,
  36        attrs,
  37        innerBlocks,
  38        innerHTML,
  39        innerContent
  40      };
  41    }
  42    function Freeform(innerHTML) {
  43      return Block(null, {}, [], innerHTML, [innerHTML]);
  44    }
  45    function Frame(block, tokenStart, tokenLength, prevOffset, leadingHtmlStart) {
  46      return {
  47        block,
  48        tokenStart,
  49        tokenLength,
  50        prevOffset: prevOffset || tokenStart + tokenLength,
  51        leadingHtmlStart
  52      };
  53    }
  54    var parse = (doc) => {
  55      document = doc;
  56      offset = 0;
  57      output = [];
  58      stack = [];
  59      tokenizer.lastIndex = 0;
  60      do {
  61      } while (proceed());
  62      return output;
  63    };
  64    function proceed() {
  65      const stackDepth = stack.length;
  66      const next = nextToken();
  67      const [tokenType, blockName, attrs, startOffset, tokenLength] = next;
  68      const leadingHtmlStart = startOffset > offset ? offset : null;
  69      switch (tokenType) {
  70        case "no-more-tokens":
  71          if (0 === stackDepth) {
  72            addFreeform();
  73            return false;
  74          }
  75          if (1 === stackDepth) {
  76            addBlockFromStack();
  77            return false;
  78          }
  79          while (0 < stack.length) {
  80            addBlockFromStack();
  81          }
  82          return false;
  83        case "void-block":
  84          if (0 === stackDepth) {
  85            if (null !== leadingHtmlStart) {
  86              output.push(
  87                Freeform(
  88                  document.substr(
  89                    leadingHtmlStart,
  90                    startOffset - leadingHtmlStart
  91                  )
  92                )
  93              );
  94            }
  95            output.push(Block(blockName, attrs, [], "", []));
  96            offset = startOffset + tokenLength;
  97            return true;
  98          }
  99          addInnerBlock(
 100            Block(blockName, attrs, [], "", []),
 101            startOffset,
 102            tokenLength
 103          );
 104          offset = startOffset + tokenLength;
 105          return true;
 106        case "block-opener":
 107          stack.push(
 108            Frame(
 109              Block(blockName, attrs, [], "", []),
 110              startOffset,
 111              tokenLength,
 112              startOffset + tokenLength,
 113              leadingHtmlStart
 114            )
 115          );
 116          offset = startOffset + tokenLength;
 117          return true;
 118        case "block-closer":
 119          if (0 === stackDepth) {
 120            addFreeform();
 121            return false;
 122          }
 123          if (1 === stackDepth) {
 124            addBlockFromStack(startOffset);
 125            offset = startOffset + tokenLength;
 126            return true;
 127          }
 128          const stackTop = stack.pop();
 129          const html = document.substr(
 130            stackTop.prevOffset,
 131            startOffset - stackTop.prevOffset
 132          );
 133          stackTop.block.innerHTML += html;
 134          stackTop.block.innerContent.push(html);
 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      const matches = tokenizer.exec(document);
 158      if (null === matches) {
 159        return ["no-more-tokens", "", null, 0, 0];
 160      }
 161      const startedAt = matches.index;
 162      const [
 163        match,
 164        closerMatch,
 165        namespaceMatch,
 166        nameMatch,
 167        attrsMatch,
 168        ,
 169        voidMatch
 170      ] = matches;
 171      const length = match.length;
 172      const isCloser = !!closerMatch;
 173      const isVoid = !!voidMatch;
 174      const namespace = namespaceMatch || "core/";
 175      const name = namespace + nameMatch;
 176      const hasAttrs = !!attrsMatch;
 177      const attrs = hasAttrs ? parseJSON(attrsMatch) : {};
 178      if (isCloser && (isVoid || hasAttrs)) {
 179      }
 180      if (isVoid) {
 181        return ["void-block", name, attrs, startedAt, length];
 182      }
 183      if (isCloser) {
 184        return ["block-closer", name, null, startedAt, length];
 185      }
 186      return ["block-opener", name, attrs, startedAt, length];
 187    }
 188    function addFreeform(rawLength) {
 189      const length = rawLength ? rawLength : document.length - offset;
 190      if (0 === length) {
 191        return;
 192      }
 193      output.push(Freeform(document.substr(offset, length)));
 194    }
 195    function addInnerBlock(block, tokenStart, tokenLength, lastOffset) {
 196      const parent = stack[stack.length - 1];
 197      parent.block.innerBlocks.push(block);
 198      const html = document.substr(
 199        parent.prevOffset,
 200        tokenStart - parent.prevOffset
 201      );
 202      if (html) {
 203        parent.block.innerHTML += html;
 204        parent.block.innerContent.push(html);
 205      }
 206      parent.block.innerContent.push(null);
 207      parent.prevOffset = lastOffset ? lastOffset : tokenStart + tokenLength;
 208    }
 209    function addBlockFromStack(endOffset) {
 210      const { block, leadingHtmlStart, prevOffset, tokenStart } = stack.pop();
 211      const html = endOffset ? document.substr(prevOffset, endOffset - prevOffset) : document.substr(prevOffset);
 212      if (html) {
 213        block.innerHTML += html;
 214        block.innerContent.push(html);
 215      }
 216      if (null !== leadingHtmlStart) {
 217        output.push(
 218          Freeform(
 219            document.substr(
 220              leadingHtmlStart,
 221              tokenStart - leadingHtmlStart
 222            )
 223          )
 224        );
 225      }
 226      output.push(block);
 227    }
 228    return __toCommonJS(index_exports);
 229  })();
 230  (window.wp ||= {}).blockSerializationDefaultParser = wp.blockSerializationDefaultParser;
 231  })();


Generated : Fri Aug 7 08:20:20 2026 Cross-referenced by PHPXref