@mdsvex/parse 1.0.0-next.0 → 1.0.0-next.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (329) hide show
  1. package/CHANGELOG.md +213 -0
  2. package/WIRE_FORMAT.md +5 -1
  3. package/dist/buf-utils.js +23 -23
  4. package/dist/buf_utils.d.ts +1 -1
  5. package/dist/cursor.d.ts +17 -13
  6. package/dist/cursor.js +118 -31
  7. package/dist/main.d.ts +362 -26
  8. package/dist/main.js +5841 -3420
  9. package/dist/node_view-bvM0i0-p.js +407 -0
  10. package/dist/node_view.d.ts +11 -12
  11. package/dist/opcodes.d.ts +23 -6
  12. package/dist/plugin_dispatch.d.ts +22 -8
  13. package/dist/tree-builder.js +185 -66
  14. package/dist/tree_builder.d.ts +42 -11
  15. package/dist/undo_log.d.ts +11 -1
  16. package/dist/utils.d.ts +77 -51
  17. package/dist/utils.js +687 -209
  18. package/dist/wire-tree-builder.js +69 -71
  19. package/dist/wire_tree_builder.d.ts +4 -7
  20. package/package.json +1 -1
  21. package/src/buf_utils.ts +10 -14
  22. package/src/cursor.ts +74 -57
  23. package/src/main.ts +9898 -5593
  24. package/src/node_view.ts +159 -140
  25. package/src/opcodes.ts +39 -7
  26. package/src/plugin_dispatch.ts +328 -219
  27. package/src/tree_builder.ts +278 -98
  28. package/src/undo_log.ts +58 -24
  29. package/src/utils.spec.ts +86 -9
  30. package/src/utils.ts +769 -392
  31. package/src/wire_emitter.ts +6 -0
  32. package/src/wire_tree_builder.ts +39 -52
  33. package/test/block_quotes.spec.ts +78 -7
  34. package/test/code_spans.spec.ts +158 -6
  35. package/test/crlf.spec.ts +49 -1
  36. package/test/directives.spec.ts +530 -1
  37. package/test/emphasis.spec.ts +10 -4
  38. package/test/fixtures/pfm/block_quotes/pfm_fence_close.md +4 -0
  39. package/test/fixtures/pfm/block_quotes/pfm_fence_close_compact.md +4 -0
  40. package/test/fixtures/pfm/block_quotes/pfm_fence_close_end_quote.md +5 -0
  41. package/test/fixtures/pfm/block_quotes/pfm_fence_close_info.md +5 -0
  42. package/test/fixtures/pfm/block_quotes/pfm_fence_close_longer.md +4 -0
  43. package/test/fixtures/pfm/block_quotes/pfm_fence_close_nested.md +3 -0
  44. package/test/fixtures/pfm/block_quotes/pfm_fence_close_twice.md +6 -0
  45. package/test/fixtures/pfm/block_quotes/pfm_fence_info_eof.md +1 -0
  46. package/test/fixtures/pfm/block_quotes/pfm_fence_unclosed.md +2 -0
  47. package/test/fixtures/pfm/code_spans/pfm_328_5.md +1 -0
  48. package/test/fixtures/pfm/code_spans/pfm_328_6.md +1 -0
  49. package/test/fixtures/pfm/code_spans/pfm_328_7.md +1 -0
  50. package/test/fixtures/pfm/code_spans/pfm_328_8.md +1 -0
  51. package/test/fixtures/pfm/code_spans/pfm_328_9.md +2 -0
  52. package/test/fixtures/pfm/fenced_code_blocks/pfm_info_eof.md +1 -0
  53. package/test/fixtures/pfm/fenced_code_blocks/pfm_info_eof_no_lf.md +1 -0
  54. package/test/fixtures/pfm/generic_directives/106.md +1 -0
  55. package/test/fixtures/pfm/generic_directives/107.md +3 -0
  56. package/test/fixtures/pfm/generic_directives/108.md +1 -0
  57. package/test/fixtures/pfm/generic_directives/109.md +1 -0
  58. package/test/fixtures/pfm/generic_directives/110.md +1 -0
  59. package/test/fixtures/pfm/generic_directives/111.md +1 -0
  60. package/test/fixtures/pfm/generic_directives/112.md +1 -0
  61. package/test/fixtures/pfm/generic_directives/113.md +1 -0
  62. package/test/fixtures/pfm/generic_directives/114.md +2 -0
  63. package/test/fixtures/pfm/generic_directives/115.md +1 -0
  64. package/test/fixtures/pfm/generic_directives/116.md +6 -0
  65. package/test/fixtures/pfm/generic_directives/117.md +3 -0
  66. package/test/fixtures/pfm/generic_directives/118.md +6 -0
  67. package/test/fixtures/pfm/generic_directives/119.md +3 -0
  68. package/test/fixtures/pfm/generic_directives/120.md +5 -0
  69. package/test/fixtures/pfm/generic_directives/121.md +3 -0
  70. package/test/fixtures/pfm/generic_directives/122.md +4 -0
  71. package/test/fixtures/pfm/generic_directives/123.md +2 -0
  72. package/test/html.spec.ts +44 -1
  73. package/test/html_block_containers.spec.ts +445 -0
  74. package/test/incremental.spec.ts +271 -0
  75. package/test/lists.spec.ts +107 -1
  76. package/test/parser_reuse.spec.ts +40 -0
  77. package/test/plugins.spec.ts +196 -226
  78. package/test/print.ts +2 -3
  79. package/test/run_bytecode.spec.ts +36 -0
  80. package/test/snapshots/hybrid/component-children.snap.ast +9 -16
  81. package/test/snapshots/hybrid/component-simple-00.snap.ast +1 -2
  82. package/test/snapshots/hybrid/component-simple-01.snap.ast +1 -2
  83. package/test/snapshots/hybrid/component-simple-03.snap.ast +13 -13
  84. package/test/snapshots/hybrid/component-simple-04.snap.ast +1 -2
  85. package/test/snapshots/hybrid/error.snap.ast +1 -2
  86. package/test/snapshots/markdown/blockquote-nested-markdown.snap.ast +2 -1
  87. package/test/snapshots/markdown/emphasis.snap.ast +2 -2
  88. package/test/snapshots/markdown/encodeHTMLCodeTags.snap.ast +1 -2
  89. package/test/snapshots/markdown/github-style-codeblock-inside-quote.snap.ast +8 -1
  90. package/test/snapshots/markdown/html-comments.snap.ast +11 -13
  91. package/test/snapshots/markdown/html5-strutural-tags.snap.ast +16 -31
  92. package/test/snapshots/markdown/line-starts-with-html.snap.ast +5 -6
  93. package/test/snapshots/markdown/literal-html-tags.snap.ast +11 -14
  94. package/test/snapshots/markdown/nested-blockquote.snap.ast +3 -3
  95. package/test/snapshots/markdown/pre-code-tags.snap.ast +5 -8
  96. package/test/snapshots/pfm/block_quotes/237.snap.ast +1 -2
  97. package/test/snapshots/pfm/block_quotes/251.snap.ast +5 -5
  98. package/test/snapshots/pfm/block_quotes/pfm_fence_close.snap.ast +6 -0
  99. package/test/snapshots/pfm/block_quotes/pfm_fence_close_compact.snap.ast +6 -0
  100. package/test/snapshots/pfm/block_quotes/pfm_fence_close_end_quote.snap.ast +8 -0
  101. package/test/snapshots/pfm/block_quotes/pfm_fence_close_info.snap.ast +4 -0
  102. package/test/snapshots/pfm/block_quotes/pfm_fence_close_longer.snap.ast +6 -0
  103. package/test/snapshots/pfm/block_quotes/pfm_fence_close_nested.snap.ast +5 -0
  104. package/test/snapshots/pfm/block_quotes/pfm_fence_close_twice.snap.ast +5 -0
  105. package/test/snapshots/pfm/block_quotes/pfm_fence_info_eof.snap.ast +3 -0
  106. package/test/snapshots/pfm/block_quotes/pfm_fence_unclosed.snap.ast +3 -0
  107. package/test/snapshots/pfm/code_spans/344.snap.ast +1 -1
  108. package/test/snapshots/pfm/code_spans/347.snap.ast +1 -2
  109. package/test/snapshots/pfm/code_spans/pfm_328_5.snap.ast +6 -0
  110. package/test/snapshots/pfm/code_spans/pfm_328_6.snap.ast +6 -0
  111. package/test/snapshots/pfm/code_spans/pfm_328_7.snap.ast +6 -0
  112. package/test/snapshots/pfm/code_spans/pfm_328_8.snap.ast +8 -0
  113. package/test/snapshots/pfm/code_spans/pfm_328_9.snap.ast +10 -0
  114. package/test/snapshots/pfm/emphasis_and_strong_emphasis/417.snap.ast +2 -2
  115. package/test/snapshots/pfm/emphasis_and_strong_emphasis/438.snap.ast +1 -1
  116. package/test/snapshots/pfm/emphasis_and_strong_emphasis/441.snap.ast +1 -1
  117. package/test/snapshots/pfm/emphasis_and_strong_emphasis/444.snap.ast +1 -1
  118. package/test/snapshots/pfm/emphasis_and_strong_emphasis/445.snap.ast +1 -1
  119. package/test/snapshots/pfm/emphasis_and_strong_emphasis/450.snap.ast +1 -1
  120. package/test/snapshots/pfm/emphasis_and_strong_emphasis/453.snap.ast +1 -1
  121. package/test/snapshots/pfm/emphasis_and_strong_emphasis/456.snap.ast +1 -1
  122. package/test/snapshots/pfm/emphasis_and_strong_emphasis/457.snap.ast +1 -1
  123. package/test/snapshots/pfm/emphasis_and_strong_emphasis/473.snap.ast +1 -1
  124. package/test/snapshots/pfm/emphasis_and_strong_emphasis/475.snap.ast +1 -1
  125. package/test/snapshots/pfm/emphasis_and_strong_emphasis/476.snap.ast +1 -1
  126. package/test/snapshots/pfm/emphasis_and_strong_emphasis/477.snap.ast +1 -1
  127. package/test/snapshots/pfm/emphasis_and_strong_emphasis/478.snap.ast +3 -4
  128. package/test/snapshots/pfm/emphasis_and_strong_emphasis/479.snap.ast +3 -4
  129. package/test/snapshots/pfm/fenced_code_blocks/126.snap.ast +1 -2
  130. package/test/snapshots/pfm/fenced_code_blocks/pfm_info_eof.snap.ast +2 -0
  131. package/test/snapshots/pfm/fenced_code_blocks/pfm_info_eof_no_lf.snap.ast +2 -0
  132. package/test/snapshots/pfm/generic_directives/011.snap.ast +3 -1
  133. package/test/snapshots/pfm/generic_directives/015.snap.ast +3 -1
  134. package/test/snapshots/pfm/generic_directives/016.snap.ast +3 -1
  135. package/test/snapshots/pfm/generic_directives/017.snap.ast +3 -1
  136. package/test/snapshots/pfm/generic_directives/019.snap.ast +3 -1
  137. package/test/snapshots/pfm/generic_directives/020.snap.ast +3 -1
  138. package/test/snapshots/pfm/generic_directives/030.snap.ast +4 -6
  139. package/test/snapshots/pfm/generic_directives/034.snap.ast +6 -2
  140. package/test/snapshots/pfm/generic_directives/036.snap.ast +5 -1
  141. package/test/snapshots/pfm/generic_directives/038.snap.ast +6 -2
  142. package/test/snapshots/pfm/generic_directives/039.snap.ast +3 -1
  143. package/test/snapshots/pfm/generic_directives/040.snap.ast +3 -1
  144. package/test/snapshots/pfm/generic_directives/044.snap.ast +3 -1
  145. package/test/snapshots/pfm/generic_directives/046.snap.ast +3 -1
  146. package/test/snapshots/pfm/generic_directives/089.snap.ast +1 -1
  147. package/test/snapshots/pfm/generic_directives/103.snap.ast +6 -2
  148. package/test/snapshots/pfm/generic_directives/106.snap.ast +9 -0
  149. package/test/snapshots/pfm/generic_directives/107.snap.ast +9 -0
  150. package/test/snapshots/pfm/generic_directives/108.snap.ast +5 -0
  151. package/test/snapshots/pfm/generic_directives/109.snap.ast +4 -0
  152. package/test/snapshots/pfm/generic_directives/110.snap.ast +10 -0
  153. package/test/snapshots/pfm/generic_directives/111.snap.ast +7 -0
  154. package/test/snapshots/pfm/generic_directives/112.snap.ast +18 -0
  155. package/test/snapshots/pfm/generic_directives/113.snap.ast +7 -0
  156. package/test/snapshots/pfm/generic_directives/114.snap.ast +12 -0
  157. package/test/snapshots/pfm/generic_directives/115.snap.ast +10 -0
  158. package/test/snapshots/pfm/generic_directives/116.snap.ast +13 -0
  159. package/test/snapshots/pfm/generic_directives/117.snap.ast +9 -0
  160. package/test/snapshots/pfm/generic_directives/118.snap.ast +10 -0
  161. package/test/snapshots/pfm/generic_directives/119.snap.ast +9 -0
  162. package/test/snapshots/pfm/generic_directives/120.snap.ast +12 -0
  163. package/test/snapshots/pfm/generic_directives/121.snap.ast +3 -0
  164. package/test/snapshots/pfm/generic_directives/122.snap.ast +3 -0
  165. package/test/snapshots/pfm/generic_directives/123.snap.ast +3 -0
  166. package/test/snapshots/pfm/hard_line_breaks/642.snap.ast +1 -1
  167. package/test/snapshots/pfm/hard_line_breaks/643.snap.ast +1 -1
  168. package/test/snapshots/pfm/html/148.snap.ast +1 -1
  169. package/test/snapshots/pfm/html/150.snap.ast +1 -1
  170. package/test/snapshots/pfm/html/159.snap.ast +2 -3
  171. package/test/snapshots/pfm/html/162.snap.ast +3 -4
  172. package/test/snapshots/pfm/html/163.snap.ast +1 -1
  173. package/test/snapshots/pfm/html/164.snap.ast +3 -4
  174. package/test/snapshots/pfm/html/166.snap.ast +1 -1
  175. package/test/snapshots/pfm/html/168.snap.ast +2 -3
  176. package/test/snapshots/pfm/html/169.snap.ast +5 -7
  177. package/test/snapshots/pfm/html/174.snap.ast +3 -3
  178. package/test/snapshots/pfm/html/182.snap.ast +1 -1
  179. package/test/snapshots/pfm/html/613.snap.ast +2 -2
  180. package/test/snapshots/pfm/html/615.snap.ast +2 -2
  181. package/test/snapshots/pfm/images/585.snap.ast +1 -1
  182. package/test/snapshots/pfm/images/589.snap.ast +1 -1
  183. package/test/snapshots/pfm/link_reference_definitions/201.snap.ast +1 -1
  184. package/test/snapshots/pfm/link_reference_definitions/208.snap.ast +1 -1
  185. package/test/snapshots/pfm/links/491.snap.ast +1 -1
  186. package/test/snapshots/pfm/links/494.snap.ast +2 -2
  187. package/test/snapshots/pfm/links/516.snap.ast +8 -9
  188. package/test/snapshots/pfm/links/521.snap.ast +1 -1
  189. package/test/snapshots/pfm/links/530.snap.ast +8 -12
  190. package/test/snapshots/pfm/links/534.snap.ast +1 -1
  191. package/test/snapshots/pfm/links/552.snap.ast +2 -2
  192. package/test/snapshots/pfm/links/559.snap.ast +1 -1
  193. package/test/snapshots/pfm/links/564.snap.ast +1 -1
  194. package/test/snapshots/pfm/list_items/260.snap.ast +3 -4
  195. package/test/snapshots/pfm/list_items/293.snap.ast +2 -2
  196. package/test/snapshots/pfm/tabs/16.snap.ast +2 -3
  197. package/test/snapshots/pfm/tabs/4.snap.ast +6 -5
  198. package/test/snapshots/svelte/00-introduction/00-hello-world/App.snap.ast +3 -4
  199. package/test/snapshots/svelte/00-introduction/02-styling/App.snap.ast +2 -3
  200. package/test/snapshots/svelte/00-introduction/03-nested-components/App.snap.ast +2 -3
  201. package/test/snapshots/svelte/00-introduction/03-nested-components/Nested.snap.ast +1 -2
  202. package/test/snapshots/svelte/00-introduction/04-html-tags/App.snap.ast +1 -2
  203. package/test/snapshots/svelte/01-reactivity/00-reactive-assignments/App.snap.ast +5 -6
  204. package/test/snapshots/svelte/01-reactivity/01-reactive-declarations/App.snap.ast +11 -14
  205. package/test/snapshots/svelte/01-reactivity/02-reactive-statements/App.snap.ast +5 -6
  206. package/test/snapshots/svelte/02-props/00-declaring-props/Nested.snap.ast +2 -3
  207. package/test/snapshots/svelte/02-props/01-default-values/App.snap.ast +1 -1
  208. package/test/snapshots/svelte/02-props/01-default-values/Nested.snap.ast +2 -3
  209. package/test/snapshots/svelte/02-props/02-spread-props/Info.snap.ast +16 -17
  210. package/test/snapshots/svelte/03-logic/00-if-blocks/App.snap.ast +4 -8
  211. package/test/snapshots/svelte/03-logic/01-else-blocks/App.snap.ast +4 -8
  212. package/test/snapshots/svelte/03-logic/02-else-if-blocks/App.snap.ast +6 -9
  213. package/test/snapshots/svelte/03-logic/03-each-blocks/App.snap.ast +5 -8
  214. package/test/snapshots/svelte/03-logic/04-keyed-each-blocks/App.snap.ast +4 -7
  215. package/test/snapshots/svelte/03-logic/04-keyed-each-blocks/Thing.snap.ast +4 -5
  216. package/test/snapshots/svelte/04-events/02-event-modifiers/App.snap.ast +2 -3
  217. package/test/snapshots/svelte/04-events/03-component-events/Inner.snap.ast +2 -3
  218. package/test/snapshots/svelte/04-events/04-event-forwarding/Inner.snap.ast +2 -3
  219. package/test/snapshots/svelte/04-events/05-dom-event-forwarding/FancyButton.snap.ast +2 -3
  220. package/test/snapshots/svelte/05-bindings/00-text-inputs/App.snap.ast +3 -4
  221. package/test/snapshots/svelte/05-bindings/01-numeric-inputs/App.snap.ast +9 -8
  222. package/test/snapshots/svelte/05-bindings/02-checkbox-inputs/App.snap.ast +7 -11
  223. package/test/snapshots/svelte/05-bindings/03-group-inputs/App.snap.ast +21 -30
  224. package/test/snapshots/svelte/05-bindings/04-textarea-inputs/App.snap.ast +1 -2
  225. package/test/snapshots/svelte/05-bindings/05-select-bindings/App.snap.ast +5 -9
  226. package/test/snapshots/svelte/05-bindings/06-multiple-select-bindings/App.snap.ast +18 -26
  227. package/test/snapshots/svelte/05-bindings/07-each-block-bindings/App.snap.ast +7 -11
  228. package/test/snapshots/svelte/05-bindings/08-media-elements/App.snap.ast +15 -19
  229. package/test/snapshots/svelte/05-bindings/09-dimensions/App.snap.ast +7 -9
  230. package/test/snapshots/svelte/05-bindings/11-component-bindings/App.snap.ast +1 -2
  231. package/test/snapshots/svelte/05-bindings/11-component-bindings/Keypad.snap.ast +32 -34
  232. package/test/snapshots/svelte/06-lifecycle/00-onmount/App.snap.ast +3 -6
  233. package/test/snapshots/svelte/06-lifecycle/01-ondestroy/App.snap.ast +6 -7
  234. package/test/snapshots/svelte/06-lifecycle/02-update/App.snap.ast +2 -4
  235. package/test/snapshots/svelte/07-stores/00-writable-stores/App.snap.ast +4 -5
  236. package/test/snapshots/svelte/07-stores/00-writable-stores/Decrementer.snap.ast +2 -3
  237. package/test/snapshots/svelte/07-stores/00-writable-stores/Incrementer.snap.ast +2 -3
  238. package/test/snapshots/svelte/07-stores/00-writable-stores/Resetter.snap.ast +2 -3
  239. package/test/snapshots/svelte/07-stores/01-auto-subscriptions/App.snap.ast +4 -5
  240. package/test/snapshots/svelte/07-stores/01-auto-subscriptions/Decrementer.snap.ast +2 -3
  241. package/test/snapshots/svelte/07-stores/01-auto-subscriptions/Incrementer.snap.ast +2 -3
  242. package/test/snapshots/svelte/07-stores/01-auto-subscriptions/Resetter.snap.ast +2 -3
  243. package/test/snapshots/svelte/07-stores/02-readable-stores/App.snap.ast +2 -3
  244. package/test/snapshots/svelte/07-stores/03-derived-stores/App.snap.ast +8 -10
  245. package/test/snapshots/svelte/07-stores/04-custom-stores/App.snap.ast +7 -11
  246. package/test/snapshots/svelte/08-motion/00-tweened/App.snap.ast +10 -15
  247. package/test/snapshots/svelte/08-motion/01-spring/App.snap.ast +10 -10
  248. package/test/snapshots/svelte/09-transitions/00-transition/App.snap.ast +3 -5
  249. package/test/snapshots/svelte/09-transitions/01-adding-parameters-to-transitions/App.snap.ast +3 -5
  250. package/test/snapshots/svelte/09-transitions/02-in-and-out/App.snap.ast +3 -5
  251. package/test/snapshots/svelte/09-transitions/03-custom-css-transitions/App.snap.ast +4 -6
  252. package/test/snapshots/svelte/09-transitions/04-custom-js-transitions/App.snap.ast +3 -5
  253. package/test/snapshots/svelte/09-transitions/05-transition-events/App.snap.ast +5 -8
  254. package/test/snapshots/svelte/10-animations/00-animate/App.snap.ast +16 -20
  255. package/test/snapshots/svelte/11-easing/00-easing/App.snap.ast +16 -11
  256. package/test/snapshots/svelte/11-easing/00-easing/Controls.snap.ast +11 -16
  257. package/test/snapshots/svelte/11-easing/00-easing/Grid.snap.ast +2 -2
  258. package/test/snapshots/svelte/12-svg/01-clock/App.snap.ast +2 -1
  259. package/test/snapshots/svelte/12-svg/02-bar-chart/App.snap.ast +7 -9
  260. package/test/snapshots/svelte/12-svg/03-area-chart/App.snap.ast +14 -15
  261. package/test/snapshots/svelte/12-svg/04-scatterplot/App.snap.ast +7 -5
  262. package/test/snapshots/svelte/12-svg/04-scatterplot/Scatterplot.snap.ast +6 -6
  263. package/test/snapshots/svelte/12-svg/05-svg-transitions/App.snap.ast +5 -6
  264. package/test/snapshots/svelte/13-actions/01-adding-parameters-to-actions/App.snap.ast +2 -3
  265. package/test/snapshots/svelte/14-classes/00-classes/App.snap.ast +3 -6
  266. package/test/snapshots/svelte/14-classes/01-class-shorthand/App.snap.ast +2 -3
  267. package/test/snapshots/svelte/15-composition/00-slots/App.snap.ast +3 -5
  268. package/test/snapshots/svelte/15-composition/01-slot-fallbacks/App.snap.ast +3 -5
  269. package/test/snapshots/svelte/15-composition/01-slot-fallbacks/Box.snap.ast +1 -2
  270. package/test/snapshots/svelte/15-composition/02-named-slots/App.snap.ast +7 -11
  271. package/test/snapshots/svelte/15-composition/02-named-slots/ContactCard.snap.ast +3 -6
  272. package/test/snapshots/svelte/15-composition/03-slot-props/App.snap.ast +9 -15
  273. package/test/snapshots/svelte/15-composition/04-modal/App.snap.ast +17 -26
  274. package/test/snapshots/svelte/15-composition/04-modal/Modal.snap.ast +3 -3
  275. package/test/snapshots/svelte/16-context/00-context-api/App.snap.ast +10 -5
  276. package/test/snapshots/svelte/17-special-elements/00-svelte-self/File.snap.ast +1 -2
  277. package/test/snapshots/svelte/17-special-elements/00-svelte-self/Folder.snap.ast +1 -2
  278. package/test/snapshots/svelte/17-special-elements/01-svelte-component/App.snap.ast +1 -2
  279. package/test/snapshots/svelte/17-special-elements/01-svelte-component/BlueThing.snap.ast +1 -2
  280. package/test/snapshots/svelte/17-special-elements/01-svelte-component/GreenThing.snap.ast +1 -2
  281. package/test/snapshots/svelte/17-special-elements/01-svelte-component/RedThing.snap.ast +1 -2
  282. package/test/snapshots/svelte/17-special-elements/02-svelte-window/App.snap.ast +3 -6
  283. package/test/snapshots/svelte/17-special-elements/03-svelte-window-bindings/App.snap.ast +2 -4
  284. package/test/snapshots/svelte/17-special-elements/05-svelte-head/App.snap.ast +6 -12
  285. package/test/snapshots/svelte/18-module-context/01-module-exports/App.snap.ast +2 -3
  286. package/test/snapshots/svelte/18-module-context/01-module-exports/AudioPlayer.snap.ast +4 -7
  287. package/test/snapshots/svelte/19-debugging/00-debug/App.snap.ast +5 -7
  288. package/test/snapshots/svelte/19-debugging/00-debug/Test.snap.ast +7 -11
  289. package/test/snapshots/svelte/20-7guis/01-7guis-counter/App.snap.ast +2 -3
  290. package/test/snapshots/svelte/20-7guis/02-7guis-temperature/App.snap.ast +2 -2
  291. package/test/snapshots/svelte/20-7guis/03-7guis-flight-booker/App.snap.ast +4 -7
  292. package/test/snapshots/svelte/20-7guis/04-7guis-timer/App.snap.ast +13 -17
  293. package/test/snapshots/svelte/20-7guis/05-7guis-crud/App.snap.ast +11 -13
  294. package/test/snapshots/svelte/20-7guis/06-7guis-circles/App.snap.ast +10 -11
  295. package/test/snapshots/svelte/21-miscellaneous/01-hacker-news/Comment.snap.ast +4 -6
  296. package/test/snapshots/svelte/21-miscellaneous/01-hacker-news/Item.snap.ast +9 -13
  297. package/test/snapshots/svelte/21-miscellaneous/01-hacker-news/List.snap.ast +3 -5
  298. package/test/snapshots/svelte/21-miscellaneous/01-hacker-news/Summary.snap.ast +7 -11
  299. package/test/snapshots/svelte/21-miscellaneous/02-immutable-data/App.snap.ast +2 -4
  300. package/test/snapshots/svelte/99-embeds/20181225-blog-svelte-css-in-js/App.snap.ast +6 -7
  301. package/test/snapshots/svelte/99-embeds/20181225-blog-svelte-css-in-js/Hero.snap.ast +3 -5
  302. package/test/snapshots/svelte/99-embeds/20190420-blog-write-less-code/App.snap.ast +6 -7
  303. package/test/svelte_expressions.spec.ts +93 -4
  304. package/test/svelte_tags.spec.ts +27 -12
  305. package/test/tables.spec.ts +347 -0
  306. package/test/tree_builder.spec.ts +286 -1
  307. package/test/unclosed_delimiters.spec.ts +529 -0
  308. package/tsc/buf_utils.d.ts +1 -1
  309. package/tsc/buf_utils.js +9 -9
  310. package/tsc/cursor.d.ts +17 -13
  311. package/tsc/cursor.js +60 -43
  312. package/tsc/main.d.ts +362 -26
  313. package/tsc/main.js +8955 -4891
  314. package/tsc/node_view.d.ts +11 -12
  315. package/tsc/node_view.js +134 -102
  316. package/tsc/opcodes.d.ts +23 -6
  317. package/tsc/plugin_dispatch.d.ts +22 -8
  318. package/tsc/plugin_dispatch.js +292 -170
  319. package/tsc/tree_builder.d.ts +42 -11
  320. package/tsc/tree_builder.js +237 -82
  321. package/tsc/undo_log.d.ts +11 -1
  322. package/tsc/undo_log.js +54 -19
  323. package/tsc/utils.d.ts +77 -51
  324. package/tsc/utils.js +687 -344
  325. package/tsc/utils.spec.js +83 -9
  326. package/tsc/wire_emitter.js +6 -0
  327. package/tsc/wire_tree_builder.d.ts +4 -7
  328. package/tsc/wire_tree_builder.js +34 -37
  329. package/dist/node_view-BEh2-H86.js +0 -373
