@react-x11/components 0.9.0 → 0.10.0

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 (550) hide show
  1. package/README.md +10 -4
  2. package/dist/charts/node.d.ts +11 -1
  3. package/dist/charts/node.d.ts.map +1 -1
  4. package/dist/charts/node.js +44 -14
  5. package/dist/charts/node.js.map +1 -1
  6. package/dist/charts/render.d.ts +12 -3
  7. package/dist/charts/render.d.ts.map +1 -1
  8. package/dist/charts/render.js +94 -25
  9. package/dist/charts/render.js.map +1 -1
  10. package/dist/code/index.d.ts.map +1 -1
  11. package/dist/code/index.js +3 -1
  12. package/dist/code/index.js.map +1 -1
  13. package/dist/code-editor/completion.d.ts.map +1 -1
  14. package/dist/code-editor/completion.js +3 -2
  15. package/dist/code-editor/completion.js.map +1 -1
  16. package/dist/code-editor/index.d.ts.map +1 -1
  17. package/dist/code-editor/index.js +19 -0
  18. package/dist/code-editor/index.js.map +1 -1
  19. package/dist/code-editor/node.d.ts +177 -7
  20. package/dist/code-editor/node.d.ts.map +1 -1
  21. package/dist/code-editor/node.js +1329 -164
  22. package/dist/code-editor/node.js.map +1 -1
  23. package/dist/code-language/languages/glsl.d.ts.map +1 -1
  24. package/dist/code-language/languages/glsl.js +10 -2
  25. package/dist/code-language/languages/glsl.js.map +1 -1
  26. package/dist/code-language/languages/javascript.d.ts.map +1 -1
  27. package/dist/code-language/languages/javascript.js +18 -3
  28. package/dist/code-language/languages/javascript.js.map +1 -1
  29. package/dist/code-language/languages/json.d.ts.map +1 -1
  30. package/dist/code-language/languages/json.js +10 -2
  31. package/dist/code-language/languages/json.js.map +1 -1
  32. package/dist/code-language/languages/shell.d.ts.map +1 -1
  33. package/dist/code-language/languages/shell.js +10 -2
  34. package/dist/code-language/languages/shell.js.map +1 -1
  35. package/dist/code-language/languages/sql.d.ts.map +1 -1
  36. package/dist/code-language/languages/sql.js +20 -2
  37. package/dist/code-language/languages/sql.js.map +1 -1
  38. package/dist/code-language/stream.d.ts +22 -0
  39. package/dist/code-language/stream.d.ts.map +1 -1
  40. package/dist/code-language/stream.js +259 -9
  41. package/dist/code-language/stream.js.map +1 -1
  42. package/dist/code-language/types.d.ts +5 -4
  43. package/dist/code-language/types.d.ts.map +1 -1
  44. package/dist/codeblock/index.d.ts +13 -2
  45. package/dist/codeblock/index.d.ts.map +1 -1
  46. package/dist/codeblock/index.js +20 -3
  47. package/dist/codeblock/index.js.map +1 -1
  48. package/dist/flow/draw.d.ts +50 -0
  49. package/dist/flow/draw.d.ts.map +1 -1
  50. package/dist/flow/draw.js +132 -33
  51. package/dist/flow/draw.js.map +1 -1
  52. package/dist/flow/gl/color.d.ts +13 -0
  53. package/dist/flow/gl/color.d.ts.map +1 -0
  54. package/dist/flow/gl/color.js +82 -0
  55. package/dist/flow/gl/color.js.map +1 -0
  56. package/dist/flow/gl/index.d.ts +60 -0
  57. package/dist/flow/gl/index.d.ts.map +1 -0
  58. package/dist/flow/gl/index.js +283 -0
  59. package/dist/flow/gl/index.js.map +1 -0
  60. package/dist/flow/gl/pack.d.ts +133 -0
  61. package/dist/flow/gl/pack.d.ts.map +1 -0
  62. package/dist/flow/gl/pack.js +408 -0
  63. package/dist/flow/gl/pack.js.map +1 -0
  64. package/dist/flow/gl/renderer.d.ts +95 -0
  65. package/dist/flow/gl/renderer.d.ts.map +1 -0
  66. package/dist/flow/gl/renderer.js +459 -0
  67. package/dist/flow/gl/renderer.js.map +1 -0
  68. package/dist/flow/gl/shaders.d.ts +79 -0
  69. package/dist/flow/gl/shaders.d.ts.map +1 -0
  70. package/dist/flow/gl/shaders.js +346 -0
  71. package/dist/flow/gl/shaders.js.map +1 -0
  72. package/dist/flow/gl/text.d.ts +318 -0
  73. package/dist/flow/gl/text.d.ts.map +1 -0
  74. package/dist/flow/gl/text.js +1103 -0
  75. package/dist/flow/gl/text.js.map +1 -0
  76. package/dist/flow/index.d.ts +1 -1
  77. package/dist/flow/index.d.ts.map +1 -1
  78. package/dist/flow/index.js +619 -64
  79. package/dist/flow/index.js.map +1 -1
  80. package/dist/flow/node.d.ts +621 -72
  81. package/dist/flow/node.d.ts.map +1 -1
  82. package/dist/flow/node.js +2253 -1018
  83. package/dist/flow/node.js.map +1 -1
  84. package/dist/flow/paint.d.ts +35 -0
  85. package/dist/flow/paint.d.ts.map +1 -0
  86. package/dist/flow/paint.js +393 -0
  87. package/dist/flow/paint.js.map +1 -0
  88. package/dist/flow/paths.d.ts +14 -0
  89. package/dist/flow/paths.d.ts.map +1 -1
  90. package/dist/flow/paths.js +45 -19
  91. package/dist/flow/paths.js.map +1 -1
  92. package/dist/flow/scene.d.ts +584 -0
  93. package/dist/flow/scene.d.ts.map +1 -0
  94. package/dist/flow/scene.js +1453 -0
  95. package/dist/flow/scene.js.map +1 -0
  96. package/dist/flow/types.d.ts +111 -1
  97. package/dist/flow/types.d.ts.map +1 -1
  98. package/dist/html/controls.d.ts +28 -0
  99. package/dist/html/controls.d.ts.map +1 -1
  100. package/dist/html/controls.js +52 -2
  101. package/dist/html/controls.js.map +1 -1
  102. package/dist/html/css/calc.d.ts +14 -0
  103. package/dist/html/css/calc.d.ts.map +1 -0
  104. package/dist/html/css/calc.js +344 -0
  105. package/dist/html/css/calc.js.map +1 -0
  106. package/dist/html/css/cascade.d.ts +240 -6
  107. package/dist/html/css/cascade.d.ts.map +1 -1
  108. package/dist/html/css/cascade.js +1327 -66
  109. package/dist/html/css/cascade.js.map +1 -1
  110. package/dist/html/css/color.d.ts +23 -0
  111. package/dist/html/css/color.d.ts.map +1 -0
  112. package/dist/html/css/color.js +740 -0
  113. package/dist/html/css/color.js.map +1 -0
  114. package/dist/html/css/content.d.ts +92 -0
  115. package/dist/html/css/content.d.ts.map +1 -0
  116. package/dist/html/css/content.js +388 -0
  117. package/dist/html/css/content.js.map +1 -0
  118. package/dist/html/css/counter-styles.d.ts +76 -0
  119. package/dist/html/css/counter-styles.d.ts.map +1 -0
  120. package/dist/html/css/counter-styles.js +893 -0
  121. package/dist/html/css/counter-styles.js.map +1 -0
  122. package/dist/html/css/decode.d.ts +12 -0
  123. package/dist/html/css/decode.d.ts.map +1 -0
  124. package/dist/html/css/decode.js +116 -0
  125. package/dist/html/css/decode.js.map +1 -0
  126. package/dist/html/css/parse.d.ts +110 -10
  127. package/dist/html/css/parse.d.ts.map +1 -1
  128. package/dist/html/css/parse.js +1473 -180
  129. package/dist/html/css/parse.js.map +1 -1
  130. package/dist/html/css/style.d.ts +498 -15
  131. package/dist/html/css/style.d.ts.map +1 -1
  132. package/dist/html/css/style.js +3698 -206
  133. package/dist/html/css/style.js.map +1 -1
  134. package/dist/html/css/ua.d.ts.map +1 -1
  135. package/dist/html/css/ua.js +57 -11
  136. package/dist/html/css/ua.js.map +1 -1
  137. package/dist/html/css/values.d.ts +37 -3
  138. package/dist/html/css/values.d.ts.map +1 -1
  139. package/dist/html/css/values.js +122 -48
  140. package/dist/html/css/values.js.map +1 -1
  141. package/dist/html/css/vars.d.ts +52 -0
  142. package/dist/html/css/vars.d.ts.map +1 -0
  143. package/dist/html/css/vars.js +358 -0
  144. package/dist/html/css/vars.js.map +1 -0
  145. package/dist/html/dom.d.ts +30 -4
  146. package/dist/html/dom.d.ts.map +1 -1
  147. package/dist/html/dom.js +278 -17
  148. package/dist/html/dom.js.map +1 -1
  149. package/dist/html/fonts.d.ts +106 -0
  150. package/dist/html/fonts.d.ts.map +1 -0
  151. package/dist/html/fonts.js +565 -0
  152. package/dist/html/fonts.js.map +1 -0
  153. package/dist/html/gif.d.ts +15 -0
  154. package/dist/html/gif.d.ts.map +1 -0
  155. package/dist/html/gif.js +229 -0
  156. package/dist/html/gif.js.map +1 -0
  157. package/dist/html/index.d.ts +50 -11
  158. package/dist/html/index.d.ts.map +1 -1
  159. package/dist/html/index.js +115 -9
  160. package/dist/html/index.js.map +1 -1
  161. package/dist/html/layout/block.d.ts +226 -3
  162. package/dist/html/layout/block.d.ts.map +1 -1
  163. package/dist/html/layout/block.js +2937 -197
  164. package/dist/html/layout/block.js.map +1 -1
  165. package/dist/html/layout/boxes.d.ts +340 -17
  166. package/dist/html/layout/boxes.d.ts.map +1 -1
  167. package/dist/html/layout/boxes.js +1714 -182
  168. package/dist/html/layout/boxes.js.map +1 -1
  169. package/dist/html/layout/cache.d.ts +58 -0
  170. package/dist/html/layout/cache.d.ts.map +1 -0
  171. package/dist/html/layout/cache.js +224 -0
  172. package/dist/html/layout/cache.js.map +1 -0
  173. package/dist/html/layout/collapse.d.ts +45 -0
  174. package/dist/html/layout/collapse.d.ts.map +1 -0
  175. package/dist/html/layout/collapse.js +246 -0
  176. package/dist/html/layout/collapse.js.map +1 -0
  177. package/dist/html/layout/css-grid.d.ts +7 -0
  178. package/dist/html/layout/css-grid.d.ts.map +1 -0
  179. package/dist/html/layout/css-grid.js +900 -0
  180. package/dist/html/layout/css-grid.js.map +1 -0
  181. package/dist/html/layout/flex.d.ts.map +1 -1
  182. package/dist/html/layout/flex.js +1025 -89
  183. package/dist/html/layout/flex.js.map +1 -1
  184. package/dist/html/layout/floats.d.ts +45 -6
  185. package/dist/html/layout/floats.d.ts.map +1 -1
  186. package/dist/html/layout/floats.js +113 -14
  187. package/dist/html/layout/floats.js.map +1 -1
  188. package/dist/html/layout/grid-lines.d.ts +39 -0
  189. package/dist/html/layout/grid-lines.d.ts.map +1 -0
  190. package/dist/html/layout/grid-lines.js +119 -0
  191. package/dist/html/layout/grid-lines.js.map +1 -0
  192. package/dist/html/layout/grid.d.ts +53 -0
  193. package/dist/html/layout/grid.d.ts.map +1 -0
  194. package/dist/html/layout/grid.js +153 -0
  195. package/dist/html/layout/grid.js.map +1 -0
  196. package/dist/html/layout/inline.d.ts +151 -1
  197. package/dist/html/layout/inline.d.ts.map +1 -1
  198. package/dist/html/layout/inline.js +3375 -174
  199. package/dist/html/layout/inline.js.map +1 -1
  200. package/dist/html/layout/shaping.d.ts +6 -0
  201. package/dist/html/layout/shaping.d.ts.map +1 -0
  202. package/dist/html/layout/shaping.js +100 -0
  203. package/dist/html/layout/shaping.js.map +1 -0
  204. package/dist/html/layout/table.d.ts +3 -0
  205. package/dist/html/layout/table.d.ts.map +1 -1
  206. package/dist/html/layout/table.js +681 -175
  207. package/dist/html/layout/table.js.map +1 -1
  208. package/dist/html/layout/tracks.d.ts +44 -0
  209. package/dist/html/layout/tracks.d.ts.map +1 -0
  210. package/dist/html/layout/tracks.js +349 -0
  211. package/dist/html/layout/tracks.js.map +1 -0
  212. package/dist/html/node.d.ts +169 -4
  213. package/dist/html/node.d.ts.map +1 -1
  214. package/dist/html/node.js +1331 -100
  215. package/dist/html/node.js.map +1 -1
  216. package/dist/html/paint.d.ts +92 -3
  217. package/dist/html/paint.d.ts.map +1 -1
  218. package/dist/html/paint.js +3503 -141
  219. package/dist/html/paint.js.map +1 -1
  220. package/dist/html/resources.d.ts +60 -14
  221. package/dist/html/resources.d.ts.map +1 -1
  222. package/dist/html/resources.js +122 -36
  223. package/dist/html/resources.js.map +1 -1
  224. package/dist/html/surfaces.d.ts +23 -0
  225. package/dist/html/surfaces.d.ts.map +1 -0
  226. package/dist/html/surfaces.js +68 -0
  227. package/dist/html/surfaces.js.map +1 -0
  228. package/dist/html/svg.d.ts +80 -0
  229. package/dist/html/svg.d.ts.map +1 -0
  230. package/dist/html/svg.js +435 -0
  231. package/dist/html/svg.js.map +1 -0
  232. package/dist/html/url.d.ts +23 -0
  233. package/dist/html/url.d.ts.map +1 -0
  234. package/dist/html/url.js +80 -0
  235. package/dist/html/url.js.map +1 -0
  236. package/dist/index.d.ts +1 -1
  237. package/dist/index.d.ts.map +1 -1
  238. package/dist/index.js.map +1 -1
  239. package/dist/internal/collate.d.ts +3 -0
  240. package/dist/internal/collate.d.ts.map +1 -0
  241. package/dist/internal/collate.js +11 -0
  242. package/dist/internal/collate.js.map +1 -0
  243. package/dist/internal/markdown/ast.d.ts +11 -0
  244. package/dist/internal/markdown/ast.d.ts.map +1 -1
  245. package/dist/internal/markdown/parse.d.ts.map +1 -1
  246. package/dist/internal/markdown/parse.js +109 -8
  247. package/dist/internal/markdown/parse.js.map +1 -1
  248. package/dist/internal/prewarm.d.ts +18 -0
  249. package/dist/internal/prewarm.d.ts.map +1 -0
  250. package/dist/internal/prewarm.js +42 -0
  251. package/dist/internal/prewarm.js.map +1 -0
  252. package/dist/internal/sdf.d.ts +20 -0
  253. package/dist/internal/sdf.d.ts.map +1 -0
  254. package/dist/internal/sdf.js +216 -0
  255. package/dist/internal/sdf.js.map +1 -0
  256. package/dist/internal/splice.d.ts +12 -0
  257. package/dist/internal/splice.d.ts.map +1 -0
  258. package/dist/internal/splice.js +23 -0
  259. package/dist/internal/splice.js.map +1 -0
  260. package/dist/internal/window.d.ts +12 -0
  261. package/dist/internal/window.d.ts.map +1 -1
  262. package/dist/internal/window.js +23 -6
  263. package/dist/internal/window.js.map +1 -1
  264. package/dist/maps/anchors.d.ts +6 -1
  265. package/dist/maps/anchors.d.ts.map +1 -1
  266. package/dist/maps/anchors.js +260 -1
  267. package/dist/maps/anchors.js.map +1 -1
  268. package/dist/maps/controller.d.ts +57 -0
  269. package/dist/maps/controller.d.ts.map +1 -1
  270. package/dist/maps/controller.js +97 -18
  271. package/dist/maps/controller.js.map +1 -1
  272. package/dist/maps/gl/buckets.d.ts +5 -1
  273. package/dist/maps/gl/buckets.d.ts.map +1 -1
  274. package/dist/maps/gl/buckets.js +5 -2
  275. package/dist/maps/gl/buckets.js.map +1 -1
  276. package/dist/maps/gl/build-worker.d.ts +2 -0
  277. package/dist/maps/gl/build-worker.d.ts.map +1 -1
  278. package/dist/maps/gl/build-worker.js +1 -1
  279. package/dist/maps/gl/build-worker.js.map +1 -1
  280. package/dist/maps/gl/cover.d.ts +23 -4
  281. package/dist/maps/gl/cover.d.ts.map +1 -1
  282. package/dist/maps/gl/cover.js +61 -48
  283. package/dist/maps/gl/cover.js.map +1 -1
  284. package/dist/maps/gl/placement.d.ts +21 -8
  285. package/dist/maps/gl/placement.d.ts.map +1 -1
  286. package/dist/maps/gl/placement.js +133 -58
  287. package/dist/maps/gl/placement.js.map +1 -1
  288. package/dist/maps/gl/renderer.d.ts +36 -4
  289. package/dist/maps/gl/renderer.d.ts.map +1 -1
  290. package/dist/maps/gl/renderer.js +81 -24
  291. package/dist/maps/gl/renderer.js.map +1 -1
  292. package/dist/maps/gl/shaders.d.ts +18 -17
  293. package/dist/maps/gl/shaders.d.ts.map +1 -1
  294. package/dist/maps/gl/shaders.js +34 -32
  295. package/dist/maps/gl/shaders.js.map +1 -1
  296. package/dist/maps/gl/store.d.ts +10 -0
  297. package/dist/maps/gl/store.d.ts.map +1 -1
  298. package/dist/maps/gl/store.js +47 -4
  299. package/dist/maps/gl/store.js.map +1 -1
  300. package/dist/maps/gl/text.d.ts +153 -24
  301. package/dist/maps/gl/text.d.ts.map +1 -1
  302. package/dist/maps/gl/text.js +379 -63
  303. package/dist/maps/gl/text.js.map +1 -1
  304. package/dist/maps/gl/view.d.ts +293 -2
  305. package/dist/maps/gl/view.d.ts.map +1 -1
  306. package/dist/maps/gl/view.js +317 -82
  307. package/dist/maps/gl/view.js.map +1 -1
  308. package/dist/maps/icons.d.ts +32 -0
  309. package/dist/maps/icons.d.ts.map +1 -0
  310. package/dist/maps/icons.js +151 -0
  311. package/dist/maps/icons.js.map +1 -0
  312. package/dist/maps/index.d.ts +2 -0
  313. package/dist/maps/index.d.ts.map +1 -1
  314. package/dist/maps/index.js +2 -0
  315. package/dist/maps/index.js.map +1 -1
  316. package/dist/maps/labels.d.ts +56 -4
  317. package/dist/maps/labels.d.ts.map +1 -1
  318. package/dist/maps/labels.js +213 -46
  319. package/dist/maps/labels.js.map +1 -1
  320. package/dist/maps/node.d.ts +17 -0
  321. package/dist/maps/node.d.ts.map +1 -1
  322. package/dist/maps/node.js +64 -30
  323. package/dist/maps/node.js.map +1 -1
  324. package/dist/maps/proj.d.ts +48 -0
  325. package/dist/maps/proj.d.ts.map +1 -1
  326. package/dist/maps/proj.js +73 -0
  327. package/dist/maps/proj.js.map +1 -1
  328. package/dist/maps/sources.d.ts +5 -3
  329. package/dist/maps/sources.d.ts.map +1 -1
  330. package/dist/maps/sources.js +5 -3
  331. package/dist/maps/sources.js.map +1 -1
  332. package/dist/maps/style.d.ts +35 -0
  333. package/dist/maps/style.d.ts.map +1 -1
  334. package/dist/maps/style.js.map +1 -1
  335. package/dist/maps/styles.d.ts +12 -0
  336. package/dist/maps/styles.d.ts.map +1 -1
  337. package/dist/maps/styles.js +120 -6
  338. package/dist/maps/styles.js.map +1 -1
  339. package/dist/maps/tiles.d.ts +31 -11
  340. package/dist/maps/tiles.d.ts.map +1 -1
  341. package/dist/maps/tiles.js +61 -27
  342. package/dist/maps/tiles.js.map +1 -1
  343. package/dist/maps/types.d.ts +28 -4
  344. package/dist/maps/types.d.ts.map +1 -1
  345. package/dist/markdown/index.d.ts.map +1 -1
  346. package/dist/markdown/index.js +112 -43
  347. package/dist/markdown/index.js.map +1 -1
  348. package/dist/markdown/spans.d.ts.map +1 -1
  349. package/dist/markdown/spans.js +6 -0
  350. package/dist/markdown/spans.js.map +1 -1
  351. package/dist/qml/containers.d.ts.map +1 -1
  352. package/dist/qml/containers.js +6 -1
  353. package/dist/qml/containers.js.map +1 -1
  354. package/dist/qml/interaction.d.ts.map +1 -1
  355. package/dist/qml/interaction.js +38 -5
  356. package/dist/qml/interaction.js.map +1 -1
  357. package/dist/qml/layouts.d.ts.map +1 -1
  358. package/dist/qml/layouts.js +21 -6
  359. package/dist/qml/layouts.js.map +1 -1
  360. package/dist/qml/qtquick.d.ts.map +1 -1
  361. package/dist/qml/qtquick.js +9 -2
  362. package/dist/qml/qtquick.js.map +1 -1
  363. package/dist/qml/react.d.ts.map +1 -1
  364. package/dist/qml/react.js +9 -7
  365. package/dist/qml/react.js.map +1 -1
  366. package/dist/reorder/clock.d.ts +7 -0
  367. package/dist/reorder/clock.d.ts.map +1 -0
  368. package/dist/reorder/clock.js +26 -0
  369. package/dist/reorder/clock.js.map +1 -0
  370. package/dist/reorder/index.d.ts +25 -3
  371. package/dist/reorder/index.d.ts.map +1 -1
  372. package/dist/reorder/index.js +64 -19
  373. package/dist/reorder/index.js.map +1 -1
  374. package/dist/rich-text-editor/index.d.ts +0 -11
  375. package/dist/rich-text-editor/index.d.ts.map +1 -1
  376. package/dist/rich-text-editor/index.js +35 -1
  377. package/dist/rich-text-editor/index.js.map +1 -1
  378. package/dist/rich-text-editor/keymap.d.ts.map +1 -1
  379. package/dist/rich-text-editor/keymap.js +2 -1
  380. package/dist/rich-text-editor/keymap.js.map +1 -1
  381. package/dist/rich-text-editor/keys.d.ts +17 -6
  382. package/dist/rich-text-editor/keys.d.ts.map +1 -1
  383. package/dist/rich-text-editor/keys.js +135 -26
  384. package/dist/rich-text-editor/keys.js.map +1 -1
  385. package/dist/rich-text-editor/marks.d.ts +9 -0
  386. package/dist/rich-text-editor/marks.d.ts.map +1 -0
  387. package/dist/rich-text-editor/marks.js +83 -0
  388. package/dist/rich-text-editor/marks.js.map +1 -0
  389. package/dist/rich-text-editor/render.d.ts.map +1 -1
  390. package/dist/rich-text-editor/render.js +28 -10
  391. package/dist/rich-text-editor/render.js.map +1 -1
  392. package/dist/rich-text-editor/toolbar.d.ts.map +1 -1
  393. package/dist/rich-text-editor/toolbar.js +1 -1
  394. package/dist/rich-text-editor/toolbar.js.map +1 -1
  395. package/dist/rich-text-editor/virtual.d.ts.map +1 -1
  396. package/dist/rich-text-editor/virtual.js +17 -0
  397. package/dist/rich-text-editor/virtual.js.map +1 -1
  398. package/dist/richtext/node.d.ts +46 -2
  399. package/dist/richtext/node.d.ts.map +1 -1
  400. package/dist/richtext/node.js +187 -4
  401. package/dist/richtext/node.js.map +1 -1
  402. package/dist/richtext/runs.d.ts +13 -8
  403. package/dist/richtext/runs.d.ts.map +1 -1
  404. package/dist/richtext/runs.js +18 -8
  405. package/dist/richtext/runs.js.map +1 -1
  406. package/dist/table/index.d.ts.map +1 -1
  407. package/dist/table/index.js +23 -0
  408. package/dist/table/index.js.map +1 -1
  409. package/dist/table/rows.d.ts +3 -2
  410. package/dist/table/rows.d.ts.map +1 -1
  411. package/dist/table/rows.js +20 -6
  412. package/dist/table/rows.js.map +1 -1
  413. package/dist/terminal/index.d.ts.map +1 -1
  414. package/dist/terminal/index.js +4 -0
  415. package/dist/terminal/index.js.map +1 -1
  416. package/dist/terminal/vt/pty.d.ts +13 -2
  417. package/dist/terminal/vt/pty.d.ts.map +1 -1
  418. package/dist/terminal/vt/pty.js +18 -3
  419. package/dist/terminal/vt/pty.js.map +1 -1
  420. package/dist/terminal-output/index.d.ts.map +1 -1
  421. package/dist/terminal-output/index.js +6 -1
  422. package/dist/terminal-output/index.js.map +1 -1
  423. package/dist/three/canvas.d.ts.map +1 -1
  424. package/dist/three/canvas.js +85 -7
  425. package/dist/three/canvas.js.map +1 -1
  426. package/dist/three/events.d.ts +22 -4
  427. package/dist/three/events.d.ts.map +1 -1
  428. package/dist/three/events.js +34 -4
  429. package/dist/three/events.js.map +1 -1
  430. package/dist/tree/index.d.ts.map +1 -1
  431. package/dist/tree/index.js +17 -0
  432. package/dist/tree/index.js.map +1 -1
  433. package/package.json +42 -33
  434. package/src/charts/node.ts +62 -26
  435. package/src/charts/render.ts +120 -30
  436. package/src/code/index.ts +3 -1
  437. package/src/code-editor/completion.ts +3 -2
  438. package/src/code-editor/index.ts +19 -0
  439. package/src/code-editor/node.ts +1574 -184
  440. package/src/code-language/languages/glsl.ts +12 -2
  441. package/src/code-language/languages/javascript.ts +21 -4
  442. package/src/code-language/languages/json.ts +12 -2
  443. package/src/code-language/languages/shell.ts +12 -2
  444. package/src/code-language/languages/sql.ts +22 -2
  445. package/src/code-language/stream.ts +257 -17
  446. package/src/code-language/types.ts +5 -4
  447. package/src/codeblock/index.ts +20 -3
  448. package/src/flow/draw.ts +167 -41
  449. package/src/flow/gl/color.ts +85 -0
  450. package/src/flow/gl/index.ts +342 -0
  451. package/src/flow/gl/pack.ts +585 -0
  452. package/src/flow/gl/renderer.ts +679 -0
  453. package/src/flow/gl/shaders.ts +371 -0
  454. package/src/flow/gl/text.ts +1295 -0
  455. package/src/flow/index.ts +794 -67
  456. package/src/flow/node.ts +2496 -1256
  457. package/src/flow/paint.ts +511 -0
  458. package/src/flow/paths.ts +80 -15
  459. package/src/flow/scene.ts +2199 -0
  460. package/src/flow/types.ts +113 -1
  461. package/src/html/controls.ts +73 -2
  462. package/src/html/css/calc.ts +385 -0
  463. package/src/html/css/cascade.ts +1403 -62
  464. package/src/html/css/color.ts +755 -0
  465. package/src/html/css/content.ts +417 -0
  466. package/src/html/css/counter-styles.ts +973 -0
  467. package/src/html/css/decode.ts +130 -0
  468. package/src/html/css/parse.ts +1514 -160
  469. package/src/html/css/style.ts +4102 -222
  470. package/src/html/css/ua.ts +58 -12
  471. package/src/html/css/values.ts +150 -50
  472. package/src/html/css/vars.ts +348 -0
  473. package/src/html/dom.ts +284 -17
  474. package/src/html/fonts.ts +641 -0
  475. package/src/html/gif.ts +233 -0
  476. package/src/html/index.ts +180 -20
  477. package/src/html/layout/block.ts +3456 -230
  478. package/src/html/layout/boxes.ts +2100 -190
  479. package/src/html/layout/cache.ts +289 -0
  480. package/src/html/layout/collapse.ts +317 -0
  481. package/src/html/layout/css-grid.ts +1063 -0
  482. package/src/html/layout/flex.ts +1115 -106
  483. package/src/html/layout/floats.ts +128 -14
  484. package/src/html/layout/grid-lines.ts +141 -0
  485. package/src/html/layout/grid.ts +181 -0
  486. package/src/html/layout/inline.ts +3943 -192
  487. package/src/html/layout/shaping.ts +140 -0
  488. package/src/html/layout/table.ts +696 -186
  489. package/src/html/layout/tracks.ts +437 -0
  490. package/src/html/node.ts +1481 -118
  491. package/src/html/paint.ts +4293 -169
  492. package/src/html/resources.ts +161 -50
  493. package/src/html/surfaces.ts +81 -0
  494. package/src/html/svg.ts +493 -0
  495. package/src/html/url.ts +86 -0
  496. package/src/index.ts +2 -0
  497. package/src/internal/collate.ts +12 -0
  498. package/src/internal/markdown/ast.ts +11 -0
  499. package/src/internal/markdown/parse.ts +154 -11
  500. package/src/internal/prewarm.ts +45 -0
  501. package/src/internal/sdf.ts +227 -0
  502. package/src/internal/splice.ts +25 -0
  503. package/src/internal/window.ts +36 -5
  504. package/src/maps/anchors.ts +307 -1
  505. package/src/maps/controller.ts +116 -31
  506. package/src/maps/gl/buckets.ts +5 -1
  507. package/src/maps/gl/build-worker.ts +13 -2
  508. package/src/maps/gl/cover.ts +79 -44
  509. package/src/maps/gl/placement.ts +221 -69
  510. package/src/maps/gl/renderer.ts +104 -20
  511. package/src/maps/gl/shaders.ts +35 -32
  512. package/src/maps/gl/store.ts +66 -17
  513. package/src/maps/gl/text.ts +444 -71
  514. package/src/maps/gl/view.ts +361 -96
  515. package/src/maps/icons.ts +183 -0
  516. package/src/maps/index.ts +4 -0
  517. package/src/maps/labels.ts +269 -49
  518. package/src/maps/node.ts +82 -49
  519. package/src/maps/proj.ts +87 -0
  520. package/src/maps/sources.ts +5 -3
  521. package/src/maps/style.ts +32 -0
  522. package/src/maps/styles.ts +215 -4
  523. package/src/maps/tiles.ts +64 -31
  524. package/src/maps/types.ts +28 -4
  525. package/src/markdown/index.ts +149 -67
  526. package/src/markdown/spans.ts +5 -0
  527. package/src/qml/containers.tsx +6 -1
  528. package/src/qml/interaction.tsx +42 -5
  529. package/src/qml/layouts.tsx +21 -6
  530. package/src/qml/qtquick.tsx +9 -2
  531. package/src/qml/react.tsx +7 -5
  532. package/src/reorder/clock.ts +27 -0
  533. package/src/reorder/index.ts +100 -27
  534. package/src/rich-text-editor/index.ts +34 -1
  535. package/src/rich-text-editor/keymap.ts +1 -1
  536. package/src/rich-text-editor/keys.ts +135 -26
  537. package/src/rich-text-editor/marks.ts +101 -0
  538. package/src/rich-text-editor/render.ts +49 -12
  539. package/src/rich-text-editor/toolbar.ts +1 -1
  540. package/src/rich-text-editor/virtual.ts +25 -0
  541. package/src/richtext/node.ts +235 -14
  542. package/src/richtext/runs.ts +23 -12
  543. package/src/table/index.ts +29 -0
  544. package/src/table/rows.ts +18 -8
  545. package/src/terminal/index.ts +4 -0
  546. package/src/terminal/vt/pty.ts +16 -2
  547. package/src/terminal-output/index.ts +6 -1
  548. package/src/three/canvas.ts +102 -8
  549. package/src/three/events.ts +49 -9
  550. package/src/tree/index.ts +19 -0
