StyleValue.cpp 50 KB

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