package/tsc/utils.js CHANGED
@@ -1,5 +1,77 @@
1
1
  /** default number of token entries to preallocate. */
2
2
  const DEFAULT_TOKEN_CAPACITY = 128;
3
+ /** under 2 kb of slab, cheaper than a resize for a slightly larger document */
4
+ const MIN_NODE_CAPACITY = 32;
5
+ /**
6
+ * a buffer past the carve cap is its own ArrayBuffer, far costlier than a
7
+ * carve, so small documents should fit the slab
8
+ */
9
+ const SLAB_BYTES = 1048576;
10
+ /**
11
+ * caps the tail a full slab can waste at a quarter, high since a one shot
12
+ * parse gives back what it does not use
13
+ */
14
+ const SLAB_MAX_CARVE = 262144;
15
+ const EMPTY_U32 = new Uint32Array(0);
16
+ /** most documents set no metadata, the first set_metadata swaps in a real array */
17
+ const NO_META = Object.freeze([]);
18
+ const NODE_BYTES = 12 /* NodeField.stride */ * 4;
19
+ /** shared empty strings until own_strings, never written */
20
+ const NO_STRINGS = [];
21
+ /**
22
+ * uninitialized where the host allows, the template and a push write every
23
+ * word of a node before anything reads it
24
+ */
25
+ const host_buffer = globalThis.Buffer;
26
+ const unzeroed = host_buffer !== undefined &&
27
+ typeof host_buffer.allocUnsafeSlow === 'function';
28
+ function new_storage(bytes) {
29
+ return unzeroed
30
+ ? host_buffer.allocUnsafeSlow(bytes).buffer
31
+ : new ArrayBuffer(bytes);
32
+ }
33
+ /** a template copy repeats at most this many nodes, a longer source falls out of cache */
34
+ const TEMPLATE_BLOCK = 512;
35
+ /**
36
+ * template slots from up to to, no end, value, links, pending or meta, so a
37
+ * push writes only kind, start, parent and the links it makes
38
+ */
39
+ function fill_template(n, from, to) {
40
+ // a copy is a runtime call, stores beat it for the few nodes a small document has
41
+ const short = to - from <= 64 ? to : from + 64;
42
+ for (let b = from * 12 /* NodeField.stride */; b < short * 12 /* NodeField.stride */;) {
43
+ n[b + 2 /* NodeField.end */] = 0xffffffff;
44
+ n[b + 3 /* NodeField.value_start */] = 0;
45
+ n[b + 4 /* NodeField.value_end */] = 0;
46
+ n[b + 6 /* NodeField.next */] = 0xffffffff;
47
+ n[b + 7 /* NodeField.prev */] = 0xffffffff;
48
+ n[b + 8 /* NodeField.first_child */] = 0xffffffff;
49
+ n[b + 9 /* NodeField.last_child */] = 0xffffffff;
50
+ n[b + 10 /* NodeField.pending */] = 0;
51
+ n[b + 11 /* NodeField.meta */] = 0;
52
+ b += 12 /* NodeField.stride */;
53
+ }
54
+ let done = short;
55
+ while (done < to) {
56
+ let count = done - from;
57
+ if (count > TEMPLATE_BLOCK)
58
+ count = TEMPLATE_BLOCK;
59
+ if (count > to - done)
60
+ count = to - done;
61
+ n.copyWithin(done * 12 /* NodeField.stride */, from * 12 /* NodeField.stride */, (from + count) * 12 /* NodeField.stride */);
62
+ done += count;
63
+ }
64
+ }
65
+ const SLAB_NODES = (SLAB_BYTES / NODE_BYTES) | 0;
66
+ const FILL_CHUNK = 4096;
67
+ /**
68
+ * a carve costs one view not a backing store but keeps its whole slab alive,
69
+ * a slab holds the template from slab_used on, a trim returns only unwritten slots
70
+ */
71
+ let slab = new ArrayBuffer(0);
72
+ let slab_used = SLAB_BYTES;
73
+ /** the view whose region ends at slab_used, it can grow in place or shrink */
74
+ let last_carve = null;
3
75
  /** default number of error entries to preallocate. */
