StyleValue.cpp 81 KB

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