StyleValue.cpp 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
  4. * Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
  5. * Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #include <AK/ByteBuffer.h>
  10. #include <LibGfx/Palette.h>
  11. #include <LibWeb/CSS/Serialize.h>
  12. #include <LibWeb/CSS/StyleValue.h>
  13. #include <LibWeb/CSS/StyleValues/AngleStyleValue.h>
  14. #include <LibWeb/CSS/StyleValues/BackgroundRepeatStyleValue.h>
  15. #include <LibWeb/CSS/StyleValues/BackgroundSizeStyleValue.h>
  16. #include <LibWeb/CSS/StyleValues/BackgroundStyleValue.h>
  17. #include <LibWeb/CSS/StyleValues/BorderRadiusShorthandStyleValue.h>
  18. #include <LibWeb/CSS/StyleValues/BorderRadiusStyleValue.h>
  19. #include <LibWeb/CSS/StyleValues/BorderStyleValue.h>
  20. #include <LibWeb/CSS/StyleValues/ColorStyleValue.h>
  21. #include <LibWeb/CSS/StyleValues/ContentStyleValue.h>
  22. #include <LibWeb/CSS/StyleValues/FilterValueListStyleValue.h>
  23. #include <LibWeb/CSS/StyleValues/FlexStyleValue.h>
  24. #include <LibWeb/DOM/Document.h>
  25. #include <LibWeb/HTML/BrowsingContext.h>
  26. #include <LibWeb/Loader/LoadRequest.h>
  27. #include <LibWeb/Loader/ResourceLoader.h>
  28. #include <LibWeb/Page/Page.h>
  29. #include <LibWeb/Painting/GradientPainting.h>
  30. #include <LibWeb/Platform/Timer.h>
  31. namespace Web::CSS {
  32. StyleValue::StyleValue(Type type)
  33. : m_type(type)
  34. {
  35. }
  36. AbstractImageStyleValue const& StyleValue::as_abstract_image() const
  37. {
  38. VERIFY(is_abstract_image());
  39. return static_cast<AbstractImageStyleValue const&>(*this);
  40. }
  41. AngleStyleValue const& StyleValue::as_angle() const
  42. {
  43. VERIFY(is_angle());
  44. return static_cast<AngleStyleValue const&>(*this);
  45. }
  46. BackgroundStyleValue const& StyleValue::as_background() const
  47. {
  48. VERIFY(is_background());
  49. return static_cast<BackgroundStyleValue const&>(*this);
  50. }
  51. BackgroundRepeatStyleValue const& StyleValue::as_background_repeat() const
  52. {
  53. VERIFY(is_background_repeat());
  54. return static_cast<BackgroundRepeatStyleValue const&>(*this);
  55. }
  56. BackgroundSizeStyleValue const& StyleValue::as_background_size() const
  57. {
  58. VERIFY(is_background_size());
  59. return static_cast<BackgroundSizeStyleValue const&>(*this);
  60. }
  61. BorderStyleValue const& StyleValue::as_border() const
  62. {
  63. VERIFY(is_border());
  64. return static_cast<BorderStyleValue const&>(*this);
  65. }
  66. BorderRadiusStyleValue const& StyleValue::as_border_radius() const
  67. {
  68. VERIFY(is_border_radius());
  69. return static_cast<BorderRadiusStyleValue const&>(*this);
  70. }
  71. BorderRadiusShorthandStyleValue const& StyleValue::as_border_radius_shorthand() const
  72. {
  73. VERIFY(is_border_radius_shorthand());
  74. return static_cast<BorderRadiusShorthandStyleValue const&>(*this);
  75. }
  76. ShadowStyleValue const& StyleValue::as_shadow() const
  77. {
  78. VERIFY(is_shadow());
  79. return static_cast<ShadowStyleValue const&>(*this);
  80. }
  81. CalculatedStyleValue const& StyleValue::as_calculated() const
  82. {
  83. VERIFY(is_calculated());
  84. return static_cast<CalculatedStyleValue const&>(*this);
  85. }
  86. ColorStyleValue const& StyleValue::as_color() const
  87. {
  88. VERIFY(is_color());
  89. return static_cast<ColorStyleValue const&>(*this);
  90. }
  91. ConicGradientStyleValue const& StyleValue::as_conic_gradient() const
  92. {
  93. VERIFY(is_conic_gradient());
  94. return static_cast<ConicGradientStyleValue const&>(*this);
  95. }
  96. ContentStyleValue const& StyleValue::as_content() const
  97. {
  98. VERIFY(is_content());
  99. return static_cast<ContentStyleValue const&>(*this);
  100. }
  101. FilterValueListStyleValue const& StyleValue::as_filter_value_list() const
  102. {
  103. VERIFY(is_filter_value_list());
  104. return static_cast<FilterValueListStyleValue const&>(*this);
  105. }
  106. FlexStyleValue const& StyleValue::as_flex() const
  107. {
  108. VERIFY(is_flex());
  109. return static_cast<FlexStyleValue const&>(*this);
  110. }
  111. FlexFlowStyleValue const& StyleValue::as_flex_flow() const
  112. {
  113. VERIFY(is_flex_flow());
  114. return static_cast<FlexFlowStyleValue const&>(*this);
  115. }
  116. FontStyleValue const& StyleValue::as_font() const
  117. {
  118. VERIFY(is_font());
  119. return static_cast<FontStyleValue const&>(*this);
  120. }
  121. FrequencyStyleValue const& StyleValue::as_frequency() const
  122. {
  123. VERIFY(is_frequency());
  124. return static_cast<FrequencyStyleValue const&>(*this);
  125. }
  126. GridTrackPlacementShorthandStyleValue const& StyleValue::as_grid_track_placement_shorthand() const
  127. {
  128. VERIFY(is_grid_track_placement_shorthand());
  129. return static_cast<GridTrackPlacementShorthandStyleValue const&>(*this);
  130. }
  131. GridAreaShorthandStyleValue const& StyleValue::as_grid_area_shorthand() const
  132. {
  133. VERIFY(is_grid_area_shorthand());
  134. return static_cast<GridAreaShorthandStyleValue const&>(*this);
  135. }
  136. GridTemplateAreaStyleValue const& StyleValue::as_grid_template_area() const
  137. {
  138. VERIFY(is_grid_template_area());
  139. return static_cast<GridTemplateAreaStyleValue const&>(*this);
  140. }
  141. GridTrackPlacementStyleValue const& StyleValue::as_grid_track_placement() const
  142. {
  143. VERIFY(is_grid_track_placement());
  144. return static_cast<GridTrackPlacementStyleValue const&>(*this);
  145. }
  146. IdentifierStyleValue const& StyleValue::as_identifier() const
  147. {
  148. VERIFY(is_identifier());
  149. return static_cast<IdentifierStyleValue const&>(*this);
  150. }
  151. ImageStyleValue const& StyleValue::as_image() const
  152. {
  153. VERIFY(is_image());
  154. return static_cast<ImageStyleValue const&>(*this);
  155. }
  156. InheritStyleValue const& StyleValue::as_inherit() const
  157. {
  158. VERIFY(is_inherit());
  159. return static_cast<InheritStyleValue const&>(*this);
  160. }
  161. InitialStyleValue const& StyleValue::as_initial() const
  162. {
  163. VERIFY(is_initial());
  164. return static_cast<InitialStyleValue const&>(*this);
  165. }
  166. LengthStyleValue const& StyleValue::as_length() const
  167. {
  168. VERIFY(is_length());
  169. return static_cast<LengthStyleValue const&>(*this);
  170. }
  171. GridTrackSizeStyleValue const& StyleValue::as_grid_track_size_list() const
  172. {
  173. VERIFY(is_grid_track_size_list());
  174. return static_cast<GridTrackSizeStyleValue const&>(*this);
  175. }
  176. LinearGradientStyleValue const& StyleValue::as_linear_gradient() const
  177. {
  178. VERIFY(is_linear_gradient());
  179. return static_cast<LinearGradientStyleValue const&>(*this);
  180. }
  181. ListStyleStyleValue const& StyleValue::as_list_style() const
  182. {
  183. VERIFY(is_list_style());
  184. return static_cast<ListStyleStyleValue const&>(*this);
  185. }
  186. NumericStyleValue const& StyleValue::as_numeric() const
  187. {
  188. VERIFY(is_numeric());
  189. return static_cast<NumericStyleValue const&>(*this);
  190. }
  191. OverflowStyleValue const& StyleValue::as_overflow() const
  192. {
  193. VERIFY(is_overflow());
  194. return static_cast<OverflowStyleValue const&>(*this);
  195. }
  196. PercentageStyleValue const& StyleValue::as_percentage() const
  197. {
  198. VERIFY(is_percentage());
  199. return static_cast<PercentageStyleValue const&>(*this);
  200. }
  201. PositionStyleValue const& StyleValue::as_position() const
  202. {
  203. VERIFY(is_position());
  204. return static_cast<PositionStyleValue const&>(*this);
  205. }
  206. RadialGradientStyleValue const& StyleValue::as_radial_gradient() const
  207. {
  208. VERIFY(is_radial_gradient());
  209. return static_cast<RadialGradientStyleValue const&>(*this);
  210. }
  211. RectStyleValue const& StyleValue::as_rect() const
  212. {
  213. VERIFY(is_rect());
  214. return static_cast<RectStyleValue const&>(*this);
  215. }
  216. ResolutionStyleValue const& StyleValue::as_resolution() const
  217. {
  218. VERIFY(is_resolution());
  219. return static_cast<ResolutionStyleValue const&>(*this);
  220. }
  221. StringStyleValue const& StyleValue::as_string() const
  222. {
  223. VERIFY(is_string());
  224. return static_cast<StringStyleValue const&>(*this);
  225. }
  226. TextDecorationStyleValue const& StyleValue::as_text_decoration() const
  227. {
  228. VERIFY(is_text_decoration());
  229. return static_cast<TextDecorationStyleValue const&>(*this);
  230. }
  231. TimeStyleValue const& StyleValue::as_time() const
  232. {
  233. VERIFY(is_time());
  234. return static_cast<TimeStyleValue const&>(*this);
  235. }
  236. TransformationStyleValue const& StyleValue::as_transformation() const
  237. {
  238. VERIFY(is_transformation());
  239. return static_cast<TransformationStyleValue const&>(*this);
  240. }
  241. UnresolvedStyleValue const& StyleValue::as_unresolved() const
  242. {
  243. VERIFY(is_unresolved());
  244. return static_cast<UnresolvedStyleValue const&>(*this);
  245. }
  246. UnsetStyleValue const& StyleValue::as_unset() const
  247. {
  248. VERIFY(is_unset());
  249. return static_cast<UnsetStyleValue const&>(*this);
  250. }
  251. StyleValueList const& StyleValue::as_value_list() const
  252. {
  253. VERIFY(is_value_list());
  254. return static_cast<StyleValueList const&>(*this);
  255. }
  256. void CalculatedStyleValue::CalculationResult::add(CalculationResult const& other, Layout::Node const* layout_node, PercentageBasis const& percentage_basis)
  257. {
  258. add_or_subtract_internal(SumOperation::Add, other, layout_node, percentage_basis);
  259. }
  260. void CalculatedStyleValue::CalculationResult::subtract(CalculationResult const& other, Layout::Node const* layout_node, PercentageBasis const& percentage_basis)
  261. {
  262. add_or_subtract_internal(SumOperation::Subtract, other, layout_node, percentage_basis);
  263. }
  264. void CalculatedStyleValue::CalculationResult::add_or_subtract_internal(SumOperation op, CalculationResult const& other, Layout::Node const* layout_node, PercentageBasis const& percentage_basis)
  265. {
  266. // We know from validation when resolving the type, that "both sides have the same type, or that one side is a <number> and the other is an <integer>".
  267. // Though, having the same type may mean that one side is a <dimension> and the other a <percentage>.
  268. // Note: This is almost identical to ::add()
  269. m_value.visit(
  270. [&](Number const& number) {
  271. auto other_number = other.m_value.get<Number>();
  272. if (op == SumOperation::Add) {
  273. m_value = number + other_number;
  274. } else {
  275. m_value = number - other_number;
  276. }
  277. },
  278. [&](Angle const& angle) {
  279. auto this_degrees = angle.to_degrees();
  280. if (other.m_value.has<Angle>()) {
  281. auto other_degrees = other.m_value.get<Angle>().to_degrees();
  282. if (op == SumOperation::Add)
  283. m_value = Angle::make_degrees(this_degrees + other_degrees);
  284. else
  285. m_value = Angle::make_degrees(this_degrees - other_degrees);
  286. } else {
  287. VERIFY(percentage_basis.has<Angle>());
  288. auto other_degrees = percentage_basis.get<Angle>().percentage_of(other.m_value.get<Percentage>()).to_degrees();
  289. if (op == SumOperation::Add)
  290. m_value = Angle::make_degrees(this_degrees + other_degrees);
  291. else
  292. m_value = Angle::make_degrees(this_degrees - other_degrees);
  293. }
  294. },
  295. [&](Frequency const& frequency) {
  296. auto this_hertz = frequency.to_hertz();
  297. if (other.m_value.has<Frequency>()) {
  298. auto other_hertz = other.m_value.get<Frequency>().to_hertz();
  299. if (op == SumOperation::Add)
  300. m_value = Frequency::make_hertz(this_hertz + other_hertz);
  301. else
  302. m_value = Frequency::make_hertz(this_hertz - other_hertz);
  303. } else {
  304. VERIFY(percentage_basis.has<Frequency>());
  305. auto other_hertz = percentage_basis.get<Frequency>().percentage_of(other.m_value.get<Percentage>()).to_hertz();
  306. if (op == SumOperation::Add)
  307. m_value = Frequency::make_hertz(this_hertz + other_hertz);
  308. else
  309. m_value = Frequency::make_hertz(this_hertz - other_hertz);
  310. }
  311. },
  312. [&](Length const& length) {
  313. auto this_px = length.to_px(*layout_node);
  314. if (other.m_value.has<Length>()) {
  315. auto other_px = other.m_value.get<Length>().to_px(*layout_node);
  316. if (op == SumOperation::Add)
  317. m_value = Length::make_px(this_px + other_px);
  318. else
  319. m_value = Length::make_px(this_px - other_px);
  320. } else {
  321. VERIFY(percentage_basis.has<Length>());
  322. auto other_px = percentage_basis.get<Length>().percentage_of(other.m_value.get<Percentage>()).to_px(*layout_node);
  323. if (op == SumOperation::Add)
  324. m_value = Length::make_px(this_px + other_px);
  325. else
  326. m_value = Length::make_px(this_px - other_px);
  327. }
  328. },
  329. [&](Time const& time) {
  330. auto this_seconds = time.to_seconds();
  331. if (other.m_value.has<Time>()) {
  332. auto other_seconds = other.m_value.get<Time>().to_seconds();
  333. if (op == SumOperation::Add)
  334. m_value = Time::make_seconds(this_seconds + other_seconds);
  335. else
  336. m_value = Time::make_seconds(this_seconds - other_seconds);
  337. } else {
  338. VERIFY(percentage_basis.has<Time>());
  339. auto other_seconds = percentage_basis.get<Time>().percentage_of(other.m_value.get<Percentage>()).to_seconds();
  340. if (op == SumOperation::Add)
  341. m_value = Time::make_seconds(this_seconds + other_seconds);
  342. else
  343. m_value = Time::make_seconds(this_seconds - other_seconds);
  344. }
  345. },
  346. [&](Percentage const& percentage) {
  347. if (other.m_value.has<Percentage>()) {
  348. if (op == SumOperation::Add)
  349. m_value = Percentage { percentage.value() + other.m_value.get<Percentage>().value() };
  350. else
  351. m_value = Percentage { percentage.value() - other.m_value.get<Percentage>().value() };
  352. return;
  353. }
  354. // Other side isn't a percentage, so the easiest way to handle it without duplicating all the logic, is just to swap `this` and `other`.
  355. CalculationResult new_value = other;
  356. if (op == SumOperation::Add) {
  357. new_value.add(*this, layout_node, percentage_basis);
  358. } else {
  359. // Turn 'this - other' into '-other + this', as 'A + B == B + A', but 'A - B != B - A'
  360. new_value.multiply_by({ Number { Number::Type::Integer, -1.0f } }, layout_node);
  361. new_value.add(*this, layout_node, percentage_basis);
  362. }
  363. *this = new_value;
  364. });
  365. }
  366. void CalculatedStyleValue::CalculationResult::multiply_by(CalculationResult const& other, Layout::Node const* layout_node)
  367. {
  368. // We know from validation when resolving the type, that at least one side must be a <number> or <integer>.
  369. // Both of these are represented as a float.
  370. VERIFY(m_value.has<Number>() || other.m_value.has<Number>());
  371. bool other_is_number = other.m_value.has<Number>();
  372. m_value.visit(
  373. [&](Number const& number) {
  374. if (other_is_number) {
  375. m_value = number * other.m_value.get<Number>();
  376. } else {
  377. // Avoid duplicating all the logic by swapping `this` and `other`.
  378. CalculationResult new_value = other;
  379. new_value.multiply_by(*this, layout_node);
  380. *this = new_value;
  381. }
  382. },
  383. [&](Angle const& angle) {
  384. m_value = Angle::make_degrees(angle.to_degrees() * other.m_value.get<Number>().value());
  385. },
  386. [&](Frequency const& frequency) {
  387. m_value = Frequency::make_hertz(frequency.to_hertz() * other.m_value.get<Number>().value());
  388. },
  389. [&](Length const& length) {
  390. VERIFY(layout_node);
  391. m_value = Length::make_px(length.to_px(*layout_node) * other.m_value.get<Number>().value());
  392. },
  393. [&](Time const& time) {
  394. m_value = Time::make_seconds(time.to_seconds() * other.m_value.get<Number>().value());
  395. },
  396. [&](Percentage const& percentage) {
  397. m_value = Percentage { percentage.value() * other.m_value.get<Number>().value() };
  398. });
  399. }
  400. void CalculatedStyleValue::CalculationResult::divide_by(CalculationResult const& other, Layout::Node const* layout_node)
  401. {
  402. // We know from validation when resolving the type, that `other` must be a <number> or <integer>.
  403. // Both of these are represented as a Number.
  404. auto denominator = other.m_value.get<Number>().value();
  405. // FIXME: Dividing by 0 is invalid, and should be caught during parsing.
  406. VERIFY(denominator != 0.0f);
  407. m_value.visit(
  408. [&](Number const& number) {
  409. m_value = Number {
  410. Number::Type::Number,
  411. number.value() / denominator
  412. };
  413. },
  414. [&](Angle const& angle) {
  415. m_value = Angle::make_degrees(angle.to_degrees() / denominator);
  416. },
  417. [&](Frequency const& frequency) {
  418. m_value = Frequency::make_hertz(frequency.to_hertz() / denominator);
  419. },
  420. [&](Length const& length) {
  421. VERIFY(layout_node);
  422. m_value = Length::make_px(length.to_px(*layout_node) / denominator);
  423. },
  424. [&](Time const& time) {
  425. m_value = Time::make_seconds(time.to_seconds() / denominator);
  426. },
  427. [&](Percentage const& percentage) {
  428. m_value = Percentage { percentage.value() / denominator };
  429. });
  430. }
  431. ErrorOr<String> CalculatedStyleValue::to_string() const
  432. {
  433. return String::formatted("calc({})", TRY(m_expression->to_string()));
  434. }
  435. bool CalculatedStyleValue::equals(StyleValue const& other) const
  436. {
  437. if (type() != other.type())
  438. return false;
  439. // This is a case where comparing the strings actually makes sense.
  440. return to_string().release_value_but_fixme_should_propagate_errors() == other.to_string().release_value_but_fixme_should_propagate_errors();
  441. }
  442. ErrorOr<String> CalculatedStyleValue::CalcNumberValue::to_string() const
  443. {
  444. return value.visit(
  445. [](Number const& number) -> ErrorOr<String> { return String::number(number.value()); },
  446. [](NonnullOwnPtr<CalcNumberSum> const& sum) -> ErrorOr<String> { return String::formatted("({})", TRY(sum->to_string())); });
  447. }
  448. ErrorOr<String> CalculatedStyleValue::CalcValue::to_string() const
  449. {
  450. return value.visit(
  451. [](Number const& number) -> ErrorOr<String> { return String::number(number.value()); },
  452. [](NonnullOwnPtr<CalcSum> const& sum) -> ErrorOr<String> { return String::formatted("({})", TRY(sum->to_string())); },
  453. [](auto const& v) -> ErrorOr<String> { return v.to_string(); });
  454. }
  455. ErrorOr<String> CalculatedStyleValue::CalcSum::to_string() const
  456. {
  457. StringBuilder builder;
  458. TRY(builder.try_append(TRY(first_calc_product->to_string())));
  459. for (auto const& item : zero_or_more_additional_calc_products)
  460. TRY(builder.try_append(TRY(item->to_string())));
  461. return builder.to_string();
  462. }
  463. ErrorOr<String> CalculatedStyleValue::CalcNumberSum::to_string() const
  464. {
  465. StringBuilder builder;
  466. TRY(builder.try_append(TRY(first_calc_number_product->to_string())));
  467. for (auto const& item : zero_or_more_additional_calc_number_products)
  468. TRY(builder.try_append(TRY(item->to_string())));
  469. return builder.to_string();
  470. }
  471. ErrorOr<String> CalculatedStyleValue::CalcProduct::to_string() const
  472. {
  473. StringBuilder builder;
  474. TRY(builder.try_append(TRY(first_calc_value.to_string())));
  475. for (auto const& item : zero_or_more_additional_calc_values)
  476. TRY(builder.try_append(TRY(item->to_string())));
  477. return builder.to_string();
  478. }
  479. ErrorOr<String> CalculatedStyleValue::CalcSumPartWithOperator::to_string() const
  480. {
  481. return String::formatted(" {} {}", op == SumOperation::Add ? "+"sv : "-"sv, TRY(value->to_string()));
  482. }
  483. ErrorOr<String> CalculatedStyleValue::CalcProductPartWithOperator::to_string() const
  484. {
  485. auto value_string = TRY(value.visit(
  486. [](CalcValue const& v) { return v.to_string(); },
  487. [](CalcNumberValue const& v) { return v.to_string(); }));
  488. return String::formatted(" {} {}", op == ProductOperation::Multiply ? "*"sv : "/"sv, value_string);
  489. }
  490. ErrorOr<String> CalculatedStyleValue::CalcNumberProduct::to_string() const
  491. {
  492. StringBuilder builder;
  493. TRY(builder.try_append(TRY(first_calc_number_value.to_string())));
  494. for (auto const& item : zero_or_more_additional_calc_number_values)
  495. TRY(builder.try_append(TRY(item->to_string())));
  496. return builder.to_string();
  497. }
  498. ErrorOr<String> CalculatedStyleValue::CalcNumberProductPartWithOperator::to_string() const
  499. {
  500. return String::formatted(" {} {}", op == ProductOperation::Multiply ? "*"sv : "/"sv, TRY(value.to_string()));
  501. }
  502. ErrorOr<String> CalculatedStyleValue::CalcNumberSumPartWithOperator::to_string() const
  503. {
  504. return String::formatted(" {} {}", op == SumOperation::Add ? "+"sv : "-"sv, TRY(value->to_string()));
  505. }
  506. Optional<Angle> CalculatedStyleValue::resolve_angle() const
  507. {
  508. auto result = m_expression->resolve(nullptr, {});
  509. if (result.value().has<Angle>())
  510. return result.value().get<Angle>();
  511. return {};
  512. }
  513. Optional<Angle> CalculatedStyleValue::resolve_angle_percentage(Angle const& percentage_basis) const
  514. {
  515. auto result = m_expression->resolve(nullptr, percentage_basis);
  516. return result.value().visit(
  517. [&](Angle const& angle) -> Optional<Angle> {
  518. return angle;
  519. },
  520. [&](Percentage const& percentage) -> Optional<Angle> {
  521. return percentage_basis.percentage_of(percentage);
  522. },
  523. [&](auto const&) -> Optional<Angle> {
  524. return {};
  525. });
  526. }
  527. Optional<Frequency> CalculatedStyleValue::resolve_frequency() const
  528. {
  529. auto result = m_expression->resolve(nullptr, {});
  530. if (result.value().has<Frequency>())
  531. return result.value().get<Frequency>();
  532. return {};
  533. }
  534. Optional<Frequency> CalculatedStyleValue::resolve_frequency_percentage(Frequency const& percentage_basis) const
  535. {
  536. auto result = m_expression->resolve(nullptr, percentage_basis);
  537. return result.value().visit(
  538. [&](Frequency const& frequency) -> Optional<Frequency> {
  539. return frequency;
  540. },
  541. [&](Percentage const& percentage) -> Optional<Frequency> {
  542. return percentage_basis.percentage_of(percentage);
  543. },
  544. [&](auto const&) -> Optional<Frequency> {
  545. return {};
  546. });
  547. }
  548. Optional<Length> CalculatedStyleValue::resolve_length(Layout::Node const& layout_node) const
  549. {
  550. auto result = m_expression->resolve(&layout_node, {});
  551. if (result.value().has<Length>())
  552. return result.value().get<Length>();
  553. return {};
  554. }
  555. Optional<Length> CalculatedStyleValue::resolve_length_percentage(Layout::Node const& layout_node, Length const& percentage_basis) const
  556. {
  557. auto result = m_expression->resolve(&layout_node, percentage_basis);
  558. return result.value().visit(
  559. [&](Length const& length) -> Optional<Length> {
  560. return length;
  561. },
  562. [&](Percentage const& percentage) -> Optional<Length> {
  563. return percentage_basis.percentage_of(percentage);
  564. },
  565. [&](auto const&) -> Optional<Length> {
  566. return {};
  567. });
  568. }
  569. Optional<Percentage> CalculatedStyleValue::resolve_percentage() const
  570. {
  571. auto result = m_expression->resolve(nullptr, {});
  572. if (result.value().has<Percentage>())
  573. return result.value().get<Percentage>();
  574. return {};
  575. }
  576. Optional<Time> CalculatedStyleValue::resolve_time() const
  577. {
  578. auto result = m_expression->resolve(nullptr, {});
  579. if (result.value().has<Time>())
  580. return result.value().get<Time>();
  581. return {};
  582. }
  583. Optional<Time> CalculatedStyleValue::resolve_time_percentage(Time const& percentage_basis) const
  584. {
  585. auto result = m_expression->resolve(nullptr, percentage_basis);
  586. return result.value().visit(
  587. [&](Time const& time) -> Optional<Time> {
  588. return time;
  589. },
  590. [&](auto const&) -> Optional<Time> {
  591. return {};
  592. });
  593. }
  594. Optional<float> CalculatedStyleValue::resolve_number()
  595. {
  596. auto result = m_expression->resolve(nullptr, {});
  597. if (result.value().has<Number>())
  598. return result.value().get<Number>().value();
  599. return {};
  600. }
  601. Optional<i64> CalculatedStyleValue::resolve_integer()
  602. {
  603. auto result = m_expression->resolve(nullptr, {});
  604. if (result.value().has<Number>())
  605. return result.value().get<Number>().integer_value();
  606. return {};
  607. }
  608. static bool is_number(CalculatedStyleValue::ResolvedType type)
  609. {
  610. return type == CalculatedStyleValue::ResolvedType::Number || type == CalculatedStyleValue::ResolvedType::Integer;
  611. }
  612. static bool is_dimension(CalculatedStyleValue::ResolvedType type)
  613. {
  614. return type != CalculatedStyleValue::ResolvedType::Number
  615. && type != CalculatedStyleValue::ResolvedType::Integer
  616. && type != CalculatedStyleValue::ResolvedType::Percentage;
  617. }
  618. template<typename SumWithOperator>
  619. static Optional<CalculatedStyleValue::ResolvedType> resolve_sum_type(CalculatedStyleValue::ResolvedType first_type, Vector<NonnullOwnPtr<SumWithOperator>> const& zero_or_more_additional_products)
  620. {
  621. auto type = first_type;
  622. for (auto const& product : zero_or_more_additional_products) {
  623. auto maybe_product_type = product->resolved_type();
  624. if (!maybe_product_type.has_value())
  625. return {};
  626. auto product_type = maybe_product_type.value();
  627. // At + or -, check that both sides have the same type, or that one side is a <number> and the other is an <integer>.
  628. // If both sides are the same type, resolve to that type.
  629. if (product_type == type)
  630. continue;
  631. // If one side is a <number> and the other is an <integer>, resolve to <number>.
  632. if (is_number(type) && is_number(product_type)) {
  633. type = CalculatedStyleValue::ResolvedType::Number;
  634. continue;
  635. }
  636. // FIXME: calc() handles <percentage> by allowing them to pretend to be whatever <dimension> type is allowed at this location.
  637. // Since we can't easily check what that type is, we just allow <percentage> to combine with any other <dimension> type.
  638. if (type == CalculatedStyleValue::ResolvedType::Percentage && is_dimension(product_type)) {
  639. type = product_type;
  640. continue;
  641. }
  642. if (is_dimension(type) && product_type == CalculatedStyleValue::ResolvedType::Percentage)
  643. continue;
  644. return {};
  645. }
  646. return type;
  647. }
  648. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcSum::resolved_type() const
  649. {
  650. auto maybe_type = first_calc_product->resolved_type();
  651. if (!maybe_type.has_value())
  652. return {};
  653. auto type = maybe_type.value();
  654. return resolve_sum_type(type, zero_or_more_additional_calc_products);
  655. }
  656. // https://www.w3.org/TR/CSS2/visufx.html#value-def-shape
  657. Gfx::FloatRect EdgeRect::resolved(Layout::Node const& layout_node, Gfx::FloatRect border_box) const
  658. {
  659. // In CSS 2.1, the only valid <shape> value is: rect(<top>, <right>, <bottom>, <left>) where
  660. // <top> and <bottom> specify offsets from the top border edge of the box, and <right>, and
  661. // <left> specify offsets from the left border edge of the box.
  662. // The value 'auto' means that a given edge of the clipping region will be the same as the edge
  663. // of the element's generated border box (i.e., 'auto' means the same as '0' for <top> and
  664. // <left>, the same as the used value of the height plus the sum of vertical padding and border
  665. // widths for <bottom>, and the same as the used value of the width plus the sum of the
  666. // horizontal padding and border widths for <right>, such that four 'auto' values result in the
  667. // clipping region being the same as the element's border box).
  668. auto left = border_box.left() + (left_edge.is_auto() ? 0 : left_edge.to_px(layout_node)).value();
  669. auto top = border_box.top() + (top_edge.is_auto() ? 0 : top_edge.to_px(layout_node)).value();
  670. auto right = border_box.left() + (right_edge.is_auto() ? border_box.width() : right_edge.to_px(layout_node)).value();
  671. auto bottom = border_box.top() + (bottom_edge.is_auto() ? border_box.height() : bottom_edge.to_px(layout_node)).value();
  672. return Gfx::FloatRect {
  673. left,
  674. top,
  675. right - left,
  676. bottom - top
  677. };
  678. }
  679. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcNumberSum::resolved_type() const
  680. {
  681. auto maybe_type = first_calc_number_product->resolved_type();
  682. if (!maybe_type.has_value())
  683. return {};
  684. auto type = maybe_type.value();
  685. return resolve_sum_type(type, zero_or_more_additional_calc_number_products);
  686. }
  687. template<typename ProductWithOperator>
  688. static Optional<CalculatedStyleValue::ResolvedType> resolve_product_type(CalculatedStyleValue::ResolvedType first_type, Vector<NonnullOwnPtr<ProductWithOperator>> const& zero_or_more_additional_values)
  689. {
  690. auto type = first_type;
  691. for (auto const& value : zero_or_more_additional_values) {
  692. auto maybe_value_type = value->resolved_type();
  693. if (!maybe_value_type.has_value())
  694. return {};
  695. auto value_type = maybe_value_type.value();
  696. if (value->op == CalculatedStyleValue::ProductOperation::Multiply) {
  697. // At *, check that at least one side is <number>.
  698. if (!(is_number(type) || is_number(value_type)))
  699. return {};
  700. // If both sides are <integer>, resolve to <integer>.
  701. if (type == CalculatedStyleValue::ResolvedType::Integer && value_type == CalculatedStyleValue::ResolvedType::Integer) {
  702. type = CalculatedStyleValue::ResolvedType::Integer;
  703. } else {
  704. // Otherwise, resolve to the type of the other side.
  705. if (is_number(type))
  706. type = value_type;
  707. }
  708. continue;
  709. } else {
  710. VERIFY(value->op == CalculatedStyleValue::ProductOperation::Divide);
  711. // At /, check that the right side is <number>.
  712. if (!is_number(value_type))
  713. return {};
  714. // If the left side is <integer>, resolve to <number>.
  715. if (type == CalculatedStyleValue::ResolvedType::Integer) {
  716. type = CalculatedStyleValue::ResolvedType::Number;
  717. } else {
  718. // Otherwise, resolve to the type of the left side.
  719. }
  720. // FIXME: Division by zero makes the whole calc() expression invalid.
  721. }
  722. }
  723. return type;
  724. }
  725. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcProduct::resolved_type() const
  726. {
  727. auto maybe_type = first_calc_value.resolved_type();
  728. if (!maybe_type.has_value())
  729. return {};
  730. auto type = maybe_type.value();
  731. return resolve_product_type(type, zero_or_more_additional_calc_values);
  732. }
  733. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcSumPartWithOperator::resolved_type() const
  734. {
  735. return value->resolved_type();
  736. }
  737. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcNumberProduct::resolved_type() const
  738. {
  739. auto maybe_type = first_calc_number_value.resolved_type();
  740. if (!maybe_type.has_value())
  741. return {};
  742. auto type = maybe_type.value();
  743. return resolve_product_type(type, zero_or_more_additional_calc_number_values);
  744. }
  745. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcNumberProductPartWithOperator::resolved_type() const
  746. {
  747. return value.resolved_type();
  748. }
  749. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcNumberSumPartWithOperator::resolved_type() const
  750. {
  751. return value->resolved_type();
  752. }
  753. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcProductPartWithOperator::resolved_type() const
  754. {
  755. return value.visit(
  756. [](CalcValue const& calc_value) {
  757. return calc_value.resolved_type();
  758. },
  759. [](CalcNumberValue const& calc_number_value) {
  760. return calc_number_value.resolved_type();
  761. });
  762. }
  763. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcValue::resolved_type() const
  764. {
  765. return value.visit(
  766. [](Number const& number) -> Optional<CalculatedStyleValue::ResolvedType> {
  767. return { number.is_integer() ? ResolvedType::Integer : ResolvedType::Number };
  768. },
  769. [](Angle const&) -> Optional<CalculatedStyleValue::ResolvedType> { return { ResolvedType::Angle }; },
  770. [](Frequency const&) -> Optional<CalculatedStyleValue::ResolvedType> { return { ResolvedType::Frequency }; },
  771. [](Length const&) -> Optional<CalculatedStyleValue::ResolvedType> { return { ResolvedType::Length }; },
  772. [](Percentage const&) -> Optional<CalculatedStyleValue::ResolvedType> { return { ResolvedType::Percentage }; },
  773. [](Time const&) -> Optional<CalculatedStyleValue::ResolvedType> { return { ResolvedType::Time }; },
  774. [](NonnullOwnPtr<CalcSum> const& sum) { return sum->resolved_type(); });
  775. }
  776. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcNumberValue::resolved_type() const
  777. {
  778. return value.visit(
  779. [](Number const& number) -> Optional<CalculatedStyleValue::ResolvedType> {
  780. return { number.is_integer() ? ResolvedType::Integer : ResolvedType::Number };
  781. },
  782. [](NonnullOwnPtr<CalcNumberSum> const& sum) { return sum->resolved_type(); });
  783. }
  784. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcNumberValue::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  785. {
  786. return value.visit(
  787. [&](Number const& number) -> CalculatedStyleValue::CalculationResult {
  788. return CalculatedStyleValue::CalculationResult { number };
  789. },
  790. [&](NonnullOwnPtr<CalcNumberSum> const& sum) -> CalculatedStyleValue::CalculationResult {
  791. return sum->resolve(layout_node, percentage_basis);
  792. });
  793. }
  794. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcValue::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  795. {
  796. return value.visit(
  797. [&](NonnullOwnPtr<CalcSum> const& sum) -> CalculatedStyleValue::CalculationResult {
  798. return sum->resolve(layout_node, percentage_basis);
  799. },
  800. [&](auto const& v) -> CalculatedStyleValue::CalculationResult {
  801. return CalculatedStyleValue::CalculationResult { v };
  802. });
  803. }
  804. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcSum::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  805. {
  806. auto value = first_calc_product->resolve(layout_node, percentage_basis);
  807. for (auto& additional_product : zero_or_more_additional_calc_products) {
  808. auto additional_value = additional_product->resolve(layout_node, percentage_basis);
  809. if (additional_product->op == CalculatedStyleValue::SumOperation::Add)
  810. value.add(additional_value, layout_node, percentage_basis);
  811. else if (additional_product->op == CalculatedStyleValue::SumOperation::Subtract)
  812. value.subtract(additional_value, layout_node, percentage_basis);
  813. else
  814. VERIFY_NOT_REACHED();
  815. }
  816. return value;
  817. }
  818. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcNumberSum::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  819. {
  820. auto value = first_calc_number_product->resolve(layout_node, percentage_basis);
  821. for (auto& additional_product : zero_or_more_additional_calc_number_products) {
  822. auto additional_value = additional_product->resolve(layout_node, percentage_basis);
  823. if (additional_product->op == CSS::CalculatedStyleValue::SumOperation::Add)
  824. value.add(additional_value, layout_node, percentage_basis);
  825. else if (additional_product->op == CalculatedStyleValue::SumOperation::Subtract)
  826. value.subtract(additional_value, layout_node, percentage_basis);
  827. else
  828. VERIFY_NOT_REACHED();
  829. }
  830. return value;
  831. }
  832. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcProduct::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  833. {
  834. auto value = first_calc_value.resolve(layout_node, percentage_basis);
  835. for (auto& additional_value : zero_or_more_additional_calc_values) {
  836. additional_value->value.visit(
  837. [&](CalculatedStyleValue::CalcValue const& calc_value) {
  838. VERIFY(additional_value->op == CalculatedStyleValue::ProductOperation::Multiply);
  839. auto resolved_value = calc_value.resolve(layout_node, percentage_basis);
  840. value.multiply_by(resolved_value, layout_node);
  841. },
  842. [&](CalculatedStyleValue::CalcNumberValue const& calc_number_value) {
  843. VERIFY(additional_value->op == CalculatedStyleValue::ProductOperation::Divide);
  844. auto resolved_calc_number_value = calc_number_value.resolve(layout_node, percentage_basis);
  845. // FIXME: Checking for division by 0 should happen during parsing.
  846. VERIFY(resolved_calc_number_value.value().get<Number>().value() != 0.0f);
  847. value.divide_by(resolved_calc_number_value, layout_node);
  848. });
  849. }
  850. return value;
  851. }
  852. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcNumberProduct::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  853. {
  854. auto value = first_calc_number_value.resolve(layout_node, percentage_basis);
  855. for (auto& additional_number_value : zero_or_more_additional_calc_number_values) {
  856. auto additional_value = additional_number_value->resolve(layout_node, percentage_basis);
  857. if (additional_number_value->op == CalculatedStyleValue::ProductOperation::Multiply)
  858. value.multiply_by(additional_value, layout_node);
  859. else if (additional_number_value->op == CalculatedStyleValue::ProductOperation::Divide)
  860. value.divide_by(additional_value, layout_node);
  861. else
  862. VERIFY_NOT_REACHED();
  863. }
  864. return value;
  865. }
  866. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcProductPartWithOperator::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  867. {
  868. return value.visit(
  869. [&](CalcValue const& calc_value) {
  870. return calc_value.resolve(layout_node, percentage_basis);
  871. },
  872. [&](CalcNumberValue const& calc_number_value) {
  873. return calc_number_value.resolve(layout_node, percentage_basis);
  874. });
  875. }
  876. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcSumPartWithOperator::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  877. {
  878. return value->resolve(layout_node, percentage_basis);
  879. }
  880. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcNumberProductPartWithOperator::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  881. {
  882. return value.resolve(layout_node, percentage_basis);
  883. }
  884. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcNumberSumPartWithOperator::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  885. {
  886. return value->resolve(layout_node, percentage_basis);
  887. }
  888. ErrorOr<String> FlexFlowStyleValue::to_string() const
  889. {
  890. return String::formatted("{} {}", TRY(m_properties.flex_direction->to_string()), TRY(m_properties.flex_wrap->to_string()));
  891. }
  892. ErrorOr<String> FontStyleValue::to_string() const
  893. {
  894. return String::formatted("{} {} {} / {} {}", TRY(m_properties.font_style->to_string()), TRY(m_properties.font_weight->to_string()), TRY(m_properties.font_size->to_string()), TRY(m_properties.line_height->to_string()), TRY(m_properties.font_families->to_string()));
  895. }
  896. ErrorOr<String> GridTrackPlacementShorthandStyleValue::to_string() const
  897. {
  898. if (m_properties.end->grid_track_placement().is_auto())
  899. return String::formatted("{}", TRY(m_properties.start->grid_track_placement().to_string()));
  900. return String::formatted("{} / {}", TRY(m_properties.start->grid_track_placement().to_string()), TRY(m_properties.end->grid_track_placement().to_string()));
  901. }
  902. ErrorOr<String> GridAreaShorthandStyleValue::to_string() const
  903. {
  904. StringBuilder builder;
  905. if (!m_properties.row_start->as_grid_track_placement().grid_track_placement().is_auto())
  906. TRY(builder.try_appendff("{}", TRY(m_properties.row_start->as_grid_track_placement().grid_track_placement().to_string())));
  907. if (!m_properties.column_start->as_grid_track_placement().grid_track_placement().is_auto())
  908. TRY(builder.try_appendff(" / {}", TRY(m_properties.column_start->as_grid_track_placement().grid_track_placement().to_string())));
  909. if (!m_properties.row_end->as_grid_track_placement().grid_track_placement().is_auto())
  910. TRY(builder.try_appendff(" / {}", TRY(m_properties.row_end->as_grid_track_placement().grid_track_placement().to_string())));
  911. if (!m_properties.column_end->as_grid_track_placement().grid_track_placement().is_auto())
  912. TRY(builder.try_appendff(" / {}", TRY(m_properties.column_end->as_grid_track_placement().grid_track_placement().to_string())));
  913. return builder.to_string();
  914. }
  915. ErrorOr<String> GridTrackPlacementStyleValue::to_string() const
  916. {
  917. return m_grid_track_placement.to_string();
  918. }
  919. ErrorOr<String> GridTemplateAreaStyleValue::to_string() const
  920. {
  921. StringBuilder builder;
  922. for (size_t y = 0; y < m_grid_template_area.size(); ++y) {
  923. for (size_t x = 0; x < m_grid_template_area[y].size(); ++x) {
  924. TRY(builder.try_appendff("{}", m_grid_template_area[y][x]));
  925. if (x < m_grid_template_area[y].size() - 1)
  926. TRY(builder.try_append(" "sv));
  927. }
  928. if (y < m_grid_template_area.size() - 1)
  929. TRY(builder.try_append(", "sv));
  930. }
  931. return builder.to_string();
  932. }
  933. ErrorOr<String> GridTrackSizeStyleValue::to_string() const
  934. {
  935. return m_grid_track_size_list.to_string();
  936. }
  937. ErrorOr<String> IdentifierStyleValue::to_string() const
  938. {
  939. return String::from_utf8(CSS::string_from_value_id(m_id));
  940. }
  941. bool IdentifierStyleValue::has_color() const
  942. {
  943. switch (m_id) {
  944. case ValueID::Currentcolor:
  945. case ValueID::LibwebLink:
  946. case ValueID::LibwebPaletteActiveLink:
  947. case ValueID::LibwebPaletteActiveWindowBorder1:
  948. case ValueID::LibwebPaletteActiveWindowBorder2:
  949. case ValueID::LibwebPaletteActiveWindowTitle:
  950. case ValueID::LibwebPaletteBase:
  951. case ValueID::LibwebPaletteBaseText:
  952. case ValueID::LibwebPaletteButton:
  953. case ValueID::LibwebPaletteButtonText:
  954. case ValueID::LibwebPaletteDesktopBackground:
  955. case ValueID::LibwebPaletteFocusOutline:
  956. case ValueID::LibwebPaletteHighlightWindowBorder1:
  957. case ValueID::LibwebPaletteHighlightWindowBorder2:
  958. case ValueID::LibwebPaletteHighlightWindowTitle:
  959. case ValueID::LibwebPaletteHoverHighlight:
  960. case ValueID::LibwebPaletteInactiveSelection:
  961. case ValueID::LibwebPaletteInactiveSelectionText:
  962. case ValueID::LibwebPaletteInactiveWindowBorder1:
  963. case ValueID::LibwebPaletteInactiveWindowBorder2:
  964. case ValueID::LibwebPaletteInactiveWindowTitle:
  965. case ValueID::LibwebPaletteLink:
  966. case ValueID::LibwebPaletteMenuBase:
  967. case ValueID::LibwebPaletteMenuBaseText:
  968. case ValueID::LibwebPaletteMenuSelection:
  969. case ValueID::LibwebPaletteMenuSelectionText:
  970. case ValueID::LibwebPaletteMenuStripe:
  971. case ValueID::LibwebPaletteMovingWindowBorder1:
  972. case ValueID::LibwebPaletteMovingWindowBorder2:
  973. case ValueID::LibwebPaletteMovingWindowTitle:
  974. case ValueID::LibwebPaletteRubberBandBorder:
  975. case ValueID::LibwebPaletteRubberBandFill:
  976. case ValueID::LibwebPaletteRuler:
  977. case ValueID::LibwebPaletteRulerActiveText:
  978. case ValueID::LibwebPaletteRulerBorder:
  979. case ValueID::LibwebPaletteRulerInactiveText:
  980. case ValueID::LibwebPaletteSelection:
  981. case ValueID::LibwebPaletteSelectionText:
  982. case ValueID::LibwebPaletteSyntaxComment:
  983. case ValueID::LibwebPaletteSyntaxControlKeyword:
  984. case ValueID::LibwebPaletteSyntaxIdentifier:
  985. case ValueID::LibwebPaletteSyntaxKeyword:
  986. case ValueID::LibwebPaletteSyntaxNumber:
  987. case ValueID::LibwebPaletteSyntaxOperator:
  988. case ValueID::LibwebPaletteSyntaxPreprocessorStatement:
  989. case ValueID::LibwebPaletteSyntaxPreprocessorValue:
  990. case ValueID::LibwebPaletteSyntaxPunctuation:
  991. case ValueID::LibwebPaletteSyntaxString:
  992. case ValueID::LibwebPaletteSyntaxType:
  993. case ValueID::LibwebPaletteTextCursor:
  994. case ValueID::LibwebPaletteThreedHighlight:
  995. case ValueID::LibwebPaletteThreedShadow1:
  996. case ValueID::LibwebPaletteThreedShadow2:
  997. case ValueID::LibwebPaletteVisitedLink:
  998. case ValueID::LibwebPaletteWindow:
  999. case ValueID::LibwebPaletteWindowText:
  1000. return true;
  1001. default:
  1002. return false;
  1003. }
  1004. }
  1005. Color IdentifierStyleValue::to_color(Layout::NodeWithStyle const& node) const
  1006. {
  1007. if (id() == CSS::ValueID::Currentcolor) {
  1008. if (!node.has_style())
  1009. return Color::Black;
  1010. return node.computed_values().color();
  1011. }
  1012. auto& document = node.document();
  1013. if (id() == CSS::ValueID::LibwebLink)
  1014. return document.link_color();
  1015. if (!document.page())
  1016. return {};
  1017. auto palette = document.page()->palette();
  1018. switch (id()) {
  1019. case CSS::ValueID::LibwebPaletteDesktopBackground:
  1020. return palette.color(ColorRole::DesktopBackground);
  1021. case CSS::ValueID::LibwebPaletteActiveWindowBorder1:
  1022. return palette.color(ColorRole::ActiveWindowBorder1);
  1023. case CSS::ValueID::LibwebPaletteActiveWindowBorder2:
  1024. return palette.color(ColorRole::ActiveWindowBorder2);
  1025. case CSS::ValueID::LibwebPaletteActiveWindowTitle:
  1026. return palette.color(ColorRole::ActiveWindowTitle);
  1027. case CSS::ValueID::LibwebPaletteInactiveWindowBorder1:
  1028. return palette.color(ColorRole::InactiveWindowBorder1);
  1029. case CSS::ValueID::LibwebPaletteInactiveWindowBorder2:
  1030. return palette.color(ColorRole::InactiveWindowBorder2);
  1031. case CSS::ValueID::LibwebPaletteInactiveWindowTitle:
  1032. return palette.color(ColorRole::InactiveWindowTitle);
  1033. case CSS::ValueID::LibwebPaletteMovingWindowBorder1:
  1034. return palette.color(ColorRole::MovingWindowBorder1);
  1035. case CSS::ValueID::LibwebPaletteMovingWindowBorder2:
  1036. return palette.color(ColorRole::MovingWindowBorder2);
  1037. case CSS::ValueID::LibwebPaletteMovingWindowTitle:
  1038. return palette.color(ColorRole::MovingWindowTitle);
  1039. case CSS::ValueID::LibwebPaletteHighlightWindowBorder1:
  1040. return palette.color(ColorRole::HighlightWindowBorder1);
  1041. case CSS::ValueID::LibwebPaletteHighlightWindowBorder2:
  1042. return palette.color(ColorRole::HighlightWindowBorder2);
  1043. case CSS::ValueID::LibwebPaletteHighlightWindowTitle:
  1044. return palette.color(ColorRole::HighlightWindowTitle);
  1045. case CSS::ValueID::LibwebPaletteMenuStripe:
  1046. return palette.color(ColorRole::MenuStripe);
  1047. case CSS::ValueID::LibwebPaletteMenuBase:
  1048. return palette.color(ColorRole::MenuBase);
  1049. case CSS::ValueID::LibwebPaletteMenuBaseText:
  1050. return palette.color(ColorRole::MenuBaseText);
  1051. case CSS::ValueID::LibwebPaletteMenuSelection:
  1052. return palette.color(ColorRole::MenuSelection);
  1053. case CSS::ValueID::LibwebPaletteMenuSelectionText:
  1054. return palette.color(ColorRole::MenuSelectionText);
  1055. case CSS::ValueID::LibwebPaletteWindow:
  1056. return palette.color(ColorRole::Window);
  1057. case CSS::ValueID::LibwebPaletteWindowText:
  1058. return palette.color(ColorRole::WindowText);
  1059. case CSS::ValueID::LibwebPaletteButton:
  1060. return palette.color(ColorRole::Button);
  1061. case CSS::ValueID::LibwebPaletteButtonText:
  1062. return palette.color(ColorRole::ButtonText);
  1063. case CSS::ValueID::LibwebPaletteBase:
  1064. return palette.color(ColorRole::Base);
  1065. case CSS::ValueID::LibwebPaletteBaseText:
  1066. return palette.color(ColorRole::BaseText);
  1067. case CSS::ValueID::LibwebPaletteThreedHighlight:
  1068. return palette.color(ColorRole::ThreedHighlight);
  1069. case CSS::ValueID::LibwebPaletteThreedShadow1:
  1070. return palette.color(ColorRole::ThreedShadow1);
  1071. case CSS::ValueID::LibwebPaletteThreedShadow2:
  1072. return palette.color(ColorRole::ThreedShadow2);
  1073. case CSS::ValueID::LibwebPaletteHoverHighlight:
  1074. return palette.color(ColorRole::HoverHighlight);
  1075. case CSS::ValueID::LibwebPaletteSelection:
  1076. return palette.color(ColorRole::Selection);
  1077. case CSS::ValueID::LibwebPaletteSelectionText:
  1078. return palette.color(ColorRole::SelectionText);
  1079. case CSS::ValueID::LibwebPaletteInactiveSelection:
  1080. return palette.color(ColorRole::InactiveSelection);
  1081. case CSS::ValueID::LibwebPaletteInactiveSelectionText:
  1082. return palette.color(ColorRole::InactiveSelectionText);
  1083. case CSS::ValueID::LibwebPaletteRubberBandFill:
  1084. return palette.color(ColorRole::RubberBandFill);
  1085. case CSS::ValueID::LibwebPaletteRubberBandBorder:
  1086. return palette.color(ColorRole::RubberBandBorder);
  1087. case CSS::ValueID::LibwebPaletteLink:
  1088. return palette.color(ColorRole::Link);
  1089. case CSS::ValueID::LibwebPaletteActiveLink:
  1090. return palette.color(ColorRole::ActiveLink);
  1091. case CSS::ValueID::LibwebPaletteVisitedLink:
  1092. return palette.color(ColorRole::VisitedLink);
  1093. case CSS::ValueID::LibwebPaletteRuler:
  1094. return palette.color(ColorRole::Ruler);
  1095. case CSS::ValueID::LibwebPaletteRulerBorder:
  1096. return palette.color(ColorRole::RulerBorder);
  1097. case CSS::ValueID::LibwebPaletteRulerActiveText:
  1098. return palette.color(ColorRole::RulerActiveText);
  1099. case CSS::ValueID::LibwebPaletteRulerInactiveText:
  1100. return palette.color(ColorRole::RulerInactiveText);
  1101. case CSS::ValueID::LibwebPaletteTextCursor:
  1102. return palette.color(ColorRole::TextCursor);
  1103. case CSS::ValueID::LibwebPaletteFocusOutline:
  1104. return palette.color(ColorRole::FocusOutline);
  1105. case CSS::ValueID::LibwebPaletteSyntaxComment:
  1106. return palette.color(ColorRole::SyntaxComment);
  1107. case CSS::ValueID::LibwebPaletteSyntaxNumber:
  1108. return palette.color(ColorRole::SyntaxNumber);
  1109. case CSS::ValueID::LibwebPaletteSyntaxString:
  1110. return palette.color(ColorRole::SyntaxString);
  1111. case CSS::ValueID::LibwebPaletteSyntaxType:
  1112. return palette.color(ColorRole::SyntaxType);
  1113. case CSS::ValueID::LibwebPaletteSyntaxPunctuation:
  1114. return palette.color(ColorRole::SyntaxPunctuation);
  1115. case CSS::ValueID::LibwebPaletteSyntaxOperator:
  1116. return palette.color(ColorRole::SyntaxOperator);
  1117. case CSS::ValueID::LibwebPaletteSyntaxKeyword:
  1118. return palette.color(ColorRole::SyntaxKeyword);
  1119. case CSS::ValueID::LibwebPaletteSyntaxControlKeyword:
  1120. return palette.color(ColorRole::SyntaxControlKeyword);
  1121. case CSS::ValueID::LibwebPaletteSyntaxIdentifier:
  1122. return palette.color(ColorRole::SyntaxIdentifier);
  1123. case CSS::ValueID::LibwebPaletteSyntaxPreprocessorStatement:
  1124. return palette.color(ColorRole::SyntaxPreprocessorStatement);
  1125. case CSS::ValueID::LibwebPaletteSyntaxPreprocessorValue:
  1126. return palette.color(ColorRole::SyntaxPreprocessorValue);
  1127. default:
  1128. return {};
  1129. }
  1130. }
  1131. ImageStyleValue::ImageStyleValue(AK::URL const& url)
  1132. : AbstractImageStyleValue(Type::Image)
  1133. , m_url(url)
  1134. {
  1135. }
  1136. void ImageStyleValue::load_any_resources(DOM::Document& document)
  1137. {
  1138. if (resource())
  1139. return;
  1140. m_document = &document;
  1141. auto request = LoadRequest::create_for_url_on_page(m_url, document.page());
  1142. set_resource(ResourceLoader::the().load_resource(Resource::Type::Image, request));
  1143. }
  1144. void ImageStyleValue::resource_did_load()
  1145. {
  1146. if (!m_document)
  1147. return;
  1148. // FIXME: Do less than a full repaint if possible?
  1149. if (m_document && m_document->browsing_context())
  1150. m_document->browsing_context()->set_needs_display();
  1151. if (resource()->is_animated() && resource()->frame_count() > 1) {
  1152. m_timer = Platform::Timer::create();
  1153. m_timer->set_interval(resource()->frame_duration(0));
  1154. m_timer->on_timeout = [this] { animate(); };
  1155. m_timer->start();
  1156. }
  1157. }
  1158. void ImageStyleValue::animate()
  1159. {
  1160. m_current_frame_index = (m_current_frame_index + 1) % resource()->frame_count();
  1161. auto current_frame_duration = resource()->frame_duration(m_current_frame_index);
  1162. if (current_frame_duration != m_timer->interval())
  1163. m_timer->restart(current_frame_duration);
  1164. if (m_current_frame_index == resource()->frame_count() - 1) {
  1165. ++m_loops_completed;
  1166. if (m_loops_completed > 0 && m_loops_completed == resource()->loop_count())
  1167. m_timer->stop();
  1168. }
  1169. if (on_animate)
  1170. on_animate();
  1171. }
  1172. Gfx::Bitmap const* ImageStyleValue::bitmap(size_t frame_index) const
  1173. {
  1174. if (!resource())
  1175. return nullptr;
  1176. return resource()->bitmap(frame_index);
  1177. }
  1178. ErrorOr<String> ImageStyleValue::to_string() const
  1179. {
  1180. return serialize_a_url(m_url.to_deprecated_string());
  1181. }
  1182. bool ImageStyleValue::equals(StyleValue const& other) const
  1183. {
  1184. if (type() != other.type())
  1185. return false;
  1186. return m_url == other.as_image().m_url;
  1187. }
  1188. Optional<CSSPixels> ImageStyleValue::natural_width() const
  1189. {
  1190. if (auto* b = bitmap(0); b != nullptr)
  1191. return b->width();
  1192. return {};
  1193. }
  1194. Optional<CSSPixels> ImageStyleValue::natural_height() const
  1195. {
  1196. if (auto* b = bitmap(0); b != nullptr)
  1197. return b->height();
  1198. return {};
  1199. }
  1200. void ImageStyleValue::paint(PaintContext& context, DevicePixelRect const& dest_rect, CSS::ImageRendering image_rendering) const
  1201. {
  1202. if (auto* b = bitmap(m_current_frame_index); b != nullptr)
  1203. context.painter().draw_scaled_bitmap(dest_rect.to_type<int>(), *b, bitmap(0)->rect(), 1.0f, to_gfx_scaling_mode(image_rendering));
  1204. }
  1205. static ErrorOr<void> serialize_color_stop_list(StringBuilder& builder, auto const& color_stop_list)
  1206. {
  1207. bool first = true;
  1208. for (auto const& element : color_stop_list) {
  1209. if (!first)
  1210. TRY(builder.try_append(", "sv));
  1211. if (element.transition_hint.has_value())
  1212. TRY(builder.try_appendff("{}, "sv, TRY(element.transition_hint->value.to_string())));
  1213. TRY(serialize_a_srgb_value(builder, element.color_stop.color));
  1214. for (auto position : Array { &element.color_stop.position, &element.color_stop.second_position }) {
  1215. if (position->has_value())
  1216. TRY(builder.try_appendff(" {}"sv, TRY((*position)->to_string())));
  1217. }
  1218. first = false;
  1219. }
  1220. return {};
  1221. }
  1222. ErrorOr<String> LinearGradientStyleValue::to_string() const
  1223. {
  1224. StringBuilder builder;
  1225. auto side_or_corner_to_string = [](SideOrCorner value) {
  1226. switch (value) {
  1227. case SideOrCorner::Top:
  1228. return "top"sv;
  1229. case SideOrCorner::Bottom:
  1230. return "bottom"sv;
  1231. case SideOrCorner::Left:
  1232. return "left"sv;
  1233. case SideOrCorner::Right:
  1234. return "right"sv;
  1235. case SideOrCorner::TopLeft:
  1236. return "top left"sv;
  1237. case SideOrCorner::TopRight:
  1238. return "top right"sv;
  1239. case SideOrCorner::BottomLeft:
  1240. return "bottom left"sv;
  1241. case SideOrCorner::BottomRight:
  1242. return "bottom right"sv;
  1243. default:
  1244. VERIFY_NOT_REACHED();
  1245. }
  1246. };
  1247. if (m_properties.gradient_type == GradientType::WebKit)
  1248. TRY(builder.try_append("-webkit-"sv));
  1249. if (is_repeating())
  1250. TRY(builder.try_append("repeating-"sv));
  1251. TRY(builder.try_append("linear-gradient("sv));
  1252. TRY(m_properties.direction.visit(
  1253. [&](SideOrCorner side_or_corner) -> ErrorOr<void> {
  1254. return builder.try_appendff("{}{}, "sv, m_properties.gradient_type == GradientType::Standard ? "to "sv : ""sv, side_or_corner_to_string(side_or_corner));
  1255. },
  1256. [&](Angle const& angle) -> ErrorOr<void> {
  1257. return builder.try_appendff("{}, "sv, TRY(angle.to_string()));
  1258. }));
  1259. TRY(serialize_color_stop_list(builder, m_properties.color_stop_list));
  1260. TRY(builder.try_append(")"sv));
  1261. return builder.to_string();
  1262. }
  1263. bool LinearGradientStyleValue::equals(StyleValue const& other_) const
  1264. {
  1265. if (type() != other_.type())
  1266. return false;
  1267. auto& other = other_.as_linear_gradient();
  1268. return m_properties == other.m_properties;
  1269. }
  1270. float LinearGradientStyleValue::angle_degrees(CSSPixelSize gradient_size) const
  1271. {
  1272. auto corner_angle_degrees = [&] {
  1273. return static_cast<float>(atan2(gradient_size.height().value(), gradient_size.width().value())) * 180 / AK::Pi<float>;
  1274. };
  1275. return m_properties.direction.visit(
  1276. [&](SideOrCorner side_or_corner) {
  1277. auto angle = [&] {
  1278. switch (side_or_corner) {
  1279. case SideOrCorner::Top:
  1280. return 0.0f;
  1281. case SideOrCorner::Bottom:
  1282. return 180.0f;
  1283. case SideOrCorner::Left:
  1284. return 270.0f;
  1285. case SideOrCorner::Right:
  1286. return 90.0f;
  1287. case SideOrCorner::TopRight:
  1288. return corner_angle_degrees();
  1289. case SideOrCorner::BottomLeft:
  1290. return corner_angle_degrees() + 180.0f;
  1291. case SideOrCorner::TopLeft:
  1292. return -corner_angle_degrees();
  1293. case SideOrCorner::BottomRight:
  1294. return -(corner_angle_degrees() + 180.0f);
  1295. default:
  1296. VERIFY_NOT_REACHED();
  1297. }
  1298. }();
  1299. // Note: For unknowable reasons the angles are opposite on the -webkit- version
  1300. if (m_properties.gradient_type == GradientType::WebKit)
  1301. return angle + 180.0f;
  1302. return angle;
  1303. },
  1304. [&](Angle const& angle) {
  1305. return angle.to_degrees();
  1306. });
  1307. }
  1308. void LinearGradientStyleValue::resolve_for_size(Layout::Node const& node, CSSPixelSize size) const
  1309. {
  1310. if (m_resolved.has_value() && m_resolved->size == size)
  1311. return;
  1312. m_resolved = ResolvedData { Painting::resolve_linear_gradient_data(node, size, *this), size };
  1313. }
  1314. void LinearGradientStyleValue::paint(PaintContext& context, DevicePixelRect const& dest_rect, CSS::ImageRendering) const
  1315. {
  1316. VERIFY(m_resolved.has_value());
  1317. Painting::paint_linear_gradient(context, dest_rect, m_resolved->data);
  1318. }
  1319. CSSPixelPoint PositionValue::resolved(Layout::Node const& node, CSSPixelRect const& rect) const
  1320. {
  1321. // Note: A preset + a none default x/y_relative_to is impossible in the syntax (and makes little sense)
  1322. CSSPixels x = horizontal_position.visit(
  1323. [&](HorizontalPreset preset) -> CSSPixels {
  1324. return rect.width() * [&] {
  1325. switch (preset) {
  1326. case HorizontalPreset::Left:
  1327. return 0.0f;
  1328. case HorizontalPreset::Center:
  1329. return 0.5f;
  1330. case HorizontalPreset::Right:
  1331. return 1.0f;
  1332. default:
  1333. VERIFY_NOT_REACHED();
  1334. }
  1335. }();
  1336. },
  1337. [&](LengthPercentage length_percentage) -> CSSPixels {
  1338. return length_percentage.resolved(node, Length::make_px(rect.width())).to_px(node);
  1339. });
  1340. CSSPixels y = vertical_position.visit(
  1341. [&](VerticalPreset preset) -> CSSPixels {
  1342. return rect.height() * [&] {
  1343. switch (preset) {
  1344. case VerticalPreset::Top:
  1345. return 0.0f;
  1346. case VerticalPreset::Center:
  1347. return 0.5f;
  1348. case VerticalPreset::Bottom:
  1349. return 1.0f;
  1350. default:
  1351. VERIFY_NOT_REACHED();
  1352. }
  1353. }();
  1354. },
  1355. [&](LengthPercentage length_percentage) -> CSSPixels {
  1356. return length_percentage.resolved(node, Length::make_px(rect.height())).to_px(node);
  1357. });
  1358. if (x_relative_to == HorizontalEdge::Right)
  1359. x = rect.width() - x;
  1360. if (y_relative_to == VerticalEdge::Bottom)
  1361. y = rect.height() - y;
  1362. return CSSPixelPoint { rect.x() + x, rect.y() + y };
  1363. }
  1364. ErrorOr<void> PositionValue::serialize(StringBuilder& builder) const
  1365. {
  1366. // Note: This means our serialization with simplify any with explicit edges that are just `top left`.
  1367. bool has_relative_edges = x_relative_to == HorizontalEdge::Right || y_relative_to == VerticalEdge::Bottom;
  1368. if (has_relative_edges)
  1369. TRY(builder.try_append(x_relative_to == HorizontalEdge::Left ? "left "sv : "right "sv));
  1370. TRY(horizontal_position.visit(
  1371. [&](HorizontalPreset preset) -> ErrorOr<void> {
  1372. return builder.try_append([&] {
  1373. switch (preset) {
  1374. case HorizontalPreset::Left:
  1375. return "left"sv;
  1376. case HorizontalPreset::Center:
  1377. return "center"sv;
  1378. case HorizontalPreset::Right:
  1379. return "right"sv;
  1380. default:
  1381. VERIFY_NOT_REACHED();
  1382. }
  1383. }());
  1384. },
  1385. [&](LengthPercentage length_percentage) -> ErrorOr<void> {
  1386. return builder.try_appendff(TRY(length_percentage.to_string()));
  1387. }));
  1388. TRY(builder.try_append(' '));
  1389. if (has_relative_edges)
  1390. TRY(builder.try_append(y_relative_to == VerticalEdge::Top ? "top "sv : "bottom "sv));
  1391. TRY(vertical_position.visit(
  1392. [&](VerticalPreset preset) -> ErrorOr<void> {
  1393. return builder.try_append([&] {
  1394. switch (preset) {
  1395. case VerticalPreset::Top:
  1396. return "top"sv;
  1397. case VerticalPreset::Center:
  1398. return "center"sv;
  1399. case VerticalPreset::Bottom:
  1400. return "bottom"sv;
  1401. default:
  1402. VERIFY_NOT_REACHED();
  1403. }
  1404. }());
  1405. },
  1406. [&](LengthPercentage length_percentage) -> ErrorOr<void> {
  1407. return builder.try_append(TRY(length_percentage.to_string()));
  1408. }));
  1409. return {};
  1410. }
  1411. ErrorOr<String> RadialGradientStyleValue::to_string() const
  1412. {
  1413. StringBuilder builder;
  1414. if (is_repeating())
  1415. TRY(builder.try_append("repeating-"sv));
  1416. TRY(builder.try_appendff("radial-gradient({} "sv,
  1417. m_properties.ending_shape == EndingShape::Circle ? "circle"sv : "ellipse"sv));
  1418. TRY(m_properties.size.visit(
  1419. [&](Extent extent) -> ErrorOr<void> {
  1420. return builder.try_append([&] {
  1421. switch (extent) {
  1422. case Extent::ClosestCorner:
  1423. return "closest-corner"sv;
  1424. case Extent::ClosestSide:
  1425. return "closest-side"sv;
  1426. case Extent::FarthestCorner:
  1427. return "farthest-corner"sv;
  1428. case Extent::FarthestSide:
  1429. return "farthest-side"sv;
  1430. default:
  1431. VERIFY_NOT_REACHED();
  1432. }
  1433. }());
  1434. },
  1435. [&](CircleSize const& circle_size) -> ErrorOr<void> {
  1436. return builder.try_append(TRY(circle_size.radius.to_string()));
  1437. },
  1438. [&](EllipseSize const& ellipse_size) -> ErrorOr<void> {
  1439. return builder.try_appendff("{} {}", TRY(ellipse_size.radius_a.to_string()), TRY(ellipse_size.radius_b.to_string()));
  1440. }));
  1441. if (m_properties.position != PositionValue::center()) {
  1442. TRY(builder.try_appendff(" at "sv));
  1443. TRY(m_properties.position.serialize(builder));
  1444. }
  1445. TRY(builder.try_append(", "sv));
  1446. TRY(serialize_color_stop_list(builder, m_properties.color_stop_list));
  1447. TRY(builder.try_append(')'));
  1448. return builder.to_string();
  1449. }
  1450. Gfx::FloatSize RadialGradientStyleValue::resolve_size(Layout::Node const& node, Gfx::FloatPoint center, Gfx::FloatRect const& size) const
  1451. {
  1452. auto const side_shape = [&](auto distance_function) {
  1453. auto const distance_from = [&](float v, float a, float b, auto distance_function) {
  1454. return distance_function(fabs(a - v), fabs(b - v));
  1455. };
  1456. auto x_dist = distance_from(center.x(), size.left(), size.right(), distance_function);
  1457. auto y_dist = distance_from(center.y(), size.top(), size.bottom(), distance_function);
  1458. if (m_properties.ending_shape == EndingShape::Circle) {
  1459. auto dist = distance_function(x_dist, y_dist);
  1460. return Gfx::FloatSize { dist, dist };
  1461. } else {
  1462. return Gfx::FloatSize { x_dist, y_dist };
  1463. }
  1464. };
  1465. auto const closest_side_shape = [&] {
  1466. return side_shape(AK::min<float>);
  1467. };
  1468. auto const farthest_side_shape = [&] {
  1469. return side_shape(AK::max<float>);
  1470. };
  1471. auto const corner_distance = [&](auto distance_compare, Gfx::FloatPoint& corner) {
  1472. auto top_left_distance = size.top_left().distance_from(center);
  1473. auto top_right_distance = size.top_right().distance_from(center);
  1474. auto bottom_right_distance = size.bottom_right().distance_from(center);
  1475. auto bottom_left_distance = size.bottom_left().distance_from(center);
  1476. auto distance = top_left_distance;
  1477. if (distance_compare(top_right_distance, distance)) {
  1478. corner = size.top_right();
  1479. distance = top_right_distance;
  1480. }
  1481. if (distance_compare(bottom_right_distance, distance)) {
  1482. corner = size.top_right();
  1483. distance = bottom_right_distance;
  1484. }
  1485. if (distance_compare(bottom_left_distance, distance)) {
  1486. corner = size.top_right();
  1487. distance = bottom_left_distance;
  1488. }
  1489. return distance;
  1490. };
  1491. auto const closest_corner_distance = [&](Gfx::FloatPoint& corner) {
  1492. return corner_distance([](float a, float b) { return a < b; }, corner);
  1493. };
  1494. auto const farthest_corner_distance = [&](Gfx::FloatPoint& corner) {
  1495. return corner_distance([](float a, float b) { return a > b; }, corner);
  1496. };
  1497. auto const corner_shape = [&](auto corner_distance, auto get_shape) {
  1498. Gfx::FloatPoint corner {};
  1499. auto distance = corner_distance(corner);
  1500. if (m_properties.ending_shape == EndingShape::Ellipse) {
  1501. auto shape = get_shape();
  1502. auto aspect_ratio = shape.width() / shape.height();
  1503. auto p = corner - center;
  1504. auto radius_a = AK::sqrt(p.y() * p.y() * aspect_ratio * aspect_ratio + p.x() * p.x());
  1505. auto radius_b = radius_a / aspect_ratio;
  1506. return Gfx::FloatSize { radius_a, radius_b };
  1507. }
  1508. return Gfx::FloatSize { distance, distance };
  1509. };
  1510. // https://w3c.github.io/csswg-drafts/css-images/#radial-gradient-syntax
  1511. auto resolved_size = m_properties.size.visit(
  1512. [&](Extent extent) {
  1513. switch (extent) {
  1514. case Extent::ClosestSide:
  1515. // The ending shape is sized so that it exactly meets the side of the gradient box closest to the gradient’s center.
  1516. // If the shape is an ellipse, it exactly meets the closest side in each dimension.
  1517. return closest_side_shape();
  1518. case Extent::ClosestCorner:
  1519. // The ending shape is sized so that it passes through the corner of the gradient box closest to the gradient’s center.
  1520. // If the shape is an ellipse, the ending shape is given the same aspect-ratio it would have if closest-side were specified
  1521. return corner_shape(closest_corner_distance, closest_side_shape);
  1522. case Extent::FarthestCorner:
  1523. // Same as closest-corner, except the ending shape is sized based on the farthest corner.
  1524. // If the shape is an ellipse, the ending shape is given the same aspect ratio it would have if farthest-side were specified.
  1525. return corner_shape(farthest_corner_distance, farthest_side_shape);
  1526. case Extent::FarthestSide:
  1527. // Same as closest-side, except the ending shape is sized based on the farthest side(s).
  1528. return farthest_side_shape();
  1529. default:
  1530. VERIFY_NOT_REACHED();
  1531. }
  1532. },
  1533. [&](CircleSize const& circle_size) {
  1534. auto radius = circle_size.radius.to_px(node);
  1535. return Gfx::FloatSize { radius, radius };
  1536. },
  1537. [&](EllipseSize const& ellipse_size) {
  1538. auto radius_a = ellipse_size.radius_a.resolved(node, CSS::Length::make_px(size.width())).to_px(node);
  1539. auto radius_b = ellipse_size.radius_b.resolved(node, CSS::Length::make_px(size.height())).to_px(node);
  1540. return Gfx::FloatSize { radius_a, radius_b };
  1541. });
  1542. // Handle degenerate cases
  1543. // https://w3c.github.io/csswg-drafts/css-images/#degenerate-radials
  1544. constexpr auto arbitrary_small_number = 1e-10;
  1545. constexpr auto arbitrary_large_number = 1e10;
  1546. // If the ending shape is a circle with zero radius:
  1547. if (m_properties.ending_shape == EndingShape::Circle && resolved_size.is_empty()) {
  1548. // Render as if the ending shape was a circle whose radius was an arbitrary very small number greater than zero.
  1549. // This will make the gradient continue to look like a circle.
  1550. return Gfx::FloatSize { arbitrary_small_number, arbitrary_small_number };
  1551. }
  1552. // If the ending shape has zero width (regardless of the height):
  1553. if (resolved_size.width() <= 0) {
  1554. // Render as if the ending shape was an ellipse whose height was an arbitrary very large number
  1555. // and whose width was an arbitrary very small number greater than zero.
  1556. // This will make the gradient look similar to a horizontal linear gradient that is mirrored across the center of the ellipse.
  1557. // It also means that all color-stop positions specified with a percentage resolve to 0px.
  1558. return Gfx::FloatSize { arbitrary_small_number, arbitrary_large_number };
  1559. }
  1560. // Otherwise, if the ending shape has zero height:
  1561. if (resolved_size.height() <= 0) {
  1562. // Render as if the ending shape was an ellipse whose width was an arbitrary very large number and whose height
  1563. // was an arbitrary very small number greater than zero. This will make the gradient look like a solid-color image equal
  1564. // to the color of the last color-stop, or equal to the average color of the gradient if it’s repeating.
  1565. return Gfx::FloatSize { arbitrary_large_number, arbitrary_small_number };
  1566. }
  1567. return resolved_size;
  1568. }
  1569. void RadialGradientStyleValue::resolve_for_size(Layout::Node const& node, CSSPixelSize paint_size) const
  1570. {
  1571. CSSPixelRect gradient_box { { 0, 0 }, paint_size };
  1572. auto center = m_properties.position.resolved(node, gradient_box).to_type<float>();
  1573. auto gradient_size = resolve_size(node, center, gradient_box.to_type<float>());
  1574. if (m_resolved.has_value() && m_resolved->gradient_size == gradient_size)
  1575. return;
  1576. m_resolved = ResolvedData {
  1577. Painting::resolve_radial_gradient_data(node, gradient_size.to_type<CSSPixels>(), *this),
  1578. gradient_size,
  1579. center,
  1580. };
  1581. }
  1582. bool RadialGradientStyleValue::equals(StyleValue const& other) const
  1583. {
  1584. if (type() != other.type())
  1585. return false;
  1586. auto& other_gradient = other.as_radial_gradient();
  1587. return m_properties == other_gradient.m_properties;
  1588. }
  1589. void RadialGradientStyleValue::paint(PaintContext& context, DevicePixelRect const& dest_rect, CSS::ImageRendering) const
  1590. {
  1591. VERIFY(m_resolved.has_value());
  1592. Painting::paint_radial_gradient(context, dest_rect, m_resolved->data,
  1593. context.rounded_device_point(m_resolved->center.to_type<CSSPixels>()),
  1594. context.rounded_device_size(m_resolved->gradient_size.to_type<CSSPixels>()));
  1595. }
  1596. ErrorOr<String> ConicGradientStyleValue::to_string() const
  1597. {
  1598. StringBuilder builder;
  1599. if (is_repeating())
  1600. TRY(builder.try_append("repeating-"sv));
  1601. TRY(builder.try_append("conic-gradient("sv));
  1602. bool has_from_angle = false;
  1603. bool has_at_position = false;
  1604. if ((has_from_angle = m_properties.from_angle.to_degrees() != 0))
  1605. TRY(builder.try_appendff("from {}", TRY(m_properties.from_angle.to_string())));
  1606. if ((has_at_position = m_properties.position != PositionValue::center())) {
  1607. if (has_from_angle)
  1608. TRY(builder.try_append(' '));
  1609. TRY(builder.try_appendff("at "sv));
  1610. TRY(m_properties.position.serialize(builder));
  1611. }
  1612. if (has_from_angle || has_at_position)
  1613. TRY(builder.try_append(", "sv));
  1614. TRY(serialize_color_stop_list(builder, m_properties.color_stop_list));
  1615. TRY(builder.try_append(')'));
  1616. return builder.to_string();
  1617. }
  1618. void ConicGradientStyleValue::resolve_for_size(Layout::Node const& node, CSSPixelSize size) const
  1619. {
  1620. if (!m_resolved.has_value())
  1621. m_resolved = ResolvedData { Painting::resolve_conic_gradient_data(node, *this), {} };
  1622. m_resolved->position = m_properties.position.resolved(node, CSSPixelRect { { 0, 0 }, size });
  1623. }
  1624. void ConicGradientStyleValue::paint(PaintContext& context, DevicePixelRect const& dest_rect, CSS::ImageRendering) const
  1625. {
  1626. VERIFY(m_resolved.has_value());
  1627. Painting::paint_conic_gradient(context, dest_rect, m_resolved->data, context.rounded_device_point(m_resolved->position));
  1628. }
  1629. bool ConicGradientStyleValue::equals(StyleValue const& other) const
  1630. {
  1631. if (type() != other.type())
  1632. return false;
  1633. auto& other_gradient = other.as_conic_gradient();
  1634. return m_properties == other_gradient.m_properties;
  1635. }
  1636. float ConicGradientStyleValue::angle_degrees() const
  1637. {
  1638. return m_properties.from_angle.to_degrees();
  1639. }
  1640. ErrorOr<String> ListStyleStyleValue::to_string() const
  1641. {
  1642. return String::formatted("{} {} {}", TRY(m_properties.position->to_string()), TRY(m_properties.image->to_string()), TRY(m_properties.style_type->to_string()));
  1643. }
  1644. ErrorOr<String> NumericStyleValue::to_string() const
  1645. {
  1646. return m_value.visit(
  1647. [](auto value) {
  1648. return String::formatted("{}", value);
  1649. });
  1650. }
  1651. ErrorOr<String> OverflowStyleValue::to_string() const
  1652. {
  1653. return String::formatted("{} {}", TRY(m_properties.overflow_x->to_string()), TRY(m_properties.overflow_y->to_string()));
  1654. }
  1655. ErrorOr<String> PercentageStyleValue::to_string() const
  1656. {
  1657. return m_percentage.to_string();
  1658. }
  1659. ErrorOr<String> PositionStyleValue::to_string() const
  1660. {
  1661. auto to_string = [](PositionEdge edge) {
  1662. switch (edge) {
  1663. case PositionEdge::Left:
  1664. return "left";
  1665. case PositionEdge::Right:
  1666. return "right";
  1667. case PositionEdge::Top:
  1668. return "top";
  1669. case PositionEdge::Bottom:
  1670. return "bottom";
  1671. }
  1672. VERIFY_NOT_REACHED();
  1673. };
  1674. return String::formatted("{} {} {} {}", to_string(m_properties.edge_x), TRY(m_properties.offset_x.to_string()), to_string(m_properties.edge_y), TRY(m_properties.offset_y.to_string()));
  1675. }
  1676. ErrorOr<String> RectStyleValue::to_string() const
  1677. {
  1678. return String::formatted("rect({} {} {} {})", m_rect.top_edge, m_rect.right_edge, m_rect.bottom_edge, m_rect.left_edge);
  1679. }
  1680. ErrorOr<String> ShadowStyleValue::to_string() const
  1681. {
  1682. StringBuilder builder;
  1683. TRY(builder.try_appendff("{} {} {} {} {}", m_properties.color.to_deprecated_string(), TRY(m_properties.offset_x.to_string()), TRY(m_properties.offset_y.to_string()), TRY(m_properties.blur_radius.to_string()), TRY(m_properties.spread_distance.to_string())));
  1684. if (m_properties.placement == ShadowPlacement::Inner)
  1685. TRY(builder.try_append(" inset"sv));
  1686. return builder.to_string();
  1687. }
  1688. ErrorOr<String> TextDecorationStyleValue::to_string() const
  1689. {
  1690. return String::formatted("{} {} {} {}", TRY(m_properties.line->to_string()), TRY(m_properties.thickness->to_string()), TRY(m_properties.style->to_string()), TRY(m_properties.color->to_string()));
  1691. }
  1692. ErrorOr<String> TransformationStyleValue::to_string() const
  1693. {
  1694. StringBuilder builder;
  1695. TRY(builder.try_append(CSS::to_string(m_properties.transform_function)));
  1696. TRY(builder.try_append('('));
  1697. for (size_t i = 0; i < m_properties.values.size(); ++i) {
  1698. TRY(builder.try_append(TRY(m_properties.values[i]->to_string())));
  1699. if (i != m_properties.values.size() - 1)
  1700. TRY(builder.try_append(", "sv));
  1701. }
  1702. TRY(builder.try_append(')'));
  1703. return builder.to_string();
  1704. }
  1705. bool TransformationStyleValue::Properties::operator==(Properties const& other) const
  1706. {
  1707. return transform_function == other.transform_function && values.span() == other.values.span();
  1708. }
  1709. ErrorOr<String> UnresolvedStyleValue::to_string() const
  1710. {
  1711. StringBuilder builder;
  1712. for (auto& value : m_values)
  1713. TRY(builder.try_append(TRY(value.to_string())));
  1714. return builder.to_string();
  1715. }
  1716. bool UnresolvedStyleValue::equals(StyleValue const& other) const
  1717. {
  1718. if (type() != other.type())
  1719. return false;
  1720. // This is a case where comparing the strings actually makes sense.
  1721. return to_string().release_value_but_fixme_should_propagate_errors() == other.to_string().release_value_but_fixme_should_propagate_errors();
  1722. }
  1723. bool StyleValueList::Properties::operator==(Properties const& other) const
  1724. {
  1725. return separator == other.separator && values.span() == other.values.span();
  1726. }
  1727. ErrorOr<String> StyleValueList::to_string() const
  1728. {
  1729. auto separator = ""sv;
  1730. switch (m_properties.separator) {
  1731. case Separator::Space:
  1732. separator = " "sv;
  1733. break;
  1734. case Separator::Comma:
  1735. separator = ", "sv;
  1736. break;
  1737. default:
  1738. VERIFY_NOT_REACHED();
  1739. }
  1740. StringBuilder builder;
  1741. for (size_t i = 0; i < m_properties.values.size(); ++i) {
  1742. TRY(builder.try_append(TRY(m_properties.values[i]->to_string())));
  1743. if (i != m_properties.values.size() - 1)
  1744. TRY(builder.try_append(separator));
  1745. }
  1746. return builder.to_string();
  1747. }
  1748. ValueComparingNonnullRefPtr<GridTemplateAreaStyleValue> GridTemplateAreaStyleValue::create(Vector<Vector<String>> grid_template_area)
  1749. {
  1750. return adopt_ref(*new GridTemplateAreaStyleValue(grid_template_area));
  1751. }
  1752. ValueComparingNonnullRefPtr<GridTrackPlacementStyleValue> GridTrackPlacementStyleValue::create(CSS::GridTrackPlacement grid_track_placement)
  1753. {
  1754. return adopt_ref(*new GridTrackPlacementStyleValue(grid_track_placement));
  1755. }
  1756. ValueComparingNonnullRefPtr<GridTrackSizeStyleValue> GridTrackSizeStyleValue::create(CSS::GridTrackSizeList grid_track_size_list)
  1757. {
  1758. return adopt_ref(*new GridTrackSizeStyleValue(grid_track_size_list));
  1759. }
  1760. ValueComparingNonnullRefPtr<GridTrackSizeStyleValue> GridTrackSizeStyleValue::make_auto()
  1761. {
  1762. return adopt_ref(*new GridTrackSizeStyleValue(CSS::GridTrackSizeList()));
  1763. }
  1764. ValueComparingNonnullRefPtr<RectStyleValue> RectStyleValue::create(EdgeRect rect)
  1765. {
  1766. return adopt_ref(*new RectStyleValue(rect));
  1767. }
  1768. ValueComparingNonnullRefPtr<LengthStyleValue> LengthStyleValue::create(Length const& length)
  1769. {
  1770. if (length.is_auto()) {
  1771. static auto value = adopt_ref(*new LengthStyleValue(CSS::Length::make_auto()));
  1772. return value;
  1773. }
  1774. if (length.is_px()) {
  1775. if (length.raw_value() == 0) {
  1776. static auto value = adopt_ref(*new LengthStyleValue(CSS::Length::make_px(0)));
  1777. return value;
  1778. }
  1779. if (length.raw_value() == 1) {
  1780. static auto value = adopt_ref(*new LengthStyleValue(CSS::Length::make_px(1)));
  1781. return value;
  1782. }
  1783. }
  1784. return adopt_ref(*new LengthStyleValue(length));
  1785. }
  1786. Optional<CSS::Length> absolutized_length(CSS::Length const& length, CSSPixelRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, CSSPixels font_size, CSSPixels root_font_size, CSSPixels line_height, CSSPixels root_line_height)
  1787. {
  1788. if (length.is_px())
  1789. return {};
  1790. if (length.is_absolute() || length.is_relative()) {
  1791. auto px = length.to_px(viewport_rect, font_metrics, font_size, root_font_size, line_height, root_line_height);
  1792. return CSS::Length::make_px(px);
  1793. }
  1794. return {};
  1795. }
  1796. ValueComparingNonnullRefPtr<StyleValue const> StyleValue::absolutized(CSSPixelRect const&, Gfx::FontPixelMetrics const&, CSSPixels, CSSPixels, CSSPixels, CSSPixels) const
  1797. {
  1798. return *this;
  1799. }
  1800. ValueComparingNonnullRefPtr<StyleValue const> LengthStyleValue::absolutized(CSSPixelRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, CSSPixels font_size, CSSPixels root_font_size, CSSPixels line_height, CSSPixels root_line_height) const
  1801. {
  1802. if (auto length = absolutized_length(m_length, viewport_rect, font_metrics, font_size, root_font_size, line_height, root_line_height); length.has_value())
  1803. return LengthStyleValue::create(length.release_value());
  1804. return *this;
  1805. }
  1806. ValueComparingNonnullRefPtr<StyleValue const> ShadowStyleValue::absolutized(CSSPixelRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, CSSPixels font_size, CSSPixels root_font_size, CSSPixels line_height, CSSPixels root_line_height) const
  1807. {
  1808. auto absolutized_offset_x = absolutized_length(m_properties.offset_x, viewport_rect, font_metrics, font_size, root_font_size, line_height, root_line_height).value_or(m_properties.offset_x);
  1809. auto absolutized_offset_y = absolutized_length(m_properties.offset_y, viewport_rect, font_metrics, font_size, root_font_size, line_height, root_line_height).value_or(m_properties.offset_y);
  1810. auto absolutized_blur_radius = absolutized_length(m_properties.blur_radius, viewport_rect, font_metrics, font_size, root_font_size, line_height, root_line_height).value_or(m_properties.blur_radius);
  1811. auto absolutized_spread_distance = absolutized_length(m_properties.spread_distance, viewport_rect, font_metrics, font_size, root_font_size, line_height, root_line_height).value_or(m_properties.spread_distance);
  1812. return ShadowStyleValue::create(m_properties.color, absolutized_offset_x, absolutized_offset_y, absolutized_blur_radius, absolutized_spread_distance, m_properties.placement);
  1813. }
  1814. bool CalculatedStyleValue::contains_percentage() const
  1815. {
  1816. return m_expression->contains_percentage();
  1817. }
  1818. bool CalculatedStyleValue::CalcSum::contains_percentage() const
  1819. {
  1820. if (first_calc_product->contains_percentage())
  1821. return true;
  1822. for (auto& part : zero_or_more_additional_calc_products) {
  1823. if (part->contains_percentage())
  1824. return true;
  1825. }
  1826. return false;
  1827. }
  1828. bool CalculatedStyleValue::CalcSumPartWithOperator::contains_percentage() const
  1829. {
  1830. return value->contains_percentage();
  1831. }
  1832. bool CalculatedStyleValue::CalcProduct::contains_percentage() const
  1833. {
  1834. if (first_calc_value.contains_percentage())
  1835. return true;
  1836. for (auto& part : zero_or_more_additional_calc_values) {
  1837. if (part->contains_percentage())
  1838. return true;
  1839. }
  1840. return false;
  1841. }
  1842. bool CalculatedStyleValue::CalcProductPartWithOperator::contains_percentage() const
  1843. {
  1844. return value.visit(
  1845. [](CalcValue const& value) { return value.contains_percentage(); },
  1846. [](CalcNumberValue const&) { return false; });
  1847. }
  1848. bool CalculatedStyleValue::CalcValue::contains_percentage() const
  1849. {
  1850. return value.visit(
  1851. [](Percentage const&) { return true; },
  1852. [](NonnullOwnPtr<CalcSum> const& sum) { return sum->contains_percentage(); },
  1853. [](auto const&) { return false; });
  1854. }
  1855. bool calculated_style_value_contains_percentage(CalculatedStyleValue const& value)
  1856. {
  1857. return value.contains_percentage();
  1858. }
  1859. }