@moondraft/import 0.0.0-stage → 0.2.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 (337) hide show
  1. package/CHANGELOG.md +36 -0
  2. package/LICENSE +34 -0
  3. package/README.md +36 -2
  4. package/SOURCE.md +394 -0
  5. package/dist/adapter/convert.d.ts +144 -0
  6. package/dist/adapter/convert.js +2811 -0
  7. package/dist/adapter/figma-descriptions.d.ts +7 -0
  8. package/dist/adapter/figma-descriptions.js +34 -0
  9. package/dist/adapter/figma-grids.d.ts +12 -0
  10. package/dist/adapter/figma-grids.js +175 -0
  11. package/dist/adapter/figma-handoff.d.ts +12 -0
  12. package/dist/adapter/figma-handoff.js +167 -0
  13. package/dist/adapter/figma-hyperlinks.d.ts +13 -0
  14. package/dist/adapter/figma-hyperlinks.js +77 -0
  15. package/dist/adapter/figma.d.ts +12 -0
  16. package/dist/adapter/figma.js +40 -0
  17. package/dist/adapter/file-components.d.ts +20 -0
  18. package/dist/adapter/file-components.js +352 -0
  19. package/dist/adapter/file-resources.d.ts +29 -0
  20. package/dist/adapter/file-resources.js +851 -0
  21. package/dist/adapter/image-crop.d.ts +18 -0
  22. package/dist/adapter/image-crop.js +30 -0
  23. package/dist/adapter/index.d.ts +6 -0
  24. package/dist/adapter/index.js +9 -0
  25. package/dist/adapter/link.d.ts +49 -0
  26. package/dist/adapter/link.js +892 -0
  27. package/dist/adapter/payload.d.ts +31 -0
  28. package/dist/adapter/payload.js +78 -0
  29. package/dist/adapter/shapes.d.ts +31 -0
  30. package/dist/adapter/shapes.js +75 -0
  31. package/dist/adapter/variables.d.ts +45 -0
  32. package/dist/adapter/variables.js +106 -0
  33. package/dist/figma-file.d.ts +38 -0
  34. package/dist/figma-file.js +97 -0
  35. package/dist/figma-worker.d.ts +11 -0
  36. package/dist/figma-worker.js +12 -0
  37. package/dist/figma.d.ts +26 -0
  38. package/dist/figma.js +230 -0
  39. package/dist/index.d.ts +29 -0
  40. package/dist/index.js +50 -0
  41. package/dist/report.d.ts +42 -0
  42. package/dist/report.js +142 -0
  43. package/dist/serialize.d.ts +7 -0
  44. package/dist/serialize.js +79 -0
  45. package/dist/svg.d.ts +33 -0
  46. package/dist/svg.js +44 -0
  47. package/dist/syntax.d.ts +8 -0
  48. package/dist/syntax.js +19 -0
  49. package/dist/taco/figma-import/clipboard.d.ts +25 -0
  50. package/dist/taco/figma-import/clipboard.js +87 -0
  51. package/dist/taco/figma-import/clipboardSize.d.ts +31 -0
  52. package/dist/taco/figma-import/clipboardSize.js +107 -0
  53. package/dist/taco/figma-import/container/compression.d.ts +9 -0
  54. package/dist/taco/figma-import/container/compression.js +174 -0
  55. package/dist/taco/figma-import/container/error.d.ts +15 -0
  56. package/dist/taco/figma-import/container/error.js +19 -0
  57. package/dist/taco/figma-import/container/kiwiContainer.d.ts +41 -0
  58. package/dist/taco/figma-import/container/kiwiContainer.js +61 -0
  59. package/dist/taco/figma-import/container/limits.d.ts +22 -0
  60. package/dist/taco/figma-import/container/limits.js +22 -0
  61. package/dist/taco/figma-import/container/zip.d.ts +14 -0
  62. package/dist/taco/figma-import/container/zip.js +154 -0
  63. package/dist/taco/figma-import/figFile.d.ts +34 -0
  64. package/dist/taco/figma-import/figFile.js +91 -0
  65. package/dist/taco/figma-import/figFileDocument.d.ts +25 -0
  66. package/dist/taco/figma-import/figFileDocument.js +100 -0
  67. package/dist/taco/figma-import/import/blobs.d.ts +10 -0
  68. package/dist/taco/figma-import/import/blobs.js +23 -0
  69. package/dist/taco/figma-import/import/carry.d.ts +24 -0
  70. package/dist/taco/figma-import/import/carry.js +66 -0
  71. package/dist/taco/figma-import/import/componentProps.d.ts +90 -0
  72. package/dist/taco/figma-import/import/componentProps.js +329 -0
  73. package/dist/taco/figma-import/import/componentSets.d.ts +85 -0
  74. package/dist/taco/figma-import/import/componentSets.js +647 -0
  75. package/dist/taco/figma-import/import/converters/effects.d.ts +31 -0
  76. package/dist/taco/figma-import/import/converters/effects.js +127 -0
  77. package/dist/taco/figma-import/import/converters/ellipse.d.ts +7 -0
  78. package/dist/taco/figma-import/import/converters/ellipse.js +54 -0
  79. package/dist/taco/figma-import/import/converters/exportSettings.d.ts +4 -0
  80. package/dist/taco/figma-import/import/converters/exportSettings.js +49 -0
  81. package/dist/taco/figma-import/import/converters/frame.d.ts +21 -0
  82. package/dist/taco/figma-import/import/converters/frame.js +63 -0
  83. package/dist/taco/figma-import/import/converters/gradient.d.ts +74 -0
  84. package/dist/taco/figma-import/import/converters/gradient.js +108 -0
  85. package/dist/taco/figma-import/import/converters/group.d.ts +19 -0
  86. package/dist/taco/figma-import/import/converters/group.js +30 -0
  87. package/dist/taco/figma-import/import/converters/image.d.ts +13 -0
  88. package/dist/taco/figma-import/import/converters/image.js +94 -0
  89. package/dist/taco/figma-import/import/converters/layout.d.ts +33 -0
  90. package/dist/taco/figma-import/import/converters/layout.js +187 -0
  91. package/dist/taco/figma-import/import/converters/line.d.ts +11 -0
  92. package/dist/taco/figma-import/import/converters/line.js +40 -0
  93. package/dist/taco/figma-import/import/converters/rectangle.d.ts +3 -0
  94. package/dist/taco/figma-import/import/converters/rectangle.js +11 -0
  95. package/dist/taco/figma-import/import/converters/section.d.ts +8 -0
  96. package/dist/taco/figma-import/import/converters/section.js +29 -0
  97. package/dist/taco/figma-import/import/converters/shared.d.ts +71 -0
  98. package/dist/taco/figma-import/import/converters/shared.js +372 -0
  99. package/dist/taco/figma-import/import/converters/text.d.ts +54 -0
  100. package/dist/taco/figma-import/import/converters/text.js +707 -0
  101. package/dist/taco/figma-import/import/converters/vector.d.ts +105 -0
  102. package/dist/taco/figma-import/import/converters/vector.js +609 -0
  103. package/dist/taco/figma-import/import/converters/vectorNetwork.d.ts +114 -0
  104. package/dist/taco/figma-import/import/converters/vectorNetwork.js +503 -0
  105. package/dist/taco/figma-import/import/fields.d.ts +15 -0
  106. package/dist/taco/figma-import/import/fields.js +29 -0
  107. package/dist/taco/figma-import/import/instances.d.ts +184 -0
  108. package/dist/taco/figma-import/import/instances.js +1530 -0
  109. package/dist/taco/figma-import/import/nestingCycles.d.ts +86 -0
  110. package/dist/taco/figma-import/import/nestingCycles.js +488 -0
  111. package/dist/taco/figma-import/import/slots.d.ts +56 -0
  112. package/dist/taco/figma-import/import/slots.js +96 -0
  113. package/dist/taco/figma-import/import/styles.d.ts +48 -0
  114. package/dist/taco/figma-import/import/styles.js +48 -0
  115. package/dist/taco/figma-import/import/tree.d.ts +66 -0
  116. package/dist/taco/figma-import/import/tree.js +194 -0
  117. package/dist/taco/figma-import/import/types.d.ts +232 -0
  118. package/dist/taco/figma-import/import/types.js +33 -0
  119. package/dist/taco/figma-import/import/variables.d.ts +137 -0
  120. package/dist/taco/figma-import/import/variables.js +769 -0
  121. package/dist/taco/figma-import/import/variantTrees.d.ts +20 -0
  122. package/dist/taco/figma-import/import/variantTrees.js +520 -0
  123. package/dist/taco/figma-import/import/walk.d.ts +207 -0
  124. package/dist/taco/figma-import/import/walk.js +1464 -0
  125. package/dist/taco/figma-import/index.d.ts +22 -0
  126. package/dist/taco/figma-import/index.js +13 -0
  127. package/dist/taco/figma-import/kiwi/byteReader.d.ts +37 -0
  128. package/dist/taco/figma-import/kiwi/byteReader.js +151 -0
  129. package/dist/taco/figma-import/kiwi/error.d.ts +14 -0
  130. package/dist/taco/figma-import/kiwi/error.js +29 -0
  131. package/dist/taco/figma-import/kiwi/limits.d.ts +18 -0
  132. package/dist/taco/figma-import/kiwi/limits.js +18 -0
  133. package/dist/taco/figma-import/kiwi/message.d.ts +12 -0
  134. package/dist/taco/figma-import/kiwi/message.js +147 -0
  135. package/dist/taco/figma-import/kiwi/schema.d.ts +42 -0
  136. package/dist/taco/figma-import/kiwi/schema.js +206 -0
  137. package/dist/taco/figma-import/kiwi/types.d.ts +46 -0
  138. package/dist/taco/figma-import/kiwi/types.js +18 -0
  139. package/dist/taco/model/agent-hidden-fields.d.ts +20 -0
  140. package/dist/taco/model/agent-hidden-fields.js +29 -0
  141. package/dist/taco/model/asset-users.d.ts +23 -0
  142. package/dist/taco/model/asset-users.js +128 -0
  143. package/dist/taco/model/auto-layout-lines.d.ts +32 -0
  144. package/dist/taco/model/auto-layout-lines.js +70 -0
  145. package/dist/taco/model/codec.d.ts +444 -0
  146. package/dist/taco/model/codec.js +5665 -0
  147. package/dist/taco/model/color.d.ts +11 -0
  148. package/dist/taco/model/color.js +61 -0
  149. package/dist/taco/model/comment-operations.d.ts +102 -0
  150. package/dist/taco/model/comment-operations.js +227 -0
  151. package/dist/taco/model/component-evaluation.d.ts +96 -0
  152. package/dist/taco/model/component-evaluation.js +555 -0
  153. package/dist/taco/model/component-expressions.d.ts +242 -0
  154. package/dist/taco/model/component-expressions.js +1082 -0
  155. package/dist/taco/model/component-properties.d.ts +111 -0
  156. package/dist/taco/model/component-properties.js +339 -0
  157. package/dist/taco/model/component-samples.d.ts +83 -0
  158. package/dist/taco/model/component-samples.js +290 -0
  159. package/dist/taco/model/component-validity.d.ts +85 -0
  160. package/dist/taco/model/component-validity.js +397 -0
  161. package/dist/taco/model/components.d.ts +155 -0
  162. package/dist/taco/model/components.js +505 -0
  163. package/dist/taco/model/connector-ends.d.ts +20 -0
  164. package/dist/taco/model/connector-ends.js +188 -0
  165. package/dist/taco/model/connector-geometry.d.ts +74 -0
  166. package/dist/taco/model/connector-geometry.js +293 -0
  167. package/dist/taco/model/corner-radii.d.ts +26 -0
  168. package/dist/taco/model/corner-radii.js +47 -0
  169. package/dist/taco/model/derivations.d.ts +81 -0
  170. package/dist/taco/model/derivations.js +151 -0
  171. package/dist/taco/model/export-settings.d.ts +53 -0
  172. package/dist/taco/model/export-settings.js +156 -0
  173. package/dist/taco/model/expressions/ast.d.ts +153 -0
  174. package/dist/taco/model/expressions/ast.js +6 -0
  175. package/dist/taco/model/expressions/cache.d.ts +23 -0
  176. package/dist/taco/model/expressions/cache.js +43 -0
  177. package/dist/taco/model/expressions/check.d.ts +72 -0
  178. package/dist/taco/model/expressions/check.js +517 -0
  179. package/dist/taco/model/expressions/completion.d.ts +61 -0
  180. package/dist/taco/model/expressions/completion.js +421 -0
  181. package/dist/taco/model/expressions/evaluate.d.ts +32 -0
  182. package/dist/taco/model/expressions/evaluate.js +519 -0
  183. package/dist/taco/model/expressions/forms.d.ts +84 -0
  184. package/dist/taco/model/expressions/forms.js +200 -0
  185. package/dist/taco/model/expressions/index.d.ts +14 -0
  186. package/dist/taco/model/expressions/index.js +17 -0
  187. package/dist/taco/model/expressions/lexer.d.ts +42 -0
  188. package/dist/taco/model/expressions/lexer.js +213 -0
  189. package/dist/taco/model/expressions/library.d.ts +33 -0
  190. package/dist/taco/model/expressions/library.js +117 -0
  191. package/dist/taco/model/expressions/names.d.ts +43 -0
  192. package/dist/taco/model/expressions/names.js +99 -0
  193. package/dist/taco/model/expressions/parser.d.ts +15 -0
  194. package/dist/taco/model/expressions/parser.js +438 -0
  195. package/dist/taco/model/expressions/rename.d.ts +34 -0
  196. package/dist/taco/model/expressions/rename.js +46 -0
  197. package/dist/taco/model/expressions/source.d.ts +15 -0
  198. package/dist/taco/model/expressions/source.js +161 -0
  199. package/dist/taco/model/expressions/types.d.ts +119 -0
  200. package/dist/taco/model/expressions/types.js +102 -0
  201. package/dist/taco/model/expressions/values.d.ts +97 -0
  202. package/dist/taco/model/expressions/values.js +160 -0
  203. package/dist/taco/model/geometry-limits.d.ts +16 -0
  204. package/dist/taco/model/geometry-limits.js +67 -0
  205. package/dist/taco/model/geometry.d.ts +47 -0
  206. package/dist/taco/model/geometry.js +193 -0
  207. package/dist/taco/model/group-bounds.d.ts +38 -0
  208. package/dist/taco/model/group-bounds.js +144 -0
  209. package/dist/taco/model/id-table.d.ts +37 -0
  210. package/dist/taco/model/id-table.js +80 -0
  211. package/dist/taco/model/index.d.ts +25 -0
  212. package/dist/taco/model/index.js +27 -0
  213. package/dist/taco/model/instance-resolver.d.ts +376 -0
  214. package/dist/taco/model/instance-resolver.js +1778 -0
  215. package/dist/taco/model/layer-syntax.d.ts +340 -0
  216. package/dist/taco/model/layer-syntax.js +3506 -0
  217. package/dist/taco/model/layout/constraints.d.ts +84 -0
  218. package/dist/taco/model/layout/constraints.js +445 -0
  219. package/dist/taco/model/layout/frame-layout.d.ts +46 -0
  220. package/dist/taco/model/layout/frame-layout.js +618 -0
  221. package/dist/taco/model/layout/node-sizing.d.ts +163 -0
  222. package/dist/taco/model/layout/node-sizing.js +486 -0
  223. package/dist/taco/model/legacy-connector-caps.d.ts +15 -0
  224. package/dist/taco/model/legacy-connector-caps.js +70 -0
  225. package/dist/taco/model/linear-call.d.ts +48 -0
  226. package/dist/taco/model/linear-call.js +58 -0
  227. package/dist/taco/model/new-nodes.d.ts +145 -0
  228. package/dist/taco/model/new-nodes.js +277 -0
  229. package/dist/taco/model/node-builder.d.ts +449 -0
  230. package/dist/taco/model/node-builder.js +2294 -0
  231. package/dist/taco/model/node-table-watch.d.ts +36 -0
  232. package/dist/taco/model/node-table-watch.js +63 -0
  233. package/dist/taco/model/node-transform.d.ts +39 -0
  234. package/dist/taco/model/node-transform.js +79 -0
  235. package/dist/taco/model/node-users-index.d.ts +10 -0
  236. package/dist/taco/model/node-users-index.js +82 -0
  237. package/dist/taco/model/operation-references.d.ts +80 -0
  238. package/dist/taco/model/operation-references.js +399 -0
  239. package/dist/taco/model/operations.d.ts +610 -0
  240. package/dist/taco/model/operations.js +2882 -0
  241. package/dist/taco/model/overlay-table.d.ts +71 -0
  242. package/dist/taco/model/overlay-table.js +171 -0
  243. package/dist/taco/model/override-remap.d.ts +69 -0
  244. package/dist/taco/model/override-remap.js +230 -0
  245. package/dist/taco/model/parent-table.d.ts +53 -0
  246. package/dist/taco/model/parent-table.js +193 -0
  247. package/dist/taco/model/problem-collector.d.ts +27 -0
  248. package/dist/taco/model/problem-collector.js +101 -0
  249. package/dist/taco/model/sha256.d.ts +10 -0
  250. package/dist/taco/model/sha256.js +79 -0
  251. package/dist/taco/model/shape-geometry.d.ts +96 -0
  252. package/dist/taco/model/shape-geometry.js +368 -0
  253. package/dist/taco/model/shared-node-table.d.ts +97 -0
  254. package/dist/taco/model/shared-node-table.js +546 -0
  255. package/dist/taco/model/slots.d.ts +113 -0
  256. package/dist/taco/model/slots.js +281 -0
  257. package/dist/taco/model/stroke-settings.d.ts +37 -0
  258. package/dist/taco/model/stroke-settings.js +90 -0
  259. package/dist/taco/model/svg-path-import.d.ts +89 -0
  260. package/dist/taco/model/svg-path-import.js +219 -0
  261. package/dist/taco/model/text-measurement.d.ts +142 -0
  262. package/dist/taco/model/text-measurement.js +846 -0
  263. package/dist/taco/model/text-reads.d.ts +66 -0
  264. package/dist/taco/model/text-reads.js +128 -0
  265. package/dist/taco/model/text-sizer.d.ts +358 -0
  266. package/dist/taco/model/text-sizer.js +996 -0
  267. package/dist/taco/model/theme-users.d.ts +59 -0
  268. package/dist/taco/model/theme-users.js +312 -0
  269. package/dist/taco/model/thumbnail.d.ts +15 -0
  270. package/dist/taco/model/thumbnail.js +34 -0
  271. package/dist/taco/model/types.d.ts +2369 -0
  272. package/dist/taco/model/types.js +366 -0
  273. package/dist/taco/model/unknown-property.d.ts +5 -0
  274. package/dist/taco/model/unknown-property.js +7 -0
  275. package/dist/taco/model/validated-documents.d.ts +22 -0
  276. package/dist/taco/model/validated-documents.js +22 -0
  277. package/dist/taco/model/variable-modes.d.ts +61 -0
  278. package/dist/taco/model/variable-modes.js +438 -0
  279. package/dist/taco/model/variable-references.d.ts +54 -0
  280. package/dist/taco/model/variable-references.js +183 -0
  281. package/dist/taco/model/variables.d.ts +24 -0
  282. package/dist/taco/model/variables.js +151 -0
  283. package/dist/taco/model/vector-network.d.ts +34 -0
  284. package/dist/taco/model/vector-network.js +411 -0
  285. package/dist/taco/svg-import/import-svg.d.ts +36 -0
  286. package/dist/taco/svg-import/import-svg.js +729 -0
  287. package/dist/taco/svg-import/index.d.ts +3 -0
  288. package/dist/taco/svg-import/index.js +3 -0
  289. package/dist/taco/svg-import/limits/limits.d.ts +79 -0
  290. package/dist/taco/svg-import/limits/limits.js +160 -0
  291. package/dist/taco/svg-import/paint/gradients.d.ts +64 -0
  292. package/dist/taco/svg-import/paint/gradients.js +429 -0
  293. package/dist/taco/svg-import/paint/paint.d.ts +32 -0
  294. package/dist/taco/svg-import/paint/paint.js +162 -0
  295. package/dist/taco/svg-import/paths/paths.d.ts +58 -0
  296. package/dist/taco/svg-import/paths/paths.js +205 -0
  297. package/dist/taco/svg-import/public-types.d.ts +279 -0
  298. package/dist/taco/svg-import/public-types.js +56 -0
  299. package/dist/taco/svg-import/report/report.d.ts +44 -0
  300. package/dist/taco/svg-import/report/report.js +79 -0
  301. package/dist/taco/svg-import/reuse/reuse.d.ts +99 -0
  302. package/dist/taco/svg-import/reuse/reuse.js +840 -0
  303. package/dist/taco/svg-import/shapes/shapes.d.ts +35 -0
  304. package/dist/taco/svg-import/shapes/shapes.js +386 -0
  305. package/dist/taco/svg-import/structure/effects.d.ts +26 -0
  306. package/dist/taco/svg-import/structure/effects.js +618 -0
  307. package/dist/taco/svg-import/structure/image.d.ts +24 -0
  308. package/dist/taco/svg-import/structure/image.js +495 -0
  309. package/dist/taco/svg-import/structure/walk.d.ts +85 -0
  310. package/dist/taco/svg-import/structure/walk.js +544 -0
  311. package/dist/taco/svg-import/style/color.d.ts +40 -0
  312. package/dist/taco/svg-import/style/color.js +275 -0
  313. package/dist/taco/svg-import/style/computed.d.ts +214 -0
  314. package/dist/taco/svg-import/style/computed.js +1534 -0
  315. package/dist/taco/svg-import/style/css.d.ts +127 -0
  316. package/dist/taco/svg-import/style/css.js +541 -0
  317. package/dist/taco/svg-import/style/media.d.ts +25 -0
  318. package/dist/taco/svg-import/style/media.js +307 -0
  319. package/dist/taco/svg-import/style/properties.d.ts +43 -0
  320. package/dist/taco/svg-import/style/properties.js +135 -0
  321. package/dist/taco/svg-import/style/selector-reach.d.ts +30 -0
  322. package/dist/taco/svg-import/style/selector-reach.js +169 -0
  323. package/dist/taco/svg-import/style/selector-validity.d.ts +14 -0
  324. package/dist/taco/svg-import/style/selector-validity.js +307 -0
  325. package/dist/taco/svg-import/text/text.d.ts +29 -0
  326. package/dist/taco/svg-import/text/text.js +545 -0
  327. package/dist/taco/svg-import/transform/matrix.d.ts +111 -0
  328. package/dist/taco/svg-import/transform/matrix.js +229 -0
  329. package/dist/taco/svg-import/tree/element-data.d.ts +52 -0
  330. package/dist/taco/svg-import/tree/element-data.js +155 -0
  331. package/dist/taco/svg-import/tree/elements.d.ts +36 -0
  332. package/dist/taco/svg-import/tree/elements.js +121 -0
  333. package/dist/taco/svg-import/units/length.d.ts +43 -0
  334. package/dist/taco/svg-import/units/length.js +125 -0
  335. package/dist/taco/svg-import/units/viewbox.d.ts +35 -0
  336. package/dist/taco/svg-import/units/viewbox.js +77 -0
  337. package/package.json +55 -4
