[ Index ]

PHP Cross Reference of WordPress

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


Generated: Sun Sep 20 01:00:03 2020 Cross-referenced by PHPXref 0.7.1