@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,35 +1,39 @@
1
- import { type NodeBuffer, NodeKind, kind_to_string, string_to_kind } from "./utils";
2
- import { UndoLog, UndoEntryKind } from "./undo_log";
1
+ import {
2
+ type NodeBuffer,
3
+ NodeField,
4
+ NodeKind,
5
+ kind_to_string,
6
+ string_to_kind,
7
+ } from './utils';
8
+ import { UndoLog, UndoEntryKind } from './undo_log';
3
9
  import {
4
10
  NodeView,
5
11
  ViewCache,
6
12
  type TextSource,
7
13
  SourceTextSource,
8
14
  WireTextSource,
9
- } from "./node_view";
15
+ } from './node_view';
10
16
  import type {
11
17
  ParsePlugin,
12
18
  NodeHandler,
13
19
  ComposedHandler,
14
20
  PluginContext,
15
- IdRegister,
16
- } from "./plugin_types";
21
+ } from './plugin_types';
17
22
 
18
23
  const NONE = 0xffffffff;
19
24
 
20
25
  /** total number of node kinds in the enum. */
21
- const NODE_KIND_COUNT = 35;
26
+ const NODE_KIND_COUNT = 36;
22
27
 
23
28
  /** first synthetic id. high bit flag partitions id space from parser ids. */
24
29
  const SYNTHETIC_ID_BASE = 0x40000000;
25
30
 
26
-
27
31
  /** single handler: no wrapper needed, just normalize return type. */
