index.cjs 101 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836
  1. 'use strict';
  2. function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
  3. function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
  4. function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
  5. function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
  6. function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
  7. function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
  8. function _get() { if (typeof Reflect !== "undefined" && Reflect.get) { _get = Reflect.get.bind(); } else { _get = function _get(target, property, receiver) { var base = _superPropBase(target, property); if (!base) return; var desc = Object.getOwnPropertyDescriptor(base, property); if (desc.get) { return desc.get.call(arguments.length < 3 ? target : receiver); } return desc.value; }; } return _get.apply(this, arguments); }
  9. function _superPropBase(object, property) { while (!Object.prototype.hasOwnProperty.call(object, property)) { object = _getPrototypeOf(object); if (object === null) break; } return object; }
  10. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
  11. function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
  12. function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
  13. function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
  14. function _wrapNativeSuper(Class) { var _cache = typeof Map === "function" ? new Map() : undefined; _wrapNativeSuper = function _wrapNativeSuper(Class) { if (Class === null || !_isNativeFunction(Class)) return Class; if (typeof Class !== "function") { throw new TypeError("Super expression must either be null or a function"); } if (typeof _cache !== "undefined") { if (_cache.has(Class)) return _cache.get(Class); _cache.set(Class, Wrapper); } function Wrapper() { return _construct(Class, arguments, _getPrototypeOf(this).constructor); } Wrapper.prototype = Object.create(Class.prototype, { constructor: { value: Wrapper, enumerable: false, writable: true, configurable: true } }); return _setPrototypeOf(Wrapper, Class); }; return _wrapNativeSuper(Class); }
  15. function _construct(Parent, args, Class) { if (_isNativeReflectConstruct()) { _construct = Reflect.construct.bind(); } else { _construct = function _construct(Parent, args, Class) { var a = [null]; a.push.apply(a, args); var Constructor = Function.bind.apply(Parent, a); var instance = new Constructor(); if (Class) _setPrototypeOf(instance, Class.prototype); return instance; }; } return _construct.apply(null, arguments); }
  16. function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
  17. function _isNativeFunction(fn) { try { return Function.toString.call(fn).indexOf("[native code]") !== -1; } catch (e) { return typeof fn === "function"; } }
  18. function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
  19. function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
  20. function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
  21. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  22. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
  23. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
  24. function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
  25. function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
  26. var OrderedMap = require('orderedmap');
  27. function _findDiffStart(a, b, pos) {
  28. for (var i = 0;; i++) {
  29. if (i == a.childCount || i == b.childCount) return a.childCount == b.childCount ? null : pos;
  30. var childA = a.child(i),
  31. childB = b.child(i);
  32. if (childA == childB) {
  33. pos += childA.nodeSize;
  34. continue;
  35. }
  36. if (!childA.sameMarkup(childB)) return pos;
  37. if (childA.isText && childA.text != childB.text) {
  38. for (var j = 0; childA.text[j] == childB.text[j]; j++) pos++;
  39. return pos;
  40. }
  41. if (childA.content.size || childB.content.size) {
  42. var inner = _findDiffStart(childA.content, childB.content, pos + 1);
  43. if (inner != null) return inner;
  44. }
  45. pos += childA.nodeSize;
  46. }
  47. }
  48. function _findDiffEnd(a, b, posA, posB) {
  49. for (var iA = a.childCount, iB = b.childCount;;) {
  50. if (iA == 0 || iB == 0) return iA == iB ? null : {
  51. a: posA,
  52. b: posB
  53. };
  54. var childA = a.child(--iA),
  55. childB = b.child(--iB),
  56. size = childA.nodeSize;
  57. if (childA == childB) {
  58. posA -= size;
  59. posB -= size;
  60. continue;
  61. }
  62. if (!childA.sameMarkup(childB)) return {
  63. a: posA,
  64. b: posB
  65. };
  66. if (childA.isText && childA.text != childB.text) {
  67. var same = 0,
  68. minSize = Math.min(childA.text.length, childB.text.length);
  69. while (same < minSize && childA.text[childA.text.length - same - 1] == childB.text[childB.text.length - same - 1]) {
  70. same++;
  71. posA--;
  72. posB--;
  73. }
  74. return {
  75. a: posA,
  76. b: posB
  77. };
  78. }
  79. if (childA.content.size || childB.content.size) {
  80. var inner = _findDiffEnd(childA.content, childB.content, posA - 1, posB - 1);
  81. if (inner) return inner;
  82. }
  83. posA -= size;
  84. posB -= size;
  85. }
  86. }
  87. var Fragment = function () {
  88. function Fragment(content, size) {
  89. _classCallCheck(this, Fragment);
  90. this.content = content;
  91. this.size = size || 0;
  92. if (size == null) for (var i = 0; i < content.length; i++) this.size += content[i].nodeSize;
  93. }
  94. _createClass(Fragment, [{
  95. key: "nodesBetween",
  96. value: function nodesBetween(from, to, f) {
  97. var nodeStart = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 0;
  98. var parent = arguments.length > 4 ? arguments[4] : undefined;
  99. for (var i = 0, pos = 0; pos < to; i++) {
  100. var child = this.content[i],
  101. end = pos + child.nodeSize;
  102. if (end > from && f(child, nodeStart + pos, parent || null, i) !== false && child.content.size) {
  103. var start = pos + 1;
  104. child.nodesBetween(Math.max(0, from - start), Math.min(child.content.size, to - start), f, nodeStart + start);
  105. }
  106. pos = end;
  107. }
  108. }
  109. }, {
  110. key: "descendants",
  111. value: function descendants(f) {
  112. this.nodesBetween(0, this.size, f);
  113. }
  114. }, {
  115. key: "textBetween",
  116. value: function textBetween(from, to, blockSeparator, leafText) {
  117. var text = "",
  118. first = true;
  119. this.nodesBetween(from, to, function (node, pos) {
  120. var nodeText = node.isText ? node.text.slice(Math.max(from, pos) - pos, to - pos) : !node.isLeaf ? "" : leafText ? typeof leafText === "function" ? leafText(node) : leafText : node.type.spec.leafText ? node.type.spec.leafText(node) : "";
  121. if (node.isBlock && (node.isLeaf && nodeText || node.isTextblock) && blockSeparator) {
  122. if (first) first = false;else text += blockSeparator;
  123. }
  124. text += nodeText;
  125. }, 0);
  126. return text;
  127. }
  128. }, {
  129. key: "append",
  130. value: function append(other) {
  131. if (!other.size) return this;
  132. if (!this.size) return other;
  133. var last = this.lastChild,
  134. first = other.firstChild,
  135. content = this.content.slice(),
  136. i = 0;
  137. if (last.isText && last.sameMarkup(first)) {
  138. content[content.length - 1] = last.withText(last.text + first.text);
  139. i = 1;
  140. }
  141. for (; i < other.content.length; i++) content.push(other.content[i]);
  142. return new Fragment(content, this.size + other.size);
  143. }
  144. }, {
  145. key: "cut",
  146. value: function cut(from) {
  147. var to = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.size;
  148. if (from == 0 && to == this.size) return this;
  149. var result = [],
  150. size = 0;
  151. if (to > from) for (var i = 0, pos = 0; pos < to; i++) {
  152. var child = this.content[i],
  153. end = pos + child.nodeSize;
  154. if (end > from) {
  155. if (pos < from || end > to) {
  156. if (child.isText) child = child.cut(Math.max(0, from - pos), Math.min(child.text.length, to - pos));else child = child.cut(Math.max(0, from - pos - 1), Math.min(child.content.size, to - pos - 1));
  157. }
  158. result.push(child);
  159. size += child.nodeSize;
  160. }
  161. pos = end;
  162. }
  163. return new Fragment(result, size);
  164. }
  165. }, {
  166. key: "cutByIndex",
  167. value: function cutByIndex(from, to) {
  168. if (from == to) return Fragment.empty;
  169. if (from == 0 && to == this.content.length) return this;
  170. return new Fragment(this.content.slice(from, to));
  171. }
  172. }, {
  173. key: "replaceChild",
  174. value: function replaceChild(index, node) {
  175. var current = this.content[index];
  176. if (current == node) return this;
  177. var copy = this.content.slice();
  178. var size = this.size + node.nodeSize - current.nodeSize;
  179. copy[index] = node;
  180. return new Fragment(copy, size);
  181. }
  182. }, {
  183. key: "addToStart",
  184. value: function addToStart(node) {
  185. return new Fragment([node].concat(this.content), this.size + node.nodeSize);
  186. }
  187. }, {
  188. key: "addToEnd",
  189. value: function addToEnd(node) {
  190. return new Fragment(this.content.concat(node), this.size + node.nodeSize);
  191. }
  192. }, {
  193. key: "eq",
  194. value: function eq(other) {
  195. if (this.content.length != other.content.length) return false;
  196. for (var i = 0; i < this.content.length; i++) if (!this.content[i].eq(other.content[i])) return false;
  197. return true;
  198. }
  199. }, {
  200. key: "firstChild",
  201. get: function get() {
  202. return this.content.length ? this.content[0] : null;
  203. }
  204. }, {
  205. key: "lastChild",
  206. get: function get() {
  207. return this.content.length ? this.content[this.content.length - 1] : null;
  208. }
  209. }, {
  210. key: "childCount",
  211. get: function get() {
  212. return this.content.length;
  213. }
  214. }, {
  215. key: "child",
  216. value: function child(index) {
  217. var found = this.content[index];
  218. if (!found) throw new RangeError("Index " + index + " out of range for " + this);
  219. return found;
  220. }
  221. }, {
  222. key: "maybeChild",
  223. value: function maybeChild(index) {
  224. return this.content[index] || null;
  225. }
  226. }, {
  227. key: "forEach",
  228. value: function forEach(f) {
  229. for (var i = 0, p = 0; i < this.content.length; i++) {
  230. var child = this.content[i];
  231. f(child, p, i);
  232. p += child.nodeSize;
  233. }
  234. }
  235. }, {
  236. key: "findDiffStart",
  237. value: function findDiffStart(other) {
  238. var pos = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
  239. return _findDiffStart(this, other, pos);
  240. }
  241. }, {
  242. key: "findDiffEnd",
  243. value: function findDiffEnd(other) {
  244. var pos = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.size;
  245. var otherPos = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : other.size;
  246. return _findDiffEnd(this, other, pos, otherPos);
  247. }
  248. }, {
  249. key: "findIndex",
  250. value: function findIndex(pos) {
  251. var round = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : -1;
  252. if (pos == 0) return retIndex(0, pos);
  253. if (pos == this.size) return retIndex(this.content.length, pos);
  254. if (pos > this.size || pos < 0) throw new RangeError("Position ".concat(pos, " outside of fragment (").concat(this, ")"));
  255. for (var i = 0, curPos = 0;; i++) {
  256. var cur = this.child(i),
  257. end = curPos + cur.nodeSize;
  258. if (end >= pos) {
  259. if (end == pos || round > 0) return retIndex(i + 1, end);
  260. return retIndex(i, curPos);
  261. }
  262. curPos = end;
  263. }
  264. }
  265. }, {
  266. key: "toString",
  267. value: function toString() {
  268. return "<" + this.toStringInner() + ">";
  269. }
  270. }, {
  271. key: "toStringInner",
  272. value: function toStringInner() {
  273. return this.content.join(", ");
  274. }
  275. }, {
  276. key: "toJSON",
  277. value: function toJSON() {
  278. return this.content.length ? this.content.map(function (n) {
  279. return n.toJSON();
  280. }) : null;
  281. }
  282. }], [{
  283. key: "fromJSON",
  284. value: function fromJSON(schema, value) {
  285. if (!value) return Fragment.empty;
  286. if (!Array.isArray(value)) throw new RangeError("Invalid input for Fragment.fromJSON");
  287. return new Fragment(value.map(schema.nodeFromJSON));
  288. }
  289. }, {
  290. key: "fromArray",
  291. value: function fromArray(array) {
  292. if (!array.length) return Fragment.empty;
  293. var joined,
  294. size = 0;
  295. for (var i = 0; i < array.length; i++) {
  296. var node = array[i];
  297. size += node.nodeSize;
  298. if (i && node.isText && array[i - 1].sameMarkup(node)) {
  299. if (!joined) joined = array.slice(0, i);
  300. joined[joined.length - 1] = node.withText(joined[joined.length - 1].text + node.text);
  301. } else if (joined) {
  302. joined.push(node);
  303. }
  304. }
  305. return new Fragment(joined || array, size);
  306. }
  307. }, {
  308. key: "from",
  309. value: function from(nodes) {
  310. if (!nodes) return Fragment.empty;
  311. if (nodes instanceof Fragment) return nodes;
  312. if (Array.isArray(nodes)) return this.fromArray(nodes);
  313. if (nodes.attrs) return new Fragment([nodes], nodes.nodeSize);
  314. throw new RangeError("Can not convert " + nodes + " to a Fragment" + (nodes.nodesBetween ? " (looks like multiple versions of prosemirror-model were loaded)" : ""));
  315. }
  316. }]);
  317. return Fragment;
  318. }();
  319. Fragment.empty = new Fragment([], 0);
  320. var found = {
  321. index: 0,
  322. offset: 0
  323. };
  324. function retIndex(index, offset) {
  325. found.index = index;
  326. found.offset = offset;
  327. return found;
  328. }
  329. function compareDeep(a, b) {
  330. if (a === b) return true;
  331. if (!(a && _typeof(a) == "object") || !(b && _typeof(b) == "object")) return false;
  332. var array = Array.isArray(a);
  333. if (Array.isArray(b) != array) return false;
  334. if (array) {
  335. if (a.length != b.length) return false;
  336. for (var i = 0; i < a.length; i++) if (!compareDeep(a[i], b[i])) return false;
  337. } else {
  338. for (var p in a) if (!(p in b) || !compareDeep(a[p], b[p])) return false;
  339. for (var _p in b) if (!(_p in a)) return false;
  340. }
  341. return true;
  342. }
  343. var Mark = function () {
  344. function Mark(type, attrs) {
  345. _classCallCheck(this, Mark);
  346. this.type = type;
  347. this.attrs = attrs;
  348. }
  349. _createClass(Mark, [{
  350. key: "addToSet",
  351. value: function addToSet(set) {
  352. var copy,
  353. placed = false;
  354. for (var i = 0; i < set.length; i++) {
  355. var other = set[i];
  356. if (this.eq(other)) return set;
  357. if (this.type.excludes(other.type)) {
  358. if (!copy) copy = set.slice(0, i);
  359. } else if (other.type.excludes(this.type)) {
  360. return set;
  361. } else {
  362. if (!placed && other.type.rank > this.type.rank) {
  363. if (!copy) copy = set.slice(0, i);
  364. copy.push(this);
  365. placed = true;
  366. }
  367. if (copy) copy.push(other);
  368. }
  369. }
  370. if (!copy) copy = set.slice();
  371. if (!placed) copy.push(this);
  372. return copy;
  373. }
  374. }, {
  375. key: "removeFromSet",
  376. value: function removeFromSet(set) {
  377. for (var i = 0; i < set.length; i++) if (this.eq(set[i])) return set.slice(0, i).concat(set.slice(i + 1));
  378. return set;
  379. }
  380. }, {
  381. key: "isInSet",
  382. value: function isInSet(set) {
  383. for (var i = 0; i < set.length; i++) if (this.eq(set[i])) return true;
  384. return false;
  385. }
  386. }, {
  387. key: "eq",
  388. value: function eq(other) {
  389. return this == other || this.type == other.type && compareDeep(this.attrs, other.attrs);
  390. }
  391. }, {
  392. key: "toJSON",
  393. value: function toJSON() {
  394. var obj = {
  395. type: this.type.name
  396. };
  397. for (var _ in this.attrs) {
  398. obj.attrs = this.attrs;
  399. break;
  400. }
  401. return obj;
  402. }
  403. }], [{
  404. key: "fromJSON",
  405. value: function fromJSON(schema, json) {
  406. if (!json) throw new RangeError("Invalid input for Mark.fromJSON");
  407. var type = schema.marks[json.type];
  408. if (!type) throw new RangeError("There is no mark type ".concat(json.type, " in this schema"));
  409. return type.create(json.attrs);
  410. }
  411. }, {
  412. key: "sameSet",
  413. value: function sameSet(a, b) {
  414. if (a == b) return true;
  415. if (a.length != b.length) return false;
  416. for (var i = 0; i < a.length; i++) if (!a[i].eq(b[i])) return false;
  417. return true;
  418. }
  419. }, {
  420. key: "setFrom",
  421. value: function setFrom(marks) {
  422. if (!marks || Array.isArray(marks) && marks.length == 0) return Mark.none;
  423. if (marks instanceof Mark) return [marks];
  424. var copy = marks.slice();
  425. copy.sort(function (a, b) {
  426. return a.type.rank - b.type.rank;
  427. });
  428. return copy;
  429. }
  430. }]);
  431. return Mark;
  432. }();
  433. Mark.none = [];
  434. var ReplaceError = function (_Error) {
  435. _inherits(ReplaceError, _Error);
  436. var _super = _createSuper(ReplaceError);
  437. function ReplaceError() {
  438. _classCallCheck(this, ReplaceError);
  439. return _super.apply(this, arguments);
  440. }
  441. return _createClass(ReplaceError);
  442. }(_wrapNativeSuper(Error));
  443. var Slice = function () {
  444. function Slice(content, openStart, openEnd) {
  445. _classCallCheck(this, Slice);
  446. this.content = content;
  447. this.openStart = openStart;
  448. this.openEnd = openEnd;
  449. }
  450. _createClass(Slice, [{
  451. key: "size",
  452. get: function get() {
  453. return this.content.size - this.openStart - this.openEnd;
  454. }
  455. }, {
  456. key: "insertAt",
  457. value: function insertAt(pos, fragment) {
  458. var content = insertInto(this.content, pos + this.openStart, fragment);
  459. return content && new Slice(content, this.openStart, this.openEnd);
  460. }
  461. }, {
  462. key: "removeBetween",
  463. value: function removeBetween(from, to) {
  464. return new Slice(removeRange(this.content, from + this.openStart, to + this.openStart), this.openStart, this.openEnd);
  465. }
  466. }, {
  467. key: "eq",
  468. value: function eq(other) {
  469. return this.content.eq(other.content) && this.openStart == other.openStart && this.openEnd == other.openEnd;
  470. }
  471. }, {
  472. key: "toString",
  473. value: function toString() {
  474. return this.content + "(" + this.openStart + "," + this.openEnd + ")";
  475. }
  476. }, {
  477. key: "toJSON",
  478. value: function toJSON() {
  479. if (!this.content.size) return null;
  480. var json = {
  481. content: this.content.toJSON()
  482. };
  483. if (this.openStart > 0) json.openStart = this.openStart;
  484. if (this.openEnd > 0) json.openEnd = this.openEnd;
  485. return json;
  486. }
  487. }], [{
  488. key: "fromJSON",
  489. value: function fromJSON(schema, json) {
  490. if (!json) return Slice.empty;
  491. var openStart = json.openStart || 0,
  492. openEnd = json.openEnd || 0;
  493. if (typeof openStart != "number" || typeof openEnd != "number") throw new RangeError("Invalid input for Slice.fromJSON");
  494. return new Slice(Fragment.fromJSON(schema, json.content), openStart, openEnd);
  495. }
  496. }, {
  497. key: "maxOpen",
  498. value: function maxOpen(fragment) {
  499. var openIsolating = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
  500. var openStart = 0,
  501. openEnd = 0;
  502. for (var n = fragment.firstChild; n && !n.isLeaf && (openIsolating || !n.type.spec.isolating); n = n.firstChild) openStart++;
  503. for (var _n = fragment.lastChild; _n && !_n.isLeaf && (openIsolating || !_n.type.spec.isolating); _n = _n.lastChild) openEnd++;
  504. return new Slice(fragment, openStart, openEnd);
  505. }
  506. }]);
  507. return Slice;
  508. }();
  509. Slice.empty = new Slice(Fragment.empty, 0, 0);
  510. function removeRange(content, from, to) {
  511. var _content$findIndex = content.findIndex(from),
  512. index = _content$findIndex.index,
  513. offset = _content$findIndex.offset,
  514. child = content.maybeChild(index);
  515. var _content$findIndex2 = content.findIndex(to),
  516. indexTo = _content$findIndex2.index,
  517. offsetTo = _content$findIndex2.offset;
  518. if (offset == from || child.isText) {
  519. if (offsetTo != to && !content.child(indexTo).isText) throw new RangeError("Removing non-flat range");
  520. return content.cut(0, from).append(content.cut(to));
  521. }
  522. if (index != indexTo) throw new RangeError("Removing non-flat range");
  523. return content.replaceChild(index, child.copy(removeRange(child.content, from - offset - 1, to - offset - 1)));
  524. }
  525. function insertInto(content, dist, insert, parent) {
  526. var _content$findIndex3 = content.findIndex(dist),
  527. index = _content$findIndex3.index,
  528. offset = _content$findIndex3.offset,
  529. child = content.maybeChild(index);
  530. if (offset == dist || child.isText) {
  531. if (parent && !parent.canReplace(index, index, insert)) return null;
  532. return content.cut(0, dist).append(insert).append(content.cut(dist));
  533. }
  534. var inner = insertInto(child.content, dist - offset - 1, insert);
  535. return inner && content.replaceChild(index, child.copy(inner));
  536. }
  537. function _replace($from, $to, slice) {
  538. if (slice.openStart > $from.depth) throw new ReplaceError("Inserted content deeper than insertion position");
  539. if ($from.depth - slice.openStart != $to.depth - slice.openEnd) throw new ReplaceError("Inconsistent open depths");
  540. return replaceOuter($from, $to, slice, 0);
  541. }
  542. function replaceOuter($from, $to, slice, depth) {
  543. var index = $from.index(depth),
  544. node = $from.node(depth);
  545. if (index == $to.index(depth) && depth < $from.depth - slice.openStart) {
  546. var inner = replaceOuter($from, $to, slice, depth + 1);
  547. return node.copy(node.content.replaceChild(index, inner));
  548. } else if (!slice.content.size) {
  549. return close(node, replaceTwoWay($from, $to, depth));
  550. } else if (!slice.openStart && !slice.openEnd && $from.depth == depth && $to.depth == depth) {
  551. var parent = $from.parent,
  552. content = parent.content;
  553. return close(parent, content.cut(0, $from.parentOffset).append(slice.content).append(content.cut($to.parentOffset)));
  554. } else {
  555. var _prepareSliceForRepla = prepareSliceForReplace(slice, $from),
  556. start = _prepareSliceForRepla.start,
  557. end = _prepareSliceForRepla.end;
  558. return close(node, replaceThreeWay($from, start, end, $to, depth));
  559. }
  560. }
  561. function checkJoin(main, sub) {
  562. if (!sub.type.compatibleContent(main.type)) throw new ReplaceError("Cannot join " + sub.type.name + " onto " + main.type.name);
  563. }
  564. function joinable($before, $after, depth) {
  565. var node = $before.node(depth);
  566. checkJoin(node, $after.node(depth));
  567. return node;
  568. }
  569. function addNode(child, target) {
  570. var last = target.length - 1;
  571. if (last >= 0 && child.isText && child.sameMarkup(target[last])) target[last] = child.withText(target[last].text + child.text);else target.push(child);
  572. }
  573. function addRange($start, $end, depth, target) {
  574. var node = ($end || $start).node(depth);
  575. var startIndex = 0,
  576. endIndex = $end ? $end.index(depth) : node.childCount;
  577. if ($start) {
  578. startIndex = $start.index(depth);
  579. if ($start.depth > depth) {
  580. startIndex++;
  581. } else if ($start.textOffset) {
  582. addNode($start.nodeAfter, target);
  583. startIndex++;
  584. }
  585. }
  586. for (var i = startIndex; i < endIndex; i++) addNode(node.child(i), target);
  587. if ($end && $end.depth == depth && $end.textOffset) addNode($end.nodeBefore, target);
  588. }
  589. function close(node, content) {
  590. node.type.checkContent(content);
  591. return node.copy(content);
  592. }
  593. function replaceThreeWay($from, $start, $end, $to, depth) {
  594. var openStart = $from.depth > depth && joinable($from, $start, depth + 1);
  595. var openEnd = $to.depth > depth && joinable($end, $to, depth + 1);
  596. var content = [];
  597. addRange(null, $from, depth, content);
  598. if (openStart && openEnd && $start.index(depth) == $end.index(depth)) {
  599. checkJoin(openStart, openEnd);
  600. addNode(close(openStart, replaceThreeWay($from, $start, $end, $to, depth + 1)), content);
  601. } else {
  602. if (openStart) addNode(close(openStart, replaceTwoWay($from, $start, depth + 1)), content);
  603. addRange($start, $end, depth, content);
  604. if (openEnd) addNode(close(openEnd, replaceTwoWay($end, $to, depth + 1)), content);
  605. }
  606. addRange($to, null, depth, content);
  607. return new Fragment(content);
  608. }
  609. function replaceTwoWay($from, $to, depth) {
  610. var content = [];
  611. addRange(null, $from, depth, content);
  612. if ($from.depth > depth) {
  613. var type = joinable($from, $to, depth + 1);
  614. addNode(close(type, replaceTwoWay($from, $to, depth + 1)), content);
  615. }
  616. addRange($to, null, depth, content);
  617. return new Fragment(content);
  618. }
  619. function prepareSliceForReplace(slice, $along) {
  620. var extra = $along.depth - slice.openStart,
  621. parent = $along.node(extra);
  622. var node = parent.copy(slice.content);
  623. for (var i = extra - 1; i >= 0; i--) node = $along.node(i).copy(Fragment.from(node));
  624. return {
  625. start: node.resolveNoCache(slice.openStart + extra),
  626. end: node.resolveNoCache(node.content.size - slice.openEnd - extra)
  627. };
  628. }
  629. var ResolvedPos = function () {
  630. function ResolvedPos(pos, path, parentOffset) {
  631. _classCallCheck(this, ResolvedPos);
  632. this.pos = pos;
  633. this.path = path;
  634. this.parentOffset = parentOffset;
  635. this.depth = path.length / 3 - 1;
  636. }
  637. _createClass(ResolvedPos, [{
  638. key: "resolveDepth",
  639. value: function resolveDepth(val) {
  640. if (val == null) return this.depth;
  641. if (val < 0) return this.depth + val;
  642. return val;
  643. }
  644. }, {
  645. key: "parent",
  646. get: function get() {
  647. return this.node(this.depth);
  648. }
  649. }, {
  650. key: "doc",
  651. get: function get() {
  652. return this.node(0);
  653. }
  654. }, {
  655. key: "node",
  656. value: function node(depth) {
  657. return this.path[this.resolveDepth(depth) * 3];
  658. }
  659. }, {
  660. key: "index",
  661. value: function index(depth) {
  662. return this.path[this.resolveDepth(depth) * 3 + 1];
  663. }
  664. }, {
  665. key: "indexAfter",
  666. value: function indexAfter(depth) {
  667. depth = this.resolveDepth(depth);
  668. return this.index(depth) + (depth == this.depth && !this.textOffset ? 0 : 1);
  669. }
  670. }, {
  671. key: "start",
  672. value: function start(depth) {
  673. depth = this.resolveDepth(depth);
  674. return depth == 0 ? 0 : this.path[depth * 3 - 1] + 1;
  675. }
  676. }, {
  677. key: "end",
  678. value: function end(depth) {
  679. depth = this.resolveDepth(depth);
  680. return this.start(depth) + this.node(depth).content.size;
  681. }
  682. }, {
  683. key: "before",
  684. value: function before(depth) {
  685. depth = this.resolveDepth(depth);
  686. if (!depth) throw new RangeError("There is no position before the top-level node");
  687. return depth == this.depth + 1 ? this.pos : this.path[depth * 3 - 1];
  688. }
  689. }, {
  690. key: "after",
  691. value: function after(depth) {
  692. depth = this.resolveDepth(depth);
  693. if (!depth) throw new RangeError("There is no position after the top-level node");
  694. return depth == this.depth + 1 ? this.pos : this.path[depth * 3 - 1] + this.path[depth * 3].nodeSize;
  695. }
  696. }, {
  697. key: "textOffset",
  698. get: function get() {
  699. return this.pos - this.path[this.path.length - 1];
  700. }
  701. }, {
  702. key: "nodeAfter",
  703. get: function get() {
  704. var parent = this.parent,
  705. index = this.index(this.depth);
  706. if (index == parent.childCount) return null;
  707. var dOff = this.pos - this.path[this.path.length - 1],
  708. child = parent.child(index);
  709. return dOff ? parent.child(index).cut(dOff) : child;
  710. }
  711. }, {
  712. key: "nodeBefore",
  713. get: function get() {
  714. var index = this.index(this.depth);
  715. var dOff = this.pos - this.path[this.path.length - 1];
  716. if (dOff) return this.parent.child(index).cut(0, dOff);
  717. return index == 0 ? null : this.parent.child(index - 1);
  718. }
  719. }, {
  720. key: "posAtIndex",
  721. value: function posAtIndex(index, depth) {
  722. depth = this.resolveDepth(depth);
  723. var node = this.path[depth * 3],
  724. pos = depth == 0 ? 0 : this.path[depth * 3 - 1] + 1;
  725. for (var i = 0; i < index; i++) pos += node.child(i).nodeSize;
  726. return pos;
  727. }
  728. }, {
  729. key: "marks",
  730. value: function marks() {
  731. var parent = this.parent,
  732. index = this.index();
  733. if (parent.content.size == 0) return Mark.none;
  734. if (this.textOffset) return parent.child(index).marks;
  735. var main = parent.maybeChild(index - 1),
  736. other = parent.maybeChild(index);
  737. if (!main) {
  738. var tmp = main;
  739. main = other;
  740. other = tmp;
  741. }
  742. var marks = main.marks;
  743. for (var i = 0; i < marks.length; i++) if (marks[i].type.spec.inclusive === false && (!other || !marks[i].isInSet(other.marks))) marks = marks[i--].removeFromSet(marks);
  744. return marks;
  745. }
  746. }, {
  747. key: "marksAcross",
  748. value: function marksAcross($end) {
  749. var after = this.parent.maybeChild(this.index());
  750. if (!after || !after.isInline) return null;
  751. var marks = after.marks,
  752. next = $end.parent.maybeChild($end.index());
  753. for (var i = 0; i < marks.length; i++) if (marks[i].type.spec.inclusive === false && (!next || !marks[i].isInSet(next.marks))) marks = marks[i--].removeFromSet(marks);
  754. return marks;
  755. }
  756. }, {
  757. key: "sharedDepth",
  758. value: function sharedDepth(pos) {
  759. for (var depth = this.depth; depth > 0; depth--) if (this.start(depth) <= pos && this.end(depth) >= pos) return depth;
  760. return 0;
  761. }
  762. }, {
  763. key: "blockRange",
  764. value: function blockRange() {
  765. var other = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this;
  766. var pred = arguments.length > 1 ? arguments[1] : undefined;
  767. if (other.pos < this.pos) return other.blockRange(this);
  768. for (var d = this.depth - (this.parent.inlineContent || this.pos == other.pos ? 1 : 0); d >= 0; d--) if (other.pos <= this.end(d) && (!pred || pred(this.node(d)))) return new NodeRange(this, other, d);
  769. return null;
  770. }
  771. }, {
  772. key: "sameParent",
  773. value: function sameParent(other) {
  774. return this.pos - this.parentOffset == other.pos - other.parentOffset;
  775. }
  776. }, {
  777. key: "max",
  778. value: function max(other) {
  779. return other.pos > this.pos ? other : this;
  780. }
  781. }, {
  782. key: "min",
  783. value: function min(other) {
  784. return other.pos < this.pos ? other : this;
  785. }
  786. }, {
  787. key: "toString",
  788. value: function toString() {
  789. var str = "";
  790. for (var i = 1; i <= this.depth; i++) str += (str ? "/" : "") + this.node(i).type.name + "_" + this.index(i - 1);
  791. return str + ":" + this.parentOffset;
  792. }
  793. }], [{
  794. key: "resolve",
  795. value: function resolve(doc, pos) {
  796. if (!(pos >= 0 && pos <= doc.content.size)) throw new RangeError("Position " + pos + " out of range");
  797. var path = [];
  798. var start = 0,
  799. parentOffset = pos;
  800. for (var node = doc;;) {
  801. var _node$content$findInd = node.content.findIndex(parentOffset),
  802. index = _node$content$findInd.index,
  803. offset = _node$content$findInd.offset;
  804. var rem = parentOffset - offset;
  805. path.push(node, index, start + offset);
  806. if (!rem) break;
  807. node = node.child(index);
  808. if (node.isText) break;
  809. parentOffset = rem - 1;
  810. start += offset + 1;
  811. }
  812. return new ResolvedPos(pos, path, parentOffset);
  813. }
  814. }, {
  815. key: "resolveCached",
  816. value: function resolveCached(doc, pos) {
  817. for (var i = 0; i < resolveCache.length; i++) {
  818. var cached = resolveCache[i];
  819. if (cached.pos == pos && cached.doc == doc) return cached;
  820. }
  821. var result = resolveCache[resolveCachePos] = ResolvedPos.resolve(doc, pos);
  822. resolveCachePos = (resolveCachePos + 1) % resolveCacheSize;
  823. return result;
  824. }
  825. }]);
  826. return ResolvedPos;
  827. }();
  828. var resolveCache = [],
  829. resolveCachePos = 0,
  830. resolveCacheSize = 12;
  831. var NodeRange = function () {
  832. function NodeRange($from, $to, depth) {
  833. _classCallCheck(this, NodeRange);
  834. this.$from = $from;
  835. this.$to = $to;
  836. this.depth = depth;
  837. }
  838. _createClass(NodeRange, [{
  839. key: "start",
  840. get: function get() {
  841. return this.$from.before(this.depth + 1);
  842. }
  843. }, {
  844. key: "end",
  845. get: function get() {
  846. return this.$to.after(this.depth + 1);
  847. }
  848. }, {
  849. key: "parent",
  850. get: function get() {
  851. return this.$from.node(this.depth);
  852. }
  853. }, {
  854. key: "startIndex",
  855. get: function get() {
  856. return this.$from.index(this.depth);
  857. }
  858. }, {
  859. key: "endIndex",
  860. get: function get() {
  861. return this.$to.indexAfter(this.depth);
  862. }
  863. }]);
  864. return NodeRange;
  865. }();
  866. var emptyAttrs = Object.create(null);
  867. var Node = function () {
  868. function Node(type, attrs, content) {
  869. var marks = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : Mark.none;
  870. _classCallCheck(this, Node);
  871. this.type = type;
  872. this.attrs = attrs;
  873. this.marks = marks;
  874. this.content = content || Fragment.empty;
  875. }
  876. _createClass(Node, [{
  877. key: "nodeSize",
  878. get: function get() {
  879. return this.isLeaf ? 1 : 2 + this.content.size;
  880. }
  881. }, {
  882. key: "childCount",
  883. get: function get() {
  884. return this.content.childCount;
  885. }
  886. }, {
  887. key: "child",
  888. value: function child(index) {
  889. return this.content.child(index);
  890. }
  891. }, {
  892. key: "maybeChild",
  893. value: function maybeChild(index) {
  894. return this.content.maybeChild(index);
  895. }
  896. }, {
  897. key: "forEach",
  898. value: function forEach(f) {
  899. this.content.forEach(f);
  900. }
  901. }, {
  902. key: "nodesBetween",
  903. value: function nodesBetween(from, to, f) {
  904. var startPos = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 0;
  905. this.content.nodesBetween(from, to, f, startPos, this);
  906. }
  907. }, {
  908. key: "descendants",
  909. value: function descendants(f) {
  910. this.nodesBetween(0, this.content.size, f);
  911. }
  912. }, {
  913. key: "textContent",
  914. get: function get() {
  915. return this.isLeaf && this.type.spec.leafText ? this.type.spec.leafText(this) : this.textBetween(0, this.content.size, "");
  916. }
  917. }, {
  918. key: "textBetween",
  919. value: function textBetween(from, to, blockSeparator, leafText) {
  920. return this.content.textBetween(from, to, blockSeparator, leafText);
  921. }
  922. }, {
  923. key: "firstChild",
  924. get: function get() {
  925. return this.content.firstChild;
  926. }
  927. }, {
  928. key: "lastChild",
  929. get: function get() {
  930. return this.content.lastChild;
  931. }
  932. }, {
  933. key: "eq",
  934. value: function eq(other) {
  935. return this == other || this.sameMarkup(other) && this.content.eq(other.content);
  936. }
  937. }, {
  938. key: "sameMarkup",
  939. value: function sameMarkup(other) {
  940. return this.hasMarkup(other.type, other.attrs, other.marks);
  941. }
  942. }, {
  943. key: "hasMarkup",
  944. value: function hasMarkup(type, attrs, marks) {
  945. return this.type == type && compareDeep(this.attrs, attrs || type.defaultAttrs || emptyAttrs) && Mark.sameSet(this.marks, marks || Mark.none);
  946. }
  947. }, {
  948. key: "copy",
  949. value: function copy() {
  950. var content = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
  951. if (content == this.content) return this;
  952. return new Node(this.type, this.attrs, content, this.marks);
  953. }
  954. }, {
  955. key: "mark",
  956. value: function mark(marks) {
  957. return marks == this.marks ? this : new Node(this.type, this.attrs, this.content, marks);
  958. }
  959. }, {
  960. key: "cut",
  961. value: function cut(from) {
  962. var to = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.content.size;
  963. if (from == 0 && to == this.content.size) return this;
  964. return this.copy(this.content.cut(from, to));
  965. }
  966. }, {
  967. key: "slice",
  968. value: function slice(from) {
  969. var to = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.content.size;
  970. var includeParents = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  971. if (from == to) return Slice.empty;
  972. var $from = this.resolve(from),
  973. $to = this.resolve(to);
  974. var depth = includeParents ? 0 : $from.sharedDepth(to);
  975. var start = $from.start(depth),
  976. node = $from.node(depth);
  977. var content = node.content.cut($from.pos - start, $to.pos - start);
  978. return new Slice(content, $from.depth - depth, $to.depth - depth);
  979. }
  980. }, {
  981. key: "replace",
  982. value: function replace(from, to, slice) {
  983. return _replace(this.resolve(from), this.resolve(to), slice);
  984. }
  985. }, {
  986. key: "nodeAt",
  987. value: function nodeAt(pos) {
  988. for (var node = this;;) {
  989. var _node$content$findInd2 = node.content.findIndex(pos),
  990. index = _node$content$findInd2.index,
  991. offset = _node$content$findInd2.offset;
  992. node = node.maybeChild(index);
  993. if (!node) return null;
  994. if (offset == pos || node.isText) return node;
  995. pos -= offset + 1;
  996. }
  997. }
  998. }, {
  999. key: "childAfter",
  1000. value: function childAfter(pos) {
  1001. var _this$content$findInd = this.content.findIndex(pos),
  1002. index = _this$content$findInd.index,
  1003. offset = _this$content$findInd.offset;
  1004. return {
  1005. node: this.content.maybeChild(index),
  1006. index: index,
  1007. offset: offset
  1008. };
  1009. }
  1010. }, {
  1011. key: "childBefore",
  1012. value: function childBefore(pos) {
  1013. if (pos == 0) return {
  1014. node: null,
  1015. index: 0,
  1016. offset: 0
  1017. };
  1018. var _this$content$findInd2 = this.content.findIndex(pos),
  1019. index = _this$content$findInd2.index,
  1020. offset = _this$content$findInd2.offset;
  1021. if (offset < pos) return {
  1022. node: this.content.child(index),
  1023. index: index,
  1024. offset: offset
  1025. };
  1026. var node = this.content.child(index - 1);
  1027. return {
  1028. node: node,
  1029. index: index - 1,
  1030. offset: offset - node.nodeSize
  1031. };
  1032. }
  1033. }, {
  1034. key: "resolve",
  1035. value: function resolve(pos) {
  1036. return ResolvedPos.resolveCached(this, pos);
  1037. }
  1038. }, {
  1039. key: "resolveNoCache",
  1040. value: function resolveNoCache(pos) {
  1041. return ResolvedPos.resolve(this, pos);
  1042. }
  1043. }, {
  1044. key: "rangeHasMark",
  1045. value: function rangeHasMark(from, to, type) {
  1046. var found = false;
  1047. if (to > from) this.nodesBetween(from, to, function (node) {
  1048. if (type.isInSet(node.marks)) found = true;
  1049. return !found;
  1050. });
  1051. return found;
  1052. }
  1053. }, {
  1054. key: "isBlock",
  1055. get: function get() {
  1056. return this.type.isBlock;
  1057. }
  1058. }, {
  1059. key: "isTextblock",
  1060. get: function get() {
  1061. return this.type.isTextblock;
  1062. }
  1063. }, {
  1064. key: "inlineContent",
  1065. get: function get() {
  1066. return this.type.inlineContent;
  1067. }
  1068. }, {
  1069. key: "isInline",
  1070. get: function get() {
  1071. return this.type.isInline;
  1072. }
  1073. }, {
  1074. key: "isText",
  1075. get: function get() {
  1076. return this.type.isText;
  1077. }
  1078. }, {
  1079. key: "isLeaf",
  1080. get: function get() {
  1081. return this.type.isLeaf;
  1082. }
  1083. }, {
  1084. key: "isAtom",
  1085. get: function get() {
  1086. return this.type.isAtom;
  1087. }
  1088. }, {
  1089. key: "toString",
  1090. value: function toString() {
  1091. if (this.type.spec.toDebugString) return this.type.spec.toDebugString(this);
  1092. var name = this.type.name;
  1093. if (this.content.size) name += "(" + this.content.toStringInner() + ")";
  1094. return wrapMarks(this.marks, name);
  1095. }
  1096. }, {
  1097. key: "contentMatchAt",
  1098. value: function contentMatchAt(index) {
  1099. var match = this.type.contentMatch.matchFragment(this.content, 0, index);
  1100. if (!match) throw new Error("Called contentMatchAt on a node with invalid content");
  1101. return match;
  1102. }
  1103. }, {
  1104. key: "canReplace",
  1105. value: function canReplace(from, to) {
  1106. var replacement = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : Fragment.empty;
  1107. var start = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 0;
  1108. var end = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : replacement.childCount;
  1109. var one = this.contentMatchAt(from).matchFragment(replacement, start, end);
  1110. var two = one && one.matchFragment(this.content, to);
  1111. if (!two || !two.validEnd) return false;
  1112. for (var i = start; i < end; i++) if (!this.type.allowsMarks(replacement.child(i).marks)) return false;
  1113. return true;
  1114. }
  1115. }, {
  1116. key: "canReplaceWith",
  1117. value: function canReplaceWith(from, to, type, marks) {
  1118. if (marks && !this.type.allowsMarks(marks)) return false;
  1119. var start = this.contentMatchAt(from).matchType(type);
  1120. var end = start && start.matchFragment(this.content, to);
  1121. return end ? end.validEnd : false;
  1122. }
  1123. }, {
  1124. key: "canAppend",
  1125. value: function canAppend(other) {
  1126. if (other.content.size) return this.canReplace(this.childCount, this.childCount, other.content);else return this.type.compatibleContent(other.type);
  1127. }
  1128. }, {
  1129. key: "check",
  1130. value: function check() {
  1131. this.type.checkContent(this.content);
  1132. var copy = Mark.none;
  1133. for (var i = 0; i < this.marks.length; i++) copy = this.marks[i].addToSet(copy);
  1134. if (!Mark.sameSet(copy, this.marks)) throw new RangeError("Invalid collection of marks for node ".concat(this.type.name, ": ").concat(this.marks.map(function (m) {
  1135. return m.type.name;
  1136. })));
  1137. this.content.forEach(function (node) {
  1138. return node.check();
  1139. });
  1140. }
  1141. }, {
  1142. key: "toJSON",
  1143. value: function toJSON() {
  1144. var obj = {
  1145. type: this.type.name
  1146. };
  1147. for (var _ in this.attrs) {
  1148. obj.attrs = this.attrs;
  1149. break;
  1150. }
  1151. if (this.content.size) obj.content = this.content.toJSON();
  1152. if (this.marks.length) obj.marks = this.marks.map(function (n) {
  1153. return n.toJSON();
  1154. });
  1155. return obj;
  1156. }
  1157. }], [{
  1158. key: "fromJSON",
  1159. value: function fromJSON(schema, json) {
  1160. if (!json) throw new RangeError("Invalid input for Node.fromJSON");
  1161. var marks = null;
  1162. if (json.marks) {
  1163. if (!Array.isArray(json.marks)) throw new RangeError("Invalid mark data for Node.fromJSON");
  1164. marks = json.marks.map(schema.markFromJSON);
  1165. }
  1166. if (json.type == "text") {
  1167. if (typeof json.text != "string") throw new RangeError("Invalid text node in JSON");
  1168. return schema.text(json.text, marks);
  1169. }
  1170. var content = Fragment.fromJSON(schema, json.content);
  1171. return schema.nodeType(json.type).create(json.attrs, content, marks);
  1172. }
  1173. }]);
  1174. return Node;
  1175. }();
  1176. Node.prototype.text = undefined;
  1177. var TextNode = function (_Node) {
  1178. _inherits(TextNode, _Node);
  1179. var _super2 = _createSuper(TextNode);
  1180. function TextNode(type, attrs, content, marks) {
  1181. var _this;
  1182. _classCallCheck(this, TextNode);
  1183. _this = _super2.call(this, type, attrs, null, marks);
  1184. if (!content) throw new RangeError("Empty text nodes are not allowed");
  1185. _this.text = content;
  1186. return _this;
  1187. }
  1188. _createClass(TextNode, [{
  1189. key: "toString",
  1190. value: function toString() {
  1191. if (this.type.spec.toDebugString) return this.type.spec.toDebugString(this);
  1192. return wrapMarks(this.marks, JSON.stringify(this.text));
  1193. }
  1194. }, {
  1195. key: "textContent",
  1196. get: function get() {
  1197. return this.text;
  1198. }
  1199. }, {
  1200. key: "textBetween",
  1201. value: function textBetween(from, to) {
  1202. return this.text.slice(from, to);
  1203. }
  1204. }, {
  1205. key: "nodeSize",
  1206. get: function get() {
  1207. return this.text.length;
  1208. }
  1209. }, {
  1210. key: "mark",
  1211. value: function mark(marks) {
  1212. return marks == this.marks ? this : new TextNode(this.type, this.attrs, this.text, marks);
  1213. }
  1214. }, {
  1215. key: "withText",
  1216. value: function withText(text) {
  1217. if (text == this.text) return this;
  1218. return new TextNode(this.type, this.attrs, text, this.marks);
  1219. }
  1220. }, {
  1221. key: "cut",
  1222. value: function cut() {
  1223. var from = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
  1224. var to = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.text.length;
  1225. if (from == 0 && to == this.text.length) return this;
  1226. return this.withText(this.text.slice(from, to));
  1227. }
  1228. }, {
  1229. key: "eq",
  1230. value: function eq(other) {
  1231. return this.sameMarkup(other) && this.text == other.text;
  1232. }
  1233. }, {
  1234. key: "toJSON",
  1235. value: function toJSON() {
  1236. var base = _get(_getPrototypeOf(TextNode.prototype), "toJSON", this).call(this);
  1237. base.text = this.text;
  1238. return base;
  1239. }
  1240. }]);
  1241. return TextNode;
  1242. }(Node);
  1243. function wrapMarks(marks, str) {
  1244. for (var i = marks.length - 1; i >= 0; i--) str = marks[i].type.name + "(" + str + ")";
  1245. return str;
  1246. }
  1247. var ContentMatch = function () {
  1248. function ContentMatch(validEnd) {
  1249. _classCallCheck(this, ContentMatch);
  1250. this.validEnd = validEnd;
  1251. this.next = [];
  1252. this.wrapCache = [];
  1253. }
  1254. _createClass(ContentMatch, [{
  1255. key: "matchType",
  1256. value: function matchType(type) {
  1257. for (var i = 0; i < this.next.length; i++) if (this.next[i].type == type) return this.next[i].next;
  1258. return null;
  1259. }
  1260. }, {
  1261. key: "matchFragment",
  1262. value: function matchFragment(frag) {
  1263. var start = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
  1264. var end = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : frag.childCount;
  1265. var cur = this;
  1266. for (var i = start; cur && i < end; i++) cur = cur.matchType(frag.child(i).type);
  1267. return cur;
  1268. }
  1269. }, {
  1270. key: "inlineContent",
  1271. get: function get() {
  1272. return this.next.length != 0 && this.next[0].type.isInline;
  1273. }
  1274. }, {
  1275. key: "defaultType",
  1276. get: function get() {
  1277. for (var i = 0; i < this.next.length; i++) {
  1278. var type = this.next[i].type;
  1279. if (!(type.isText || type.hasRequiredAttrs())) return type;
  1280. }
  1281. return null;
  1282. }
  1283. }, {
  1284. key: "compatible",
  1285. value: function compatible(other) {
  1286. for (var i = 0; i < this.next.length; i++) for (var j = 0; j < other.next.length; j++) if (this.next[i].type == other.next[j].type) return true;
  1287. return false;
  1288. }
  1289. }, {
  1290. key: "fillBefore",
  1291. value: function fillBefore(after) {
  1292. var toEnd = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  1293. var startIndex = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
  1294. var seen = [this];
  1295. function search(match, types) {
  1296. var finished = match.matchFragment(after, startIndex);
  1297. if (finished && (!toEnd || finished.validEnd)) return Fragment.from(types.map(function (tp) {
  1298. return tp.createAndFill();
  1299. }));
  1300. for (var i = 0; i < match.next.length; i++) {
  1301. var _match$next$i = match.next[i],
  1302. type = _match$next$i.type,
  1303. next = _match$next$i.next;
  1304. if (!(type.isText || type.hasRequiredAttrs()) && seen.indexOf(next) == -1) {
  1305. seen.push(next);
  1306. var _found = search(next, types.concat(type));
  1307. if (_found) return _found;
  1308. }
  1309. }
  1310. return null;
  1311. }
  1312. return search(this, []);
  1313. }
  1314. }, {
  1315. key: "findWrapping",
  1316. value: function findWrapping(target) {
  1317. for (var i = 0; i < this.wrapCache.length; i += 2) if (this.wrapCache[i] == target) return this.wrapCache[i + 1];
  1318. var computed = this.computeWrapping(target);
  1319. this.wrapCache.push(target, computed);
  1320. return computed;
  1321. }
  1322. }, {
  1323. key: "computeWrapping",
  1324. value: function computeWrapping(target) {
  1325. var seen = Object.create(null),
  1326. active = [{
  1327. match: this,
  1328. type: null,
  1329. via: null
  1330. }];
  1331. while (active.length) {
  1332. var current = active.shift(),
  1333. match = current.match;
  1334. if (match.matchType(target)) {
  1335. var result = [];
  1336. for (var obj = current; obj.type; obj = obj.via) result.push(obj.type);
  1337. return result.reverse();
  1338. }
  1339. for (var i = 0; i < match.next.length; i++) {
  1340. var _match$next$i2 = match.next[i],
  1341. type = _match$next$i2.type,
  1342. next = _match$next$i2.next;
  1343. if (!type.isLeaf && !type.hasRequiredAttrs() && !(type.name in seen) && (!current.type || next.validEnd)) {
  1344. active.push({
  1345. match: type.contentMatch,
  1346. type: type,
  1347. via: current
  1348. });
  1349. seen[type.name] = true;
  1350. }
  1351. }
  1352. }
  1353. return null;
  1354. }
  1355. }, {
  1356. key: "edgeCount",
  1357. get: function get() {
  1358. return this.next.length;
  1359. }
  1360. }, {
  1361. key: "edge",
  1362. value: function edge(n) {
  1363. if (n >= this.next.length) throw new RangeError("There's no ".concat(n, "th edge in this content match"));
  1364. return this.next[n];
  1365. }
  1366. }, {
  1367. key: "toString",
  1368. value: function toString() {
  1369. var seen = [];
  1370. function scan(m) {
  1371. seen.push(m);
  1372. for (var i = 0; i < m.next.length; i++) if (seen.indexOf(m.next[i].next) == -1) scan(m.next[i].next);
  1373. }
  1374. scan(this);
  1375. return seen.map(function (m, i) {
  1376. var out = i + (m.validEnd ? "*" : " ") + " ";
  1377. for (var _i = 0; _i < m.next.length; _i++) out += (_i ? ", " : "") + m.next[_i].type.name + "->" + seen.indexOf(m.next[_i].next);
  1378. return out;
  1379. }).join("\n");
  1380. }
  1381. }], [{
  1382. key: "parse",
  1383. value: function parse(string, nodeTypes) {
  1384. var stream = new TokenStream(string, nodeTypes);
  1385. if (stream.next == null) return ContentMatch.empty;
  1386. var expr = parseExpr(stream);
  1387. if (stream.next) stream.err("Unexpected trailing text");
  1388. var match = dfa(nfa(expr));
  1389. checkForDeadEnds(match, stream);
  1390. return match;
  1391. }
  1392. }]);
  1393. return ContentMatch;
  1394. }();
  1395. ContentMatch.empty = new ContentMatch(true);
  1396. var TokenStream = function () {
  1397. function TokenStream(string, nodeTypes) {
  1398. _classCallCheck(this, TokenStream);
  1399. this.string = string;
  1400. this.nodeTypes = nodeTypes;
  1401. this.inline = null;
  1402. this.pos = 0;
  1403. this.tokens = string.split(/\s*(?=\b|\W|$)/);
  1404. if (this.tokens[this.tokens.length - 1] == "") this.tokens.pop();
  1405. if (this.tokens[0] == "") this.tokens.shift();
  1406. }
  1407. _createClass(TokenStream, [{
  1408. key: "next",
  1409. get: function get() {
  1410. return this.tokens[this.pos];
  1411. }
  1412. }, {
  1413. key: "eat",
  1414. value: function eat(tok) {
  1415. return this.next == tok && (this.pos++ || true);
  1416. }
  1417. }, {
  1418. key: "err",
  1419. value: function err(str) {
  1420. throw new SyntaxError(str + " (in content expression '" + this.string + "')");
  1421. }
  1422. }]);
  1423. return TokenStream;
  1424. }();
  1425. function parseExpr(stream) {
  1426. var exprs = [];
  1427. do {
  1428. exprs.push(parseExprSeq(stream));
  1429. } while (stream.eat("|"));
  1430. return exprs.length == 1 ? exprs[0] : {
  1431. type: "choice",
  1432. exprs: exprs
  1433. };
  1434. }
  1435. function parseExprSeq(stream) {
  1436. var exprs = [];
  1437. do {
  1438. exprs.push(parseExprSubscript(stream));
  1439. } while (stream.next && stream.next != ")" && stream.next != "|");
  1440. return exprs.length == 1 ? exprs[0] : {
  1441. type: "seq",
  1442. exprs: exprs
  1443. };
  1444. }
  1445. function parseExprSubscript(stream) {
  1446. var expr = parseExprAtom(stream);
  1447. for (;;) {
  1448. if (stream.eat("+")) expr = {
  1449. type: "plus",
  1450. expr: expr
  1451. };else if (stream.eat("*")) expr = {
  1452. type: "star",
  1453. expr: expr
  1454. };else if (stream.eat("?")) expr = {
  1455. type: "opt",
  1456. expr: expr
  1457. };else if (stream.eat("{")) expr = parseExprRange(stream, expr);else break;
  1458. }
  1459. return expr;
  1460. }
  1461. function parseNum(stream) {
  1462. if (/\D/.test(stream.next)) stream.err("Expected number, got '" + stream.next + "'");
  1463. var result = Number(stream.next);
  1464. stream.pos++;
  1465. return result;
  1466. }
  1467. function parseExprRange(stream, expr) {
  1468. var min = parseNum(stream),
  1469. max = min;
  1470. if (stream.eat(",")) {
  1471. if (stream.next != "}") max = parseNum(stream);else max = -1;
  1472. }
  1473. if (!stream.eat("}")) stream.err("Unclosed braced range");
  1474. return {
  1475. type: "range",
  1476. min: min,
  1477. max: max,
  1478. expr: expr
  1479. };
  1480. }
  1481. function resolveName(stream, name) {
  1482. var types = stream.nodeTypes,
  1483. type = types[name];
  1484. if (type) return [type];
  1485. var result = [];
  1486. for (var typeName in types) {
  1487. var _type = types[typeName];
  1488. if (_type.groups.indexOf(name) > -1) result.push(_type);
  1489. }
  1490. if (result.length == 0) stream.err("No node type or group '" + name + "' found");
  1491. return result;
  1492. }
  1493. function parseExprAtom(stream) {
  1494. if (stream.eat("(")) {
  1495. var expr = parseExpr(stream);
  1496. if (!stream.eat(")")) stream.err("Missing closing paren");
  1497. return expr;
  1498. } else if (!/\W/.test(stream.next)) {
  1499. var exprs = resolveName(stream, stream.next).map(function (type) {
  1500. if (stream.inline == null) stream.inline = type.isInline;else if (stream.inline != type.isInline) stream.err("Mixing inline and block content");
  1501. return {
  1502. type: "name",
  1503. value: type
  1504. };
  1505. });
  1506. stream.pos++;
  1507. return exprs.length == 1 ? exprs[0] : {
  1508. type: "choice",
  1509. exprs: exprs
  1510. };
  1511. } else {
  1512. stream.err("Unexpected token '" + stream.next + "'");
  1513. }
  1514. }
  1515. function nfa(expr) {
  1516. var nfa = [[]];
  1517. connect(compile(expr, 0), node());
  1518. return nfa;
  1519. function node() {
  1520. return nfa.push([]) - 1;
  1521. }
  1522. function edge(from, to, term) {
  1523. var edge = {
  1524. term: term,
  1525. to: to
  1526. };
  1527. nfa[from].push(edge);
  1528. return edge;
  1529. }
  1530. function connect(edges, to) {
  1531. edges.forEach(function (edge) {
  1532. return edge.to = to;
  1533. });
  1534. }
  1535. function compile(expr, from) {
  1536. if (expr.type == "choice") {
  1537. return expr.exprs.reduce(function (out, expr) {
  1538. return out.concat(compile(expr, from));
  1539. }, []);
  1540. } else if (expr.type == "seq") {
  1541. for (var i = 0;; i++) {
  1542. var next = compile(expr.exprs[i], from);
  1543. if (i == expr.exprs.length - 1) return next;
  1544. connect(next, from = node());
  1545. }
  1546. } else if (expr.type == "star") {
  1547. var loop = node();
  1548. edge(from, loop);
  1549. connect(compile(expr.expr, loop), loop);
  1550. return [edge(loop)];
  1551. } else if (expr.type == "plus") {
  1552. var _loop = node();
  1553. connect(compile(expr.expr, from), _loop);
  1554. connect(compile(expr.expr, _loop), _loop);
  1555. return [edge(_loop)];
  1556. } else if (expr.type == "opt") {
  1557. return [edge(from)].concat(compile(expr.expr, from));
  1558. } else if (expr.type == "range") {
  1559. var cur = from;
  1560. for (var _i2 = 0; _i2 < expr.min; _i2++) {
  1561. var _next = node();
  1562. connect(compile(expr.expr, cur), _next);
  1563. cur = _next;
  1564. }
  1565. if (expr.max == -1) {
  1566. connect(compile(expr.expr, cur), cur);
  1567. } else {
  1568. for (var _i3 = expr.min; _i3 < expr.max; _i3++) {
  1569. var _next2 = node();
  1570. edge(cur, _next2);
  1571. connect(compile(expr.expr, cur), _next2);
  1572. cur = _next2;
  1573. }
  1574. }
  1575. return [edge(cur)];
  1576. } else if (expr.type == "name") {
  1577. return [edge(from, undefined, expr.value)];
  1578. } else {
  1579. throw new Error("Unknown expr type");
  1580. }
  1581. }
  1582. }
  1583. function cmp(a, b) {
  1584. return b - a;
  1585. }
  1586. function nullFrom(nfa, node) {
  1587. var result = [];
  1588. scan(node);
  1589. return result.sort(cmp);
  1590. function scan(node) {
  1591. var edges = nfa[node];
  1592. if (edges.length == 1 && !edges[0].term) return scan(edges[0].to);
  1593. result.push(node);
  1594. for (var i = 0; i < edges.length; i++) {
  1595. var _edges$i = edges[i],
  1596. term = _edges$i.term,
  1597. to = _edges$i.to;
  1598. if (!term && result.indexOf(to) == -1) scan(to);
  1599. }
  1600. }
  1601. }
  1602. function dfa(nfa) {
  1603. var labeled = Object.create(null);
  1604. return explore(nullFrom(nfa, 0));
  1605. function explore(states) {
  1606. var out = [];
  1607. states.forEach(function (node) {
  1608. nfa[node].forEach(function (_ref) {
  1609. var term = _ref.term,
  1610. to = _ref.to;
  1611. if (!term) return;
  1612. var set;
  1613. for (var i = 0; i < out.length; i++) if (out[i][0] == term) set = out[i][1];
  1614. nullFrom(nfa, to).forEach(function (node) {
  1615. if (!set) out.push([term, set = []]);
  1616. if (set.indexOf(node) == -1) set.push(node);
  1617. });
  1618. });
  1619. });
  1620. var state = labeled[states.join(",")] = new ContentMatch(states.indexOf(nfa.length - 1) > -1);
  1621. for (var i = 0; i < out.length; i++) {
  1622. var _states = out[i][1].sort(cmp);
  1623. state.next.push({
  1624. type: out[i][0],
  1625. next: labeled[_states.join(",")] || explore(_states)
  1626. });
  1627. }
  1628. return state;
  1629. }
  1630. }
  1631. function checkForDeadEnds(match, stream) {
  1632. for (var i = 0, work = [match]; i < work.length; i++) {
  1633. var state = work[i],
  1634. dead = !state.validEnd,
  1635. nodes = [];
  1636. for (var j = 0; j < state.next.length; j++) {
  1637. var _state$next$j = state.next[j],
  1638. type = _state$next$j.type,
  1639. next = _state$next$j.next;
  1640. nodes.push(type.name);
  1641. if (dead && !(type.isText || type.hasRequiredAttrs())) dead = false;
  1642. if (work.indexOf(next) == -1) work.push(next);
  1643. }
  1644. if (dead) stream.err("Only non-generatable nodes (" + nodes.join(", ") + ") in a required position (see https://prosemirror.net/docs/guide/#generatable)");
  1645. }
  1646. }
  1647. function defaultAttrs(attrs) {
  1648. var defaults = Object.create(null);
  1649. for (var attrName in attrs) {
  1650. var attr = attrs[attrName];
  1651. if (!attr.hasDefault) return null;
  1652. defaults[attrName] = attr["default"];
  1653. }
  1654. return defaults;
  1655. }
  1656. function _computeAttrs(attrs, value) {
  1657. var built = Object.create(null);
  1658. for (var name in attrs) {
  1659. var given = value && value[name];
  1660. if (given === undefined) {
  1661. var attr = attrs[name];
  1662. if (attr.hasDefault) given = attr["default"];else throw new RangeError("No value supplied for attribute " + name);
  1663. }
  1664. built[name] = given;
  1665. }
  1666. return built;
  1667. }
  1668. function initAttrs(attrs) {
  1669. var result = Object.create(null);
  1670. if (attrs) for (var name in attrs) result[name] = new Attribute(attrs[name]);
  1671. return result;
  1672. }
  1673. var NodeType = function () {
  1674. function NodeType(name, schema, spec) {
  1675. _classCallCheck(this, NodeType);
  1676. this.name = name;
  1677. this.schema = schema;
  1678. this.spec = spec;
  1679. this.markSet = null;
  1680. this.groups = spec.group ? spec.group.split(" ") : [];
  1681. this.attrs = initAttrs(spec.attrs);
  1682. this.defaultAttrs = defaultAttrs(this.attrs);
  1683. this.contentMatch = null;
  1684. this.inlineContent = null;
  1685. this.isBlock = !(spec.inline || name == "text");
  1686. this.isText = name == "text";
  1687. }
  1688. _createClass(NodeType, [{
  1689. key: "isInline",
  1690. get: function get() {
  1691. return !this.isBlock;
  1692. }
  1693. }, {
  1694. key: "isTextblock",
  1695. get: function get() {
  1696. return this.isBlock && this.inlineContent;
  1697. }
  1698. }, {
  1699. key: "isLeaf",
  1700. get: function get() {
  1701. return this.contentMatch == ContentMatch.empty;
  1702. }
  1703. }, {
  1704. key: "isAtom",
  1705. get: function get() {
  1706. return this.isLeaf || !!this.spec.atom;
  1707. }
  1708. }, {
  1709. key: "whitespace",
  1710. get: function get() {
  1711. return this.spec.whitespace || (this.spec.code ? "pre" : "normal");
  1712. }
  1713. }, {
  1714. key: "hasRequiredAttrs",
  1715. value: function hasRequiredAttrs() {
  1716. for (var n in this.attrs) if (this.attrs[n].isRequired) return true;
  1717. return false;
  1718. }
  1719. }, {
  1720. key: "compatibleContent",
  1721. value: function compatibleContent(other) {
  1722. return this == other || this.contentMatch.compatible(other.contentMatch);
  1723. }
  1724. }, {
  1725. key: "computeAttrs",
  1726. value: function computeAttrs(attrs) {
  1727. if (!attrs && this.defaultAttrs) return this.defaultAttrs;else return _computeAttrs(this.attrs, attrs);
  1728. }
  1729. }, {
  1730. key: "create",
  1731. value: function create() {
  1732. var attrs = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
  1733. var content = arguments.length > 1 ? arguments[1] : undefined;
  1734. var marks = arguments.length > 2 ? arguments[2] : undefined;
  1735. if (this.isText) throw new Error("NodeType.create can't construct text nodes");
  1736. return new Node(this, this.computeAttrs(attrs), Fragment.from(content), Mark.setFrom(marks));
  1737. }
  1738. }, {
  1739. key: "createChecked",
  1740. value: function createChecked() {
  1741. var attrs = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
  1742. var content = arguments.length > 1 ? arguments[1] : undefined;
  1743. var marks = arguments.length > 2 ? arguments[2] : undefined;
  1744. content = Fragment.from(content);
  1745. this.checkContent(content);
  1746. return new Node(this, this.computeAttrs(attrs), content, Mark.setFrom(marks));
  1747. }
  1748. }, {
  1749. key: "createAndFill",
  1750. value: function createAndFill() {
  1751. var attrs = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
  1752. var content = arguments.length > 1 ? arguments[1] : undefined;
  1753. var marks = arguments.length > 2 ? arguments[2] : undefined;
  1754. attrs = this.computeAttrs(attrs);
  1755. content = Fragment.from(content);
  1756. if (content.size) {
  1757. var before = this.contentMatch.fillBefore(content);
  1758. if (!before) return null;
  1759. content = before.append(content);
  1760. }
  1761. var matched = this.contentMatch.matchFragment(content);
  1762. var after = matched && matched.fillBefore(Fragment.empty, true);
  1763. if (!after) return null;
  1764. return new Node(this, attrs, content.append(after), Mark.setFrom(marks));
  1765. }
  1766. }, {
  1767. key: "validContent",
  1768. value: function validContent(content) {
  1769. var result = this.contentMatch.matchFragment(content);
  1770. if (!result || !result.validEnd) return false;
  1771. for (var i = 0; i < content.childCount; i++) if (!this.allowsMarks(content.child(i).marks)) return false;
  1772. return true;
  1773. }
  1774. }, {
  1775. key: "checkContent",
  1776. value: function checkContent(content) {
  1777. if (!this.validContent(content)) throw new RangeError("Invalid content for node ".concat(this.name, ": ").concat(content.toString().slice(0, 50)));
  1778. }
  1779. }, {
  1780. key: "allowsMarkType",
  1781. value: function allowsMarkType(markType) {
  1782. return this.markSet == null || this.markSet.indexOf(markType) > -1;
  1783. }
  1784. }, {
  1785. key: "allowsMarks",
  1786. value: function allowsMarks(marks) {
  1787. if (this.markSet == null) return true;
  1788. for (var i = 0; i < marks.length; i++) if (!this.allowsMarkType(marks[i].type)) return false;
  1789. return true;
  1790. }
  1791. }, {
  1792. key: "allowedMarks",
  1793. value: function allowedMarks(marks) {
  1794. if (this.markSet == null) return marks;
  1795. var copy;
  1796. for (var i = 0; i < marks.length; i++) {
  1797. if (!this.allowsMarkType(marks[i].type)) {
  1798. if (!copy) copy = marks.slice(0, i);
  1799. } else if (copy) {
  1800. copy.push(marks[i]);
  1801. }
  1802. }
  1803. return !copy ? marks : copy.length ? copy : Mark.none;
  1804. }
  1805. }], [{
  1806. key: "compile",
  1807. value: function compile(nodes, schema) {
  1808. var result = Object.create(null);
  1809. nodes.forEach(function (name, spec) {
  1810. return result[name] = new NodeType(name, schema, spec);
  1811. });
  1812. var topType = schema.spec.topNode || "doc";
  1813. if (!result[topType]) throw new RangeError("Schema is missing its top node type ('" + topType + "')");
  1814. if (!result.text) throw new RangeError("Every schema needs a 'text' type");
  1815. for (var _ in result.text.attrs) throw new RangeError("The text node type should not have attributes");
  1816. return result;
  1817. }
  1818. }]);
  1819. return NodeType;
  1820. }();
  1821. var Attribute = function () {
  1822. function Attribute(options) {
  1823. _classCallCheck(this, Attribute);
  1824. this.hasDefault = Object.prototype.hasOwnProperty.call(options, "default");
  1825. this["default"] = options["default"];
  1826. }
  1827. _createClass(Attribute, [{
  1828. key: "isRequired",
  1829. get: function get() {
  1830. return !this.hasDefault;
  1831. }
  1832. }]);
  1833. return Attribute;
  1834. }();
  1835. var MarkType = function () {
  1836. function MarkType(name, rank, schema, spec) {
  1837. _classCallCheck(this, MarkType);
  1838. this.name = name;
  1839. this.rank = rank;
  1840. this.schema = schema;
  1841. this.spec = spec;
  1842. this.attrs = initAttrs(spec.attrs);
  1843. this.excluded = null;
  1844. var defaults = defaultAttrs(this.attrs);
  1845. this.instance = defaults ? new Mark(this, defaults) : null;
  1846. }
  1847. _createClass(MarkType, [{
  1848. key: "create",
  1849. value: function create() {
  1850. var attrs = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
  1851. if (!attrs && this.instance) return this.instance;
  1852. return new Mark(this, _computeAttrs(this.attrs, attrs));
  1853. }
  1854. }, {
  1855. key: "removeFromSet",
  1856. value: function removeFromSet(set) {
  1857. for (var i = 0; i < set.length; i++) if (set[i].type == this) {
  1858. set = set.slice(0, i).concat(set.slice(i + 1));
  1859. i--;
  1860. }
  1861. return set;
  1862. }
  1863. }, {
  1864. key: "isInSet",
  1865. value: function isInSet(set) {
  1866. for (var i = 0; i < set.length; i++) if (set[i].type == this) return set[i];
  1867. }
  1868. }, {
  1869. key: "excludes",
  1870. value: function excludes(other) {
  1871. return this.excluded.indexOf(other) > -1;
  1872. }
  1873. }], [{
  1874. key: "compile",
  1875. value: function compile(marks, schema) {
  1876. var result = Object.create(null),
  1877. rank = 0;
  1878. marks.forEach(function (name, spec) {
  1879. return result[name] = new MarkType(name, rank++, schema, spec);
  1880. });
  1881. return result;
  1882. }
  1883. }]);
  1884. return MarkType;
  1885. }();
  1886. var Schema = function () {
  1887. function Schema(spec) {
  1888. _classCallCheck(this, Schema);
  1889. this.cached = Object.create(null);
  1890. var instanceSpec = this.spec = {};
  1891. for (var prop in spec) instanceSpec[prop] = spec[prop];
  1892. instanceSpec.nodes = OrderedMap.from(spec.nodes), instanceSpec.marks = OrderedMap.from(spec.marks || {}), this.nodes = NodeType.compile(this.spec.nodes, this);
  1893. this.marks = MarkType.compile(this.spec.marks, this);
  1894. var contentExprCache = Object.create(null);
  1895. for (var _prop in this.nodes) {
  1896. if (_prop in this.marks) throw new RangeError(_prop + " can not be both a node and a mark");
  1897. var type = this.nodes[_prop],
  1898. contentExpr = type.spec.content || "",
  1899. markExpr = type.spec.marks;
  1900. type.contentMatch = contentExprCache[contentExpr] || (contentExprCache[contentExpr] = ContentMatch.parse(contentExpr, this.nodes));
  1901. type.inlineContent = type.contentMatch.inlineContent;
  1902. type.markSet = markExpr == "_" ? null : markExpr ? gatherMarks(this, markExpr.split(" ")) : markExpr == "" || !type.inlineContent ? [] : null;
  1903. }
  1904. for (var _prop2 in this.marks) {
  1905. var _type2 = this.marks[_prop2],
  1906. excl = _type2.spec.excludes;
  1907. _type2.excluded = excl == null ? [_type2] : excl == "" ? [] : gatherMarks(this, excl.split(" "));
  1908. }
  1909. this.nodeFromJSON = this.nodeFromJSON.bind(this);
  1910. this.markFromJSON = this.markFromJSON.bind(this);
  1911. this.topNodeType = this.nodes[this.spec.topNode || "doc"];
  1912. this.cached.wrappings = Object.create(null);
  1913. }
  1914. _createClass(Schema, [{
  1915. key: "node",
  1916. value: function node(type) {
  1917. var attrs = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
  1918. var content = arguments.length > 2 ? arguments[2] : undefined;
  1919. var marks = arguments.length > 3 ? arguments[3] : undefined;
  1920. if (typeof type == "string") type = this.nodeType(type);else if (!(type instanceof NodeType)) throw new RangeError("Invalid node type: " + type);else if (type.schema != this) throw new RangeError("Node type from different schema used (" + type.name + ")");
  1921. return type.createChecked(attrs, content, marks);
  1922. }
  1923. }, {
  1924. key: "text",
  1925. value: function text(_text, marks) {
  1926. var type = this.nodes.text;
  1927. return new TextNode(type, type.defaultAttrs, _text, Mark.setFrom(marks));
  1928. }
  1929. }, {
  1930. key: "mark",
  1931. value: function mark(type, attrs) {
  1932. if (typeof type == "string") type = this.marks[type];
  1933. return type.create(attrs);
  1934. }
  1935. }, {
  1936. key: "nodeFromJSON",
  1937. value: function nodeFromJSON(json) {
  1938. return Node.fromJSON(this, json);
  1939. }
  1940. }, {
  1941. key: "markFromJSON",
  1942. value: function markFromJSON(json) {
  1943. return Mark.fromJSON(this, json);
  1944. }
  1945. }, {
  1946. key: "nodeType",
  1947. value: function nodeType(name) {
  1948. var found = this.nodes[name];
  1949. if (!found) throw new RangeError("Unknown node type: " + name);
  1950. return found;
  1951. }
  1952. }]);
  1953. return Schema;
  1954. }();
  1955. function gatherMarks(schema, marks) {
  1956. var found = [];
  1957. for (var i = 0; i < marks.length; i++) {
  1958. var name = marks[i],
  1959. mark = schema.marks[name],
  1960. ok = mark;
  1961. if (mark) {
  1962. found.push(mark);
  1963. } else {
  1964. for (var prop in schema.marks) {
  1965. var _mark = schema.marks[prop];
  1966. if (name == "_" || _mark.spec.group && _mark.spec.group.split(" ").indexOf(name) > -1) found.push(ok = _mark);
  1967. }
  1968. }
  1969. if (!ok) throw new SyntaxError("Unknown mark type: '" + marks[i] + "'");
  1970. }
  1971. return found;
  1972. }
  1973. var DOMParser = function () {
  1974. function DOMParser(schema, rules) {
  1975. var _this2 = this;
  1976. _classCallCheck(this, DOMParser);
  1977. this.schema = schema;
  1978. this.rules = rules;
  1979. this.tags = [];
  1980. this.styles = [];
  1981. rules.forEach(function (rule) {
  1982. if (rule.tag) _this2.tags.push(rule);else if (rule.style) _this2.styles.push(rule);
  1983. });
  1984. this.normalizeLists = !this.tags.some(function (r) {
  1985. if (!/^(ul|ol)\b/.test(r.tag) || !r.node) return false;
  1986. var node = schema.nodes[r.node];
  1987. return node.contentMatch.matchType(node);
  1988. });
  1989. }
  1990. _createClass(DOMParser, [{
  1991. key: "parse",
  1992. value: function parse(dom) {
  1993. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  1994. var context = new ParseContext(this, options, false);
  1995. context.addAll(dom, options.from, options.to);
  1996. return context.finish();
  1997. }
  1998. }, {
  1999. key: "parseSlice",
  2000. value: function parseSlice(dom) {
  2001. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  2002. var context = new ParseContext(this, options, true);
  2003. context.addAll(dom, options.from, options.to);
  2004. return Slice.maxOpen(context.finish());
  2005. }
  2006. }, {
  2007. key: "matchTag",
  2008. value: function matchTag(dom, context, after) {
  2009. for (var i = after ? this.tags.indexOf(after) + 1 : 0; i < this.tags.length; i++) {
  2010. var rule = this.tags[i];
  2011. if (matches(dom, rule.tag) && (rule.namespace === undefined || dom.namespaceURI == rule.namespace) && (!rule.context || context.matchesContext(rule.context))) {
  2012. if (rule.getAttrs) {
  2013. var result = rule.getAttrs(dom);
  2014. if (result === false) continue;
  2015. rule.attrs = result || undefined;
  2016. }
  2017. return rule;
  2018. }
  2019. }
  2020. }
  2021. }, {
  2022. key: "matchStyle",
  2023. value: function matchStyle(prop, value, context, after) {
  2024. for (var i = after ? this.styles.indexOf(after) + 1 : 0; i < this.styles.length; i++) {
  2025. var rule = this.styles[i],
  2026. style = rule.style;
  2027. if (style.indexOf(prop) != 0 || rule.context && !context.matchesContext(rule.context) || style.length > prop.length && (style.charCodeAt(prop.length) != 61 || style.slice(prop.length + 1) != value)) continue;
  2028. if (rule.getAttrs) {
  2029. var result = rule.getAttrs(value);
  2030. if (result === false) continue;
  2031. rule.attrs = result || undefined;
  2032. }
  2033. return rule;
  2034. }
  2035. }
  2036. }], [{
  2037. key: "schemaRules",
  2038. value: function schemaRules(schema) {
  2039. var result = [];
  2040. function insert(rule) {
  2041. var priority = rule.priority == null ? 50 : rule.priority,
  2042. i = 0;
  2043. for (; i < result.length; i++) {
  2044. var next = result[i],
  2045. nextPriority = next.priority == null ? 50 : next.priority;
  2046. if (nextPriority < priority) break;
  2047. }
  2048. result.splice(i, 0, rule);
  2049. }
  2050. var _loop2 = function _loop2(name) {
  2051. var rules = schema.marks[name].spec.parseDOM;
  2052. if (rules) rules.forEach(function (rule) {
  2053. insert(rule = copy(rule));
  2054. if (!(rule.mark || rule.ignore || rule.clearMark)) rule.mark = name;
  2055. });
  2056. };
  2057. for (var name in schema.marks) {
  2058. _loop2(name);
  2059. }
  2060. var _loop3 = function _loop3(_name) {
  2061. var rules = schema.nodes[_name].spec.parseDOM;
  2062. if (rules) rules.forEach(function (rule) {
  2063. insert(rule = copy(rule));
  2064. if (!(rule.node || rule.ignore || rule.mark)) rule.node = _name;
  2065. });
  2066. };
  2067. for (var _name in schema.nodes) {
  2068. _loop3(_name);
  2069. }
  2070. return result;
  2071. }
  2072. }, {
  2073. key: "fromSchema",
  2074. value: function fromSchema(schema) {
  2075. return schema.cached.domParser || (schema.cached.domParser = new DOMParser(schema, DOMParser.schemaRules(schema)));
  2076. }
  2077. }]);
  2078. return DOMParser;
  2079. }();
  2080. var blockTags = {
  2081. address: true,
  2082. article: true,
  2083. aside: true,
  2084. blockquote: true,
  2085. canvas: true,
  2086. dd: true,
  2087. div: true,
  2088. dl: true,
  2089. fieldset: true,
  2090. figcaption: true,
  2091. figure: true,
  2092. footer: true,
  2093. form: true,
  2094. h1: true,
  2095. h2: true,
  2096. h3: true,
  2097. h4: true,
  2098. h5: true,
  2099. h6: true,
  2100. header: true,
  2101. hgroup: true,
  2102. hr: true,
  2103. li: true,
  2104. noscript: true,
  2105. ol: true,
  2106. output: true,
  2107. p: true,
  2108. pre: true,
  2109. section: true,
  2110. table: true,
  2111. tfoot: true,
  2112. ul: true
  2113. };
  2114. var ignoreTags = {
  2115. head: true,
  2116. noscript: true,
  2117. object: true,
  2118. script: true,
  2119. style: true,
  2120. title: true
  2121. };
  2122. var listTags = {
  2123. ol: true,
  2124. ul: true
  2125. };
  2126. var OPT_PRESERVE_WS = 1,
  2127. OPT_PRESERVE_WS_FULL = 2,
  2128. OPT_OPEN_LEFT = 4;
  2129. function wsOptionsFor(type, preserveWhitespace, base) {
  2130. if (preserveWhitespace != null) return (preserveWhitespace ? OPT_PRESERVE_WS : 0) | (preserveWhitespace === "full" ? OPT_PRESERVE_WS_FULL : 0);
  2131. return type && type.whitespace == "pre" ? OPT_PRESERVE_WS | OPT_PRESERVE_WS_FULL : base & ~OPT_OPEN_LEFT;
  2132. }
  2133. var NodeContext = function () {
  2134. function NodeContext(type, attrs, marks, pendingMarks, solid, match, options) {
  2135. _classCallCheck(this, NodeContext);
  2136. this.type = type;
  2137. this.attrs = attrs;
  2138. this.marks = marks;
  2139. this.pendingMarks = pendingMarks;
  2140. this.solid = solid;
  2141. this.options = options;
  2142. this.content = [];
  2143. this.activeMarks = Mark.none;
  2144. this.stashMarks = [];
  2145. this.match = match || (options & OPT_OPEN_LEFT ? null : type.contentMatch);
  2146. }
  2147. _createClass(NodeContext, [{
  2148. key: "findWrapping",
  2149. value: function findWrapping(node) {
  2150. if (!this.match) {
  2151. if (!this.type) return [];
  2152. var fill = this.type.contentMatch.fillBefore(Fragment.from(node));
  2153. if (fill) {
  2154. this.match = this.type.contentMatch.matchFragment(fill);
  2155. } else {
  2156. var start = this.type.contentMatch,
  2157. wrap;
  2158. if (wrap = start.findWrapping(node.type)) {
  2159. this.match = start;
  2160. return wrap;
  2161. } else {
  2162. return null;
  2163. }
  2164. }
  2165. }
  2166. return this.match.findWrapping(node.type);
  2167. }
  2168. }, {
  2169. key: "finish",
  2170. value: function finish(openEnd) {
  2171. if (!(this.options & OPT_PRESERVE_WS)) {
  2172. var last = this.content[this.content.length - 1],
  2173. m;
  2174. if (last && last.isText && (m = /[ \t\r\n\u000c]+$/.exec(last.text))) {
  2175. var text = last;
  2176. if (last.text.length == m[0].length) this.content.pop();else this.content[this.content.length - 1] = text.withText(text.text.slice(0, text.text.length - m[0].length));
  2177. }
  2178. }
  2179. var content = Fragment.from(this.content);
  2180. if (!openEnd && this.match) content = content.append(this.match.fillBefore(Fragment.empty, true));
  2181. return this.type ? this.type.create(this.attrs, content, this.marks) : content;
  2182. }
  2183. }, {
  2184. key: "popFromStashMark",
  2185. value: function popFromStashMark(mark) {
  2186. for (var i = this.stashMarks.length - 1; i >= 0; i--) if (mark.eq(this.stashMarks[i])) return this.stashMarks.splice(i, 1)[0];
  2187. }
  2188. }, {
  2189. key: "applyPending",
  2190. value: function applyPending(nextType) {
  2191. for (var i = 0, pending = this.pendingMarks; i < pending.length; i++) {
  2192. var mark = pending[i];
  2193. if ((this.type ? this.type.allowsMarkType(mark.type) : markMayApply(mark.type, nextType)) && !mark.isInSet(this.activeMarks)) {
  2194. this.activeMarks = mark.addToSet(this.activeMarks);
  2195. this.pendingMarks = mark.removeFromSet(this.pendingMarks);
  2196. }
  2197. }
  2198. }
  2199. }, {
  2200. key: "inlineContext",
  2201. value: function inlineContext(node) {
  2202. if (this.type) return this.type.inlineContent;
  2203. if (this.content.length) return this.content[0].isInline;
  2204. return node.parentNode && !blockTags.hasOwnProperty(node.parentNode.nodeName.toLowerCase());
  2205. }
  2206. }]);
  2207. return NodeContext;
  2208. }();
  2209. var ParseContext = function () {
  2210. function ParseContext(parser, options, isOpen) {
  2211. _classCallCheck(this, ParseContext);
  2212. this.parser = parser;
  2213. this.options = options;
  2214. this.isOpen = isOpen;
  2215. this.open = 0;
  2216. var topNode = options.topNode,
  2217. topContext;
  2218. var topOptions = wsOptionsFor(null, options.preserveWhitespace, 0) | (isOpen ? OPT_OPEN_LEFT : 0);
  2219. if (topNode) topContext = new NodeContext(topNode.type, topNode.attrs, Mark.none, Mark.none, true, options.topMatch || topNode.type.contentMatch, topOptions);else if (isOpen) topContext = new NodeContext(null, null, Mark.none, Mark.none, true, null, topOptions);else topContext = new NodeContext(parser.schema.topNodeType, null, Mark.none, Mark.none, true, null, topOptions);
  2220. this.nodes = [topContext];
  2221. this.find = options.findPositions;
  2222. this.needsBlock = false;
  2223. }
  2224. _createClass(ParseContext, [{
  2225. key: "top",
  2226. get: function get() {
  2227. return this.nodes[this.open];
  2228. }
  2229. }, {
  2230. key: "addDOM",
  2231. value: function addDOM(dom) {
  2232. if (dom.nodeType == 3) this.addTextNode(dom);else if (dom.nodeType == 1) this.addElement(dom);
  2233. }
  2234. }, {
  2235. key: "withStyleRules",
  2236. value: function withStyleRules(dom, f) {
  2237. var style = dom.getAttribute("style");
  2238. if (!style) return f();
  2239. var marks = this.readStyles(parseStyles(style));
  2240. if (!marks) return;
  2241. var _marks = _slicedToArray(marks, 2),
  2242. addMarks = _marks[0],
  2243. removeMarks = _marks[1],
  2244. top = this.top;
  2245. for (var i = 0; i < removeMarks.length; i++) this.removePendingMark(removeMarks[i], top);
  2246. for (var _i4 = 0; _i4 < addMarks.length; _i4++) this.addPendingMark(addMarks[_i4]);
  2247. f();
  2248. for (var _i5 = 0; _i5 < addMarks.length; _i5++) this.removePendingMark(addMarks[_i5], top);
  2249. for (var _i6 = 0; _i6 < removeMarks.length; _i6++) this.addPendingMark(removeMarks[_i6]);
  2250. }
  2251. }, {
  2252. key: "addTextNode",
  2253. value: function addTextNode(dom) {
  2254. var value = dom.nodeValue;
  2255. var top = this.top;
  2256. if (top.options & OPT_PRESERVE_WS_FULL || top.inlineContext(dom) || /[^ \t\r\n\u000c]/.test(value)) {
  2257. if (!(top.options & OPT_PRESERVE_WS)) {
  2258. value = value.replace(/[ \t\r\n\u000c]+/g, " ");
  2259. if (/^[ \t\r\n\u000c]/.test(value) && this.open == this.nodes.length - 1) {
  2260. var nodeBefore = top.content[top.content.length - 1];
  2261. var domNodeBefore = dom.previousSibling;
  2262. if (!nodeBefore || domNodeBefore && domNodeBefore.nodeName == 'BR' || nodeBefore.isText && /[ \t\r\n\u000c]$/.test(nodeBefore.text)) value = value.slice(1);
  2263. }
  2264. } else if (!(top.options & OPT_PRESERVE_WS_FULL)) {
  2265. value = value.replace(/\r?\n|\r/g, " ");
  2266. } else {
  2267. value = value.replace(/\r\n?/g, "\n");
  2268. }
  2269. if (value) this.insertNode(this.parser.schema.text(value));
  2270. this.findInText(dom);
  2271. } else {
  2272. this.findInside(dom);
  2273. }
  2274. }
  2275. }, {
  2276. key: "addElement",
  2277. value: function addElement(dom, matchAfter) {
  2278. var _this3 = this;
  2279. var name = dom.nodeName.toLowerCase(),
  2280. ruleID;
  2281. if (listTags.hasOwnProperty(name) && this.parser.normalizeLists) normalizeList(dom);
  2282. var rule = this.options.ruleFromNode && this.options.ruleFromNode(dom) || (ruleID = this.parser.matchTag(dom, this, matchAfter));
  2283. if (rule ? rule.ignore : ignoreTags.hasOwnProperty(name)) {
  2284. this.findInside(dom);
  2285. this.ignoreFallback(dom);
  2286. } else if (!rule || rule.skip || rule.closeParent) {
  2287. if (rule && rule.closeParent) this.open = Math.max(0, this.open - 1);else if (rule && rule.skip.nodeType) dom = rule.skip;
  2288. var sync,
  2289. top = this.top,
  2290. oldNeedsBlock = this.needsBlock;
  2291. if (blockTags.hasOwnProperty(name)) {
  2292. if (top.content.length && top.content[0].isInline && this.open) {
  2293. this.open--;
  2294. top = this.top;
  2295. }
  2296. sync = true;
  2297. if (!top.type) this.needsBlock = true;
  2298. } else if (!dom.firstChild) {
  2299. this.leafFallback(dom);
  2300. return;
  2301. }
  2302. if (rule && rule.skip) this.addAll(dom);else this.withStyleRules(dom, function () {
  2303. return _this3.addAll(dom);
  2304. });
  2305. if (sync) this.sync(top);
  2306. this.needsBlock = oldNeedsBlock;
  2307. } else {
  2308. this.withStyleRules(dom, function () {
  2309. _this3.addElementByRule(dom, rule, rule.consuming === false ? ruleID : undefined);
  2310. });
  2311. }
  2312. }
  2313. }, {
  2314. key: "leafFallback",
  2315. value: function leafFallback(dom) {
  2316. if (dom.nodeName == "BR" && this.top.type && this.top.type.inlineContent) this.addTextNode(dom.ownerDocument.createTextNode("\n"));
  2317. }
  2318. }, {
  2319. key: "ignoreFallback",
  2320. value: function ignoreFallback(dom) {
  2321. if (dom.nodeName == "BR" && (!this.top.type || !this.top.type.inlineContent)) this.findPlace(this.parser.schema.text("-"));
  2322. }
  2323. }, {
  2324. key: "readStyles",
  2325. value: function readStyles(styles) {
  2326. var _this4 = this;
  2327. var add = Mark.none,
  2328. remove = Mark.none;
  2329. for (var i = 0; i < styles.length; i += 2) {
  2330. var _loop4 = function _loop4(_after) {
  2331. var rule = _this4.parser.matchStyle(styles[i], styles[i + 1], _this4, _after);
  2332. if (!rule) {
  2333. after = _after;
  2334. return 0;
  2335. }
  2336. if (rule.ignore) return {
  2337. v: null
  2338. };
  2339. if (rule.clearMark) {
  2340. _this4.top.pendingMarks.concat(_this4.top.activeMarks).forEach(function (m) {
  2341. if (rule.clearMark(m)) remove = m.addToSet(remove);
  2342. });
  2343. } else {
  2344. add = _this4.parser.schema.marks[rule.mark].create(rule.attrs).addToSet(add);
  2345. }
  2346. if (rule.consuming === false) _after = rule;else {
  2347. after = _after;
  2348. return 0;
  2349. }
  2350. after = _after;
  2351. },
  2352. _ret;
  2353. for (var after = undefined;;) {
  2354. _ret = _loop4(after);
  2355. if (_ret === 0) break;
  2356. if (_ret) return _ret.v;
  2357. }
  2358. }
  2359. return [add, remove];
  2360. }
  2361. }, {
  2362. key: "addElementByRule",
  2363. value: function addElementByRule(dom, rule, continueAfter) {
  2364. var _this5 = this;
  2365. var sync, nodeType, mark;
  2366. if (rule.node) {
  2367. nodeType = this.parser.schema.nodes[rule.node];
  2368. if (!nodeType.isLeaf) {
  2369. sync = this.enter(nodeType, rule.attrs || null, rule.preserveWhitespace);
  2370. } else if (!this.insertNode(nodeType.create(rule.attrs))) {
  2371. this.leafFallback(dom);
  2372. }
  2373. } else {
  2374. var markType = this.parser.schema.marks[rule.mark];
  2375. mark = markType.create(rule.attrs);
  2376. this.addPendingMark(mark);
  2377. }
  2378. var startIn = this.top;
  2379. if (nodeType && nodeType.isLeaf) {
  2380. this.findInside(dom);
  2381. } else if (continueAfter) {
  2382. this.addElement(dom, continueAfter);
  2383. } else if (rule.getContent) {
  2384. this.findInside(dom);
  2385. rule.getContent(dom, this.parser.schema).forEach(function (node) {
  2386. return _this5.insertNode(node);
  2387. });
  2388. } else {
  2389. var contentDOM = dom;
  2390. if (typeof rule.contentElement == "string") contentDOM = dom.querySelector(rule.contentElement);else if (typeof rule.contentElement == "function") contentDOM = rule.contentElement(dom);else if (rule.contentElement) contentDOM = rule.contentElement;
  2391. this.findAround(dom, contentDOM, true);
  2392. this.addAll(contentDOM);
  2393. }
  2394. if (sync && this.sync(startIn)) this.open--;
  2395. if (mark) this.removePendingMark(mark, startIn);
  2396. }
  2397. }, {
  2398. key: "addAll",
  2399. value: function addAll(parent, startIndex, endIndex) {
  2400. var index = startIndex || 0;
  2401. for (var dom = startIndex ? parent.childNodes[startIndex] : parent.firstChild, end = endIndex == null ? null : parent.childNodes[endIndex]; dom != end; dom = dom.nextSibling, ++index) {
  2402. this.findAtPoint(parent, index);
  2403. this.addDOM(dom);
  2404. }
  2405. this.findAtPoint(parent, index);
  2406. }
  2407. }, {
  2408. key: "findPlace",
  2409. value: function findPlace(node) {
  2410. var route, sync;
  2411. for (var depth = this.open; depth >= 0; depth--) {
  2412. var cx = this.nodes[depth];
  2413. var _found2 = cx.findWrapping(node);
  2414. if (_found2 && (!route || route.length > _found2.length)) {
  2415. route = _found2;
  2416. sync = cx;
  2417. if (!_found2.length) break;
  2418. }
  2419. if (cx.solid) break;
  2420. }
  2421. if (!route) return false;
  2422. this.sync(sync);
  2423. for (var i = 0; i < route.length; i++) this.enterInner(route[i], null, false);
  2424. return true;
  2425. }
  2426. }, {
  2427. key: "insertNode",
  2428. value: function insertNode(node) {
  2429. if (node.isInline && this.needsBlock && !this.top.type) {
  2430. var block = this.textblockFromContext();
  2431. if (block) this.enterInner(block);
  2432. }
  2433. if (this.findPlace(node)) {
  2434. this.closeExtra();
  2435. var top = this.top;
  2436. top.applyPending(node.type);
  2437. if (top.match) top.match = top.match.matchType(node.type);
  2438. var marks = top.activeMarks;
  2439. for (var i = 0; i < node.marks.length; i++) if (!top.type || top.type.allowsMarkType(node.marks[i].type)) marks = node.marks[i].addToSet(marks);
  2440. top.content.push(node.mark(marks));
  2441. return true;
  2442. }
  2443. return false;
  2444. }
  2445. }, {
  2446. key: "enter",
  2447. value: function enter(type, attrs, preserveWS) {
  2448. var ok = this.findPlace(type.create(attrs));
  2449. if (ok) this.enterInner(type, attrs, true, preserveWS);
  2450. return ok;
  2451. }
  2452. }, {
  2453. key: "enterInner",
  2454. value: function enterInner(type) {
  2455. var attrs = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
  2456. var solid = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  2457. var preserveWS = arguments.length > 3 ? arguments[3] : undefined;
  2458. this.closeExtra();
  2459. var top = this.top;
  2460. top.applyPending(type);
  2461. top.match = top.match && top.match.matchType(type);
  2462. var options = wsOptionsFor(type, preserveWS, top.options);
  2463. if (top.options & OPT_OPEN_LEFT && top.content.length == 0) options |= OPT_OPEN_LEFT;
  2464. this.nodes.push(new NodeContext(type, attrs, top.activeMarks, top.pendingMarks, solid, null, options));
  2465. this.open++;
  2466. }
  2467. }, {
  2468. key: "closeExtra",
  2469. value: function closeExtra() {
  2470. var openEnd = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  2471. var i = this.nodes.length - 1;
  2472. if (i > this.open) {
  2473. for (; i > this.open; i--) this.nodes[i - 1].content.push(this.nodes[i].finish(openEnd));
  2474. this.nodes.length = this.open + 1;
  2475. }
  2476. }
  2477. }, {
  2478. key: "finish",
  2479. value: function finish() {
  2480. this.open = 0;
  2481. this.closeExtra(this.isOpen);
  2482. return this.nodes[0].finish(this.isOpen || this.options.topOpen);
  2483. }
  2484. }, {
  2485. key: "sync",
  2486. value: function sync(to) {
  2487. for (var i = this.open; i >= 0; i--) if (this.nodes[i] == to) {
  2488. this.open = i;
  2489. return true;
  2490. }
  2491. return false;
  2492. }
  2493. }, {
  2494. key: "currentPos",
  2495. get: function get() {
  2496. this.closeExtra();
  2497. var pos = 0;
  2498. for (var i = this.open; i >= 0; i--) {
  2499. var content = this.nodes[i].content;
  2500. for (var j = content.length - 1; j >= 0; j--) pos += content[j].nodeSize;
  2501. if (i) pos++;
  2502. }
  2503. return pos;
  2504. }
  2505. }, {
  2506. key: "findAtPoint",
  2507. value: function findAtPoint(parent, offset) {
  2508. if (this.find) for (var i = 0; i < this.find.length; i++) {
  2509. if (this.find[i].node == parent && this.find[i].offset == offset) this.find[i].pos = this.currentPos;
  2510. }
  2511. }
  2512. }, {
  2513. key: "findInside",
  2514. value: function findInside(parent) {
  2515. if (this.find) for (var i = 0; i < this.find.length; i++) {
  2516. if (this.find[i].pos == null && parent.nodeType == 1 && parent.contains(this.find[i].node)) this.find[i].pos = this.currentPos;
  2517. }
  2518. }
  2519. }, {
  2520. key: "findAround",
  2521. value: function findAround(parent, content, before) {
  2522. if (parent != content && this.find) for (var i = 0; i < this.find.length; i++) {
  2523. if (this.find[i].pos == null && parent.nodeType == 1 && parent.contains(this.find[i].node)) {
  2524. var pos = content.compareDocumentPosition(this.find[i].node);
  2525. if (pos & (before ? 2 : 4)) this.find[i].pos = this.currentPos;
  2526. }
  2527. }
  2528. }
  2529. }, {
  2530. key: "findInText",
  2531. value: function findInText(textNode) {
  2532. if (this.find) for (var i = 0; i < this.find.length; i++) {
  2533. if (this.find[i].node == textNode) this.find[i].pos = this.currentPos - (textNode.nodeValue.length - this.find[i].offset);
  2534. }
  2535. }
  2536. }, {
  2537. key: "matchesContext",
  2538. value: function matchesContext(context) {
  2539. var _this6 = this;
  2540. if (context.indexOf("|") > -1) return context.split(/\s*\|\s*/).some(this.matchesContext, this);
  2541. var parts = context.split("/");
  2542. var option = this.options.context;
  2543. var useRoot = !this.isOpen && (!option || option.parent.type == this.nodes[0].type);
  2544. var minDepth = -(option ? option.depth + 1 : 0) + (useRoot ? 0 : 1);
  2545. var match = function match(i, depth) {
  2546. for (; i >= 0; i--) {
  2547. var part = parts[i];
  2548. if (part == "") {
  2549. if (i == parts.length - 1 || i == 0) continue;
  2550. for (; depth >= minDepth; depth--) if (match(i - 1, depth)) return true;
  2551. return false;
  2552. } else {
  2553. var next = depth > 0 || depth == 0 && useRoot ? _this6.nodes[depth].type : option && depth >= minDepth ? option.node(depth - minDepth).type : null;
  2554. if (!next || next.name != part && next.groups.indexOf(part) == -1) return false;
  2555. depth--;
  2556. }
  2557. }
  2558. return true;
  2559. };
  2560. return match(parts.length - 1, this.open);
  2561. }
  2562. }, {
  2563. key: "textblockFromContext",
  2564. value: function textblockFromContext() {
  2565. var $context = this.options.context;
  2566. if ($context) for (var d = $context.depth; d >= 0; d--) {
  2567. var deflt = $context.node(d).contentMatchAt($context.indexAfter(d)).defaultType;
  2568. if (deflt && deflt.isTextblock && deflt.defaultAttrs) return deflt;
  2569. }
  2570. for (var name in this.parser.schema.nodes) {
  2571. var type = this.parser.schema.nodes[name];
  2572. if (type.isTextblock && type.defaultAttrs) return type;
  2573. }
  2574. }
  2575. }, {
  2576. key: "addPendingMark",
  2577. value: function addPendingMark(mark) {
  2578. var found = findSameMarkInSet(mark, this.top.pendingMarks);
  2579. if (found) this.top.stashMarks.push(found);
  2580. this.top.pendingMarks = mark.addToSet(this.top.pendingMarks);
  2581. }
  2582. }, {
  2583. key: "removePendingMark",
  2584. value: function removePendingMark(mark, upto) {
  2585. for (var depth = this.open; depth >= 0; depth--) {
  2586. var level = this.nodes[depth];
  2587. var _found3 = level.pendingMarks.lastIndexOf(mark);
  2588. if (_found3 > -1) {
  2589. level.pendingMarks = mark.removeFromSet(level.pendingMarks);
  2590. } else {
  2591. level.activeMarks = mark.removeFromSet(level.activeMarks);
  2592. var stashMark = level.popFromStashMark(mark);
  2593. if (stashMark && level.type && level.type.allowsMarkType(stashMark.type)) level.activeMarks = stashMark.addToSet(level.activeMarks);
  2594. }
  2595. if (level == upto) break;
  2596. }
  2597. }
  2598. }]);
  2599. return ParseContext;
  2600. }();
  2601. function normalizeList(dom) {
  2602. for (var child = dom.firstChild, prevItem = null; child; child = child.nextSibling) {
  2603. var name = child.nodeType == 1 ? child.nodeName.toLowerCase() : null;
  2604. if (name && listTags.hasOwnProperty(name) && prevItem) {
  2605. prevItem.appendChild(child);
  2606. child = prevItem;
  2607. } else if (name == "li") {
  2608. prevItem = child;
  2609. } else if (name) {
  2610. prevItem = null;
  2611. }
  2612. }
  2613. }
  2614. function matches(dom, selector) {
  2615. return (dom.matches || dom.msMatchesSelector || dom.webkitMatchesSelector || dom.mozMatchesSelector).call(dom, selector);
  2616. }
  2617. function parseStyles(style) {
  2618. var re = /\s*([\w-]+)\s*:\s*([^;]+)/g,
  2619. m,
  2620. result = [];
  2621. while (m = re.exec(style)) result.push(m[1], m[2].trim());
  2622. return result;
  2623. }
  2624. function copy(obj) {
  2625. var copy = {};
  2626. for (var prop in obj) copy[prop] = obj[prop];
  2627. return copy;
  2628. }
  2629. function markMayApply(markType, nodeType) {
  2630. var nodes = nodeType.schema.nodes;
  2631. var _loop5 = function _loop5() {
  2632. var parent = nodes[name];
  2633. if (!parent.allowsMarkType(markType)) return 0;
  2634. var seen = [],
  2635. scan = function scan(match) {
  2636. seen.push(match);
  2637. for (var i = 0; i < match.edgeCount; i++) {
  2638. var _match$edge = match.edge(i),
  2639. type = _match$edge.type,
  2640. next = _match$edge.next;
  2641. if (type == nodeType) return true;
  2642. if (seen.indexOf(next) < 0 && scan(next)) return true;
  2643. }
  2644. };
  2645. if (scan(parent.contentMatch)) return {
  2646. v: true
  2647. };
  2648. },
  2649. _ret2;
  2650. for (var name in nodes) {
  2651. _ret2 = _loop5();
  2652. if (_ret2 === 0) continue;
  2653. if (_ret2) return _ret2.v;
  2654. }
  2655. }
  2656. function findSameMarkInSet(mark, set) {
  2657. for (var i = 0; i < set.length; i++) {
  2658. if (mark.eq(set[i])) return set[i];
  2659. }
  2660. }
  2661. var DOMSerializer = function () {
  2662. function DOMSerializer(nodes, marks) {
  2663. _classCallCheck(this, DOMSerializer);
  2664. this.nodes = nodes;
  2665. this.marks = marks;
  2666. }
  2667. _createClass(DOMSerializer, [{
  2668. key: "serializeFragment",
  2669. value: function serializeFragment(fragment) {
  2670. var _this7 = this;
  2671. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  2672. var target = arguments.length > 2 ? arguments[2] : undefined;
  2673. if (!target) target = doc(options).createDocumentFragment();
  2674. var top = target,
  2675. active = [];
  2676. fragment.forEach(function (node) {
  2677. if (active.length || node.marks.length) {
  2678. var keep = 0,
  2679. rendered = 0;
  2680. while (keep < active.length && rendered < node.marks.length) {
  2681. var next = node.marks[rendered];
  2682. if (!_this7.marks[next.type.name]) {
  2683. rendered++;
  2684. continue;
  2685. }
  2686. if (!next.eq(active[keep][0]) || next.type.spec.spanning === false) break;
  2687. keep++;
  2688. rendered++;
  2689. }
  2690. while (keep < active.length) top = active.pop()[1];
  2691. while (rendered < node.marks.length) {
  2692. var add = node.marks[rendered++];
  2693. var markDOM = _this7.serializeMark(add, node.isInline, options);
  2694. if (markDOM) {
  2695. active.push([add, top]);
  2696. top.appendChild(markDOM.dom);
  2697. top = markDOM.contentDOM || markDOM.dom;
  2698. }
  2699. }
  2700. }
  2701. top.appendChild(_this7.serializeNodeInner(node, options));
  2702. });
  2703. return target;
  2704. }
  2705. }, {
  2706. key: "serializeNodeInner",
  2707. value: function serializeNodeInner(node, options) {
  2708. var _DOMSerializer$render = DOMSerializer.renderSpec(doc(options), this.nodes[node.type.name](node)),
  2709. dom = _DOMSerializer$render.dom,
  2710. contentDOM = _DOMSerializer$render.contentDOM;
  2711. if (contentDOM) {
  2712. if (node.isLeaf) throw new RangeError("Content hole not allowed in a leaf node spec");
  2713. this.serializeFragment(node.content, options, contentDOM);
  2714. }
  2715. return dom;
  2716. }
  2717. }, {
  2718. key: "serializeNode",
  2719. value: function serializeNode(node) {
  2720. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  2721. var dom = this.serializeNodeInner(node, options);
  2722. for (var i = node.marks.length - 1; i >= 0; i--) {
  2723. var wrap = this.serializeMark(node.marks[i], node.isInline, options);
  2724. if (wrap) {
  2725. (wrap.contentDOM || wrap.dom).appendChild(dom);
  2726. dom = wrap.dom;
  2727. }
  2728. }
  2729. return dom;
  2730. }
  2731. }, {
  2732. key: "serializeMark",
  2733. value: function serializeMark(mark, inline) {
  2734. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  2735. var toDOM = this.marks[mark.type.name];
  2736. return toDOM && DOMSerializer.renderSpec(doc(options), toDOM(mark, inline));
  2737. }
  2738. }], [{
  2739. key: "renderSpec",
  2740. value: function renderSpec(doc, structure) {
  2741. var xmlNS = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
  2742. if (typeof structure == "string") return {
  2743. dom: doc.createTextNode(structure)
  2744. };
  2745. if (structure.nodeType != null) return {
  2746. dom: structure
  2747. };
  2748. if (structure.dom && structure.dom.nodeType != null) return structure;
  2749. var tagName = structure[0],
  2750. space = tagName.indexOf(" ");
  2751. if (space > 0) {
  2752. xmlNS = tagName.slice(0, space);
  2753. tagName = tagName.slice(space + 1);
  2754. }
  2755. var contentDOM;
  2756. var dom = xmlNS ? doc.createElementNS(xmlNS, tagName) : doc.createElement(tagName);
  2757. var attrs = structure[1],
  2758. start = 1;
  2759. if (attrs && _typeof(attrs) == "object" && attrs.nodeType == null && !Array.isArray(attrs)) {
  2760. start = 2;
  2761. for (var name in attrs) if (attrs[name] != null) {
  2762. var _space = name.indexOf(" ");
  2763. if (_space > 0) dom.setAttributeNS(name.slice(0, _space), name.slice(_space + 1), attrs[name]);else dom.setAttribute(name, attrs[name]);
  2764. }
  2765. }
  2766. for (var i = start; i < structure.length; i++) {
  2767. var child = structure[i];
  2768. if (child === 0) {
  2769. if (i < structure.length - 1 || i > start) throw new RangeError("Content hole must be the only child of its parent node");
  2770. return {
  2771. dom: dom,
  2772. contentDOM: dom
  2773. };
  2774. } else {
  2775. var _DOMSerializer$render2 = DOMSerializer.renderSpec(doc, child, xmlNS),
  2776. inner = _DOMSerializer$render2.dom,
  2777. innerContent = _DOMSerializer$render2.contentDOM;
  2778. dom.appendChild(inner);
  2779. if (innerContent) {
  2780. if (contentDOM) throw new RangeError("Multiple content holes");
  2781. contentDOM = innerContent;
  2782. }
  2783. }
  2784. }
  2785. return {
  2786. dom: dom,
  2787. contentDOM: contentDOM
  2788. };
  2789. }
  2790. }, {
  2791. key: "fromSchema",
  2792. value: function fromSchema(schema) {
  2793. return schema.cached.domSerializer || (schema.cached.domSerializer = new DOMSerializer(this.nodesFromSchema(schema), this.marksFromSchema(schema)));
  2794. }
  2795. }, {
  2796. key: "nodesFromSchema",
  2797. value: function nodesFromSchema(schema) {
  2798. var result = gatherToDOM(schema.nodes);
  2799. if (!result.text) result.text = function (node) {
  2800. return node.text;
  2801. };
  2802. return result;
  2803. }
  2804. }, {
  2805. key: "marksFromSchema",
  2806. value: function marksFromSchema(schema) {
  2807. return gatherToDOM(schema.marks);
  2808. }
  2809. }]);
  2810. return DOMSerializer;
  2811. }();
  2812. function gatherToDOM(obj) {
  2813. var result = {};
  2814. for (var name in obj) {
  2815. var toDOM = obj[name].spec.toDOM;
  2816. if (toDOM) result[name] = toDOM;
  2817. }
  2818. return result;
  2819. }
  2820. function doc(options) {
  2821. return options.document || window.document;
  2822. }
  2823. exports.ContentMatch = ContentMatch;
  2824. exports.DOMParser = DOMParser;
  2825. exports.DOMSerializer = DOMSerializer;
  2826. exports.Fragment = Fragment;
  2827. exports.Mark = Mark;
  2828. exports.MarkType = MarkType;
  2829. exports.Node = Node;
  2830. exports.NodeRange = NodeRange;
  2831. exports.NodeType = NodeType;
  2832. exports.ReplaceError = ReplaceError;
  2833. exports.ResolvedPos = ResolvedPos;
  2834. exports.Schema = Schema;
  2835. exports.Slice = Slice;