4
76
  const DEFAULT_ERROR_CAPACITY = 32;
5
77
  /**
@@ -22,80 +94,82 @@ function next_power_of_two(value) {
22
94
  export const kind_to_string = (kind) => {
23
95
  switch (kind) {
24
96
  case 0 /* NodeKind.root */:
25
- return "root";
97
+ return 'root';
26
98
  case 1 /* NodeKind.text */:
27
- return "text";
99
+ return 'text';
28
100
  case 2 /* NodeKind.html */:
29
- return "html";
101
+ return 'html';
30
102
  case 3 /* NodeKind.heading */:
31
- return "heading";
103
+ return 'heading';
32
104
  case 4 /* NodeKind.mustache */:
33
- return "mustache";
105
+ return 'mustache';
34
106
  case 5 /* NodeKind.code_fence */:
35
- return "code_fence";
107
+ return 'code_fence';
36
108
  case 6 /* NodeKind.line_break */:
37
- return "line_break";
109
+ return 'line_break';
38
110
  case 7 /* NodeKind.paragraph */:
39
- return "paragraph";
111
+ return 'paragraph';
40
112
  case 8 /* NodeKind.code_span */:
41
- return "code_span";
113
+ return 'code_span';
42
114
  case 9 /* NodeKind.emphasis */:
43
- return "emphasis";
115
+ return 'emphasis';
44
116
  case 10 /* NodeKind.strong_emphasis */:
45
- return "strong_emphasis";
117
+ return 'strong_emphasis';
46
118
  case 11 /* NodeKind.thematic_break */:
47
- return "thematic_break";
119
+ return 'thematic_break';
48
120
  case 12 /* NodeKind.link */:
49
- return "link";
121
+ return 'link';
50
122
  case 13 /* NodeKind.image */:
51
- return "image";
123
+ return 'image';
52
124
  case 14 /* NodeKind.block_quote */:
53
- return "block_quote";
125
+ return 'block_quote';
54
126
  case 15 /* NodeKind.list */:
55
- return "list";
127
+ return 'list';
56
128
  case 16 /* NodeKind.list_item */:
57
- return "list_item";
129
+ return 'list_item';
58
130
  case 17 /* NodeKind.hard_break */:
59
- return "hard_break";
131
+ return 'hard_break';
60
132
  case 18 /* NodeKind.soft_break */:
61
- return "soft_break";
133
+ return 'soft_break';
62
134
  case 19 /* NodeKind.strikethrough */:
63
- return "strikethrough";
135
+ return 'strikethrough';
64
136
  case 20 /* NodeKind.superscript */:
65
- return "superscript";
137
+ return 'superscript';
66
138
  case 21 /* NodeKind.subscript */:
67
- return "subscript";
139
+ return 'subscript';
68
140
  case 22 /* NodeKind.table */:
69
- return "table";
141
+ return 'table';
70
142
  case 23 /* NodeKind.table_header */:
71
- return "table_header";
143
+ return 'table_header';
72
144
  case 24 /* NodeKind.table_row */:
73
- return "table_row";
145
+ return 'table_row';
74
146
  case 25 /* NodeKind.table_cell */:
75
- return "table_cell";
147
+ return 'table_cell';
76
148
  case 26 /* NodeKind.html_comment */:
77
- return "html_comment";
149
+ return 'html_comment';
78
150
  case 27 /* NodeKind.svelte_tag */:
79
- return "svelte_tag";
151
+ return 'svelte_tag';
80
152
  case 28 /* NodeKind.svelte_block */:
81
- return "svelte_block";
153
+ return 'svelte_block';
82
154
  case 29 /* NodeKind.svelte_branch */:
83
- return "svelte_branch";
155
+ return 'svelte_branch';
84
156
  case 30 /* NodeKind.directive_inline */:
85
- return "directive_inline";
157
+ return 'directive_inline';
86
158
  case 31 /* NodeKind.directive_leaf */:
87
- return "directive_leaf";
159
+ return 'directive_leaf';
88
160
  case 32 /* NodeKind.directive_container */:
89
- return "directive_container";
161
+ return 'directive_container';
90
162
  case 33 /* NodeKind.frontmatter */:
91
- return "frontmatter";
163
+ return 'frontmatter';
92
164
  case 34 /* NodeKind.import_statement */:
93
- return "import_statement";
165
+ return 'import_statement';
166
+ case 35 /* NodeKind.directive_label */:
167
+ return 'directive_label';
94
168
  }
95
169
  };
96
170
  /** reverse mapping from string name to numeric NodeKind. built once at module load. */
97
171
  const _string_to_kind = new Map();
98
- for (let i = 0; i <= 34; i++) {
172
+ for (let i = 0; i <= 35; i++) {
99
173
  _string_to_kind.set(kind_to_string(i), i);
100
174
  }
101
175
  /**
@@ -114,97 +188,327 @@ export const string_to_kind = (name) => {
114
188
  const extra_to_string = (kind) => {
115
189
  switch (kind) {
116
190
  case 3 /* NodeKind.heading */:
117
- return "depth";
191
+ return 'depth';
118
192
  }
119
193
  };
120
194
  /**
121
- * buffer that stores node metadata with typed arrays.
195
+ * a computed key literal takes a slow generic define, so known keys get
196
+ * constant key literals and other keys a store into an empty literal
122
197
  */
