blitz-pdf 0.0.0-stage → 0.1.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 (370) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +355 -2
  3. package/THIRD_PARTY_NOTICES.md +25 -0
  4. package/assets/icon.svg +4 -0
  5. package/dist/api.d.ts +7 -0
  6. package/dist/api.d.ts.map +1 -0
  7. package/dist/api.js +46 -0
  8. package/dist/api.js.map +1 -0
  9. package/dist/assets.d.ts +7 -0
  10. package/dist/assets.d.ts.map +1 -0
  11. package/dist/assets.js +52 -0
  12. package/dist/assets.js.map +1 -0
  13. package/dist/browser.d.ts +3 -0
  14. package/dist/browser.d.ts.map +1 -0
  15. package/dist/browser.js +3 -0
  16. package/dist/browser.js.map +1 -0
  17. package/dist/node.d.ts +3 -0
  18. package/dist/node.d.ts.map +1 -0
  19. package/dist/node.js +10 -0
  20. package/dist/node.js.map +1 -0
  21. package/dist/options.d.ts +24 -0
  22. package/dist/options.d.ts.map +1 -0
  23. package/dist/options.js +135 -0
  24. package/dist/options.js.map +1 -0
  25. package/dist/types.d.ts +51 -0
  26. package/dist/types.d.ts.map +1 -0
  27. package/dist/types.js +2 -0
  28. package/dist/types.js.map +1 -0
  29. package/examples/bun.ts +8 -0
  30. package/examples/german-invoice/README.md +70 -0
  31. package/examples/german-invoice/data.mjs +83 -0
  32. package/examples/german-invoice/invoice.css +59 -0
  33. package/examples/german-invoice/template.mjs +49 -0
  34. package/examples/german-invoice.mjs +20 -0
  35. package/examples/invoice.html +29 -0
  36. package/examples/node.mjs +8 -0
  37. package/examples/pagination.html +40 -0
  38. package/examples/pagination.mjs +8 -0
  39. package/licenses/DejaVu.txt +187 -0
  40. package/licenses/RUST_DEPENDENCIES.txt +11577 -0
  41. package/package.json +89 -4
  42. package/src/api.ts +44 -0
  43. package/src/assets.ts +43 -0
  44. package/src/browser.ts +6 -0
  45. package/src/node.ts +12 -0
  46. package/src/options.ts +101 -0
  47. package/src/types.ts +49 -0
  48. package/vendor/README.md +17 -0
  49. package/vendor/stylo/.cargo-ok +1 -0
  50. package/vendor/stylo/.cargo_vcs_info.json +6 -0
  51. package/vendor/stylo/Cargo.lock +1485 -0
  52. package/vendor/stylo/Cargo.toml +274 -0
  53. package/vendor/stylo/LICENSE +373 -0
  54. package/vendor/stylo/README.md +6 -0
  55. package/vendor/stylo/applicable_declarations.rs +328 -0
  56. package/vendor/stylo/author_styles.rs +79 -0
  57. package/vendor/stylo/bezier.rs +176 -0
  58. package/vendor/stylo/bloom.rs +414 -0
  59. package/vendor/stylo/build.rs +91 -0
  60. package/vendor/stylo/build_gecko.rs +393 -0
  61. package/vendor/stylo/color/color_function.rs +601 -0
  62. package/vendor/stylo/color/component.rs +207 -0
  63. package/vendor/stylo/color/convert.rs +935 -0
  64. package/vendor/stylo/color/gamut/raytrace.rs +265 -0
  65. package/vendor/stylo/color/gamut.rs +227 -0
  66. package/vendor/stylo/color/mix.rs +645 -0
  67. package/vendor/stylo/color/mod.rs +750 -0
  68. package/vendor/stylo/color/parsing.rs +770 -0
  69. package/vendor/stylo/color/to_css.rs +297 -0
  70. package/vendor/stylo/context.rs +716 -0
  71. package/vendor/stylo/counter_style/mod.rs +754 -0
  72. package/vendor/stylo/counter_style/predefined.rs +61 -0
  73. package/vendor/stylo/counter_style/update_predefined.py +35 -0
  74. package/vendor/stylo/custom_properties.rs +2023 -0
  75. package/vendor/stylo/custom_properties_map.rs +220 -0
  76. package/vendor/stylo/data.rs +693 -0
  77. package/vendor/stylo/device/gecko.rs +486 -0
  78. package/vendor/stylo/device/mod.rs +304 -0
  79. package/vendor/stylo/device/servo.rs +507 -0
  80. package/vendor/stylo/dom.rs +1164 -0
  81. package/vendor/stylo/dom_apis.rs +937 -0
  82. package/vendor/stylo/driver.rs +172 -0
  83. package/vendor/stylo/error_reporting.rs +484 -0
  84. package/vendor/stylo/font_face.rs +558 -0
  85. package/vendor/stylo/font_metrics.rs +124 -0
  86. package/vendor/stylo/gecko/anon_boxes.toml +113 -0
  87. package/vendor/stylo/gecko/arc_types.rs +211 -0
  88. package/vendor/stylo/gecko/conversions.rs +59 -0
  89. package/vendor/stylo/gecko/data.rs +222 -0
  90. package/vendor/stylo/gecko/media_features.rs +971 -0
  91. package/vendor/stylo/gecko/mod.rs +20 -0
  92. package/vendor/stylo/gecko/non_ts_pseudo_class_list.rs +119 -0
  93. package/vendor/stylo/gecko/pseudo_element.rs +640 -0
  94. package/vendor/stylo/gecko/pseudo_element_definition.mako.rs +298 -0
  95. package/vendor/stylo/gecko/pseudo_elements.py +137 -0
  96. package/vendor/stylo/gecko/pseudo_elements.toml +197 -0
  97. package/vendor/stylo/gecko/regen_pseudo_elements.py +33 -0
  98. package/vendor/stylo/gecko/restyle_damage.rs +127 -0
  99. package/vendor/stylo/gecko/selector_parser.rs +627 -0
  100. package/vendor/stylo/gecko/snapshot.rs +202 -0
  101. package/vendor/stylo/gecko/snapshot_helpers.rs +334 -0
  102. package/vendor/stylo/gecko/traversal.rs +52 -0
  103. package/vendor/stylo/gecko/wrapper.rs +2258 -0
  104. package/vendor/stylo/gecko_bindings/mod.rs +34 -0
  105. package/vendor/stylo/gecko_bindings/sugar/mod.rs +12 -0
  106. package/vendor/stylo/gecko_bindings/sugar/ns_com_ptr.rs +15 -0
  107. package/vendor/stylo/gecko_bindings/sugar/ns_compatibility.rs +19 -0
  108. package/vendor/stylo/gecko_bindings/sugar/ns_style_auto_array.rs +111 -0
  109. package/vendor/stylo/gecko_bindings/sugar/origin_flags.rs +31 -0
  110. package/vendor/stylo/gecko_bindings/sugar/ownership.rs +70 -0
  111. package/vendor/stylo/gecko_bindings/sugar/refptr.rs +299 -0
  112. package/vendor/stylo/gecko_string_cache/mod.rs +543 -0
  113. package/vendor/stylo/gecko_string_cache/namespace.rs +106 -0
  114. package/vendor/stylo/global_style_data.rs +224 -0
  115. package/vendor/stylo/invalidation/element/document_state.rs +156 -0
  116. package/vendor/stylo/invalidation/element/element_wrapper.rs +432 -0
  117. package/vendor/stylo/invalidation/element/invalidation_map.rs +1631 -0
  118. package/vendor/stylo/invalidation/element/invalidator.rs +1470 -0
  119. package/vendor/stylo/invalidation/element/mod.rs +13 -0
  120. package/vendor/stylo/invalidation/element/relative_selector.rs +1399 -0
  121. package/vendor/stylo/invalidation/element/restyle_hints.rs +226 -0
  122. package/vendor/stylo/invalidation/element/state_and_attributes.rs +756 -0
  123. package/vendor/stylo/invalidation/media_queries.rs +144 -0
  124. package/vendor/stylo/invalidation/mod.rs +10 -0
  125. package/vendor/stylo/invalidation/stylesheets.rs +745 -0
  126. package/vendor/stylo/invalidation/viewport_units.rs +71 -0
  127. package/vendor/stylo/lib.rs +336 -0
  128. package/vendor/stylo/logical_geometry.rs +1684 -0
  129. package/vendor/stylo/macros.rs +70 -0
  130. package/vendor/stylo/matching.rs +1226 -0
  131. package/vendor/stylo/media_queries/media_list.rs +168 -0
  132. package/vendor/stylo/media_queries/media_query.rs +190 -0
  133. package/vendor/stylo/media_queries/mod.rs +13 -0
  134. package/vendor/stylo/parallel.rs +205 -0
  135. package/vendor/stylo/parser.rs +276 -0
  136. package/vendor/stylo/piecewise_linear.rs +282 -0
  137. package/vendor/stylo/prefs.rs +33 -0
  138. package/vendor/stylo/properties/build.py +119 -0
  139. package/vendor/stylo/properties/cascade.rs +2704 -0
  140. package/vendor/stylo/properties/computed_value_flags.rs +206 -0
  141. package/vendor/stylo/properties/counted_unknown_properties.py +104 -0
  142. package/vendor/stylo/properties/counter_style_descriptors.toml +45 -0
  143. package/vendor/stylo/properties/data.py +1375 -0
  144. package/vendor/stylo/properties/declaration_block.rs +1788 -0
  145. package/vendor/stylo/properties/font_face_descriptors.toml +50 -0
  146. package/vendor/stylo/properties/gecko.mako.rs +1555 -0
  147. package/vendor/stylo/properties/helpers/animated_properties.mako.rs +800 -0
  148. package/vendor/stylo/properties/helpers.mako.rs +812 -0
  149. package/vendor/stylo/properties/longhands.toml +4305 -0
  150. package/vendor/stylo/properties/mod.rs +1857 -0
  151. package/vendor/stylo/properties/properties.html.mako +31 -0
  152. package/vendor/stylo/properties/properties.mako.rs +3103 -0
  153. package/vendor/stylo/properties/property_descriptors.toml +18 -0
  154. package/vendor/stylo/properties/shorthands.rs +4241 -0
  155. package/vendor/stylo/properties/shorthands.toml +575 -0
  156. package/vendor/stylo/properties/vendored_python/mako-1.3.10-py3-none-any.whl +0 -0
  157. package/vendor/stylo/properties/vendored_python/markupsafe/LICENSE.txt +28 -0
  158. package/vendor/stylo/properties/vendored_python/markupsafe/__init__.py +384 -0
  159. package/vendor/stylo/properties/vendored_python/toml-0.10.2-py2.py3-none-any.whl +0 -0
  160. package/vendor/stylo/properties/view_transition_descriptors.toml +11 -0
  161. package/vendor/stylo/properties_and_values/mod.rs +12 -0
  162. package/vendor/stylo/properties_and_values/registry.rs +65 -0
  163. package/vendor/stylo/properties_and_values/rule.rs +331 -0
  164. package/vendor/stylo/properties_and_values/syntax/ascii.rs +60 -0
  165. package/vendor/stylo/properties_and_values/syntax/data_type.rs +146 -0
  166. package/vendor/stylo/properties_and_values/syntax/mod.rs +437 -0
  167. package/vendor/stylo/properties_and_values/value.rs +758 -0
  168. package/vendor/stylo/queries/condition.rs +996 -0
  169. package/vendor/stylo/queries/feature.rs +199 -0
  170. package/vendor/stylo/queries/feature_expression.rs +1170 -0
  171. package/vendor/stylo/queries/mod.rs +19 -0
  172. package/vendor/stylo/queries/values.rs +46 -0
  173. package/vendor/stylo/rule_cache.rs +267 -0
  174. package/vendor/stylo/rule_collector.rs +554 -0
  175. package/vendor/stylo/rule_tree/core.rs +787 -0
  176. package/vendor/stylo/rule_tree/level.rs +396 -0
  177. package/vendor/stylo/rule_tree/map.rs +201 -0
  178. package/vendor/stylo/rule_tree/mod.rs +427 -0
  179. package/vendor/stylo/rule_tree/source.rs +80 -0
  180. package/vendor/stylo/rule_tree/unsafe_box.rs +78 -0
  181. package/vendor/stylo/scoped_tls.rs +80 -0
  182. package/vendor/stylo/selector_map.rs +1050 -0
  183. package/vendor/stylo/selector_parser.rs +305 -0
  184. package/vendor/stylo/servo/animation.rs +1922 -0
  185. package/vendor/stylo/servo/attr.rs +714 -0
  186. package/vendor/stylo/servo/encoding_support.rs +81 -0
  187. package/vendor/stylo/servo/media_features.rs +254 -0
  188. package/vendor/stylo/servo/mod.rs +16 -0
  189. package/vendor/stylo/servo/restyle_damage.rs +215 -0
  190. package/vendor/stylo/servo/selector_parser.rs +1036 -0
  191. package/vendor/stylo/servo/shadow_parts.rs +207 -0
  192. package/vendor/stylo/shared_lock.rs +271 -0
  193. package/vendor/stylo/sharing/checks.rs +281 -0
  194. package/vendor/stylo/sharing/mod.rs +1000 -0
  195. package/vendor/stylo/simple_buckets_map.rs +52 -0
  196. package/vendor/stylo/str.rs +168 -0
  197. package/vendor/stylo/style_adjuster.rs +1086 -0
  198. package/vendor/stylo/style_resolver.rs +658 -0
  199. package/vendor/stylo/stylesheet_set.rs +658 -0
  200. package/vendor/stylo/stylesheets/appearance_base_rule.rs +51 -0
  201. package/vendor/stylo/stylesheets/container_rule.rs +769 -0
  202. package/vendor/stylo/stylesheets/counter_style_rule.rs +7 -0
  203. package/vendor/stylo/stylesheets/document_rule.rs +272 -0
  204. package/vendor/stylo/stylesheets/font_face_rule.rs +7 -0
  205. package/vendor/stylo/stylesheets/font_feature_values_rule.rs +481 -0
  206. package/vendor/stylo/stylesheets/font_palette_values_rule.rs +304 -0
  207. package/vendor/stylo/stylesheets/import_rule.rs +259 -0
  208. package/vendor/stylo/stylesheets/keyframes_rule.rs +733 -0
  209. package/vendor/stylo/stylesheets/layer_rule.rs +221 -0
  210. package/vendor/stylo/stylesheets/loader.rs +29 -0
  211. package/vendor/stylo/stylesheets/margin_rule.rs +190 -0
  212. package/vendor/stylo/stylesheets/media_rule.rs +191 -0
  213. package/vendor/stylo/stylesheets/mod.rs +918 -0
  214. package/vendor/stylo/stylesheets/namespace_rule.rs +43 -0
  215. package/vendor/stylo/stylesheets/nested_declarations_rule.rs +51 -0
  216. package/vendor/stylo/stylesheets/origin.rs +249 -0
  217. package/vendor/stylo/stylesheets/page_rule.rs +354 -0
  218. package/vendor/stylo/stylesheets/position_try_rule.rs +68 -0
  219. package/vendor/stylo/stylesheets/property_rule.rs +5 -0
  220. package/vendor/stylo/stylesheets/rule_list.rs +167 -0
  221. package/vendor/stylo/stylesheets/rule_parser.rs +1189 -0
  222. package/vendor/stylo/stylesheets/rules_iterator.rs +397 -0
  223. package/vendor/stylo/stylesheets/scope_rule.rs +511 -0
  224. package/vendor/stylo/stylesheets/starting_style_rule.rs +54 -0
  225. package/vendor/stylo/stylesheets/style_rule.rs +80 -0
  226. package/vendor/stylo/stylesheets/stylesheet.rs +570 -0
  227. package/vendor/stylo/stylesheets/supports_rule.rs +486 -0
  228. package/vendor/stylo/stylesheets/view_transition_rule.rs +81 -0
  229. package/vendor/stylo/stylist.rs +5076 -0
  230. package/vendor/stylo/thread_state.rs +87 -0
  231. package/vendor/stylo/traversal.rs +765 -0
  232. package/vendor/stylo/traversal_flags.rs +68 -0
  233. package/vendor/stylo/typed_om/mod.rs +981 -0
  234. package/vendor/stylo/typed_om/numeric.rs +128 -0
  235. package/vendor/stylo/typed_om/numeric_declaration.rs +93 -0
  236. package/vendor/stylo/typed_om/numeric_type.rs +640 -0
  237. package/vendor/stylo/typed_om/sum_value.rs +426 -0
  238. package/vendor/stylo/url/gecko.rs +367 -0
  239. package/vendor/stylo/url/mod.rs +71 -0
  240. package/vendor/stylo/url/servo.rs +239 -0
  241. package/vendor/stylo/use_counters/mod.rs +137 -0
  242. package/vendor/stylo/values/animated/color.rs +103 -0
  243. package/vendor/stylo/values/animated/effects.rs +26 -0
  244. package/vendor/stylo/values/animated/font.rs +37 -0
  245. package/vendor/stylo/values/animated/grid.rs +161 -0
  246. package/vendor/stylo/values/animated/lists.rs +141 -0
  247. package/vendor/stylo/values/animated/mod.rs +550 -0
  248. package/vendor/stylo/values/animated/svg.rs +46 -0
  249. package/vendor/stylo/values/animated/text.rs +58 -0
  250. package/vendor/stylo/values/animated/transform.rs +1592 -0
  251. package/vendor/stylo/values/computed/align.rs +90 -0
  252. package/vendor/stylo/values/computed/angle.rs +130 -0
  253. package/vendor/stylo/values/computed/animation.rs +124 -0
  254. package/vendor/stylo/values/computed/background.rs +13 -0
  255. package/vendor/stylo/values/computed/basic_shape.rs +267 -0
  256. package/vendor/stylo/values/computed/border.rs +163 -0
  257. package/vendor/stylo/values/computed/box.rs +267 -0
  258. package/vendor/stylo/values/computed/calc.rs +334 -0
  259. package/vendor/stylo/values/computed/color.rs +161 -0
  260. package/vendor/stylo/values/computed/column.rs +11 -0
  261. package/vendor/stylo/values/computed/corner_shape.rs +155 -0
  262. package/vendor/stylo/values/computed/counters.rs +26 -0
  263. package/vendor/stylo/values/computed/easing.rs +109 -0
  264. package/vendor/stylo/values/computed/effects.rs +30 -0
  265. package/vendor/stylo/values/computed/flex.rs +19 -0
  266. package/vendor/stylo/values/computed/font.rs +1517 -0
  267. package/vendor/stylo/values/computed/image.rs +273 -0
  268. package/vendor/stylo/values/computed/length.rs +576 -0
  269. package/vendor/stylo/values/computed/length_percentage.rs +1055 -0
  270. package/vendor/stylo/values/computed/list.rs +16 -0
  271. package/vendor/stylo/values/computed/mod.rs +1141 -0
  272. package/vendor/stylo/values/computed/motion.rs +73 -0
  273. package/vendor/stylo/values/computed/outline.rs +7 -0
  274. package/vendor/stylo/values/computed/page.rs +77 -0
  275. package/vendor/stylo/values/computed/param.rs +9 -0
  276. package/vendor/stylo/values/computed/percentage.rs +143 -0
  277. package/vendor/stylo/values/computed/position.rs +273 -0
  278. package/vendor/stylo/values/computed/ratio.rs +88 -0
  279. package/vendor/stylo/values/computed/rect.rs +19 -0
  280. package/vendor/stylo/values/computed/resolution.rs +76 -0
  281. package/vendor/stylo/values/computed/svg.rs +66 -0
  282. package/vendor/stylo/values/computed/table.rs +7 -0
  283. package/vendor/stylo/values/computed/text.rs +199 -0
  284. package/vendor/stylo/values/computed/time.rs +83 -0
  285. package/vendor/stylo/values/computed/transform.rs +559 -0
  286. package/vendor/stylo/values/computed/tree_counting.rs +30 -0
  287. package/vendor/stylo/values/computed/ui.rs +23 -0
  288. package/vendor/stylo/values/computed/url.rs +12 -0
  289. package/vendor/stylo/values/distance.rs +139 -0
  290. package/vendor/stylo/values/generics/animation.rs +363 -0
  291. package/vendor/stylo/values/generics/background.rs +65 -0
  292. package/vendor/stylo/values/generics/basic_shape.rs +1389 -0
  293. package/vendor/stylo/values/generics/border.rs +303 -0
  294. package/vendor/stylo/values/generics/box.rs +552 -0
  295. package/vendor/stylo/values/generics/calc.rs +2986 -0
  296. package/vendor/stylo/values/generics/color.rs +318 -0
  297. package/vendor/stylo/values/generics/column.rs +44 -0
  298. package/vendor/stylo/values/generics/counters.rs +320 -0
  299. package/vendor/stylo/values/generics/easing.rs +132 -0
  300. package/vendor/stylo/values/generics/effects.rs +131 -0
  301. package/vendor/stylo/values/generics/flex.rs +34 -0
  302. package/vendor/stylo/values/generics/font.rs +376 -0
  303. package/vendor/stylo/values/generics/grid.rs +962 -0
  304. package/vendor/stylo/values/generics/image.rs +653 -0
  305. package/vendor/stylo/values/generics/length.rs +656 -0
  306. package/vendor/stylo/values/generics/mod.rs +303 -0
  307. package/vendor/stylo/values/generics/motion.rs +274 -0
  308. package/vendor/stylo/values/generics/page.rs +167 -0
  309. package/vendor/stylo/values/generics/position.rs +669 -0
  310. package/vendor/stylo/values/generics/ratio.rs +88 -0
  311. package/vendor/stylo/values/generics/rect.rs +147 -0
  312. package/vendor/stylo/values/generics/size.rs +102 -0
  313. package/vendor/stylo/values/generics/svg.rs +226 -0
  314. package/vendor/stylo/values/generics/text.rs +268 -0
  315. package/vendor/stylo/values/generics/transform.rs +1157 -0
  316. package/vendor/stylo/values/generics/ui.rs +147 -0
  317. package/vendor/stylo/values/generics/url.rs +51 -0
  318. package/vendor/stylo/values/mod.rs +820 -0
  319. package/vendor/stylo/values/resolved/animation.rs +33 -0
  320. package/vendor/stylo/values/resolved/color.rs +53 -0
  321. package/vendor/stylo/values/resolved/counters.rs +45 -0
  322. package/vendor/stylo/values/resolved/mod.rs +316 -0
  323. package/vendor/stylo/values/specified/align.rs +711 -0
  324. package/vendor/stylo/values/specified/angle.rs +407 -0
  325. package/vendor/stylo/values/specified/animation.rs +921 -0
  326. package/vendor/stylo/values/specified/background.rs +270 -0
  327. package/vendor/stylo/values/specified/basic_shape.rs +986 -0
  328. package/vendor/stylo/values/specified/border.rs +430 -0
  329. package/vendor/stylo/values/specified/box.rs +2231 -0
  330. package/vendor/stylo/values/specified/calc.rs +1657 -0
  331. package/vendor/stylo/values/specified/color.rs +1210 -0
  332. package/vendor/stylo/values/specified/column.rs +11 -0
  333. package/vendor/stylo/values/specified/corner_shape.rs +152 -0
  334. package/vendor/stylo/values/specified/counters.rs +242 -0
  335. package/vendor/stylo/values/specified/easing.rs +225 -0
  336. package/vendor/stylo/values/specified/effects.rs +421 -0
  337. package/vendor/stylo/values/specified/flex.rs +42 -0
  338. package/vendor/stylo/values/specified/font.rs +2103 -0
  339. package/vendor/stylo/values/specified/frequency.rs +39 -0
  340. package/vendor/stylo/values/specified/grid.rs +407 -0
  341. package/vendor/stylo/values/specified/image.rs +1385 -0
  342. package/vendor/stylo/values/specified/intersection_observer.rs +80 -0
  343. package/vendor/stylo/values/specified/length.rs +2024 -0
  344. package/vendor/stylo/values/specified/list.rs +190 -0
  345. package/vendor/stylo/values/specified/mod.rs +531 -0
  346. package/vendor/stylo/values/specified/motion.rs +316 -0
  347. package/vendor/stylo/values/specified/number.rs +603 -0
  348. package/vendor/stylo/values/specified/outline.rs +69 -0
  349. package/vendor/stylo/values/specified/page.rs +94 -0
  350. package/vendor/stylo/values/specified/param.rs +121 -0
  351. package/vendor/stylo/values/specified/percentage.rs +364 -0
  352. package/vendor/stylo/values/specified/position.rs +2247 -0
  353. package/vendor/stylo/values/specified/ratio.rs +29 -0
  354. package/vendor/stylo/values/specified/rect.rs +28 -0
  355. package/vendor/stylo/values/specified/resolution.rs +237 -0
  356. package/vendor/stylo/values/specified/source_size_list.rs +135 -0
  357. package/vendor/stylo/values/specified/svg.rs +427 -0
  358. package/vendor/stylo/values/specified/svg_path.rs +1115 -0
  359. package/vendor/stylo/values/specified/table.rs +38 -0
  360. package/vendor/stylo/values/specified/text.rs +1561 -0
  361. package/vendor/stylo/values/specified/time.rs +313 -0
  362. package/vendor/stylo/values/specified/transform.rs +599 -0
  363. package/vendor/stylo/values/specified/tree_counting.rs +32 -0
  364. package/vendor/stylo/values/specified/ui.rs +344 -0
  365. package/vendor/stylo/values/specified/url.rs +12 -0
  366. package/vendor/stylo/values/tagged_numeric.rs +253 -0
  367. package/wasm/blitz_pdf.d.ts +67 -0
  368. package/wasm/blitz_pdf.js +699 -0
  369. package/wasm/blitz_pdf_bg.wasm +0 -0
  370. package/wasm/blitz_pdf_bg.wasm.d.ts +20 -0