package/src/html/node.ts CHANGED
@@ -48,27 +48,53 @@ import type { Style } from 'react-x11/style';
48
48
  import type { Element } from 'domhandler';
49
49
 
50
50
  import { codePointAtOffset, codeUnitOffsets } from '../internal/text.js';
51
- import { attr, HtmlSource, isElement, tagOf } from './dom.js';
51
+ import { attr, HtmlSource, imageUrlOf, isElement, tagOf } from './dom.js';
52
52
  import type { Document } from './dom.js';
53
53
  import { Cascade } from './css/cascade.js';
54
- import { parseStylesheet } from './css/parse.js';
54
+ import { mediaMatches, parseStylesheet } from './css/parse.js';
55
55
  import type { Stylesheet } from './css/parse.js';
56
56
  import { uaStylesheet } from './css/ua.js';
57
- import type { RootLook } from './css/style.js';
58
- import { buildBoxes } from './layout/boxes.js';
59
- import type { Box, BoxTree, ReplacedKind } from './layout/boxes.js';
57
+ import type { ComputedStyle, RootLook } from './css/style.js';
58
+ import { buildBoxes, CONTENT_IMAGES } from './layout/boxes.js';
59
+ import type {
60
+ Box,
61
+ BoxTree,
62
+ LineText,
63
+ ReplacedKind,
64
+ TextLayoutLike,
65
+ } from './layout/boxes.js';
60
66
  import { layoutDocument } from './layout/block.js';