198
+ export function make_meta(key, value) {
199
+ switch (key) {
200
+ case 'tag':
201
+ return { tag: value };
202
+ case 'href':
203
+ return { href: value };
204
+ case 'src':
205
+ return { src: value };
206
+ case 'ordered':
207
+ return { ordered: value };
208
+ case 'info_start':
209
+ return { info_start: value };
210
+ case 'name':
211
+ return { name: value };
212
+ case 'alignments':
213
+ return { alignments: value };
214
+ case 'attributes':
215
+ return { attributes: value };
216
+ case 'self_closing':
217
+ return { self_closing: value };
218
+ case 'title':
219
+ return { title: value };
220
+ case 'start':
221
+ return { start: value };
222
+ case 'tight':
223
+ return { tight: value };
224
+ case 'info_end':
225
+ return { info_end: value };
226
+ case 'args':
227
+ return { args: value };
228
+ case 'col_count':
229
+ return { col_count: value };
230
+ default: {
231
+ // a set would change the prototype, a literal defines an own property
232
+ if (key === '__proto__')
233
+ return { [key]: value };
234
+ const meta = {};
235
+ meta[key] = value;
236
+ return meta;
237
+ }
238
+ }
239
+ }
240
+ /** named stores stay monomorphic, a keyed store goes megamorphic across shapes */
241
+ export function merge_meta(meta, key, value) {
242
+ switch (key) {
243
+ case 'attributes':
244
+ meta.attributes = value;
245
+ return;
246
+ case 'start':
247
+ meta.start = value;
248
+ return;
249
+ case 'tight':
250
+ meta.tight = value;
251
+ return;
252
+ case 'title':
253
+ meta.title = value;
254
+ return;
255
+ case 'self_closing':
256
+ meta.self_closing = value;
257
+ return;
258
+ case 'info_end':
259
+ meta.info_end = value;
260
+ return;
261
+ case 'col_count':
262
+ meta.col_count = value;
263
+ return;
264
+ case 'args':
265
+ meta.args = value;
266
+ return;
267
+ case 'tag':
268
+ meta.tag = value;
269
+ return;
270
+ case 'href':
271
+ meta.href = value;
272
+ return;
273
+ case 'src':
274
+ meta.src = value;
275
+ return;
276
+ case 'ordered':
277
+ meta.ordered = value;
278
+ return;
279
+ case 'info_start':
280
+ meta.info_start = value;
281
+ return;
282
+ case 'name':
283
+ meta.name = value;
284
+ return;
285
+ case 'alignments':
286
+ meta.alignments = value;
287
+ return;
288
+ default:
289
+ meta[key] = value;
290
+ }
291
+ }
123
292
  export class NodeBuffer {
124
- /**
125
- * create a buffer that stores token metadata with typed arrays.
126
- * @param initial_capacity requested starting capacity for tokens.
127
- */
128
293
  constructor(initial_capacity = DEFAULT_TOKEN_CAPACITY) {
129
- const capacity = next_power_of_two(initial_capacity);
130
- this.capacity = capacity;
131
- this._kinds = new Uint8Array(capacity);
132
- this._starts = new Uint32Array(capacity);
133
- this._ends = new Uint32Array(capacity);
134
- this._extras = new Uint16Array(capacity);
135
- this._value_starts = new Uint32Array(capacity);
136
- this._value_ends = new Uint32Array(capacity);
137
- this.has_metadata = new Uint8Array(Math.max(1, capacity >> 3));
138
- this.metadata = new Map();
139
- this._strings = [];
140
- this._parents = new Uint32Array(capacity);
141
- this._next_siblings = new Uint32Array(capacity);
142
- this._prev_siblings = new Uint32Array(capacity);
143
- this._children_starts = new Uint32Array(capacity);
144
- this._children_ends = new Uint32Array(capacity);
145
- this._pending_nodes = new Uint32Array(capacity);
294
+ // v8 sizes in-object slots from constructor stores, so every field is
295
+ // initialized here, not only in alloc, or the tail fields spill out of line
296
+ /** @internal */
297
+ this._capacity = 0;
298
+ /** @internal do not mutate externally */
299
+ this._n = EMPTY_U32;
300
+ /** @internal */
301
+ this._meta = NO_META;
302
+ /** @internal pre-materialized text strings (used by wiretreebuilder). index -> string. */
303
+ this._strings = NO_STRINGS;
304
+ /** @internal read by TreeBuilder to check ids against indices */
146
305
  this._size = 0;
147
- this.push(0 /* NodeKind.root */, 0);
306
+ /** slots from _size up to here hold the template */
307
+ this._filled = 0;
308
+ // the comparison also sends a nan capacity to the floor
309
+ let n = this.alloc(next_power_of_two(initial_capacity > MIN_NODE_CAPACITY
310
+ ? initial_capacity
311
+ : MIN_NODE_CAPACITY));
312
+ // the root push by hand, an inlined push_node would count against the
313
+ // inlining budget of every new TreeBuilder caller
314
+ if (this._filled === 0)
315
+ n = this.fill(0);
316
+ n[0] = 0 /* NodeKind.root */;
317
+ n[1 /* NodeField.start */] = 0;
318
+ n[5 /* NodeField.parent */] = 0xffffffff;
319
+ this._size = 1;
320
+ }
321
+ alloc(capacity) {
322
+ // a node is a whole number of words, so every carve stays word aligned
323
+ const bytes = capacity * NODE_BYTES;
324
+ let buffer;
325
+ let base = 0;
326
+ if (bytes <= SLAB_MAX_CARVE) {
327
+ if (slab_used + bytes > SLAB_BYTES) {
328
+ slab = new_storage(SLAB_BYTES);
329
+ slab_used = 0;
330
+ fill_template(new Uint32Array(slab), 0, SLAB_NODES);
331
+ }
332
+ buffer = slab;
333
+ base = slab_used;
334
+ slab_used = base + bytes;
335
+ }
336
+ else {
337
+ buffer = new_storage(bytes);
338
+ }
339
+ const n = new Uint32Array(buffer, base, capacity * 12 /* NodeField.stride */);
340
+ if (buffer === slab) {
341
+ last_carve = n;
342
+ this._filled = capacity;
343
+ }
344
+ else
345
+ this._filled = 0;
346
+ this._capacity = capacity;
347
+ this._n = n;
348
+ return n;
349
+ }
350
+ /** @internal */
351
+ own_strings() {
352
+ const strings = this._strings;
353
+ return strings === NO_STRINGS ? (this._strings = []) : strings;
354
+ }
355
+ /** gives the unused tail of the last slab carve back, a later push resizes */
356
+ trim() {
357
+ if (this._n !== last_carve)
358
+ return;
359
+ slab_used -= (this._capacity - this._size) * NODE_BYTES;
360
+ this._capacity = this._size;
361
+ this._filled = this._size;
148
362
  }
149
- /** clear previously pushed tokens without reallocating storage. */
363
+ /** clear nodes without reallocating storage */
150
364
  reset() {
151
- this._size = 0;
365
+ // push writes every word of a node, so stale words need no clearing
366
+ // a length store is a runtime call even when already empty
367
+ if (this._meta.length !== 0)
368
+ this._meta.length = 0;
369
+ if (this._strings.length !== 0)
370
+ this._strings.length = 0;
371
+ // make the pushed slots template again, copying the template above them
372
+ // when there is enough, one copy costs about what stores for eight nodes do
373
+ const size = this._size;
374
+ if (size !== 0) {
375
+ if (size > 8 && size << 1 <= this._filled) {
376
+ const n = this._n;
377
+ for (let done = 0; done < size;) {
378
+ let count = size - done;
379
+ if (count > TEMPLATE_BLOCK)
380
+ count = TEMPLATE_BLOCK;
381
+ n.copyWithin(done * 12 /* NodeField.stride */, size * 12 /* NodeField.stride */, (size + count) * 12 /* NodeField.stride */);
382
+ done += count;
383
+ }
384
+ }
385
+ else
386
+ fill_template(this._n, 0, size);
387
+ this._size = 0;
388
+ }
152
389
  }
153
- /** number of tokens currently stored. */
154
390
  get size() {
155
391
  return this._size;
156
392
  }
157
393
  /**
158
- * push a token descriptor into the buffer.
159
- * @param kind token category.
394
+ * push a node as the last child of parent
395
+ * @param kind node category.
160
396
  * @param cursor cursor position
161
- * @param parent index of the parent node, or -1 for root.
162
- * @param extra extra metadata stored alongside the token.
397
+ * @param parent index of the parent node, or 0xffffffff for none.
398
+ * @param extra extra metadata stored alongside the node.
163
399
  * @param metadata optional metadata associated with the node.
164
400
  */
165
401
  push(kind, cursor, parent = 0xffffffff, extra = 0, metadata) {
402
+ const index = this.push_node(kind, cursor, parent, extra, false);
403
+ if (metadata !== undefined)
404
+ this.set_metadata(index, metadata);
405
+ return index;
406
+ }
407
+ push_pending(kind, cursor, parent = 0xffffffff, extra = 0, metadata) {
408
+ const index = this.push_node(kind, cursor, parent, extra, true);
409
+ if (metadata !== undefined)
410
+ this.set_metadata(index, metadata);
411
+ return index;
412
+ }
413
+ /** one body for every open so the builder inlines a single copy */
414
+ push_node(kind, cursor, parent, extra, pending) {
166
415
  const index = this._size;
167
- if (index >= this.capacity) {
168
- this.grow();
169
- }
170
- this._kinds[index] = kind;
171
- this._starts[index] = cursor >>> 0;
172
- this._ends[index] = 0xffffffff;
173
- this._extras[index] = extra & 0xffff;
416
+ let n = this._n;
417
+ if (index >= this._filled)
418
+ n = this.fill(index);
419
+ // the template holds every other word
420
+ const b = index * 12 /* NodeField.stride */;
421
+ n[b] = (kind & 0xff) | ((extra & 0xffff) << 8);
422
+ n[b + 1 /* NodeField.start */] = cursor;
423
+ n[b + 5 /* NodeField.parent */] = parent;
424
+ if (pending)
425
+ n[b + 10 /* NodeField.pending */] = 1;
174
426
  this._size = index + 1;
175
- this._parents[index] = parent;
176
- this._next_siblings[index] = 0xffffffff;
177
- this._prev_siblings[index] = 0xffffffff;
178
- this._children_starts[index] = 0xffffffff;
179
- this._children_ends[index] = 0xffffffff;
180
427
  if (parent !== 0xffffffff) {
181
- const last = this._children_ends[parent];
428
+ const p = parent * 12 /* NodeField.stride */;
429
+ const last = n[p + 9 /* NodeField.last_child */];
182
430
  if (last === 0xffffffff) {
183
431
  // first child
184
- this._children_starts[parent] = index;
432
+ n[p + 8 /* NodeField.first_child */] = index;
185
433
  }
186
434
  else {
187
435
  // append after last child, o(1)
188
- this._next_siblings[last] = index;
189
- this._prev_siblings[index] = last;
436
+ n[last * 12 /* NodeField.stride */ + 6 /* NodeField.next */] = index;
437
+ n[b + 7 /* NodeField.prev */] = last;
190
438
  }
191
- this._children_ends[parent] = index;
439
+ n[p + 9 /* NodeField.last_child */] = index;
192
440
  }
193
- if (metadata !== undefined) {
194
- this.metadata.set(index, metadata);
195
- // bitmask for metadata
196
- this.has_metadata[index >> 3] |= 1 << (index & 7);
441
+ return index;
442
+ }
443
+ /** parent must be a node, not 0xffffffff */
444
+ push_text(start, end, parent) {
445
+ const index = this._size;
446
+ let n = this._n;
447
+ if (index >= this._filled)
448
+ n = this.fill(index);
449
+ const b = index * 12 /* NodeField.stride */;
450
+ n[b] = 1 /* NodeKind.text */;
451
+ n[b + 1 /* NodeField.start */] = start;
452
+ n[b + 2 /* NodeField.end */] = end;
453
+ n[b + 3 /* NodeField.value_start */] = start;
454
+ n[b + 4 /* NodeField.value_end */] = end;
455
+ n[b + 5 /* NodeField.parent */] = parent;
456
+ this._size = index + 1;
457
+ const p = parent * 12 /* NodeField.stride */;
458
+ const last = n[p + 9 /* NodeField.last_child */];
459
+ if (last === 0xffffffff) {
460
+ n[p + 8 /* NodeField.first_child */] = index;
461
+ }
462
+ else {
463
+ n[last * 12 /* NodeField.stride */ + 6 /* NodeField.next */] = index;
464
+ n[b + 7 /* NodeField.prev */] = last;
197
465
  }
466
+ n[p + 9 /* NodeField.last_child */] = index;
198
467
  return index;
199
468
  }
200
- push_pending(kind, cursor, parent = 0xffffffff, extra = 0, metadata) {
201
- const index = this.push(kind, cursor, parent, extra, metadata);
202
- this._pending_nodes[index] = 1;
469
+ /** a closed node with no extra and a value range, as open, value and close would leave it */
470
+ push_leaf(kind, start, value_start, value_end, end, parent) {
471
+ const index = this._size;
472
+ let n = this._n;
473
+ if (index >= this._filled)
474
+ n = this.fill(index);
475
+ const b = index * 12 /* NodeField.stride */;
476
+ n[b] = kind;
477
+ n[b + 1 /* NodeField.start */] = start;
478
+ n[b + 2 /* NodeField.end */] = end;
479
+ n[b + 3 /* NodeField.value_start */] = value_start;
480
+ n[b + 4 /* NodeField.value_end */] = value_end;
481
+ n[b + 5 /* NodeField.parent */] = parent;
482
+ this._size = index + 1;
483
+ const p = parent * 12 /* NodeField.stride */;
484
+ const last = n[p + 9 /* NodeField.last_child */];
485
+ if (last === 0xffffffff) {
486
+ n[p + 8 /* NodeField.first_child */] = index;
487
+ }
488
+ else {
489
+ n[last * 12 /* NodeField.stride */ + 6 /* NodeField.next */] = index;
490
+ n[b + 7 /* NodeField.prev */] = last;
491
+ }
492
+ n[p + 9 /* NodeField.last_child */] = index;
203
493
  return index;
204
494
  }
495
+ fill(index) {
496
+ let n = this._n;
497
+ if (index >= this._capacity)
498
+ n = this.grow();
499
+ let filled = this._filled;
500
+ if (index >= filled) {
501
+ let to = filled + (filled > FILL_CHUNK ? filled : FILL_CHUNK);
502
+ if (to > this._capacity)
503
+ to = this._capacity;
504
+ fill_template(n, filled, to);
505
+ this._filled = filled = to;
506
+ }
507
+ return n;
508
+ }
205
509
  /**
206
510
  * allocate a node slot without linking it into any parent's child list.
207
- * the caller is responsible for setting _parents and linking into the
511
+ * the caller is responsible for setting the parent and linking into the
208
512
  * sibling chain manually. used by plugin structural mutations (prepend).
209
513
  * @param kind node kind.
210
514
  * @param cursor source position.
@@ -213,25 +517,7 @@ export class NodeBuffer {
213
517
  * @returns buffer index of the new node.
214
518
  */
215
519
  push_unlinked(kind, cursor, extra = 0, metadata) {
216
- const index = this._size;
217
- if (index >= this.capacity) {
218
- this.grow();
219
- }
220
- this._kinds[index] = kind;
221
- this._starts[index] = cursor >>> 0;
222
- this._ends[index] = 0xffffffff;
223
- this._extras[index] = extra & 0xffff;
224
- this._size = index + 1;
225
- this._parents[index] = 0xffffffff;
226
- this._next_siblings[index] = 0xffffffff;
227
- this._prev_siblings[index] = 0xffffffff;
228
- this._children_starts[index] = 0xffffffff;
229
- this._children_ends[index] = 0xffffffff;
230
- if (metadata !== undefined) {
231
- this.metadata.set(index, metadata);
232
- this.has_metadata[index >> 3] |= 1 << (index & 7);
233
- }
234
- return index;
520
+ return this.push(kind, cursor, 0xffffffff, extra, metadata);
235
521
  }
236
522
  /**
237
523
  * insert a new wrapper node between a parent and all its current children.
@@ -245,33 +531,34 @@ export class NodeBuffer {
245
531
  * @returns buffer index of the new wrapper node.
246
532
  */
247
533
  wrap_children(parent_idx, new_kind, extra = 0, metadata) {
248
- const first_child = this._children_starts[parent_idx];
249
- const last_child = this._children_ends[parent_idx];
534
+ const first_child = this.first_child_at(parent_idx);
535
+ const last_child = this.last_child_at(parent_idx);
250
536
  // detach children from parent
251
- this._children_starts[parent_idx] = 0xffffffff;
252
- this._children_ends[parent_idx] = 0xffffffff;
537
+ this.set_first_child(parent_idx, 0xffffffff);
538
+ this.set_last_child(parent_idx, 0xffffffff);
253
539
  // push wrapper as sole child of parent (auto-links via push)
254
540
  const wrapper_idx = this.push(new_kind, 0, parent_idx, extra, metadata);
255
541
  // reattach old children to wrapper
256
542
  if (first_child !== 0xffffffff) {
257
- this._children_starts[wrapper_idx] = first_child;
258
- this._children_ends[wrapper_idx] = last_child;
543
+ this.set_first_child(wrapper_idx, first_child);
544
+ this.set_last_child(wrapper_idx, last_child);
259
545
  // update parent pointers on all moved children
260
546
  let child = first_child;
261
- while (child !== 0xffffffff && this._parents[child] === parent_idx) {
262
- this._parents[child] = wrapper_idx;
263
- child = this._next_siblings[child];
547
+ while (child !== 0xffffffff && this.parent_at(child) === parent_idx) {
548
+ this.set_parent(child, wrapper_idx);
549
+ child = this.next_at(child);
264
550
  }
265
551
  }
266
552
  return wrapper_idx;
267
553
  }
268
554
  commit_node(index) {
269
- this._pending_nodes[index] = 0;
555
+ this._n[index * 12 /* NodeField.stride */ + 10 /* NodeField.pending */] = 0;
270
556
  }
271
557
  get_pending() {
272
558
  const result = [];
559
+ const n = this._n;
273
560
  for (let i = 0; i < this._size; i++) {
274
- if (this._pending_nodes[i] !== 0) {
561
+ if (n[i * 12 /* NodeField.stride */ + 10 /* NodeField.pending */] !== 0) {
275
562
  result.push(i);
276
563
  }
277
564
  }
@@ -294,16 +581,16 @@ export class NodeBuffer {
294
581
  * @param delimiter_text optional pre-resolved delimiter string (wire path).
295
582
  * if provided, stored in _strings. if absent, value range is set from
296
583
  * the node's start position.
584
+ * @param text_start source offset of delimiter_text, at the node start the
585
+ * repaired range slices the same text so it is not stored
297
586
  */
298
- handle_repair(index, delimiter_text) {
299
- const parent = this._parents[index];
300
- const kind = this._kinds[index];
301
- const parent_kind = parent !== 0xffffffff ? this._kinds[parent] : undefined;
302
- // tight-list speculation repair
303
- // a pending paragraph inside a list_item represents the "loose"
304
- // wrapper that tight lists don't need. revoking it simply drops
305
- // the wrapper and reparents children to the list_item.
306
- if (kind === 7 /* NodeKind.paragraph */ && parent_kind === 16 /* NodeKind.list_item */) {
587
+ handle_repair(index, delimiter_text, text_start) {
588
+ const parent = this.parent_at(index);
589
+ const kind = this.kind_at(index);
590
+ const parent_kind = parent !== 0xffffffff ? this.kind_at(parent) : undefined;
591
+ // a revoked paragraph is a wrapper a tight list item or a paragraph of only tags does not need
592
+ // a revoked table cell was a merge marker, the cell it joined spans its column
593
+ if (kind === 7 /* NodeKind.paragraph */ || kind === 25 /* NodeKind.table_cell */) {
307
594
  this.unwrap_node(index);
308
595
  return;
309
596
  }
@@ -315,7 +602,7 @@ export class NodeBuffer {
315
602
  if (parent_kind === 0 /* NodeKind.root */ ||
316
603
  parent_kind === 14 /* NodeKind.block_quote */ ||
317
604
  parent_kind === 16 /* NodeKind.list_item */) {
318
- const start = this._starts[index];
605
+ const start = this.start_at(index);
319
606
  let end;
320
607
  if (delimiter_text !== undefined) {
321
608
  // wire path: delimiter_text is the literal open-tag source
@@ -324,24 +611,24 @@ export class NodeBuffer {
324
611
  else {
325
612
  // source path: prefer the node's recorded end; fall back to the
326
613
  // nearest meaningful child position so we never collapse to start.
327
- end = this._ends[index];
614
+ end = this.end_at(index);
328
615
  if (end === 0xffffffff) {
329
- let child = this._children_starts[index];
330
- while (child !== 0xffffffff && this._parents[child] === index) {
331
- if (this._kinds[child] === 6 /* NodeKind.line_break */) {
332
- const lb_start = this._starts[child];
616
+ let child = this.first_child_at(index);
617
+ while (child !== 0xffffffff && this.parent_at(child) === index) {
618
+ if (this.kind_at(child) === 6 /* NodeKind.line_break */) {
619
+ const lb_start = this.start_at(child);
333
620
  if (lb_start > 0 && (end === 0xffffffff || lb_start > end)) {
334
621
  end = lb_start;
335
622
  }
336
623
  }
337
624
  else {
338
- const child_end = this._ends[child];
625
+ const child_end = this.end_at(child);
339
626
  if (child_end !== 0xffffffff &&
340
627
  (end === 0xffffffff || child_end > end)) {
341
628
  end = child_end;
342
629
  }
343
630
  }
344
- child = this._next_siblings[child];
631
+ child = this.next_at(child);
345
632
  }
346
633
  }
347
634
  if (end === 0xffffffff || end <= start) {
@@ -351,45 +638,45 @@ export class NodeBuffer {
351
638
  }
352
639
  // reparent children to the grandparent so trailing parsed content
353
640
  // (e.g. block elements that followed the unclosed open tag) is kept.
354
- const child_chain = this._children_starts[index];
355
- this._children_starts[index] = 0xffffffff;
356
- this._children_ends[index] = 0xffffffff;
641
+ const child_chain = this.first_child_at(index);
642
+ this.set_first_child(index, 0xffffffff);
643
+ this.set_last_child(index, 0xffffffff);
357
644
  // convert this node into a paragraph holding the literal open-tag text.
358
645
  this.set_kind(index, 7 /* NodeKind.paragraph */);
359
- this.metadata.delete(index);
360
- this._ends[index] = end;
646
+ this.delete_metadata(index);
647
+ this.set_end(index, end);
361
648
  const text_idx = this.push(1 /* NodeKind.text */, start, index);
362
- this._value_starts[text_idx] = start;
363
- this._value_ends[text_idx] = end;
364
- this._ends[text_idx] = end;
365
- if (delimiter_text !== undefined) {
366
- this._strings[text_idx] = delimiter_text;
649
+ this.set_value(text_idx, start, end);
650
+ this.set_end(text_idx, end);
651
+ if (delimiter_text !== undefined && text_start !== start) {
652
+ this.own_strings()[text_idx] = delimiter_text;
367
653
  }
368
654
  // splice the original children in after the rewritten node so they
369
655
  // appear as siblings under the parent.
370
656
  if (child_chain !== 0xffffffff) {
371
- const original_next = this._next_siblings[index];
657
+ const original_next = this.next_at(index);
372
658
  let last_child = child_chain;
373
659
  let scan = child_chain;
374
- while (scan !== 0xffffffff && this._parents[scan] === index) {
375
- this._parents[scan] = parent;
660
+ while (scan !== 0xffffffff && this.parent_at(scan) === index) {
661
+ this.set_parent(scan, parent);
376
662
  last_child = scan;
377
- scan = this._next_siblings[scan];
663
+ scan = this.next_at(scan);
378
664
  }
379
- this._next_siblings[last_child] = original_next;
665
+ this.set_next(last_child, original_next);
380
666
  if (original_next !== 0xffffffff) {
381
- this._prev_siblings[original_next] = last_child;
667
+ this.set_prev(original_next, last_child);
382
668
  }
383
- else if (parent !== 0xffffffff && this._children_ends[parent] === index) {
384
- this._children_ends[parent] = last_child;
669
+ else if (parent !== 0xffffffff &&
670
+ this.last_child_at(parent) === index) {
671
+ this.set_last_child(parent, last_child);
385
672
  }
386
- this._next_siblings[index] = child_chain;
387
- this._prev_siblings[child_chain] = index;
673
+ this.set_next(index, child_chain);
674
+ this.set_prev(child_chain, index);
388
675
  }
389
676
  return;
390
677
  }
391
678
  // inline repair
392
- const first_child = this._children_starts[index];
679
+ const first_child = this.first_child_at(index);
393
680
  // convert the wrapper to a plain text node whose value is the
394
681
  // literal delimiter (e.g. "~" for subscript, "<div>" for an
395
682
  // inline html open tag). any children stay in the document,
@@ -397,107 +684,116 @@ export class NodeBuffer {
397
684
  // content is preserved after revocation.
398
685
  this.set_kind(index, 1 /* NodeKind.text */);
399
686
  if (delimiter_text !== undefined) {
400
- this._strings[index] = delimiter_text;
401
- const start = this._starts[index];
687
+ const start = this.start_at(index);
688
+ if (text_start !== start)
689
+ this.own_strings()[index] = delimiter_text;
402
690
  const end = start + delimiter_text.length;
403
- this._value_starts[index] = start;
404
- this._value_ends[index] = end;
405
- this._ends[index] = end;
691
+ this.set_value(index, start, end);
692
+ this.set_end(index, end);
406
693
  if (first_child === 0xffffffff) {
407
- this._children_starts[index] = 0xffffffff;
408
- this._children_ends[index] = 0xffffffff;
694
+ this.set_first_child(index, 0xffffffff);
695
+ this.set_last_child(index, 0xffffffff);
409
696
  return;
410
697
  }
411
698
  // reparent children and splice them in after this text node.
412
699
  let child = first_child;
413
700
  let last_child = first_child;
414
- while (child !== 0xffffffff && this._parents[child] === index) {
415
- this._parents[child] = parent;
701
+ while (child !== 0xffffffff && this.parent_at(child) === index) {
702
+ this.set_parent(child, parent);
416
703
  last_child = child;
417
- child = this._next_siblings[child];
704
+ child = this.next_at(child);
418
705
  }
419
- const node_next = this._next_siblings[index];
420
- this._next_siblings[index] = first_child;
421
- this._prev_siblings[first_child] = index;
422
- if (node_next !== 0xffffffff && this._parents[node_next] === parent) {
423
- this._next_siblings[last_child] = node_next;
424
- this._prev_siblings[node_next] = last_child;
706
+ const node_next = this.next_at(index);
707
+ this.set_next(index, first_child);
708
+ this.set_prev(first_child, index);
709
+ if (node_next !== 0xffffffff && this.parent_at(node_next) === parent) {
710
+ this.set_next(last_child, node_next);
711
+ this.set_prev(node_next, last_child);
425
712
  }
426
713
  else {
427
- this._next_siblings[last_child] = 0xffffffff;
714
+ this.set_next(last_child, 0xffffffff);
428
715
  }
429
- if (this._children_ends[parent] === index) {
430
- this._children_ends[parent] = last_child;
716
+ if (this.last_child_at(parent) === index) {
717
+ this.set_last_child(parent, last_child);
431
718
  }
432
- this._children_starts[index] = 0xffffffff;
433
- this._children_ends[index] = 0xffffffff;
719
+ this.set_first_child(index, 0xffffffff);
720
+ this.set_last_child(index, 0xffffffff);
434
721
  return;
435
722
  }
436
723
  // source-based fallback, derive the delimiter byte range from
437
724
  // the node's own start offset.
438
725
  if (first_child === 0xffffffff) {
439
- this._children_starts[index] = 0xffffffff;
440
- this._children_ends[index] = 0xffffffff;
441
- this._value_starts[index] = this._starts[index];
442
- if (this._ends[index] === 0xffffffff) {
443
- this._value_ends[index] = this._starts[index] + 1;
444
- this._ends[index] = this._starts[index] + 1;
726
+ this.set_first_child(index, 0xffffffff);
727
+ this.set_last_child(index, 0xffffffff);
728
+ const start = this.start_at(index);
729
+ if (this.end_at(index) === 0xffffffff) {
730
+ // an opener that records where its content starts ends there
731
+ const content_start = this.value_start_at(index);
732
+ const end = content_start > start ? content_start : start + 1;
733
+ this.set_value(index, start, end);
734
+ this.set_end(index, end);
445
735
  }
446
736
  else {
447
- this._value_ends[index] = this._ends[index];
737
+ this.set_value_start(index, start);
738
+ this.set_value_end(index, this.end_at(index));
448
739
  }
449
740
  return;
450
741
  }
451
742
  // walk sibling chain and reparent only direct children
452
743
  let child = first_child;
453
744
  let last_child = first_child;
454
- while (child !== 0xffffffff && this._parents[child] === index) {
455
- this._parents[child] = parent;
745
+ while (child !== 0xffffffff && this.parent_at(child) === index) {
746
+ this.set_parent(child, parent);
456
747
  last_child = child;
457
- child = this._next_siblings[child];
748
+ child = this.next_at(child);
458
749
  }
459
- // if exactly one child and it's text, merge the delimiter into it
750
+ // merge a lone text child into the delimiter, a cell text ends at the pipe past its value
460
751
  if (last_child === first_child &&
461
- this._kinds[first_child] === 1 /* NodeKind.text */) {
462
- this._value_starts[index] = this._starts[index];
463
- this._value_ends[index] = this._ends[first_child];
464
- this._ends[index] = this._ends[first_child];
752
+ this.kind_at(first_child) === 1 /* NodeKind.text */) {
753
+ this.set_value(index, this.start_at(index), this.value_end_at(first_child));
754
+ this.set_end(index, this.end_at(first_child));
465
755
  // skip the child in the sibling chain
466
- this._next_siblings[index] = this._next_siblings[first_child];
467
- if (this._next_siblings[first_child] !== 0xffffffff) {
468
- this._prev_siblings[this._next_siblings[first_child]] = index;
756
+ const after = this.next_at(first_child);
757
+ this.set_next(index, after);
758
+ if (after !== 0xffffffff) {
759
+ this.set_prev(after, index);
469
760
  }
470
- this._children_starts[index] = 0xffffffff;
471
- this._children_ends[index] = 0xffffffff;
761
+ this.set_first_child(index, 0xffffffff);
762
+ this.set_last_child(index, 0xffffffff);
472
763
  return;
473
764
  }
474
765
  // insert converted node as sibling before first_child, reparent rest
475
- const first_child_prev = this._prev_siblings[first_child];
476
- this._next_siblings[index] = first_child;
477
- this._prev_siblings[first_child] = index;
766
+ const first_child_prev = this.prev_at(first_child);
767
+ this.set_next(index, first_child);
768
+ this.set_prev(first_child, index);
478
769
  if (first_child_prev !== 0xffffffff) {
479
- this._next_siblings[first_child_prev] = index;
480
- this._prev_siblings[index] = first_child_prev;
770
+ this.set_next(first_child_prev, index);
771
+ this.set_prev(index, first_child_prev);
481
772
  }
482
- if (this._children_ends[parent] === index) {
483
- this._children_ends[parent] = last_child;
773
+ if (this.last_child_at(parent) === index) {
774
+ this.set_last_child(parent, last_child);
484
775
  }
485
776
  // clear children references
486
- this._children_starts[index] = 0xffffffff;
487
- this._children_ends[index] = 0xffffffff;
488
- this._value_starts[index] = this._starts[index];
489
- this._value_ends[index] = this._value_starts[first_child];
490
- this._ends[index] = this._value_ends[index];
777
+ this.set_first_child(index, 0xffffffff);
778
+ this.set_last_child(index, 0xffffffff);
779
+ // use start, a soft_break child has no value range
780
+ const value_start = this.start_at(index);
781
+ const value_end = this.start_at(first_child);
782
+ this.set_value(index, value_start, value_end);
783
+ this.set_end(index, value_end);
491
784
  }
492
785
  repair() {
493
- const pending_nodes = this._pending_nodes.forEach((node, index) => {
494
- if (node !== 0) {
495
- this.handle_repair(index);
786
+ const n = this._n;
787
+ for (let i = 0; i < this._size; i++) {
788
+ if (n[i * 12 /* NodeField.stride */ + 10 /* NodeField.pending */] !== 0) {
789
+ this.handle_repair(i);
496
790
  }
497
- });
791
+ }
498
792
  }
499
793
  pop() {
500
- this._size -= 1;
794
+ const i = --this._size;
795
+ // the popped slot is the template again, as a push expects
796
+ fill_template(this._n, i, i + 1);
501
797
  }
502
798
  /**
503
799
  * remove a node from the tree and reparent its children to its parent.
@@ -505,88 +801,81 @@ export class NodeBuffer {
505
801
  * in the parent's child list.
506
802
  */
507
803
  unwrap_node(index) {
508
- const parent = this._parents[index];
509
- const first_child = this._children_starts[index];
804
+ const parent = this.parent_at(index);
805
+ const first_child = this.first_child_at(index);
510
806
  if (first_child === 0xffffffff) {
511
807
  // no children, remove from sibling chain
512
- const prev_sib = this._prev_siblings[index];
513
- const next_sib = this._next_siblings[index];
808
+ const prev_sib = this.prev_at(index);
809
+ const next_sib = this.next_at(index);
514
810
  if (prev_sib !== 0xffffffff) {
515
- this._next_siblings[prev_sib] = next_sib;
811
+ this.set_next(prev_sib, next_sib);
516
812
  }
517
813
  else if (parent !== 0xffffffff) {
518
- this._children_starts[parent] = next_sib;
814
+ this.set_first_child(parent, next_sib);
519
815
  }
520
816
  if (next_sib !== 0xffffffff) {
521
- this._prev_siblings[next_sib] = prev_sib;
817
+ this.set_prev(next_sib, prev_sib);
522
818
  }
523
819
  // update parent's last child if we removed the tail
524
- if (parent !== 0xffffffff && this._children_ends[parent] === index) {
525
- this._children_ends[parent] = prev_sib; // 0xffffffff if was only child
820
+ if (parent !== 0xffffffff && this.last_child_at(parent) === index) {
821
+ this.set_last_child(parent, prev_sib); // 0xffffffff if was only child
526
822
  }
527
823
  return;
528
824
  }
529
825
  // reparent all children and find last child
530
826
  let child = first_child;
531
827
  let last_child = first_child;
532
- while (child !== 0xffffffff && this._parents[child] === index) {
533
- this._parents[child] = parent;
828
+ while (child !== 0xffffffff && this.parent_at(child) === index) {
829
+ this.set_parent(child, parent);
534
830
  last_child = child;
535
- child = this._next_siblings[child];
831
+ child = this.next_at(child);
536
832
  }
537
833
  // splice children into parent's child list where this node was
538
- const prev_sib = this._prev_siblings[index];
539
- const next_sib = this._next_siblings[last_child] !== 0xffffffff &&
540
- this._parents[this._next_siblings[last_child]] === index
541
- ? 0xffffffff
542
- : this._next_siblings[last_child];
834
+ const prev_sib = this.prev_at(index);
543
835
  // the actual next sibling of the unwrapped node (not of last_child)
544
- const node_next = this._next_siblings[index];
836
+ const node_next = this.next_at(index);
545
837
  if (prev_sib !== 0xffffffff) {
546
- this._next_siblings[prev_sib] = first_child;
547
- this._prev_siblings[first_child] = prev_sib;
838
+ this.set_next(prev_sib, first_child);
839
+ this.set_prev(first_child, prev_sib);
548
840
  }
549
841
  else if (parent !== 0xffffffff) {
550
- this._children_starts[parent] = first_child;
551
- this._prev_siblings[first_child] = 0xffffffff;
842
+ this.set_first_child(parent, first_child);
843
+ this.set_prev(first_child, 0xffffffff);
552
844
  }
553
845
  // link last child to the unwrapped node's next sibling
554
846
  if (node_next !== 0xffffffff && node_next !== first_child) {
555
- this._next_siblings[last_child] = node_next;
556
- this._prev_siblings[node_next] = last_child;
847
+ this.set_next(last_child, node_next);
848
+ this.set_prev(node_next, last_child);
557
849
  }
558
850
  else {
559
- this._next_siblings[last_child] = 0xffffffff;
851
+ this.set_next(last_child, 0xffffffff);
560
852
  }
561
853
  // update parent's last child if we replaced the tail
562
- if (parent !== 0xffffffff && this._children_ends[parent] === index) {
563
- this._children_ends[parent] = last_child;
854
+ if (parent !== 0xffffffff && this.last_child_at(parent) === index) {
855
+ this.set_last_child(parent, last_child);
564
856
  }
565
857
  // clear the unwrapped node's links
566
- this._children_starts[index] = 0xffffffff;
567
- this._children_ends[index] = 0xffffffff;
858
+ this.set_first_child(index, 0xffffffff);
859
+ this.set_last_child(index, 0xffffffff);
568
860
  }
569
861
  set_parent_kind(index, kind) {
570
- const parent_index = this._parents[index];
862
+ const parent_index = this.parent_at(index);
571
863
  if (parent_index !== 0xffffffff) {
572
- this._kinds[parent_index] = kind;
864
+ this.set_kind(parent_index, kind);
573
865
  }
574
866
  }
575
867
  /** return indices of all nodes matching the given kind (lazy scan). */
576
868
  get_kinds(kind) {
577
869
  const result = [];
870
+ const n = this._n;
578
871
  for (let i = 0; i < this._size; i++) {
579
- if (this._kinds[i] === kind)
872
+ if ((n[i * 12 /* NodeField.stride */] & 0xff) === kind)
580
873
  result.push(i);
581
874
  }
582
875
  return result;
583
876
  }
584
- /**
585
- * get the token kind recorded at the supplied index.
586
- * @param index token index.
587
- */
588
877
  kind_at(index) {
589
- return this._kinds[index];
878
+ return (this._n[index * 12 /* NodeField.stride */] & 0xff);
590
879
  }
591
880
  /**
592
881
  * set the kind of the node at the given index
@@ -594,15 +883,12 @@ export class NodeBuffer {
594
883
  * @param kind new kind
595
884
  */
596
885
  set_kind(index, kind) {
597
- this._kinds[index] = kind;
598
- }
599
- /** @internal set prev_siblings for wiretreebuilder revoke. */
600
- prev_siblings_set(index, value) {
601
- this._prev_siblings[index] = value;
886
+ const n = this._n;
887
+ const b = index * 12 /* NodeField.stride */;
888
+ n[b] = (n[b] & 0xffff00) | (kind & 0xff);
602
889
  }
603
- /** @internal set children_ends for wiretreebuilder revoke. */
604
- children_ends_set(index, value) {
605
- this._children_ends[index] = value;
890
+ extra_at(index) {
891
+ return this._n[index * 12 /* NodeField.stride */] >>> 8;
606
892
  }
607
893
  /**
608
894
  * set the extra of the node at the given index
@@ -610,7 +896,16 @@ export class NodeBuffer {
610
896
  * @param extra new extra
611
897
  */
612
898
  set_extra(index, extra) {
613
- this._extras[index] = extra & 0xffff;
899
+ const n = this._n;
900
+ const b = index * 12 /* NodeField.stride */;
901
+ n[b] = (n[b] & 0xff) | ((extra & 0xffff) << 8);
902
+ }
903
+ start_at(index) {
904
+ return this._n[index * 12 /* NodeField.stride */ + 1 /* NodeField.start */];
905
+ }
906
+ /** 0xffffffff while open */
907
+ end_at(index) {
908
+ return this._n[index * 12 /* NodeField.stride */ + 2 /* NodeField.end */];
614
909
  }
615
910
  /**
616
911
  * set the end position of the node at the given index
@@ -618,87 +913,135 @@ export class NodeBuffer {
618
913
  * @param end new end position
619
914
  */
620
915
  set_end(index, end) {
621
- this._ends[index] = end >>> 0;
916
+ this._n[index * 12 /* NodeField.stride */ + 2 /* NodeField.end */] = end >>> 0;
622
917
  }
623
918
  gently_set_end(index, end) {
624
- if (this._ends[index] !== 0xffffffff)
919
+ const i = index * 12 /* NodeField.stride */ + 2 /* NodeField.end */;
920
+ if (this._n[i] !== 0xffffffff)
625
921
  return;
626
- this._ends[index] = end >>> 0;
922
+ this._n[i] = end >>> 0;
923
+ }
924
+ value_start_at(index) {
925
+ return this._n[index * 12 /* NodeField.stride */ + 3 /* NodeField.value_start */];
926
+ }
927
+ value_end_at(index) {
928
+ return this._n[index * 12 /* NodeField.stride */ + 4 /* NodeField.value_end */];
929
+ }
930
+ set_value(index, value_start, value_end) {
931
+ const n = this._n;
932
+ const b = index * 12 /* NodeField.stride */;
933
+ n[b + 3 /* NodeField.value_start */] = value_start;
934
+ n[b + 4 /* NodeField.value_end */] = value_end;
935
+ }
936
+ set_value_start(index, value_start) {
937
+ this._n[index * 12 /* NodeField.stride */ + 3 /* NodeField.value_start */] = value_start;
938
+ }
939
+ set_value_end(index, value_end) {
940
+ this._n[index * 12 /* NodeField.stride */ + 4 /* NodeField.value_end */] = value_end;
627
941
  }
628
942
  gently_set_value_end(index, end) {
629
- if (this._value_ends[index] !== 0xffffffff)
943
+ const i = index * 12 /* NodeField.stride */ + 4 /* NodeField.value_end */;
944
+ if (this._n[i] !== 0xffffffff)
630
945
  return;
631
- this._value_ends[index] = end >>> 0;
946
+ this._n[i] = end >>> 0;
947
+ }
948
+ parent_at(index) {
949
+ return this._n[index * 12 /* NodeField.stride */ + 5 /* NodeField.parent */];
950
+ }
951
+ set_parent(index, parent) {
952
+ this._n[index * 12 /* NodeField.stride */ + 5 /* NodeField.parent */] = parent;
953
+ }
954
+ next_at(index) {
955
+ return this._n[index * 12 /* NodeField.stride */ + 6 /* NodeField.next */];
956
+ }
957
+ set_next(index, next) {
958
+ this._n[index * 12 /* NodeField.stride */ + 6 /* NodeField.next */] = next;
959
+ }
960
+ prev_at(index) {
961
+ return this._n[index * 12 /* NodeField.stride */ + 7 /* NodeField.prev */];
962
+ }
963
+ set_prev(index, prev) {
964
+ this._n[index * 12 /* NodeField.stride */ + 7 /* NodeField.prev */] = prev;
965
+ }
966
+ first_child_at(index) {
967
+ return this._n[index * 12 /* NodeField.stride */ + 8 /* NodeField.first_child */];
968
+ }
969
+ set_first_child(index, child) {
970
+ this._n[index * 12 /* NodeField.stride */ + 8 /* NodeField.first_child */] = child;
971
+ }
972
+ last_child_at(index) {
973
+ return this._n[index * 12 /* NodeField.stride */ + 9 /* NodeField.last_child */];
974
+ }
975
+ set_last_child(index, child) {
976
+ this._n[index * 12 /* NodeField.stride */ + 9 /* NodeField.last_child */] = child;
977
+ }
978
+ pending_at(index) {
979
+ return this._n[index * 12 /* NodeField.stride */ + 10 /* NodeField.pending */];
632
980
  }
633
981
  /** pre-grow to avoid repeated resizes when the final size is estimable. */
634
982
  ensure_capacity(needed) {
635
- while (this.capacity < needed)
636
- this.grow();
983
+ let next = this._capacity;
984
+ if (next >= needed)
985
+ return;
986
+ while (next < needed)
987
+ next <<= 1;
988
+ this.resize(next);
637
989
  }
638
990
  /** double the backing storage when capacity is exhausted. */
639
991
  grow() {
640
- const next = this.capacity << 1;
641
- const next_kinds = new Uint8Array(next);
642
- const next_starts = new Uint32Array(next);
643
- const next_ends = new Uint32Array(next);
644
- const next_extras = new Uint16Array(next);
645
- const next_value_starts = new Uint32Array(next);
646
- const next_value_ends = new Uint32Array(next);
647
- const next_parents = new Uint32Array(next);
648
- const next_next_siblings = new Uint32Array(next);
649
- const next_prev_siblings = new Uint32Array(next);
650
- const next_children_starts = new Uint32Array(next);
651
- const next_children_ends = new Uint32Array(next);
652
- const next_pending_nodes = new Uint32Array(next);
653
- const next_has_metadata = new Uint8Array(Math.max(1, next >> 3));
654
- next_kinds.set(this._kinds);
655
- next_starts.set(this._starts);
656
- next_ends.set(this._ends);
657
- next_extras.set(this._extras);
658
- next_value_starts.set(this._value_starts);
659
- next_value_ends.set(this._value_ends);
660
- next_parents.set(this._parents);
661
- next_next_siblings.set(this._next_siblings);
662
- next_prev_siblings.set(this._prev_siblings);
663
- next_children_starts.set(this._children_starts);
664
- next_children_ends.set(this._children_ends);
665
- next_pending_nodes.set(this._pending_nodes);
666
- next_has_metadata.set(this.has_metadata);
667
- this.capacity = next;
668
- this._kinds = next_kinds;
669
- this._starts = next_starts;
670
- this._ends = next_ends;
671
- this._extras = next_extras;
672
- this._value_starts = next_value_starts;
673
- this._value_ends = next_value_ends;
674
- this._parents = next_parents;
675
- this._next_siblings = next_next_siblings;
676
- this._prev_siblings = next_prev_siblings;
677
- this._children_starts = next_children_starts;
678
- this._children_ends = next_children_ends;
679
- this._pending_nodes = next_pending_nodes;
680
- this.has_metadata = next_has_metadata;
681
- }
682
- set_metadata(index, metadata) {
683
- this.metadata.set(index, metadata);
684
- this.has_metadata[index >> 3] |= 1 << (index & 7);
992
+ return this.resize(this._capacity << 1);
685
993
  }
686
- metadata_at(index) {
687
- // check bit first
688
- if (!(this.has_metadata[index >> 3] & (1 << (index & 7)))) {
689
- return undefined; // fast path: no map lookup
994
+ resize(next) {
995
+ const old = this._n;
996
+ const words = this._capacity * 12 /* NodeField.stride */;
997
+ if (old === last_carve) {
998
+ // nothing was carved since, so extend the region in place
999
+ const base = slab_used - words * 4;
1000
+ const bytes = next * NODE_BYTES;
1001
+ if (bytes <= SLAB_MAX_CARVE && base + bytes <= SLAB_BYTES) {
1002
+ const n = new Uint32Array(slab, base, next * 12 /* NodeField.stride */);
1003
+ slab_used = base + bytes;
1004
+ last_carve = n;
1005
+ this._capacity = next;
1006
+ this._filled = next;
1007
+ this._n = n;
1008
+ return n;
1009
+ }
690
1010
  }
691
- return this.metadata.get(index);
1011
+ const n = this.alloc(next);
1012
+ // slots from _size on are template in both, a carve has it already
1013
+ const size = this._size;
1014
+ n.set(old.length === size * 12 /* NodeField.stride */
1015
+ ? old
1016
+ : old.subarray(0, size * 12 /* NodeField.stride */));
1017
+ if (this._filled < size)
1018
+ this._filled = size;
1019
+ return n;
692
1020
  }
693
- set_value(index, value_start, value_end) {
694
- this._value_starts[index] = value_start;
695
- this._value_ends[index] = value_end;
1021
+ set_metadata(index, metadata) {
1022
+ const n = this._n;
1023
+ const i = index * 12 /* NodeField.stride */ + 11 /* NodeField.meta */;
1024
+ const slot = n[i];
1025
+ if (slot !== 0)
1026
+ this._meta[slot - 1] = metadata;
1027
+ else {
1028
+ const meta = this._meta;
1029
+ // a literal holds one slot, a push onto an empty array reserves seventeen
1030
+ if (meta === NO_META) {
1031
+ this._meta = [metadata];
1032
+ n[i] = 1;
1033
+ }
1034
+ else
1035
+ n[i] = meta.push(metadata);
1036
+ }
696
1037
  }
697
- set_value_start(index, value_start) {
698
- this._value_starts[index] = value_start;
1038
+ metadata_at(index) {
1039
+ const slot = this._n[index * 12 /* NodeField.stride */ + 11 /* NodeField.meta */];
1040
+ // an index past the storage reads undefined, which indexes _meta at nan
1041
+ return slot === 0 ? undefined : this._meta[slot - 1];
699
1042
  }
700
- set_value_end(index, value_end) {
701
- this._value_ends[index] = value_end;
1043
+ delete_metadata(index) {
1044
+ this._n[index * 12 /* NodeField.stride */ + 11 /* NodeField.meta */] = 0;
702
1045
  }
703
1046
  /**
704
1047
  * get the node at the given index or the root node if no index is provided
@@ -706,33 +1049,33 @@ export class NodeBuffer {
706
1049
  * @returns node
707
1050
  */
708
1051
  get_node(index = 0) {
709
- const extra_string = extra_to_string(this._kinds[index]);
1052
+ const kind = this.kind_at(index);
1053
+ const extra_string = extra_to_string(kind);
710
1054
  const extras_object = extra_string
711
- ? { [extra_string]: this._extras[index] }
1055
+ ? { [extra_string]: this.extra_at(index) }
712
1056
  : {};
713
1057
  const _children = [];
714
1058
  // walk sibling chain to get all direct children
715
- let child = this._children_starts[index];
716
- while (child !== 0xffffffff && this._parents[child] === index) {
1059
+ let child = this.first_child_at(index);
1060
+ while (child !== 0xffffffff && this.parent_at(child) === index) {
717
1061
  _children.push(child);
718
- child = this._next_siblings[child];
1062
+ child = this.next_at(child);
719
1063
  }
1064
+ const parent = this.parent_at(index);
1065
+ const next = this.next_at(index);
1066
+ const prev = this.prev_at(index);
720
1067
  return {
721
- kind: kind_to_string(this._kinds[index]),
722
- start: this._starts[index],
723
- end: this._ends[index],
1068
+ kind: kind_to_string(kind),
1069
+ start: this.start_at(index),
1070
+ end: this.end_at(index),
724
1071
  metadata: {
725
1072
  ...this.metadata_at(index),
726
1073
  ...extras_object,
727
1074
  },
728
- parent: this._parents[index] === 0xffffffff ? null : this._parents[index],
729
- next: this._next_siblings[index] === 0xffffffff
730
- ? null
731
- : this._next_siblings[index],
732
- prev: this._prev_siblings[index] === 0xffffffff
733
- ? null
734
- : this._prev_siblings[index],
735
- value: [this._value_starts[index], this._value_ends[index]],
1075
+ parent: parent === 0xffffffff ? null : parent,
1076
+ next: next === 0xffffffff ? null : next,
1077
+ prev: prev === 0xffffffff ? null : prev,
1078
+ value: [this.value_start_at(index), this.value_end_at(index)],
736
1079
  children: _children,
737
1080
  index: index,
738
1081
  };