@@ -0,0 +1,787 @@
1
+ /* This Source Code Form is subject to the terms of the Mozilla Public
2
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
3
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
+
5
+ #![allow(unsafe_code)]
6
+
7
+ use crate::applicable_declarations::CascadePriority;
8
+ use crate::shared_lock::StylesheetGuards;
9
+ use crate::stylesheets::layer_rule::LayerOrder;
10
+ use malloc_size_of::{MallocShallowSizeOf, MallocSizeOf, MallocSizeOfOps};
11
+ use parking_lot::RwLock;
12
+ use smallvec::SmallVec;
13
+ use std::fmt;
14
+ use std::hash;
15
+ use std::io::Write;
16
+ use std::mem;
17
+ use std::ptr;
18
+ use std::sync::atomic::{self, AtomicPtr, AtomicUsize, Ordering};
19
+
20
+ use super::map::{Entry, Map};
21
+ use super::unsafe_box::UnsafeBox;
22
+ use super::{CascadeLevel, CascadeOrigin, RuleCascadeFlags, StyleSource};
23
+
24
+ /// The rule tree, the structure servo uses to preserve the results of selector
25
+ /// matching.
26
+ ///
27
+ /// This is organized as a tree of rules. When a node matches a set of rules,
28
+ /// they're inserted in order in the tree, starting with the less specific one.
29
+ ///
30
+ /// When a rule is inserted in the tree, other elements may share the path up to
31
+ /// a given rule. If that's the case, we don't duplicate child nodes, but share
32
+ /// them.
33
+ ///
34
+ /// When the rule node refcount drops to zero, it doesn't get freed. It gets
35
+ /// instead put into a free list, and it is potentially GC'd after a while.
36
+ ///
37
+ /// That way, a rule node that represents a likely-to-match-again rule (like a
38
+ /// :hover rule) can be reused if we haven't GC'd it yet.
39
+ #[derive(Debug)]
40
+ pub struct RuleTree {
41
+ root: StrongRuleNode,
42
+ }
43
+
44
+ impl Drop for RuleTree {
45
+ fn drop(&mut self) {
46
+ unsafe { self.swap_free_list_and_gc(ptr::null_mut()) }
47
+ }
48
+ }
49
+
50
+ impl MallocSizeOf for RuleTree {
51
+ fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
52
+ let mut n = 0;
53
+ let mut stack = SmallVec::<[_; 32]>::new();
54
+ stack.push(self.root.clone());
55
+
56
+ while let Some(node) = stack.pop() {
57
+ n += unsafe { ops.malloc_size_of(&*node.p) };
58
+ let children = node.p.children.read();
59
+ children.shallow_size_of(ops);
60
+ for c in &*children {
61
+ stack.push(unsafe { c.upgrade() });
62
+ }
63
+ }
64
+
65
+ n
66
+ }
67
+ }
68
+
69
+ #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
70
+ struct ChildKey(CascadePriority, ptr::NonNull<()>);
71
+ unsafe impl Send for ChildKey {}
72
+ unsafe impl Sync for ChildKey {}
73
+
74
+ impl Default for RuleTree {
75
+ fn default() -> Self {
76
+ Self::new()
77
+ }
78
+ }
79
+
80
+ impl RuleTree {
81
+ /// Construct a new rule tree.
82
+ pub fn new() -> Self {
83
+ RuleTree {
84
+ root: StrongRuleNode::new(Box::new(RuleNode::root())),
85
+ }
86
+ }
87
+
88
+ /// Get the root rule node.
89
+ pub fn root(&self) -> &StrongRuleNode {
90
+ &self.root
91
+ }
92
+
93
+ /// This can only be called when no other threads is accessing this tree.
94
+ pub fn gc(&self) {
95
+ unsafe { self.swap_free_list_and_gc(RuleNode::DANGLING_PTR) }
96
+ }
97
+
98
+ /// This can only be called when no other threads is accessing this tree.
99
+ pub fn maybe_gc(&self) {
100
+ #[cfg(debug_assertions)]
101
+ self.maybe_dump_stats();
102
+
103
+ if self.root.p.approximate_free_count.load(Ordering::Relaxed) > RULE_TREE_GC_INTERVAL {
104
+ self.gc();
105
+ }
106
+ }
107
+
108
+ #[cfg(debug_assertions)]
109
+ fn maybe_dump_stats(&self) {
110
+ use itertools::Itertools;
111
+ use std::cell::Cell;
112
+ use std::time::{Duration, Instant};
113
+
114
+ if !log_enabled!(log::Level::Trace) {
115
+ return;
116
+ }
117
+
118
+ const RULE_TREE_STATS_INTERVAL: Duration = Duration::from_secs(2);
119
+
120
+ thread_local! {
121
+ pub static LAST_STATS: Cell<Instant> = Cell::new(Instant::now());
122
+ };
123
+
124
+ let should_dump = LAST_STATS.with(|s| {
125
+ let now = Instant::now();
126
+ if now.duration_since(s.get()) < RULE_TREE_STATS_INTERVAL {
127
+ return false;
128
+ }
129
+ s.set(now);
130
+ true
131
+ });
132
+
133
+ if !should_dump {
134
+ return;
135
+ }
136
+
137
+ let mut children_count = rustc_hash::FxHashMap::default();
138
+
139
+ let mut stack = SmallVec::<[_; 32]>::new();
140
+ stack.push(self.root.clone());
141
+ while let Some(node) = stack.pop() {
142
+ let children = node.p.children.read();
143
+ *children_count.entry(children.len()).or_insert(0) += 1;
144
+ for c in &*children {
145
+ stack.push(unsafe { c.upgrade() });
146
+ }
147
+ }
148
+
149
+ trace!("Rule tree stats:");
150
+ let counts = children_count.keys().sorted();
151
+ for count in counts {
152
+ trace!(" {} - {}", count, children_count[count]);
153
+ }
154
+ }
155
+
156
+ /// Steals the free list and drops its contents.
157
+ unsafe fn swap_free_list_and_gc(&self, ptr: *mut RuleNode) {
158
+ let root = &self.root.p;
159
+
160
+ debug_assert!(!root.next_free.load(Ordering::Relaxed).is_null());
161
+
162
+ // Reset the approximate free count to zero, as we are going to steal
163
+ // the free list.
164
+ root.approximate_free_count.store(0, Ordering::Relaxed);
165
+
166
+ // Steal the free list head. Memory loads on nodes while iterating it
167
+ // must observe any prior changes that occured so this requires
168
+ // acquire ordering, but there are no writes that need to be kept
169
+ // before this swap so there is no need for release.
170
+ let mut head = root.next_free.swap(ptr, Ordering::Acquire);
171
+
172
+ while head != RuleNode::DANGLING_PTR {
173
+ debug_assert!(!head.is_null());
174
+
175
+ let mut node = unsafe { UnsafeBox::from_raw(head) };
176
+
177
+ // The root node cannot go on the free list.
178
+ debug_assert!(node.root.is_some());
179
+
180
+ // The refcount of nodes on the free list never goes below 1.
181
+ debug_assert!(node.refcount.load(Ordering::Relaxed) > 0);
182
+
183
+ // No one else is currently writing to that field. Get the address
184
+ // of the next node in the free list and replace it with null,
185
+ // other threads will now consider that this node is not on the
186
+ // free list.
187
+ head = node.next_free.swap(ptr::null_mut(), Ordering::Relaxed);
188
+
189
+ // This release write synchronises with the acquire fence in
190
+ // `WeakRuleNode::upgrade`, making sure that if `upgrade` observes
191
+ // decrements the refcount to 0, it will also observe the
192
+ // `node.next_free` swap to null above.
193
+ if node.refcount.fetch_sub(1, Ordering::Release) == 1 {
194
+ unsafe {
195
+ // And given it observed the null swap above, it will need
196
+ // `pretend_to_be_on_free_list` to finish its job, writing
197
+ // `RuleNode::DANGLING_PTR` in `node.next_free`.
198
+ RuleNode::pretend_to_be_on_free_list(&node);
199
+ // Drop this node now that we just observed its refcount going down to zero.
200
+ RuleNode::drop_without_free_list(&mut node);
201
+ }
202
+ }
203
+ }
204
+ }
205
+ }
206
+
207
+ /// The number of RuleNodes added to the free list before we will consider
208
+ /// doing a GC when calling maybe_gc(). (The value is copied from Gecko,
209
+ /// where it likely did not result from a rigorous performance analysis.)
210
+ const RULE_TREE_GC_INTERVAL: usize = 300;
211
+
212
+ /// A node in the rule tree.
213
+ struct RuleNode {
214
+ /// The root node. Only the root has no root pointer, for obvious reasons.
215
+ root: Option<WeakRuleNode>,
216
+
217
+ /// The parent rule node. Only the root has no parent.
218
+ parent: Option<StrongRuleNode>,
219
+
220
+ /// The actual style source, either coming from a selector in a StyleRule,
221
+ /// or a raw property declaration block (like the style attribute).
222
+ ///
223
+ /// None for the root node.
224
+ source: Option<StyleSource>,
225
+
226
+ /// The cascade level + layer order this rule is positioned at.
227
+ cascade_priority: CascadePriority,
228
+
229
+ /// The refcount of this node.
230
+ ///
231
+ /// Starts at one. Incremented in `StrongRuleNode::clone` and
232
+ /// `WeakRuleNode::upgrade`. Decremented in `StrongRuleNode::drop`
233
+ /// and `RuleTree::swap_free_list_and_gc`.
234
+ ///
235
+ /// If a non-root node's refcount reaches zero, it is incremented back to at
236
+ /// least one in `RuleNode::pretend_to_be_on_free_list` until the caller who
237
+ /// observed it dropping to zero had a chance to try to remove it from its
238
+ /// parent's children list.
239
+ ///
240
+ /// The refcount should never be decremented to zero if the value in
241
+ /// `next_free` is not null.
242
+ refcount: AtomicUsize,
243
+
244
+ /// Only used for the root, stores the number of free rule nodes that are
245
+ /// around.
246
+ approximate_free_count: AtomicUsize,
247
+
248
+ /// The children of a given rule node. Children remove themselves from here
249
+ /// when they go away.
250
+ children: RwLock<Map<ChildKey, WeakRuleNode>>,
251
+
252
+ /// This field has two different meanings depending on whether this is the
253
+ /// root node or not.
254
+ ///
255
+ /// If it is the root, it represents the head of the free list. It may be
256
+ /// null, which means the free list is gone because the tree was dropped,
257
+ /// and it may be `RuleNode::DANGLING_PTR`, which means the free list is
258
+ /// empty.
259
+ ///
260
+ /// If it is not the root node, this field is either null if the node is
261
+ /// not on the free list, `RuleNode::DANGLING_PTR` if it is the last item
262
+ /// on the free list or the node is pretending to be on the free list, or
263
+ /// any valid non-null pointer representing the next item on the free list
264
+ /// after this one.
265
+ ///
266
+ /// See `RuleNode::push_on_free_list`, `swap_free_list_and_gc`, and
267
+ /// `WeakRuleNode::upgrade`.
268
+ ///
269
+ /// Two threads should never attempt to put the same node on the free list
270
+ /// both at the same time.
271
+ next_free: AtomicPtr<RuleNode>,
272
+ }
273
+
274
+ // On Gecko builds, hook into the leak checking machinery.
275
+ #[cfg(feature = "gecko_refcount_logging")]
276
+ mod gecko_leak_checking {
277
+ use super::RuleNode;
278
+ use std::mem::size_of;
279
+ use std::os::raw::{c_char, c_void};
280
+
281
+ unsafe extern "C" {
282
+ fn NS_LogCtor(aPtr: *mut c_void, aTypeName: *const c_char, aSize: u32);
283
+ fn NS_LogDtor(aPtr: *mut c_void, aTypeName: *const c_char, aSize: u32);
284
+ }
285
+ static NAME: &'static [u8] = b"RuleNode\0";
286
+
287
+ /// Logs the creation of a heap-allocated object to Gecko's leak-checking machinery.
288
+ pub(super) fn log_ctor(ptr: *const RuleNode) {
289
+ let s = NAME as *const [u8] as *const u8 as *const c_char;
290
+ unsafe {
291
+ NS_LogCtor(ptr as *mut c_void, s, size_of::<RuleNode>() as u32);
292
+ }
293
+ }
294
+
295
+ /// Logs the destruction of a heap-allocated object to Gecko's leak-checking machinery.
296
+ pub(super) fn log_dtor(ptr: *const RuleNode) {
297
+ let s = NAME as *const [u8] as *const u8 as *const c_char;
298
+ unsafe {
299
+ NS_LogDtor(ptr as *mut c_void, s, size_of::<RuleNode>() as u32);
300
+ }
301
+ }
302
+ }
303
+
304
+ #[inline(always)]
305
+ fn log_new(_ptr: *const RuleNode) {
306
+ #[cfg(feature = "gecko_refcount_logging")]
307
+ gecko_leak_checking::log_ctor(_ptr);
308
+ }
309
+
310
+ #[inline(always)]
311
+ fn log_drop(_ptr: *const RuleNode) {
312
+ #[cfg(feature = "gecko_refcount_logging")]
313
+ gecko_leak_checking::log_dtor(_ptr);
314
+ }
315
+
316
+ impl RuleNode {
317
+ const DANGLING_PTR: *mut Self = ptr::NonNull::dangling().as_ptr();
318
+
319
+ unsafe fn new(
320
+ root: WeakRuleNode,
321
+ parent: StrongRuleNode,
322
+ source: StyleSource,
323
+ cascade_priority: CascadePriority,
324
+ ) -> Self {
325
+ debug_assert!(root.p.parent.is_none());
326
+ source.mark_in_rule_tree();
327
+ RuleNode {
328
+ root: Some(root),
329
+ parent: Some(parent),
330
+ source: Some(source),
331
+ cascade_priority,
332
+ refcount: AtomicUsize::new(1),
333
+ children: Default::default(),
334
+ approximate_free_count: AtomicUsize::new(0),
335
+ next_free: AtomicPtr::new(ptr::null_mut()),
336
+ }
337
+ }
338
+
339
+ fn root() -> Self {
340
+ RuleNode {
341
+ root: None,
342
+ parent: None,
343
+ source: None,
344
+ cascade_priority: CascadePriority::new(
345
+ CascadeLevel::new(CascadeOrigin::UA),
346
+ LayerOrder::root(),
347
+ RuleCascadeFlags::empty(),
348
+ ),
349
+ refcount: AtomicUsize::new(1),
350
+ approximate_free_count: AtomicUsize::new(0),
351
+ children: Default::default(),
352
+ next_free: AtomicPtr::new(RuleNode::DANGLING_PTR),
353
+ }
354
+ }
355
+
356
+ fn key(&self) -> ChildKey {
357
+ ChildKey(
358
+ self.cascade_priority,
359
+ self.source
360
+ .as_ref()
361
+ .expect("Called key() on the root node")
362
+ .key(),
363
+ )
364
+ }
365
+
366
+ /// Drops a node without ever putting it on the free list.
367
+ ///
368
+ /// Note that the node may not be dropped if we observe that its refcount
369
+ /// isn't zero anymore when we write-lock its parent's children map to
370
+ /// remove it.
371
+ ///
372
+ /// This loops over parents of dropped nodes if their own refcount reaches
373
+ /// zero to avoid recursion when dropping deep hierarchies of nodes.
374
+ ///
375
+ /// For non-root nodes, this should always be preceded by a call of
376
+ /// `RuleNode::pretend_to_be_on_free_list`.
377
+ unsafe fn drop_without_free_list(this: &mut UnsafeBox<Self>) {
378
+ // We clone the box and shadow the original one to be able to loop
379
+ // over its ancestors if they also need to be dropped.
380
+ let mut this = unsafe { UnsafeBox::clone(this) };
381
+ loop {
382
+ // If the node has a parent, we need to remove it from its parent's
383
+ // children list.
384
+ if let Some(parent) = this.parent.as_ref() {
385
+ debug_assert!(!this.next_free.load(Ordering::Relaxed).is_null());
386
+
387
+ // We lock the parent's children list, which means no other
388
+ // thread will have any more opportunity to resurrect the node
389
+ // anymore.
390
+ let mut children = parent.p.children.write();
391
+
392
+ this.next_free.store(ptr::null_mut(), Ordering::Relaxed);
393
+
394
+ // We decrement the counter to remove the "pretend to be
395
+ // on the free list" reference.
396
+ let old_refcount = this.refcount.fetch_sub(1, Ordering::Release);
397
+ debug_assert!(old_refcount != 0);
398
+ if old_refcount != 1 {
399
+ // Other threads resurrected this node and those references
400
+ // are still alive, we have nothing to do anymore.
401
+ return;
402
+ }
403
+
404
+ // We finally remove the node from its parent's children list,
405
+ // there are now no other references to it and it cannot
406
+ // be resurrected anymore even after we unlock the list.
407
+ debug!(
408
+ "Remove from child list: {:?}, parent: {:?}",
409
+ this.as_mut_ptr(),
410
+ this.parent.as_ref().map(|p| p.p.as_mut_ptr())
411
+ );
412
+ let weak = children.remove(&this.key(), |node| node.p.key()).unwrap();
413
+ assert_eq!(weak.p.as_mut_ptr(), this.as_mut_ptr());
414
+ } else {
415
+ debug_assert_eq!(this.next_free.load(Ordering::Relaxed), ptr::null_mut());
416
+ debug_assert_eq!(this.refcount.load(Ordering::Relaxed), 0);
417
+ }
418
+
419
+ // We are going to drop this node for good this time, as per the
420
+ // usual refcounting protocol we need an acquire fence here before
421
+ // we run the destructor.
422
+ //
423
+ // See https://github.com/rust-lang/rust/pull/41714#issuecomment-298996916
424
+ // for why it doesn't matter whether this is a load or a fence.
425
+ atomic::fence(Ordering::Acquire);
426
+
427
+ // Remove the parent reference from the child to avoid
428
+ // recursively dropping it and putting it on the free list.
429
+ let parent = unsafe { UnsafeBox::deref_mut(&mut this).parent.take() };
430
+
431
+ // We now drop the actual box and its contents, no one should
432
+ // access the current value in `this` anymore.
433
+ log_drop(&*this);
434
+ unsafe { UnsafeBox::drop(&mut this) };
435
+
436
+ if let Some(parent) = parent {
437
+ // We will attempt to drop the node's parent without the free
438
+ // list, so we clone the inner unsafe box and forget the
439
+ // original parent to avoid running its `StrongRuleNode`
440
+ // destructor which would attempt to use the free list if it
441
+ // still exists.
442
+ this = unsafe { UnsafeBox::clone(&parent.p) };
443
+ mem::forget(parent);
444
+ if this.refcount.fetch_sub(1, Ordering::Release) == 1 {
445
+ debug_assert_eq!(this.next_free.load(Ordering::Relaxed), ptr::null_mut());
446
+ if this.root.is_some() {
447
+ unsafe {
448
+ RuleNode::pretend_to_be_on_free_list(&this);
449
+ }
450
+ }
451
+ // Parent also reached refcount zero, we loop to drop it.
452
+ continue;
453
+ }
454
+ }
455
+
456
+ return;
457
+ }
458
+ }
459
+
460
+ /// Pushes this node on the tree's free list. Returns false if the free list
461
+ /// is gone. Should only be called after we decremented a node's refcount
462
+ /// to zero and pretended to be on the free list.
463
+ unsafe fn push_on_free_list(this: &UnsafeBox<Self>) -> bool {
464
+ let root = &this.root.as_ref().unwrap().p;
465
+
466
+ debug_assert!(this.refcount.load(Ordering::Relaxed) > 0);
467
+ debug_assert_eq!(this.next_free.load(Ordering::Relaxed), Self::DANGLING_PTR);
468
+
469
+ // Increment the approximate free count by one.
470
+ root.approximate_free_count.fetch_add(1, Ordering::Relaxed);
471
+
472
+ // If the compare-exchange operation fails in the loop, we will retry
473
+ // with the new head value, so this can be a relaxed load.
474
+ let mut head = root.next_free.load(Ordering::Relaxed);
475
+
476
+ while !head.is_null() {
477
+ // Two threads can never attempt to push the same node on the free
478
+ // list both at the same time, so whoever else pushed a node on the
479
+ // free list cannot have done so with this node.
480
+ debug_assert_ne!(head, this.as_mut_ptr());
481
+
482
+ // Store the current head of the free list in this node.
483
+ this.next_free.store(head, Ordering::Relaxed);
484
+
485
+ // Any thread acquiring the free list must observe the previous
486
+ // next_free changes that occured, hence the release ordering
487
+ // on success.
488
+ match root.next_free.compare_exchange_weak(
489
+ head,
490
+ this.as_mut_ptr(),
491
+ Ordering::Release,
492
+ Ordering::Relaxed,
493
+ ) {
494
+ Ok(_) => {
495
+ // This node is now on the free list, caller should not use
496
+ // the node anymore.
497
+ return true;
498
+ },
499
+ Err(new_head) => head = new_head,
500
+ }
501
+ }
502
+
503
+ // Tree was dropped and free list has been destroyed. We did not push
504
+ // this node on the free list but we still pretend to be on the free
505
+ // list to be ready to call `drop_without_free_list`.
506
+ false
507
+ }
508
+
509
+ /// Makes the node pretend to be on the free list. This will increment the
510
+ /// refcount by 1 and store `Self::DANGLING_PTR` in `next_free`. This
511
+ /// method should only be called after caller decremented the refcount to
512
+ /// zero, with the null pointer stored in `next_free`.
513
+ unsafe fn pretend_to_be_on_free_list(this: &UnsafeBox<Self>) {
514
+ debug_assert_eq!(this.next_free.load(Ordering::Relaxed), ptr::null_mut());
515
+ this.refcount.fetch_add(1, Ordering::Relaxed);
516
+ this.next_free.store(Self::DANGLING_PTR, Ordering::Release);
517
+ }
518
+
519
+ fn as_mut_ptr(&self) -> *mut RuleNode {
520
+ self as *const RuleNode as *mut RuleNode
521
+ }
522
+ }
523
+
524
+ pub(crate) struct WeakRuleNode {
525
+ p: UnsafeBox<RuleNode>,
526
+ }
527
+
528
+ /// A strong reference to a rule node.
529
+ pub struct StrongRuleNode {
530
+ p: UnsafeBox<RuleNode>,
531
+ }
532
+
533
+ #[cfg(feature = "servo")]
534
+ malloc_size_of::malloc_size_of_is_0!(StrongRuleNode);
535
+
536
+ impl StrongRuleNode {
537
+ fn new(n: Box<RuleNode>) -> Self {
538
+ debug_assert_eq!(n.parent.is_none(), n.source.is_none());
539
+
540
+ log_new(&*n);
541
+
542
+ debug!("Creating rule node: {:p}", &*n);
543
+
544
+ Self {
545
+ p: UnsafeBox::from_box(n),
546
+ }
547
+ }
548
+
549
+ unsafe fn from_unsafe_box(p: UnsafeBox<RuleNode>) -> Self {
550
+ Self { p }
551
+ }
552
+
553
+ unsafe fn downgrade(&self) -> WeakRuleNode {
554
+ unsafe {
555
+ WeakRuleNode {
556
+ p: UnsafeBox::clone(&self.p),
557
+ }
558
+ }
559
+ }
560
+
561
+ /// Get the parent rule node of this rule node.
562
+ pub fn parent(&self) -> Option<&StrongRuleNode> {
563
+ self.p.parent.as_ref()
564
+ }
565
+
566
+ pub(super) fn ensure_child(
567
+ &self,
568
+ root: &StrongRuleNode,
569
+ source: StyleSource,
570
+ cascade_priority: CascadePriority,
571
+ ) -> StrongRuleNode {
572
+ debug_assert!(
573
+ self.p.cascade_priority <= cascade_priority,
574
+ "Should be ordered (instead {:?} > {:?}), from {:?} and {:?}",
575
+ self.p.cascade_priority,
576
+ cascade_priority,
577
+ self.p.source,
578
+ source,
579
+ );
580
+
581
+ let key = ChildKey(cascade_priority, source.key());
582
+ {
583
+ let children = self.p.children.read();
584
+ if let Some(child) = children.get(&key, |node| node.p.key()) {
585
+ // Sound to call because we read-locked the parent's children.
586
+ return unsafe { child.upgrade() };
587
+ }
588
+ }
589
+ let mut children = self.p.children.write();
590
+ match children.entry(key, |node| node.p.key()) {
591
+ Entry::Occupied(child) => {
592
+ // Sound to call because we write-locked the parent's children.
593
+ unsafe { child.upgrade() }
594
+ },
595
+ Entry::Vacant(entry) => unsafe {
596
+ let node = StrongRuleNode::new(Box::new(RuleNode::new(
597
+ root.downgrade(),
598
+ self.clone(),
599
+ source,
600
+ cascade_priority,
601
+ )));
602
+ // Sound to call because we still own a strong reference to
603
+ // this node, through the `node` variable itself that we are
604
+ // going to return to the caller.
605
+ entry.insert(node.downgrade());
606
+ node
607
+ },
608
+ }
609
+ }
610
+
611
+ /// Get the style source corresponding to this rule node. May return `None`
612
+ /// if it's the root node, which means that the node hasn't matched any
613
+ /// rules.
614
+ pub fn style_source(&self) -> Option<&StyleSource> {
615
+ self.p.source.as_ref()
616
+ }
617
+
618
+ /// The cascade priority.
619
+ #[inline]
620
+ pub fn cascade_priority(&self) -> CascadePriority {
621
+ self.p.cascade_priority
622
+ }
623
+
624
+ /// The cascade level.
625
+ #[inline]
626
+ pub fn cascade_level(&self) -> CascadeLevel {
627
+ self.cascade_priority().cascade_level()
628
+ }
629
+
630
+ /// The importance.
631
+ #[inline]
632
+ pub fn importance(&self) -> crate::properties::Importance {
633
+ self.cascade_level().importance()
634
+ }
635
+
636
+ /// Returns whether this node has any child, only intended for testing
637
+ /// purposes.
638
+ pub unsafe fn has_children_for_testing(&self) -> bool {
639
+ !self.p.children.read().is_empty()
640
+ }
641
+
642
+ pub(super) fn dump<W: Write>(&self, guards: &StylesheetGuards, writer: &mut W, indent: usize) {
643
+ const INDENT_INCREMENT: usize = 4;
644
+
645
+ for _ in 0..indent {
646
+ let _ = write!(writer, " ");
647
+ }
648
+
649
+ let _ = writeln!(
650
+ writer,
651
+ " - {:p} (ref: {:?}, parent: {:?})",
652
+ &*self.p,
653
+ self.p.refcount.load(Ordering::Relaxed),
654
+ self.parent().map(|p| &*p.p as *const RuleNode)
655
+ );
656
+
657
+ for _ in 0..indent {
658
+ let _ = write!(writer, " ");
659
+ }
660
+
661
+ if let Some(source) = self.style_source() {
662
+ source.dump(self.cascade_level().guard(guards), writer);
663
+ } else {
664
+ if indent != 0 {
665
+ warn!("How has this happened?");
666
+ }
667
+ let _ = write!(writer, "(root)");
668
+ }
669
+
670
+ let _ = writeln!(writer);
671
+ for child in &*self.p.children.read() {
672
+ unsafe {
673
+ child
674
+ .upgrade()
675
+ .dump(guards, writer, indent + INDENT_INCREMENT);
676
+ }
677
+ }
678
+ }
679
+ }
680
+
681
+ impl Clone for StrongRuleNode {
682
+ fn clone(&self) -> Self {
683
+ debug!(
684
+ "{:p}: {:?}+",
685
+ &*self.p,
686
+ self.p.refcount.load(Ordering::Relaxed)
687
+ );
688
+ debug_assert!(self.p.refcount.load(Ordering::Relaxed) > 0);
689
+ self.p.refcount.fetch_add(1, Ordering::Relaxed);
690
+ unsafe { StrongRuleNode::from_unsafe_box(UnsafeBox::clone(&self.p)) }
691
+ }
692
+ }
693
+
694
+ impl Drop for StrongRuleNode {
695
+ #[cfg_attr(feature = "servo", allow(unused_mut))]
696
+ fn drop(&mut self) {
697
+ let node = &*self.p;
698
+ debug!("{:p}: {:?}-", node, node.refcount.load(Ordering::Relaxed));
699
+ debug!(
700
+ "Dropping node: {:p}, root: {:?}, parent: {:?}",
701
+ node,
702
+ node.root.as_ref().map(|r| &*r.p as *const RuleNode),
703
+ node.parent.as_ref().map(|p| &*p.p as *const RuleNode)
704
+ );
705
+
706
+ let should_drop = {
707
+ debug_assert!(node.refcount.load(Ordering::Relaxed) > 0);
708
+ node.refcount.fetch_sub(1, Ordering::Release) == 1
709
+ };
710
+
711
+ if !should_drop {
712
+ // The refcount didn't even drop zero yet, there is nothing for us
713
+ // to do anymore.
714
+ return;
715
+ }
716
+
717
+ unsafe {
718
+ if node.root.is_some() {
719
+ // This is a non-root node and we just observed the refcount
720
+ // dropping to zero, we need to pretend to be on the free list
721
+ // to unstuck any thread who tried to resurrect this node first
722
+ // through `WeakRuleNode::upgrade`.
723
+ RuleNode::pretend_to_be_on_free_list(&self.p);
724
+
725
+ // Attempt to push the node on the free list. This may fail
726
+ // if the free list is gone.
727
+ if RuleNode::push_on_free_list(&self.p) {
728
+ return;
729
+ }
730
+ }
731
+
732
+ // Either this was the last reference of the root node, or the
733
+ // tree rule is gone and there is no free list anymore. Drop the
734
+ // node.
735
+ RuleNode::drop_without_free_list(&mut self.p);
736
+ }
737
+ }
738
+ }
739
+
740
+ impl WeakRuleNode {
741
+ /// Upgrades this weak node reference, returning a strong one.
742
+ ///
743
+ /// Must be called with items stored in a node's children list. The children
744
+ /// list must at least be read-locked when this is called.
745
+ unsafe fn upgrade(&self) -> StrongRuleNode {
746
+ debug!("Upgrading weak node: {:p}", &*self.p);
747
+
748
+ if self.p.refcount.fetch_add(1, Ordering::Relaxed) == 0 {
749
+ // We observed a refcount of 0, we need to wait for this node to
750
+ // be put on the free list. Resetting the `next_free` pointer to
751
+ // null is only done in `RuleNode::drop_without_free_list`, just
752
+ // before a release refcount decrement, so this acquire fence here
753
+ // makes sure that we observed the write to null before we loop
754
+ // until there is a non-null value.
755
+ atomic::fence(Ordering::Acquire);
756
+ while self.p.next_free.load(Ordering::Relaxed).is_null() {}
757
+ }
758
+ unsafe { StrongRuleNode::from_unsafe_box(UnsafeBox::clone(&self.p)) }
759
+ }
760
+ }
761
+
762
+ impl fmt::Debug for StrongRuleNode {
763
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
764
+ (&*self.p as *const RuleNode).fmt(f)
765
+ }
766
+ }
767
+
768
+ impl Eq for StrongRuleNode {}
769
+ impl PartialEq for StrongRuleNode {
770
+ fn eq(&self, other: &Self) -> bool {
771
+ &*self.p as *const RuleNode == &*other.p
772
+ }
773
+ }
774
+
775
+ impl hash::Hash for StrongRuleNode {
776
+ fn hash<H>(&self, state: &mut H)
777
+ where
778
+ H: hash::Hasher,
779
+ {
780
+ (&*self.p as *const RuleNode).hash(state)
781
+ }
782
+ }
783
+
784
+ // Large pages generate thousands of RuleNode objects.
785
+ size_of_test!(RuleNode, 80);
786
+ // StrongRuleNode should be pointer-sized even inside an option.
787
+ size_of_test!(Option<StrongRuleNode>, 8);