StyleValue.cpp 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
  4. * Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
  5. * Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #include <AK/ByteBuffer.h>
  10. #include <LibGfx/Palette.h>
  11. #include <LibWeb/CSS/Serialize.h>
  12. #include <LibWeb/CSS/StyleValue.h>
  13. #include <LibWeb/CSS/StyleValues/AbstractImageStyleValue.h>
  14. #include <LibWeb/CSS/StyleValues/AngleStyleValue.h>
  15. #include <LibWeb/CSS/StyleValues/BackgroundRepeatStyleValue.h>
  16. #include <LibWeb/CSS/StyleValues/BackgroundSizeStyleValue.h>
  17. #include <LibWeb/CSS/StyleValues/BackgroundStyleValue.h>
  18. #include <LibWeb/CSS/StyleValues/BorderRadiusShorthandStyleValue.h>
  19. #include <LibWeb/CSS/StyleValues/BorderRadiusStyleValue.h>
  20. #include <LibWeb/CSS/StyleValues/BorderStyleValue.h>
  21. #include <LibWeb/CSS/StyleValues/ColorStyleValue.h>
  22. #include <LibWeb/CSS/StyleValues/ConicGradientStyleValue.h>
  23. #include <LibWeb/CSS/StyleValues/ContentStyleValue.h>
  24. #include <LibWeb/CSS/StyleValues/FilterValueListStyleValue.h>
  25. #include <LibWeb/CSS/StyleValues/FlexFlowStyleValue.h>
  26. #include <LibWeb/CSS/StyleValues/FlexStyleValue.h>
  27. #include <LibWeb/CSS/StyleValues/FontStyleValue.h>
  28. #include <LibWeb/CSS/StyleValues/FrequencyStyleValue.h>
  29. #include <LibWeb/CSS/StyleValues/GridAreaShorthandStyleValue.h>
  30. #include <LibWeb/CSS/StyleValues/GridTemplateAreaStyleValue.h>
  31. #include <LibWeb/CSS/StyleValues/GridTrackPlacementShorthandStyleValue.h>
  32. #include <LibWeb/CSS/StyleValues/GridTrackPlacementStyleValue.h>
  33. #include <LibWeb/CSS/StyleValues/GridTrackSizeStyleValue.h>
  34. #include <LibWeb/CSS/StyleValues/IdentifierStyleValue.h>
  35. #include <LibWeb/CSS/StyleValues/ImageStyleValue.h>
  36. #include <LibWeb/CSS/StyleValues/InheritStyleValue.h>
  37. #include <LibWeb/CSS/StyleValues/InitialStyleValue.h>
  38. #include <LibWeb/CSS/StyleValues/LengthStyleValue.h>
  39. #include <LibWeb/CSS/StyleValues/LinearGradientStyleValue.h>
  40. #include <LibWeb/CSS/StyleValues/ListStyleStyleValue.h>
  41. #include <LibWeb/CSS/StyleValues/NumericStyleValue.h>
  42. #include <LibWeb/CSS/StyleValues/OverflowStyleValue.h>
  43. #include <LibWeb/CSS/StyleValues/PercentageStyleValue.h>
  44. #include <LibWeb/CSS/StyleValues/PositionStyleValue.h>
  45. #include <LibWeb/CSS/StyleValues/RadialGradientStyleValue.h>
  46. #include <LibWeb/CSS/StyleValues/ResolutionStyleValue.h>
  47. #include <LibWeb/CSS/StyleValues/ShadowStyleValue.h>
  48. #include <LibWeb/CSS/StyleValues/StringStyleValue.h>
  49. #include <LibWeb/CSS/StyleValues/TextDecorationStyleValue.h>
  50. #include <LibWeb/CSS/StyleValues/TimeStyleValue.h>
  51. #include <LibWeb/CSS/StyleValues/TransformationStyleValue.h>
  52. #include <LibWeb/CSS/StyleValues/UnresolvedStyleValue.h>
  53. #include <LibWeb/CSS/StyleValues/UnsetStyleValue.h>
  54. #include <LibWeb/DOM/Document.h>
  55. #include <LibWeb/HTML/BrowsingContext.h>
  56. #include <LibWeb/Loader/LoadRequest.h>
  57. #include <LibWeb/Loader/ResourceLoader.h>
  58. #include <LibWeb/Page/Page.h>
  59. #include <LibWeb/Painting/GradientPainting.h>
  60. #include <LibWeb/Platform/Timer.h>
  61. namespace Web::CSS {
  62. StyleValue::StyleValue(Type type)
  63. : m_type(type)
  64. {
  65. }
  66. AbstractImageStyleValue const& StyleValue::as_abstract_image() const
  67. {
  68. VERIFY(is_abstract_image());
  69. return static_cast<AbstractImageStyleValue const&>(*this);
  70. }
  71. AngleStyleValue const& StyleValue::as_angle() const
  72. {
  73. VERIFY(is_angle());
  74. return static_cast<AngleStyleValue const&>(*this);
  75. }
  76. BackgroundStyleValue const& StyleValue::as_background() const
  77. {
  78. VERIFY(is_background());
  79. return static_cast<BackgroundStyleValue const&>(*this);
  80. }
  81. BackgroundRepeatStyleValue const& StyleValue::as_background_repeat() const
  82. {
  83. VERIFY(is_background_repeat());
  84. return static_cast<BackgroundRepeatStyleValue const&>(*this);
  85. }
  86. BackgroundSizeStyleValue const& StyleValue::as_background_size() const
  87. {
  88. VERIFY(is_background_size());
  89. return static_cast<BackgroundSizeStyleValue const&>(*this);
  90. }
  91. BorderStyleValue const& StyleValue::as_border() const
  92. {
  93. VERIFY(is_border());
  94. return static_cast<BorderStyleValue const&>(*this);
  95. }
  96. BorderRadiusStyleValue const& StyleValue::as_border_radius() const
  97. {
  98. VERIFY(is_border_radius());
  99. return static_cast<BorderRadiusStyleValue const&>(*this);
  100. }
  101. BorderRadiusShorthandStyleValue const& StyleValue::as_border_radius_shorthand() const
  102. {
  103. VERIFY(is_border_radius_shorthand());
  104. return static_cast<BorderRadiusShorthandStyleValue const&>(*this);
  105. }
  106. ShadowStyleValue const& StyleValue::as_shadow() const
  107. {
  108. VERIFY(is_shadow());
  109. return static_cast<ShadowStyleValue const&>(*this);
  110. }
  111. CalculatedStyleValue const& StyleValue::as_calculated() const
  112. {
  113. VERIFY(is_calculated());
  114. return static_cast<CalculatedStyleValue const&>(*this);
  115. }
  116. ColorStyleValue const& StyleValue::as_color() const
  117. {
  118. VERIFY(is_color());
  119. return static_cast<ColorStyleValue const&>(*this);
  120. }
  121. ConicGradientStyleValue const& StyleValue::as_conic_gradient() const
  122. {
  123. VERIFY(is_conic_gradient());
  124. return static_cast<ConicGradientStyleValue const&>(*this);
  125. }
  126. ContentStyleValue const& StyleValue::as_content() const
  127. {
  128. VERIFY(is_content());
  129. return static_cast<ContentStyleValue const&>(*this);
  130. }
  131. FilterValueListStyleValue const& StyleValue::as_filter_value_list() const
  132. {
  133. VERIFY(is_filter_value_list());
  134. return static_cast<FilterValueListStyleValue const&>(*this);
  135. }
  136. FlexStyleValue const& StyleValue::as_flex() const
  137. {
  138. VERIFY(is_flex());
  139. return static_cast<FlexStyleValue const&>(*this);
  140. }
  141. FlexFlowStyleValue const& StyleValue::as_flex_flow() const
  142. {
  143. VERIFY(is_flex_flow());
  144. return static_cast<FlexFlowStyleValue const&>(*this);
  145. }
  146. FontStyleValue const& StyleValue::as_font() const
  147. {
  148. VERIFY(is_font());
  149. return static_cast<FontStyleValue const&>(*this);
  150. }
  151. FrequencyStyleValue const& StyleValue::as_frequency() const
  152. {
  153. VERIFY(is_frequency());
  154. return static_cast<FrequencyStyleValue const&>(*this);
  155. }
  156. GridTrackPlacementShorthandStyleValue const& StyleValue::as_grid_track_placement_shorthand() const
  157. {
  158. VERIFY(is_grid_track_placement_shorthand());
  159. return static_cast<GridTrackPlacementShorthandStyleValue const&>(*this);
  160. }
  161. GridAreaShorthandStyleValue const& StyleValue::as_grid_area_shorthand() const
  162. {
  163. VERIFY(is_grid_area_shorthand());
  164. return static_cast<GridAreaShorthandStyleValue const&>(*this);
  165. }
  166. GridTemplateAreaStyleValue const& StyleValue::as_grid_template_area() const
  167. {
  168. VERIFY(is_grid_template_area());
  169. return static_cast<GridTemplateAreaStyleValue const&>(*this);
  170. }
  171. GridTrackPlacementStyleValue const& StyleValue::as_grid_track_placement() const
  172. {
  173. VERIFY(is_grid_track_placement());
  174. return static_cast<GridTrackPlacementStyleValue const&>(*this);
  175. }
  176. IdentifierStyleValue const& StyleValue::as_identifier() const
  177. {
  178. VERIFY(is_identifier());
  179. return static_cast<IdentifierStyleValue const&>(*this);
  180. }
  181. ImageStyleValue const& StyleValue::as_image() const
  182. {
  183. VERIFY(is_image());
  184. return static_cast<ImageStyleValue const&>(*this);
  185. }
  186. InheritStyleValue const& StyleValue::as_inherit() const
  187. {
  188. VERIFY(is_inherit());
  189. return static_cast<InheritStyleValue const&>(*this);
  190. }
  191. InitialStyleValue const& StyleValue::as_initial() const
  192. {
  193. VERIFY(is_initial());
  194. return static_cast<InitialStyleValue const&>(*this);
  195. }
  196. LengthStyleValue const& StyleValue::as_length() const
  197. {
  198. VERIFY(is_length());
  199. return static_cast<LengthStyleValue const&>(*this);
  200. }
  201. GridTrackSizeStyleValue const& StyleValue::as_grid_track_size_list() const
  202. {
  203. VERIFY(is_grid_track_size_list());
  204. return static_cast<GridTrackSizeStyleValue const&>(*this);
  205. }
  206. LinearGradientStyleValue const& StyleValue::as_linear_gradient() const
  207. {
  208. VERIFY(is_linear_gradient());
  209. return static_cast<LinearGradientStyleValue const&>(*this);
  210. }
  211. ListStyleStyleValue const& StyleValue::as_list_style() const
  212. {
  213. VERIFY(is_list_style());
  214. return static_cast<ListStyleStyleValue const&>(*this);
  215. }
  216. NumericStyleValue const& StyleValue::as_numeric() const
  217. {
  218. VERIFY(is_numeric());
  219. return static_cast<NumericStyleValue const&>(*this);
  220. }
  221. OverflowStyleValue const& StyleValue::as_overflow() const
  222. {
  223. VERIFY(is_overflow());
  224. return static_cast<OverflowStyleValue const&>(*this);
  225. }
  226. PercentageStyleValue const& StyleValue::as_percentage() const
  227. {
  228. VERIFY(is_percentage());
  229. return static_cast<PercentageStyleValue const&>(*this);
  230. }
  231. PositionStyleValue const& StyleValue::as_position() const
  232. {
  233. VERIFY(is_position());
  234. return static_cast<PositionStyleValue const&>(*this);
  235. }
  236. RadialGradientStyleValue const& StyleValue::as_radial_gradient() const
  237. {
  238. VERIFY(is_radial_gradient());
  239. return static_cast<RadialGradientStyleValue const&>(*this);
  240. }
  241. RectStyleValue const& StyleValue::as_rect() const
  242. {
  243. VERIFY(is_rect());
  244. return static_cast<RectStyleValue const&>(*this);
  245. }
  246. ResolutionStyleValue const& StyleValue::as_resolution() const
  247. {
  248. VERIFY(is_resolution());
  249. return static_cast<ResolutionStyleValue const&>(*this);
  250. }
  251. StringStyleValue const& StyleValue::as_string() const
  252. {
  253. VERIFY(is_string());
  254. return static_cast<StringStyleValue const&>(*this);
  255. }
  256. TextDecorationStyleValue const& StyleValue::as_text_decoration() const
  257. {
  258. VERIFY(is_text_decoration());
  259. return static_cast<TextDecorationStyleValue const&>(*this);
  260. }
  261. TimeStyleValue const& StyleValue::as_time() const
  262. {
  263. VERIFY(is_time());
  264. return static_cast<TimeStyleValue const&>(*this);
  265. }
  266. TransformationStyleValue const& StyleValue::as_transformation() const
  267. {
  268. VERIFY(is_transformation());
  269. return static_cast<TransformationStyleValue const&>(*this);
  270. }
  271. UnresolvedStyleValue const& StyleValue::as_unresolved() const
  272. {
  273. VERIFY(is_unresolved());
  274. return static_cast<UnresolvedStyleValue const&>(*this);
  275. }
  276. UnsetStyleValue const& StyleValue::as_unset() const
  277. {
  278. VERIFY(is_unset());
  279. return static_cast<UnsetStyleValue const&>(*this);
  280. }
  281. StyleValueList const& StyleValue::as_value_list() const
  282. {
  283. VERIFY(is_value_list());
  284. return static_cast<StyleValueList const&>(*this);
  285. }
  286. void CalculatedStyleValue::CalculationResult::add(CalculationResult const& other, Layout::Node const* layout_node, PercentageBasis const& percentage_basis)
  287. {
  288. add_or_subtract_internal(SumOperation::Add, other, layout_node, percentage_basis);
  289. }
  290. void CalculatedStyleValue::CalculationResult::subtract(CalculationResult const& other, Layout::Node const* layout_node, PercentageBasis const& percentage_basis)
  291. {
  292. add_or_subtract_internal(SumOperation::Subtract, other, layout_node, percentage_basis);
  293. }
  294. void CalculatedStyleValue::CalculationResult::add_or_subtract_internal(SumOperation op, CalculationResult const& other, Layout::Node const* layout_node, PercentageBasis const& percentage_basis)
  295. {
  296. // 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>".
  297. // Though, having the same type may mean that one side is a <dimension> and the other a <percentage>.
  298. // Note: This is almost identical to ::add()
  299. m_value.visit(
  300. [&](Number const& number) {
  301. auto other_number = other.m_value.get<Number>();
  302. if (op == SumOperation::Add) {
  303. m_value = number + other_number;
  304. } else {
  305. m_value = number - other_number;
  306. }
  307. },
  308. [&](Angle const& angle) {
  309. auto this_degrees = angle.to_degrees();
  310. if (other.m_value.has<Angle>()) {
  311. auto other_degrees = other.m_value.get<Angle>().to_degrees();
  312. if (op == SumOperation::Add)
  313. m_value = Angle::make_degrees(this_degrees + other_degrees);
  314. else
  315. m_value = Angle::make_degrees(this_degrees - other_degrees);
  316. } else {
  317. VERIFY(percentage_basis.has<Angle>());
  318. auto other_degrees = percentage_basis.get<Angle>().percentage_of(other.m_value.get<Percentage>()).to_degrees();
  319. if (op == SumOperation::Add)
  320. m_value = Angle::make_degrees(this_degrees + other_degrees);
  321. else
  322. m_value = Angle::make_degrees(this_degrees - other_degrees);
  323. }
  324. },
  325. [&](Frequency const& frequency) {
  326. auto this_hertz = frequency.to_hertz();
  327. if (other.m_value.has<Frequency>()) {
  328. auto other_hertz = other.m_value.get<Frequency>().to_hertz();
  329. if (op == SumOperation::Add)
  330. m_value = Frequency::make_hertz(this_hertz + other_hertz);
  331. else
  332. m_value = Frequency::make_hertz(this_hertz - other_hertz);
  333. } else {
  334. VERIFY(percentage_basis.has<Frequency>());
  335. auto other_hertz = percentage_basis.get<Frequency>().percentage_of(other.m_value.get<Percentage>()).to_hertz();
  336. if (op == SumOperation::Add)
  337. m_value = Frequency::make_hertz(this_hertz + other_hertz);
  338. else
  339. m_value = Frequency::make_hertz(this_hertz - other_hertz);
  340. }
  341. },
  342. [&](Length const& length) {
  343. auto this_px = length.to_px(*layout_node);
  344. if (other.m_value.has<Length>()) {
  345. auto other_px = other.m_value.get<Length>().to_px(*layout_node);
  346. if (op == SumOperation::Add)
  347. m_value = Length::make_px(this_px + other_px);
  348. else
  349. m_value = Length::make_px(this_px - other_px);
  350. } else {
  351. VERIFY(percentage_basis.has<Length>());
  352. auto other_px = percentage_basis.get<Length>().percentage_of(other.m_value.get<Percentage>()).to_px(*layout_node);
  353. if (op == SumOperation::Add)
  354. m_value = Length::make_px(this_px + other_px);
  355. else
  356. m_value = Length::make_px(this_px - other_px);
  357. }
  358. },
  359. [&](Time const& time) {
  360. auto this_seconds = time.to_seconds();
  361. if (other.m_value.has<Time>()) {
  362. auto other_seconds = other.m_value.get<Time>().to_seconds();
  363. if (op == SumOperation::Add)
  364. m_value = Time::make_seconds(this_seconds + other_seconds);
  365. else
  366. m_value = Time::make_seconds(this_seconds - other_seconds);
  367. } else {
  368. VERIFY(percentage_basis.has<Time>());
  369. auto other_seconds = percentage_basis.get<Time>().percentage_of(other.m_value.get<Percentage>()).to_seconds();
  370. if (op == SumOperation::Add)
  371. m_value = Time::make_seconds(this_seconds + other_seconds);
  372. else
  373. m_value = Time::make_seconds(this_seconds - other_seconds);
  374. }
  375. },
  376. [&](Percentage const& percentage) {
  377. if (other.m_value.has<Percentage>()) {
  378. if (op == SumOperation::Add)
  379. m_value = Percentage { percentage.value() + other.m_value.get<Percentage>().value() };
  380. else
  381. m_value = Percentage { percentage.value() - other.m_value.get<Percentage>().value() };
  382. return;
  383. }
  384. // 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`.
  385. CalculationResult new_value = other;
  386. if (op == SumOperation::Add) {
  387. new_value.add(*this, layout_node, percentage_basis);
  388. } else {
  389. // Turn 'this - other' into '-other + this', as 'A + B == B + A', but 'A - B != B - A'
  390. new_value.multiply_by({ Number { Number::Type::Integer, -1.0f } }, layout_node);
  391. new_value.add(*this, layout_node, percentage_basis);
  392. }
  393. *this = new_value;
  394. });
  395. }
  396. void CalculatedStyleValue::CalculationResult::multiply_by(CalculationResult const& other, Layout::Node const* layout_node)
  397. {
  398. // We know from validation when resolving the type, that at least one side must be a <number> or <integer>.
  399. // Both of these are represented as a float.
  400. VERIFY(m_value.has<Number>() || other.m_value.has<Number>());
  401. bool other_is_number = other.m_value.has<Number>();
  402. m_value.visit(
  403. [&](Number const& number) {
  404. if (other_is_number) {
  405. m_value = number * other.m_value.get<Number>();
  406. } else {
  407. // Avoid duplicating all the logic by swapping `this` and `other`.
  408. CalculationResult new_value = other;
  409. new_value.multiply_by(*this, layout_node);
  410. *this = new_value;
  411. }
  412. },
  413. [&](Angle const& angle) {
  414. m_value = Angle::make_degrees(angle.to_degrees() * other.m_value.get<Number>().value());
  415. },
  416. [&](Frequency const& frequency) {
  417. m_value = Frequency::make_hertz(frequency.to_hertz() * other.m_value.get<Number>().value());
  418. },
  419. [&](Length const& length) {
  420. VERIFY(layout_node);
  421. m_value = Length::make_px(length.to_px(*layout_node) * other.m_value.get<Number>().value());
  422. },
  423. [&](Time const& time) {
  424. m_value = Time::make_seconds(time.to_seconds() * other.m_value.get<Number>().value());
  425. },
  426. [&](Percentage const& percentage) {
  427. m_value = Percentage { percentage.value() * other.m_value.get<Number>().value() };
  428. });
  429. }
  430. void CalculatedStyleValue::CalculationResult::divide_by(CalculationResult const& other, Layout::Node const* layout_node)
  431. {
  432. // We know from validation when resolving the type, that `other` must be a <number> or <integer>.
  433. // Both of these are represented as a Number.
  434. auto denominator = other.m_value.get<Number>().value();
  435. // FIXME: Dividing by 0 is invalid, and should be caught during parsing.
  436. VERIFY(denominator != 0.0f);
  437. m_value.visit(
  438. [&](Number const& number) {
  439. m_value = Number {
  440. Number::Type::Number,
  441. number.value() / denominator
  442. };
  443. },
  444. [&](Angle const& angle) {
  445. m_value = Angle::make_degrees(angle.to_degrees() / denominator);
  446. },
  447. [&](Frequency const& frequency) {
  448. m_value = Frequency::make_hertz(frequency.to_hertz() / denominator);
  449. },
  450. [&](Length const& length) {
  451. VERIFY(layout_node);
  452. m_value = Length::make_px(length.to_px(*layout_node) / denominator);
  453. },
  454. [&](Time const& time) {
  455. m_value = Time::make_seconds(time.to_seconds() / denominator);
  456. },
  457. [&](Percentage const& percentage) {
  458. m_value = Percentage { percentage.value() / denominator };
  459. });
  460. }
  461. ErrorOr<String> CalculatedStyleValue::to_string() const
  462. {
  463. return String::formatted("calc({})", TRY(m_expression->to_string()));
  464. }
  465. bool CalculatedStyleValue::equals(StyleValue const& other) const
  466. {
  467. if (type() != other.type())
  468. return false;
  469. // This is a case where comparing the strings actually makes sense.
  470. return to_string().release_value_but_fixme_should_propagate_errors() == other.to_string().release_value_but_fixme_should_propagate_errors();
  471. }
  472. ErrorOr<String> CalculatedStyleValue::CalcNumberValue::to_string() const
  473. {
  474. return value.visit(
  475. [](Number const& number) -> ErrorOr<String> { return String::number(number.value()); },
  476. [](NonnullOwnPtr<CalcNumberSum> const& sum) -> ErrorOr<String> { return String::formatted("({})", TRY(sum->to_string())); });
  477. }
  478. ErrorOr<String> CalculatedStyleValue::CalcValue::to_string() const
  479. {
  480. return value.visit(
  481. [](Number const& number) -> ErrorOr<String> { return String::number(number.value()); },
  482. [](NonnullOwnPtr<CalcSum> const& sum) -> ErrorOr<String> { return String::formatted("({})", TRY(sum->to_string())); },
  483. [](auto const& v) -> ErrorOr<String> { return v.to_string(); });
  484. }
  485. ErrorOr<String> CalculatedStyleValue::CalcSum::to_string() const
  486. {
  487. StringBuilder builder;
  488. TRY(builder.try_append(TRY(first_calc_product->to_string())));
  489. for (auto const& item : zero_or_more_additional_calc_products)
  490. TRY(builder.try_append(TRY(item->to_string())));
  491. return builder.to_string();
  492. }
  493. ErrorOr<String> CalculatedStyleValue::CalcNumberSum::to_string() const
  494. {
  495. StringBuilder builder;
  496. TRY(builder.try_append(TRY(first_calc_number_product->to_string())));
  497. for (auto const& item : zero_or_more_additional_calc_number_products)
  498. TRY(builder.try_append(TRY(item->to_string())));
  499. return builder.to_string();
  500. }
  501. ErrorOr<String> CalculatedStyleValue::CalcProduct::to_string() const
  502. {
  503. StringBuilder builder;
  504. TRY(builder.try_append(TRY(first_calc_value.to_string())));
  505. for (auto const& item : zero_or_more_additional_calc_values)
  506. TRY(builder.try_append(TRY(item->to_string())));
  507. return builder.to_string();
  508. }
  509. ErrorOr<String> CalculatedStyleValue::CalcSumPartWithOperator::to_string() const
  510. {
  511. return String::formatted(" {} {}", op == SumOperation::Add ? "+"sv : "-"sv, TRY(value->to_string()));
  512. }
  513. ErrorOr<String> CalculatedStyleValue::CalcProductPartWithOperator::to_string() const
  514. {
  515. auto value_string = TRY(value.visit(
  516. [](CalcValue const& v) { return v.to_string(); },
  517. [](CalcNumberValue const& v) { return v.to_string(); }));
  518. return String::formatted(" {} {}", op == ProductOperation::Multiply ? "*"sv : "/"sv, value_string);
  519. }
  520. ErrorOr<String> CalculatedStyleValue::CalcNumberProduct::to_string() const
  521. {
  522. StringBuilder builder;
  523. TRY(builder.try_append(TRY(first_calc_number_value.to_string())));
  524. for (auto const& item : zero_or_more_additional_calc_number_values)
  525. TRY(builder.try_append(TRY(item->to_string())));
  526. return builder.to_string();
  527. }
  528. ErrorOr<String> CalculatedStyleValue::CalcNumberProductPartWithOperator::to_string() const
  529. {
  530. return String::formatted(" {} {}", op == ProductOperation::Multiply ? "*"sv : "/"sv, TRY(value.to_string()));
  531. }
  532. ErrorOr<String> CalculatedStyleValue::CalcNumberSumPartWithOperator::to_string() const
  533. {
  534. return String::formatted(" {} {}", op == SumOperation::Add ? "+"sv : "-"sv, TRY(value->to_string()));
  535. }
  536. Optional<Angle> CalculatedStyleValue::resolve_angle() const
  537. {
  538. auto result = m_expression->resolve(nullptr, {});
  539. if (result.value().has<Angle>())
  540. return result.value().get<Angle>();
  541. return {};
  542. }
  543. Optional<Angle> CalculatedStyleValue::resolve_angle_percentage(Angle const& percentage_basis) const
  544. {
  545. auto result = m_expression->resolve(nullptr, percentage_basis);
  546. return result.value().visit(
  547. [&](Angle const& angle) -> Optional<Angle> {
  548. return angle;
  549. },
  550. [&](Percentage const& percentage) -> Optional<Angle> {
  551. return percentage_basis.percentage_of(percentage);
  552. },
  553. [&](auto const&) -> Optional<Angle> {
  554. return {};
  555. });
  556. }
  557. Optional<Frequency> CalculatedStyleValue::resolve_frequency() const
  558. {
  559. auto result = m_expression->resolve(nullptr, {});
  560. if (result.value().has<Frequency>())
  561. return result.value().get<Frequency>();
  562. return {};
  563. }
  564. Optional<Frequency> CalculatedStyleValue::resolve_frequency_percentage(Frequency const& percentage_basis) const
  565. {
  566. auto result = m_expression->resolve(nullptr, percentage_basis);
  567. return result.value().visit(
  568. [&](Frequency const& frequency) -> Optional<Frequency> {
  569. return frequency;
  570. },
  571. [&](Percentage const& percentage) -> Optional<Frequency> {
  572. return percentage_basis.percentage_of(percentage);
  573. },
  574. [&](auto const&) -> Optional<Frequency> {
  575. return {};
  576. });
  577. }
  578. Optional<Length> CalculatedStyleValue::resolve_length(Layout::Node const& layout_node) const
  579. {
  580. auto result = m_expression->resolve(&layout_node, {});
  581. if (result.value().has<Length>())
  582. return result.value().get<Length>();
  583. return {};
  584. }
  585. Optional<Length> CalculatedStyleValue::resolve_length_percentage(Layout::Node const& layout_node, Length const& percentage_basis) const
  586. {
  587. auto result = m_expression->resolve(&layout_node, percentage_basis);
  588. return result.value().visit(
  589. [&](Length const& length) -> Optional<Length> {
  590. return length;
  591. },
  592. [&](Percentage const& percentage) -> Optional<Length> {
  593. return percentage_basis.percentage_of(percentage);
  594. },
  595. [&](auto const&) -> Optional<Length> {
  596. return {};
  597. });
  598. }
  599. Optional<Percentage> CalculatedStyleValue::resolve_percentage() const
  600. {
  601. auto result = m_expression->resolve(nullptr, {});
  602. if (result.value().has<Percentage>())
  603. return result.value().get<Percentage>();
  604. return {};
  605. }
  606. Optional<Time> CalculatedStyleValue::resolve_time() const
  607. {
  608. auto result = m_expression->resolve(nullptr, {});
  609. if (result.value().has<Time>())
  610. return result.value().get<Time>();
  611. return {};
  612. }
  613. Optional<Time> CalculatedStyleValue::resolve_time_percentage(Time const& percentage_basis) const
  614. {
  615. auto result = m_expression->resolve(nullptr, percentage_basis);
  616. return result.value().visit(
  617. [&](Time const& time) -> Optional<Time> {
  618. return time;
  619. },
  620. [&](auto const&) -> Optional<Time> {
  621. return {};
  622. });
  623. }
  624. Optional<float> CalculatedStyleValue::resolve_number()
  625. {
  626. auto result = m_expression->resolve(nullptr, {});
  627. if (result.value().has<Number>())
  628. return result.value().get<Number>().value();
  629. return {};
  630. }
  631. Optional<i64> CalculatedStyleValue::resolve_integer()
  632. {
  633. auto result = m_expression->resolve(nullptr, {});
  634. if (result.value().has<Number>())
  635. return result.value().get<Number>().integer_value();
  636. return {};
  637. }
  638. static bool is_number(CalculatedStyleValue::ResolvedType type)
  639. {
  640. return type == CalculatedStyleValue::ResolvedType::Number || type == CalculatedStyleValue::ResolvedType::Integer;
  641. }
  642. static bool is_dimension(CalculatedStyleValue::ResolvedType type)
  643. {
  644. return type != CalculatedStyleValue::ResolvedType::Number
  645. && type != CalculatedStyleValue::ResolvedType::Integer
  646. && type != CalculatedStyleValue::ResolvedType::Percentage;
  647. }
  648. template<typename SumWithOperator>
  649. static Optional<CalculatedStyleValue::ResolvedType> resolve_sum_type(CalculatedStyleValue::ResolvedType first_type, Vector<NonnullOwnPtr<SumWithOperator>> const& zero_or_more_additional_products)
  650. {
  651. auto type = first_type;
  652. for (auto const& product : zero_or_more_additional_products) {
  653. auto maybe_product_type = product->resolved_type();
  654. if (!maybe_product_type.has_value())
  655. return {};
  656. auto product_type = maybe_product_type.value();
  657. // At + or -, check that both sides have the same type, or that one side is a <number> and the other is an <integer>.
  658. // If both sides are the same type, resolve to that type.
  659. if (product_type == type)
  660. continue;
  661. // If one side is a <number> and the other is an <integer>, resolve to <number>.
  662. if (is_number(type) && is_number(product_type)) {
  663. type = CalculatedStyleValue::ResolvedType::Number;
  664. continue;
  665. }
  666. // FIXME: calc() handles <percentage> by allowing them to pretend to be whatever <dimension> type is allowed at this location.
  667. // Since we can't easily check what that type is, we just allow <percentage> to combine with any other <dimension> type.
  668. if (type == CalculatedStyleValue::ResolvedType::Percentage && is_dimension(product_type)) {
  669. type = product_type;
  670. continue;
  671. }
  672. if (is_dimension(type) && product_type == CalculatedStyleValue::ResolvedType::Percentage)
  673. continue;
  674. return {};
  675. }
  676. return type;
  677. }
  678. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcSum::resolved_type() const
  679. {
  680. auto maybe_type = first_calc_product->resolved_type();
  681. if (!maybe_type.has_value())
  682. return {};
  683. auto type = maybe_type.value();
  684. return resolve_sum_type(type, zero_or_more_additional_calc_products);
  685. }
  686. // https://www.w3.org/TR/CSS2/visufx.html#value-def-shape
  687. Gfx::FloatRect EdgeRect::resolved(Layout::Node const& layout_node, Gfx::FloatRect border_box) const
  688. {
  689. // In CSS 2.1, the only valid <shape> value is: rect(<top>, <right>, <bottom>, <left>) where
  690. // <top> and <bottom> specify offsets from the top border edge of the box, and <right>, and
  691. // <left> specify offsets from the left border edge of the box.
  692. // The value 'auto' means that a given edge of the clipping region will be the same as the edge
  693. // of the element's generated border box (i.e., 'auto' means the same as '0' for <top> and
  694. // <left>, the same as the used value of the height plus the sum of vertical padding and border
  695. // widths for <bottom>, and the same as the used value of the width plus the sum of the
  696. // horizontal padding and border widths for <right>, such that four 'auto' values result in the
  697. // clipping region being the same as the element's border box).
  698. auto left = border_box.left() + (left_edge.is_auto() ? 0 : left_edge.to_px(layout_node)).value();
  699. auto top = border_box.top() + (top_edge.is_auto() ? 0 : top_edge.to_px(layout_node)).value();
  700. auto right = border_box.left() + (right_edge.is_auto() ? border_box.width() : right_edge.to_px(layout_node)).value();
  701. auto bottom = border_box.top() + (bottom_edge.is_auto() ? border_box.height() : bottom_edge.to_px(layout_node)).value();
  702. return Gfx::FloatRect {
  703. left,
  704. top,
  705. right - left,
  706. bottom - top
  707. };
  708. }
  709. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcNumberSum::resolved_type() const
  710. {
  711. auto maybe_type = first_calc_number_product->resolved_type();
  712. if (!maybe_type.has_value())
  713. return {};
  714. auto type = maybe_type.value();
  715. return resolve_sum_type(type, zero_or_more_additional_calc_number_products);
  716. }
  717. template<typename ProductWithOperator>
  718. static Optional<CalculatedStyleValue::ResolvedType> resolve_product_type(CalculatedStyleValue::ResolvedType first_type, Vector<NonnullOwnPtr<ProductWithOperator>> const& zero_or_more_additional_values)
  719. {
  720. auto type = first_type;
  721. for (auto const& value : zero_or_more_additional_values) {
  722. auto maybe_value_type = value->resolved_type();
  723. if (!maybe_value_type.has_value())
  724. return {};
  725. auto value_type = maybe_value_type.value();
  726. if (value->op == CalculatedStyleValue::ProductOperation::Multiply) {
  727. // At *, check that at least one side is <number>.
  728. if (!(is_number(type) || is_number(value_type)))
  729. return {};
  730. // If both sides are <integer>, resolve to <integer>.
  731. if (type == CalculatedStyleValue::ResolvedType::Integer && value_type == CalculatedStyleValue::ResolvedType::Integer) {
  732. type = CalculatedStyleValue::ResolvedType::Integer;
  733. } else {
  734. // Otherwise, resolve to the type of the other side.
  735. if (is_number(type))
  736. type = value_type;
  737. }
  738. continue;
  739. } else {
  740. VERIFY(value->op == CalculatedStyleValue::ProductOperation::Divide);
  741. // At /, check that the right side is <number>.
  742. if (!is_number(value_type))
  743. return {};
  744. // If the left side is <integer>, resolve to <number>.
  745. if (type == CalculatedStyleValue::ResolvedType::Integer) {
  746. type = CalculatedStyleValue::ResolvedType::Number;
  747. } else {
  748. // Otherwise, resolve to the type of the left side.
  749. }
  750. // FIXME: Division by zero makes the whole calc() expression invalid.
  751. }
  752. }
  753. return type;
  754. }
  755. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcProduct::resolved_type() const
  756. {
  757. auto maybe_type = first_calc_value.resolved_type();
  758. if (!maybe_type.has_value())
  759. return {};
  760. auto type = maybe_type.value();
  761. return resolve_product_type(type, zero_or_more_additional_calc_values);
  762. }
  763. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcSumPartWithOperator::resolved_type() const
  764. {
  765. return value->resolved_type();
  766. }
  767. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcNumberProduct::resolved_type() const
  768. {
  769. auto maybe_type = first_calc_number_value.resolved_type();
  770. if (!maybe_type.has_value())
  771. return {};
  772. auto type = maybe_type.value();
  773. return resolve_product_type(type, zero_or_more_additional_calc_number_values);
  774. }
  775. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcNumberProductPartWithOperator::resolved_type() const
  776. {
  777. return value.resolved_type();
  778. }
  779. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcNumberSumPartWithOperator::resolved_type() const
  780. {
  781. return value->resolved_type();
  782. }
  783. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcProductPartWithOperator::resolved_type() const
  784. {
  785. return value.visit(
  786. [](CalcValue const& calc_value) {
  787. return calc_value.resolved_type();
  788. },
  789. [](CalcNumberValue const& calc_number_value) {
  790. return calc_number_value.resolved_type();
  791. });
  792. }
  793. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcValue::resolved_type() const
  794. {
  795. return value.visit(
  796. [](Number const& number) -> Optional<CalculatedStyleValue::ResolvedType> {
  797. return { number.is_integer() ? ResolvedType::Integer : ResolvedType::Number };
  798. },
  799. [](Angle const&) -> Optional<CalculatedStyleValue::ResolvedType> { return { ResolvedType::Angle }; },
  800. [](Frequency const&) -> Optional<CalculatedStyleValue::ResolvedType> { return { ResolvedType::Frequency }; },
  801. [](Length const&) -> Optional<CalculatedStyleValue::ResolvedType> { return { ResolvedType::Length }; },
  802. [](Percentage const&) -> Optional<CalculatedStyleValue::ResolvedType> { return { ResolvedType::Percentage }; },
  803. [](Time const&) -> Optional<CalculatedStyleValue::ResolvedType> { return { ResolvedType::Time }; },
  804. [](NonnullOwnPtr<CalcSum> const& sum) { return sum->resolved_type(); });
  805. }
  806. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcNumberValue::resolved_type() const
  807. {
  808. return value.visit(
  809. [](Number const& number) -> Optional<CalculatedStyleValue::ResolvedType> {
  810. return { number.is_integer() ? ResolvedType::Integer : ResolvedType::Number };
  811. },
  812. [](NonnullOwnPtr<CalcNumberSum> const& sum) { return sum->resolved_type(); });
  813. }
  814. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcNumberValue::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  815. {
  816. return value.visit(
  817. [&](Number const& number) -> CalculatedStyleValue::CalculationResult {
  818. return CalculatedStyleValue::CalculationResult { number };
  819. },
  820. [&](NonnullOwnPtr<CalcNumberSum> const& sum) -> CalculatedStyleValue::CalculationResult {
  821. return sum->resolve(layout_node, percentage_basis);
  822. });
  823. }
  824. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcValue::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  825. {
  826. return value.visit(
  827. [&](NonnullOwnPtr<CalcSum> const& sum) -> CalculatedStyleValue::CalculationResult {
  828. return sum->resolve(layout_node, percentage_basis);
  829. },
  830. [&](auto const& v) -> CalculatedStyleValue::CalculationResult {
  831. return CalculatedStyleValue::CalculationResult { v };
  832. });
  833. }
  834. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcSum::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  835. {
  836. auto value = first_calc_product->resolve(layout_node, percentage_basis);
  837. for (auto& additional_product : zero_or_more_additional_calc_products) {
  838. auto additional_value = additional_product->resolve(layout_node, percentage_basis);
  839. if (additional_product->op == CalculatedStyleValue::SumOperation::Add)
  840. value.add(additional_value, layout_node, percentage_basis);
  841. else if (additional_product->op == CalculatedStyleValue::SumOperation::Subtract)
  842. value.subtract(additional_value, layout_node, percentage_basis);
  843. else
  844. VERIFY_NOT_REACHED();
  845. }
  846. return value;
  847. }
  848. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcNumberSum::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  849. {
  850. auto value = first_calc_number_product->resolve(layout_node, percentage_basis);
  851. for (auto& additional_product : zero_or_more_additional_calc_number_products) {
  852. auto additional_value = additional_product->resolve(layout_node, percentage_basis);
  853. if (additional_product->op == CSS::CalculatedStyleValue::SumOperation::Add)
  854. value.add(additional_value, layout_node, percentage_basis);
  855. else if (additional_product->op == CalculatedStyleValue::SumOperation::Subtract)
  856. value.subtract(additional_value, layout_node, percentage_basis);
  857. else
  858. VERIFY_NOT_REACHED();
  859. }
  860. return value;
  861. }
  862. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcProduct::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  863. {
  864. auto value = first_calc_value.resolve(layout_node, percentage_basis);
  865. for (auto& additional_value : zero_or_more_additional_calc_values) {
  866. additional_value->value.visit(
  867. [&](CalculatedStyleValue::CalcValue const& calc_value) {
  868. VERIFY(additional_value->op == CalculatedStyleValue::ProductOperation::Multiply);
  869. auto resolved_value = calc_value.resolve(layout_node, percentage_basis);
  870. value.multiply_by(resolved_value, layout_node);
  871. },
  872. [&](CalculatedStyleValue::CalcNumberValue const& calc_number_value) {
  873. VERIFY(additional_value->op == CalculatedStyleValue::ProductOperation::Divide);
  874. auto resolved_calc_number_value = calc_number_value.resolve(layout_node, percentage_basis);
  875. // FIXME: Checking for division by 0 should happen during parsing.
  876. VERIFY(resolved_calc_number_value.value().get<Number>().value() != 0.0f);
  877. value.divide_by(resolved_calc_number_value, layout_node);
  878. });
  879. }
  880. return value;
  881. }
  882. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcNumberProduct::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  883. {
  884. auto value = first_calc_number_value.resolve(layout_node, percentage_basis);
  885. for (auto& additional_number_value : zero_or_more_additional_calc_number_values) {
  886. auto additional_value = additional_number_value->resolve(layout_node, percentage_basis);
  887. if (additional_number_value->op == CalculatedStyleValue::ProductOperation::Multiply)
  888. value.multiply_by(additional_value, layout_node);
  889. else if (additional_number_value->op == CalculatedStyleValue::ProductOperation::Divide)
  890. value.divide_by(additional_value, layout_node);
  891. else
  892. VERIFY_NOT_REACHED();
  893. }
  894. return value;
  895. }
  896. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcProductPartWithOperator::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  897. {
  898. return value.visit(
  899. [&](CalcValue const& calc_value) {
  900. return calc_value.resolve(layout_node, percentage_basis);
  901. },
  902. [&](CalcNumberValue const& calc_number_value) {
  903. return calc_number_value.resolve(layout_node, percentage_basis);
  904. });
  905. }
  906. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcSumPartWithOperator::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  907. {
  908. return value->resolve(layout_node, percentage_basis);
  909. }
  910. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcNumberProductPartWithOperator::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  911. {
  912. return value.resolve(layout_node, percentage_basis);
  913. }
  914. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcNumberSumPartWithOperator::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  915. {
  916. return value->resolve(layout_node, percentage_basis);
  917. }
  918. CSSPixelPoint PositionValue::resolved(Layout::Node const& node, CSSPixelRect const& rect) const
  919. {
  920. // Note: A preset + a none default x/y_relative_to is impossible in the syntax (and makes little sense)
  921. CSSPixels x = horizontal_position.visit(
  922. [&](HorizontalPreset preset) -> CSSPixels {
  923. return rect.width() * [&] {
  924. switch (preset) {
  925. case HorizontalPreset::Left:
  926. return 0.0f;
  927. case HorizontalPreset::Center:
  928. return 0.5f;
  929. case HorizontalPreset::Right:
  930. return 1.0f;
  931. default:
  932. VERIFY_NOT_REACHED();
  933. }
  934. }();
  935. },
  936. [&](LengthPercentage length_percentage) -> CSSPixels {
  937. return length_percentage.resolved(node, Length::make_px(rect.width())).to_px(node);
  938. });
  939. CSSPixels y = vertical_position.visit(
  940. [&](VerticalPreset preset) -> CSSPixels {
  941. return rect.height() * [&] {
  942. switch (preset) {
  943. case VerticalPreset::Top:
  944. return 0.0f;
  945. case VerticalPreset::Center:
  946. return 0.5f;
  947. case VerticalPreset::Bottom:
  948. return 1.0f;
  949. default:
  950. VERIFY_NOT_REACHED();
  951. }
  952. }();
  953. },
  954. [&](LengthPercentage length_percentage) -> CSSPixels {
  955. return length_percentage.resolved(node, Length::make_px(rect.height())).to_px(node);
  956. });
  957. if (x_relative_to == HorizontalEdge::Right)
  958. x = rect.width() - x;
  959. if (y_relative_to == VerticalEdge::Bottom)
  960. y = rect.height() - y;
  961. return CSSPixelPoint { rect.x() + x, rect.y() + y };
  962. }
  963. ErrorOr<void> PositionValue::serialize(StringBuilder& builder) const
  964. {
  965. // Note: This means our serialization with simplify any with explicit edges that are just `top left`.
  966. bool has_relative_edges = x_relative_to == HorizontalEdge::Right || y_relative_to == VerticalEdge::Bottom;
  967. if (has_relative_edges)
  968. TRY(builder.try_append(x_relative_to == HorizontalEdge::Left ? "left "sv : "right "sv));
  969. TRY(horizontal_position.visit(
  970. [&](HorizontalPreset preset) -> ErrorOr<void> {
  971. return builder.try_append([&] {
  972. switch (preset) {
  973. case HorizontalPreset::Left:
  974. return "left"sv;
  975. case HorizontalPreset::Center:
  976. return "center"sv;
  977. case HorizontalPreset::Right:
  978. return "right"sv;
  979. default:
  980. VERIFY_NOT_REACHED();
  981. }
  982. }());
  983. },
  984. [&](LengthPercentage length_percentage) -> ErrorOr<void> {
  985. return builder.try_appendff(TRY(length_percentage.to_string()));
  986. }));
  987. TRY(builder.try_append(' '));
  988. if (has_relative_edges)
  989. TRY(builder.try_append(y_relative_to == VerticalEdge::Top ? "top "sv : "bottom "sv));
  990. TRY(vertical_position.visit(
  991. [&](VerticalPreset preset) -> ErrorOr<void> {
  992. return builder.try_append([&] {
  993. switch (preset) {
  994. case VerticalPreset::Top:
  995. return "top"sv;
  996. case VerticalPreset::Center:
  997. return "center"sv;
  998. case VerticalPreset::Bottom:
  999. return "bottom"sv;
  1000. default:
  1001. VERIFY_NOT_REACHED();
  1002. }
  1003. }());
  1004. },
  1005. [&](LengthPercentage length_percentage) -> ErrorOr<void> {
  1006. return builder.try_append(TRY(length_percentage.to_string()));
  1007. }));
  1008. return {};
  1009. }
  1010. ErrorOr<String> RectStyleValue::to_string() const
  1011. {
  1012. return String::formatted("rect({} {} {} {})", m_rect.top_edge, m_rect.right_edge, m_rect.bottom_edge, m_rect.left_edge);
  1013. }
  1014. bool StyleValueList::Properties::operator==(Properties const& other) const
  1015. {
  1016. return separator == other.separator && values.span() == other.values.span();
  1017. }
  1018. ErrorOr<String> StyleValueList::to_string() const
  1019. {
  1020. auto separator = ""sv;
  1021. switch (m_properties.separator) {
  1022. case Separator::Space:
  1023. separator = " "sv;
  1024. break;
  1025. case Separator::Comma:
  1026. separator = ", "sv;
  1027. break;
  1028. default:
  1029. VERIFY_NOT_REACHED();
  1030. }
  1031. StringBuilder builder;
  1032. for (size_t i = 0; i < m_properties.values.size(); ++i) {
  1033. TRY(builder.try_append(TRY(m_properties.values[i]->to_string())));
  1034. if (i != m_properties.values.size() - 1)
  1035. TRY(builder.try_append(separator));
  1036. }
  1037. return builder.to_string();
  1038. }
  1039. ValueComparingNonnullRefPtr<RectStyleValue> RectStyleValue::create(EdgeRect rect)
  1040. {
  1041. return adopt_ref(*new RectStyleValue(rect));
  1042. }
  1043. Optional<CSS::Length> absolutized_length(CSS::Length const& length, CSSPixelRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, CSSPixels font_size, CSSPixels root_font_size, CSSPixels line_height, CSSPixels root_line_height)
  1044. {
  1045. if (length.is_px())
  1046. return {};
  1047. if (length.is_absolute() || length.is_relative()) {
  1048. auto px = length.to_px(viewport_rect, font_metrics, font_size, root_font_size, line_height, root_line_height);
  1049. return CSS::Length::make_px(px);
  1050. }
  1051. return {};
  1052. }
  1053. ValueComparingNonnullRefPtr<StyleValue const> StyleValue::absolutized(CSSPixelRect const&, Gfx::FontPixelMetrics const&, CSSPixels, CSSPixels, CSSPixels, CSSPixels) const
  1054. {
  1055. return *this;
  1056. }
  1057. bool CalculatedStyleValue::contains_percentage() const
  1058. {
  1059. return m_expression->contains_percentage();
  1060. }
  1061. bool CalculatedStyleValue::CalcSum::contains_percentage() const
  1062. {
  1063. if (first_calc_product->contains_percentage())
  1064. return true;
  1065. for (auto& part : zero_or_more_additional_calc_products) {
  1066. if (part->contains_percentage())
  1067. return true;
  1068. }
  1069. return false;
  1070. }
  1071. bool CalculatedStyleValue::CalcSumPartWithOperator::contains_percentage() const
  1072. {
  1073. return value->contains_percentage();
  1074. }
  1075. bool CalculatedStyleValue::CalcProduct::contains_percentage() const
  1076. {
  1077. if (first_calc_value.contains_percentage())
  1078. return true;
  1079. for (auto& part : zero_or_more_additional_calc_values) {
  1080. if (part->contains_percentage())
  1081. return true;
  1082. }
  1083. return false;
  1084. }
  1085. bool CalculatedStyleValue::CalcProductPartWithOperator::contains_percentage() const
  1086. {
  1087. return value.visit(
  1088. [](CalcValue const& value) { return value.contains_percentage(); },
  1089. [](CalcNumberValue const&) { return false; });
  1090. }
  1091. bool CalculatedStyleValue::CalcValue::contains_percentage() const
  1092. {
  1093. return value.visit(
  1094. [](Percentage const&) { return true; },
  1095. [](NonnullOwnPtr<CalcSum> const& sum) { return sum->contains_percentage(); },
  1096. [](auto const&) { return false; });
  1097. }
  1098. bool calculated_style_value_contains_percentage(CalculatedStyleValue const& value)
  1099. {
  1100. return value.contains_percentage();
  1101. }
  1102. }