@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
@@ -1,32 +1,101 @@
1
- import type { Emitter } from "./opcodes";
2
- import { NodeBuffer, NodeKind } from "./utils";
3
- import type { PluginDispatcher } from "./plugin_dispatch";
1
+ import type { Emitter } from './opcodes';
2
+ import {
3
+ NodeBuffer,
4
+ NodeField,
5
+ NodeKind,
6
+ make_meta,
7
+ merge_meta,
8
+ } from './utils';
9
+ import type { PluginDispatcher } from './plugin_dispatch';
10
+
11
+ const NONE = 0xffffffff;
12
+
13
+ /** open_wants of a builder without plugins, never written */
14
+ const NO_WANTS = new Uint8Array(64);
4
15
 
5
16
  /**
6
17
  * consumes opcodes from PFMParser and builds a NodeBuffer.
7
18
  * this is the backward-compatibility layer: the opcode stream is the
8
19
  * primary output, but existing tests and consumers expect a NodeBuffer.
20
+ *
21
+ * ids are dense so each id is its own buffer index, an id that misses the next
22
+ * free slot, as after a revoke pushes a repair node, switches to an id map for
23
+ * the rest of the document, plugins always use the map since they push
24
+ * synthetic nodes
9
25
  */
10
26
  export class TreeBuilder implements Emitter {
11
27
  private nodes: NodeBuffer;
12
- /** maps opcode id -> NodeBuffer index. plain array, ids are sequential integers. */
13
- private id_to_index: number[] = [];
14
- /** maps opcode id -> NodeKind. plain array for o(1) lookup. */
15
- private id_to_kind: number[] = [];
28
+ /** null while ids equal buffer indices */
29
+ private id_to_index: number[] | null = null;
30
+ /**
31
+ * buffer index and kind at open of each node a revoke rewrote, read only
32
+ * for a close or text that does not pass the kind
33
+ */
34
+ private revoked: number[] | null = null;
35
+ /** kind at open by index, built from revoked on the first read */
36
+ private revoked_map: Map<number, number> | null = null;
37
+ /** entries of revoked already copied into revoked_map */
38
+ private revoked_mapped = 0;
16
39
  /** optional plugin dispatcher. null when no plugins registered. */
17
40
  private dispatcher: PluginDispatcher | null;
18
-
19
- /** callback for dispatcher to register synthetic node ids. */
20
- private register_id = (synthetic_id: number, buf_idx: number): void => {
21
- this.id_to_index[synthetic_id] = buf_idx;
22
- };
41
+ private wants: Uint8Array;
23
42
 
24
43
  constructor(capacity: number, dispatcher?: PluginDispatcher) {
25
44
  this.nodes = new NodeBuffer(capacity);
26
- // NodeBuffer constructor auto-creates root at index 0
27
- this.id_to_index[0] = 0;
28
- this.id_to_kind[0] = NodeKind.root;
29
45
  this.dispatcher = dispatcher ?? null;
46
+ this.wants = dispatcher !== undefined ? dispatcher.open_wants : NO_WANTS;
47
+ }
48
+
49
+ /**
50
+ * Reset a no-plugin builder for another complete document.
51
+ *
52
+ * A plugin dispatcher owns a source-specific TextSource, so those builders
53
+ * cannot be safely reused for a different document.
54
+ */
55
+ reset(): void {
56
+ if (this.dispatcher !== null) {
57
+ throw new Error('TreeBuilder with plugins cannot be reset');
58
+ }
59
+ this.nodes.reset();
60
+ this.nodes.push(NodeKind.root, 0);
61
+ this.id_to_index = null;
62
+ if (this.revoked !== null) {
63
+ this.revoked = null;
64
+ this.revoked_map = null;
65
+ this.revoked_mapped = 0;
66
+ }
67
+ }
68
+
69
+ private index_of(id: number): number | undefined {
70
+ const map = this.id_to_index;
71
+ return map === null ? id : map[id];
72
+ }
73
+
74
+ private start_id_map(): number[] {
75
+ const size = this.nodes._size;
76
+ const map: number[] = [];
77
+ for (let i = 0; i < size; i++) map.push(i);
78
+ this.id_to_index = map;
79
+ return map;
80
+ }
81
+
82
+ /** for callers that do not pass the kind, a revoke may have rewritten it */
83
+ private opened_kind(idx: number): number {
84
+ // plugins rewrite kinds too, the dispatcher logs every rewrite
85
+ const revoked =
86
+ this.dispatcher !== null ? this.dispatcher.kind_log() : this.revoked;
87
+ if (revoked !== null) {
88
+ let map = this.revoked_map;
89
+ if (map === null) map = this.revoked_map = new Map();
90
+ // the first revoke of an index holds the kind at open
91
+ for (let i = this.revoked_mapped; i < revoked.length; i += 2) {
92
+ if (!map.has(revoked[i])) map.set(revoked[i], revoked[i + 1]);
93
+ }
94
+ this.revoked_mapped = revoked.length;
95
+ const kind = map.get(idx);
96
+ if (kind !== undefined) return kind;
97
+ }
98
+ return this.nodes._n[idx * NodeField.stride] & 0xff;
30
99
  }
31
100
 
32
101
  open(
@@ -35,101 +104,172 @@ export class TreeBuilder implements Emitter {
35
104
  start: number,
36
105
  parent: number,
37
106
  extra: number,
38
- pending: boolean,
107
+ pending: boolean
108
+ ): void {
109
+ const nodes = this.nodes;
110
+ // parents are opened ids below this one so they are indices too, id 0
111
+ // never matches as the buffer makes the root, the rare rest stays out of line
112
+ if (this.id_to_index === null && id === nodes._size && !this.wants[kind]) {
113
+ // >>> 0 turns the -1 of no parent into NONE
114
+ nodes.push_node(kind, start, parent >>> 0, extra, pending);
115
+ return;
116
+ }
117
+ if (id !== 0) this.open_slow(id, kind, start, parent, extra, pending);
118
+ }
119
+
120
+ private open_slow(
121
+ id: number,
122
+ kind: NodeKind,
123
+ start: number,
124
+ parent: number,
125
+ extra: number,
126
+ pending: boolean
127
+ ): void {
128
+ const nodes = this.nodes;
129
+ const dispatcher = this.dispatcher;
130
+ if (
131
+ dispatcher !== null &&
132
+ this.id_to_index === null &&
133
+ id === nodes._size
134
+ ) {
135
+ let parent_idx = parent === -1 ? NONE : parent;
136
+ if (!dispatcher.wants_open(kind)) {
137
+ nodes.push_node(kind, start, parent_idx, extra, pending);
138
+ return;
139
+ }
140
+ // children of a wrap_inner parent go to its wrapper
141
+ if (parent_idx !== NONE) {
142
+ const redirect = dispatcher.get_redirect(parent_idx);
143
+ if (redirect !== undefined) parent_idx = redirect;
144
+ }
145
+ const idx = nodes.push_node(kind, start, parent_idx, extra, pending);
146
+ if (dispatcher.has_handlers(kind)) {
147
+ dispatcher.dispatch_open(idx, kind, nodes);
148
+ this.wants = dispatcher.open_wants;
149
+ }
150
+ return;
151
+ }
152
+ this.open_mapped(id, kind, start, parent, extra, pending);
153
+ }
154
+
155
+ /** an open once a plugin pushed nodes without ids or ids skipped a slot */
156
+ private open_mapped(
157
+ id: number,
158
+ kind: NodeKind,
159
+ start: number,
160
+ parent: number,
161
+ extra: number,
162
+ pending: boolean
39
163
  ): void {
40
- // root (id=0) is auto-created by NodeBuffer constructor, skip
41
- if (id === 0) return;
164
+ const nodes = this.nodes;
165
+ const dispatcher = this.dispatcher;
166
+ const map = this.id_to_index ?? this.start_id_map();
42
167
 
43
- let parent_idx =
44
- parent === -1 ? 0xffffffff : (this.id_to_index[parent] ?? 0xffffffff);
168
+ let parent_idx = parent === -1 ? NONE : (map[parent] ?? NONE);
45
169
 
46
170
  // plugin redirect: if parent has a wrap_inner wrapper, children go there
47
- if (this.dispatcher && parent_idx !== 0xffffffff) {
48
- const redirect = this.dispatcher.get_redirect(parent_idx);
171
+ if (dispatcher !== null && parent_idx !== NONE) {
172
+ const redirect = dispatcher.get_redirect(parent_idx);
49
173
  if (redirect !== undefined) parent_idx = redirect;
50
174
  }
51
175
 
52
- const idx = pending
53
- ? this.nodes.push_pending(kind, start, parent_idx, extra)
54
- : this.nodes.push(kind, start, parent_idx, extra);
55
- this.id_to_index[id] = idx;
56
- this.id_to_kind[id] = kind;
176
+ const idx = nodes.push_node(kind, start, parent_idx, extra, pending);
177
+ map[id] = idx;
57
178
 
58
- // plugin dispatch
59
- if (this.dispatcher && this.dispatcher.has_handlers(kind)) {
60
- this.dispatcher.dispatch_open(
61
- idx,
62
- kind,
63
- this.nodes,
64
- this.register_id,
65
- );
179
+ if (dispatcher !== null && dispatcher.has_handlers(kind)) {
180
+ dispatcher.dispatch_open(idx, kind, nodes);
181
+ this.wants = dispatcher.open_wants;
66
182
  }
67
183
  }
68
184
 
69
- close(id: number, end: number): void {
70
- const idx = this.id_to_index[id];
185
+ close(id: number, end: number, kind?: NodeKind): void {
186
+ const idx = this.index_of(id);
71
187
  if (idx === undefined) return;
72
- this.nodes.set_end(idx, end);
73
-
74
- // capture pending state before close dispatch / commit
75
- const was_pending = this.nodes._pending_nodes[idx] === 1;
188
+ const b = idx * NodeField.stride;
189
+ const n = this.nodes._n;
190
+ n[b + NodeField.end] = end;
191
+ // the rare closes go to a call so close inlines cheaply at every emit site
192
+ if (
193
+ this.dispatcher === null &&
194
+ kind !== undefined &&
195
+ kind !== NodeKind.list &&
196
+ (kind !== NodeKind.paragraph || n[b + NodeField.pending] === 0)
197
+ ) {
198
+ n[b + NodeField.pending] = 0;
199
+ return;
200
+ }
201
+ this.close_rest(idx, b, kind);
202
+ }
76
203
 
77
- // plugin close dispatch: fire close callbacks before committing
78
- if (this.dispatcher) {
79
- this.dispatcher.dispatch_close(idx, this.nodes);
204
+ private close_rest(idx: number, b: number, kind: NodeKind | undefined): void {
205
+ const nodes = this.nodes;
206
+ // fire close callbacks before committing, a quiet dispatcher has none
207
+ const dispatcher = this.dispatcher;
208
+ if (dispatcher !== null && !dispatcher.quiet()) {
209
+ dispatcher.dispatch_close(idx, nodes);
80
210
  }
81
211
 
82
212
  // pending paragraphs inside list_items are tight-list speculation
83
213
  // wrappers, they stay pending after close until the list closes
84
214
  // and the parser either revokes (tight) or commits (loose) them.
85
- const kind = this.id_to_kind[id];
86
- const parent_kind = this.nodes._kinds[
87
- this.nodes._parents[idx]
88
- ] as NodeKind;
215
+ // dispatch_close already committed the undo log of a node not pending
216
+ if (kind === undefined) kind = this.opened_kind(idx);
217
+ const n = nodes._n;
89
218
  if (
90
- !(
91
- kind === NodeKind.paragraph &&
92
- parent_kind === NodeKind.list_item &&
93
- this.nodes._pending_nodes[idx] === 1
94
- )
219
+ kind === NodeKind.paragraph &&
220
+ n[b + NodeField.pending] === 1 &&
221
+ (n[n[b + NodeField.parent] * NodeField.stride] & 0xff) ===
222
+ NodeKind.list_item
95
223
  ) {
96
- this.nodes.commit_node(idx);
97
- if (this.dispatcher && !was_pending) {
98
- this.dispatcher.dispatch_commit(idx);
99
- }
224
+ return;
100
225
  }
226
+ n[b + NodeField.pending] = 0;
227
+ if (kind === NodeKind.list) this.unwrap_tight_list(idx);
228
+ }
101
229
 
102
- // tight list unwrapping: if this is a list with tight=true, walk
103
- // items and unwrap their paragraph children. safe no-op when the
104
- // parser already revoked them via finalize_list_pending_paras.
105
- if (kind === NodeKind.list) {
106
- const meta = this.nodes.metadata_at(idx);
107
- if (meta && meta.tight) {
108
- const list_node = this.nodes.get_node(idx);
109
- for (const item_idx of list_node.children) {
110
- const item = this.nodes.get_node(item_idx);
111
- for (const child_idx of item.children) {
112
- if (this.nodes.kind_at(child_idx) === NodeKind.paragraph) {
113
- this.nodes.unwrap_node(child_idx);
114
- }
115
- }
230
+ /** a no op when finalize_list_pending_paras already revoked the paragraphs */
231
+ private unwrap_tight_list(idx: number): void {
232
+ const nodes = this.nodes;
233
+ const meta = nodes.metadata_at(idx);
234
+ if (!meta || !meta.tight) return;
235
+ // next is read before the unwrap, so children it splices in are skipped
236
+ let item = nodes.first_child_at(idx);
237
+ let more_items = item !== NONE && nodes.parent_at(item) === idx;
238
+ while (more_items) {
239
+ const next_item = nodes.next_at(item);
240
+ more_items = next_item !== NONE && nodes.parent_at(next_item) === idx;
241
+ let child = nodes.first_child_at(item);
242
+ let more = child !== NONE && nodes.parent_at(child) === item;
243
+ while (more) {
244
+ const next = nodes.next_at(child);
245
+ more = next !== NONE && nodes.parent_at(next) === item;
246
+ if (nodes.kind_at(child) === NodeKind.paragraph) {
247
+ nodes.unwrap_node(child);
116
248
  }
249
+ child = next;
117
250
  }
251
+ item = next_item;
118
252
  }
119
253
  }
120
254
 
121
- text(parent: number, start: number, end: number): void {
122
- let parent_idx = this.id_to_index[parent];
255
+ text(
256
+ parent: number,
257
+ start: number,
258
+ end: number,
259
+ parent_kind?: NodeKind
260
+ ): void {
261
+ let parent_idx = this.index_of(parent);
123
262
  if (parent_idx === undefined) return;
124
263
 
125
- // plugin redirect: text targeting a wrapped parent goes to the wrapper
126
- if (this.dispatcher) {
264
+ if (this.dispatcher !== null) {
265
+ if (parent_kind === undefined) parent_kind = this.opened_kind(parent_idx);
266
+ // plugin redirect: text targeting a wrapped parent goes to the wrapper
127
267
  const redirect = this.dispatcher.get_redirect(parent_idx);
128
268
  if (redirect !== undefined) parent_idx = redirect;
269
+ } else if (parent_kind === undefined) {
270
+ parent_kind = this.opened_kind(parent_idx);
129
271
  }
130
272
 
131
- const parent_kind = this.id_to_kind[parent];
132
-
133
273
  // nodes that store content as a value range (no child text node)
134
274
  if (
135
275
  parent_kind === NodeKind.heading ||
@@ -139,35 +279,58 @@ export class TreeBuilder implements Emitter {
139
279
  ) {
140
280
  this.nodes.set_value(parent_idx, start, end);
141
281
  } else {
142
- // create a child text node
143
- const idx = this.nodes.push(NodeKind.text, start, parent_idx);
144
- this.nodes.set_value(idx, start, end);
145
- this.nodes.set_end(idx, end);
282
+ // fills the slot of the id the parser reserved for this text node
283
+ this.nodes.push_text(start, end, parent_idx);
284
+ }
285
+ }
286
+
287
+ leaf(
288
+ id: number,
289
+ kind: NodeKind,
290
+ start: number,
291
+ parent: number,
292
+ value_start: number,
293
+ value_end: number,
294
+ end: number
295
+ ): void {
296
+ const nodes = this.nodes;
297
+ const dispatcher = this.dispatcher;
298
+ if (
299
+ this.id_to_index === null &&
300
+ id === nodes._size &&
301
+ (dispatcher === null || (!this.wants[kind] && dispatcher.quiet()))
302
+ ) {
303
+ nodes.push_leaf(kind, start, value_start, value_end, end, parent);
304
+ return;
146
305
  }
306
+ this.open(id, kind, start, parent, 0, false);
307
+ this.set_value_start(id, value_start);
308
+ this.set_value_end(id, value_end);
309
+ this.close(id, end, kind);
147
310
  }
148
311
 
149
312
  attr(id: number, key: string, value: any): void {
150
- const idx = this.id_to_index[id];
313
+ const idx = this.index_of(id);
151
314
  if (idx === undefined) return;
152
315
 
153
316
  switch (key) {
154
- case "value":
317
+ case 'value':
155
318
  this.nodes.set_value(idx, value[0], value[1]);
156
319
  break;
157
- case "value_start":
320
+ case 'value_start':
158
321
  this.nodes.set_value_start(idx, value);
159
322
  break;
160
- case "value_end":
323
+ case 'value_end':
161
324
  this.nodes.set_value_end(idx, value);
162
325
  break;
163
326
  default: {
164
- // merge into metadata map
165
- const existing = this.nodes.metadata_at(idx);
327
+ // mutated in place, so no set_metadata call is needed
328
+ const nodes = this.nodes;
329
+ const existing = nodes.metadata_at(idx);
166
330
  if (existing) {
167
- existing[key] = value;
168
- this.nodes.set_metadata(idx, existing);
331
+ merge_meta(existing, key, value);
169
332
  } else {
170
- this.nodes.set_metadata(idx, { [key]: value });
333
+ nodes.set_metadata(idx, make_meta(key, value));
171
334
  }
172
335
  break;
173
336
  }
@@ -175,29 +338,41 @@ export class TreeBuilder implements Emitter {
175
338
  }
176
339
 
177
340
  set_value_start(id: number, pos: number): void {
178
- const idx = this.id_to_index[id];
341
+ const idx = this.index_of(id);
179
342
  if (idx !== undefined) this.nodes.set_value_start(idx, pos);
180
343
  }
181
344
 
182
345
  set_value_end(id: number, pos: number): void {
183
- const idx = this.id_to_index[id];
346
+ const idx = this.index_of(id);
184
347
  if (idx !== undefined) this.nodes.set_value_end(idx, pos);
185
348
  }
186
349
 
187
- revoke(id: number, source_text?: string): void {
188
- const idx = this.id_to_index[id];
350
+ revoke(id: number, source_text?: string, text_start?: number): void {
351
+ const idx = this.index_of(id);
189
352
  if (idx === undefined) return;
353
+ const nodes = this.nodes;
190
354
 
191
355
  // plugin revoke: undo mutations before handle_repair
192
- if (this.dispatcher) {
193
- this.dispatcher.dispatch_revoke(idx, this.nodes);
356
+ if (this.dispatcher !== null) {
357
+ this.dispatcher.dispatch_revoke(idx, nodes);
358
+ const kind = nodes.kind_at(idx);
359
+ nodes.handle_repair(idx, source_text, text_start);
360
+ if (nodes.kind_at(idx) !== kind) this.dispatcher.log_kind(idx, kind);
361
+ return;
194
362
  }
195
363
 
196
- this.nodes.handle_repair(idx, source_text);
364
+ const kind = nodes.kind_at(idx);
365
+ nodes.handle_repair(idx, source_text, text_start);
366
+ // close and text act on the kind at open, callers that omit it read it here
367
+ if (nodes.kind_at(idx) !== kind) {
368
+ const revoked = this.revoked;
369
+ if (revoked === null) this.revoked = [idx, kind];
370
+ else revoked.push(idx, kind);
371
+ }
197
372
  }
198
373
 
199
374
  commit(id: number): void {
200
- const idx = this.id_to_index[id];
375
+ const idx = this.index_of(id);
201
376
  if (idx === undefined) return;
202
377
  this.nodes.commit_node(idx);
203
378
  if (this.dispatcher) {
@@ -207,6 +382,11 @@ export class TreeBuilder implements Emitter {
207
382
 
208
383
  cursor(_pos: number): void {}
209
384
 
385
+ /** an incremental document is complete, give its unused slab tail back */
386
+ end(): void {
387
+ this.nodes.trim();
388
+ }
389
+
210
390
  /** extract the built NodeBuffer. */
211
391
  get_buffer(): NodeBuffer {
212
392
  return this.nodes;
package/src/undo_log.ts CHANGED
@@ -1,9 +1,9 @@
1
- import type { NodeBuffer } from "./utils";
1
+ import type { NodeBuffer } from './utils';
2
2
 
3
3
  const NONE = 0xffffffff;
4
4
 
5
5
  /** sentinel: the attribute key did not exist before this write. */
6
- export const ATTR_DID_NOT_EXIST: unique symbol = Symbol("ATTR_DID_NOT_EXIST");
6
+ export const ATTR_DID_NOT_EXIST: unique symbol = Symbol('ATTR_DID_NOT_EXIST');
7
7
 
8
8
  // biome-ignore lint/suspicious/no-const-enum: matches project convention
9
9
  export const enum UndoEntryKind {
@@ -80,28 +80,34 @@ export type UndoEntry =
80
80
  * the entire subtree is detached.
81
81
  */
82
82
  function _unlink_child(buf: NodeBuffer, parent: number, child: number): void {
83
- const prev = buf._prev_siblings[child];
84
- const next = buf._next_siblings[child];
83
+ const prev = buf.prev_at(child);
84
+ const next = buf.next_at(child);
85
85
 
86
86
  if (prev !== NONE) {
87
- buf._next_siblings[prev] = next;
88
- } else if (buf._children_starts[parent] === child) {
89
- buf._children_starts[parent] = next !== NONE && buf._parents[next] === parent ? next : NONE;
87
+ buf.set_next(prev, next);
88
+ } else if (buf.first_child_at(parent) === child) {
89
+ buf.set_first_child(
90
+ parent,
91
+ next !== NONE && buf.parent_at(next) === parent ? next : NONE
92
+ );
90
93
  }
91
94
 
92
- if (next !== NONE && buf._parents[next] === parent) {
93
- buf._prev_siblings[next] = prev;
95
+ if (next !== NONE && buf.parent_at(next) === parent) {
96
+ buf.set_prev(next, prev);
94
97
  }
95
98
 
96
- if (buf._children_ends[parent] === child) {
97
- buf._children_ends[parent] = prev !== NONE ? prev : NONE;
99
+ if (buf.last_child_at(parent) === child) {
100
+ buf.set_last_child(parent, prev !== NONE ? prev : NONE);
98
101
  }
99
102
 
100
- buf._parents[child] = NONE;
101
- buf._next_siblings[child] = NONE;
102
- buf._prev_siblings[child] = NONE;
103
+ buf.set_parent(child, NONE);
104
+ buf.set_next(child, NONE);
105
+ buf.set_prev(child, NONE);
103
106
  }
104
107
 
108
+ /** never written, every write swaps in a map of its own first */
109
+ const NO_LOGS: Map<number, UndoEntry[]> = new Map();
110
+
105
111
  /**
106
112
  * per-node undo log for plugin mutations.
107
113
  *
@@ -114,7 +120,8 @@ function _unlink_child(buf: NodeBuffer, parent: number, child: number): void {
114
120
  */
115
121
  export class UndoLog {
116
122
  /** per-handler-node logs. maps buffer index -> append-only entry array. */
117
- private logs: Map<number, UndoEntry[]> = new Map();
123
+ // a shared empty map until the first entry, most documents record none
124
+ private logs: Map<number, UndoEntry[]> = NO_LOGS;
118
125
 
119
126
  /**
120
127
  * the buffer index of the node whose handler is currently running.
@@ -123,6 +130,18 @@ export class UndoLog {
123
130
  */
124
131
  private active_node: number = NONE;
125
132
 
133
+ /**
134
+ * buffer index and prior kind of every kind rewrite, recorded or not, in
135
+ * order, so the first entry of an index holds its kind at open
136
+ */
137
+ kind_changes: number[] | null = null;
138
+
139
+ log_kind(target: number, prior_kind: number): void {
140
+ const log = this.kind_changes;
141
+ if (log === null) this.kind_changes = [target, prior_kind];
142
+ else log.push(target, prior_kind);
143
+ }
144
+
126
145
  /** called by dispatcher before invoking plugin handlers for a node. */
127
146
  set_active_node(index: number): void {
128
147
  this.active_node = index;
@@ -133,8 +152,13 @@ export class UndoLog {
133
152
  this.active_node = NONE;
134
153
  }
135
154
 
155
+ /** false while nothing would keep an entry */
156
+ get recording(): boolean {
157
+ return this.active_node !== NONE;
158
+ }
136
159
 
137
160
  record_attr_set(target: number, key: string, prior_value: any): void {
161
+ if (this.active_node === NONE) return;
138
162
  this._append({
139
163
  kind: UndoEntryKind.AttrSet,
140
164
  target,
@@ -153,6 +177,8 @@ export class UndoLog {
153
177
  }
154
178
 
155
179
  record_type_change(target: number, prior_kind: number): void {
180
+ this.log_kind(target, prior_kind);
181
+ if (this.active_node === NONE) return;
156
182
  this._append({
157
183
  kind: UndoEntryKind.TypeChange,
158
184
  target,
@@ -164,7 +190,7 @@ export class UndoLog {
164
190
  parent: number,
165
191
  wrapper: number,
166
192
  prior_first_child: number,
167
- prior_last_child: number,
193
+ prior_last_child: number
168
194
  ): void {
169
195
  this._append({
170
196
  kind: UndoEntryKind.WrapInner,
@@ -178,7 +204,7 @@ export class UndoLog {
178
204
  record_prepend(
179
205
  parent: number,
180
206
  created: number,
181
- prior_first_child: number,
207
+ prior_first_child: number
182
208
  ): void {
183
209
  this._append({
184
210
  kind: UndoEntryKind.Prepend,
@@ -191,7 +217,7 @@ export class UndoLog {
191
217
  record_append(
192
218
  parent: number,
193
219
  created: number,
194
- prior_last_child: number,
220
+ prior_last_child: number
195
221
  ): void {
196
222
  this._append({
197
223
  kind: UndoEntryKind.Append,
@@ -204,15 +230,16 @@ export class UndoLog {
204
230
  private _append(entry: UndoEntry): void {
205
231
  const node = this.active_node;
206
232
  if (node === NONE) return;
207
- let log = this.logs.get(node);
233
+ let logs = this.logs;
234
+ if (logs === NO_LOGS) logs = this.logs = new Map();
235
+ let log = logs.get(node);
208
236
  if (log === undefined) {
209
237
  log = [];
210
- this.logs.set(node, log);
238
+ logs.set(node, log);
211
239
  }
212
240
  log.push(entry);
213
241
  }
214
242
 
215
-
216
243
  /**
217
244
  * revoke all mutations attributed to the given handler node.
218
245
  * walks the undo log in reverse order, restoring prior state.
@@ -245,7 +272,7 @@ export class UndoLog {
245
272
  }
246
273
 
247
274
  case UndoEntryKind.TypeChange: {
248
- buf._kinds[entry.target] = entry.prior_kind;
275
+ buf.set_kind(entry.target, entry.prior_kind);
249
276
  break;
250
277
  }
251
278
 
@@ -256,7 +283,7 @@ export class UndoLog {
256
283
  const parent = entry.parent;
257
284
  const wrapper = entry.wrapper;
258
285
  buf.unwrap_node(wrapper);
259
- buf._parents[wrapper] = NONE;
286
+ buf.set_parent(wrapper, NONE);
260
287
  break;
261
288
  }
262
289
 
@@ -281,7 +308,12 @@ export class UndoLog {
281
308
  * commit a node, discard its undo log, making mutations permanent.
282
309
  */
283
310
  commit(handler_node: number): void {
284
- this.logs.delete(handler_node);
311
+ if (this.logs.size !== 0) this.logs.delete(handler_node);
312
+ }
313
+
314
+ /** true when no node holds a mutation that a revoke would undo */
315
+ get empty(): boolean {
316
+ return this.logs.size === 0;
285
317
  }
286
318
 
287
319
  /** check whether a node has any recorded mutations. */
@@ -291,12 +323,14 @@ export class UndoLog {
291
323
 
292
324
  /** get undo entries for a node (for redirect detection). */
293
325
  get_entries(handler_node: number): UndoEntry[] | undefined {
326
+ if (this.logs.size === 0) return undefined;
294
327
  return this.logs.get(handler_node);
295
328
  }
296
329
 
297
330
  /** discard all logs. */
298
331
  clear(): void {
299
332
  this.logs.clear();
333
+ this.kind_changes = null;
300
334
  this.active_node = NONE;
301
335
  }
302
336
  }