67
+ import { TextLayoutCache } from './layout/cache.js';
68
+ import { shapingSafe } from './layout/shaping.js';
69
+ import { SurfaceCache } from './surfaces.js';
70
+ import { inlineDecoration, runFor } from './layout/inline.js';
61
71
  import type { FontsLike } from './layout/inline.js';
72
+ import type { TextRun } from '../richtext/index.js';
62
73
  // Through the inline module rather than a second cache: the offsets table for
63
74
  // a layout is built once, on the first selection that needs it.
64
- import { layoutOffsets as layoutOffsetsOf } from './layout/inline.js';
75
+ import {
76
+ documentOffsetOf,
77
+ layoutOffsetOf,
78
+ layoutOffsets as layoutOffsetsOf,
79
+ } from './layout/inline.js';
65
80
  import { lineBands as bandsFor } from '../richtext/runs.js';
66
- import { paintDocument, queryChildIndex } from './paint.js';
81
+ import {
82
+ clipsOverflow,
83
+ containingBlockOf,
84
+ hasRect,
85
+ holds,
86
+ holdsAbsolute,
87
+ paintDocument,
88
+ queryChildIndex,
89
+ } from './paint.js';
67
90
  import type { PaintContext } from './paint.js';
68
91
  import { controlRectsOf, measureControl } from './controls.js';
69
- import type { ControlRect } from './controls.js';
92
+ import type { BareField, ControlRect } from './controls.js';
70
93
  import { ResourceStore } from './resources.js';
71
94
  import type { ResourceRequest, ResourceResult } from './resources.js';
95
+ import { WebFonts } from './fonts.js';
96
+ import type { DeclaredFace } from './fonts.js';
97
+ import { resolveUrl, UrlResolver } from './url.js';
72
98
 
73
99
  /** The element name — registration key, `node.kind` and JSX tag alike. */
74
100
  export const ELEMENT = 'htmlview';
@@ -95,6 +121,12 @@ export interface HtmlViewProps {
95
121
  complete?: boolean;
96
122
  /** Author stylesheets applied after the document's own. */
97
123
  stylesheet?: string | string[];
124
+ /** The encoding the host decoded `source` from: what a stylesheet handed
125
+ * over as bytes falls back to. */
126
+ charset?: string;
127
+ /** The URL the document came from, which its relative URLs resolve
128
+ * against — see `url.ts`. */
129
+ baseUrl?: string | null;
98
130
  look: RootLook;
99
131
  selectionColor?: string;
100
132
  onResource?: (
@@ -118,11 +150,48 @@ export function registerHtmlView(): void {
118
150
  // `source` and `look` are this element's own vocabulary and neither is a
119
151
  // style name today; declaring them keeps the DEV flat-style-prop
120
152
  // assertion honest if core's vocabulary grows underneath us.
121
- semanticNames: ['source', 'look', 'stylesheet', 'complete'],
153
+ semanticNames: [
154
+ 'source',
155
+ 'look',
156
+ 'stylesheet',
157
+ 'charset',
158
+ 'complete',
159
+ 'baseUrl',
160
+ ],
122
161
  childrenAllowed: false,
123
162
  });
124
163
  }
125
164
 
165
+ /** How many widths' sizes a document keeps (`HtmlViewNode._sizeAt`): the
166
+ * two a frame of a resize asks, and a little slack for a drag that turns
167
+ * back. */
168
+ const SIZES_KEPT = 4;
169
+
170
+ /**
171
+ * A size in the whole pixels core lays out in, rounded up — less the float
172
+ * noise a sum of Yoga's single-precision positions carries: a page exactly
173
+ * `100vh` tall came to 737.0000076 under a 737-pixel viewport, and a
174
+ * rounding that took it for 738 scrolled it by a pixel under a scrollbar.
175
+ */
176
+ function wholePixels(n: number): number {
177
+ return Math.max(0, Math.ceil(n - 1 / 64));
178
+ }
179
+
180
+ /** How deep `@import`s are followed: an import in an import in an import is
181
+ * a stylesheet; sixteen of them is a loop that changes its URL each time. */
182
+ const MAX_IMPORT_DEPTH = 16;
183
+
184
+ /** A scheme at the start: an absolute URL, a base that means something. */
185
+ const ABSOLUTE_URL = /^[a-zA-Z][a-zA-Z0-9+.-]*:/;
186
+
187
+ /** What warming a document's faces needs of the fonts: ntk's
188
+ * `FontManager#prewarm`, which names faces from the release that added
189
+ * them (8.14) and warms the family's four before it. An engine without it
190
+ * has nothing to look up. */
191
+ interface WarmingFonts {
192
+ prewarm?(family: string, faces?: { weight: number; style: string }[]): void;
193
+ }
194
+
126
195
  /** What changed, and therefore how far back up the pipeline to go. */
