[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

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

   1  this["wp"] = this["wp"] || {}; this["wp"]["blockSerializationDefaultParser"] =
   2  /******/ (function(modules) { // webpackBootstrap
   3  /******/     // The module cache
   4  /******/     var installedModules = {};
   5  /******/
   6  /******/     // The require function
   7  /******/ 	function __webpack_require__(moduleId) {
   8  /******/
   9  /******/         // Check if module is in cache
  10  /******/         if(installedModules[moduleId]) {
  11  /******/             return installedModules[moduleId].exports;
  12  /******/         }
  13  /******/         // Create a new module (and put it into the cache)
  14  /******/         var module = installedModules[moduleId] = {
  15  /******/             i: moduleId,
  16  /******/             l: false,
  17  /******/             exports: {}
  18  /******/         };
  19  /******/
  20  /******/         // Execute the module function
  21  /******/         modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
  22  /******/
  23  /******/         // Flag the module as loaded
  24  /******/         module.l = true;
  25  /******/
  26  /******/         // Return the exports of the module
  27  /******/         return module.exports;
  28  /******/     }
  29  /******/
  30  /******/
  31  /******/     // expose the modules object (__webpack_modules__)
  32  /******/     __webpack_require__.m = modules;
  33  /******/
  34  /******/     // expose the module cache
  35  /******/     __webpack_require__.c = installedModules;
  36  /******/
  37  /******/     // define getter function for harmony exports
  38  /******/     __webpack_require__.d = function(exports, name, getter) {
  39  /******/         if(!__webpack_require__.o(exports, name)) {
  40  /******/             Object.defineProperty(exports, name, { enumerable: true, get: getter });
  41  /******/         }
  42  /******/     };
  43  /******/
  44  /******/     // define __esModule on exports
  45  /******/     __webpack_require__.r = function(exports) {
  46  /******/         if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
  47  /******/             Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
  48  /******/         }
  49  /******/         Object.defineProperty(exports, '__esModule', { value: true });
  50  /******/     };
  51  /******/
  52  /******/     // create a fake namespace object
  53  /******/     // mode & 1: value is a module id, require it
  54  /******/     // mode & 2: merge all properties of value into the ns
  55  /******/     // mode & 4: return value when already ns object
  56  /******/     // mode & 8|1: behave like require
  57  /******/     __webpack_require__.t = function(value, mode) {
  58  /******/         if(mode & 1) value = __webpack_require__(value);
  59  /******/         if(mode & 8) return value;
  60  /******/         if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
  61  /******/         var ns = Object.create(null);
  62  /******/         __webpack_require__.r(ns);
  63  /******/         Object.defineProperty(ns, 'default', { enumerable: true, value: value });
  64  /******/         if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
  65  /******/         return ns;
  66  /******/     };
  67  /******/
  68  /******/     // getDefaultExport function for compatibility with non-harmony modules
  69  /******/     __webpack_require__.n = function(module) {
  70  /******/         var getter = module && module.__esModule ?
  71  /******/ 			function getDefault() { return module['default']; } :
  72  /******/ 			function getModuleExports() { return module; };
  73  /******/         __webpack_require__.d(getter, 'a', getter);
  74  /******/         return getter;
  75  /******/     };
  76  /******/
  77  /******/     // Object.prototype.hasOwnProperty.call
  78  /******/     __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
  79  /******/
  80  /******/     // __webpack_public_path__
  81  /******/     __webpack_require__.p = "";
  82  /******/
  83  /******/
  84  /******/     // Load entry module and return exports
  85  /******/     return __webpack_require__(__webpack_require__.s = 319);
  86  /******/ })
  87  /************************************************************************/
  88  /******/ ({
  89  
  90  /***/ 12:
  91  /***/ (function(module, __webpack_exports__, __webpack_require__) {
  92  
  93  "use strict";
  94  
  95  // EXPORTS
  96  __webpack_require__.d(__webpack_exports__, "a", function() { return /* binding */ _slicedToArray; });
  97  
  98  // EXTERNAL MODULE: ./node_modules/@babel/runtime/helpers/esm/arrayWithHoles.js
  99  var arrayWithHoles = __webpack_require__(38);
 100  
 101  // CONCATENATED MODULE: ./node_modules/@babel/runtime/helpers/esm/iterableToArrayLimit.js
 102  function _iterableToArrayLimit(arr, i) {
 103    var _i = arr && (typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]);
 104  
 105    if (_i == null) return;
 106    var _arr = [];
 107    var _n = true;
 108    var _d = false;
 109  
 110    var _s, _e;
 111  
 112    try {
 113      for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) {
 114        _arr.push(_s.value);
 115  
 116        if (i && _arr.length === i) break;
 117      }
 118    } catch (err) {
 119      _d = true;
 120      _e = err;
 121    } finally {
 122      try {
 123        if (!_n && _i["return"] != null) _i["return"]();
 124      } finally {
 125        if (_d) throw _e;
 126      }
 127    }
 128  
 129    return _arr;
 130  }
 131  // EXTERNAL MODULE: ./node_modules/@babel/runtime/helpers/esm/unsupportedIterableToArray.js
 132  var unsupportedIterableToArray = __webpack_require__(28);
 133  
 134  // EXTERNAL MODULE: ./node_modules/@babel/runtime/helpers/esm/nonIterableRest.js
 135  var nonIterableRest = __webpack_require__(39);
 136  
 137  // CONCATENATED MODULE: ./node_modules/@babel/runtime/helpers/esm/slicedToArray.js
 138  
 139  
 140  
 141  
 142  function _slicedToArray(arr, i) {
 143    return Object(arrayWithHoles["a" /* default */])(arr) || _iterableToArrayLimit(arr, i) || Object(unsupportedIterableToArray["a" /* default */])(arr, i) || Object(nonIterableRest["a" /* default */])();
 144  }
 145  
 146  /***/ }),
 147  
 148  /***/ 24:
 149  /***/ (function(module, __webpack_exports__, __webpack_require__) {
 150  
 151  "use strict";
 152  /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return _arrayLikeToArray; });
 153  function _arrayLikeToArray(arr, len) {
 154    if (len == null || len > arr.length) len = arr.length;
 155  
 156    for (var i = 0, arr2 = new Array(len); i < len; i++) {
 157      arr2[i] = arr[i];
 158    }
 159  
 160    return arr2;
 161  }
 162  
 163  /***/ }),
 164  
 165  /***/ 28:
 166  /***/ (function(module, __webpack_exports__, __webpack_require__) {
 167  
 168  "use strict";
 169  /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return _unsupportedIterableToArray; });
 170  /* harmony import */ var _arrayLikeToArray_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(24);
 171  
 172  function _unsupportedIterableToArray(o, minLen) {
 173    if (!o) return;
 174    if (typeof o === "string") return Object(_arrayLikeToArray_js__WEBPACK_IMPORTED_MODULE_0__[/* default */ "a"])(o, minLen);
 175    var n = Object.prototype.toString.call(o).slice(8, -1);
 176    if (n === "Object" && o.constructor) n = o.constructor.name;
 177    if (n === "Map" || n === "Set") return Array.from(o);
 178    if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return Object(_arrayLikeToArray_js__WEBPACK_IMPORTED_MODULE_0__[/* default */ "a"])(o, minLen);
 179  }
 180  
 181  /***/ }),
 182  
 183  /***/ 319:
 184  /***/ (function(module, __webpack_exports__, __webpack_require__) {
 185  
 186  "use strict";
 187  __webpack_require__.r(__webpack_exports__);
 188  /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "parse", function() { return parse; });
 189  /* harmony import */ var _babel_runtime_helpers_esm_slicedToArray__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(12);
 190  
 191  var document;
 192  var offset;
 193  var output;
 194  var stack;
 195  /**
 196   * Matches block comment delimiters
 197   *
 198   * While most of this pattern is straightforward the attribute parsing
 199   * incorporates a tricks to make sure we don't choke on specific input
 200   *
 201   *  - since JavaScript has no possessive quantifier or atomic grouping
 202   *    we are emulating it with a trick
 203   *
 204   *    we want a possessive quantifier or atomic group to prevent backtracking
 205   *    on the `}`s should we fail to match the remainder of the pattern
 206   *
 207   *    we can emulate this with a positive lookahead and back reference
 208   *    (a++)*c === ((?=(a+))\1)*c
 209   *
 210   *    let's examine an example:
 211   *      - /(a+)*c/.test('aaaaaaaaaaaaad') fails after over 49,000 steps
 212   *      - /(a++)*c/.test('aaaaaaaaaaaaad') fails after 85 steps
 213   *      - /(?>a+)*c/.test('aaaaaaaaaaaaad') fails after 126 steps
 214   *
 215   *    this is because the possessive `++` and the atomic group `(?>)`
 216   *    tell the engine that all those `a`s belong together as a single group
 217   *    and so it won't split it up when stepping backwards to try and match
 218   *
 219   *    if we use /((?=(a+))\1)*c/ then we get the same behavior as the atomic group
 220   *    or possessive and prevent the backtracking because the `a+` is matched but
 221   *    not captured. thus, we find the long string of `a`s and remember it, then
 222   *    reference it as a whole unit inside our pattern
 223   *
 224   *    @see http://instanceof.me/post/52245507631/regex-emulate-atomic-grouping-with-lookahead
 225   *    @see http://blog.stevenlevithan.com/archives/mimic-atomic-groups
 226   *    @see https://javascript.info/regexp-infinite-backtracking-problem
 227   *
 228   *    once browsers reliably support atomic grouping or possessive
 229   *    quantifiers natively we should remove this trick and simplify
 230   *
 231   * @type {RegExp}
 232   *
 233   * @since 3.8.0
 234   * @since 4.6.1 added optimization to prevent backtracking on attribute parsing
 235   */
 236  
 237  var tokenizer = /<!--\s+(\/)?wp:([a-z][a-z0-9_-]*\/)?([a-z][a-z0-9_-]*)\s+({(?:(?=([^}]+|}+(?=})|(?!}\s+\/?-->)[^])*)\5|[^]*?)}\s+)?(\/)?-->/g;
 238  
 239  function Block(blockName, attrs, innerBlocks, innerHTML, innerContent) {
 240    return {
 241      blockName: blockName,
 242      attrs: attrs,
 243      innerBlocks: innerBlocks,
 244      innerHTML: innerHTML,
 245      innerContent: innerContent
 246    };
 247  }
 248  
 249  function Freeform(innerHTML) {
 250    return Block(null, {}, [], innerHTML, [innerHTML]);
 251  }
 252  
 253  function Frame(block, tokenStart, tokenLength, prevOffset, leadingHtmlStart) {
 254    return {
 255      block: block,
 256      tokenStart: tokenStart,
 257      tokenLength: tokenLength,
 258      prevOffset: prevOffset || tokenStart + tokenLength,
 259      leadingHtmlStart: leadingHtmlStart
 260    };
 261  }
 262  /**
 263   * Parser function, that converts input HTML into a block based structure.
 264   *
 265   * @param {string} doc The HTML document to parse.
 266   *
 267   * @example
 268   * Input post:
 269   * ```html
 270   * <!-- wp:columns {"columns":3} -->
 271   * <div class="wp-block-columns has-3-columns"><!-- wp:column -->
 272   * <div class="wp-block-column"><!-- wp:paragraph -->
 273   * <p>Left</p>
 274   * <!-- /wp:paragraph --></div>
 275   * <!-- /wp:column -->
 276   *
 277   * <!-- wp:column -->
 278   * <div class="wp-block-column"><!-- wp:paragraph -->
 279   * <p><strong>Middle</strong></p>
 280   * <!-- /wp:paragraph --></div>
 281   * <!-- /wp:column -->
 282   *
 283   * <!-- wp:column -->
 284   * <div class="wp-block-column"></div>
 285   * <!-- /wp:column --></div>
 286   * <!-- /wp:columns -->
 287   * ```
 288   *
 289   * Parsing code:
 290   * ```js
 291   * import { parse } from '@wordpress/block-serialization-default-parser';
 292   *
 293   * parse( post ) === [
 294   *     {
 295   *         blockName: "core/columns",
 296   *         attrs: {
 297   *             columns: 3
 298   *         },
 299   *         innerBlocks: [
 300   *             {
 301   *                 blockName: "core/column",
 302   *                 attrs: null,
 303   *                 innerBlocks: [
 304   *                     {
 305   *                         blockName: "core/paragraph",
 306   *                         attrs: null,
 307   *                         innerBlocks: [],
 308   *                         innerHTML: "\n<p>Left</p>\n"
 309   *                     }
 310   *                 ],
 311   *                 innerHTML: '\n<div class="wp-block-column"></div>\n'
 312   *             },
 313   *             {
 314   *                 blockName: "core/column",
 315   *                 attrs: null,
 316   *                 innerBlocks: [
 317   *                     {
 318   *                         blockName: "core/paragraph",
 319   *                         attrs: null,
 320   *                         innerBlocks: [],
 321   *                         innerHTML: "\n<p><strong>Middle</strong></p>\n"
 322   *                     }
 323   *                 ],
 324   *                 innerHTML: '\n<div class="wp-block-column"></div>\n'
 325   *             },
 326   *             {
 327   *                 blockName: "core/column",
 328   *                 attrs: null,
 329   *                 innerBlocks: [],
 330   *                 innerHTML: '\n<div class="wp-block-column"></div>\n'
 331   *             }
 332   *         ],
 333   *         innerHTML: '\n<div class="wp-block-columns has-3-columns">\n\n\n\n</div>\n'
 334   *     }
 335   * ];
 336   * ```
 337   * @return {Array} A block-based representation of the input HTML.
 338   */
 339  
 340  
 341  var parse = function parse(doc) {
 342    document = doc;
 343    offset = 0;
 344    output = [];
 345    stack = [];
 346    tokenizer.lastIndex = 0;
 347  
 348    do {// twiddle our thumbs
 349    } while (proceed());
 350  
 351    return output;
 352  };
 353  
 354  function proceed() {
 355    var next = nextToken();
 356  
 357    var _next = Object(_babel_runtime_helpers_esm_slicedToArray__WEBPACK_IMPORTED_MODULE_0__[/* default */ "a"])(next, 5),
 358        tokenType = _next[0],
 359        blockName = _next[1],
 360        attrs = _next[2],
 361        startOffset = _next[3],
 362        tokenLength = _next[4];
 363  
 364    var stackDepth = stack.length; // we may have some HTML soup before the next block
 365  
 366    var leadingHtmlStart = startOffset > offset ? offset : null;
 367  
 368    switch (tokenType) {
 369      case 'no-more-tokens':
 370        // if not in a block then flush output
 371        if (0 === stackDepth) {
 372          addFreeform();
 373          return false;
 374        } // Otherwise we have a problem
 375        // This is an error
 376        // we have options
 377        //  - treat it all as freeform text
 378        //  - assume an implicit closer (easiest when not nesting)
 379        // for the easy case we'll assume an implicit closer
 380  
 381  
 382        if (1 === stackDepth) {
 383          addBlockFromStack();
 384          return false;
 385        } // for the nested case where it's more difficult we'll
 386        // have to assume that multiple closers are missing
 387        // and so we'll collapse the whole stack piecewise
 388  
 389  
 390        while (0 < stack.length) {
 391          addBlockFromStack();
 392        }
 393  
 394        return false;
 395  
 396      case 'void-block':
 397        // easy case is if we stumbled upon a void block
 398        // in the top-level of the document
 399        if (0 === stackDepth) {
 400          if (null !== leadingHtmlStart) {
 401            output.push(Freeform(document.substr(leadingHtmlStart, startOffset - leadingHtmlStart)));
 402          }
 403  
 404          output.push(Block(blockName, attrs, [], '', []));
 405          offset = startOffset + tokenLength;
 406          return true;
 407        } // otherwise we found an inner block
 408  
 409  
 410        addInnerBlock(Block(blockName, attrs, [], '', []), startOffset, tokenLength);
 411        offset = startOffset + tokenLength;
 412        return true;
 413  
 414      case 'block-opener':
 415        // track all newly-opened blocks on the stack
 416        stack.push(Frame(Block(blockName, attrs, [], '', []), startOffset, tokenLength, startOffset + tokenLength, leadingHtmlStart));
 417        offset = startOffset + tokenLength;
 418        return true;
 419  
 420      case 'block-closer':
 421        // if we're missing an opener we're in trouble
 422        // This is an error
 423        if (0 === stackDepth) {
 424          // we have options
 425          //  - assume an implicit opener
 426          //  - assume _this_ is the opener
 427          //  - give up and close out the document
 428          addFreeform();
 429          return false;
 430        } // if we're not nesting then this is easy - close the block
 431  
 432  
 433        if (1 === stackDepth) {
 434          addBlockFromStack(startOffset);
 435          offset = startOffset + tokenLength;
 436          return true;
 437        } // otherwise we're nested and we have to close out the current
 438        // block and add it as a innerBlock to the parent
 439  
 440  
 441        var stackTop = stack.pop();
 442        var html = document.substr(stackTop.prevOffset, startOffset - stackTop.prevOffset);
 443        stackTop.block.innerHTML += html;
 444        stackTop.block.innerContent.push(html);
 445        stackTop.prevOffset = startOffset + tokenLength;
 446        addInnerBlock(stackTop.block, stackTop.tokenStart, stackTop.tokenLength, startOffset + tokenLength);
 447        offset = startOffset + tokenLength;
 448        return true;
 449  
 450      default:
 451        // This is an error
 452        addFreeform();
 453        return false;
 454    }
 455  }
 456  /**
 457   * Parse JSON if valid, otherwise return null
 458   *
 459   * Note that JSON coming from the block comment
 460   * delimiters is constrained to be an object
 461   * and cannot be things like `true` or `null`
 462   *
 463   * @param {string} input JSON input string to parse
 464   * @return {Object|null} parsed JSON if valid
 465   */
 466  
 467  
 468  function parseJSON(input) {
 469    try {
 470      return JSON.parse(input);
 471    } catch (e) {
 472      return null;
 473    }
 474  }
 475  
 476  function nextToken() {
 477    // aye the magic
 478    // we're using a single RegExp to tokenize the block comment delimiters
 479    // we're also using a trick here because the only difference between a
 480    // block opener and a block closer is the leading `/` before `wp:` (and
 481    // a closer has no attributes). we can trap them both and process the
 482    // match back in JavaScript to see which one it was.
 483    var matches = tokenizer.exec(document); // we have no more tokens
 484  
 485    if (null === matches) {
 486      return ['no-more-tokens'];
 487    }
 488  
 489    var startedAt = matches.index;
 490  
 491    var _matches = Object(_babel_runtime_helpers_esm_slicedToArray__WEBPACK_IMPORTED_MODULE_0__[/* default */ "a"])(matches, 7),
 492        match = _matches[0],
 493        closerMatch = _matches[1],
 494        namespaceMatch = _matches[2],
 495        nameMatch = _matches[3],
 496        attrsMatch
 497    /* internal/unused */
 498    = _matches[4],
 499        voidMatch = _matches[6];
 500  
 501    var length = match.length;
 502    var isCloser = !!closerMatch;
 503    var isVoid = !!voidMatch;
 504    var namespace = namespaceMatch || 'core/';
 505    var name = namespace + nameMatch;
 506    var hasAttrs = !!attrsMatch;
 507    var attrs = hasAttrs ? parseJSON(attrsMatch) : {}; // This state isn't allowed
 508    // This is an error
 509  
 510    if (isCloser && (isVoid || hasAttrs)) {// we can ignore them since they don't hurt anything
 511      // we may warn against this at some point or reject it
 512    }
 513  
 514    if (isVoid) {
 515      return ['void-block', name, attrs, startedAt, length];
 516    }
 517  
 518    if (isCloser) {
 519      return ['block-closer', name, null, startedAt, length];
 520    }
 521  
 522    return ['block-opener', name, attrs, startedAt, length];
 523  }
 524  
 525  function addFreeform(rawLength) {
 526    var length = rawLength ? rawLength : document.length - offset;
 527  
 528    if (0 === length) {
 529      return;
 530    }
 531  
 532    output.push(Freeform(document.substr(offset, length)));
 533  }
 534  
 535  function addInnerBlock(block, tokenStart, tokenLength, lastOffset) {
 536    var parent = stack[stack.length - 1];
 537    parent.block.innerBlocks.push(block);
 538    var html = document.substr(parent.prevOffset, tokenStart - parent.prevOffset);
 539  
 540    if (html) {
 541      parent.block.innerHTML += html;
 542      parent.block.innerContent.push(html);
 543    }
 544  
 545    parent.block.innerContent.push(null);
 546    parent.prevOffset = lastOffset ? lastOffset : tokenStart + tokenLength;
 547  }
 548  
 549  function addBlockFromStack(endOffset) {
 550    var _stack$pop = stack.pop(),
 551        block = _stack$pop.block,
 552        leadingHtmlStart = _stack$pop.leadingHtmlStart,
 553        prevOffset = _stack$pop.prevOffset,
 554        tokenStart = _stack$pop.tokenStart;
 555  
 556    var html = endOffset ? document.substr(prevOffset, endOffset - prevOffset) : document.substr(prevOffset);
 557  
 558    if (html) {
 559      block.innerHTML += html;
 560      block.innerContent.push(html);
 561    }
 562  
 563    if (null !== leadingHtmlStart) {
 564      output.push(Freeform(document.substr(leadingHtmlStart, tokenStart - leadingHtmlStart)));
 565    }
 566  
 567    output.push(block);
 568  }
 569  
 570  
 571  /***/ }),
 572  
 573  /***/ 38:
 574  /***/ (function(module, __webpack_exports__, __webpack_require__) {
 575  
 576  "use strict";
 577  /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return _arrayWithHoles; });
 578  function _arrayWithHoles(arr) {
 579    if (Array.isArray(arr)) return arr;
 580  }
 581  
 582  /***/ }),
 583  
 584  /***/ 39:
 585  /***/ (function(module, __webpack_exports__, __webpack_require__) {
 586  
 587  "use strict";
 588  /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return _nonIterableRest; });
 589  function _nonIterableRest() {
 590    throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
 591  }
 592  
 593  /***/ })
 594  
 595  /******/ });


Generated : Sun May 16 08:20:01 2021 Cross-referenced by PHPXref