@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
@@ -3,6 +3,10 @@ import { parse_markdown_svelte, PFMParser } from '../src/main';
3
3
  import { NodeKind } from '../src/utils';
4
4
  import type { NodeBuffer } from '../src/utils';
5
5
  import { TreeBuilder } from '../src/tree_builder';
6
+ import { WireEmitter } from '../src/wire_emitter';
7
+ import { WireTreeBuilder } from '../src/wire_tree_builder';
8
+ import { Cursor } from '../src/cursor';
9
+ import { print_ast } from './print';
6
10
 
7
11
  function non_breaks(nodes: NodeBuffer, parent: number = 0) {
8
12
  return nodes
@@ -43,6 +47,22 @@ function parse_incremental(input: string, chunk_size: number = 1) {
43
47
  return tree.get_buffer();
44
48
  }
45
49
 
50
+ function parse_wire_incremental(input: string, chunk_size: number = 1) {
51
+ const emitter = new WireEmitter();
52
+ const parser = new PFMParser(emitter);
53
+ const builder = new WireTreeBuilder();
54
+ parser.init();
55
+ for (let i = 0; i < input.length; i += chunk_size) {
56
+ emitter.set_source(input.slice(0, i + chunk_size));
57
+ parser.feed(input.slice(i, i + chunk_size));
58
+ builder.apply(emitter.flush());
59
+ }
60
+ emitter.set_source(input);
61
+ parser.finish();
62
+ builder.apply(emitter.flush());
63
+ return builder.get_buffer();
64
+ }
65
+
46
66
  describe('Tables (GFM)', () => {
47
67
  test('basic table with header and data rows', () => {
48
68
  const input = '| foo | bar |\n| --- | --- |\n| baz | bim |\n';
@@ -147,6 +167,33 @@ describe('Tables (GFM)', () => {
147
167
  expect(cell_text(nodes, row_cells[1].index, input)).toBe('2');
148
168
  });
149
169
 
170
+ test('cells past the header columns leave nothing in the table', () => {
171
+ const input =
172
+ '| a | b |\n|---|---|\n| x | y | z |\n| 1 | 2 | *e* [l](u) `c` \\| w |\n| p | q | \n| m | n | o';
173
+ const parses = [
174
+ parse_markdown_svelte(input).nodes,
175
+ parse_incremental(input, 1),
176
+ parse_incremental(input, 3),
177
+ parse_wire_incremental(input, 1),
178
+ parse_wire_incremental(input, 4),
179
+ ];
180
+ for (const nodes of parses) {
181
+ const table = non_breaks(nodes)[0];
182
+ const kinds = get_children(nodes, table.index).map((n) => n.kind);
183
+ expect(kinds).toEqual([
184
+ 'table_header',
185
+ 'table_row',
186
+ 'table_row',
187
+ 'table_row',
188
+ 'table_row',
189
+ ]);
190
+ for (const row of get_children(nodes, table.index).slice(1)) {
191
+ const cells = get_children(nodes, row.index);
192
+ expect(cells.map((c) => c.kind)).toEqual(['table_cell', 'table_cell']);
193
+ }
194
+ }
195
+ });
196
+
150
197
  test('header/delimiter count mismatch is not a table', () => {
151
198
  const input = '| a | b |\n| - |\n';
152
199
  const { nodes } = parse_markdown_svelte(input);
@@ -661,3 +708,303 @@ describe('Tables (GFM)', () => {
661
708
  expect(cell_text(nodes, row_cells[1].index, input)).toBe('bim');
662
709
  });
663
710
  });
711
+
712
+ /** read by cursor so wire strings and source slices compare alike */
713
+ function data_cell_texts(nodes: NodeBuffer, source: string): string[] {
714
+ const cursor = new Cursor(nodes, source);
715
+ const texts: string[] = [];
716
+ const table = non_breaks(nodes)[0];
717
+ for (const row of get_children(nodes, table.index)) {
718
+ if (row.kind !== 'table_row') continue;
719
+ for (const cell of get_children(nodes, row.index)) {
720
+ let text = '';
721
+ for (const child of cell.children) {
722
+ cursor.move_to(child);
723
+ if (cursor.kind === NodeKind.text) text += cursor.text();
724
+ }
725
+ texts.push(text);
726
+ }
727
+ }
728
+ return texts;
729
+ }
730
+
731
+ /** one line per row, each cell as its column and text plus +cN or +rN spans, a wire tree holds its own text */
732
+ function grid(nodes: NodeBuffer, source: string | null): string[] {
733
+ const table = non_breaks(nodes).find((n) => n.kind === 'table');
734
+ if (!table) return [];
735
+ return get_children(nodes, table.index).map((row) => {
736
+ const cells = get_children(nodes, row.index).map((cell) => {
737
+ const text = get_children(nodes, cell.index)
738
+ .filter((n) => n.kind === 'text')
739
+ .map((n) =>
740
+ source === null
741
+ ? nodes._strings[n.index]
742
+ : source.slice(n.value[0], n.value[1])
743
+ )
744
+ .join('');
745
+ let s = `${nodes.extra_at(cell.index)}:${text}`;
746
+ if (cell.metadata?.colspan) s += `+c${cell.metadata.colspan}`;
747
+ if (cell.metadata?.rowspan) s += `+r${cell.metadata.rowspan}`;
748
+ return s;
749
+ });
750
+ return (row.kind === 'table_header' ? 'head ' : 'row ') + cells.join(' ');
751
+ });
752
+ }
753
+
754
+ function table_meta(nodes: NodeBuffer) {
755
+ return non_breaks(nodes).find((n) => n.kind === 'table')?.metadata;
756
+ }
757
+
758
+ function parse_over_wire(source: string, chunk: number): NodeBuffer {
759
+ const emitter = new WireEmitter();
760
+ const parser = new PFMParser(emitter);
761
+ const builder = new WireTreeBuilder();
762
+ parser.init();
763
+ for (let i = 0; i < source.length; i += chunk) {
764
+ emitter.set_source(source.slice(0, i + chunk));
765
+ parser.feed(source.slice(i, i + chunk));
766
+ builder.apply(emitter.flush());
767
+ }
768
+ emitter.set_source(source);
769
+ parser.finish();
770
+ builder.apply(emitter.flush());
771
+ return builder.get_buffer();
772
+ }
773
+
774
+ /** the grid and table metadata of a batch parse, checked against every chunked and wire parse */
775
+ function extended(input: string) {
776
+ const nodes = parse_markdown_svelte(input).nodes;
777
+ const g = grid(nodes, input);
778
+ const meta = table_meta(nodes);
779
+ for (let chunk = 1; chunk <= 7; chunk++) {
780
+ const inc = parse_incremental(input, chunk);
781
+ expect(grid(inc, input), `chunk ${chunk}`).toEqual(g);
782
+ expect(table_meta(inc), `chunk ${chunk}`).toEqual(meta);
783
+ const wire = parse_over_wire(input, chunk);
784
+ expect(grid(wire, null), `wire chunk ${chunk}`).toEqual(g);
785
+ expect(table_meta(wire), `wire chunk ${chunk}`).toEqual(meta);
786
+ }
787
+ return { grid: g, meta };
788
+ }
789
+
790
+ describe('Tables (header columns)', () => {
791
+ test('a || marks left header columns', () => {
792
+ const { grid, meta } = extended(
793
+ '| maybe || title | title 2 |\n|-------||-------|---------|\n| hello || text | text 2 |\n'
794
+ );
795
+ expect(meta?.col_count).toBe(3);
796
+ expect(meta?.header_columns).toEqual([1, 0]);
797
+ expect(meta?.spans).toBeUndefined();
798
+ expect(grid).toEqual([
799
+ 'head 0:maybe 1:title 2:title 2',
800
+ 'row 0:hello 1:text 2:text 2',
801
+ ]);
802
+ });
803
+
804
+ test('a || nearer the right marks right header columns', () => {
805
+ const { grid, meta } = extended(
806
+ '| title | title 2 || |\n|-------|---------||--------|\n| text | text 2 || head 1 |\n'
807
+ );
808
+ expect(meta?.header_columns).toEqual([0, 1]);
809
+ expect(grid).toEqual([
810
+ 'head 0:title 1:title 2 2:',
811
+ 'row 0:text 1:text 2 2:head 1',
812
+ ]);
813
+ });
814
+
815
+ test('one || between equal sides heads the left', () => {
816
+ const { meta } = extended('| a | b || c | d |\n|---|---||---|---|\n');
817
+ expect(meta?.header_columns).toEqual([2, 0]);
818
+ });
819
+
820
+ test('two || mark both sides', () => {
821
+ const { grid, meta } = extended(
822
+ '| a || b || c |\n|---||---||---|\n| x || y || z |\n'
823
+ );
824
+ expect(meta?.header_columns).toEqual([1, 1]);
825
+ expect(grid).toEqual(['head 0:a 1:b 2:c', 'row 0:x 1:y 2:z']);
826
+ });
827
+
828
+ test('a body row may use a single pipe at the boundary', () => {
829
+ const { grid } = extended('| a || b |\n|---||---|\n| x | y |\n');
830
+ expect(grid).toEqual(['head 0:a 1:b', 'row 0:x 1:y']);
831
+ });
832
+
833
+ test('a || away from the boundary is an empty cell', () => {
834
+ const { grid } = extended(
835
+ '| a || b | c | d |\n|---||---|---|---|\n| x || y || z |\n'
836
+ );
837
+ expect(grid).toEqual(['head 0:a 1:b 2:c 3:d', 'row 0:x 1:y 2: 3:z']);
838
+ });
839
+
840
+ test('a || only in the header row is an empty cell', () => {
841
+ const { grid, meta } = extended('| a || b |\n|---|---|---|\n| x | | y |\n');
842
+ expect(meta?.header_columns).toBeUndefined();
843
+ expect(grid).toEqual(['head 0:a 1: 2:b', 'row 0:x 1: 2:y']);
844
+ });
845
+
846
+ test('a || only in the delimiter row is not a table', () => {
847
+ const { nodes } = parse_markdown_svelte('| a | b |\n|---||---|\n');
848
+ expect(non_breaks(nodes)[0].kind).toBe('paragraph');
849
+ });
850
+ });
851
+
852
+ describe('Tables (merged cells)', () => {
853
+ test('lone > cells widen the header cell before them', () => {
854
+ const { grid, meta } = extended(
855
+ '| title |> |> |\n|-------|---|---|\n| text | b | c |\n'
856
+ );
857
+ expect(meta?.spans).toBe(true);
858
+ expect(grid).toEqual(['head 0:title+c3', 'row 0:text 1:b 2:c']);
859
+ });
860
+
861
+ test('a > with spaces around it merges in a body row', () => {
862
+ const { grid } = extended('| a | b | c |\n|---|---|---|\n| x | > | y |\n');
863
+ expect(grid).toEqual(['head 0:a 1:b 2:c', 'row 0:x+c2 2:y']);
864
+ });
865
+
866
+ test('lone ^ cells extend the cell above', () => {
867
+ const { grid } = extended(
868
+ '| title | B | C |\n|-------|---|---|\n| text | b | c |\n|^ | b |^ |\n'
869
+ );
870
+ expect(grid).toEqual([
871
+ 'head 0:title 1:B 2:C',
872
+ 'row 0:text+r2 1:b 2:c+r2',
873
+ 'row 1:b',
874
+ ]);
875
+ });
876
+
877
+ test('a ^ chain spans three rows', () => {
878
+ const { grid } = extended('| a |\n|---|\n| x |\n| ^ |\n| ^ |\n| y |\n');
879
+ // a row every column of which merged up keeps its tr
880
+ expect(grid).toEqual([
881
+ 'head 0:a',
882
+ 'row 0:x+r3',
883
+ 'row ',
884
+ 'row ',
885
+ 'row 0:y',
886
+ ]);
887
+ });
888
+
889
+ test('> and ^ together merge a rectangle', () => {
890
+ const { grid } = extended(
891
+ '| a | b | c |\n|---|---|---|\n| x |> | y |\n|^ |^ | z |\n'
892
+ );
893
+ expect(grid).toEqual([
894
+ 'head 0:a 1:b 2:c',
895
+ 'row 0:x+c2+r2 2:y',
896
+ 'row 2:z',
897
+ ]);
898
+ });
899
+
900
+ test('merges compose with header columns', () => {
901
+ const { grid, meta } = extended(
902
+ '| || spanning |> |\n|-----------||-----------|---|\n| left head || text | b |\n|^ || more |> |\n'
903
+ );
904
+ expect(meta?.header_columns).toEqual([1, 0]);
905
+ expect(grid).toEqual([
906
+ 'head 0: 1:spanning+c2',
907
+ 'row 0:left head+r2 1:text 2:b',
908
+ 'row 1:more+c2',
909
+ ]);
910
+ });
911
+
912
+ test('a marker as the last cell before a linefeed or the end merges', () => {
913
+ const { grid } = extended('| a | b |\n|---|---|\n| x |>');
914
+ expect(grid).toEqual(['head 0:a 1:b', 'row 0:x+c2']);
915
+ });
916
+
917
+ test('a > in the first column stays text', () => {
918
+ const { grid } = extended('| a | b |\n|---|---|\n| > | y |\n');
919
+ expect(grid).toEqual(['head 0:a 1:b', 'row 0:> 1:y']);
920
+ });
921
+
922
+ test('a > never crosses a header column boundary', () => {
923
+ const { grid } = extended('| a || b |>|\n|---||---|---|\n| x || > | y |\n');
924
+ expect(grid).toEqual(['head 0:a 1:b+c2', 'row 0:x 1:> 2:y']);
925
+ });
926
+
927
+ test('a ^ under the header row stays text', () => {
928
+ const { grid } = extended('| a | b |\n|---|---|\n| ^ | y |\n');
929
+ expect(grid).toEqual(['head 0:a 1:b', 'row 0:^ 1:y']);
930
+ });
931
+
932
+ test('a ^ in the header row stays text', () => {
933
+ const { grid } = extended('| a | ^ |\n|---|---|\n');
934
+ expect(grid).toEqual(['head 0:a 1:^']);
935
+ });
936
+
937
+ test('a cell with more than the marker stays text', () => {
938
+ const { grid } = extended(
939
+ '| a | b | c |\n|---|---|---|\n| x | > quote | >> |\n'
940
+ );
941
+ expect(grid[1]).toBe('row 0:x 1:> quote 2:>>');
942
+ });
943
+
944
+ test('an escaped marker stays text', () => {
945
+ const { grid } = extended(
946
+ '| a | b |\n|---|---|\n| x | \\> |\n| \\^ | y |\n'
947
+ );
948
+ expect(grid).toEqual(['head 0:a 1:b', 'row 0:x 1:>', 'row 0:^ 1:y']);
949
+ });
950
+
951
+ test('a ^ under part of a wide cell stays text', () => {
952
+ const { grid } = extended(
953
+ '| a | b |\n|---|---|\n| x |> |\n|^ | q |\n| y | ^ |\n'
954
+ );
955
+ expect(grid).toEqual([
956
+ 'head 0:a 1:b',
957
+ 'row 0:x+c2',
958
+ 'row 0:^ 1:q+r2',
959
+ 'row 0:y',
960
+ ]);
961
+ });
962
+
963
+ test('a > after a ^ stays text', () => {
964
+ const { grid } = extended('| a | b |\n|---|---|\n| x | y |\n|^ |> |\n');
965
+ expect(grid).toEqual(['head 0:a 1:b', 'row 0:x+r2 1:y', 'row 1:>']);
966
+ });
967
+ });
968
+
969
+ describe('unclosed delimiters revoked at a cell boundary', () => {
970
+ test('`^2` stays literal without the trailing space', () => {
971
+ const input = '| a |\n|---|\n| ^2 |\n';
972
+ expect(print_ast(parse_markdown_svelte(input).nodes, input)).toBe(
973
+ [
974
+ 'root',
975
+ ' table alignments=["none"] col_count=1',
976
+ ' table_header',
977
+ ' table_cell',
978
+ ' text "a"',
979
+ ' table_row',
980
+ ' table_cell',
981
+ ' text "^2"',
982
+ ].join('\n')
983
+ );
984
+ });
985
+
986
+ const cases: [string, string][] = [
987
+ ['| a |\n|---|\n| ^2 |\n', '^2'],
988
+ ['| a |\n|---|\n| ~2 |\n', '~2'],
989
+ ['| a |\n|---|\n| *2 |\n', '*2'],
990
+ ['| a |\n|---|\n| [2 |\n', '[2'],
991
+ ['| a |\n|---|\n| x ^2 y |\n', 'x ^2 y'],
992
+ ['| a |\n|---|\n| `2 |\n', '`2'],
993
+ ['| a |\n|---|\n| x ` |\n', 'x `'],
994
+ ['| a | b |\n|---|---|\n| ` x | y |\n', '` x|y'],
995
+ ['| a |\n|---|\n| x ` \nfoo\n', 'x `|foo'],
996
+ ['| a | b |\n|---|---|\n| ^2 | ~3\t|\n', '^2|~3'],
997
+ ];
998
+
999
+ test.each(cases)('%j: batch, incremental and wire agree', (input, cells) => {
1000
+ const expected = cells.split('|');
1001
+ const batch = parse_markdown_svelte(input).nodes;
1002
+ const batch_ast = print_ast(batch, input);
1003
+ expect(data_cell_texts(batch, input)).toEqual(expected);
1004
+ for (const size of [1, 2, 3, 5]) {
1005
+ const inc = parse_incremental(input, size);
1006
+ expect(print_ast(inc, input)).toBe(batch_ast);
1007
+ }
1008
+ expect(data_cell_texts(parse_over_wire(input, 1), input)).toEqual(expected);
1009
+ });
1010
+ });
@@ -1,6 +1,8 @@
1
1
  import { describe, it, expect } from 'vitest';
2
+ import { PFMParser, parse_markdown_svelte } from '../src/main';
3
+ import type { Emitter } from '../src/opcodes';
2
4
  import { TreeBuilder } from '../src/tree_builder';
3
- import { NodeKind } from '../src/utils';
5
+ import { NodeBuffer, NodeKind } from '../src/utils';
4
6
 
5
7
  describe('TreeBuilder', () => {
6
8
  describe('basic node creation', () => {
@@ -340,3 +342,286 @@ describe('TreeBuilder', () => {
340
342
  });
341
343
  });
342
344
  });
345
+
346
+ // no non-root id lands on its own slot, so the builder has to map them
347
+ class ShiftedIds implements Emitter {
348
+ constructor(
349
+ private out: Emitter,
350
+ private offset: number
351
+ ) {}
352
+ private shift(id: number): number {
353
+ return id <= 0 ? id : id + this.offset;
354
+ }
355
+ open(
356
+ id: number,
357
+ kind: NodeKind,
358
+ start: number,
359
+ parent: number,
360
+ extra: number,
361
+ pending: boolean
362
+ ): void {
363
+ this.out.open(
364
+ this.shift(id),
365
+ kind,
366
+ start,
367
+ this.shift(parent),
368
+ extra,
369
+ pending
370
+ );
371
+ }
372
+ close(id: number, end: number): void {
373
+ this.out.close(this.shift(id), end);
374
+ }
375
+ text(parent: number, start: number, end: number): void {
376
+ this.out.text(this.shift(parent), start, end);
377
+ }
378
+ attr(id: number, key: string, value: any): void {
379
+ this.out.attr(this.shift(id), key, value);
380
+ }
381
+ set_value_start(id: number, pos: number): void {
382
+ this.out.set_value_start(this.shift(id), pos);
383
+ }
384
+ set_value_end(id: number, pos: number): void {
385
+ this.out.set_value_end(this.shift(id), pos);
386
+ }
387
+ revoke(id: number, source_text?: string): void {
388
+ this.out.revoke(this.shift(id), source_text);
389
+ }
390
+ commit(id: number): void {
391
+ this.out.commit(this.shift(id));
392
+ }
393
+ cursor(pos: number): void {
394
+ this.out.cursor(pos);
395
+ }
396
+ }
397
+
398
+ function dump(nodes: NodeBuffer, index = 0): unknown {
399
+ const node = nodes.get_node(index);
400
+ return {
401
+ kind: node.kind,
402
+ start: node.start,
403
+ end: node.end,
404
+ value: node.value,
405
+ metadata: node.metadata,
406
+ pending: nodes.pending_at(index),
407
+ children: node.children.map((child) => dump(nodes, child)),
408
+ };
409
+ }
410
+
411
+ describe('TreeBuilder ids as indices', () => {
412
+ const docs = [
413
+ '| a | b |\n| - | - |\n| x | |\n\nafter *the* table\n',
414
+ '<div>\nunclosed html block\n\n- item *one*\n- item two\n\npara `code` end\n',
415
+ '- tight\n- list\n\n1. loose\n\n2. list\n\n> quote **strong\n',
416
+ 'text with snake_case and *unclosed emphasis\n\n| h |\n| - |\n| c |\n',
417
+ '<span>inline <b>open\n\n# heading {x}\n\n```js\ncode\n```\n',
418
+ ];
419
+
420
+ for (const doc of docs) {
421
+ it(`builds the same tree with ids as slots and with a map: ${JSON.stringify(doc)}`, () => {
422
+ const direct = new TreeBuilder(8);
423
+ new PFMParser(direct).parse(doc);
424
+ const mapped = new TreeBuilder(8);
425
+ new PFMParser(new ShiftedIds(mapped, 1000)).parse(doc);
426
+ expect(dump(direct.get_buffer())).toEqual(dump(mapped.get_buffer()));
427
+ });
428
+ }
429
+
430
+ it('keeps ids equal to slots, so the builder never needs its id map', () => {
431
+ for (const doc of docs) {
432
+ const tb = new TreeBuilder(8);
433
+ new PFMParser(tb).parse(doc);
434
+ expect((tb as unknown as { id_to_index: unknown }).id_to_index).toBe(
435
+ null
436
+ );
437
+ }
438
+ });
439
+ });
440
+
441
+ describe('parser id tables', () => {
442
+ const docs = [
443
+ '- tight\n- list\n\n1. loose\n\n2. list\n\n> quote **strong\n',
444
+ '<div>\nunclosed html block\n\n- item *one*\n- item two\n\npara `code` end\n',
445
+ 'text *a* _b_ ~~c~~ [link](x) ![img](y) <span>open\n\n| h | i |\n| - | - |\n| c | d |\n'.repeat(
446
+ 40
447
+ ),
448
+ ];
449
+
450
+ function parse_whole(doc: string): unknown {
451
+ const tb = new TreeBuilder(8);
452
+ new PFMParser(tb).parse(doc);
453
+ return dump(tb.get_buffer());
454
+ }
455
+
456
+ it('keeps interleaved incremental parsers apart', () => {
457
+ const expected = docs.map(parse_whole);
458
+ const builders = docs.map(() => new TreeBuilder(8));
459
+ const parsers = builders.map((tb) => new PFMParser(tb));
460
+ for (const p of parsers) p.init();
461
+ const longest = Math.max(...docs.map((d) => d.length));
462
+ for (let at = 0; at < longest; at += 7) {
463
+ for (let i = 0; i < docs.length; i++) {
464
+ const chunk = docs[i].slice(at, at + 7);
465
+ if (chunk.length > 0) parsers[i].feed(chunk);
466
+ }
467
+ }
468
+ for (const p of parsers) p.finish();
469
+ builders.forEach((tb, i) =>
470
+ expect(dump(tb.get_buffer())).toEqual(expected[i])
471
+ );
472
+ });
473
+
474
+ it('gives the same trees when one parser is reused across documents', () => {
475
+ const expected = docs.map(parse_whole);
476
+ const tb = new TreeBuilder(8);
477
+ const parser = new PFMParser(tb);
478
+ for (const order of [
479
+ [2, 0, 1],
480
+ [0, 2, 1, 0],
481
+ ]) {
482
+ for (const i of order) {
483
+ tb.reset();
484
+ parser.parse(docs[i]);
485
+ expect(dump(tb.get_buffer())).toEqual(expected[i]);
486
+ }
487
+ }
488
+ });
489
+
490
+ it('gives the same trees from the parser parse_markdown_svelte shares', () => {
491
+ const expected = docs.map(parse_whole);
492
+ for (const i of [2, 0, 1, 1, 2, 0]) {
493
+ expect(dump(parse_markdown_svelte(docs[i]).nodes)).toEqual(expected[i]);
494
+ }
495
+ const tabs = '\t- a\n\t\t- b\n\tc\n';
496
+ const wide = new TreeBuilder(8);
497
+ new PFMParser(wide, 4).parse(tabs);
498
+ expect(dump(parse_markdown_svelte(tabs, { tab_size: 4 }).nodes)).toEqual(
499
+ dump(wide.get_buffer())
500
+ );
501
+ expect(dump(parse_markdown_svelte(tabs).nodes)).toEqual(parse_whole(tabs));
502
+ });
503
+
504
+ it('recovers the shared parser from a throw and a reentrant parse', () => {
505
+ const expected = docs.map(parse_whole);
506
+ const boom = {
507
+ paragraph: {
508
+ parse() {
509
+ throw new Error('boom');
510
+ },
511
+ },
512
+ };
513
+ expect(() => parse_markdown_svelte(docs[1], { plugins: [boom] })).toThrow(
514
+ 'boom'
515
+ );
516
+ expect(dump(parse_markdown_svelte(docs[0]).nodes)).toEqual(expected[0]);
517
+
518
+ const inner: unknown[] = [];
519
+ const reenter = {
520
+ paragraph: {
521
+ parse() {
522
+ inner.push(dump(parse_markdown_svelte(docs[2]).nodes));
523
+ },
524
+ },
525
+ };
526
+ const outer = parse_markdown_svelte(docs[0], { plugins: [reenter] });
527
+ expect(inner.length).toBeGreaterThan(0);
528
+ for (const tree of inner) expect(tree).toEqual(expected[2]);
529
+ expect(dump(outer.nodes)).toEqual(expected[0]);
530
+ expect(dump(parse_markdown_svelte(docs[1]).nodes)).toEqual(expected[1]);
531
+ });
532
+
533
+ class Recorder implements Emitter {
534
+ ops: unknown[][] = [];
535
+ open(...args: unknown[]): void {
536
+ this.ops.push(['open', ...args]);
537
+ }
538
+ close(...args: unknown[]): void {
539
+ this.ops.push(['close', ...args]);
540
+ }
541
+ text(...args: unknown[]): void {
542
+ this.ops.push(['text', ...args]);
543
+ }
544
+ attr(...args: unknown[]): void {
545
+ this.ops.push(['attr', ...args]);
546
+ }
547
+ set_value_start(...args: unknown[]): void {
548
+ this.ops.push(['set_value_start', ...args]);
549
+ }
550
+ set_value_end(...args: unknown[]): void {
551
+ this.ops.push(['set_value_end', ...args]);
552
+ }
553
+ revoke(...args: unknown[]): void {
554
+ this.ops.push(['revoke', ...args]);
555
+ }
556
+ commit(...args: unknown[]): void {
557
+ this.ops.push(['commit', ...args]);
558
+ }
559
+ cursor(...args: unknown[]): void {
560
+ this.ops.push(['cursor', ...args]);
561
+ }
562
+ }
563
+
564
+ interface PendingSlots {
565
+ pending_count: number;
566
+ emit_open(
567
+ kind: NodeKind,
568
+ start: number,
569
+ parent: number,
570
+ extra: number,
571
+ pending: boolean
572
+ ): number;
573
+ pending_has(id: number): boolean;
574
+ pending_remove(id: number): void;
575
+ }
576
+
577
+ it('tracks pending ids past slot 2^23', () => {
578
+ const p = new PFMParser(new Recorder()) as unknown as PendingSlots & {
579
+ init(): void;
580
+ };
581
+ p.init();
582
+ // stands in for 2^23 pending nodes, past which the slot no longer fits beside the kind in one int32
583
+ p.pending_count = 1 << 23;
584
+ const a = p.emit_open(NodeKind.link, 0, 0, 0, true);
585
+ const b = p.emit_open(NodeKind.strong_emphasis, 0, 0, 0, true);
586
+ expect(p.pending_has(a)).toBe(true);
587
+ expect(p.pending_has(b)).toBe(true);
588
+ p.pending_remove(a);
589
+ expect(p.pending_count).toBe((1 << 23) + 1);
590
+ expect(p.pending_has(a)).toBe(false);
591
+ expect(p.pending_has(b)).toBe(true);
592
+ p.pending_remove(b);
593
+ expect(p.pending_count).toBe(1 << 23);
594
+ expect(p.pending_has(b)).toBe(false);
595
+ });
596
+
597
+ it('keeps a finished parser off the id tables it handed back', () => {
598
+ const later = 'para *e* [x\n\n> more **s**';
599
+ const clean = new Recorder();
600
+ const pc = new PFMParser(clean);
601
+ pc.init();
602
+ pc.feed(later);
603
+ pc.finish();
604
+
605
+ // drains spare tables left by earlier tests so pa allocates its own
606
+ new PFMParser(new Recorder()).init();
607
+ const first = new Recorder();
608
+ const pa = new PFMParser(first);
609
+ pa.parse('# hi *x\n\ntext **b** [y\n');
610
+ const seen = first.ops.length;
611
+
612
+ const second = new Recorder();
613
+ const pb = new PFMParser(second);
614
+ pb.init();
615
+ pb.feed(later);
616
+
617
+ // out of contract, pa is not initialized again
618
+ pa.finish();
619
+ const repeat = first.ops.slice(seen).filter((op) => op[0] !== 'cursor');
620
+ pa.feed('\n\nmore *z* [w\n');
621
+ pa.finish();
622
+
623
+ pb.finish();
624
+ expect(second.ops).toEqual(clean.ops);
625
+ expect(repeat).toEqual([]);
626
+ });
627
+ });