@@ -0,0 +1,729 @@
1
+ import { getNodeBounds, multiplyTransforms, nodeLocalTransform, } from "../model/index.js";
2
+ import { DEFAULT_SVG_IMPORT_LIMITS, } from "./public-types.js";
3
+ import { attr, childElements, isSvgElement, pathStep, sameTagIndex, } from "./tree/elements.js";
4
+ import { describeStyleRejections, parseStyleSheet } from "./style/css.js";
5
+ import { selectorCannotMatch } from "./style/selector-reach.js";
6
+ import { evaluateMediaQueryList } from "./style/media.js";
7
+ import { computeElementStyle, initialComputedStyle, StyleRules } from "./style/computed.js";
8
+ import { parseSvgLength } from "./units/length.js";
9
+ import { parsePreserveAspectRatio, parseViewBox, viewBoxTransform, DEFAULT_PRESERVE_ASPECT_RATIO, } from "./units/viewbox.js";
10
+ import { IDENTITY_MATRIX, invertMatrix } from "./transform/matrix.js";
11
+ import { LimitGuard, SvgImportRefusalError } from "./limits/limits.js";
12
+ import { ReportBuilder } from "./report/report.js";
13
+ import { buildChildren, reportConditionAttributes, shiftNode, unionBoxes, } from "./structure/walk.js";
14
+ import { ComponentRegistry, planReuse, probeContext } from "./reuse/reuse.js";
15
+ import { rasterFramePlan } from "./shapes/shapes.js";
16
+ import { layerChrome } from "./paths/paths.js";
17
+ const STYLE_VALUES_NOTE = { what: "Style values that cannot be read", kept: "The values are ignored" };
18
+ const ROOT_SIZE_NOTE = { what: "Root sizes that cannot be read", kept: "The root is sized from what can be read" };
19
+ const ROOT_TRANSFORM_NOTE = { what: "Transforms on the root", kept: "The import is placed where it is dropped or pasted" };
20
+ /** Node types of the text a byte count includes: text (3) and CDATA (4). */
21
+ const TEXT_NODE_TYPES = new Set([3, 4]);
22
+ /**
23
+ * Most distinct class names one element may carry. Real files carry a
24
+ * handful; the cap keeps a class attribute written to inflate the selector
25
+ * index from growing it without end.
26
+ */
27
+ const MAX_ELEMENT_CLASSES = 256;
28
+ /**
29
+ * Adds one class attribute's names to the document's class index, scanning
30
+ * the value token by token so the whole token list is never allocated: each
31
+ * token counts one unit of style work, and an element naming more distinct
32
+ * classes than the cap refuses the import, since the index only ever needs
33
+ * the set of names and such a list never matches a real rule.
34
+ */
35
+ function indexClasses(value, classes, guard) {
36
+ const own = new Set();
37
+ let start = -1;
38
+ const flush = (end) => {
39
+ if (start === -1)
40
+ return;
41
+ const name = value.slice(start, end);
42
+ start = -1;
43
+ guard.countStyleWork(1);
44
+ if (own.has(name))
45
+ return;
46
+ own.add(name);
47
+ if (own.size > MAX_ELEMENT_CLASSES) {
48
+ throw new SvgImportRefusalError("too-much-style-work", `An element carries more than ${String(MAX_ELEMENT_CLASSES)} distinct class names`);
49
+ }
50
+ classes.add(name);
51
+ };
52
+ for (let index = 0; index < value.length; index += 1) {
53
+ const ch = value.charAt(index);
54
+ if (ch === " " || ch === "\t" || ch === "\n" || ch === "\r" || ch === "\f")
55
+ flush(index);
56
+ else if (start === -1)
57
+ start = index;
58
+ }
59
+ flush(value.length);
60
+ }
61
+ /** The characters of an element's own text children, not its descendants'. */
62
+ function ownTextLength(element) {
63
+ let length = 0;
64
+ for (let index = 0; index < element.childNodes.length; index += 1) {
65
+ const child = element.childNodes[index];
66
+ if (TEXT_NODE_TYPES.has(child.nodeType) && "nodeValue" in child)
67
+ length += child.nodeValue?.length ?? 0;
68
+ }
69
+ return length;
70
+ }
71
+ /**
72
+ * Counts and indexes one tree: caps, ids, element paths, `<style>` rules and
73
+ * computed styles. `countBytes` is false when the byte cap was already
74
+ * measured on the markup string.
75
+ */
76
+ function prePass(root, rootPath, guard, report, sizing, countBytes) {
77
+ const viewport = sizing.viewport;
78
+ const ids = new Map();
79
+ const paths = new Map();
80
+ const styleTexts = [];
81
+ const classes = new Set();
82
+ const attributes = new Set();
83
+ let title = null;
84
+ let inputBytes = 0;
85
+ const walk = (element, elementPath, depth) => {
86
+ guard.countElement();
87
+ guard.checkDepth(depth);
88
+ paths.set(element, elementPath);
89
+ inputBytes += element.localName.length + 2;
90
+ for (let index = 0; index < element.attributes.length; index += 1) {
91
+ const attribute = element.attributes[index];
92
+ inputBytes += attribute.name.length + attribute.value.length + 3;
93
+ attributes.add(attribute.name.toLowerCase());
94
+ if (attribute.name === "class")
95
+ indexClasses(attribute.value, classes, guard);
96
+ }
97
+ inputBytes += ownTextLength(element);
98
+ const id = attr(element, "id");
99
+ if (id !== null && !ids.has(id))
100
+ ids.set(id, element);
101
+ if (isSvgElement(element, "style")) {
102
+ // Only a CSS style sheet applies: a `type` that is not `text/css`, in
103
+ // any letter case, makes a browser skip the element, so it is skipped
104
+ // the same way, without a report.
105
+ const type = attr(element, "type");
106
+ if (type === null || type.toLowerCase() === "text/css") {
107
+ styleTexts.push({ path: elementPath, text: element.textContent ?? "", media: attr(element, "media") });
108
+ }
109
+ }
110
+ if (title === null && isSvgElement(element, "title") && depth === 2) {
111
+ const text = element.textContent?.trim() ?? "";
112
+ if (text.length > 0)
113
+ title = text;
114
+ }
115
+ for (const child of childElements(element)) {
116
+ walk(child, `${elementPath} > ${pathStep(child, sameTagIndex(element, child))}`, depth + 1);
117
+ }
118
+ };
119
+ walk(root, rootPath, 1);
120
+ if (countBytes)
121
+ guard.checkInputBytes(inputBytes);
122
+ // Rules that cannot apply to the import's light look at the root's size
123
+ // are skipped without a report line: a selector no element can match
124
+ // (such as a dark-mode class set on a page above the SVG) and an @media
125
+ // block whose query is false (such as prefers-color-scheme: dark).
126
+ const reach = { classes, ids: new Set(ids.keys()), attributes };
127
+ const sheetOptions = {
128
+ media: { size: sizing.frame },
129
+ cannotMatch: (selector) => selectorCannotMatch(selector, reach),
130
+ };
131
+ const rules = [];
132
+ let order = 0;
133
+ for (const styleText of styleTexts) {
134
+ // A <style media="..."> applies only when its query does, like an
135
+ // @media block: a false query is skipped without a line, one that
136
+ // cannot be evaluated is left out and listed.
137
+ if (styleText.media !== null) {
138
+ const applies = evaluateMediaQueryList(styleText.media, sheetOptions.media);
139
+ if (applies === null) {
140
+ report(styleText.path, "style", `the media query "${styleText.media.trim()}" cannot be evaluated; the style element's rules are left out`, { what: "Style rules for media that cannot be checked", kept: "The rules are left out" });
141
+ continue;
142
+ }
143
+ if (!applies)
144
+ continue;
145
+ }
146
+ const parsed = parseStyleSheet(styleText.text, sheetOptions, (units) => guard.countStyleWork(units));
147
+ for (const rule of parsed.rules) {
148
+ rules.push({ ...rule, order: order + rule.order });
149
+ }
150
+ order += parsed.rules.length;
151
+ for (const line of describeStyleRejections(parsed.rejected, parsed.rejectedOverflow)) {
152
+ report(styleText.path, "style", line, { what: "Style rules that cannot be read", kept: "The rules are ignored" });
153
+ }
154
+ }
155
+ const styleRules = new StyleRules(rules, guard);
156
+ const styles = new Map();
157
+ // Each element's declarations whose value cannot be read are listed here,
158
+ // once per element, except under display:none, where nothing is drawn
159
+ // and the walk reports the hidden element itself.
160
+ const compute = (element, parent, hidden) => {
161
+ const style = computeElementStyle(element, parent, styleRules, viewport);
162
+ styles.set(element, style);
163
+ const childrenHidden = hidden || style.display === "none";
164
+ if (!hidden) {
165
+ const elementPath = paths.get(element) ?? rootPath;
166
+ for (const declaration of style.invalid) {
167
+ report(elementPath, element.localName, declaration.reason === undefined
168
+ ? `the ${declaration.property} value "${declaration.value}" cannot be read and is ignored`
169
+ : `the ${declaration.property} value "${declaration.value}" is ignored: ${declaration.reason}`, STYLE_VALUES_NOTE);
170
+ }
171
+ if (style.invalidOverflow > 0) {
172
+ report(elementPath, element.localName, `${String(style.invalidOverflow)} more declarations cannot be read and are ignored`, STYLE_VALUES_NOTE);
173
+ }
174
+ for (const line of describeStyleRejections(style.styleAttributeRejected.rejected, style.styleAttributeRejected.overflow)) {
175
+ report(elementPath, element.localName, `the style attribute is partly ignored: ${line}`, STYLE_VALUES_NOTE);
176
+ }
177
+ if (style.unsupported.length > 0) {
178
+ const listed = style.unsupported.map((declaration) => `${declaration.property} "${declaration.value}"`).join(", ");
179
+ report(elementPath, element.localName, `not supported, so ignored: ${listed}`, { what: "Unsupported style properties", kept: "The properties are ignored" });
180
+ }
181
+ }
182
+ for (const child of childElements(element))
183
+ compute(child, style, childrenHidden);
184
+ };
185
+ compute(root, null, false);
186
+ return { ids, rules: styleRules, styles, paths, title };
187
+ }
188
+ /**
189
+ * The namespaces a `DOMParser` puts its `parsererror` element in: XHTML in
190
+ * Chrome and Safari, its own in Firefox. An element of that name in any
191
+ * other namespace, such as the SVG one, is the author's and is not an error.
192
+ */
193
+ const PARSER_ERROR_NAMESPACES = new Set([
194
+ "http://www.w3.org/1999/xhtml",
195
+ "http://www.mozilla.org/newlayout/xml/parsererror.xml",
196
+ ]);
197
+ const XHTML_NAMESPACE = "http://www.w3.org/1999/xhtml";
198
+ /**
199
+ * The text of a browser-generated `parsererror` element in a parsed tree, or
200
+ * null. A browser's `DOMParser` does not throw on malformed XML: it returns
201
+ * the parser's message in a `parsererror` element — as the document root in
202
+ * Firefox, and inside the generated error page (an XHTML `html` root with
203
+ * the `parsererror` in its body) in Chrome and Safari (probed in Chrome for
204
+ * Testing 151). A `parsererror` anywhere else, such as one an author wrote
205
+ * inside a `<foreignObject>`, is document content, not an error.
206
+ */
207
+ function parserErrorText(root) {
208
+ const generated = isParserError(root) ? root : errorPageParserError(root);
209
+ if (generated === null)
210
+ return null;
211
+ const text = (generated.textContent ?? "").replace(/\s+/g, " ").trim();
212
+ return text.length > 0 ? parserErrorLines(text) : "the parser gave no message";
213
+ }
214
+ /** Whether the element is a browser's `parsererror` (in one of the two namespaces they use). */
215
+ function isParserError(element) {
216
+ return element.localName === "parsererror" && PARSER_ERROR_NAMESPACES.has(element.namespaceURI ?? "");
217
+ }
218
+ /** The `parsererror` of Chrome's or Safari's generated error page: an XHTML `html` root with one inside. */
219
+ function errorPageParserError(root) {
220
+ if (root.localName !== "html" || (root.namespaceURI ?? "") !== XHTML_NAMESPACE)
221
+ return null;
222
+ const stack = [root];
223
+ while (stack.length > 0) {
224
+ const element = stack.pop();
225
+ if (isParserError(element))
226
+ return element;
227
+ const children = childElements(element);
228
+ for (let index = children.length - 1; index >= 0; index -= 1)
229
+ stack.push(children[index]);
230
+ }
231
+ return null;
232
+ }
233
+ /** How Chrome starts each error in a `parsererror` element's text. */
234
+ const CHROME_ERROR_LINE = /error on line (\d+) at column (\d+):\s*/g;
235
+ /** The sentence Chrome puts after the errors. */
236
+ const CHROME_ERROR_FOOTER = "Below is a rendering of the page up to the first error.";
237
+ /**
238
+ * Only the errors of a `parsererror` element's text. Chrome wraps them in
239
+ * page text ("This page contains the following errors:" before, "Below is a
240
+ * rendering of the page up to the first error." after); each error becomes
241
+ * `line L, column C: message`, as the importer's other parse messages read.
242
+ * Text without an error line in Chrome's form is returned whole.
243
+ */
244
+ function parserErrorLines(text) {
245
+ const starts = Array.from(text.matchAll(CHROME_ERROR_LINE));
246
+ if (starts.length === 0)
247
+ return text;
248
+ const lines = starts.map((start, index) => {
249
+ const from = start.index + start[0].length;
250
+ const to = index + 1 < starts.length ? starts[index + 1].index : text.length;
251
+ let message = text.slice(from, to).trim();
252
+ if (message.endsWith(CHROME_ERROR_FOOTER))
253
+ message = message.slice(0, -CHROME_ERROR_FOOTER.length).trim();
254
+ return `line ${start[1]}, column ${start[2]}: ${message}`;
255
+ });
256
+ return lines.join("; ");
257
+ }
258
+ /** The message of a thrown parser error, in plain words. */
259
+ function thrownText(error) {
260
+ return error instanceof Error ? error.message : String(error);
261
+ }
262
+ /** The size CSS gives an image with no width, height or aspect ratio of its own. */
263
+ const SVG_DEFAULT_SIZE = Object.freeze({ width: 300, height: 150 });
264
+ /**
265
+ * Decides the root's size from `width`/`height` and the viewBox, per the
266
+ * spec. A missing side takes the viewBox's aspect ratio from the side that
267
+ * is set, as a browser sizes an SVG with one side given.
268
+ */
269
+ function rootSizing(root, report) {
270
+ const lengthOf = (name) => {
271
+ const text = attr(root, name);
272
+ if (text === null)
273
+ return null;
274
+ const parsed = parseSvgLength(text, { fontSize: 16 });
275
+ if (parsed === null) {
276
+ report(`the root ${name} "${text}" cannot be read`, ROOT_SIZE_NOTE);
277
+ return null;
278
+ }
279
+ if (parsed.kind === "percent")
280
+ return null;
281
+ return parsed.value >= 0 ? parsed.value : null;
282
+ };
283
+ let width = lengthOf("width");
284
+ let height = lengthOf("height");
285
+ const viewBoxText = attr(root, "viewBox");
286
+ const parsedViewBox = viewBoxText === null ? null : parseViewBox(viewBoxText);
287
+ if (viewBoxText !== null && parsedViewBox === null) {
288
+ report(`the viewBox "${viewBoxText}" cannot be read`, ROOT_SIZE_NOTE);
289
+ }
290
+ if (width === 0 || height === 0) {
291
+ // An explicit zero is not a missing side: the viewport has no area, so nothing inside it draws.
292
+ const frame = { width: width ?? 0, height: height ?? 0 };
293
+ return { frame, childrenMatrix: IDENTITY_MATRIX, viewport: frame, zeroSide: { side: width === 0 ? "width" : "height", of: "root" } };
294
+ }
295
+ // A negative viewBox side is an error that voids the viewBox; a zero side disables rendering (SVG 2 §8.6).
296
+ if (parsedViewBox !== null && (parsedViewBox.width < 0 || parsedViewBox.height < 0)) {
297
+ report(`the viewBox "${viewBoxText ?? ""}" has a negative size, so it is ignored`, ROOT_SIZE_NOTE);
298
+ }
299
+ else if (parsedViewBox !== null && (parsedViewBox.width === 0 || parsedViewBox.height === 0)) {
300
+ const frame = { width: width ?? parsedViewBox.width, height: height ?? parsedViewBox.height };
301
+ return {
302
+ frame,
303
+ childrenMatrix: IDENTITY_MATRIX,
304
+ viewport: frame,
305
+ zeroSide: { side: parsedViewBox.width === 0 ? "width" : "height", of: "viewBox" },
306
+ };
307
+ }
308
+ const viewBox = parsedViewBox !== null && parsedViewBox.width > 0 && parsedViewBox.height > 0
309
+ ? parsedViewBox
310
+ : null;
311
+ const aspect = parsePreserveAspectRatio(attr(root, "preserveAspectRatio") ?? "")
312
+ ?? DEFAULT_PRESERVE_ASPECT_RATIO;
313
+ if (viewBox !== null) {
314
+ if (width !== null && height === null)
315
+ height = width * (viewBox.height / viewBox.width);
316
+ if (height !== null && width === null)
317
+ width = height * (viewBox.width / viewBox.height);
318
+ }
319
+ else if ((width === null) !== (height === null)) {
320
+ // One side and no aspect ratio: the missing side takes the CSS default
321
+ // image size, 300x150, as a browser sizes it, so the set side is kept.
322
+ width ??= SVG_DEFAULT_SIZE.width;
323
+ height ??= SVG_DEFAULT_SIZE.height;
324
+ }
325
+ if (width !== null && height !== null) {
326
+ const matrix = viewBox === null
327
+ ? IDENTITY_MATRIX
328
+ : viewBoxTransform(viewBox, width, height, aspect);
329
+ return {
330
+ frame: { width, height },
331
+ childrenMatrix: matrix,
332
+ viewport: viewBox === null ? { width, height } : { width: viewBox.width, height: viewBox.height },
333
+ zeroSide: null,
334
+ };
335
+ }
336
+ if (viewBox !== null) {
337
+ // The viewBox alone sizes the root: content maps 1:1, moved so the
338
+ // viewBox's top-left corner is the frame's origin.
339
+ return {
340
+ frame: { width: viewBox.width, height: viewBox.height },
341
+ childrenMatrix: [1, 0, 0, 1, -viewBox.minX, -viewBox.minY],
342
+ viewport: { width: viewBox.width, height: viewBox.height },
343
+ zeroSide: null,
344
+ };
345
+ }
346
+ return { frame: null, childrenMatrix: IDENTITY_MATRIX, viewport: null, zeroSide: null };
347
+ }
348
+ /**
349
+ * What may come before the root of an SVG document: a byte order mark, the
350
+ * XML declaration, comments, processing instructions and a doctype (with an
351
+ * internal subset).
352
+ */
353
+ const MARKUP_PROLOGUE = /^?(?:\s+|<\?[\s\S]*?\?>|<!--[\s\S]*?-->|<!DOCTYPE[^>[]*(?:\[[\s\S]*?\])?\s*>)*/i;
354
+ /** The root start tag: its name and its attributes, each value quoted. */
355
+ const MARKUP_ROOT_START_TAG = /^<([A-Za-z_][\w.:-]*)((?:\s+[^\s=/>]+\s*=\s*(?:"[^"]*"|'[^']*'))*)\s*\/?>/;
356
+ const MARKUP_ATTRIBUTE = /([^\s=/>]+)\s*=\s*(?:"([^"]*)"|'([^']*)')/g;
357
+ const XML_ENTITIES = { lt: "<", gt: ">", amp: "&", quot: "\"", apos: "'" };
358
+ /** An attribute value as XML reads it: references decoded, whitespace made spaces; null for another entity. */
359
+ function markupAttributeValue(raw) {
360
+ let unknown = false;
361
+ const decoded = raw.replace(/&(#x[0-9a-fA-F]+|#[0-9]+|[A-Za-z][\w.-]*);/g, (_match, name) => {
362
+ if (name.startsWith("#")) {
363
+ const codePoint = name.startsWith("#x") ? Number.parseInt(name.slice(2), 16) : Number.parseInt(name.slice(1), 10);
364
+ // Past the last Unicode code point (or a surrogate or zero) XML refuses
365
+ // the document; String.fromCodePoint would throw.
366
+ if (codePoint === 0 || codePoint > 0x10ffff || (codePoint >= 0xd800 && codePoint <= 0xdfff)) {
367
+ unknown = true;
368
+ return "";
369
+ }
370
+ return String.fromCodePoint(codePoint);
371
+ }
372
+ const known = XML_ENTITIES[name];
373
+ if (known === undefined)
374
+ unknown = true;
375
+ return known ?? "";
376
+ });
377
+ return unknown ? null : decoded.replace(/[\t\n\r]/g, " ");
378
+ }
379
+ /**
380
+ * The size an import of `markup` will give its root, read from the root
381
+ * start tag alone (`width`, `height` and `viewBox`, by the same rule the
382
+ * import uses), so it is known before the document is parsed. Null when the
383
+ * root is sized to its content (no usable width, height or viewBox, such as
384
+ * percentages with no viewBox) or the start tag cannot be read here; the
385
+ * import itself may still refuse a document this returns a size for.
386
+ */
387
+ export function svgMarkupRootSize(markup) {
388
+ const prologue = MARKUP_PROLOGUE.exec(markup)?.[0] ?? "";
389
+ // A root start tag is short; the cap keeps a malformed one from scanning the whole text.
390
+ const tag = MARKUP_ROOT_START_TAG.exec(markup.slice(prologue.length, prologue.length + 64 * 1024));
391
+ if (tag === null)
392
+ return null;
393
+ const name = tag[1];
394
+ const localName = name.slice(name.indexOf(":") + 1);
395
+ if (localName !== "svg")
396
+ return null;
397
+ const attributes = new Map();
398
+ for (const match of tag[2].matchAll(MARKUP_ATTRIBUTE)) {
399
+ const value = markupAttributeValue(match[2] ?? match[3] ?? "");
400
+ if (value === null)
401
+ return null;
402
+ attributes.set(match[1], value);
403
+ }
404
+ const root = {
405
+ nodeType: 1,
406
+ localName,
407
+ namespaceURI: attributes.get("xmlns") ?? null,
408
+ getAttribute: (attribute) => attributes.get(attribute) ?? null,
409
+ attributes: [...attributes].map(([attribute, value]) => ({ name: attribute, value })),
410
+ children: [],
411
+ textContent: null,
412
+ childNodes: [],
413
+ };
414
+ return rootSizing(root, () => { }).frame;
415
+ }
416
+ /** Builds the walk context of one document (the main one or a nested SVG image). */
417
+ function documentContext(shared, pass, reuse, viewport, componentRegistry) {
418
+ return {
419
+ ...shared,
420
+ rules: pass.rules,
421
+ ids: pass.ids,
422
+ styles: pass.styles,
423
+ paths: pass.paths,
424
+ viewport,
425
+ reuse,
426
+ componentRegistry,
427
+ };
428
+ }
429
+ /**
430
+ * The root layer's opacity and blend mode, from the root `<svg>`'s own
431
+ * style. The root stays visible: visibility is inherited and a child may set
432
+ * it back to visible, so each drawn layer carries its own computed value.
433
+ */
434
+ function rootChrome(style) {
435
+ const chrome = layerChrome(style);
436
+ return {
437
+ visible: true,
438
+ opacity: chrome.opacity,
439
+ ...(chrome.blendMode === null ? {} : { blendMode: chrome.blendMode }),
440
+ };
441
+ }
442
+ /** A root's `transform` text, or null when it has none or it is a no-op (empty or `none`). */
443
+ function rootTransformText(root) {
444
+ const text = attr(root, "transform")?.trim() ?? "";
445
+ return text === "" || text.toLowerCase() === "none" ? null : text;
446
+ }
447
+ /**
448
+ * Lists a clip path, mask or filter on an SVG root as left out: they act on
449
+ * the root's box, which the import places as it is.
450
+ */
451
+ function reportRootEffects(style, report) {
452
+ for (const [property, reference] of [["clip-path", style.clipPath], ["mask", style.mask], ["filter", style.filter]]) {
453
+ if (reference !== null) {
454
+ report(`the root's ${property} is not applied; the import's root is the box placed in the document, so it is not clipped, masked or filtered`, { what: "Clips, masks and filters on the root", kept: "The root is kept without them" });
455
+ }
456
+ }
457
+ }
458
+ /** Runs the walk over one SVG root and assembles the result. */
459
+ function runImport(root, options, guard, countBytes) {
460
+ const parserError = parserErrorText(root);
461
+ if (parserError !== null) {
462
+ throw new SvgImportRefusalError("malformed", `The SVG is not well-formed XML: ${parserError}`);
463
+ }
464
+ if (!isSvgElement(root, "svg")) {
465
+ throw new SvgImportRefusalError("not-svg", "The document's root is not an <svg> element in the SVG namespace");
466
+ }
467
+ const reportBuilder = new ReportBuilder();
468
+ const shared = {
469
+ idSource: options.idSource,
470
+ fontAvailable: options.fontAvailable,
471
+ measureText: options.measureText,
472
+ reportBuilder,
473
+ report: (elementPath, element, kind, reason, note) => reportBuilder.add(elementPath, element, kind, reason, note),
474
+ limits: guard,
475
+ components: [],
476
+ images: [],
477
+ imageAssets: new Map(),
478
+ nestedRegistries: new Map(),
479
+ textRunStyles: new Map(),
480
+ runNestedImport: options.parseSvg === undefined ? null : nestedImport(options.parseSvg),
481
+ probing: false,
482
+ };
483
+ const sizing = rootSizing(root, (reason, note) => reportBuilder.add("svg", "svg", "left-out", reason, note));
484
+ const pass = prePass(root, "svg", guard, (elementPath, element, reason, note) => reportBuilder.add(elementPath, element, "left-out", reason, note), sizing, countBytes);
485
+ const reuse = planReuse(root, pass.ids, guard);
486
+ const ctx = documentContext(shared, pass, reuse, sizing.viewport, new ComponentRegistry());
487
+ const name = options.name?.replace(/\.svg$/i, "") ?? pass.title ?? "SVG";
488
+ const rootStyle = pass.styles.get(root) ?? initialComputedStyle();
489
+ // A root whose display is none draws nothing: its layer stays, empty, and the report says why.
490
+ const hidden = rootStyle.display === "none";
491
+ if (hidden) {
492
+ reportBuilder.add("svg", "svg", "left-out", "display:none", { what: "Hidden root (display:none)", kept: "The root layer is kept, empty" });
493
+ }
494
+ if (sizing.zeroSide !== null) {
495
+ const owner = sizing.zeroSide.of === "root" ? "root's" : "viewBox's";
496
+ reportBuilder.add("svg", "svg", "left-out", `the ${owner} ${sizing.zeroSide.side} is zero, so nothing inside it draws`, { what: "Roots with no area", kept: "The root layer is kept, empty" });
497
+ }
498
+ // A transform on the root moves, turns or scales the root's own box (about
499
+ // its centre), not the content inside it. The import's root is placed
500
+ // where it is dropped or pasted and sized as the box, so the transform is
501
+ // left out, and listed.
502
+ const rootTransform = rootTransformText(root);
503
+ if (rootTransform !== null) {
504
+ reportBuilder.add("svg", "svg", "left-out", `the root's transform "${rootTransform}" is not applied; the import is placed where it is dropped or pasted, at the size of the root's box`, ROOT_TRANSFORM_NOTE);
505
+ }
506
+ // A clip path, mask or filter on the root acts on the root's box, which
507
+ // is placed in the document as it is, so they are left out, and listed.
508
+ reportRootEffects(rootStyle, (reason, note) => reportBuilder.add("svg", "svg", "left-out", reason, note));
509
+ // The root is not built by buildElement, so its conditions are reported here.
510
+ reportConditionAttributes(root, "svg", shared.report);
511
+ const { children, nodes } = hidden || sizing.zeroSide !== null
512
+ ? { children: [], nodes: [] }
513
+ : buildChildren(root, rootStyle, sizing.childrenMatrix, "svg", ctx);
514
+ const result = (rootNode, others) => {
515
+ // Lines no element's layer took (root and style lines, and lines of
516
+ // dropped top-level elements) go to their element's nearest layer, else the root.
517
+ reportBuilder.placeRest(rootNode.id);
518
+ return {
519
+ root: rootNode,
520
+ nodes: [rootNode, ...others],
521
+ components: shared.components,
522
+ images: shared.images,
523
+ report: reportBuilder.entries(),
524
+ textRunStyles: shared.textRunStyles,
525
+ };
526
+ };
527
+ if (sizing.frame !== null) {
528
+ return result({
529
+ id: options.idSource(),
530
+ name,
531
+ locked: false,
532
+ ...rootChrome(rootStyle),
533
+ type: "frame",
534
+ frame: { x: 0, y: 0, width: sizing.frame.width, height: sizing.frame.height },
535
+ rotation: 0,
536
+ children: children.map((child) => child.id),
537
+ layout: { kind: "absolute" },
538
+ clipContent: true,
539
+ }, nodes);
540
+ }
541
+ // No size and no viewBox: a group sized to its content, as the spec says.
542
+ const frame = unionBoxes(children.map((child) => getNodeBounds(child))) ?? { x: 0, y: 0, width: 0, height: 0 };
543
+ const shifted = children.map((child) => shiftNode(child, -frame.x, -frame.y));
544
+ const shiftedById = new Map(shifted.map((child) => [child.id, child]));
545
+ return result({
546
+ id: options.idSource(),
547
+ name,
548
+ locked: false,
549
+ ...rootChrome(rootStyle),
550
+ type: "group",
551
+ frame: { x: frame.x, y: frame.y, width: frame.width, height: frame.height },
552
+ rotation: 0,
553
+ children: shifted.map((child) => child.id),
554
+ }, nodes.map((node) => shiftedById.get(node.id) ?? node));
555
+ }
556
+ /** Runs one import, turning a refusal thrown anywhere inside into the returned refusal. */
557
+ function refusing(run) {
558
+ try {
559
+ return run();
560
+ }
561
+ catch (error) {
562
+ if (error instanceof SvgImportRefusalError)
563
+ return error.refusal();
564
+ throw error;
565
+ }
566
+ }
567
+ /** The caps of one import: the defaults with the options' overrides. */
568
+ function limitGuard(options) {
569
+ return new LimitGuard({ ...DEFAULT_SVG_IMPORT_LIMITS, ...options.limits });
570
+ }
571
+ /** The UTF-8 byte length of a string, counted without encoding it. */
572
+ function utf8Length(text) {
573
+ let bytes = 0;
574
+ for (let index = 0; index < text.length; index += 1) {
575
+ const code = text.charCodeAt(index);
576
+ if (code < 0x80)
577
+ bytes += 1;
578
+ else if (code < 0x800)
579
+ bytes += 2;
580
+ else if (code >= 0xd800 && code <= 0xdbff && index + 1 < text.length) {
581
+ const next = text.charCodeAt(index + 1);
582
+ if (next >= 0xdc00 && next <= 0xdfff) {
583
+ bytes += 4;
584
+ index += 1;
585
+ }
586
+ else {
587
+ bytes += 3;
588
+ }
589
+ }
590
+ else
591
+ bytes += 3;
592
+ }
593
+ return bytes;
594
+ }
595
+ /**
596
+ * Imports one SVG document from its markup: the common entry point. The
597
+ * byte cap is measured on the string before anything is parsed; the
598
+ * markup is parsed with `options.parseSvg` (the host passes its
599
+ * `DOMParser`). Markup that is not well-formed XML is refused with the code
600
+ * `malformed` and the parser's message, whether the parser throws or returns
601
+ * a browser's error page with a `parsererror` element, as `DOMParser` does.
602
+ * The same input with the same `idSource` always gives the same output, and
603
+ * a refusal means nothing was imported.
604
+ */
605
+ export function importSvgMarkup(svgText, options) {
606
+ return refusing(() => {
607
+ const guard = limitGuard(options);
608
+ guard.checkInputBytes(utf8Length(svgText));
609
+ let root;
610
+ try {
611
+ root = options.parseSvg(svgText);
612
+ }
613
+ catch (error) {
614
+ throw new SvgImportRefusalError("malformed", `The SVG is not well-formed XML: ${thrownText(error)}`);
615
+ }
616
+ return runImport(root, options, guard, false);
617
+ });
618
+ }
619
+ /**
620
+ * Imports one SVG document from an element tree the host already parsed — a
621
+ * browser `DOMParser` result or a test's own parse. A browser's error page
622
+ * (a `parsererror` root, or one inside a generated `html` page) is refused
623
+ * as `malformed`. The byte cap is measured approximately on the tree
624
+ * (element names, attributes and text). The same input with the same
625
+ * `idSource` always gives the same output, and a refusal means nothing was
626
+ * imported.
627
+ */
628
+ export function importSvg(root, options) {
629
+ return refusing(() => runImport(root, options, limitGuard(options), true));
630
+ }
631
+ /**
632
+ * The nested-import hook: parses and imports an SVG data-URI image under the
633
+ * same caps, report and component registries as the document it sits in.
634
+ * Every side effect goes through the calling context, so a component probe
635
+ * (see `reuse.ts`) can rebuild a nested image without adding anything.
636
+ */
637
+ function nestedImport(parseSvg) {
638
+ return (svgText, matrix, rect, aspect, elementPath, outer) => {
639
+ let nestedRoot;
640
+ try {
641
+ nestedRoot = parseSvg(svgText);
642
+ }
643
+ catch (error) {
644
+ return { reason: `the nested SVG image is not well-formed XML: ${thrownText(error)}` };
645
+ }
646
+ const parserError = parserErrorText(nestedRoot);
647
+ if (parserError !== null) {
648
+ return { reason: `the nested SVG image is not well-formed XML: ${parserError}` };
649
+ }
650
+ if (!isSvgElement(nestedRoot, "svg")) {
651
+ return { reason: "the nested image's root is not an <svg> element in the SVG namespace" };
652
+ }
653
+ const nestedPath = `${elementPath} > image:svg`;
654
+ const sizing = rootSizing(nestedRoot, (reason, note) => outer.report(nestedPath, "svg", "left-out", reason, note));
655
+ if (sizing.frame === null) {
656
+ return { reason: "the nested SVG image has no size and no viewBox" };
657
+ }
658
+ if (sizing.zeroSide !== null) {
659
+ return {
660
+ reason: `the nested SVG image's ${sizing.zeroSide.of} ${sizing.zeroSide.side} is zero, so nothing inside it draws`,
661
+ };
662
+ }
663
+ // Fit the nested viewport into the image's rect with preserveAspectRatio,
664
+ // then place that rect like the image element itself would be placed.
665
+ const plan = rasterFramePlan(rect, matrix);
666
+ if (plan === null) {
667
+ return { reason: "the image's transform skews it or cannot be inverted, which a frame cannot show" };
668
+ }
669
+ const pass = prePass(nestedRoot, nestedPath, outer.limits, (path, element, reason, note) => outer.report(path, element, "left-out", reason, note), sizing, false);
670
+ const reuse = planReuse(nestedRoot, pass.ids, outer.limits);
671
+ let registry = outer.nestedRegistries.get(svgText);
672
+ if (registry === undefined) {
673
+ registry = new ComponentRegistry();
674
+ if (!outer.probing)
675
+ outer.nestedRegistries.set(svgText, registry);
676
+ }
677
+ const ctx = documentContext(outer, pass, reuse, sizing.viewport, registry);
678
+ const rootStyle = pass.styles.get(nestedRoot) ?? initialComputedStyle();
679
+ if (rootStyle.display === "none")
680
+ return { reason: "the nested SVG image's root has display:none" };
681
+ const frameNode = {
682
+ id: outer.idSource(),
683
+ name: "SVG",
684
+ locked: false,
685
+ ...rootChrome(rootStyle),
686
+ type: "frame",
687
+ frame: plan.frame,
688
+ rotation: plan.rotation,
689
+ ...(plan.flipY ? { flipY: true } : {}),
690
+ children: [],
691
+ layout: { kind: "absolute" },
692
+ clipContent: true,
693
+ };
694
+ // The children are built in the frame's own coordinates: the image
695
+ // element's matrix, then the fit into its rect, then the nested root's
696
+ // viewBox, all seen from inside the placed frame.
697
+ const fit = viewBoxTransform({ minX: 0, minY: 0, width: sizing.frame.width, height: sizing.frame.height }, rect.width, rect.height, aspect);
698
+ const intoRect = multiplyTransforms(multiplyTransforms(matrix, [1, 0, 0, 1, rect.x, rect.y]), fit);
699
+ const fromFrame = invertMatrix(nodeLocalTransform(frameNode));
700
+ const childrenMatrix = multiplyTransforms(fromFrame, multiplyTransforms(intoRect, sizing.childrenMatrix));
701
+ // A <use> cycle inside a nested image refuses only that image, and only
702
+ // when something the image draws reaches it, as in the top-level file.
703
+ // When the image holds a cycle, a probe build (no ids, lines, components
704
+ // or assets kept; its work counts against the import-wide caps) finds out
705
+ // first, so a refusal leaves nothing of the image behind.
706
+ if (reuse.cycles.size > 0) {
707
+ try {
708
+ buildChildren(nestedRoot, rootStyle, childrenMatrix, nestedPath, probeContext(ctx));
709
+ }
710
+ catch (error) {
711
+ if (error instanceof SvgImportRefusalError && error.code === "use-cycle") {
712
+ return { reason: `the nested SVG image was not imported: ${error.message}` };
713
+ }
714
+ throw error;
715
+ }
716
+ }
717
+ reportRootEffects(rootStyle, (reason, note) => outer.report(nestedPath, "svg", "left-out", reason, note));
718
+ reportConditionAttributes(nestedRoot, nestedPath, outer.report);
719
+ // As on the top-level root, a transform on the nested root is left out:
720
+ // the frame is placed by the image element's own box.
721
+ const nestedTransform = rootTransformText(nestedRoot);
722
+ if (nestedTransform !== null) {
723
+ outer.report(nestedPath, "svg", "left-out", `the nested SVG image's root transform "${nestedTransform}" is not applied; the image is placed by its own box`, ROOT_TRANSFORM_NOTE);
724
+ }
725
+ const { children, nodes } = buildChildren(nestedRoot, rootStyle, childrenMatrix, nestedPath, ctx);
726
+ const top = { ...frameNode, children: children.map((child) => child.id) };
727
+ return { built: { top, nodes: [top, ...nodes] } };
728
+ };
729
+ }