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


Generated: Wed Apr 8 01:00:03 2020 Cross-referenced by PHPXref 0.7.1