28
32
  function compose_1(h: NodeHandler): ComposedHandler {
29
33
  const parse = h.parse;
30
34
  return function handler_1(
31
35
  view: NodeView,
32
- ctx: PluginContext,
36
+ ctx: PluginContext
33
37
  ): (() => void)[] | null {
34
38
  const cb = parse(view, ctx);
35
39
  if (cb) return [cb];
@@ -43,7 +47,7 @@ function compose_2(a: NodeHandler, b: NodeHandler): ComposedHandler {
43
47
  const pb = b.parse;
44
48
  return function handler_2(
45
49
  view: NodeView,
46
- ctx: PluginContext,
50
+ ctx: PluginContext
47
51
  ): (() => void)[] | null {
48
52
  const ca = pa(view, ctx);
49
53
  const cb = pb(view, ctx);
@@ -60,14 +64,14 @@ function compose_2(a: NodeHandler, b: NodeHandler): ComposedHandler {
60
64
  function compose_3(
61
65
  a: NodeHandler,
62
66
  b: NodeHandler,
63
- c: NodeHandler,
67
+ c: NodeHandler
64
68
  ): ComposedHandler {
65
69
  const pa = a.parse;
66
70
  const pb = b.parse;
67
71
  const pc = c.parse;
68
72
  return function handler_3(
69
73
  view: NodeView,
70
- ctx: PluginContext,
74
+ ctx: PluginContext
71
75
  ): (() => void)[] | null {
72
76
  const ca = pa(view, ctx);
73
77
  const cb = pb(view, ctx);
@@ -92,7 +96,7 @@ function compose_n(handlers: NodeHandler[]): ComposedHandler {
92
96
  const len = parses.length;
93
97
  return function handler_n(
94
98
  view: NodeView,
95
- ctx: PluginContext,
99
+ ctx: PluginContext
96
100
  ): (() => void)[] | null {
97
101
  let result: (() => void)[] | null = null;
98
102
  for (let i = 0; i < len; i++) {
@@ -118,16 +122,20 @@ function compose(handlers: NodeHandler[]): ComposedHandler {
118
122
  }
119
123
  }
120
124
 
121
-
122
- /** 35 slots, one per NodeKind. null means no handlers. */
125
+ /** 36 slots, one per NodeKind, null means no handlers */
123
126
  type HandlersTable = (ComposedHandler | null)[];
124
127
 
125
128
  interface RegistrationResult {
126
129
  fused: HandlersTable;
127
130
  sequential: { plugin: ParsePlugin; handlers: HandlersTable }[];
128
131
  has_handler: Uint32Array;
132
+ /** 1 for each kind with a fused handler, shared, never written */
133
+ open_wants: Uint8Array;
129
134
  }
130
135
 
136
+ /** open_wants once a redirect exists, any open may need retargeting */
137
+ const ALL_WANTS = new Uint8Array(64).fill(1);
138
+
131
139
  function register_plugins(plugins: ParsePlugin[]): RegistrationResult {
132
140
  const fused_plugins: ParsePlugin[] = [];
133
141
  const sequential_plugins: ParsePlugin[] = [];
@@ -142,17 +150,16 @@ function register_plugins(plugins: ParsePlugin[]): RegistrationResult {
142
150
 
143
151
  // build per-kind handler lists for fused plugins
144
152
  const per_kind: (NodeHandler[] | null)[] = new Array(NODE_KIND_COUNT).fill(
145
- null,
153
+ null
146
154
  );
147
155
 
148
156
  for (const plugin of fused_plugins) {
149
157
  for (const key of Object.keys(plugin)) {
150
- if (key === "sequential") continue;
158
+ if (key === 'sequential') continue;
151
159
  const kind = string_to_kind(key);
152
160
  if (kind === undefined) continue;
153
161
  const entry = plugin[key];
154
- if (!entry || typeof entry !== "object" || !("parse" in entry))
155
- continue;
162
+ if (!entry || typeof entry !== 'object' || !('parse' in entry)) continue;
156
163
  if (!per_kind[kind]) per_kind[kind] = [];
157
164
  per_kind[kind]!.push(entry as NodeHandler);
158
165
  }
@@ -160,7 +167,7 @@ function register_plugins(plugins: ParsePlugin[]): RegistrationResult {
160
167
 
161
168
  // compose fused handlers
162
169
  const fused: HandlersTable = new Array(NODE_KIND_COUNT).fill(null);
163
- const has_handler = new Uint32Array(2); // 64 bits, need 35
170
+ const has_handler = new Uint32Array(2); // 64 bits, need 36
164
171
 
165
172
  for (let i = 0; i < NODE_KIND_COUNT; i++) {
166
173
  const list = per_kind[i];
@@ -174,236 +181,245 @@ function register_plugins(plugins: ParsePlugin[]): RegistrationResult {
174
181
  const sequential = sequential_plugins.map((plugin) => {
175
182
  const table: HandlersTable = new Array(NODE_KIND_COUNT).fill(null);
176
183
  for (const key of Object.keys(plugin)) {
177
- if (key === "sequential") continue;
184
+ if (key === 'sequential') continue;
178
185
  const kind = string_to_kind(key);
179
186
  if (kind === undefined) continue;
180
187
  const entry = plugin[key];
181
- if (!entry || typeof entry !== "object" || !("parse" in entry))
182
- continue;
188
+ if (!entry || typeof entry !== 'object' || !('parse' in entry)) continue;
183
189
  table[kind] = compose_1(entry as NodeHandler);
184
190
  }
185
191
  return { plugin, handlers: table };
186
192
  });
187
193
 
188
- return { fused, sequential, has_handler };
194
+ // a pass with no handler would do nothing
195
+ return {
196
+ fused,
197
+ sequential: sequential.filter((pass) =>
198
+ pass.handlers.some((h) => h !== null)
199
+ ),
200
+ has_handler,
201
+ open_wants: wants_of(has_handler),
202
+ };
189
203
  }
190
204
 
205
+ function wants_of(has_handler: Uint32Array): Uint8Array {
206
+ const wants = new Uint8Array(64);
207
+ for (let k = 0; k < 64; k++) {
208
+ if (has_handler[k >> 5] & (1 << (k & 31))) wants[k] = 1;
209
+ }
210
+ return wants;
211
+ }
212
+
213
+ /** parse_of for an entry the registration skips */
214
+ const NO_PARSE = {};
215
+
216
+ function parse_of(entry: unknown): unknown {
217
+ return entry && typeof entry === 'object' && 'parse' in entry
218
+ ? (entry as NodeHandler).parse
219
+ : NO_PARSE;
220
+ }
221
+
222
+ /** everything register_plugins reads, in the order it reads it */
223
+ function snapshot_plugins(plugins: ParsePlugin[]): unknown[] {
224
+ const snap: unknown[] = [plugins.length];
225
+ for (let i = 0; i < plugins.length; i++) {
226
+ const p = plugins[i];
227
+ const keys = Object.keys(p);
228
+ snap.push(p, !!p.sequential, keys.length);
229
+ for (let k = 0; k < keys.length; k++) {
230
+ const entry = p[keys[k]];
231
+ snap.push(keys[k], entry, parse_of(entry));
232
+ }
233
+ }
234
+ return snap;
235
+ }
236
+
237
+ function snapshot_matches(plugins: ParsePlugin[], snap: unknown[]): boolean {
238
+ let j = 0;
239
+ if (snap[j++] !== plugins.length) return false;
240
+ for (let i = 0; i < plugins.length; i++) {
241
+ const p = plugins[i];
242
+ if (snap[j++] !== p) return false;
243
+ if (snap[j++] !== !!p.sequential) return false;
244
+ // inherited enumerable keys change the count or a key, so an exact match
245
+ // means the same own keys without allocating Object.keys
246
+ const count = snap[j++] as number;
247
+ const keys_end = j + count * 3;
248
+ for (const key in p) {
249
+ if (j === keys_end || snap[j++] !== key) return false;
250
+ const entry = p[key];
251
+ if (snap[j++] !== entry) return false;
252
+ if (snap[j++] !== parse_of(entry)) return false;
253
+ }
254
+ if (j !== keys_end) return false;
255
+ }
256
+ return true;
257
+ }
258
+
259
+ // nothing writes a registration after register_plugins, so documents whose
260
+ // plugins hold the same objects, keys and handlers can share one
261
+ let last_snapshot: unknown[] | null = null;
262
+ let last_registration: RegistrationResult | null = null;
263
+
264
+ function registration_for(plugins: ParsePlugin[]): RegistrationResult {
265
+ if (last_snapshot !== null && snapshot_matches(plugins, last_snapshot)) {
266
+ return last_registration!;
267
+ }
268
+ const reg = register_plugins(plugins);
269
+ last_snapshot = snapshot_plugins(plugins);
270
+ last_registration = reg;
271
+ return reg;
272
+ }
191
273
 
192
274
  /**
193
- * stores close callbacks indexed by buffer index.
194
- * flat array gives O(1) access since buffer indices are sequential.
275
+ * a Map since handled nodes can sit over 1024 indices apart, and an array
276
+ * written past such a gap falls back to dictionary elements
195
277
  */
196
278
  class CloseCallbackStore {
197
- private store: ((() => void)[] | undefined)[] = [];
279
+ private store: Map<number, (() => void)[]> | null = null;
280
+ // entries still set, most nodes close with none so the lookup is skipped
281
+ live = 0;
198
282
 
199
283
  set(idx: number, callbacks: (() => void)[]): void {
200
- this.store[idx] = callbacks;
284
+ let store = this.store;
285
+ if (store === null) store = this.store = new Map();
286
+ store.set(idx, callbacks);
287
+ this.live = store.size;
201
288
  }
202
289
 
203
290
  /** take close callbacks for a node, removing the entry. returns undefined if none. */
204
291
  take(idx: number): (() => void)[] | undefined {
205
- const cbs = this.store[idx];
206
- if (cbs !== undefined) this.store[idx] = undefined;
292
+ if (this.live === 0) return undefined;
293
+ const store = this.store!;
294
+ const cbs = store.get(idx);
295
+ if (cbs !== undefined) {
296
+ store.delete(idx);
297
+ this.live = store.size;
298
+ }
207
299
  return cbs;
208
300
  }
209
301
 
210
302
  /** fire close callbacks for a node, removing the entry. */
211
303
  fire(idx: number): void {
212
- const cbs = this.store[idx];
304
+ const cbs = this.take(idx);
213
305
  if (cbs === undefined) return;
214
306
  for (let i = 0; i < cbs.length; i++) {
215
307
  cbs[i]();
216
308
  }
217
- this.store[idx] = undefined;
218
309
  }
219
310
 
220
311
  /** discard callbacks without firing (for revocation). */
221
312
  discard(idx: number): void {
222
- this.store[idx] = undefined;
313
+ this.take(idx);
223
314
  }
224
315
 
225
316
  reset(): void {
226
- this.store.length = 0;
317
+ if (this.store !== null) this.store.clear();
318
+ this.live = 0;
227
319
  }
228
320
  }
229
321
 
230
-
231
322
  /**
232
323
  * dispatch plugin handlers for a node open event.
233
324
  *
234
- * the switch destructures the handlers table into 35 locals at the top.
235
- * each arm calls its own local, giving the jit a monomorphic call site
236
- * per kind.
325
+ * each switch arm is a monomorphic call site for its own kind
237
326
  */
238
327
  function dispatch_open(
239
328
  kind: NodeKind,
240
329
  view: NodeView,
241
330
  ctx: PluginContext,
242
331
  fused: HandlersTable,
243
- has_handler: Uint32Array,
332
+ has_handler: Uint32Array
244
333
  ): (() => void)[] | null {
245
334
  // bitmask fast path: no handler for this kind
246
335
  if (!(has_handler[kind >> 5] & (1 << (kind & 31)))) return null;
247
336
 
248
- // destructure into monomorphic locals
249
- const h_0 = fused[0];
250
- const h_1 = fused[1];
251
- const h_2 = fused[2];
252
- const h_3 = fused[3];
253
- const h_4 = fused[4];
254
- const h_5 = fused[5];
255
- const h_6 = fused[6];
256
- const h_7 = fused[7];
257
- const h_8 = fused[8];
258
- const h_9 = fused[9];
259
- const h_10 = fused[10];
260
- const h_11 = fused[11];
261
- const h_12 = fused[12];
262
- const h_13 = fused[13];
263
- const h_14 = fused[14];
264
- const h_15 = fused[15];
265
- const h_16 = fused[16];
266
- const h_17 = fused[17];
267
- const h_18 = fused[18];
268
- const h_19 = fused[19];
269
- const h_20 = fused[20];
270
- const h_21 = fused[21];
271
- const h_22 = fused[22];
272
- const h_23 = fused[23];
273
- const h_24 = fused[24];
274
- const h_25 = fused[25];
275
- const h_26 = fused[26];
276
- const h_27 = fused[27];
277
- const h_28 = fused[28];
278
- const h_29 = fused[29];
279
- const h_30 = fused[30];
280
- const h_31 = fused[31];
281
- const h_32 = fused[32];
282
- const h_33 = fused[33];
283
- const h_34 = fused[34];
284
-
285
337
  switch (kind) {
286
338
  case 0:
287
- return h_0!(view, ctx);
339
+ return fused[0]!(view, ctx);
288
340
  case 1:
289
- return h_1!(view, ctx);
341
+ return fused[1]!(view, ctx);
290
342
  case 2:
291
- return h_2!(view, ctx);
343
+ return fused[2]!(view, ctx);
292
344
  case 3:
293
- return h_3!(view, ctx);
345
+ return fused[3]!(view, ctx);
294
346
  case 4:
295
- return h_4!(view, ctx);
347
+ return fused[4]!(view, ctx);
296
348
  case 5:
297
- return h_5!(view, ctx);
349
+ return fused[5]!(view, ctx);
298
350
  case 6:
299
- return h_6!(view, ctx);
351
+ return fused[6]!(view, ctx);
300
352
  case 7:
301
- return h_7!(view, ctx);
353
+ return fused[7]!(view, ctx);
302
354
  case 8:
303
- return h_8!(view, ctx);
355
+ return fused[8]!(view, ctx);
304
356
  case 9:
305
- return h_9!(view, ctx);
357
+ return fused[9]!(view, ctx);
306
358
  case 10:
307
- return h_10!(view, ctx);
359
+ return fused[10]!(view, ctx);
308
360
  case 11:
309
- return h_11!(view, ctx);
361
+ return fused[11]!(view, ctx);
310
362
  case 12:
311
- return h_12!(view, ctx);
363
+ return fused[12]!(view, ctx);
312
364
  case 13:
313
- return h_13!(view, ctx);
365
+ return fused[13]!(view, ctx);
314
366
  case 14:
315
- return h_14!(view, ctx);
367
+ return fused[14]!(view, ctx);
316
368
  case 15:
317
- return h_15!(view, ctx);
369
+ return fused[15]!(view, ctx);
318
370
  case 16:
319
- return h_16!(view, ctx);
371
+ return fused[16]!(view, ctx);
320
372
  case 17:
321
- return h_17!(view, ctx);
373
+ return fused[17]!(view, ctx);
322
374
  case 18:
323
- return h_18!(view, ctx);
375
+ return fused[18]!(view, ctx);
324
376
  case 19:
325
- return h_19!(view, ctx);
377
+ return fused[19]!(view, ctx);
326
378
  case 20:
327
- return h_20!(view, ctx);
379
+ return fused[20]!(view, ctx);
328
380
  case 21:
329
- return h_21!(view, ctx);
381
+ return fused[21]!(view, ctx);
330
382
  case 22:
331
- return h_22!(view, ctx);
383
+ return fused[22]!(view, ctx);
332
384
  case 23:
333
- return h_23!(view, ctx);
385
+ return fused[23]!(view, ctx);
334
386
  case 24:
335
- return h_24!(view, ctx);
387
+ return fused[24]!(view, ctx);
336
388
  case 25:
337
- return h_25!(view, ctx);
389
+ return fused[25]!(view, ctx);
338
390
  case 26:
339
- return h_26!(view, ctx);
391
+ return fused[26]!(view, ctx);
340
392
  case 27:
341
- return h_27!(view, ctx);
393
+ return fused[27]!(view, ctx);
342
394
  case 28:
343
- return h_28!(view, ctx);
395
+ return fused[28]!(view, ctx);
344
396
  case 29:
345
- return h_29!(view, ctx);
397
+ return fused[29]!(view, ctx);
346
398
  case 30:
347
- return h_30!(view, ctx);
399
+ return fused[30]!(view, ctx);
348
400
  case 31:
349
- return h_31!(view, ctx);
401
+ return fused[31]!(view, ctx);
350
402
  case 32:
351
- return h_32!(view, ctx);
403
+ return fused[32]!(view, ctx);
352
404
  case 33:
353
- return h_33!(view, ctx);
405
+ return fused[33]!(view, ctx);
354
406
  case 34:
355
- return h_34!(view, ctx);
407
+ return fused[34]!(view, ctx);
408
+ case 35:
409
+ return fused[35]!(view, ctx);
356
410
  default:
357
411
  return null;
358
412
  }
359
413
  }
360
414
 
415
+ /** shared undo log until a view or a kind rewrite needs one, never written */
416
+ const NO_UNDO = new UndoLog();
361
417
 
362
- /**
363
- * depth-first walk over the soa buffer.
364
- * calls visitor(idx, kind, false) on open, visitor(idx, kind, true) on close.
365
- */
366
- function walk_tree(
367
- buf: NodeBuffer,
368
- visitor: (idx: number, kind: NodeKind, is_close: boolean) => void,
369
- ): void {
370
- const children_starts = buf._children_starts;
371
- const next_siblings = buf._next_siblings;
372
- const parents = buf._parents;
373
- const kinds = buf._kinds;
374
-
375
- let idx = children_starts[0]; // first child of root
376
- if (idx === NONE) return;
377
-
378
- const stack: number[] = [];
379
-
380
- while (true) {
381
- visitor(idx, kinds[idx] as NodeKind, false);
382
-
383
- const child = children_starts[idx];
384
- if (child !== NONE) {
385
- stack.push(idx);
386
- idx = child;
387
- continue;
388
- }
389
-
390
- visitor(idx, kinds[idx] as NodeKind, true);
391
-
392
- let next = next_siblings[idx];
393
- while (
394
- (next === NONE || parents[next] !== parents[idx]) &&
395
- stack.length > 0
396
- ) {
397
- idx = stack.pop()!;
398
- visitor(idx, kinds[idx] as NodeKind, true);
399
- next = next_siblings[idx];
400
- }
401
-
402
- if (next === NONE || parents[next] !== parents[idx]) break;
403
- idx = next;
404
- }
405
- }
418
+ /** shared close callbacks until the first is set, never written */
419
+ const NO_CLOSE_CBS = new CloseCallbackStore();
406
420
 
421
+ /** shared redirects until the first wrap_inner, never written */
422
+ const NO_REDIRECTS: Map<number, number> = new Map();
407
423
 
408
424
  /**
409
425
  * orchestrates plugin dispatch for both TreeBuilder and WireTreeBuilder.
@@ -415,25 +431,75 @@ export class PluginDispatcher {
415
431
  private fused: HandlersTable;
416
432
  private has_handler: Uint32Array;
417
433
  private sequential: { plugin: ParsePlugin; handlers: HandlersTable }[];
418
- private undo: UndoLog = new UndoLog();
419
- private close_cbs: CloseCallbackStore = new CloseCallbackStore();
420
- private ctx: PluginContext = {};
434
+ private undo: UndoLog = NO_UNDO;
435
+ private close_cbs: CloseCallbackStore = NO_CLOSE_CBS;
436
+ private ctx: PluginContext | null = null;
421
437
  private text_source: TextSource;
422
438
 
423
439
  /**
424
440
  * redirect map: when wrap_inner is called, subsequent children
425
441
  * targeting the parent should land in the wrapper instead.
426
442
  */
427
- private redirects: Map<number, number> = new Map();
443
+ private redirects: Map<number, number> = NO_REDIRECTS;
428
444
 
429
445
  private next_synthetic_id = SYNTHETIC_ID_BASE;
430
446
 
447
+ /** one cache serves every dispatch, a view only holds an index */
448
+ private cache: ViewCache | null = null;
449
+
450
+ /**
451
+ * 1 for each kind whose open needs the dispatcher, every kind once a redirect
452
+ * exists, redirects only start in dispatch_open so builders reread it after one
453
+ */
454
+ open_wants: Uint8Array;
455
+
431
456
  constructor(plugins: ParsePlugin[], text_source: TextSource) {
432
- const reg = register_plugins(plugins);
457
+ const reg = registration_for(plugins);
433
458
  this.fused = reg.fused;
434
459
  this.has_handler = reg.has_handler;
435
460
  this.sequential = reg.sequential;
436
461
  this.text_source = text_source;
462
+ this.open_wants = reg.open_wants;
463
+ }
464
+
465
+ /** cleared since a callback view may have filled it after the last dispatch */
466
+ private views(buf: NodeBuffer): ViewCache {
467
+ const cache = this.cache;
468
+ if (cache === null) {
469
+ let undo = this.undo;
470
+ if (undo === NO_UNDO) undo = this.undo = new UndoLog();
471
+ return (this.cache = new ViewCache(buf, this.text_source, undo));
472
+ }
473
+ cache.clear();
474
+ cache.rebind(buf, this.text_source);
475
+ return cache;
476
+ }
477
+
478
+ /** buffer index and prior kind of each kind rewrite, see UndoLog */
479
+ kind_log(): number[] | null {
480
+ return this.undo.kind_changes;
481
+ }
482
+
483
+ /** a kind rewrite outside the plugins, such as a revoke repair */
484
+ log_kind(buf_idx: number, prior_kind: number): void {
485
+ let undo = this.undo;
486
+ if (undo === NO_UNDO) undo = this.undo = new UndoLog();
487
+ undo.log_kind(buf_idx, prior_kind);
488
+ }
489
+
490
+ /** an open of this kind needs a handler call or a redirect lookup */
491
+ wants_open(kind: NodeKind): boolean {
492
+ return (
493
+ (this.has_handler[kind >> 5] & (1 << (kind & 31))) !== 0 ||
494
+ this.redirects.size !== 0
495
+ );
496
+ }
497
+
498
+ /** a close needs no dispatch */
499
+ quiet(): boolean {
500
+ return (
501
+ this.close_cbs.live === 0 && this.redirects.size === 0 && this.undo.empty
502
+ );
437
503
  }
438
504
 
439
505
  /** check whether any fused handlers exist for this kind. */
@@ -443,17 +509,17 @@ export class PluginDispatcher {
443
509
 
444
510
  /** check if a parent index has a redirect (wrap_inner). */
445
511
  get_redirect(parent_idx: number): number | undefined {
512
+ if (this.redirects.size === 0) return undefined;
446
513
  return this.redirects.get(parent_idx);
447
514
  }
448
515
 
449
- /** register a wrap_inner redirect. */
450
- set_redirect(parent_idx: number, wrapper_idx: number): void {
451
- this.redirects.set(parent_idx, wrapper_idx);
452
- }
453
-
454
- /** clear a redirect (on parent close). */
455
- clear_redirect(parent_idx: number): void {
456
- this.redirects.delete(parent_idx);
516
+ private own_redirects(): Map<number, number> {
517
+ let redirects = this.redirects;
518
+ if (redirects === NO_REDIRECTS) {
519
+ redirects = this.redirects = new Map();
520
+ this.open_wants = ALL_WANTS;
521
+ }
522
+ return redirects;
457
523
  }
458
524
 
459
525
  /** allocate a new synthetic node id. */
@@ -470,27 +536,17 @@ export class PluginDispatcher {
470
536
  * dispatch fused plugin handlers on node open.
471
537
  * creates a ViewCache, runs handlers, stores close callbacks.
472
538
  */
473
- dispatch_open(
474
- buf_idx: number,
475
- kind: NodeKind,
476
- buf: NodeBuffer,
477
- id_register: IdRegister,
478
- ): void {
479
- const cache = new ViewCache(
480
- buf,
481
- this.text_source,
482
- this.undo,
483
- buf_idx,
484
- );
539
+ dispatch_open(buf_idx: number, kind: NodeKind, buf: NodeBuffer): void {
540
+ const cache = this.views(buf);
485
541
  const view = cache.get(buf_idx)!;
486
542
 
487
543
  this.undo.set_active_node(buf_idx);
488
544
  const callbacks = dispatch_open(
489
545
  kind,
490
546
  view,
491
- this.ctx,
547
+ this.ctx ?? (this.ctx = {}),
492
548
  this.fused,
493
- this.has_handler,
549
+ this.has_handler
494
550
  );
495
551
  this.undo.clear_active_node();
496
552
 
@@ -502,13 +558,16 @@ export class PluginDispatcher {
502
558
  for (let i = 0; i < entries.length; i++) {
503
559
  const e = entries[i];
504
560
  if (e.kind === UndoEntryKind.WrapInner) {
505
- this.redirects.set(e.parent, e.wrapper);
561
+ this.own_redirects().set(e.parent, e.wrapper);
506
562
  }
507
563
  }
508
564
  }
509
565
 
510
566
  if (callbacks) {
511
- this.close_cbs.set(buf_idx, callbacks);
567
+ let close_cbs = this.close_cbs;
568
+ if (close_cbs === NO_CLOSE_CBS)
569
+ close_cbs = this.close_cbs = new CloseCallbackStore();
570
+ close_cbs.set(buf_idx, callbacks);
512
571
  }
513
572
 
514
573
  cache.clear();
@@ -522,28 +581,25 @@ export class PluginDispatcher {
522
581
  * speculation, inline emphasis).
523
582
  */
524
583
  dispatch_close(buf_idx: number, buf: NodeBuffer): void {
525
- this.redirects.delete(buf_idx);
584
+ if (this.redirects.size !== 0) this.redirects.delete(buf_idx);
526
585
 
527
586
  // take and fire close callbacks with undo attribution
528
587
  const cbs = this.close_cbs.take(buf_idx);
588
+ // callbacks cannot change the pending flag
589
+ const pending = buf.pending_at(buf_idx) !== 0;
529
590
  if (cbs) {
530
- const cache = new ViewCache(
531
- buf,
532
- this.text_source,
533
- this.undo,
534
- buf_idx,
535
- );
536
- this.undo.set_active_node(buf_idx);
591
+ // a node no longer pending commits right after its callbacks, so
592
+ // anything they recorded would be dropped unread
593
+ if (pending) this.undo.set_active_node(buf_idx);
537
594
  for (let i = 0; i < cbs.length; i++) {
538
595
  cbs[i]();
539
596
  }
540
597
  this.undo.clear_active_node();
541
- cache.clear();
542
598
  }
543
599
 
544
600
  // only commit if the node is no longer pending.
545
601
  // pending nodes can still be revoked after close.
546
- if (buf._pending_nodes[buf_idx] === 0) {
602
+ if (!pending) {
547
603
  this.undo.commit(buf_idx);
548
604
  }
549
605
  }
@@ -562,6 +618,13 @@ export class PluginDispatcher {
562
618
  * must be called BEFORE handle_repair().
563
619
  */
564
620
  dispatch_revoke(buf_idx: number, buf: NodeBuffer): void {
621
+ // nothing recorded, so nothing to undo
622
+ if (
623
+ this.redirects.size === 0 &&
624
+ this.close_cbs.live === 0 &&
625
+ this.undo.empty
626
+ )
627
+ return;
565
628
  this.redirects.delete(buf_idx);
566
629
  this.close_cbs.discard(buf_idx);
567
630
 
@@ -580,10 +643,10 @@ export class PluginDispatcher {
580
643
  this.undo.revoke(buf_idx, buf);
581
644
 
582
645
  // recurse into children to revoke their plugin state too
583
- let child = buf._children_starts[buf_idx];
584
- while (child !== NONE && buf._parents[child] === buf_idx) {
646
+ let child = buf.first_child_at(buf_idx);
647
+ while (child !== NONE && buf.parent_at(child) === buf_idx) {
585
648
  this.dispatch_revoke(child, buf);
586
- child = buf._next_siblings[child];
649
+ child = buf.next_at(child);
587
650
  }
588
651
  }
589
652
 
@@ -593,41 +656,87 @@ export class PluginDispatcher {
593
656
  */
594
657
  run_sequential(buf: NodeBuffer): void {
595
658
  for (const pass of this.sequential) {
659
+ const handlers = pass.handlers;
660
+
661
+ // reread after plugin code runs, growing the buffer moves it to new storage
662
+ let n = buf._n;
663
+ let idx = n[NodeField.first_child];
664
+ if (idx === NONE) continue;
665
+ // with no handled kind anywhere in the buffer, linked or not, the walk does nothing
666
+ const size = buf._size;
667
+ let handled = false;
668
+ for (
669
+ let b = 0, end = size * NodeField.stride;
670
+ b < end;
671
+ b += NodeField.stride
672
+ ) {
673
+ if (handlers[n[b] & 0xff] != null) {
674
+ handled = true;
675
+ break;
676
+ }
677
+ }
678
+ if (!handled) continue;
596
679
  const close_store = new CloseCallbackStore();
597
-
598
- walk_tree(buf, (idx, kind, is_close) => {
599
- if (is_close) {
600
- close_store.fire(idx);
601
- return;
680
+ const ctx = this.ctx ?? (this.ctx = {});
681
+ const stack: number[] = [];
682
+
683
+ while (true) {
684
+ const b = idx * NodeField.stride;
685
+ const handler = handlers[n[b] & 0xff];
686
+ if (handler != null) {
687
+ const cache = this.views(buf);
688
+ const view = cache.get(idx)!;
689
+ // the tree is complete so nothing revokes a sequential write, no
690
+ // active node means none is recorded
691
+ const callbacks = handler(view, ctx);
692
+ if (callbacks) close_store.set(idx, callbacks);
693
+ cache.clear();
694
+ n = buf._n;
602
695
  }
603
696
 
604
- const handler = pass.handlers[kind];
605
- if (handler === null) return;
606
-
607
- const cache = new ViewCache(
608
- buf,
609
- this.text_source,
610
- this.undo,
611
- idx,
612
- );
613
- const view = cache.get(idx)!;
697
+ const child = n[b + NodeField.first_child];
698
+ if (child !== NONE) {
699
+ stack.push(idx);
700
+ idx = child;
701
+ continue;
702
+ }
614
703
 
615
- this.undo.set_active_node(idx);
616
- const callbacks = handler(view, this.ctx);
617
- this.undo.clear_active_node();
704
+ if (close_store.live !== 0) {
705
+ close_store.fire(idx);
706
+ n = buf._n;
707
+ }
618
708
 
619
- if (callbacks) {
620
- close_store.set(idx, callbacks);
709
+ // read after the callbacks, which may relink the node
710
+ let next = n[b + NodeField.next];
711
+ let parent = n[b + NodeField.parent];
712
+ while (
713
+ (next === NONE ||
714
+ n[next * NodeField.stride + NodeField.parent] !== parent) &&
715
+ stack.length > 0
716
+ ) {
717
+ idx = stack.pop()!;
718
+ if (close_store.live !== 0) {
719
+ close_store.fire(idx);
720
+ n = buf._n;
721
+ }
722
+ const bi = idx * NodeField.stride;
723
+ next = n[bi + NodeField.next];
724
+ parent = n[bi + NodeField.parent];
621
725
  }
622
726
 
623
- cache.clear();
624
- });
727
+ if (
728
+ next === NONE ||
729
+ n[next * NodeField.stride + NodeField.parent] !== parent
730
+ )
731
+ break;
732
+ idx = next;
733
+ }
625
734
  }
626
735
  }
627
736
 
628
737
  /** reset all state. */
629
738
  reset(): void {
630
- this.undo.clear();
739
+ if (this.undo !== NO_UNDO) this.undo.clear();
631
740
  this.close_cbs.reset();
632
741
  this.redirects.clear();
633
742
  this.next_synthetic_id = SYNTHETIC_ID_BASE;