StyleValue.cpp 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  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 <LibWeb/CSS/StyleValue.h>
  10. #include <LibWeb/CSS/StyleValues/AbstractImageStyleValue.h>
  11. #include <LibWeb/CSS/StyleValues/AngleStyleValue.h>
  12. #include <LibWeb/CSS/StyleValues/BackgroundRepeatStyleValue.h>
  13. #include <LibWeb/CSS/StyleValues/BackgroundSizeStyleValue.h>
  14. #include <LibWeb/CSS/StyleValues/BackgroundStyleValue.h>
  15. #include <LibWeb/CSS/StyleValues/BorderRadiusShorthandStyleValue.h>
  16. #include <LibWeb/CSS/StyleValues/BorderRadiusStyleValue.h>
  17. #include <LibWeb/CSS/StyleValues/BorderStyleValue.h>
  18. #include <LibWeb/CSS/StyleValues/ColorStyleValue.h>
  19. #include <LibWeb/CSS/StyleValues/ConicGradientStyleValue.h>
  20. #include <LibWeb/CSS/StyleValues/ContentStyleValue.h>
  21. #include <LibWeb/CSS/StyleValues/FilterValueListStyleValue.h>
  22. #include <LibWeb/CSS/StyleValues/FlexFlowStyleValue.h>
  23. #include <LibWeb/CSS/StyleValues/FlexStyleValue.h>
  24. #include <LibWeb/CSS/StyleValues/FontStyleValue.h>
  25. #include <LibWeb/CSS/StyleValues/FrequencyStyleValue.h>
  26. #include <LibWeb/CSS/StyleValues/GridAreaShorthandStyleValue.h>
  27. #include <LibWeb/CSS/StyleValues/GridTemplateAreaStyleValue.h>
  28. #include <LibWeb/CSS/StyleValues/GridTrackPlacementShorthandStyleValue.h>
  29. #include <LibWeb/CSS/StyleValues/GridTrackPlacementStyleValue.h>
  30. #include <LibWeb/CSS/StyleValues/GridTrackSizeStyleValue.h>
  31. #include <LibWeb/CSS/StyleValues/IdentifierStyleValue.h>
  32. #include <LibWeb/CSS/StyleValues/ImageStyleValue.h>
  33. #include <LibWeb/CSS/StyleValues/InheritStyleValue.h>
  34. #include <LibWeb/CSS/StyleValues/InitialStyleValue.h>
  35. #include <LibWeb/CSS/StyleValues/LengthStyleValue.h>
  36. #include <LibWeb/CSS/StyleValues/LinearGradientStyleValue.h>
  37. #include <LibWeb/CSS/StyleValues/ListStyleStyleValue.h>
  38. #include <LibWeb/CSS/StyleValues/NumericStyleValue.h>
  39. #include <LibWeb/CSS/StyleValues/OverflowStyleValue.h>
  40. #include <LibWeb/CSS/StyleValues/PercentageStyleValue.h>
  41. #include <LibWeb/CSS/StyleValues/PositionStyleValue.h>
  42. #include <LibWeb/CSS/StyleValues/RadialGradientStyleValue.h>
  43. #include <LibWeb/CSS/StyleValues/RectStyleValue.h>
  44. #include <LibWeb/CSS/StyleValues/ResolutionStyleValue.h>
  45. #include <LibWeb/CSS/StyleValues/ShadowStyleValue.h>
  46. #include <LibWeb/CSS/StyleValues/StringStyleValue.h>
  47. #include <LibWeb/CSS/StyleValues/StyleValueList.h>
  48. #include <LibWeb/CSS/StyleValues/TextDecorationStyleValue.h>
  49. #include <LibWeb/CSS/StyleValues/TimeStyleValue.h>
  50. #include <LibWeb/CSS/StyleValues/TransformationStyleValue.h>
  51. #include <LibWeb/CSS/StyleValues/UnresolvedStyleValue.h>
  52. #include <LibWeb/CSS/StyleValues/UnsetStyleValue.h>
  53. namespace Web::CSS {
  54. StyleValue::StyleValue(Type type)
  55. : m_type(type)
  56. {
  57. }
  58. AbstractImageStyleValue const& StyleValue::as_abstract_image() const
  59. {
  60. VERIFY(is_abstract_image());
  61. return static_cast<AbstractImageStyleValue const&>(*this);
  62. }
  63. AngleStyleValue const& StyleValue::as_angle() const
  64. {
  65. VERIFY(is_angle());
  66. return static_cast<AngleStyleValue const&>(*this);
  67. }
  68. BackgroundStyleValue const& StyleValue::as_background() const
  69. {
  70. VERIFY(is_background());
  71. return static_cast<BackgroundStyleValue const&>(*this);
  72. }
  73. BackgroundRepeatStyleValue const& StyleValue::as_background_repeat() const
  74. {
  75. VERIFY(is_background_repeat());
  76. return static_cast<BackgroundRepeatStyleValue const&>(*this);
  77. }
  78. BackgroundSizeStyleValue const& StyleValue::as_background_size() const
  79. {
  80. VERIFY(is_background_size());
  81. return static_cast<BackgroundSizeStyleValue const&>(*this);
  82. }
  83. BorderStyleValue const& StyleValue::as_border() const
  84. {
  85. VERIFY(is_border());
  86. return static_cast<BorderStyleValue const&>(*this);
  87. }
  88. BorderRadiusStyleValue const& StyleValue::as_border_radius() const
  89. {
  90. VERIFY(is_border_radius());
  91. return static_cast<BorderRadiusStyleValue const&>(*this);
  92. }
  93. BorderRadiusShorthandStyleValue const& StyleValue::as_border_radius_shorthand() const
  94. {
  95. VERIFY(is_border_radius_shorthand());
  96. return static_cast<BorderRadiusShorthandStyleValue const&>(*this);
  97. }
  98. ShadowStyleValue const& StyleValue::as_shadow() const
  99. {
  100. VERIFY(is_shadow());
  101. return static_cast<ShadowStyleValue const&>(*this);
  102. }
  103. CalculatedStyleValue const& StyleValue::as_calculated() const
  104. {
  105. VERIFY(is_calculated());
  106. return static_cast<CalculatedStyleValue const&>(*this);
  107. }
  108. ColorStyleValue const& StyleValue::as_color() const
  109. {
  110. VERIFY(is_color());
  111. return static_cast<ColorStyleValue const&>(*this);
  112. }
  113. ConicGradientStyleValue const& StyleValue::as_conic_gradient() const
  114. {
  115. VERIFY(is_conic_gradient());
  116. return static_cast<ConicGradientStyleValue const&>(*this);
  117. }
  118. ContentStyleValue const& StyleValue::as_content() const
  119. {
  120. VERIFY(is_content());
  121. return static_cast<ContentStyleValue const&>(*this);
  122. }
  123. FilterValueListStyleValue const& StyleValue::as_filter_value_list() const
  124. {
  125. VERIFY(is_filter_value_list());
  126. return static_cast<FilterValueListStyleValue const&>(*this);
  127. }
  128. FlexStyleValue const& StyleValue::as_flex() const
  129. {
  130. VERIFY(is_flex());
  131. return static_cast<FlexStyleValue const&>(*this);
  132. }
  133. FlexFlowStyleValue const& StyleValue::as_flex_flow() const
  134. {
  135. VERIFY(is_flex_flow());
  136. return static_cast<FlexFlowStyleValue const&>(*this);
  137. }
  138. FontStyleValue const& StyleValue::as_font() const
  139. {
  140. VERIFY(is_font());
  141. return static_cast<FontStyleValue const&>(*this);
  142. }
  143. FrequencyStyleValue const& StyleValue::as_frequency() const
  144. {
  145. VERIFY(is_frequency());
  146. return static_cast<FrequencyStyleValue const&>(*this);
  147. }
  148. GridTrackPlacementShorthandStyleValue const& StyleValue::as_grid_track_placement_shorthand() const
  149. {
  150. VERIFY(is_grid_track_placement_shorthand());
  151. return static_cast<GridTrackPlacementShorthandStyleValue const&>(*this);
  152. }
  153. GridAreaShorthandStyleValue const& StyleValue::as_grid_area_shorthand() const
  154. {
  155. VERIFY(is_grid_area_shorthand());
  156. return static_cast<GridAreaShorthandStyleValue const&>(*this);
  157. }
  158. GridTemplateAreaStyleValue const& StyleValue::as_grid_template_area() const
  159. {
  160. VERIFY(is_grid_template_area());
  161. return static_cast<GridTemplateAreaStyleValue const&>(*this);
  162. }
  163. GridTrackPlacementStyleValue const& StyleValue::as_grid_track_placement() const
  164. {
  165. VERIFY(is_grid_track_placement());
  166. return static_cast<GridTrackPlacementStyleValue const&>(*this);
  167. }
  168. IdentifierStyleValue const& StyleValue::as_identifier() const
  169. {
  170. VERIFY(is_identifier());
  171. return static_cast<IdentifierStyleValue const&>(*this);
  172. }
  173. ImageStyleValue const& StyleValue::as_image() const
  174. {
  175. VERIFY(is_image());
  176. return static_cast<ImageStyleValue const&>(*this);
  177. }
  178. InheritStyleValue const& StyleValue::as_inherit() const
  179. {
  180. VERIFY(is_inherit());
  181. return static_cast<InheritStyleValue const&>(*this);
  182. }
  183. InitialStyleValue const& StyleValue::as_initial() const
  184. {
  185. VERIFY(is_initial());
  186. return static_cast<InitialStyleValue const&>(*this);
  187. }
  188. LengthStyleValue const& StyleValue::as_length() const
  189. {
  190. VERIFY(is_length());
  191. return static_cast<LengthStyleValue const&>(*this);
  192. }
  193. GridTrackSizeStyleValue const& StyleValue::as_grid_track_size_list() const
  194. {
  195. VERIFY(is_grid_track_size_list());
  196. return static_cast<GridTrackSizeStyleValue const&>(*this);
  197. }
  198. LinearGradientStyleValue const& StyleValue::as_linear_gradient() const
  199. {
  200. VERIFY(is_linear_gradient());
  201. return static_cast<LinearGradientStyleValue const&>(*this);
  202. }
  203. ListStyleStyleValue const& StyleValue::as_list_style() const
  204. {
  205. VERIFY(is_list_style());
  206. return static_cast<ListStyleStyleValue const&>(*this);
  207. }
  208. NumericStyleValue const& StyleValue::as_numeric() const
  209. {
  210. VERIFY(is_numeric());
  211. return static_cast<NumericStyleValue const&>(*this);
  212. }
  213. OverflowStyleValue const& StyleValue::as_overflow() const
  214. {
  215. VERIFY(is_overflow());
  216. return static_cast<OverflowStyleValue const&>(*this);
  217. }
  218. PercentageStyleValue const& StyleValue::as_percentage() const
  219. {
  220. VERIFY(is_percentage());
  221. return static_cast<PercentageStyleValue const&>(*this);
  222. }
  223. PositionStyleValue const& StyleValue::as_position() const
  224. {
  225. VERIFY(is_position());
  226. return static_cast<PositionStyleValue const&>(*this);
  227. }
  228. RadialGradientStyleValue const& StyleValue::as_radial_gradient() const
  229. {
  230. VERIFY(is_radial_gradient());
  231. return static_cast<RadialGradientStyleValue const&>(*this);
  232. }
  233. RectStyleValue const& StyleValue::as_rect() const
  234. {
  235. VERIFY(is_rect());
  236. return static_cast<RectStyleValue const&>(*this);
  237. }
  238. ResolutionStyleValue const& StyleValue::as_resolution() const
  239. {
  240. VERIFY(is_resolution());
  241. return static_cast<ResolutionStyleValue const&>(*this);
  242. }
  243. StringStyleValue const& StyleValue::as_string() const
  244. {
  245. VERIFY(is_string());
  246. return static_cast<StringStyleValue const&>(*this);
  247. }
  248. TextDecorationStyleValue const& StyleValue::as_text_decoration() const
  249. {
  250. VERIFY(is_text_decoration());
  251. return static_cast<TextDecorationStyleValue const&>(*this);
  252. }
  253. TimeStyleValue const& StyleValue::as_time() const
  254. {
  255. VERIFY(is_time());
  256. return static_cast<TimeStyleValue const&>(*this);
  257. }
  258. TransformationStyleValue const& StyleValue::as_transformation() const
  259. {
  260. VERIFY(is_transformation());
  261. return static_cast<TransformationStyleValue const&>(*this);
  262. }
  263. UnresolvedStyleValue const& StyleValue::as_unresolved() const
  264. {
  265. VERIFY(is_unresolved());
  266. return static_cast<UnresolvedStyleValue const&>(*this);
  267. }
  268. UnsetStyleValue const& StyleValue::as_unset() const
  269. {
  270. VERIFY(is_unset());
  271. return static_cast<UnsetStyleValue const&>(*this);
  272. }
  273. StyleValueList const& StyleValue::as_value_list() const
  274. {
  275. VERIFY(is_value_list());
  276. return static_cast<StyleValueList const&>(*this);
  277. }
  278. void CalculatedStyleValue::CalculationResult::add(CalculationResult const& other, Layout::Node const* layout_node, PercentageBasis const& percentage_basis)
  279. {
  280. add_or_subtract_internal(SumOperation::Add, other, layout_node, percentage_basis);
  281. }
  282. void CalculatedStyleValue::CalculationResult::subtract(CalculationResult const& other, Layout::Node const* layout_node, PercentageBasis const& percentage_basis)
  283. {
  284. add_or_subtract_internal(SumOperation::Subtract, other, layout_node, percentage_basis);
  285. }
  286. void CalculatedStyleValue::CalculationResult::add_or_subtract_internal(SumOperation op, CalculationResult const& other, Layout::Node const* layout_node, PercentageBasis const& percentage_basis)
  287. {
  288. // 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>".
  289. // Though, having the same type may mean that one side is a <dimension> and the other a <percentage>.
  290. // Note: This is almost identical to ::add()
  291. m_value.visit(
  292. [&](Number const& number) {
  293. auto other_number = other.m_value.get<Number>();
  294. if (op == SumOperation::Add) {
  295. m_value = number + other_number;
  296. } else {
  297. m_value = number - other_number;
  298. }
  299. },
  300. [&](Angle const& angle) {
  301. auto this_degrees = angle.to_degrees();
  302. if (other.m_value.has<Angle>()) {
  303. auto other_degrees = other.m_value.get<Angle>().to_degrees();
  304. if (op == SumOperation::Add)
  305. m_value = Angle::make_degrees(this_degrees + other_degrees);
  306. else
  307. m_value = Angle::make_degrees(this_degrees - other_degrees);
  308. } else {
  309. VERIFY(percentage_basis.has<Angle>());
  310. auto other_degrees = percentage_basis.get<Angle>().percentage_of(other.m_value.get<Percentage>()).to_degrees();
  311. if (op == SumOperation::Add)
  312. m_value = Angle::make_degrees(this_degrees + other_degrees);
  313. else
  314. m_value = Angle::make_degrees(this_degrees - other_degrees);
  315. }
  316. },
  317. [&](Frequency const& frequency) {
  318. auto this_hertz = frequency.to_hertz();
  319. if (other.m_value.has<Frequency>()) {
  320. auto other_hertz = other.m_value.get<Frequency>().to_hertz();
  321. if (op == SumOperation::Add)
  322. m_value = Frequency::make_hertz(this_hertz + other_hertz);
  323. else
  324. m_value = Frequency::make_hertz(this_hertz - other_hertz);
  325. } else {
  326. VERIFY(percentage_basis.has<Frequency>());
  327. auto other_hertz = percentage_basis.get<Frequency>().percentage_of(other.m_value.get<Percentage>()).to_hertz();
  328. if (op == SumOperation::Add)
  329. m_value = Frequency::make_hertz(this_hertz + other_hertz);
  330. else
  331. m_value = Frequency::make_hertz(this_hertz - other_hertz);
  332. }
  333. },
  334. [&](Length const& length) {
  335. auto this_px = length.to_px(*layout_node);
  336. if (other.m_value.has<Length>()) {
  337. auto other_px = other.m_value.get<Length>().to_px(*layout_node);
  338. if (op == SumOperation::Add)
  339. m_value = Length::make_px(this_px + other_px);
  340. else
  341. m_value = Length::make_px(this_px - other_px);
  342. } else {
  343. VERIFY(percentage_basis.has<Length>());
  344. auto other_px = percentage_basis.get<Length>().percentage_of(other.m_value.get<Percentage>()).to_px(*layout_node);
  345. if (op == SumOperation::Add)
  346. m_value = Length::make_px(this_px + other_px);
  347. else
  348. m_value = Length::make_px(this_px - other_px);
  349. }
  350. },
  351. [&](Time const& time) {
  352. auto this_seconds = time.to_seconds();
  353. if (other.m_value.has<Time>()) {
  354. auto other_seconds = other.m_value.get<Time>().to_seconds();
  355. if (op == SumOperation::Add)
  356. m_value = Time::make_seconds(this_seconds + other_seconds);
  357. else
  358. m_value = Time::make_seconds(this_seconds - other_seconds);
  359. } else {
  360. VERIFY(percentage_basis.has<Time>());
  361. auto other_seconds = percentage_basis.get<Time>().percentage_of(other.m_value.get<Percentage>()).to_seconds();
  362. if (op == SumOperation::Add)
  363. m_value = Time::make_seconds(this_seconds + other_seconds);
  364. else
  365. m_value = Time::make_seconds(this_seconds - other_seconds);
  366. }
  367. },
  368. [&](Percentage const& percentage) {
  369. if (other.m_value.has<Percentage>()) {
  370. if (op == SumOperation::Add)
  371. m_value = Percentage { percentage.value() + other.m_value.get<Percentage>().value() };
  372. else
  373. m_value = Percentage { percentage.value() - other.m_value.get<Percentage>().value() };
  374. return;
  375. }
  376. // 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`.
  377. CalculationResult new_value = other;
  378. if (op == SumOperation::Add) {
  379. new_value.add(*this, layout_node, percentage_basis);
  380. } else {
  381. // Turn 'this - other' into '-other + this', as 'A + B == B + A', but 'A - B != B - A'
  382. new_value.multiply_by({ Number { Number::Type::Integer, -1.0f } }, layout_node);
  383. new_value.add(*this, layout_node, percentage_basis);
  384. }
  385. *this = new_value;
  386. });
  387. }
  388. void CalculatedStyleValue::CalculationResult::multiply_by(CalculationResult const& other, Layout::Node const* layout_node)
  389. {
  390. // We know from validation when resolving the type, that at least one side must be a <number> or <integer>.
  391. // Both of these are represented as a float.
  392. VERIFY(m_value.has<Number>() || other.m_value.has<Number>());
  393. bool other_is_number = other.m_value.has<Number>();
  394. m_value.visit(
  395. [&](Number const& number) {
  396. if (other_is_number) {
  397. m_value = number * other.m_value.get<Number>();
  398. } else {
  399. // Avoid duplicating all the logic by swapping `this` and `other`.
  400. CalculationResult new_value = other;
  401. new_value.multiply_by(*this, layout_node);
  402. *this = new_value;
  403. }
  404. },
  405. [&](Angle const& angle) {
  406. m_value = Angle::make_degrees(angle.to_degrees() * other.m_value.get<Number>().value());
  407. },
  408. [&](Frequency const& frequency) {
  409. m_value = Frequency::make_hertz(frequency.to_hertz() * other.m_value.get<Number>().value());
  410. },
  411. [&](Length const& length) {
  412. VERIFY(layout_node);
  413. m_value = Length::make_px(length.to_px(*layout_node) * other.m_value.get<Number>().value());
  414. },
  415. [&](Time const& time) {
  416. m_value = Time::make_seconds(time.to_seconds() * other.m_value.get<Number>().value());
  417. },
  418. [&](Percentage const& percentage) {
  419. m_value = Percentage { percentage.value() * other.m_value.get<Number>().value() };
  420. });
  421. }
  422. void CalculatedStyleValue::CalculationResult::divide_by(CalculationResult const& other, Layout::Node const* layout_node)
  423. {
  424. // We know from validation when resolving the type, that `other` must be a <number> or <integer>.
  425. // Both of these are represented as a Number.
  426. auto denominator = other.m_value.get<Number>().value();
  427. // FIXME: Dividing by 0 is invalid, and should be caught during parsing.
  428. VERIFY(denominator != 0.0f);
  429. m_value.visit(
  430. [&](Number const& number) {
  431. m_value = Number {
  432. Number::Type::Number,
  433. number.value() / denominator
  434. };
  435. },
  436. [&](Angle const& angle) {
  437. m_value = Angle::make_degrees(angle.to_degrees() / denominator);
  438. },
  439. [&](Frequency const& frequency) {
  440. m_value = Frequency::make_hertz(frequency.to_hertz() / denominator);
  441. },
  442. [&](Length const& length) {
  443. VERIFY(layout_node);
  444. m_value = Length::make_px(length.to_px(*layout_node) / denominator);
  445. },
  446. [&](Time const& time) {
  447. m_value = Time::make_seconds(time.to_seconds() / denominator);
  448. },
  449. [&](Percentage const& percentage) {
  450. m_value = Percentage { percentage.value() / denominator };
  451. });
  452. }
  453. ErrorOr<String> CalculatedStyleValue::to_string() const
  454. {
  455. return String::formatted("calc({})", TRY(m_expression->to_string()));
  456. }
  457. bool CalculatedStyleValue::equals(StyleValue const& other) const
  458. {
  459. if (type() != other.type())
  460. return false;
  461. // This is a case where comparing the strings actually makes sense.
  462. return to_string().release_value_but_fixme_should_propagate_errors() == other.to_string().release_value_but_fixme_should_propagate_errors();
  463. }
  464. ErrorOr<String> CalculatedStyleValue::CalcNumberValue::to_string() const
  465. {
  466. return value.visit(
  467. [](Number const& number) -> ErrorOr<String> { return String::number(number.value()); },
  468. [](NonnullOwnPtr<CalcNumberSum> const& sum) -> ErrorOr<String> { return String::formatted("({})", TRY(sum->to_string())); });
  469. }
  470. ErrorOr<String> CalculatedStyleValue::CalcValue::to_string() const
  471. {
  472. return value.visit(
  473. [](Number const& number) -> ErrorOr<String> { return String::number(number.value()); },
  474. [](NonnullOwnPtr<CalcSum> const& sum) -> ErrorOr<String> { return String::formatted("({})", TRY(sum->to_string())); },
  475. [](auto const& v) -> ErrorOr<String> { return v.to_string(); });
  476. }
  477. ErrorOr<String> CalculatedStyleValue::CalcSum::to_string() const
  478. {
  479. StringBuilder builder;
  480. TRY(builder.try_append(TRY(first_calc_product->to_string())));
  481. for (auto const& item : zero_or_more_additional_calc_products)
  482. TRY(builder.try_append(TRY(item->to_string())));
  483. return builder.to_string();
  484. }
  485. ErrorOr<String> CalculatedStyleValue::CalcNumberSum::to_string() const
  486. {
  487. StringBuilder builder;
  488. TRY(builder.try_append(TRY(first_calc_number_product->to_string())));
  489. for (auto const& item : zero_or_more_additional_calc_number_products)
  490. TRY(builder.try_append(TRY(item->to_string())));
  491. return builder.to_string();
  492. }
  493. ErrorOr<String> CalculatedStyleValue::CalcProduct::to_string() const
  494. {
  495. StringBuilder builder;
  496. TRY(builder.try_append(TRY(first_calc_value.to_string())));
  497. for (auto const& item : zero_or_more_additional_calc_values)
  498. TRY(builder.try_append(TRY(item->to_string())));
  499. return builder.to_string();
  500. }
  501. ErrorOr<String> CalculatedStyleValue::CalcSumPartWithOperator::to_string() const
  502. {
  503. return String::formatted(" {} {}", op == SumOperation::Add ? "+"sv : "-"sv, TRY(value->to_string()));
  504. }
  505. ErrorOr<String> CalculatedStyleValue::CalcProductPartWithOperator::to_string() const
  506. {
  507. auto value_string = TRY(value.visit(
  508. [](CalcValue const& v) { return v.to_string(); },
  509. [](CalcNumberValue const& v) { return v.to_string(); }));
  510. return String::formatted(" {} {}", op == ProductOperation::Multiply ? "*"sv : "/"sv, value_string);
  511. }
  512. ErrorOr<String> CalculatedStyleValue::CalcNumberProduct::to_string() const
  513. {
  514. StringBuilder builder;
  515. TRY(builder.try_append(TRY(first_calc_number_value.to_string())));
  516. for (auto const& item : zero_or_more_additional_calc_number_values)
  517. TRY(builder.try_append(TRY(item->to_string())));
  518. return builder.to_string();
  519. }
  520. ErrorOr<String> CalculatedStyleValue::CalcNumberProductPartWithOperator::to_string() const
  521. {
  522. return String::formatted(" {} {}", op == ProductOperation::Multiply ? "*"sv : "/"sv, TRY(value.to_string()));
  523. }
  524. ErrorOr<String> CalculatedStyleValue::CalcNumberSumPartWithOperator::to_string() const
  525. {
  526. return String::formatted(" {} {}", op == SumOperation::Add ? "+"sv : "-"sv, TRY(value->to_string()));
  527. }
  528. Optional<Angle> CalculatedStyleValue::resolve_angle() const
  529. {
  530. auto result = m_expression->resolve(nullptr, {});
  531. if (result.value().has<Angle>())
  532. return result.value().get<Angle>();
  533. return {};
  534. }
  535. Optional<Angle> CalculatedStyleValue::resolve_angle_percentage(Angle const& percentage_basis) const
  536. {
  537. auto result = m_expression->resolve(nullptr, percentage_basis);
  538. return result.value().visit(
  539. [&](Angle const& angle) -> Optional<Angle> {
  540. return angle;
  541. },
  542. [&](Percentage const& percentage) -> Optional<Angle> {
  543. return percentage_basis.percentage_of(percentage);
  544. },
  545. [&](auto const&) -> Optional<Angle> {
  546. return {};
  547. });
  548. }
  549. Optional<Frequency> CalculatedStyleValue::resolve_frequency() const
  550. {
  551. auto result = m_expression->resolve(nullptr, {});
  552. if (result.value().has<Frequency>())
  553. return result.value().get<Frequency>();
  554. return {};
  555. }
  556. Optional<Frequency> CalculatedStyleValue::resolve_frequency_percentage(Frequency const& percentage_basis) const
  557. {
  558. auto result = m_expression->resolve(nullptr, percentage_basis);
  559. return result.value().visit(
  560. [&](Frequency const& frequency) -> Optional<Frequency> {
  561. return frequency;
  562. },
  563. [&](Percentage const& percentage) -> Optional<Frequency> {
  564. return percentage_basis.percentage_of(percentage);
  565. },
  566. [&](auto const&) -> Optional<Frequency> {
  567. return {};
  568. });
  569. }
  570. Optional<Length> CalculatedStyleValue::resolve_length(Layout::Node const& layout_node) const
  571. {
  572. auto result = m_expression->resolve(&layout_node, {});
  573. if (result.value().has<Length>())
  574. return result.value().get<Length>();
  575. return {};
  576. }
  577. Optional<Length> CalculatedStyleValue::resolve_length_percentage(Layout::Node const& layout_node, Length const& percentage_basis) const
  578. {
  579. auto result = m_expression->resolve(&layout_node, percentage_basis);
  580. return result.value().visit(
  581. [&](Length const& length) -> Optional<Length> {
  582. return length;
  583. },
  584. [&](Percentage const& percentage) -> Optional<Length> {
  585. return percentage_basis.percentage_of(percentage);
  586. },
  587. [&](auto const&) -> Optional<Length> {
  588. return {};
  589. });
  590. }
  591. Optional<Percentage> CalculatedStyleValue::resolve_percentage() const
  592. {
  593. auto result = m_expression->resolve(nullptr, {});
  594. if (result.value().has<Percentage>())
  595. return result.value().get<Percentage>();
  596. return {};
  597. }
  598. Optional<Time> CalculatedStyleValue::resolve_time() const
  599. {
  600. auto result = m_expression->resolve(nullptr, {});
  601. if (result.value().has<Time>())
  602. return result.value().get<Time>();
  603. return {};
  604. }
  605. Optional<Time> CalculatedStyleValue::resolve_time_percentage(Time const& percentage_basis) const
  606. {
  607. auto result = m_expression->resolve(nullptr, percentage_basis);
  608. return result.value().visit(
  609. [&](Time const& time) -> Optional<Time> {
  610. return time;
  611. },
  612. [&](auto const&) -> Optional<Time> {
  613. return {};
  614. });
  615. }
  616. Optional<float> CalculatedStyleValue::resolve_number()
  617. {
  618. auto result = m_expression->resolve(nullptr, {});
  619. if (result.value().has<Number>())
  620. return result.value().get<Number>().value();
  621. return {};
  622. }
  623. Optional<i64> CalculatedStyleValue::resolve_integer()
  624. {
  625. auto result = m_expression->resolve(nullptr, {});
  626. if (result.value().has<Number>())
  627. return result.value().get<Number>().integer_value();
  628. return {};
  629. }
  630. static bool is_number(CalculatedStyleValue::ResolvedType type)
  631. {
  632. return type == CalculatedStyleValue::ResolvedType::Number || type == CalculatedStyleValue::ResolvedType::Integer;
  633. }
  634. static bool is_dimension(CalculatedStyleValue::ResolvedType type)
  635. {
  636. return type != CalculatedStyleValue::ResolvedType::Number
  637. && type != CalculatedStyleValue::ResolvedType::Integer
  638. && type != CalculatedStyleValue::ResolvedType::Percentage;
  639. }
  640. template<typename SumWithOperator>
  641. static Optional<CalculatedStyleValue::ResolvedType> resolve_sum_type(CalculatedStyleValue::ResolvedType first_type, Vector<NonnullOwnPtr<SumWithOperator>> const& zero_or_more_additional_products)
  642. {
  643. auto type = first_type;
  644. for (auto const& product : zero_or_more_additional_products) {
  645. auto maybe_product_type = product->resolved_type();
  646. if (!maybe_product_type.has_value())
  647. return {};
  648. auto product_type = maybe_product_type.value();
  649. // At + or -, check that both sides have the same type, or that one side is a <number> and the other is an <integer>.
  650. // If both sides are the same type, resolve to that type.
  651. if (product_type == type)
  652. continue;
  653. // If one side is a <number> and the other is an <integer>, resolve to <number>.
  654. if (is_number(type) && is_number(product_type)) {
  655. type = CalculatedStyleValue::ResolvedType::Number;
  656. continue;
  657. }
  658. // FIXME: calc() handles <percentage> by allowing them to pretend to be whatever <dimension> type is allowed at this location.
  659. // Since we can't easily check what that type is, we just allow <percentage> to combine with any other <dimension> type.
  660. if (type == CalculatedStyleValue::ResolvedType::Percentage && is_dimension(product_type)) {
  661. type = product_type;
  662. continue;
  663. }
  664. if (is_dimension(type) && product_type == CalculatedStyleValue::ResolvedType::Percentage)
  665. continue;
  666. return {};
  667. }
  668. return type;
  669. }
  670. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcSum::resolved_type() const
  671. {
  672. auto maybe_type = first_calc_product->resolved_type();
  673. if (!maybe_type.has_value())
  674. return {};
  675. auto type = maybe_type.value();
  676. return resolve_sum_type(type, zero_or_more_additional_calc_products);
  677. }
  678. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcNumberSum::resolved_type() const
  679. {
  680. auto maybe_type = first_calc_number_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_number_products);
  685. }
  686. template<typename ProductWithOperator>
  687. static Optional<CalculatedStyleValue::ResolvedType> resolve_product_type(CalculatedStyleValue::ResolvedType first_type, Vector<NonnullOwnPtr<ProductWithOperator>> const& zero_or_more_additional_values)
  688. {
  689. auto type = first_type;
  690. for (auto const& value : zero_or_more_additional_values) {
  691. auto maybe_value_type = value->resolved_type();
  692. if (!maybe_value_type.has_value())
  693. return {};
  694. auto value_type = maybe_value_type.value();
  695. if (value->op == CalculatedStyleValue::ProductOperation::Multiply) {
  696. // At *, check that at least one side is <number>.
  697. if (!(is_number(type) || is_number(value_type)))
  698. return {};
  699. // If both sides are <integer>, resolve to <integer>.
  700. if (type == CalculatedStyleValue::ResolvedType::Integer && value_type == CalculatedStyleValue::ResolvedType::Integer) {
  701. type = CalculatedStyleValue::ResolvedType::Integer;
  702. } else {
  703. // Otherwise, resolve to the type of the other side.
  704. if (is_number(type))
  705. type = value_type;
  706. }
  707. continue;
  708. } else {
  709. VERIFY(value->op == CalculatedStyleValue::ProductOperation::Divide);
  710. // At /, check that the right side is <number>.
  711. if (!is_number(value_type))
  712. return {};
  713. // If the left side is <integer>, resolve to <number>.
  714. if (type == CalculatedStyleValue::ResolvedType::Integer) {
  715. type = CalculatedStyleValue::ResolvedType::Number;
  716. } else {
  717. // Otherwise, resolve to the type of the left side.
  718. }
  719. // FIXME: Division by zero makes the whole calc() expression invalid.
  720. }
  721. }
  722. return type;
  723. }
  724. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcProduct::resolved_type() const
  725. {
  726. auto maybe_type = first_calc_value.resolved_type();
  727. if (!maybe_type.has_value())
  728. return {};
  729. auto type = maybe_type.value();
  730. return resolve_product_type(type, zero_or_more_additional_calc_values);
  731. }
  732. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcSumPartWithOperator::resolved_type() const
  733. {
  734. return value->resolved_type();
  735. }
  736. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcNumberProduct::resolved_type() const
  737. {
  738. auto maybe_type = first_calc_number_value.resolved_type();
  739. if (!maybe_type.has_value())
  740. return {};
  741. auto type = maybe_type.value();
  742. return resolve_product_type(type, zero_or_more_additional_calc_number_values);
  743. }
  744. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcNumberProductPartWithOperator::resolved_type() const
  745. {
  746. return value.resolved_type();
  747. }
  748. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcNumberSumPartWithOperator::resolved_type() const
  749. {
  750. return value->resolved_type();
  751. }
  752. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcProductPartWithOperator::resolved_type() const
  753. {
  754. return value.visit(
  755. [](CalcValue const& calc_value) {
  756. return calc_value.resolved_type();
  757. },
  758. [](CalcNumberValue const& calc_number_value) {
  759. return calc_number_value.resolved_type();
  760. });
  761. }
  762. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcValue::resolved_type() const
  763. {
  764. return value.visit(
  765. [](Number const& number) -> Optional<CalculatedStyleValue::ResolvedType> {
  766. return { number.is_integer() ? ResolvedType::Integer : ResolvedType::Number };
  767. },
  768. [](Angle const&) -> Optional<CalculatedStyleValue::ResolvedType> { return { ResolvedType::Angle }; },
  769. [](Frequency const&) -> Optional<CalculatedStyleValue::ResolvedType> { return { ResolvedType::Frequency }; },
  770. [](Length const&) -> Optional<CalculatedStyleValue::ResolvedType> { return { ResolvedType::Length }; },
  771. [](Percentage const&) -> Optional<CalculatedStyleValue::ResolvedType> { return { ResolvedType::Percentage }; },
  772. [](Time const&) -> Optional<CalculatedStyleValue::ResolvedType> { return { ResolvedType::Time }; },
  773. [](NonnullOwnPtr<CalcSum> const& sum) { return sum->resolved_type(); });
  774. }
  775. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcNumberValue::resolved_type() const
  776. {
  777. return value.visit(
  778. [](Number const& number) -> Optional<CalculatedStyleValue::ResolvedType> {
  779. return { number.is_integer() ? ResolvedType::Integer : ResolvedType::Number };
  780. },
  781. [](NonnullOwnPtr<CalcNumberSum> const& sum) { return sum->resolved_type(); });
  782. }
  783. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcNumberValue::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  784. {
  785. return value.visit(
  786. [&](Number const& number) -> CalculatedStyleValue::CalculationResult {
  787. return CalculatedStyleValue::CalculationResult { number };
  788. },
  789. [&](NonnullOwnPtr<CalcNumberSum> const& sum) -> CalculatedStyleValue::CalculationResult {
  790. return sum->resolve(layout_node, percentage_basis);
  791. });
  792. }
  793. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcValue::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  794. {
  795. return value.visit(
  796. [&](NonnullOwnPtr<CalcSum> const& sum) -> CalculatedStyleValue::CalculationResult {
  797. return sum->resolve(layout_node, percentage_basis);
  798. },
  799. [&](auto const& v) -> CalculatedStyleValue::CalculationResult {
  800. return CalculatedStyleValue::CalculationResult { v };
  801. });
  802. }
  803. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcSum::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  804. {
  805. auto value = first_calc_product->resolve(layout_node, percentage_basis);
  806. for (auto& additional_product : zero_or_more_additional_calc_products) {
  807. auto additional_value = additional_product->resolve(layout_node, percentage_basis);
  808. if (additional_product->op == CalculatedStyleValue::SumOperation::Add)
  809. value.add(additional_value, layout_node, percentage_basis);
  810. else if (additional_product->op == CalculatedStyleValue::SumOperation::Subtract)
  811. value.subtract(additional_value, layout_node, percentage_basis);
  812. else
  813. VERIFY_NOT_REACHED();
  814. }
  815. return value;
  816. }
  817. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcNumberSum::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  818. {
  819. auto value = first_calc_number_product->resolve(layout_node, percentage_basis);
  820. for (auto& additional_product : zero_or_more_additional_calc_number_products) {
  821. auto additional_value = additional_product->resolve(layout_node, percentage_basis);
  822. if (additional_product->op == CSS::CalculatedStyleValue::SumOperation::Add)
  823. value.add(additional_value, layout_node, percentage_basis);
  824. else if (additional_product->op == CalculatedStyleValue::SumOperation::Subtract)
  825. value.subtract(additional_value, layout_node, percentage_basis);
  826. else
  827. VERIFY_NOT_REACHED();
  828. }
  829. return value;
  830. }
  831. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcProduct::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  832. {
  833. auto value = first_calc_value.resolve(layout_node, percentage_basis);
  834. for (auto& additional_value : zero_or_more_additional_calc_values) {
  835. additional_value->value.visit(
  836. [&](CalculatedStyleValue::CalcValue const& calc_value) {
  837. VERIFY(additional_value->op == CalculatedStyleValue::ProductOperation::Multiply);
  838. auto resolved_value = calc_value.resolve(layout_node, percentage_basis);
  839. value.multiply_by(resolved_value, layout_node);
  840. },
  841. [&](CalculatedStyleValue::CalcNumberValue const& calc_number_value) {
  842. VERIFY(additional_value->op == CalculatedStyleValue::ProductOperation::Divide);
  843. auto resolved_calc_number_value = calc_number_value.resolve(layout_node, percentage_basis);
  844. // FIXME: Checking for division by 0 should happen during parsing.
  845. VERIFY(resolved_calc_number_value.value().get<Number>().value() != 0.0f);
  846. value.divide_by(resolved_calc_number_value, layout_node);
  847. });
  848. }
  849. return value;
  850. }
  851. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcNumberProduct::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  852. {
  853. auto value = first_calc_number_value.resolve(layout_node, percentage_basis);
  854. for (auto& additional_number_value : zero_or_more_additional_calc_number_values) {
  855. auto additional_value = additional_number_value->resolve(layout_node, percentage_basis);
  856. if (additional_number_value->op == CalculatedStyleValue::ProductOperation::Multiply)
  857. value.multiply_by(additional_value, layout_node);
  858. else if (additional_number_value->op == CalculatedStyleValue::ProductOperation::Divide)
  859. value.divide_by(additional_value, layout_node);
  860. else
  861. VERIFY_NOT_REACHED();
  862. }
  863. return value;
  864. }
  865. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcProductPartWithOperator::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  866. {
  867. return value.visit(
  868. [&](CalcValue const& calc_value) {
  869. return calc_value.resolve(layout_node, percentage_basis);
  870. },
  871. [&](CalcNumberValue const& calc_number_value) {
  872. return calc_number_value.resolve(layout_node, percentage_basis);
  873. });
  874. }
  875. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcSumPartWithOperator::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  876. {
  877. return value->resolve(layout_node, percentage_basis);
  878. }
  879. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcNumberProductPartWithOperator::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  880. {
  881. return value.resolve(layout_node, percentage_basis);
  882. }
  883. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcNumberSumPartWithOperator::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  884. {
  885. return value->resolve(layout_node, percentage_basis);
  886. }
  887. ValueComparingNonnullRefPtr<StyleValue const> StyleValue::absolutized(CSSPixelRect const&, Gfx::FontPixelMetrics const&, CSSPixels, CSSPixels, CSSPixels, CSSPixels) const
  888. {
  889. return *this;
  890. }
  891. bool CalculatedStyleValue::contains_percentage() const
  892. {
  893. return m_expression->contains_percentage();
  894. }
  895. bool CalculatedStyleValue::CalcSum::contains_percentage() const
  896. {
  897. if (first_calc_product->contains_percentage())
  898. return true;
  899. for (auto& part : zero_or_more_additional_calc_products) {
  900. if (part->contains_percentage())
  901. return true;
  902. }
  903. return false;
  904. }
  905. bool CalculatedStyleValue::CalcSumPartWithOperator::contains_percentage() const
  906. {
  907. return value->contains_percentage();
  908. }
  909. bool CalculatedStyleValue::CalcProduct::contains_percentage() const
  910. {
  911. if (first_calc_value.contains_percentage())
  912. return true;
  913. for (auto& part : zero_or_more_additional_calc_values) {
  914. if (part->contains_percentage())
  915. return true;
  916. }
  917. return false;
  918. }
  919. bool CalculatedStyleValue::CalcProductPartWithOperator::contains_percentage() const
  920. {
  921. return value.visit(
  922. [](CalcValue const& value) { return value.contains_percentage(); },
  923. [](CalcNumberValue const&) { return false; });
  924. }
  925. bool CalculatedStyleValue::CalcValue::contains_percentage() const
  926. {
  927. return value.visit(
  928. [](Percentage const&) { return true; },
  929. [](NonnullOwnPtr<CalcSum> const& sum) { return sum->contains_percentage(); },
  930. [](auto const&) { return false; });
  931. }
  932. bool calculated_style_value_contains_percentage(CalculatedStyleValue const& value)
  933. {
  934. return value.contains_percentage();
  935. }
  936. }