127
196
  const enum Stale {
128
197
  Nothing = 0,
@@ -134,16 +203,43 @@ const enum Stale {
134
203
 
135
204
  export class HtmlViewNode extends Node {
136
205
  private _source = new HtmlSource();
206
+ /** Where the document's relative URLs resolve: its `<base href>` against
207
+ * the `baseUrl` prop, or the prop, or nowhere (`url.ts`). */
208
+ private _urls = new UrlResolver();
137
209
  private _resources: ResourceStore;
210
+ /** The families the document's `@font-face` rules declare (`fonts.ts`). */
211
+ private _webFonts: WebFonts;
212
+ /** The sheets the last restyle read, and the cascade built from them. */
213
+ private _sheetsRead: SheetsRead | null = null;
214
+ /** Blurred shadows, drawn once each. */
215
+ private _shadowCache: SurfaceCache | null = null;
138
216
  private _cascade: Cascade | null = null;
139
217
  private _tree: BoxTree | null = null;
218
+ /** The faces `_warmFaces` has asked the font matcher for, and of which
219
+ * fonts. */
220
+ private _warmedFaces = new Set<string>();
221
+ private _warmedFor: unknown = null;
140
222
  private _stale: Stale = Stale.Everything;
141
223
  /** The width the document was last laid out at. Not `_laidOutAt`: core's
142
224
  * `Node` has a method of that name, which a field would hide. */
143
225
  private _laidOutWidth = -1;
144
226
  private _mediaBand = -1;
227
+ /** The viewport the box tree's styles were computed at, for the ones
228
+ * that read it (`vw`, `vh`). */
229
+ private _styledWidth = -1;
230
+ private _styledHeight = -1;
145
231
  private _documentHeight = 0;
146
232
  private _documentWidth = 0;
233
+ /** The viewport height the document was last laid out under, and whether
234
+ * that layout read it (`LayoutResult.readsViewportHeight`). */
235
+ private _laidOutUnder = -1;
236
+ private _layoutReadsViewport = false;
237
+ /** Whether core is asking for this element's size right now — inside
238
+ * its layout pass, where a node must not ask to be measured again. */
239
+ private _measuring = false;
240
+ /** What the document came to at the widths it was last laid out at, while
241
+ * nothing a layout reads has changed (`_sizeAt`). */
242
+ private _sizes = new Map<number, { width: number; height: number }>();
147
243
  private _textPoints: number[] | null = null;
148
244
  /** Whether code points and code units are the same index — true unless
149
245
  * the text carries surrogate pairs. null until checked. */
@@ -155,14 +251,32 @@ export class HtmlViewNode extends Node {
155
251
 
156
252
  constructor(props: Record<string, unknown>, app: NtkApp) {
157
253
  super(ELEMENT, props, app);
254
+ const ask = (request: ResourceRequest) =>
255
+ this._props().onResource?.(request) ?? null;
158
256
  this._resources = new ResourceStore(
159
- (request) => this._props().onResource?.(request) ?? null,
257
+ ask,
258
+ (what) => {
259
+ // A late stylesheet is a new cascade: its rules, what it imports,
260
+ // the faces it declares. A late image changes intrinsic sizes, so
261
+ // the box tree is what has to be rebuilt — not merely repainted.
262
+ // Either arriving after first paint is the ordinary case, not an
263
+ // error path: a host on a network answers every request that way.
264
+ this._invalidate(what === 'stylesheet' ? Stale.Style : Stale.Boxes);
265
+ },
266
+ this._urls,
267
+ );
268
+ this._webFonts = new WebFonts(
269
+ app,
270
+ ask,
160
271
  () => {
161
- // A late resource changes intrinsic sizes, so the box tree is what
162
- // has to be rebuilt — not merely repainted. An image that arrives
163
- // after first paint is the ordinary case, not an error path.
272
+ // A face arrived: the family lists change, so the styles do, and
273
+ // every text layout kept from before was set in the face it
274
+ // replaces — under the same list, where a family of the document's
275
+ // had loaded one weight and now has another.
276
+ this._layouts = null;
164
277
  this._invalidate(Stale.Boxes);
165
278
  },
279
+ 'sans-serif',
166
280
  );
167
281
  this._read();
168
282
  }
@@ -256,9 +370,24 @@ export class HtmlViewNode extends Node {
256
370
  props.onDocument?.(this._source.document);
257
371
  this._reportedDomRevision = props.domRevision ?? 0;
258
372
  }
373
+ this._updateBase();
259
374
  this._sweep();
260
375
  }
261
376
 
377
+ /**
378
+ * The document's base URL: its first `<base href>`, resolved against the
379
+ * `baseUrl` prop, or the prop — and only an absolute one, since a relative
380
+ * base resolves nothing. Everything resolved against the old one is
381
+ * stale when it moves, the sheets' URLs with it, so the cascade is.
382
+ */
383
+ private _updateBase(): void {
384
+ const given = this._props().baseUrl || null;
385
+ const href = this._source.facts().base;
386
+ let base = href ? resolveUrl(href, given) : given;
387
+ if (base && !ABSOLUTE_URL.test(base)) base = null;
388
+ if (this._urls.setBase(base)) this._invalidate(Stale.Style);
389
+ }
390
+
262
391
  /**
263
392
  * Hand the document's own declarations to the host: every `<script>` once,
264
393
  * and every resource that has not been asked for.
@@ -286,7 +415,7 @@ export class HtmlViewNode extends Node {
286
415
  }
287
416
  for (const el of facts.resources) {
288
417
  const tag = tagOf(el);
289
- const url = tag === 'link' ? attr(el, 'href') : attr(el, 'src');
418
+ const url = tag === 'link' ? attr(el, 'href') : imageUrlOf(el);
290
419
  if (!url) continue;
291
420
  this._resources.request({
292
421
  url,
@@ -296,49 +425,190 @@ export class HtmlViewNode extends Node {
296
425
  }
297
426
  }
298
427
 
428
+ /**
429
+ * Ask for every `background-image` the styles name. Known only once the
430
+ * cascade has run, where an `<img>` is known from the markup; the store
431
+ * asks once a URL, so a tree built again asks nothing new.
432
+ */
433
+ private _requestBackgrounds(tree: BoxTree): void {
434
+ for (const box of tree.backgrounds) {
435
+ if (!box.el) continue;
436
+ // each layer's, where there is more than one
437
+ for (const url of box.style.backgroundImages ?? [
438
+ box.style.backgroundImage,
439
+ ]) {
440
+ if (typeof url === 'string') {
441
+ this._resources.request({ url, kind: 'image', element: box.el });
442
+ }
443
+ }
444
+ const border = box.style.borderImage.source;
445
+ if (typeof border === 'string') {
446
+ this._resources.request({
447
+ url: border,
448
+ kind: 'image',
449
+ element: box.el,
450
+ });
451
+ }
452
+ }
453
+ // and every image generated content names, which is only known there
454
+ // too
455
+ for (const { url, element } of tree.contentImages) {
456
+ this._resources.request({ url, kind: 'image', element });
457
+ }
458
+ }
459
+
460
+ /** Whether an image generated content names arrived as it was asked for,
461
+ * the tree built without its size: a host that answers at once is told
462
+ * nothing later, where an `<img>` is asked for before the build. */
463
+ private _contentImagesArrived(tree: BoxTree): boolean {
464
+ for (const { url, sized } of tree.contentImages) {
465
+ if (!sized && this._resources.imageSize(url)) return true;
466
+ }
467
+ return false;
468
+ }
469
+
470
+ /**
471
+ * Ask the font matcher for the faces the document's text is set in, as
472
+ * soon as its boxes say which and while its layout is still ahead. A face
473
+ * nothing had warmed was a synchronous fc-match inside that layout: the
474
+ * benchmark report's `th`, at 600, waited 38 ms before its first paint.
475
+ * One child answers every face a call names, off the event loop (ntk's
476
+ * `FontManager#prewarm`), and a layout reaching a face still on its way
477
+ * takes the answer rather than asking again. Each face is asked once, by
478
+ * the family, weight and style the layout hands the engine.
479
+ */
480
+ private _warmFaces(tree: BoxTree): void {
481
+ const fonts = this._fonts() as WarmingFonts | null;
482
+ if (typeof fonts?.prewarm !== 'function') return;
483
+ if (this._warmedFor !== fonts) {
484
+ this._warmedFor = fonts;
485
+ this._warmedFaces.clear();
486
+ }
487
+ let faces: Map<string, { weight: number; style: string }[]> | null = null;
488
+ for (const style of tree.textStyles) {
489
+ const key = `${style.fontFamily}|${style.fontWeight}|${style.fontStyle}`;
490
+ if (this._warmedFaces.has(key)) continue;
491
+ this._warmedFaces.add(key);
492
+ faces ??= new Map();
493
+ let named = faces.get(style.fontFamily);
494
+ if (!named) faces.set(style.fontFamily, (named = []));
495
+ named.push({ weight: style.fontWeight, style: style.fontStyle });
496
+ }
497
+ if (faces)
498
+ for (const [family, named] of faces) fonts.prewarm(family, named);
499
+ }
500
+
299
501
  /** Rebuild the cascade — the document's sheets plus the host's. */
300
502
  private _restyle(width: number): void {
301
503
  const props = this._props();
302
504
  const look = this._deviceLook();
303
- const sheets: Stylesheet[] = [uaStylesheet(look)];
304
- let order = 0;
505
+ const documentBase = this._urls.base;
506
+ // What the sheets are read from, in order: a `<style>`'s text or a
507
+ // fetched `<link>`'s, and after them the host's — each with the URL its
508
+ // own relative URLs resolve against: the document's for a `<style>`,
509
+ // the sheet's own for a `<link>`.
510
+ const read: SheetText[] = [];
305
511
  for (const ref of this._source.facts().sheets) {
306
- const text =
512
+ // A sheet handed over as bytes that names no encoding of its own is in
513
+ // its referrer's: a `<link charset>`, then the document's (CSS 2.1
514
+ // 4.4), and an import is in the encoding of the sheet importing it.
515
+ const linked =
307
516
  ref.kind === 'inline'
308
- ? ref.text
309
- : this._resources.stylesheetText(ref.href);
517
+ ? null
518
+ : this._resources.stylesheet(ref.href, [
519
+ attr(ref.element, 'charset') || undefined,
520
+ props.charset,
521
+ ]);
522
+ const text = ref.kind === 'inline' ? ref.text : linked?.text;
310
523
  if (!text) continue;
311
- const sheet = parseStylesheet(text, order);
312
- order += sheet.rules.length + 1;
313
- // `@import` is a resource like any other, and its rules sit *before*
314
- // the importing sheet's — so a fetched import is spliced in ahead.
315
- for (const url of sheet.imports) {
316
- this._resources.request({
317
- url,
318
- kind: 'stylesheet',
319
- element: ref.element,
320
- });
321
- const imported = this._resources.stylesheetText(url);
322
- if (imported) {
323
- const parsed = parseStylesheet(imported, order);
324
- order += parsed.rules.length + 1;
325
- sheets.push(parsed);
326
- }
327
- }
328
- sheets.push(sheet);
524
+ const encoding = linked ? linked.encoding : props.charset;
525
+ const base =
526
+ ref.kind === 'inline'
527
+ ? documentBase
528
+ : this._resources.sheetBase(ref.href);
529
+ read.push({ text, encoding, element: ref.element, base });
329
530
  }
330
531
  const extra = props.stylesheet;
331
- for (const text of Array.isArray(extra) ? extra : extra ? [extra] : []) {
332
- const sheet = parseStylesheet(text, order);
333
- order += sheet.rules.length + 1;
334
- sheets.push(sheet);
335
- }
336
- this._cascade = new Cascade(
337
- sheets,
338
- look,
339
- width,
340
- this._viewportHeight(),
341
- this._scale,
532
+ const extras = Array.isArray(extra) ? extra : extra ? [extra] : [];
533
+ const fonts = this._fonts();
534
+ // Sheets that read as they did last time are the same rules: an append
535
+ // to a streamed document, or a width across a `@media` breakpoint,
536
+ // restyles the elements without parsing a framework's stylesheet again
537
+ // or indexing its thousand rules again.
538
+ const kept = this._sheetsRead;
539
+ let faces: DeclaredFace[];
540
+ if (
541
+ kept &&
542
+ kept.look === look &&
543
+ kept.scale === this._scale &&
544
+ kept.fonts === fonts &&
545
+ this._sameSheets(kept, read, extras)
546
+ ) {
547
+ this._cascade = kept.cascade;
548
+ kept.cascade.viewportWidth = width;
549
+ kept.cascade.viewportHeight = this._viewportHeight();
550
+ faces = kept.faces;
551
+ } else {
552
+ const sheets: Stylesheet[] = [uaStylesheet(look)];
553
+ const imports: ImportRead[][] = [];
554
+ faces = [];
555
+ let order = 0;
556
+ const layers = new Map<string, number>();
557
+ // A sheet takes its place in the cascade's order once everything it
558
+ // imports has taken theirs: `@import` is a resource like any other,
559
+ // and an imported sheet's rules sit *before* the importing sheet's
560
+ // (CSS 2.1 6.4.1), or an imported rule would win a tie against the
561
+ // sheet importing it.
562
+ const place = (sheet: Stylesheet, element: Element): void => {
563
+ for (const rule of sheet.rules) rule.order = order++;
564
+ order += 1;
565
+ sheets.push(sheet);
566
+ for (const rule of sheet.fontFaces) faces.push({ rule, element });
567
+ };
568
+ for (const { text, encoding, element, base } of read) {
569
+ const sheet = parseStylesheet(text, 0, layers, base);
570
+ const seen: ImportRead[] = [];
571
+ this._placeImports(sheet, encoding, element, layers, seen, place);
572
+ imports.push(seen);
573
+ place(sheet, element);
574
+ }
575
+ for (const text of extras) {
576
+ const sheet = parseStylesheet(text, order, layers);
577
+ order += sheet.rules.length + 1;
578
+ sheets.push(sheet);
579
+ }
580
+ this._cascade = new Cascade(
581
+ sheets,
582
+ look,
583
+ width,
584
+ this._viewportHeight(),
585
+ this._scale,
586
+ fonts ? (family, size) => xHeightOf(fonts, family, size) : null,
587
+ fonts ? (family, size) => zeroWidthOf(fonts, family, size) : null,
588
+ fonts ? (family, size) => normalLineOf(fonts, family, size) : null,
589
+ faces.length ? this._webFonts : null,
590
+ );
591
+ this._sheetsRead = {
592
+ look,
593
+ scale: this._scale,
594
+ fonts,
595
+ texts: read.map((r) => r.text),
596
+ encodings: read.map((r) => r.encoding),
597
+ bases: read.map((r) => r.base),
598
+ imports,
599
+ extras,
600
+ faces,
601
+ cascade: this._cascade,
602
+ };
603
+ }
604
+ // The faces this width and scheme declare: a `@font-face` may sit in a
605
+ // `@media` block like any rule.
606
+ const cssWidth = width / this._scale;
607
+ this._webFonts.setFallback(look.fontFamily);
608
+ this._webFonts.setFaces(
609
+ faces.filter((f) =>
610
+ mediaMatches(f.rule.media, cssWidth, look.colorScheme),
611
+ ),
342
612
  );
343
613
  this._cascade.setPointer({
344
614
  hovered: new Set(this._hovered),
@@ -347,10 +617,101 @@ export class HtmlViewNode extends Node {
347
617
  this._mediaBand = this._cascade.mediaBand(width);
348
618
  }
349
619
 
620
+ /**
621
+ * Place what a sheet imports, and what each of those imports, depth
622
+ * first — asking for each through the seam, as a parse of the sheet has
623
+ * to. `seen` records every import read, in order, which is what tells the
624
+ * next restyle whether the sheets read the same (`_sameSheets`). A sheet
625
+ * that imports itself, or one of the sheets importing it, is read once.
626
+ */
627
+ private _placeImports(
628
+ sheet: Stylesheet,
629
+ encoding: string | undefined,
630
+ element: Element,
631
+ layers: Map<string, number>,
632
+ seen: ImportRead[],
633
+ place: (sheet: Stylesheet, element: Element) => void,
634
+ depth = 0,
635
+ chain: Set<string> = new Set(),
636
+ ): void {
637
+ for (const url of sheet.imports) {
638
+ this._resources.request({ url, kind: 'stylesheet', element });
639
+ const fetched = this._resources.stylesheet(url, [encoding]);
640
+ const base = this._resources.sheetBase(url);
641
+ seen.push({ url, text: fetched?.text ?? null, base });
642
+ const key = base ?? url;
643
+ if (!fetched || depth >= MAX_IMPORT_DEPTH || chain.has(key)) continue;
644
+ const imported = parseStylesheet(fetched.text, 0, layers, base);
645
+ chain.add(key);
646
+ this._placeImports(
647
+ imported,
648
+ fetched.encoding,
649
+ element,
650
+ layers,
651
+ seen,
652
+ place,
653
+ depth + 1,
654
+ chain,
655
+ );
656
+ chain.delete(key);
657
+ place(imported, element);
658
+ }
659
+ }
660
+
661
+ /** Whether the sheets read the same as the ones `kept` was built from,
662
+ * down to what each imports — asking for the imports again, as a parse
663
+ * of the sheet would have. */
664
+ private _sameSheets(
665
+ kept: SheetsRead,
666
+ read: SheetText[],
667
+ extras: string[],
668
+ ): boolean {
669
+ if (read.length !== kept.texts.length) return false;
670
+ if (extras.length !== kept.extras.length) return false;
671
+ for (let i = 0; i < extras.length; i += 1) {
672
+ if (extras[i] !== kept.extras[i]) return false;
673
+ }
674
+ for (let i = 0; i < read.length; i += 1) {
675
+ const r = read[i];
676
+ if (
677
+ r.text !== kept.texts[i] ||
678
+ r.encoding !== kept.encodings[i] ||
679
+ r.base !== kept.bases[i]
680
+ ) {
681
+ return false;
682
+ }
683
+ // The encodings an import falls back to are its importer's, which
684
+ // only a parse knows; the text an import decoded to under them is what
685
+ // was kept, and bytes that decode differently now are a new sheet.
686
+ for (const { url, text, base } of kept.imports[i]) {
687
+ this._resources.request({
688
+ url,
689
+ kind: 'stylesheet',
690
+ element: r.element,
691
+ });
692
+ const fetched = this._resources.stylesheet(url, [r.encoding]);
693
+ if ((fetched?.text ?? null) !== text) return false;
694
+ if (this._resources.sheetBase(url) !== base) return false;
695
+ }
696
+ }
697
+ return true;
698
+ }
699
+
350
700
  private _viewportHeight(): number {
351
- // The viewport a `vh` resolves against is the window's, not the
352
- // document's — a document taller than the window does not make `100vh`
353
- // taller with it.
701
+ // The viewport a `vh` resolves against is the one the document is seen
702
+ // through, not the document — a document taller than it does not make
703
+ // `100vh` taller with it. That is the box that scrolls the element,
704
+ // where one does: a browser's page area, below its tab strip and its
705
+ // toolbar, where `100vh` measured by the window put a page's footer the
706
+ // toolbar's height below the fold. The window, where nothing scrolls it.
707
+ for (let node = this.parent; node; node = node.parent) {
708
+ // `Scrollable`'s, and not every node's: asked the way core asks it
709
+ const scroller = node as { isScroller?: () => boolean };
710
+ if (!scroller.isScroller?.()) continue;
711
+ const height = node.contentBox().height;
712
+ if (height > 0) return height;
713
+ break;
714
+ }
354
715
  const root = this.root;
355
716
  const height = root?.abs?.height;
356
717
  return height && height > 0 ? height : 600;
@@ -358,20 +719,101 @@ export class HtmlViewNode extends Node {
358
719
 
359
720
  private _fonts(): FontsLike | null {
360
721
  const fonts = (this.app as { fonts?: FontsLike } | null)?.fonts;
361
- return fonts ?? null;
722
+ // a face the engine cannot shape from costs its characters, not the
723
+ // document (`shaping.ts`)
724
+ return fonts ? shapingSafe(fonts) : null;
725
+ }
726
+
727
+ /** The text layouts the last pass made, for this one to reuse. */
728
+ private _layouts: TextLayoutCache | null = null;
729
+
730
+ /** The fonts a layout pass lays text out with: the engine's, through the
731
+ * layouts the pass before made (`TextLayoutCache`). */
732
+ private _layoutFonts(): FontsLike | null {
733
+ const fonts = this._fonts();
734
+ if (!fonts) return null;
735
+ if (this._layouts?.engine !== fonts)
736
+ this._layouts = new TextLayoutCache(fonts);
737
+ this._layouts.begin();
738
+ return this._layouts.fonts;
362
739
  }
363
740
 
364
- /** Bring the pipeline up to date for a width. */
741
+ /**
742
+ * Bring the pipeline up to date for a width — or, where that throws, leave
743
+ * the document blank. From a paint a throw is the application's end, for
744
+ * a document it did not write: a stack a thousand nested elements run
745
+ * down, or a limit of the text engine's or the server's that one more
746
+ * document finds. Reported once for each thing that went wrong, and tried
747
+ * again only when something changes, or the width does.
748
+ */
365
749
  private _prepare(width: number): void {
366
750
  const target = Math.max(1, Math.floor(width));
751
+ if (this._failedAt === target && this._stale === Stale.Nothing) return;
752
+ try {
753
+ this._update(target);
754
+ this._failedAt = -1;
755
+ } catch (error) {
756
+ this._fail(error, target);
757
+ }
758
+ }
759
+
760
+ /** The width the pipeline threw at, until something changes (`_prepare`). */
761
+ private _failedAt = -1;
762
+ private _failures: Set<string> | null = null;
763
+
764
+ private _fail(error: unknown, width: number): void {
765
+ this._tree = null;
766
+ this._stale = Stale.Nothing;
767
+ this._failedAt = width;
768
+ this._laidOutWidth = -1;
769
+ this._documentWidth = 0;
770
+ this._documentHeight = 0;
771
+ const message = String((error as { stack?: unknown })?.stack ?? error);
772
+ this._failures ??= new Set();
773
+ if (this._failures.has(message)) return;
774
+ this._failures.add(message);
775
+ const g = globalThis as {
776
+ process?: { env?: Record<string, string | undefined> };
777
+ console?: { error(message: string): void };
778
+ };
779
+ if (g.process?.env?.NODE_ENV === 'production') return;
780
+ g.console?.error(
781
+ '@react-x11/components: <Html> could not lay out or paint its ' +
782
+ `document, and leaves it blank.\n${message}`,
783
+ );
784
+ }
785
+
786
+ private _update(target: number): void {
367
787
  const props = this._props();
368
788
  if ((props.domRevision ?? 0) !== this._reportedDomRevision) {
369
789
  this._reportedDomRevision = props.domRevision ?? 0;
370
790
  this._source.touch();
791
+ this._updateBase();
371
792
  this._sweep();
372
793
  if (this._stale < Stale.Style) this._stale = Stale.Style;
373
794
  }
374
- if (this._stale === Stale.Nothing && this._laidOutWidth === target) return;
795
+ // A document that reads its viewport's height — a `vh`, a percentage
796
+ // height on the root, a box placed against the initial containing block
797
+ // — is laid out again when that height moves. One that reads none, which
798
+ // is most, goes on skipping layout when the window only grew taller.
799
+ const viewport = this._viewportHeight();
800
+ const viewportMoved =
801
+ viewport !== this._laidOutUnder && this._readsViewportHeight();
802
+ // the sizes other widths came to were read from what just changed
803
+ if (this._stale !== Stale.Nothing || viewportMoved) this._sizes.clear();
804
+ if (
805
+ this._stale === Stale.Nothing &&
806
+ this._laidOutWidth === target &&
807
+ !viewportMoved
808
+ ) {
809
+ return;
810
+ }
811
+ // Nothing but the viewport: the size core laid this element out at came
812
+ // from a layout under the old one, and nothing else will tell it so.
813
+ const onlyViewport =
814
+ this._stale === Stale.Nothing && this._laidOutWidth === target;
815
+ const wasWidth = this._documentWidth;
816
+ const wasHeight = this._documentHeight;
375
817
 
376
818
  if (this._stale >= Stale.Style || !this._cascade) {
377
819
  this._restyle(target);
@@ -387,17 +829,43 @@ export class HtmlViewNode extends Node {
387
829
  const cascade = this._cascade;
388
830
  if (!cascade) return;
389
831
  cascade.viewportWidth = target;
390
- cascade.viewportHeight = this._viewportHeight();
832
+ cascade.viewportHeight = viewport;
833
+ // A `vw` or a `vh` is a number by the time a style holds it, so the
834
+ // styles computed for another viewport are wrong for this one: built
835
+ // again, where some style reads the side that moved. A document that
836
+ // reads neither — most — goes on skipping the cascade on a resize.
837
+ if (
838
+ this._stale < Stale.Boxes &&
839
+ ((cascade.readsViewportWidth && this._styledWidth !== target) ||
840
+ (cascade.readsViewportHeight && this._styledHeight !== viewport))
841
+ ) {
842
+ this._stale = Stale.Boxes;
843
+ }
391
844
 
392
845
  if (this._stale >= Stale.Boxes || !this._tree) {
393
846
  const look = this._deviceLook();
394
- this._tree = buildBoxes(this._source.document, {
395
- cascade,
396
- scale: this._scale,
397
- imageSize: (el) => this._resources.imageSize(attr(el, 'src') ?? ''),
398
- controlSize: (el, kind, style) =>
399
- measureControl(el, kind, style, this._fonts(), look),
400
- });
847
+ const build = () =>
848
+ buildBoxes(this._source.document, {
849
+ cascade,
850
+ scale: this._scale,
851
+ imageSize: (el) => this._resources.imageSize(imageUrlOf(el) ?? ''),
852
+ urlSize: (url) => this._resources.imageSize(url),
853
+ controlSize: (el, kind, style) =>
854
+ measureControl(el, kind, style, this._fonts(), look),
855
+ });
856
+ this._tree = build();
857
+ this._styledWidth = target;
858
+ this._styledHeight = viewport;
859
+ this._requestBackgrounds(this._tree);
860
+ let again = this._contentImagesArrived(this._tree);
861
+ // the faces the styles just asked for, of the families the document
862
+ // loads itself — which, answered at once, change the styles asking
863
+ if (this._webFonts.request(this._tree.text)) {
864
+ this._layouts = null;
865
+ again = true;
866
+ }
867
+ if (again) this._tree = build();
868
+ this._warmFaces(this._tree);
401
869
  this._textPoints = null;
402
870
  this._pointsAreUnits = null;
403
871
  this._laidOutWidth = -1;
@@ -405,20 +873,47 @@ export class HtmlViewNode extends Node {
405
873
 
406
874
  if (
407
875
  this._tree &&
408
- (this._laidOutWidth !== target || this._stale >= Stale.Layout)
876
+ (this._laidOutWidth !== target ||
877
+ viewportMoved ||
878
+ this._stale >= Stale.Layout)
409
879
  ) {
410
880
  const result = layoutDocument(
411
881
  this._tree,
412
- this._fonts(),
882
+ this._layoutFonts(),
413
883
  target,
414
- this._viewportHeight(),
884
+ viewport,
415
885
  );
416
886
  this._documentWidth = result.width;
417
887
  this._documentHeight = result.height;
418
888
  this._laidOutWidth = target;
889
+ this._laidOutUnder = viewport;
890
+ this._layoutReadsViewport = result.readsViewportHeight;
891
+ this._sizes.delete(target);
892
+ this._sizes.set(target, { width: result.width, height: result.height });
893
+ if (this._sizes.size > SIZES_KEPT) {
894
+ this._sizes.delete(this._sizes.keys().next().value!);
895
+ }
419
896
  this._reportControls();
420
897
  }
421
898
  this._stale = Stale.Nothing;
899
+ // Read after core's layout pass — the viewport is the box around this
900
+ // one, whose new height only that pass decides — so it is a paint that
901
+ // finds it moved, and the next frame that measures this element again.
902
+ // Inside a measure the answer already is the new size.
903
+ if (
904
+ onlyViewport &&
905
+ !this._measuring &&
906
+ (this._documentWidth !== wasWidth || this._documentHeight !== wasHeight)
907
+ ) {
908
+ this.invalidateMeasure('content');
909
+ }
910
+ }
911
+
912
+ /** Whether the document as laid out reads its viewport's height. */
913
+ private _readsViewportHeight(): boolean {
914
+ return (
915
+ this._layoutReadsViewport || this._cascade?.readsViewportHeight === true
916
+ );
422
917
  }
423
918
 
424
919
  private _reportControls(): void {
@@ -437,6 +932,16 @@ export class HtmlViewNode extends Node {
437
932
  y: r.y / s,
438
933
  width: r.width / s,
439
934
  height: r.height / s,
935
+ ...(r.bare && {
936
+ bare: {
937
+ ...r.bare,
938
+ x: r.bare.x / s,
939
+ y: r.bare.y / s,
940
+ width: r.bare.width / s,
941
+ height: r.bare.height / s,
942
+ fontSize: r.bare.fontSize / s,
943
+ },
944
+ }),
440
945
  },
441
946
  );
442
947
  if (sameRects(rects, this._controls)) return;
@@ -457,13 +962,52 @@ export class HtmlViewNode extends Node {
457
962
  */
458
963
  override measureContent({ width }: MeasureConstraints): MeasuredSize {
459
964
  const offered = Number.isFinite(width) ? width : 800;
460
- this._prepare(offered);
965
+ this._measuring = true;
966
+ let size: { width: number; height: number };
967
+ try {
968
+ size = this._sizeAt(offered);
969
+ } finally {
970
+ this._measuring = false;
971
+ }
972
+ // core takes finite numbers only, and throws on any other from the
973
+ // layout — a document of lengths no clamp foresaw is none too tall
974
+ const finite = (n: number): number => (Number.isFinite(n) ? n : 0);
461
975
  return {
462
- width: Math.ceil(Math.min(this._documentWidth, offered)),
463
- height: Math.ceil(this._documentHeight),
976
+ width: wholePixels(Math.min(finite(size.width), offered)),
977
+ height: wholePixels(finite(size.height)),
464
978
  };
465
979
  }
466
980
 
981
+ /**
982
+ * The size the document comes to at `width`.
983
+ *
984
+ * Core asks a leaf for its height at the width it was measured at as well
985
+ * as at the one it has now (`probeHeightFloors`), to learn whether a
986
+ * relayout changed what the leaf needs — so every frame of a resize asks
987
+ * two widths, and the boxes hold one. Laying the document out at the old
988
+ * width to answer, and then at the new one again for the pass after, was
989
+ * three whole passes a frame. The size a width came to is kept instead,
990
+ * and answers until anything a layout reads changes: the source, the
991
+ * styles, a resource, the viewport height. Nothing but a size comes from
992
+ * it — paint and the text accessors read the boxes, which are only ever
993
+ * laid out for real (`_prepare`).
994
+ */
995
+ private _sizeAt(width: number): { width: number; height: number } {
996
+ const target = Math.max(1, Math.floor(width));
997
+ if (
998
+ target !== this._laidOutWidth &&
999
+ this._stale === Stale.Nothing &&
1000
+ (this._props().domRevision ?? 0) === this._reportedDomRevision &&
1001
+ (!this._readsViewportHeight() ||
1002
+ this._viewportHeight() === this._laidOutUnder)
1003
+ ) {
1004
+ const known = this._sizes.get(target);
1005
+ if (known) return known;
1006
+ }
1007
+ this._prepare(width);
1008
+ return { width: this._documentWidth, height: this._documentHeight };
1009
+ }
1010
+
467
1011
  override applyProps(
468
1012
  nextProps: Record<string, unknown>,
469
1013
  prevProps: Record<string, unknown>,
@@ -471,19 +1015,31 @@ export class HtmlViewNode extends Node {
471
1015
  super.applyProps(nextProps, prevProps);
472
1016
  const next = nextProps as unknown as HtmlViewProps;
473
1017
  const prev = prevProps as unknown as HtmlViewProps;
474
- if (next.look !== prev.look || next.stylesheet !== prev.stylesheet) {
1018
+ if (
1019
+ next.look !== prev.look ||
1020
+ next.stylesheet !== prev.stylesheet ||
1021
+ next.charset !== prev.charset
1022
+ ) {
475
1023
  this._invalidate(Stale.Style);
476
1024
  }
477
1025
  if (next.source !== prev.source || next.complete !== prev.complete) {
478
1026
  this._read();
479
1027
  } else if ((next.domRevision ?? 0) !== (prev.domRevision ?? 0)) {
480
1028
  this._invalidate(Stale.Style);
1029
+ } else if ((next.baseUrl ?? null) !== (prev.baseUrl ?? null)) {
1030
+ // the same document somewhere else: every URL in it is another one
1031
+ this._updateBase();
1032
+ this._sweep();
481
1033
  }
482
1034
  }
483
1035
 
484
1036
  override destroySubtree(): void {
485
1037
  this._resources.destroy();
1038
+ this._webFonts.destroy();
486
1039
  this._source.destroy();
1040
+ this._shadowCache?.destroy();
1041
+ this._shadowCache = null;
1042
+ this._sheetsRead = null;
487
1043
  this._tree = null;
488
1044
  this._cascade = null;
489
1045
  super.destroySubtree();
@@ -586,26 +1142,108 @@ export class HtmlViewNode extends Node {
586
1142
  // handler hand over — and are the one place the two units meet on the way
587
1143
  // in. `_toDocument` multiplies.
588
1144
 
589
- /** The link under a logical window point, if any. Not part of the
590
- * selection seam: core deliberately left hover and `cursorAt` out of
591
- * #291, so following a link stays this package's. */
1145
+ /** The link under a logical window point, if any — resolved against the
1146
+ * document's base where it has one. Not part of the selection seam: core
1147
+ * deliberately left hover and `cursorAt` out of #291, so following a
1148
+ * link stays this package's. */
592
1149
  hrefAtPoint(x: number, y: number): string | null {
593
1150
  const el = this.elementAtPoint(x, y);
594
1151
  let node: Element | null = el;
595
1152
  while (node) {
596
1153
  const href = attr(node, 'href');
597
- if (href && (tagOf(node) === 'a' || tagOf(node) === 'area')) return href;
1154
+ if (href && (tagOf(node) === 'a' || tagOf(node) === 'area')) {
1155
+ return this._urls.resolve(href);
1156
+ }
598
1157
  node = isElement(node.parent) ? node.parent : null;
599
1158
  }
600
1159
  return null;
601
1160
  }
602
1161
 
1162
+ /** The URL the document's relative URLs resolve against, or null where
1163
+ * it has none (`_updateBase`). */
1164
+ get documentBase(): string | null {
1165
+ return this._urls.base;
1166
+ }
1167
+
1168
+ /**
1169
+ * Where an element is, in document coordinates: logical pixels from this
1170
+ * element's top left, the space the scroll offset of a box around it is
1171
+ * in — so scrolling to a fragment is `scrollTo({ y: rect.y })`. A block's
1172
+ * border box. An inline element's fragments, from its first line to its
1173
+ * last, each across its text, the inline-blocks and inline boxes' edges
1174
+ * it holds, and its own padding and border where it starts or ends — its
1175
+ * border box across, as `getBoundingClientRect` is (CSSOM View 6.1) — and
1176
+ * as tall as its lines; or where its text would start when it has none,
1177
+ * as `<a name>` has none. Null for an element with no box — `display:
1178
+ * none`, or not in the document.
1179
+ */
1180
+ elementRect(element: Element): Rect | null {
1181
+ this._prepare(this.abs.width || 1);
1182
+ const tree = this._tree;
1183
+ if (!tree) return null;
1184
+ const box = boxFor(tree.root, element);
1185
+ if (!box) return null;
1186
+ let rect: Rect | null = null;
1187
+ if (box.kind !== 'inline' && box.kind !== 'text') {
1188
+ rect = { x: box.x, y: box.y, width: box.width, height: box.height };
1189
+ } else {
1190
+ const bands: Rect[] = [];
1191
+ if (box.subtreeTextEnd > box.subtreeTextStart) {
1192
+ collectBands(
1193
+ tree.root,
1194
+ box.subtreeTextStart,
1195
+ box.subtreeTextEnd,
1196
+ 0,
1197
+ 0,
1198
+ bands,
1199
+ );
1200
+ }
1201
+ if (box.kind === 'inline') fragmentReach(box, bands);
1202
+ for (const band of bands) rect = rect ? unionRect(rect, band) : band;
1203
+ if (!rect) {
1204
+ // an empty subtree's range is `[0, 0)`, wherever it stands: where
1205
+ // its text would be is where the text after it starts
1206
+ const caret = caretAt(tree.root, textAfter(tree.root, box));
1207
+ if (!caret) return null;
1208
+ rect = { x: caret.x, y: caret.y, width: 0, height: caret.height };
1209
+ }
1210
+ }
1211
+ const s = this._scale;
1212
+ return {
1213
+ x: rect.x / s,
1214
+ y: rect.y / s,
1215
+ width: rect.width / s,
1216
+ height: rect.height / s,
1217
+ };
1218
+ }
1219
+
603
1220
  /** The deepest element whose box contains a logical window point. */
604
1221
  elementAtPoint(x: number, y: number): Element | null {
605
1222
  const tree = this._tree;
606
1223
  if (!tree) return null;
607
1224
  const local = this._toDocument(x, y);
608
- return deepestAt(tree.root, local.x, local.y);
1225
+ return deepestAt(tree, local.x, local.y);
1226
+ }
1227
+
1228
+ /**
1229
+ * The cursor for a point of this element's, in device pixels: what core
1230
+ * asks a drawn element as the pointer moves over it (`cursorAt`,
1231
+ * react-x11#757). The `cursor` the document's styles give what is under
1232
+ * it — a link's `pointer`, the user-agent sheet's — and where they say
1233
+ * nothing, text's I-beam over text and the arrow elsewhere, as a browser
1234
+ * shows them. The arrow is named rather than left to null: null lets
1235
+ * core fall through to the element's `defaultCursor`, which is the
1236
+ * I-beam on a selectable surface, and every `<Html>` is one.
1237
+ */
1238
+ override cursorAt(x: number, y: number): string | null {
1239
+ const tree = this._tree;
1240
+ if (!tree) return null;
1241
+ const hit = { text: false };
1242
+ const el = deepestAt(tree, x - this.abs.x, y - this.abs.y, hit);
1243
+ const cursor = el ? tree.styles.get(el)?.style.cursor : null;
1244
+ // a keyword; a `url()` this cannot load falls back, as its list would
1245
+ if (cursor && cursor !== 'auto' && /^[a-z-]+$/.test(cursor)) return cursor;
1246
+ return hit.text ? 'text' : 'default';
609
1247
  }
610
1248
 
611
1249
  /**
@@ -624,20 +1262,207 @@ export class HtmlViewNode extends Node {
624
1262
  node = isElement(node.parent) ? node.parent : null;
625
1263
  }
626
1264
  if (sameChain(chain, this._hovered)) return false;
1265
+ const was = this._hovered;
627
1266
  this._hovered = chain;
628
1267
  cascade.setPointer({ hovered: new Set(chain), active: EMPTY_SET });
629
- this._invalidate(Stale.Boxes);
1268
+ this._restyleHover(was, chain);
630
1269
  return true;
631
1270
  }
632
1271
 
633
1272
  clearHover(): boolean {
634
1273
  if (!this._hovered.length) return false;
1274
+ const was = this._hovered;
635
1275
  this._hovered = [];
636
1276
  this._cascade?.setPointer({ hovered: new Set(), active: EMPTY_SET });
637
- this._invalidate(Stale.Boxes);
1277
+ this._restyleHover(was, []);
638
1278
  return true;
639
1279
  }
640
1280
 
1281
+ /** The hovered chain moved: restyle where it did, or the document. */
1282
+ private _restyleHover(was: readonly Element[], now: readonly Element[]) {
1283
+ const done = this._hoverInPlace(was, now);
1284
+ if (done === 'painted') this.invalidate(false, this, 'props');
1285
+ else if (done === false) this._invalidate(Stale.Boxes);
1286
+ }
1287
+
1288
+ /**
1289
+ * A pointer move restyled where it happened, when all it changed is ink.
1290
+ *
1291
+ * A move to another element is a change to the cascade, and the pipeline
1292
+ * answers one by building every box again and laying the document out:
1293
+ * 270 ms on X11 and twice that on Cocoa for a Wikipedia article, on every
1294
+ * link the pointer crossed. But the rules a page writes for `:hover`
1295
+ * nearly all change a colour, an underline, a background or a border's
1296
+ * colour — 77 of that article's 79 — and those move nothing. So:
1297
+ *
1298
+ * - Only an element whose hover state flipped and that a compound testing
1299
+ * the pointer could match can change (`Cascade.hoverTouches`), with its
1300
+ * subtree, and its later siblings where a sibling combinator follows.
1301
+ * A move between two paragraphs under `a:hover` touches nothing.
1302
+ * - Those are styled again from their parents. Where anything but ink
1303
+ * differs (`PAINT_ONLY`), or a box is not the element's own — an
1304
+ * anonymous or a pseudo-element box, a marker, a control — this is
1305
+ * not the move to take.
1306
+ * - The boxes take their new styles, and each layout of their text is
1307
+ * made again from the runs it was made from (`TextLayoutCache.inputsOf`)
1308
+ * with the new ink (`runFor`), where its geometry comes out the same.
1309
+ *
1310
+ * Everything is checked before anything is changed. 'none' where nothing
1311
+ * could change, 'painted' where it was restyled here, false where the
1312
+ * document has to be built again.
1313
+ */
1314
+ private _hoverInPlace(
1315
+ was: readonly Element[],
1316
+ now: readonly Element[],
1317
+ ): 'none' | 'painted' | false {
1318
+ const cascade = this._cascade;
1319
+ const tree = this._tree;
1320
+ const layouts = this._layouts;
1321
+ if (!cascade || !tree || !layouts || !cascade.hoverLocal) return false;
1322
+ if (this._stale !== Stale.Nothing || this._laidOutWidth < 0) return false;
1323
+
1324
+ const before = new Set(was);
1325
+ const after = new Set(now);
1326
+ const roots: Element[] = [];
1327
+ // and the elements a `:has()` testing the pointer may flip, which
1328
+ // are around the ones that changed; the chains are ancestor chains,
1329
+ // so the deepest that changed on each side is where to look from
1330
+ const flipped = (el: Element, other: Set<Element>): void => {
1331
+ if (other.has(el)) return;
1332
+ if (cascade.hoverTouches(el)) roots.push(el);
1333
+ };
1334
+ for (const el of was) flipped(el, after);
1335
+ for (const el of now) flipped(el, before);
1336
+ const deepest = (chain: readonly Element[], other: Set<Element>) => {
1337
+ const el = chain[0];
1338
+ if (el && !other.has(el)) cascade.hoverAnchors(el, roots);
1339
+ };
1340
+ deepest(was, after);
1341
+ deepest(now, before);
1342
+ if (!roots.length) return 'none';
1343
+
1344
+ // what may restyle: the roots' subtrees, which inherit from them and a
1345
+ // descendant combinator reaches, and their later siblings' too where a
1346
+ // sibling combinator follows a compound that tests the pointer
1347
+ const reach = new Set<Element>();
1348
+ const collect = (el: Element): boolean => {
1349
+ const stack: Element[] = [el];
1350
+ while (stack.length) {
1351
+ const at = stack.pop()!;
1352
+ if (reach.has(at)) continue;
1353
+ reach.add(at);
1354
+ if (reach.size > HOVER_RESTYLE_LIMIT) return false;
1355
+ for (const child of at.children)
1356
+ if (isElement(child)) stack.push(child);
1357
+ }
1358
+ return true;
1359
+ };
1360
+ for (const root of roots) {
1361
+ if (!collect(root)) return false;
1362
+ if (!cascade.hoverSiblings) continue;
1363
+ for (let s = root.nextSibling; s; s = s.nextSibling) {
1364
+ if (isElement(s) && !collect(s)) return false;
1365
+ }
1366
+ }
1367
+
1368
+ // styled again from their parents, a parent first
1369
+ const fresh = new Map<Element, ComputedStyle>();
1370
+ const changed = new Map<Element, ComputedStyle>();
1371
+ let refused = false;
1372
+ const styleOf = (el: Element): ComputedStyle | null => {
1373
+ const kept = tree.styles.get(el);
1374
+ if (!kept) return null;
1375
+ if (!reach.has(el)) return kept.style;
1376
+ const done = fresh.get(el);
1377
+ if (done) return done;
1378
+ // an element at the top of the document is styled from the box above
1379
+ // it all, as the builder styled it (`BoxBuilder.run`)
1380
+ const parent = isElement(el.parent) ? el.parent : null;
1381
+ const parentStyle = parent ? styleOf(parent) : tree.root.style;
1382
+ if (!parentStyle) {
1383
+ refused = true;
1384
+ return null;
1385
+ }
1386
+ const style = cascade.styleFor(el, parentStyle, kept.inFlex);
1387
+ fresh.set(el, style);
1388
+ const diff = inkOnly(kept.style, style);
1389
+ if (diff === false) refused = true;
1390
+ else if (diff) changed.set(el, style);
1391
+ return style;
1392
+ };
1393
+ for (const el of reach) {
1394
+ styleOf(el);
1395
+ if (refused) return false;
1396
+ }
1397
+ if (!changed.size) return 'none';
1398
+ for (const el of changed.keys()) {
1399
+ const tag = tagOf(el);
1400
+ // their backgrounds are the canvas's
1401
+ if (tag === 'html' || tag === 'body') return false;
1402
+ }
1403
+
1404
+ // the boxes: each changed element's own, and nothing that takes its
1405
+ // style from one without being it
1406
+ const fonts = layouts.fonts;
1407
+ const restyled: [Box, ComputedStyle][] = [];
1408
+ const redecorated: [Box, Box['decoration']][] = [];
1409
+ const walk: Box[] = [tree.root];
1410
+ while (walk.length) {
1411
+ const box = walk.pop()!;
1412
+ for (const child of box.children) walk.push(child);
1413
+ const owner = box.el ?? nearestElement(box);
1414
+ if (!owner) continue;
1415
+ const style = changed.get(owner);
1416
+ if (!style) continue;
1417
+ const kept = tree.styles.get(owner)!;
1418
+ // an anonymous box, a pseudo-element, a marker, a control: a style
1419
+ // derived from the element's, or drawn from it somewhere else
1420
+ if (!box.el || box.style !== kept.style) return false;
1421
+ if (box.marker || box.replaced !== 'none') return false;
1422
+ restyled.push([box, style]);
1423
+ if (box.kind === 'inline') {
1424
+ const decoration = inlineDecoration(fonts, box, style);
1425
+ if ((decoration === null) !== (box.decoration === null)) {
1426
+ // a rounded box with a background is laid out with its edges
1427
+ if (style.borderRadius.some((r) => r !== 0)) return false;
1428
+ }
1429
+ if (decoration !== box.decoration) redecorated.push([box, decoration]);
1430
+ }
1431
+ }
1432
+
1433
+ // the text: each layout holding a run of a changed element's, made
1434
+ // again with its new ink, where it comes out the same shape
1435
+ const relaid = new Map<TextLayoutLike, TextLayoutLike | null>();
1436
+ const texts: LineText[] = [];
1437
+ walk.push(tree.root);
1438
+ while (walk.length) {
1439
+ const box = walk.pop()!;
1440
+ for (const child of box.children) walk.push(child);
1441
+ if (!box.lines) continue;
1442
+ for (const line of box.lines) {
1443
+ for (const text of line.texts) {
1444
+ texts.push(text);
1445
+ if (relaid.has(text.layout)) continue;
1446
+ const next = reinked(text, layouts, changed, tree.styles);
1447
+ if (next === false) return false;
1448
+ relaid.set(text.layout, next);
1449
+ }
1450
+ }
1451
+ }
1452
+
1453
+ // and only now, all of it
1454
+ for (const [box, style] of restyled) box.style = style;
1455
+ for (const [box, decoration] of redecorated) box.decoration = decoration;
1456
+ for (const text of texts) {
1457
+ const next = relaid.get(text.layout);
1458
+ if (next) text.layout = next;
1459
+ }
1460
+ for (const [el, style] of changed) {
1461
+ tree.styles.set(el, { style, inFlex: tree.styles.get(el)!.inFlex });
1462
+ }
1463
+ return 'painted';
1464
+ }
1465
+
641
1466
  /** The document, for an application that wants to read or change it. */
642
1467
  get document(): Document {
643
1468
  return this._source.document;
@@ -652,6 +1477,7 @@ export class HtmlViewNode extends Node {
652
1477
  */
653
1478
  touchDocument(): void {
654
1479
  this._source.touch();
1480
+ this._updateBase();
655
1481
  this._sweep();
656
1482
  this._invalidate(Stale.Style);
657
1483
  }
@@ -708,23 +1534,133 @@ export class HtmlViewNode extends Node {
708
1534
  damage = { x: 0, y: 0, width: window.width, height: window.height };
709
1535
  }
710
1536
  }
1537
+ try {
1538
+ this._paint(ctx, tree, range, damage);
1539
+ } catch (error) {
1540
+ this._fail(error, this._laidOutWidth);
1541
+ }
1542
+ }
1543
+
1544
+ private _paint(
1545
+ ctx: Context2D,
1546
+ tree: BoxTree,
1547
+ range: { start: number; end: number } | null,
1548
+ damage: { x: number; y: number; width: number; height: number } | null,
1549
+ ): void {
711
1550
  paintDocument(ctx as PaintContext, tree, {
712
1551
  originX: this.abs.x,
713
1552
  originY: this.abs.y,
1553
+ // the root's background covers the whole element, not only the
1554
+ // document: an element grown past its content is canvas too
1555
+ canvas: this.abs,
714
1556
  scale: this._scale,
715
1557
  damage,
716
1558
  selection: range
717
1559
  ? { start: this._toUnits(range.start), end: this._toUnits(range.end) }
718
1560
  : null,
719
1561
  selectionColor: this.selectionColor,
720
- imageFor: (box) =>
721
- box.el ? this._resources.image(attr(box.el, 'src') ?? '') : null,
1562
+ imageFor: (box) => {
1563
+ const url = box.el ? imageUrlOf(box.el) : CONTENT_IMAGES.get(box);
1564
+ return url === undefined ? null : this._resources.image(url);
1565
+ },
1566
+ backgroundImageFor: (url) => {
1567
+ const image = this._resources.image(url);
1568
+ const size = image ? this._resources.imageSize(url) : null;
1569
+ return image && size ? { image, ...size } : null;
1570
+ },
1571
+ cached: (key, width, height, draw) =>
1572
+ (this._shadowCache ??= new SurfaceCache(this.app)).get(
1573
+ key,
1574
+ width,
1575
+ height,
1576
+ draw as (ctx: unknown) => void,
1577
+ ),
722
1578
  });
723
1579
  }
724
1580
  }
725
1581
 
726
1582
  const EMPTY_SET: ReadonlySet<Element> = new Set();
727
1583
 
1584
+ /** A sheet as the restyle reads it: its text, the encoding it was decoded
1585
+ * from, the element that brought it, and what its URLs resolve against. */
1586
+ interface SheetText {
1587
+ text: string;
1588
+ encoding?: string;
1589
+ element: Element;
1590
+ base: string | null;
1591
+ }
1592
+
1593
+ /** One `@import` a sheet read, and what it read as. */
1594
+ interface ImportRead {
1595
+ url: string;
1596
+ text: string | null;
1597
+ base: string | null;
1598
+ }
1599
+
1600
+ /** What a cascade was built from, to tell whether the next would be the
1601
+ * same one: the look, scale and fonts, each sheet's text, encoding and
1602
+ * base, the texts of everything each imports, and the host's own — and
1603
+ * the faces all of them declare. */
1604
+ interface SheetsRead {
1605
+ look: RootLook;
1606
+ scale: number;
1607
+ fonts: unknown;
1608
+ texts: string[];
1609
+ encodings: (string | undefined)[];
1610
+ bases: (string | null)[];
1611
+ imports: ImportRead[][];
1612
+ extras: string[];
1613
+ faces: DeclaredFace[];
1614
+ cascade: Cascade;
1615
+ }
1616
+
1617
+ /** The first box an element made, depth first. */
1618
+ function boxFor(root: Box, element: Element): Box | null {
1619
+ const stack: Box[] = [root];
1620
+ while (stack.length) {
1621
+ const box = stack.pop()!;
1622
+ if (box.el === element) return box;
1623
+ for (let i = box.children.length - 1; i >= 0; i -= 1) {
1624
+ stack.push(box.children[i]);
1625
+ }
1626
+ }
1627
+ return null;
1628
+ }
1629
+
1630
+ /** Where the document's text goes on after a box with none: the start of
1631
+ * the next text in document order, or the end of the last before it. */
1632
+ function textAfter(root: Box, target: Box): number {
1633
+ const stack: Box[] = [root];
1634
+ let passed = false;
1635
+ let before = 0;
1636
+ while (stack.length) {
1637
+ const box = stack.pop()!;
1638
+ if (box === target) {
1639
+ passed = true;
1640
+ continue;
1641
+ }
1642
+ if (box.kind === 'text' && box.textEnd > box.textStart) {
1643
+ if (passed) return box.textStart;
1644
+ before = box.textEnd;
1645
+ }
1646
+ for (let i = box.children.length - 1; i >= 0; i -= 1) {
1647
+ stack.push(box.children[i]);
1648
+ }
1649
+ }
1650
+ return before;
1651
+ }
1652
+
1653
+ function unionRect(a: Rect, b: Rect): Rect {
1654
+ const x = Math.min(a.x, b.x);
1655
+ const y = Math.min(a.y, b.y);
1656
+ return {
1657
+ x,
1658
+ y,
1659
+ width: Math.max(a.x + a.width, b.x + b.width) - x,
1660
+ height: Math.max(a.y + a.height, b.y + b.height) - y,
1661
+ };
1662
+ }
1663
+
728
1664
  function textOf(el: Element): string {
729
1665
  let out = '';
730
1666
  for (const child of el.children) {
@@ -749,7 +1685,8 @@ function sameRects(a: ControlRect[], b: ControlRect[]): boolean {
749
1685
  p.x !== q.x ||
750
1686
  p.y !== q.y ||
751
1687
  p.width !== q.width ||
752
- p.height !== q.height
1688
+ p.height !== q.height ||
1689
+ !sameBare(p.bare, q.bare)
753
1690
  ) {
754
1691
  return false;
755
1692
  }
@@ -757,6 +1694,19 @@ function sameRects(a: ControlRect[], b: ControlRect[]): boolean {
757
1694
  return true;
758
1695
  }
759
1696
 
1697
+ function sameBare(a?: BareField, b?: BareField): boolean {
1698
+ if (!a || !b) return a === b;
1699
+ return (
1700
+ a.x === b.x &&
1701
+ a.y === b.y &&
1702
+ a.width === b.width &&
1703
+ a.height === b.height &&
1704
+ a.color === b.color &&
1705
+ a.fontFamily === b.fontFamily &&
1706
+ a.fontSize === b.fontSize
1707
+ );
1708
+ }
1709
+
760
1710
  // --- walks over the laid-out tree -------------------------------------------
761
1711
 
762
1712
  /** Distance from a coordinate to an interval, zero inside it. */
@@ -797,7 +1747,7 @@ function nearestText(box: Box, x: number, y: number): number | null {
797
1747
  const units = offsets.length
798
1748
  ? offsets[Math.max(0, Math.min(local, offsets.length - 1))]
799
1749
  : local;
800
- best = text.textStart + Math.max(0, units - text.layoutStart);
1750
+ best = Math.max(text.textStart, documentOffsetOf(text, units));
801
1751
  }
802
1752
  };
803
1753
 
@@ -908,7 +1858,7 @@ function caretAt(
908
1858
  for (const text of line.texts) {
909
1859
  if (units < text.textStart || units > text.textEnd) continue;
910
1860
  const offsets = layoutOffsetsOf(text.layout);
911
- const layoutUnits = text.layoutStart + (units - text.textStart);
1861
+ const layoutUnits = layoutOffsetOf(text, units);
912
1862
  const caret = text.layout.caretPosition(
913
1863
  codePointAtOffset(offsets, layoutUnits),
914
1864
  );
@@ -930,6 +1880,62 @@ function caretAt(
930
1880
  /** Every band a document range covers, in window coordinates. The subtree
931
1881
  * text ranges prune the walk to the boxes the range actually crosses, so a
932
1882
  * drag's small range costs its own paragraphs; only Ctrl+A pays for all. */
1883
+ /**
1884
+ * What an inline box reaches on each line besides its text, as `out`
1885
+ * rectangles as tall as the line: its own padding and border where it
1886
+ * starts or ends there — its edge less its margin, which is outside it —
1887
+ * and the inline-blocks and inline boxes' edges inside it, whole. The rest
1888
+ * of its border box across, which its text alone missed: a padded link was
1889
+ * as wide as its words (`elementRect`).
1890
+ */
1891
+ function fragmentReach(box: Box, out: Rect[]): void {
1892
+ let block = box.parent;
1893
+ while (block && !block.lines) block = block.parent;
1894
+ if (!block?.lines) return;
1895
+ const inside = (b: Box | null): boolean => {
1896
+ for (let at = b; at && at !== block; at = at.parent) {
1897
+ if (at === box) return true;
1898
+ }
1899
+ return false;
1900
+ };
1901
+ const rtl = box.style.direction === 'rtl';
1902
+ for (const line of block.lines) {
1903
+ let left = Infinity;
1904
+ let right = -Infinity;
1905
+ for (const edge of line.edges ?? []) {
1906
+ if (edge.box === box) {
1907
+ // the element's `direction` says which side its start is on, as
1908
+ // paint reads it (`paintInlineBoxes`)
1909
+ const onLeft = (edge.side === 'start') !== rtl;
1910
+ left = Math.min(left, onLeft ? edge.x + box.marginLeft : edge.x);
1911
+ right = Math.max(
1912
+ right,
1913
+ onLeft ? edge.x + edge.width : edge.x + edge.width - box.marginRight,
1914
+ );
1915
+ } else if (inside(edge.box)) {
1916
+ left = Math.min(left, edge.x);
1917
+ right = Math.max(right, edge.x + edge.width);
1918
+ }
1919
+ }
1920
+ for (const placed of line.atomics) {
1921
+ if (!inside(placed.box)) continue;
1922
+ left = Math.min(left, placed.x - placed.box.marginLeft);
1923
+ right = Math.max(
1924
+ right,
1925
+ placed.x + placed.box.width + placed.box.marginRight,
1926
+ );
1927
+ }
1928
+ if (right > left) {
1929
+ out.push({
1930
+ x: left,
1931
+ y: line.y,
1932
+ width: right - left,
1933
+ height: line.height,
1934
+ });
1935
+ }
1936
+ }
1937
+ }
1938
+
933
1939
  function collectBands(
934
1940
  box: Box,
935
1941
  from: number,
@@ -966,8 +1972,8 @@ function collectBands(
966
1972
  text.layout,
967
1973
  natural,
968
1974
  offsets,
969
- text.layoutStart + (a - text.textStart),
970
- text.layoutStart + (b - text.textStart),
1975
+ layoutOffsetOf(text, a),
1976
+ layoutOffsetOf(text, b, true),
971
1977
  )) {
972
1978
  out.push({
973
1979
  x: dx + band.x + text.drawX,
@@ -987,10 +1993,118 @@ function collectBands(
987
1993
  }
988
1994
  }
989
1995
 
1996
+ /** The layers of a hit test, in the order CSS paints them within a
1997
+ * context (`deepestAt`). */
1998
+ const HIT_NEGATIVE = -1;
1999
+ const HIT_BLOCK = 0;
2000
+ const HIT_FLOAT = 1;
2001
+ const HIT_INLINE = 2;
2002
+ const HIT_POSITIONED = 3;
2003
+
990
2004
  /** The deepest element box containing a document-space point. */
991
- function deepestAt(box: Box, x: number, y: number): Element | null {
2005
+ function deepestAt(
2006
+ tree: BoxTree,
2007
+ x: number,
2008
+ y: number,
2009
+ /** Told whether what was found was found under text: a run's, rather
2010
+ * than a box's. */
2011
+ hit?: { text: boolean },
2012
+ ): Element | null {
2013
+ const box = tree.root;
992
2014
  let found: Element | null = box.el;
993
- const visit = (node: Box): void => {
2015
+ let foundKey: readonly number[] = [];
2016
+ let viaText = false;
2017
+ // What is under a point is what was painted there last (CSS 2.1 Appendix
2018
+ // E): within a context, an in-flow block's own box, then a float over it,
2019
+ // then a line's text and atomics, then a positioned box — and a float, an
2020
+ // atomic or a positioned box paints its own content whole in its turn,
2021
+ // those layers again inside it. So a hit carries the layers down to it,
2022
+ // and takes the place of the one before where those come after them, or
2023
+ // tie: an infobox floated out of one section hangs over the next, whose
2024
+ // own box took every link in it, and a skin that puts the article in a
2025
+ // `position: relative` box put all of it in one layer. Positioned boxes
2026
+ // are painted in `z-index` order and then the document's (`byZIndex`),
2027
+ // the negative ones under the flow: the Zen Garden's `›` has
2028
+ // `z-index: 3` over the bar the "View All Designs" link fills after it.
2029
+ const take = (el: Element, key: readonly number[], text: boolean) => {
2030
+ if (compareKeys(key, foundKey) < 0) return;
2031
+ found = el;
2032
+ foundKey = key;
2033
+ viaText = text;
2034
+ };
2035
+ // A box is walked into wherever it draws — its reach, overflow and all
2036
+ // (`computePaintBounds`) — and named only where its own rectangle is. A
2037
+ // page's `html, body { height: 100% }` is one viewport tall and its
2038
+ // article overflows it, and a walk that went no further than a box's own
2039
+ // rectangle found nothing below the first screen: once Wikipedia was
2040
+ // scrolled, no link lit up. One whose reach is not known yet — before its
2041
+ // first paint — is walked into at its rectangle, as before.
2042
+ //
2043
+ // A box that clips what overflows it hides what it holds past its edge —
2044
+ // but not the positioned boxes whose containing block is outside it (CSS
2045
+ // 2.1 11.1.1), which paint puts off until its clip ends
2046
+ // (`paintPositioned`). So past the edge the walk goes on only into a box
2047
+ // that holds positioned ones, carrying the clips the point is outside
2048
+ // of, and names nothing until a positioned box escapes them all: the
2049
+ // Zen Garden's archive links are absolute `<li>`s in an `overflow:
2050
+ // hidden` list with no height of its own, and not one of them could be
2051
+ // hovered or pressed.
2052
+ const enter = (
2053
+ child: Box,
2054
+ context: readonly number[],
2055
+ clipped: readonly Box[],
2056
+ ): void => {
2057
+ const style = child.style;
2058
+ if (
2059
+ style.position !== 'static' ||
2060
+ (child.parent?.kind === 'flex' && typeof style.zIndex === 'number')
2061
+ ) {
2062
+ // a flex item with a `z-index` is layered unpositioned (`layered`)
2063
+ const z = style.zIndex === 'auto' ? 0 : style.zIndex;
2064
+ context = [...context, z < 0 ? HIT_NEGATIVE : HIT_POSITIONED, z];
2065
+ } else if (style.float !== 'none') context = [...context, HIT_FLOAT];
2066
+ if (clipped.length !== 0 && child.outOfFlow) {
2067
+ const containing = containingBlockOf(child);
2068
+ clipped = containing
2069
+ ? clipped.filter((clip) => holds(clip, containing))
2070
+ : [];
2071
+ }
2072
+ const inside =
2073
+ x >= child.x &&
2074
+ x < child.x + child.width &&
2075
+ y >= child.y &&
2076
+ y < child.y + child.height;
2077
+ const own = inside && hasRect(child);
2078
+ if (!own) {
2079
+ const reach = child.boundsY;
2080
+ const known =
2081
+ Number.isFinite(reach) &&
2082
+ (child.boundsWidth > 0 || child.boundsHeight > 0);
2083
+ if (!known) {
2084
+ if (!inside) return;
2085
+ } else if (
2086
+ x < child.boundsX ||
2087
+ x >= child.boundsX + child.boundsWidth ||
2088
+ y < reach ||
2089
+ y >= reach + child.boundsHeight
2090
+ ) {
2091
+ return;
2092
+ }
2093
+ if (clipsOverflow(child)) {
2094
+ if (!holdsAbsolute(child)) return;
2095
+ clipped = [...clipped, child];
2096
+ }
2097
+ }
2098
+ if (own && child.el && clipped.length === 0) {
2099
+ take(child.el, [...context, HIT_BLOCK], false);
2100
+ }
2101
+ visit(child, context, clipped);
2102
+ };
2103
+ const visit = (
2104
+ node: Box,
2105
+ context: readonly number[],
2106
+ clipped: readonly Box[],
2107
+ ): void => {
994
2108
  // The paint index answers a point query too — the wide level of a flat
995
2109
  // document is the root's child list, and a hit test that walked all of
996
2110
  // it would run per pointer move once hover is in the picture.
@@ -999,55 +2113,37 @@ function deepestAt(box: Box, x: number, y: number): Element | null {
999
2113
  : node.children;
1000
2114
  for (const child of candidates) {
1001
2115
  if (child.kind === 'text' || child.kind === 'break') continue;
1002
- if (
1003
- x >= child.x &&
1004
- x < child.x + child.width &&
1005
- y >= child.y &&
1006
- y < child.y + child.height
1007
- ) {
1008
- if (child.el) found = child.el;
1009
- visit(child);
1010
- }
2116
+ enter(child, context, clipped);
1011
2117
  }
1012
2118
  if (node.paintIndex && node.positionedPaint) {
1013
2119
  for (const child of node.positionedPaint) {
1014
- if (
1015
- x >= child.x &&
1016
- x < child.x + child.width &&
1017
- y >= child.y &&
1018
- y < child.y + child.height
1019
- ) {
1020
- if (child.el) found = child.el;
1021
- visit(child);
1022
- }
2120
+ enter(child, context, clipped);
1023
2121
  }
1024
2122
  }
1025
2123
  if (node.lines) {
1026
2124
  for (const line of node.lines) {
1027
2125
  for (const placed of line.atomics) {
1028
- if (
1029
- x >= placed.box.x &&
1030
- x < placed.box.x + placed.box.width &&
1031
- y >= placed.box.y &&
1032
- y < placed.box.y + placed.box.height
1033
- ) {
1034
- if (placed.box.el) found = placed.box.el;
1035
- visit(placed.box);
1036
- }
2126
+ enter(placed.box, [...context, HIT_INLINE], clipped);
1037
2127
  }
1038
2128
  // An inline box has no box of its own — its extent is the runs on
1039
2129
  // this line — so the element under a point inside a paragraph is
1040
- // found from the run rather than from a rectangle.
1041
- if (y >= line.y && y < line.y + line.height) {
2130
+ // found from the run rather than from a rectangle: from where its
2131
+ // text sits in the document, whose text boxes know their element.
2132
+ // Not from the run itself, whose layout may be one an earlier parse
2133
+ // made (`TextLayoutCache`), and which an engine may hand back with
2134
+ // nothing on it but its extent.
2135
+ if (clipped.length === 0 && y >= line.y && y < line.y + line.height) {
1042
2136
  for (const text of line.texts) {
1043
2137
  const natural = text.layout.lines[text.layoutLine];
1044
2138
  if (!natural) continue;
1045
2139
  for (const run of natural.runs) {
1046
2140
  const left = text.drawX + natural.x + run.x;
1047
2141
  if (x >= left && x < left + run.width) {
1048
- const owner = (run.span as { element?: Element } | undefined)
1049
- ?.element;
1050
- if (owner) found = owner;
2142
+ const owner = ownerOf(
2143
+ tree.textBoxes,
2144
+ text.spans.documentAt(run.start),
2145
+ );
2146
+ if (owner) take(owner, [...context, HIT_INLINE], true);
1051
2147
  }
1052
2148
  }
1053
2149
  }
@@ -1055,8 +2151,275 @@ function deepestAt(box: Box, x: number, y: number): Element | null {
1055
2151
  }
1056
2152
  }
1057
2153
  };
1058
- visit(box);
2154
+ visit(box, [], []);
2155
+ if (hit) hit.text = viaText;
1059
2156
  return found;
1060
2157
  }
1061
2158
 
2159
+ /** Paint order between two hits' layers, outermost first: negative where
2160
+ * `a` was painted under `b`. */
2161
+ function compareKeys(a: readonly number[], b: readonly number[]): number {
2162
+ const n = Math.min(a.length, b.length);
2163
+ for (let i = 0; i < n; i += 1) if (a[i] !== b[i]) return a[i] - b[i];
2164
+ return a.length - b.length;
2165
+ }
2166
+
2167
+ // --- a pointer move restyled where it happened (`_hoverInPlace`) -----------
2168
+
2169
+ /** How many elements a pointer move restyles in place before the document
2170
+ * is built again instead: a link's subtree is a handful, and a compound
2171
+ * that matches a container reaches everything in it. */
2172
+ const HOVER_RESTYLE_LIMIT = 300;
2173
+
2174
+ /**
2175
+ * The computed properties a pointer move may change in place: ink, which
2176
+ * moves nothing. A background colour is drawn inside the box it colours
2177
+ * and a border's colour on the border it has; `box-shadow`, `text-shadow`
2178
+ * and an outline's width reach past it, where the paint index has already
2179
+ * looked, and a background image has to be fetched.
2180
+ */
2181
+ const PAINT_ONLY = new Set([
2182
+ 'color',
2183
+ 'textFillColor',
2184
+ 'cursor',
2185
+ 'backgroundColor',
2186
+ 'borderTopColor',
2187
+ 'borderRightColor',
2188
+ 'borderBottomColor',
2189
+ 'borderLeftColor',
2190
+ 'outlineColor',
2191
+ 'textDecorationLine',
2192
+ 'textDecorationColor',
2193
+ 'textDecorationStyle',
2194
+ 'textDecorationThickness',
2195
+ 'textUnderlineOffset',
2196
+ 'underline',
2197
+ 'underlineStyle',
2198
+ 'underlineThickness',
2199
+ 'underlineOffset',
2200
+ 'lineThrough',
2201
+ ]);
2202
+
2203
+ /** The fields of a run its ink is: what `runFor` takes from `PAINT_ONLY`. */
2204
+ const INK_FIELDS = [
2205
+ 'color',
2206
+ 'underline',
2207
+ 'underlineStyle',
2208
+ 'underlineOffset',
2209
+ 'underlineThickness',
2210
+ 'strike',
2211
+ ] as const;
2212
+
2213
+ /** The fields of a run its shape is, which ink never changes. */
2214
+ const FACE_FIELDS = [
2215
+ 'family',
2216
+ 'size',
2217
+ 'weight',
2218
+ 'style',
2219
+ 'letterSpacing',
2220
+ 'features',
2221
+ ] as const;
2222
+
2223
+ /** Whether two styles differ in ink alone: true where they do, null where
2224
+ * they do not differ, false where something else does. Custom properties
2225
+ * are read through the properties that use them. */
2226
+ function inkOnly(was: ComputedStyle, now: ComputedStyle): boolean | null {
2227
+ const a = was as unknown as Record<string, unknown>;
2228
+ const b = now as unknown as Record<string, unknown>;
2229
+ let ink = false;
2230
+ for (const key in b) {
2231
+ if (key === 'custom' || sameValue(a[key], b[key])) continue;
2232
+ if (!PAINT_ONLY.has(key)) return false;
2233
+ ink = true;
2234
+ }
2235
+ return ink ? true : null;
2236
+ }
2237
+
2238
+ /** Structural equality for a computed value: a number, a string, or the
2239
+ * plain objects and arrays a length or a shadow list is. */
2240
+ function sameValue(x: unknown, y: unknown): boolean {
2241
+ if (x === y) return true;
2242
+ if (typeof x === 'number' && typeof y === 'number') {
2243
+ return Number.isNaN(x) && Number.isNaN(y);
2244
+ }
2245
+ if (!x || !y || typeof x !== 'object' || typeof y !== 'object') return false;
2246
+ if (Array.isArray(x) !== Array.isArray(y)) return false;
2247
+ const a = x as Record<string, unknown>;
2248
+ const b = y as Record<string, unknown>;
2249
+ const keys = Object.keys(a);
2250
+ if (keys.length !== Object.keys(b).length) return false;
2251
+ for (const key of keys) if (!sameValue(a[key], b[key])) return false;
2252
+ return true;
2253
+ }
2254
+
2255
+ /** The element an anonymous box takes its style from: its nearest
2256
+ * ancestor's that has one. */
2257
+ function nearestElement(box: Box): Element | null {
2258
+ for (let at = box.parent; at; at = at.parent) if (at.el) return at.el;
2259
+ return null;
2260
+ }
2261
+
2262
+ /**
2263
+ * `text`'s layout made again with the ink of the elements a pointer move
2264
+ * restyled: from the runs it was made from, each of a changed element's
2265
+ * given the ink its new style makes (`runFor`) and nothing else. Null where
2266
+ * none of its runs is theirs; false where that cannot be told — a run this
2267
+ * cannot place in the document, one some other pass inked (a first line's),
2268
+ * or a layout that comes out another shape.
2269
+ */
2270
+ function reinked(
2271
+ text: LineText,
2272
+ layouts: TextLayoutCache,
2273
+ changed: ReadonlyMap<Element, ComputedStyle>,
2274
+ styles: BoxTree['styles'],
2275
+ ): TextLayoutLike | null | false {
2276
+ const spans = text.spans;
2277
+ const ownerAt = (offset: number): Element | null =>
2278
+ spans.boxAt ? (spans.boxAt(offset)?.el ?? null) : null;
2279
+ const inputs = layouts.inputsOf(text.layout);
2280
+ if (!inputs || !spans.boxAt) {
2281
+ // not one this could make again: none of its text may be theirs
2282
+ for (const line of text.layout.lines) {
2283
+ for (const run of line.runs) {
2284
+ const owner = spans.boxAt ? ownerAt(run.start) : null;
2285
+ if (!spans.boxAt || (owner && changed.has(owner))) return false;
2286
+ }
2287
+ }
2288
+ return null;
2289
+ }
2290
+ let runs: TextRun[] | null = null;
2291
+ let offset = 0;
2292
+ const content = inputs.content;
2293
+ for (let i = 0; i < content.length; i += 1) {
2294
+ const run = content[i];
2295
+ const length = run.text.length;
2296
+ const owner = length ? ownerAt(offset) : null;
2297
+ const style = owner ? changed.get(owner) : undefined;
2298
+ if (owner && style) {
2299
+ if (ownerAt(offset + length - 1) !== owner) return false;
2300
+ const was = runFor(run.text, styles.get(owner)!.style);
2301
+ const now = runFor(run.text, style);
2302
+ for (const f of FACE_FIELDS) if (!sameValue(was[f], now[f])) return false;
2303
+ let next: Record<string, unknown> | null = null;
2304
+ for (const f of INK_FIELDS) {
2305
+ // a run that is not what its style made — a first line's colour
2306
+ if (!sameValue(run[f], was[f])) return false;
2307
+ if (sameValue(was[f], now[f])) continue;
2308
+ next ??= { ...run };
2309
+ if (now[f] === undefined) delete next[f];
2310
+ else next[f] = now[f];
2311
+ }
2312
+ if (next) {
2313
+ runs ??= content.slice();
2314
+ runs[i] = next as unknown as TextRun;
2315
+ }
2316
+ }
2317
+ offset += length;
2318
+ }
2319
+ if (!runs) return null;
2320
+ const layout = layouts.fonts.layout(runs, inputs.style, inputs.options);
2321
+ return sameShape(text.layout, layout) ? layout : false;
2322
+ }
2323
+
2324
+ /** Whether two layouts put the same text in the same places: ink aside,
2325
+ * the same layout. */
2326
+ function sameShape(a: TextLayoutLike, b: TextLayoutLike): boolean {
2327
+ if (a.width !== b.width || a.height !== b.height) return false;
2328
+ if (a.lines.length !== b.lines.length) return false;
2329
+ for (let i = 0; i < a.lines.length; i += 1) {
2330
+ const x = a.lines[i];
2331
+ const y = b.lines[i];
2332
+ if (
2333
+ x.x !== y.x ||
2334
+ x.y !== y.y ||
2335
+ x.width !== y.width ||
2336
+ x.height !== y.height ||
2337
+ x.baseline !== y.baseline ||
2338
+ x.ascent !== y.ascent ||
2339
+ x.descent !== y.descent ||
2340
+ x.start !== y.start ||
2341
+ x.end !== y.end ||
2342
+ x.runs.length !== y.runs.length
2343
+ ) {
2344
+ return false;
2345
+ }
2346
+ for (let j = 0; j < x.runs.length; j += 1) {
2347
+ const r = x.runs[j];
2348
+ const t = y.runs[j];
2349
+ if (
2350
+ r.x !== t.x ||
2351
+ r.width !== t.width ||
2352
+ r.start !== t.start ||
2353
+ r.end !== t.end
2354
+ ) {
2355
+ return false;
2356
+ }
2357
+ }
2358
+ }
2359
+ return true;
2360
+ }
2361
+
2362
+ /** The element whose text holds a document index: the text box around it,
2363
+ * found by bisecting the boxes in document order. */
2364
+ function ownerOf(boxes: readonly Box[], index: number): Element | null {
2365
+ let lo = 0;
2366
+ let hi = boxes.length - 1;
2367
+ while (lo < hi) {
2368
+ const mid = (lo + hi + 1) >> 1;
2369
+ if (boxes[mid].textStart <= index) lo = mid;
2370
+ else hi = mid - 1;
2371
+ }
2372
+ const box = boxes[lo];
2373
+ return box && index >= box.textStart && index < box.textEnd ? box.el : null;
2374
+ }
2375
+
1062
2376
  export type { ControlRect, ReplacedKind, ResourceRequest, ResourceResult };
2377
+
2378
+ /** The advance of a font's "0" at a size, laid out: null where the engine
2379
+ * cannot lay it out. */
2380
+ function zeroWidthOf(
2381
+ fonts: FontsLike,
2382
+ family: string,
2383
+ size: number,
2384
+ ): number | null {
2385
+ try {
2386
+ return fonts.layout([{ text: '0', family, size }], { family, size }, {})
2387
+ .width;
2388
+ } catch {
2389
+ return null;
2390
+ }
2391
+ }
2392
+
2393
+ /** A font's own line height at a size, as the engine reports it: null
2394
+ * where it cannot say. */
2395
+ function normalLineOf(
2396
+ fonts: FontsLike,
2397
+ family: string,
2398
+ size: number,
2399
+ ): number | null {
2400
+ try {
2401
+ const line = fonts.match(family, { size }).metrics(size).lineHeight;
2402
+ return line > 0 ? line : null;
2403
+ } catch {
2404
+ return null;
2405
+ }
2406
+ }
2407
+
2408
+ /** A font's x-height at a size, as the engine reports it: null where the
2409
+ * face states none, or the engine does not say. */
2410
+ function xHeightOf(
2411
+ fonts: FontsLike,
2412
+ family: string,
2413
+ size: number,
2414
+ ): number | null {
2415
+ try {
2416
+ const metrics = fonts.match(family, { size }).metrics(size) as {
2417
+ xHeight?: number | null;
2418
+ };
2419
+ const x = metrics.xHeight;
2420
+ // NaN where the face states none, which is a number
2421
+ return typeof x === 'number' && x > 0 ? x : null;
2422
+ } catch {
2423
+ return null;
2424
+ }
2425
+ }