StyleValue.cpp 88 KB

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