StyleValue.cpp 70 KB

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