CalculatedStyleValue.cpp 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
  4. * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
  5. * Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #include "CalculatedStyleValue.h"
  10. #include <LibWeb/CSS/Percentage.h>
  11. namespace Web::CSS {
  12. static bool is_number(CalculatedStyleValue::ResolvedType type)
  13. {
  14. return type == CalculatedStyleValue::ResolvedType::Number || type == CalculatedStyleValue::ResolvedType::Integer;
  15. }
  16. static bool is_dimension(CalculatedStyleValue::ResolvedType type)
  17. {
  18. return type != CalculatedStyleValue::ResolvedType::Number
  19. && type != CalculatedStyleValue::ResolvedType::Integer
  20. && type != CalculatedStyleValue::ResolvedType::Percentage;
  21. }
  22. static double resolve_value_radians(CalculatedStyleValue::CalculationResult::Value value)
  23. {
  24. return value.visit(
  25. [](Number const& number) { return number.value(); },
  26. [](Angle const& angle) { return angle.to_radians(); },
  27. [](auto const&) { VERIFY_NOT_REACHED(); return 0.0; });
  28. };
  29. static double resolve_value(CalculatedStyleValue::CalculationResult::Value value, Optional<Length::ResolutionContext const&> context)
  30. {
  31. return value.visit(
  32. [](Number const& number) { return number.value(); },
  33. [](Angle const& angle) { return angle.to_degrees(); },
  34. [](Frequency const& frequency) { return frequency.to_hertz(); },
  35. [&context](Length const& length) { return length.to_px(*context).to_double(); },
  36. [](Percentage const& percentage) { return percentage.value(); },
  37. [](Time const& time) { return time.to_seconds(); });
  38. };
  39. static CalculatedStyleValue::CalculationResult to_resolved_type(CalculatedStyleValue::ResolvedType type, double value)
  40. {
  41. switch (type) {
  42. case CalculatedStyleValue::ResolvedType::Integer:
  43. return { Number(Number::Type::Integer, value) };
  44. case CalculatedStyleValue::ResolvedType::Number:
  45. return { Number(Number::Type::Number, value) };
  46. case CalculatedStyleValue::ResolvedType::Angle:
  47. return { Angle::make_degrees(value) };
  48. case CalculatedStyleValue::ResolvedType::Frequency:
  49. return { Frequency::make_hertz(value) };
  50. case CalculatedStyleValue::ResolvedType::Length:
  51. return { Length::make_px(value) };
  52. case CalculatedStyleValue::ResolvedType::Percentage:
  53. return { Percentage(value) };
  54. case CalculatedStyleValue::ResolvedType::Time:
  55. return { Time::make_seconds(value) };
  56. }
  57. VERIFY_NOT_REACHED();
  58. };
  59. CalculationNode::CalculationNode(Type type)
  60. : m_type(type)
  61. {
  62. }
  63. CalculationNode::~CalculationNode() = default;
  64. ErrorOr<NonnullOwnPtr<NumericCalculationNode>> NumericCalculationNode::create(NumericValue value)
  65. {
  66. return adopt_nonnull_own_or_enomem(new (nothrow) NumericCalculationNode(move(value)));
  67. }
  68. NumericCalculationNode::NumericCalculationNode(NumericValue value)
  69. : CalculationNode(Type::Numeric)
  70. , m_value(move(value))
  71. {
  72. }
  73. NumericCalculationNode::~NumericCalculationNode() = default;
  74. ErrorOr<String> NumericCalculationNode::to_string() const
  75. {
  76. return m_value.visit([](auto& value) { return value.to_string(); });
  77. }
  78. Optional<CalculatedStyleValue::ResolvedType> NumericCalculationNode::resolved_type() const
  79. {
  80. return m_value.visit(
  81. [](Number const&) { return CalculatedStyleValue::ResolvedType::Number; },
  82. [](Angle const&) { return CalculatedStyleValue::ResolvedType::Angle; },
  83. [](Frequency const&) { return CalculatedStyleValue::ResolvedType::Frequency; },
  84. [](Length const&) { return CalculatedStyleValue::ResolvedType::Length; },
  85. [](Percentage const&) { return CalculatedStyleValue::ResolvedType::Percentage; },
  86. [](Time const&) { return CalculatedStyleValue::ResolvedType::Time; });
  87. }
  88. bool NumericCalculationNode::contains_percentage() const
  89. {
  90. return m_value.has<Percentage>();
  91. }
  92. CalculatedStyleValue::CalculationResult NumericCalculationNode::resolve(Optional<Length::ResolutionContext const&>, CalculatedStyleValue::PercentageBasis const&) const
  93. {
  94. return m_value;
  95. }
  96. ErrorOr<void> NumericCalculationNode::dump(StringBuilder& builder, int indent) const
  97. {
  98. return builder.try_appendff("{: >{}}NUMERIC({})\n", "", indent, TRY(m_value.visit([](auto& it) { return it.to_string(); })));
  99. }
  100. ErrorOr<NonnullOwnPtr<SumCalculationNode>> SumCalculationNode::create(Vector<NonnullOwnPtr<CalculationNode>> values)
  101. {
  102. return adopt_nonnull_own_or_enomem(new (nothrow) SumCalculationNode(move(values)));
  103. }
  104. SumCalculationNode::SumCalculationNode(Vector<NonnullOwnPtr<CalculationNode>> values)
  105. : CalculationNode(Type::Sum)
  106. , m_values(move(values))
  107. {
  108. VERIFY(!m_values.is_empty());
  109. }
  110. SumCalculationNode::~SumCalculationNode() = default;
  111. ErrorOr<String> SumCalculationNode::to_string() const
  112. {
  113. bool first = true;
  114. StringBuilder builder;
  115. for (auto& value : m_values) {
  116. if (!first)
  117. TRY(builder.try_append(" + "sv));
  118. TRY(builder.try_append(TRY(value->to_string())));
  119. first = false;
  120. }
  121. return builder.to_string();
  122. }
  123. Optional<CalculatedStyleValue::ResolvedType> SumCalculationNode::resolved_type() const
  124. {
  125. // FIXME: Implement https://www.w3.org/TR/css-values-4/#determine-the-type-of-a-calculation
  126. // For now, this is just ad-hoc, based on the old implementation.
  127. Optional<CalculatedStyleValue::ResolvedType> type;
  128. for (auto const& value : m_values) {
  129. auto maybe_value_type = value->resolved_type();
  130. if (!maybe_value_type.has_value())
  131. return {};
  132. auto value_type = maybe_value_type.value();
  133. if (!type.has_value()) {
  134. type = value_type;
  135. continue;
  136. }
  137. // At + or -, check that both sides have the same type, or that one side is a <number> and the other is an <integer>.
  138. // If both sides are the same type, resolve to that type.
  139. if (value_type == type)
  140. continue;
  141. // If one side is a <number> and the other is an <integer>, resolve to <number>.
  142. if (is_number(*type) && is_number(value_type)) {
  143. type = CalculatedStyleValue::ResolvedType::Number;
  144. continue;
  145. }
  146. // FIXME: calc() handles <percentage> by allowing them to pretend to be whatever <dimension> type is allowed at this location.
  147. // Since we can't easily check what that type is, we just allow <percentage> to combine with any other <dimension> type.
  148. if (type == CalculatedStyleValue::ResolvedType::Percentage && is_dimension(value_type)) {
  149. type = value_type;
  150. continue;
  151. }
  152. if (is_dimension(*type) && value_type == CalculatedStyleValue::ResolvedType::Percentage)
  153. continue;
  154. return {};
  155. }
  156. return type;
  157. }
  158. bool SumCalculationNode::contains_percentage() const
  159. {
  160. for (auto const& value : m_values) {
  161. if (value->contains_percentage())
  162. return true;
  163. }
  164. return false;
  165. }
  166. CalculatedStyleValue::CalculationResult SumCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  167. {
  168. Optional<CalculatedStyleValue::CalculationResult> total;
  169. for (auto& additional_product : m_values) {
  170. auto additional_value = additional_product->resolve(context, percentage_basis);
  171. if (!total.has_value()) {
  172. total = additional_value;
  173. continue;
  174. }
  175. total->add(additional_value, context, percentage_basis);
  176. }
  177. return total.value();
  178. }
  179. ErrorOr<void> SumCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  180. {
  181. for (auto& item : m_values) {
  182. TRY(item->for_each_child_node(callback));
  183. TRY(callback(item));
  184. }
  185. return {};
  186. }
  187. ErrorOr<void> SumCalculationNode::dump(StringBuilder& builder, int indent) const
  188. {
  189. TRY(builder.try_appendff("{: >{}}SUM:\n", "", indent));
  190. for (auto const& item : m_values)
  191. TRY(item->dump(builder, indent + 2));
  192. return {};
  193. }
  194. ErrorOr<NonnullOwnPtr<ProductCalculationNode>> ProductCalculationNode::create(Vector<NonnullOwnPtr<CalculationNode>> values)
  195. {
  196. return adopt_nonnull_own_or_enomem(new (nothrow) ProductCalculationNode(move(values)));
  197. }
  198. ProductCalculationNode::ProductCalculationNode(Vector<NonnullOwnPtr<CalculationNode>> values)
  199. : CalculationNode(Type::Product)
  200. , m_values(move(values))
  201. {
  202. VERIFY(!m_values.is_empty());
  203. }
  204. ProductCalculationNode::~ProductCalculationNode() = default;
  205. ErrorOr<String> ProductCalculationNode::to_string() const
  206. {
  207. bool first = true;
  208. StringBuilder builder;
  209. for (auto& value : m_values) {
  210. if (!first)
  211. TRY(builder.try_append(" * "sv));
  212. TRY(builder.try_append(TRY(value->to_string())));
  213. first = false;
  214. }
  215. return builder.to_string();
  216. }
  217. Optional<CalculatedStyleValue::ResolvedType> ProductCalculationNode::resolved_type() const
  218. {
  219. // FIXME: Implement https://www.w3.org/TR/css-values-4/#determine-the-type-of-a-calculation
  220. // For now, this is just ad-hoc, based on the old implementation.
  221. Optional<CalculatedStyleValue::ResolvedType> type;
  222. for (auto const& value : m_values) {
  223. auto maybe_value_type = value->resolved_type();
  224. if (!maybe_value_type.has_value())
  225. return {};
  226. auto value_type = maybe_value_type.value();
  227. if (!type.has_value()) {
  228. type = value_type;
  229. continue;
  230. }
  231. // At *, check that at least one side is <number>.
  232. if (!(is_number(*type) || is_number(value_type)))
  233. return {};
  234. // If both sides are <integer>, resolve to <integer>.
  235. if (type == CalculatedStyleValue::ResolvedType::Integer && value_type == CalculatedStyleValue::ResolvedType::Integer) {
  236. type = CalculatedStyleValue::ResolvedType::Integer;
  237. } else {
  238. // Otherwise, resolve to the type of the other side.
  239. if (is_number(*type))
  240. type = value_type;
  241. }
  242. }
  243. return type;
  244. }
  245. bool ProductCalculationNode::contains_percentage() const
  246. {
  247. for (auto const& value : m_values) {
  248. if (value->contains_percentage())
  249. return true;
  250. }
  251. return false;
  252. }
  253. CalculatedStyleValue::CalculationResult ProductCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  254. {
  255. Optional<CalculatedStyleValue::CalculationResult> total;
  256. for (auto& additional_product : m_values) {
  257. auto additional_value = additional_product->resolve(context, percentage_basis);
  258. if (!total.has_value()) {
  259. total = additional_value;
  260. continue;
  261. }
  262. total->multiply_by(additional_value, context);
  263. }
  264. return total.value();
  265. }
  266. ErrorOr<void> ProductCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  267. {
  268. for (auto& item : m_values) {
  269. TRY(item->for_each_child_node(callback));
  270. TRY(callback(item));
  271. }
  272. return {};
  273. }
  274. ErrorOr<void> ProductCalculationNode::dump(StringBuilder& builder, int indent) const
  275. {
  276. TRY(builder.try_appendff("{: >{}}PRODUCT:\n", "", indent));
  277. for (auto const& item : m_values)
  278. TRY(item->dump(builder, indent + 2));
  279. return {};
  280. }
  281. ErrorOr<NonnullOwnPtr<NegateCalculationNode>> NegateCalculationNode::create(NonnullOwnPtr<Web::CSS::CalculationNode> value)
  282. {
  283. return adopt_nonnull_own_or_enomem(new (nothrow) NegateCalculationNode(move(value)));
  284. }
  285. NegateCalculationNode::NegateCalculationNode(NonnullOwnPtr<CalculationNode> value)
  286. : CalculationNode(Type::Negate)
  287. , m_value(move(value))
  288. {
  289. }
  290. NegateCalculationNode::~NegateCalculationNode() = default;
  291. ErrorOr<String> NegateCalculationNode::to_string() const
  292. {
  293. return String::formatted("(0 - {})", TRY(m_value->to_string()));
  294. }
  295. Optional<CalculatedStyleValue::ResolvedType> NegateCalculationNode::resolved_type() const
  296. {
  297. return m_value->resolved_type();
  298. }
  299. bool NegateCalculationNode::contains_percentage() const
  300. {
  301. return m_value->contains_percentage();
  302. }
  303. CalculatedStyleValue::CalculationResult NegateCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  304. {
  305. auto child_value = m_value->resolve(context, percentage_basis);
  306. child_value.negate();
  307. return child_value;
  308. }
  309. ErrorOr<void> NegateCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  310. {
  311. TRY(m_value->for_each_child_node(callback));
  312. TRY(callback(m_value));
  313. return {};
  314. }
  315. ErrorOr<void> NegateCalculationNode::dump(StringBuilder& builder, int indent) const
  316. {
  317. TRY(builder.try_appendff("{: >{}}NEGATE:\n", "", indent));
  318. TRY(m_value->dump(builder, indent + 2));
  319. return {};
  320. }
  321. ErrorOr<NonnullOwnPtr<InvertCalculationNode>> InvertCalculationNode::create(NonnullOwnPtr<Web::CSS::CalculationNode> value)
  322. {
  323. return adopt_nonnull_own_or_enomem(new (nothrow) InvertCalculationNode(move(value)));
  324. }
  325. InvertCalculationNode::InvertCalculationNode(NonnullOwnPtr<CalculationNode> value)
  326. : CalculationNode(Type::Invert)
  327. , m_value(move(value))
  328. {
  329. }
  330. InvertCalculationNode::~InvertCalculationNode() = default;
  331. ErrorOr<String> InvertCalculationNode::to_string() const
  332. {
  333. return String::formatted("(1 / {})", TRY(m_value->to_string()));
  334. }
  335. Optional<CalculatedStyleValue::ResolvedType> InvertCalculationNode::resolved_type() const
  336. {
  337. auto type = m_value->resolved_type();
  338. if (type == CalculatedStyleValue::ResolvedType::Integer)
  339. return CalculatedStyleValue::ResolvedType::Number;
  340. return type;
  341. }
  342. bool InvertCalculationNode::contains_percentage() const
  343. {
  344. return m_value->contains_percentage();
  345. }
  346. CalculatedStyleValue::CalculationResult InvertCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  347. {
  348. auto child_value = m_value->resolve(context, percentage_basis);
  349. child_value.invert();
  350. return child_value;
  351. }
  352. ErrorOr<void> InvertCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  353. {
  354. TRY(m_value->for_each_child_node(callback));
  355. TRY(callback(m_value));
  356. return {};
  357. }
  358. ErrorOr<void> InvertCalculationNode::dump(StringBuilder& builder, int indent) const
  359. {
  360. TRY(builder.try_appendff("{: >{}}INVERT:\n", "", indent));
  361. TRY(m_value->dump(builder, indent + 2));
  362. return {};
  363. }
  364. ErrorOr<NonnullOwnPtr<MinCalculationNode>> MinCalculationNode::create(Vector<NonnullOwnPtr<Web::CSS::CalculationNode>> values)
  365. {
  366. return adopt_nonnull_own_or_enomem(new (nothrow) MinCalculationNode(move(values)));
  367. }
  368. MinCalculationNode::MinCalculationNode(Vector<NonnullOwnPtr<CalculationNode>> values)
  369. : CalculationNode(Type::Min)
  370. , m_values(move(values))
  371. {
  372. }
  373. MinCalculationNode::~MinCalculationNode() = default;
  374. ErrorOr<String> MinCalculationNode::to_string() const
  375. {
  376. StringBuilder builder;
  377. TRY(builder.try_append("min("sv));
  378. for (size_t i = 0; i < m_values.size(); ++i) {
  379. if (i != 0)
  380. TRY(builder.try_append(", "sv));
  381. TRY(builder.try_append(TRY(m_values[i]->to_string())));
  382. }
  383. TRY(builder.try_append(")"sv));
  384. return builder.to_string();
  385. }
  386. Optional<CalculatedStyleValue::ResolvedType> MinCalculationNode::resolved_type() const
  387. {
  388. // NOTE: We check during parsing that all values have the same type.
  389. return m_values[0]->resolved_type();
  390. }
  391. bool MinCalculationNode::contains_percentage() const
  392. {
  393. for (auto const& value : m_values) {
  394. if (value->contains_percentage())
  395. return true;
  396. }
  397. return false;
  398. }
  399. CalculatedStyleValue::CalculationResult MinCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  400. {
  401. CalculatedStyleValue::CalculationResult smallest_node = m_values.first()->resolve(context, percentage_basis);
  402. auto smallest_value = resolve_value(smallest_node.value(), context);
  403. for (size_t i = 1; i < m_values.size(); i++) {
  404. auto child_resolved = m_values[i]->resolve(context, percentage_basis);
  405. auto child_value = resolve_value(child_resolved.value(), context);
  406. if (child_value < smallest_value) {
  407. smallest_value = child_value;
  408. smallest_node = child_resolved;
  409. }
  410. }
  411. return smallest_node;
  412. }
  413. ErrorOr<void> MinCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  414. {
  415. for (auto& value : m_values) {
  416. TRY(value->for_each_child_node(callback));
  417. TRY(callback(value));
  418. }
  419. return {};
  420. }
  421. ErrorOr<void> MinCalculationNode::dump(StringBuilder& builder, int indent) const
  422. {
  423. TRY(builder.try_appendff("{: >{}}MIN:\n", "", indent));
  424. for (auto const& value : m_values)
  425. TRY(value->dump(builder, indent + 2));
  426. return {};
  427. }
  428. ErrorOr<NonnullOwnPtr<MaxCalculationNode>> MaxCalculationNode::create(Vector<NonnullOwnPtr<Web::CSS::CalculationNode>> values)
  429. {
  430. return adopt_nonnull_own_or_enomem(new (nothrow) MaxCalculationNode(move(values)));
  431. }
  432. MaxCalculationNode::MaxCalculationNode(Vector<NonnullOwnPtr<CalculationNode>> values)
  433. : CalculationNode(Type::Max)
  434. , m_values(move(values))
  435. {
  436. }
  437. MaxCalculationNode::~MaxCalculationNode() = default;
  438. ErrorOr<String> MaxCalculationNode::to_string() const
  439. {
  440. StringBuilder builder;
  441. TRY(builder.try_append("max("sv));
  442. for (size_t i = 0; i < m_values.size(); ++i) {
  443. if (i != 0)
  444. TRY(builder.try_append(", "sv));
  445. TRY(builder.try_append(TRY(m_values[i]->to_string())));
  446. }
  447. TRY(builder.try_append(")"sv));
  448. return builder.to_string();
  449. }
  450. Optional<CalculatedStyleValue::ResolvedType> MaxCalculationNode::resolved_type() const
  451. {
  452. // NOTE: We check during parsing that all values have the same type.
  453. return m_values[0]->resolved_type();
  454. }
  455. bool MaxCalculationNode::contains_percentage() const
  456. {
  457. for (auto const& value : m_values) {
  458. if (value->contains_percentage())
  459. return true;
  460. }
  461. return false;
  462. }
  463. CalculatedStyleValue::CalculationResult MaxCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  464. {
  465. CalculatedStyleValue::CalculationResult largest_node = m_values.first()->resolve(context, percentage_basis);
  466. auto largest_value = resolve_value(largest_node.value(), context);
  467. for (size_t i = 1; i < m_values.size(); i++) {
  468. auto child_resolved = m_values[i]->resolve(context, percentage_basis);
  469. auto child_value = resolve_value(child_resolved.value(), context);
  470. if (child_value > largest_value) {
  471. largest_value = child_value;
  472. largest_node = child_resolved;
  473. }
  474. }
  475. return largest_node;
  476. }
  477. ErrorOr<void> MaxCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  478. {
  479. for (auto& value : m_values) {
  480. TRY(value->for_each_child_node(callback));
  481. TRY(callback(value));
  482. }
  483. return {};
  484. }
  485. ErrorOr<void> MaxCalculationNode::dump(StringBuilder& builder, int indent) const
  486. {
  487. TRY(builder.try_appendff("{: >{}}MAX:\n", "", indent));
  488. for (auto const& value : m_values)
  489. TRY(value->dump(builder, indent + 2));
  490. return {};
  491. }
  492. ErrorOr<NonnullOwnPtr<ClampCalculationNode>> ClampCalculationNode::create(NonnullOwnPtr<CalculationNode> min, NonnullOwnPtr<CalculationNode> center, NonnullOwnPtr<CalculationNode> max)
  493. {
  494. return adopt_nonnull_own_or_enomem(new (nothrow) ClampCalculationNode(move(min), move(center), move(max)));
  495. }
  496. ClampCalculationNode::ClampCalculationNode(NonnullOwnPtr<CalculationNode> min, NonnullOwnPtr<CalculationNode> center, NonnullOwnPtr<CalculationNode> max)
  497. : CalculationNode(Type::Clamp)
  498. , m_min_value(move(min))
  499. , m_center_value(move(center))
  500. , m_max_value(move(max))
  501. {
  502. }
  503. ClampCalculationNode::~ClampCalculationNode() = default;
  504. ErrorOr<String> ClampCalculationNode::to_string() const
  505. {
  506. StringBuilder builder;
  507. TRY(builder.try_append("clamp("sv));
  508. TRY(builder.try_append(TRY(m_min_value->to_string())));
  509. TRY(builder.try_append(", "sv));
  510. TRY(builder.try_append(TRY(m_center_value->to_string())));
  511. TRY(builder.try_append(", "sv));
  512. TRY(builder.try_append(TRY(m_max_value->to_string())));
  513. TRY(builder.try_append(")"sv));
  514. return builder.to_string();
  515. }
  516. Optional<CalculatedStyleValue::ResolvedType> ClampCalculationNode::resolved_type() const
  517. {
  518. // NOTE: We check during parsing that all values have the same type.
  519. return m_min_value->resolved_type();
  520. }
  521. bool ClampCalculationNode::contains_percentage() const
  522. {
  523. return m_min_value->contains_percentage() || m_center_value->contains_percentage() || m_max_value->contains_percentage();
  524. }
  525. CalculatedStyleValue::CalculationResult ClampCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  526. {
  527. auto min_node = m_min_value->resolve(context, percentage_basis);
  528. auto center_node = m_center_value->resolve(context, percentage_basis);
  529. auto max_node = m_max_value->resolve(context, percentage_basis);
  530. auto min_value = resolve_value(min_node.value(), context);
  531. auto center_value = resolve_value(center_node.value(), context);
  532. auto max_value = resolve_value(max_node.value(), context);
  533. // NOTE: The value should be returned as "max(MIN, min(VAL, MAX))"
  534. auto chosen_value = max(min_value, min(center_value, max_value));
  535. if (chosen_value == min_value)
  536. return min_node;
  537. if (chosen_value == center_value)
  538. return center_node;
  539. if (chosen_value == max_value)
  540. return max_node;
  541. VERIFY_NOT_REACHED();
  542. }
  543. ErrorOr<void> ClampCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  544. {
  545. TRY(m_min_value->for_each_child_node(callback));
  546. TRY(m_center_value->for_each_child_node(callback));
  547. TRY(m_max_value->for_each_child_node(callback));
  548. TRY(callback(m_min_value));
  549. TRY(callback(m_center_value));
  550. TRY(callback(m_max_value));
  551. return {};
  552. }
  553. ErrorOr<void> ClampCalculationNode::dump(StringBuilder& builder, int indent) const
  554. {
  555. TRY(builder.try_appendff("{: >{}}CLAMP:\n", "", indent));
  556. TRY(m_min_value->dump(builder, indent + 2));
  557. TRY(m_center_value->dump(builder, indent + 2));
  558. TRY(m_max_value->dump(builder, indent + 2));
  559. return {};
  560. }
  561. ErrorOr<NonnullOwnPtr<AbsCalculationNode>> AbsCalculationNode::create(NonnullOwnPtr<CalculationNode> value)
  562. {
  563. return adopt_nonnull_own_or_enomem(new (nothrow) AbsCalculationNode(move(value)));
  564. }
  565. AbsCalculationNode::AbsCalculationNode(NonnullOwnPtr<CalculationNode> value)
  566. : CalculationNode(Type::Abs)
  567. , m_value(move(value))
  568. {
  569. }
  570. AbsCalculationNode::~AbsCalculationNode() = default;
  571. ErrorOr<String> AbsCalculationNode::to_string() const
  572. {
  573. StringBuilder builder;
  574. builder.append("abs("sv);
  575. builder.append(TRY(m_value->to_string()));
  576. builder.append(")"sv);
  577. return builder.to_string();
  578. }
  579. Optional<CalculatedStyleValue::ResolvedType> AbsCalculationNode::resolved_type() const
  580. {
  581. return m_value->resolved_type();
  582. }
  583. bool AbsCalculationNode::contains_percentage() const
  584. {
  585. return m_value->contains_percentage();
  586. }
  587. CalculatedStyleValue::CalculationResult AbsCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  588. {
  589. auto resolved_type = m_value->resolved_type().value();
  590. auto node_a = m_value->resolve(context, percentage_basis);
  591. auto node_a_value = resolve_value(node_a.value(), context);
  592. if (node_a_value < 0)
  593. return to_resolved_type(resolved_type, -node_a_value);
  594. return node_a;
  595. }
  596. ErrorOr<void> AbsCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  597. {
  598. TRY(m_value->for_each_child_node(callback));
  599. TRY(callback(m_value));
  600. return {};
  601. }
  602. ErrorOr<void> AbsCalculationNode::dump(StringBuilder& builder, int indent) const
  603. {
  604. TRY(builder.try_appendff("{: >{}}ABS: {}\n", "", indent, TRY(to_string())));
  605. return {};
  606. }
  607. ErrorOr<NonnullOwnPtr<SignCalculationNode>> SignCalculationNode::create(NonnullOwnPtr<CalculationNode> value)
  608. {
  609. return adopt_nonnull_own_or_enomem(new (nothrow) SignCalculationNode(move(value)));
  610. }
  611. SignCalculationNode::SignCalculationNode(NonnullOwnPtr<CalculationNode> value)
  612. : CalculationNode(Type::Sign)
  613. , m_value(move(value))
  614. {
  615. }
  616. SignCalculationNode::~SignCalculationNode() = default;
  617. ErrorOr<String> SignCalculationNode::to_string() const
  618. {
  619. StringBuilder builder;
  620. builder.append("sign("sv);
  621. builder.append(TRY(m_value->to_string()));
  622. builder.append(")"sv);
  623. return builder.to_string();
  624. }
  625. Optional<CalculatedStyleValue::ResolvedType> SignCalculationNode::resolved_type() const
  626. {
  627. return CalculatedStyleValue::ResolvedType::Integer;
  628. }
  629. bool SignCalculationNode::contains_percentage() const
  630. {
  631. return m_value->contains_percentage();
  632. }
  633. CalculatedStyleValue::CalculationResult SignCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  634. {
  635. auto node_a = m_value->resolve(context, percentage_basis);
  636. auto node_a_value = resolve_value(node_a.value(), context);
  637. if (node_a_value < 0)
  638. return { Number(Number::Type::Integer, -1) };
  639. if (node_a_value > 0)
  640. return { Number(Number::Type::Integer, 1) };
  641. return { Number(Number::Type::Integer, 0) };
  642. }
  643. ErrorOr<void> SignCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  644. {
  645. TRY(m_value->for_each_child_node(callback));
  646. TRY(callback(m_value));
  647. return {};
  648. }
  649. ErrorOr<void> SignCalculationNode::dump(StringBuilder& builder, int indent) const
  650. {
  651. TRY(builder.try_appendff("{: >{}}SIGN: {}\n", "", indent, TRY(to_string())));
  652. return {};
  653. }
  654. ErrorOr<NonnullOwnPtr<ConstantCalculationNode>> ConstantCalculationNode::create(ConstantType constant)
  655. {
  656. return adopt_nonnull_own_or_enomem(new (nothrow) ConstantCalculationNode(constant));
  657. }
  658. ConstantCalculationNode::ConstantCalculationNode(ConstantType constant)
  659. : CalculationNode(Type::Constant)
  660. , m_constant(constant)
  661. {
  662. }
  663. ConstantCalculationNode::~ConstantCalculationNode() = default;
  664. ErrorOr<String> ConstantCalculationNode::to_string() const
  665. {
  666. switch (m_constant) {
  667. case CalculationNode::ConstantType::E:
  668. return "e"_short_string;
  669. case CalculationNode::ConstantType::PI:
  670. return "pi"_short_string;
  671. case CalculationNode::ConstantType::Infinity:
  672. return "infinity"_string;
  673. case CalculationNode::ConstantType::MinusInfinity:
  674. return "-infinity"_string;
  675. case CalculationNode::ConstantType::NaN:
  676. return "NaN"_string;
  677. }
  678. VERIFY_NOT_REACHED();
  679. }
  680. Optional<CalculatedStyleValue::ResolvedType> ConstantCalculationNode::resolved_type() const
  681. {
  682. return CalculatedStyleValue::ResolvedType::Number;
  683. }
  684. CalculatedStyleValue::CalculationResult ConstantCalculationNode::resolve([[maybe_unused]] Optional<Length::ResolutionContext const&> context, [[maybe_unused]] CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  685. {
  686. switch (m_constant) {
  687. case CalculationNode::ConstantType::E:
  688. return { Number(Number::Type::Number, M_E) };
  689. case CalculationNode::ConstantType::PI:
  690. return { Number(Number::Type::Number, M_PI) };
  691. // FIXME: We need to keep track of Infinity and NaN across all nodes, since they require special handling.
  692. case CalculationNode::ConstantType::Infinity:
  693. return { Number(Number::Type::Number, NumericLimits<float>::max()) };
  694. case CalculationNode::ConstantType::MinusInfinity:
  695. return { Number(Number::Type::Number, NumericLimits<float>::lowest()) };
  696. case CalculationNode::ConstantType::NaN:
  697. return { Number(Number::Type::Number, NAN) };
  698. }
  699. VERIFY_NOT_REACHED();
  700. }
  701. ErrorOr<void> ConstantCalculationNode::for_each_child_node([[maybe_unused]] Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  702. {
  703. return {};
  704. }
  705. ErrorOr<void> ConstantCalculationNode::dump(StringBuilder& builder, int indent) const
  706. {
  707. TRY(builder.try_appendff("{: >{}}CONSTANT: {}\n", "", indent, TRY(to_string())));
  708. return {};
  709. }
  710. ErrorOr<NonnullOwnPtr<SinCalculationNode>> SinCalculationNode::create(NonnullOwnPtr<CalculationNode> value)
  711. {
  712. return adopt_nonnull_own_or_enomem(new (nothrow) SinCalculationNode(move(value)));
  713. }
  714. SinCalculationNode::SinCalculationNode(NonnullOwnPtr<CalculationNode> value)
  715. : CalculationNode(Type::Sin)
  716. , m_value(move(value))
  717. {
  718. }
  719. SinCalculationNode::~SinCalculationNode() = default;
  720. ErrorOr<String> SinCalculationNode::to_string() const
  721. {
  722. StringBuilder builder;
  723. builder.append("sin("sv);
  724. builder.append(TRY(m_value->to_string()));
  725. builder.append(")"sv);
  726. return builder.to_string();
  727. }
  728. Optional<CalculatedStyleValue::ResolvedType> SinCalculationNode::resolved_type() const
  729. {
  730. return CalculatedStyleValue::ResolvedType::Number;
  731. }
  732. bool SinCalculationNode::contains_percentage() const
  733. {
  734. return m_value->contains_percentage();
  735. }
  736. CalculatedStyleValue::CalculationResult SinCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  737. {
  738. auto node_a = m_value->resolve(context, percentage_basis);
  739. auto node_a_value = resolve_value_radians(node_a.value());
  740. auto result = sin(node_a_value);
  741. return { Number(Number::Type::Number, result) };
  742. }
  743. ErrorOr<void> SinCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  744. {
  745. TRY(m_value->for_each_child_node(callback));
  746. TRY(callback(m_value));
  747. return {};
  748. }
  749. ErrorOr<void> SinCalculationNode::dump(StringBuilder& builder, int indent) const
  750. {
  751. TRY(builder.try_appendff("{: >{}}SIN: {}\n", "", indent, TRY(to_string())));
  752. return {};
  753. }
  754. ErrorOr<NonnullOwnPtr<CosCalculationNode>> CosCalculationNode::create(NonnullOwnPtr<CalculationNode> value)
  755. {
  756. return adopt_nonnull_own_or_enomem(new (nothrow) CosCalculationNode(move(value)));
  757. }
  758. CosCalculationNode::CosCalculationNode(NonnullOwnPtr<CalculationNode> value)
  759. : CalculationNode(Type::Cos)
  760. , m_value(move(value))
  761. {
  762. }
  763. CosCalculationNode::~CosCalculationNode() = default;
  764. ErrorOr<String> CosCalculationNode::to_string() const
  765. {
  766. StringBuilder builder;
  767. builder.append("cos("sv);
  768. builder.append(TRY(m_value->to_string()));
  769. builder.append(")"sv);
  770. return builder.to_string();
  771. }
  772. Optional<CalculatedStyleValue::ResolvedType> CosCalculationNode::resolved_type() const
  773. {
  774. return CalculatedStyleValue::ResolvedType::Number;
  775. }
  776. bool CosCalculationNode::contains_percentage() const
  777. {
  778. return m_value->contains_percentage();
  779. }
  780. CalculatedStyleValue::CalculationResult CosCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  781. {
  782. auto node_a = m_value->resolve(context, percentage_basis);
  783. auto node_a_value = resolve_value_radians(node_a.value());
  784. auto result = cos(node_a_value);
  785. return { Number(Number::Type::Number, result) };
  786. }
  787. ErrorOr<void> CosCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  788. {
  789. TRY(m_value->for_each_child_node(callback));
  790. TRY(callback(m_value));
  791. return {};
  792. }
  793. ErrorOr<void> CosCalculationNode::dump(StringBuilder& builder, int indent) const
  794. {
  795. TRY(builder.try_appendff("{: >{}}COS: {}\n", "", indent, TRY(to_string())));
  796. return {};
  797. }
  798. ErrorOr<NonnullOwnPtr<TanCalculationNode>> TanCalculationNode::create(NonnullOwnPtr<CalculationNode> value)
  799. {
  800. return adopt_nonnull_own_or_enomem(new (nothrow) TanCalculationNode(move(value)));
  801. }
  802. TanCalculationNode::TanCalculationNode(NonnullOwnPtr<CalculationNode> value)
  803. : CalculationNode(Type::Tan)
  804. , m_value(move(value))
  805. {
  806. }
  807. TanCalculationNode::~TanCalculationNode() = default;
  808. ErrorOr<String> TanCalculationNode::to_string() const
  809. {
  810. StringBuilder builder;
  811. builder.append("tan("sv);
  812. builder.append(TRY(m_value->to_string()));
  813. builder.append(")"sv);
  814. return builder.to_string();
  815. }
  816. Optional<CalculatedStyleValue::ResolvedType> TanCalculationNode::resolved_type() const
  817. {
  818. return CalculatedStyleValue::ResolvedType::Number;
  819. }
  820. bool TanCalculationNode::contains_percentage() const
  821. {
  822. return m_value->contains_percentage();
  823. }
  824. CalculatedStyleValue::CalculationResult TanCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  825. {
  826. auto node_a = m_value->resolve(context, percentage_basis);
  827. auto node_a_value = resolve_value_radians(node_a.value());
  828. auto result = tan(node_a_value);
  829. return { Number(Number::Type::Number, result) };
  830. }
  831. ErrorOr<void> TanCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  832. {
  833. TRY(m_value->for_each_child_node(callback));
  834. TRY(callback(m_value));
  835. return {};
  836. }
  837. ErrorOr<void> TanCalculationNode::dump(StringBuilder& builder, int indent) const
  838. {
  839. TRY(builder.try_appendff("{: >{}}TAN: {}\n", "", indent, TRY(to_string())));
  840. return {};
  841. }
  842. ErrorOr<NonnullOwnPtr<AsinCalculationNode>> AsinCalculationNode::create(NonnullOwnPtr<CalculationNode> value)
  843. {
  844. return adopt_nonnull_own_or_enomem(new (nothrow) AsinCalculationNode(move(value)));
  845. }
  846. AsinCalculationNode::AsinCalculationNode(NonnullOwnPtr<CalculationNode> value)
  847. : CalculationNode(Type::Asin)
  848. , m_value(move(value))
  849. {
  850. }
  851. AsinCalculationNode::~AsinCalculationNode() = default;
  852. ErrorOr<String> AsinCalculationNode::to_string() const
  853. {
  854. StringBuilder builder;
  855. builder.append("asin("sv);
  856. builder.append(TRY(m_value->to_string()));
  857. builder.append(")"sv);
  858. return builder.to_string();
  859. }
  860. Optional<CalculatedStyleValue::ResolvedType> AsinCalculationNode::resolved_type() const
  861. {
  862. return CalculatedStyleValue::ResolvedType::Angle;
  863. }
  864. bool AsinCalculationNode::contains_percentage() const
  865. {
  866. return m_value->contains_percentage();
  867. }
  868. CalculatedStyleValue::CalculationResult AsinCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  869. {
  870. auto node_a = m_value->resolve(context, percentage_basis);
  871. auto node_a_value = resolve_value(node_a.value(), context);
  872. auto result = asin(node_a_value);
  873. return { Angle(result, Angle::Type::Rad) };
  874. }
  875. ErrorOr<void> AsinCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  876. {
  877. TRY(m_value->for_each_child_node(callback));
  878. TRY(callback(m_value));
  879. return {};
  880. }
  881. ErrorOr<void> AsinCalculationNode::dump(StringBuilder& builder, int indent) const
  882. {
  883. TRY(builder.try_appendff("{: >{}}ASIN: {}\n", "", indent, TRY(to_string())));
  884. return {};
  885. }
  886. ErrorOr<NonnullOwnPtr<AcosCalculationNode>> AcosCalculationNode::create(NonnullOwnPtr<CalculationNode> value)
  887. {
  888. return adopt_nonnull_own_or_enomem(new (nothrow) AcosCalculationNode(move(value)));
  889. }
  890. AcosCalculationNode::AcosCalculationNode(NonnullOwnPtr<CalculationNode> value)
  891. : CalculationNode(Type::Acos)
  892. , m_value(move(value))
  893. {
  894. }
  895. AcosCalculationNode::~AcosCalculationNode() = default;
  896. ErrorOr<String> AcosCalculationNode::to_string() const
  897. {
  898. StringBuilder builder;
  899. builder.append("acos("sv);
  900. builder.append(TRY(m_value->to_string()));
  901. builder.append(")"sv);
  902. return builder.to_string();
  903. }
  904. Optional<CalculatedStyleValue::ResolvedType> AcosCalculationNode::resolved_type() const
  905. {
  906. return CalculatedStyleValue::ResolvedType::Angle;
  907. }
  908. bool AcosCalculationNode::contains_percentage() const
  909. {
  910. return m_value->contains_percentage();
  911. }
  912. CalculatedStyleValue::CalculationResult AcosCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  913. {
  914. auto node_a = m_value->resolve(context, percentage_basis);
  915. auto node_a_value = resolve_value(node_a.value(), context);
  916. auto result = acos(node_a_value);
  917. return { Angle(result, Angle::Type::Rad) };
  918. }
  919. ErrorOr<void> AcosCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  920. {
  921. TRY(m_value->for_each_child_node(callback));
  922. TRY(callback(m_value));
  923. return {};
  924. }
  925. ErrorOr<void> AcosCalculationNode::dump(StringBuilder& builder, int indent) const
  926. {
  927. TRY(builder.try_appendff("{: >{}}ACOS: {}\n", "", indent, TRY(to_string())));
  928. return {};
  929. }
  930. ErrorOr<NonnullOwnPtr<AtanCalculationNode>> AtanCalculationNode::create(NonnullOwnPtr<CalculationNode> value)
  931. {
  932. return adopt_nonnull_own_or_enomem(new (nothrow) AtanCalculationNode(move(value)));
  933. }
  934. AtanCalculationNode::AtanCalculationNode(NonnullOwnPtr<CalculationNode> value)
  935. : CalculationNode(Type::Atan)
  936. , m_value(move(value))
  937. {
  938. }
  939. AtanCalculationNode::~AtanCalculationNode() = default;
  940. ErrorOr<String> AtanCalculationNode::to_string() const
  941. {
  942. StringBuilder builder;
  943. builder.append("atan("sv);
  944. builder.append(TRY(m_value->to_string()));
  945. builder.append(")"sv);
  946. return builder.to_string();
  947. }
  948. Optional<CalculatedStyleValue::ResolvedType> AtanCalculationNode::resolved_type() const
  949. {
  950. return CalculatedStyleValue::ResolvedType::Angle;
  951. }
  952. bool AtanCalculationNode::contains_percentage() const
  953. {
  954. return m_value->contains_percentage();
  955. }
  956. CalculatedStyleValue::CalculationResult AtanCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  957. {
  958. auto node_a = m_value->resolve(context, percentage_basis);
  959. auto node_a_value = resolve_value(node_a.value(), context);
  960. auto result = atan(node_a_value);
  961. return { Angle(result, Angle::Type::Rad) };
  962. }
  963. ErrorOr<void> AtanCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  964. {
  965. TRY(m_value->for_each_child_node(callback));
  966. TRY(callback(m_value));
  967. return {};
  968. }
  969. ErrorOr<void> AtanCalculationNode::dump(StringBuilder& builder, int indent) const
  970. {
  971. TRY(builder.try_appendff("{: >{}}ATAN: {}\n", "", indent, TRY(to_string())));
  972. return {};
  973. }
  974. ErrorOr<NonnullOwnPtr<Atan2CalculationNode>> Atan2CalculationNode::create(NonnullOwnPtr<CalculationNode> y, NonnullOwnPtr<CalculationNode> x)
  975. {
  976. return adopt_nonnull_own_or_enomem(new (nothrow) Atan2CalculationNode(move(y), move(x)));
  977. }
  978. Atan2CalculationNode::Atan2CalculationNode(NonnullOwnPtr<CalculationNode> y, NonnullOwnPtr<CalculationNode> x)
  979. : CalculationNode(Type::Atan2)
  980. , m_y(move(y))
  981. , m_x(move(x))
  982. {
  983. }
  984. Atan2CalculationNode::~Atan2CalculationNode() = default;
  985. ErrorOr<String> Atan2CalculationNode::to_string() const
  986. {
  987. StringBuilder builder;
  988. builder.append("atan2("sv);
  989. builder.append(TRY(m_y->to_string()));
  990. builder.append(", "sv);
  991. builder.append(TRY(m_x->to_string()));
  992. builder.append(")"sv);
  993. return builder.to_string();
  994. }
  995. Optional<CalculatedStyleValue::ResolvedType> Atan2CalculationNode::resolved_type() const
  996. {
  997. return CalculatedStyleValue::ResolvedType::Angle;
  998. }
  999. bool Atan2CalculationNode::contains_percentage() const
  1000. {
  1001. return m_y->contains_percentage() || m_x->contains_percentage();
  1002. }
  1003. CalculatedStyleValue::CalculationResult Atan2CalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  1004. {
  1005. auto node_a = m_y->resolve(context, percentage_basis);
  1006. auto node_a_value = resolve_value(node_a.value(), context);
  1007. auto node_b = m_x->resolve(context, percentage_basis);
  1008. auto node_b_value = resolve_value(node_b.value(), context);
  1009. auto result = atan2(node_a_value, node_b_value);
  1010. return { Angle(result, Angle::Type::Rad) };
  1011. }
  1012. ErrorOr<void> Atan2CalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  1013. {
  1014. TRY(m_y->for_each_child_node(callback));
  1015. TRY(m_x->for_each_child_node(callback));
  1016. TRY(callback(m_y));
  1017. TRY(callback(m_x));
  1018. return {};
  1019. }
  1020. ErrorOr<void> Atan2CalculationNode::dump(StringBuilder& builder, int indent) const
  1021. {
  1022. TRY(builder.try_appendff("{: >{}}ATAN2: {}\n", "", indent, TRY(to_string())));
  1023. return {};
  1024. }
  1025. ErrorOr<NonnullOwnPtr<PowCalculationNode>> PowCalculationNode::create(NonnullOwnPtr<CalculationNode> x, NonnullOwnPtr<CalculationNode> y)
  1026. {
  1027. return adopt_nonnull_own_or_enomem(new (nothrow) PowCalculationNode(move(x), move(y)));
  1028. }
  1029. PowCalculationNode::PowCalculationNode(NonnullOwnPtr<CalculationNode> x, NonnullOwnPtr<CalculationNode> y)
  1030. : CalculationNode(Type::Pow)
  1031. , m_x(move(x))
  1032. , m_y(move(y))
  1033. {
  1034. }
  1035. PowCalculationNode::~PowCalculationNode() = default;
  1036. ErrorOr<String> PowCalculationNode::to_string() const
  1037. {
  1038. StringBuilder builder;
  1039. builder.append("pow("sv);
  1040. builder.append(TRY(m_x->to_string()));
  1041. builder.append(", "sv);
  1042. builder.append(TRY(m_y->to_string()));
  1043. builder.append(")"sv);
  1044. return builder.to_string();
  1045. }
  1046. Optional<CalculatedStyleValue::ResolvedType> PowCalculationNode::resolved_type() const
  1047. {
  1048. return CalculatedStyleValue::ResolvedType::Number;
  1049. }
  1050. CalculatedStyleValue::CalculationResult PowCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  1051. {
  1052. auto node_a = m_x->resolve(context, percentage_basis);
  1053. auto node_a_value = resolve_value(node_a.value(), context);
  1054. auto node_b = m_y->resolve(context, percentage_basis);
  1055. auto node_b_value = resolve_value(node_b.value(), context);
  1056. auto result = pow(node_a_value, node_b_value);
  1057. return { Number(Number::Type::Number, result) };
  1058. }
  1059. ErrorOr<void> PowCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  1060. {
  1061. TRY(m_x->for_each_child_node(callback));
  1062. TRY(m_y->for_each_child_node(callback));
  1063. TRY(callback(m_x));
  1064. TRY(callback(m_y));
  1065. return {};
  1066. }
  1067. ErrorOr<void> PowCalculationNode::dump(StringBuilder& builder, int indent) const
  1068. {
  1069. TRY(builder.try_appendff("{: >{}}POW: {}\n", "", indent, TRY(to_string())));
  1070. return {};
  1071. }
  1072. ErrorOr<NonnullOwnPtr<SqrtCalculationNode>> SqrtCalculationNode::create(NonnullOwnPtr<CalculationNode> value)
  1073. {
  1074. return adopt_nonnull_own_or_enomem(new (nothrow) SqrtCalculationNode(move(value)));
  1075. }
  1076. SqrtCalculationNode::SqrtCalculationNode(NonnullOwnPtr<CalculationNode> value)
  1077. : CalculationNode(Type::Sqrt)
  1078. , m_value(move(value))
  1079. {
  1080. }
  1081. SqrtCalculationNode::~SqrtCalculationNode() = default;
  1082. ErrorOr<String> SqrtCalculationNode::to_string() const
  1083. {
  1084. StringBuilder builder;
  1085. builder.append("sqrt("sv);
  1086. builder.append(TRY(m_value->to_string()));
  1087. builder.append(")"sv);
  1088. return builder.to_string();
  1089. }
  1090. Optional<CalculatedStyleValue::ResolvedType> SqrtCalculationNode::resolved_type() const
  1091. {
  1092. return CalculatedStyleValue::ResolvedType::Number;
  1093. }
  1094. CalculatedStyleValue::CalculationResult SqrtCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  1095. {
  1096. auto node_a = m_value->resolve(context, percentage_basis);
  1097. auto node_a_value = resolve_value(node_a.value(), context);
  1098. auto result = sqrt(node_a_value);
  1099. return { Number(Number::Type::Number, result) };
  1100. }
  1101. ErrorOr<void> SqrtCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  1102. {
  1103. TRY(m_value->for_each_child_node(callback));
  1104. TRY(callback(m_value));
  1105. return {};
  1106. }
  1107. ErrorOr<void> SqrtCalculationNode::dump(StringBuilder& builder, int indent) const
  1108. {
  1109. TRY(builder.try_appendff("{: >{}}SQRT: {}\n", "", indent, TRY(to_string())));
  1110. return {};
  1111. }
  1112. ErrorOr<NonnullOwnPtr<HypotCalculationNode>> HypotCalculationNode::create(Vector<NonnullOwnPtr<Web::CSS::CalculationNode>> values)
  1113. {
  1114. return adopt_nonnull_own_or_enomem(new (nothrow) HypotCalculationNode(move(values)));
  1115. }
  1116. HypotCalculationNode::HypotCalculationNode(Vector<NonnullOwnPtr<CalculationNode>> values)
  1117. : CalculationNode(Type::Hypot)
  1118. , m_values(move(values))
  1119. {
  1120. }
  1121. HypotCalculationNode::~HypotCalculationNode() = default;
  1122. ErrorOr<String> HypotCalculationNode::to_string() const
  1123. {
  1124. StringBuilder builder;
  1125. TRY(builder.try_append("hypot("sv));
  1126. for (size_t i = 0; i < m_values.size(); ++i) {
  1127. if (i != 0)
  1128. TRY(builder.try_append(", "sv));
  1129. TRY(builder.try_append(TRY(m_values[i]->to_string())));
  1130. }
  1131. TRY(builder.try_append(")"sv));
  1132. return builder.to_string();
  1133. }
  1134. Optional<CalculatedStyleValue::ResolvedType> HypotCalculationNode::resolved_type() const
  1135. {
  1136. // NOTE: We check during parsing that all values have the same type.
  1137. return m_values[0]->resolved_type();
  1138. }
  1139. bool HypotCalculationNode::contains_percentage() const
  1140. {
  1141. for (auto const& value : m_values) {
  1142. if (value->contains_percentage())
  1143. return true;
  1144. }
  1145. return false;
  1146. }
  1147. CalculatedStyleValue::CalculationResult HypotCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  1148. {
  1149. double square_sum = 0.0;
  1150. for (auto const& value : m_values) {
  1151. auto child_resolved = value->resolve(context, percentage_basis);
  1152. auto child_value = resolve_value(child_resolved.value(), context);
  1153. square_sum += child_value * child_value;
  1154. }
  1155. auto result = sqrt(square_sum);
  1156. return to_resolved_type(resolved_type().value(), result);
  1157. }
  1158. ErrorOr<void> HypotCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  1159. {
  1160. for (auto& value : m_values) {
  1161. TRY(value->for_each_child_node(callback));
  1162. TRY(callback(value));
  1163. }
  1164. return {};
  1165. }
  1166. ErrorOr<void> HypotCalculationNode::dump(StringBuilder& builder, int indent) const
  1167. {
  1168. TRY(builder.try_appendff("{: >{}}HYPOT:\n", "", indent));
  1169. for (auto const& value : m_values)
  1170. TRY(value->dump(builder, indent + 2));
  1171. return {};
  1172. }
  1173. ErrorOr<NonnullOwnPtr<LogCalculationNode>> LogCalculationNode::create(NonnullOwnPtr<CalculationNode> x, NonnullOwnPtr<CalculationNode> y)
  1174. {
  1175. return adopt_nonnull_own_or_enomem(new (nothrow) LogCalculationNode(move(x), move(y)));
  1176. }
  1177. LogCalculationNode::LogCalculationNode(NonnullOwnPtr<CalculationNode> x, NonnullOwnPtr<CalculationNode> y)
  1178. : CalculationNode(Type::Log)
  1179. , m_x(move(x))
  1180. , m_y(move(y))
  1181. {
  1182. }
  1183. LogCalculationNode::~LogCalculationNode() = default;
  1184. ErrorOr<String> LogCalculationNode::to_string() const
  1185. {
  1186. StringBuilder builder;
  1187. builder.append("log("sv);
  1188. builder.append(TRY(m_x->to_string()));
  1189. builder.append(", "sv);
  1190. builder.append(TRY(m_y->to_string()));
  1191. builder.append(")"sv);
  1192. return builder.to_string();
  1193. }
  1194. Optional<CalculatedStyleValue::ResolvedType> LogCalculationNode::resolved_type() const
  1195. {
  1196. return CalculatedStyleValue::ResolvedType::Number;
  1197. }
  1198. CalculatedStyleValue::CalculationResult LogCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  1199. {
  1200. auto node_a = m_x->resolve(context, percentage_basis);
  1201. auto node_a_value = resolve_value(node_a.value(), context);
  1202. auto node_b = m_y->resolve(context, percentage_basis);
  1203. auto node_b_value = resolve_value(node_b.value(), context);
  1204. auto result = log2(node_a_value) / log2(node_b_value);
  1205. return { Number(Number::Type::Number, result) };
  1206. }
  1207. ErrorOr<void> LogCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  1208. {
  1209. TRY(m_x->for_each_child_node(callback));
  1210. TRY(m_y->for_each_child_node(callback));
  1211. TRY(callback(m_x));
  1212. TRY(callback(m_y));
  1213. return {};
  1214. }
  1215. ErrorOr<void> LogCalculationNode::dump(StringBuilder& builder, int indent) const
  1216. {
  1217. TRY(builder.try_appendff("{: >{}}LOG: {}\n", "", indent, TRY(to_string())));
  1218. return {};
  1219. }
  1220. ErrorOr<NonnullOwnPtr<ExpCalculationNode>> ExpCalculationNode::create(NonnullOwnPtr<CalculationNode> value)
  1221. {
  1222. return adopt_nonnull_own_or_enomem(new (nothrow) ExpCalculationNode(move(value)));
  1223. }
  1224. ExpCalculationNode::ExpCalculationNode(NonnullOwnPtr<CalculationNode> value)
  1225. : CalculationNode(Type::Exp)
  1226. , m_value(move(value))
  1227. {
  1228. }
  1229. ExpCalculationNode::~ExpCalculationNode() = default;
  1230. ErrorOr<String> ExpCalculationNode::to_string() const
  1231. {
  1232. StringBuilder builder;
  1233. builder.append("exp("sv);
  1234. builder.append(TRY(m_value->to_string()));
  1235. builder.append(")"sv);
  1236. return builder.to_string();
  1237. }
  1238. Optional<CalculatedStyleValue::ResolvedType> ExpCalculationNode::resolved_type() const
  1239. {
  1240. return CalculatedStyleValue::ResolvedType::Number;
  1241. }
  1242. CalculatedStyleValue::CalculationResult ExpCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  1243. {
  1244. auto node_a = m_value->resolve(context, percentage_basis);
  1245. auto node_a_value = resolve_value(node_a.value(), context);
  1246. auto result = exp(node_a_value);
  1247. return { Number(Number::Type::Number, result) };
  1248. }
  1249. ErrorOr<void> ExpCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  1250. {
  1251. TRY(m_value->for_each_child_node(callback));
  1252. TRY(callback(m_value));
  1253. return {};
  1254. }
  1255. ErrorOr<void> ExpCalculationNode::dump(StringBuilder& builder, int indent) const
  1256. {
  1257. TRY(builder.try_appendff("{: >{}}EXP: {}\n", "", indent, TRY(to_string())));
  1258. return {};
  1259. }
  1260. void CalculatedStyleValue::CalculationResult::add(CalculationResult const& other, Optional<Length::ResolutionContext const&> context, PercentageBasis const& percentage_basis)
  1261. {
  1262. add_or_subtract_internal(SumOperation::Add, other, context, percentage_basis);
  1263. }
  1264. void CalculatedStyleValue::CalculationResult::subtract(CalculationResult const& other, Optional<Length::ResolutionContext const&> context, PercentageBasis const& percentage_basis)
  1265. {
  1266. add_or_subtract_internal(SumOperation::Subtract, other, context, percentage_basis);
  1267. }
  1268. void CalculatedStyleValue::CalculationResult::add_or_subtract_internal(SumOperation op, CalculationResult const& other, Optional<Length::ResolutionContext const&> context, PercentageBasis const& percentage_basis)
  1269. {
  1270. // 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>".
  1271. // Though, having the same type may mean that one side is a <dimension> and the other a <percentage>.
  1272. // Note: This is almost identical to ::add()
  1273. m_value.visit(
  1274. [&](Number const& number) {
  1275. auto other_number = other.m_value.get<Number>();
  1276. if (op == SumOperation::Add) {
  1277. m_value = number + other_number;
  1278. } else {
  1279. m_value = number - other_number;
  1280. }
  1281. },
  1282. [&](Angle const& angle) {
  1283. auto this_degrees = angle.to_degrees();
  1284. if (other.m_value.has<Angle>()) {
  1285. auto other_degrees = other.m_value.get<Angle>().to_degrees();
  1286. if (op == SumOperation::Add)
  1287. m_value = Angle::make_degrees(this_degrees + other_degrees);
  1288. else
  1289. m_value = Angle::make_degrees(this_degrees - other_degrees);
  1290. } else {
  1291. VERIFY(percentage_basis.has<Angle>());
  1292. auto other_degrees = percentage_basis.get<Angle>().percentage_of(other.m_value.get<Percentage>()).to_degrees();
  1293. if (op == SumOperation::Add)
  1294. m_value = Angle::make_degrees(this_degrees + other_degrees);
  1295. else
  1296. m_value = Angle::make_degrees(this_degrees - other_degrees);
  1297. }
  1298. },
  1299. [&](Frequency const& frequency) {
  1300. auto this_hertz = frequency.to_hertz();
  1301. if (other.m_value.has<Frequency>()) {
  1302. auto other_hertz = other.m_value.get<Frequency>().to_hertz();
  1303. if (op == SumOperation::Add)
  1304. m_value = Frequency::make_hertz(this_hertz + other_hertz);
  1305. else
  1306. m_value = Frequency::make_hertz(this_hertz - other_hertz);
  1307. } else {
  1308. VERIFY(percentage_basis.has<Frequency>());
  1309. auto other_hertz = percentage_basis.get<Frequency>().percentage_of(other.m_value.get<Percentage>()).to_hertz();
  1310. if (op == SumOperation::Add)
  1311. m_value = Frequency::make_hertz(this_hertz + other_hertz);
  1312. else
  1313. m_value = Frequency::make_hertz(this_hertz - other_hertz);
  1314. }
  1315. },
  1316. [&](Length const& length) {
  1317. auto this_px = length.to_px(*context);
  1318. if (other.m_value.has<Length>()) {
  1319. auto other_px = other.m_value.get<Length>().to_px(*context);
  1320. if (op == SumOperation::Add)
  1321. m_value = Length::make_px(this_px + other_px);
  1322. else
  1323. m_value = Length::make_px(this_px - other_px);
  1324. } else {
  1325. VERIFY(percentage_basis.has<Length>());
  1326. auto other_px = percentage_basis.get<Length>().percentage_of(other.m_value.get<Percentage>()).to_px(*context);
  1327. if (op == SumOperation::Add)
  1328. m_value = Length::make_px(this_px + other_px);
  1329. else
  1330. m_value = Length::make_px(this_px - other_px);
  1331. }
  1332. },
  1333. [&](Time const& time) {
  1334. auto this_seconds = time.to_seconds();
  1335. if (other.m_value.has<Time>()) {
  1336. auto other_seconds = other.m_value.get<Time>().to_seconds();
  1337. if (op == SumOperation::Add)
  1338. m_value = Time::make_seconds(this_seconds + other_seconds);
  1339. else
  1340. m_value = Time::make_seconds(this_seconds - other_seconds);
  1341. } else {
  1342. VERIFY(percentage_basis.has<Time>());
  1343. auto other_seconds = percentage_basis.get<Time>().percentage_of(other.m_value.get<Percentage>()).to_seconds();
  1344. if (op == SumOperation::Add)
  1345. m_value = Time::make_seconds(this_seconds + other_seconds);
  1346. else
  1347. m_value = Time::make_seconds(this_seconds - other_seconds);
  1348. }
  1349. },
  1350. [&](Percentage const& percentage) {
  1351. if (other.m_value.has<Percentage>()) {
  1352. if (op == SumOperation::Add)
  1353. m_value = Percentage { percentage.value() + other.m_value.get<Percentage>().value() };
  1354. else
  1355. m_value = Percentage { percentage.value() - other.m_value.get<Percentage>().value() };
  1356. return;
  1357. }
  1358. // 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`.
  1359. CalculationResult new_value = other;
  1360. if (op == SumOperation::Add) {
  1361. new_value.add(*this, context, percentage_basis);
  1362. } else {
  1363. // Turn 'this - other' into '-other + this', as 'A + B == B + A', but 'A - B != B - A'
  1364. new_value.multiply_by({ Number { Number::Type::Integer, -1.0f } }, context);
  1365. new_value.add(*this, context, percentage_basis);
  1366. }
  1367. *this = new_value;
  1368. });
  1369. }
  1370. void CalculatedStyleValue::CalculationResult::multiply_by(CalculationResult const& other, Optional<Length::ResolutionContext const&> context)
  1371. {
  1372. // We know from validation when resolving the type, that at least one side must be a <number> or <integer>.
  1373. // Both of these are represented as a double.
  1374. VERIFY(m_value.has<Number>() || other.m_value.has<Number>());
  1375. bool other_is_number = other.m_value.has<Number>();
  1376. m_value.visit(
  1377. [&](Number const& number) {
  1378. if (other_is_number) {
  1379. m_value = number * other.m_value.get<Number>();
  1380. } else {
  1381. // Avoid duplicating all the logic by swapping `this` and `other`.
  1382. CalculationResult new_value = other;
  1383. new_value.multiply_by(*this, context);
  1384. *this = new_value;
  1385. }
  1386. },
  1387. [&](Angle const& angle) {
  1388. m_value = Angle::make_degrees(angle.to_degrees() * other.m_value.get<Number>().value());
  1389. },
  1390. [&](Frequency const& frequency) {
  1391. m_value = Frequency::make_hertz(frequency.to_hertz() * other.m_value.get<Number>().value());
  1392. },
  1393. [&](Length const& length) {
  1394. m_value = Length::make_px(length.to_px(*context) * static_cast<double>(other.m_value.get<Number>().value()));
  1395. },
  1396. [&](Time const& time) {
  1397. m_value = Time::make_seconds(time.to_seconds() * other.m_value.get<Number>().value());
  1398. },
  1399. [&](Percentage const& percentage) {
  1400. m_value = Percentage { percentage.value() * other.m_value.get<Number>().value() };
  1401. });
  1402. }
  1403. void CalculatedStyleValue::CalculationResult::divide_by(CalculationResult const& other, Optional<Length::ResolutionContext const&> context)
  1404. {
  1405. // We know from validation when resolving the type, that `other` must be a <number> or <integer>.
  1406. // Both of these are represented as a Number.
  1407. auto denominator = other.m_value.get<Number>().value();
  1408. // FIXME: Dividing by 0 is invalid, and should be caught during parsing.
  1409. VERIFY(denominator != 0.0);
  1410. m_value.visit(
  1411. [&](Number const& number) {
  1412. m_value = Number {
  1413. Number::Type::Number,
  1414. number.value() / denominator
  1415. };
  1416. },
  1417. [&](Angle const& angle) {
  1418. m_value = Angle::make_degrees(angle.to_degrees() / denominator);
  1419. },
  1420. [&](Frequency const& frequency) {
  1421. m_value = Frequency::make_hertz(frequency.to_hertz() / denominator);
  1422. },
  1423. [&](Length const& length) {
  1424. m_value = Length::make_px(length.to_px(*context) / static_cast<double>(denominator));
  1425. },
  1426. [&](Time const& time) {
  1427. m_value = Time::make_seconds(time.to_seconds() / denominator);
  1428. },
  1429. [&](Percentage const& percentage) {
  1430. m_value = Percentage { percentage.value() / denominator };
  1431. });
  1432. }
  1433. void CalculatedStyleValue::CalculationResult::negate()
  1434. {
  1435. m_value.visit(
  1436. [&](Number const& number) {
  1437. m_value = Number { number.type(), 0 - number.value() };
  1438. },
  1439. [&](Angle const& angle) {
  1440. m_value = Angle { 0 - angle.raw_value(), angle.type() };
  1441. },
  1442. [&](Frequency const& frequency) {
  1443. m_value = Frequency { 0 - frequency.raw_value(), frequency.type() };
  1444. },
  1445. [&](Length const& length) {
  1446. m_value = Length { 0 - length.raw_value(), length.type() };
  1447. },
  1448. [&](Time const& time) {
  1449. m_value = Time { 0 - time.raw_value(), time.type() };
  1450. },
  1451. [&](Percentage const& percentage) {
  1452. m_value = Percentage { 0 - percentage.value() };
  1453. });
  1454. }
  1455. void CalculatedStyleValue::CalculationResult::invert()
  1456. {
  1457. // FIXME: Correctly handle division by zero.
  1458. m_value.visit(
  1459. [&](Number const& number) {
  1460. m_value = Number { Number::Type::Number, 1 / number.value() };
  1461. },
  1462. [&](Angle const& angle) {
  1463. m_value = Angle { 1 / angle.raw_value(), angle.type() };
  1464. },
  1465. [&](Frequency const& frequency) {
  1466. m_value = Frequency { 1 / frequency.raw_value(), frequency.type() };
  1467. },
  1468. [&](Length const& length) {
  1469. m_value = Length { 1 / length.raw_value(), length.type() };
  1470. },
  1471. [&](Time const& time) {
  1472. m_value = Time { 1 / time.raw_value(), time.type() };
  1473. },
  1474. [&](Percentage const& percentage) {
  1475. m_value = Percentage { 1 / percentage.value() };
  1476. });
  1477. }
  1478. ErrorOr<String> CalculatedStyleValue::to_string() const
  1479. {
  1480. // FIXME: Implement this according to https://www.w3.org/TR/css-values-4/#calc-serialize once that stabilizes.
  1481. return String::formatted("calc({})", TRY(m_calculation->to_string()));
  1482. }
  1483. bool CalculatedStyleValue::equals(StyleValue const& other) const
  1484. {
  1485. if (type() != other.type())
  1486. return false;
  1487. // This is a case where comparing the strings actually makes sense.
  1488. return to_string().release_value_but_fixme_should_propagate_errors() == other.to_string().release_value_but_fixme_should_propagate_errors();
  1489. }
  1490. Optional<Angle> CalculatedStyleValue::resolve_angle() const
  1491. {
  1492. auto result = m_calculation->resolve({}, {});
  1493. if (result.value().has<Angle>())
  1494. return result.value().get<Angle>();
  1495. return {};
  1496. }
  1497. Optional<Angle> CalculatedStyleValue::resolve_angle_percentage(Angle const& percentage_basis) const
  1498. {
  1499. auto result = m_calculation->resolve({}, percentage_basis);
  1500. return result.value().visit(
  1501. [&](Angle const& angle) -> Optional<Angle> {
  1502. return angle;
  1503. },
  1504. [&](Percentage const& percentage) -> Optional<Angle> {
  1505. return percentage_basis.percentage_of(percentage);
  1506. },
  1507. [&](auto const&) -> Optional<Angle> {
  1508. return {};
  1509. });
  1510. }
  1511. Optional<Frequency> CalculatedStyleValue::resolve_frequency() const
  1512. {
  1513. auto result = m_calculation->resolve({}, {});
  1514. if (result.value().has<Frequency>())
  1515. return result.value().get<Frequency>();
  1516. return {};
  1517. }
  1518. Optional<Frequency> CalculatedStyleValue::resolve_frequency_percentage(Frequency const& percentage_basis) const
  1519. {
  1520. auto result = m_calculation->resolve({}, percentage_basis);
  1521. return result.value().visit(
  1522. [&](Frequency const& frequency) -> Optional<Frequency> {
  1523. return frequency;
  1524. },
  1525. [&](Percentage const& percentage) -> Optional<Frequency> {
  1526. return percentage_basis.percentage_of(percentage);
  1527. },
  1528. [&](auto const&) -> Optional<Frequency> {
  1529. return {};
  1530. });
  1531. }
  1532. Optional<Length> CalculatedStyleValue::resolve_length(Length::ResolutionContext const& context) const
  1533. {
  1534. auto result = m_calculation->resolve(context, {});
  1535. if (result.value().has<Length>())
  1536. return result.value().get<Length>();
  1537. return {};
  1538. }
  1539. Optional<Length> CalculatedStyleValue::resolve_length(Layout::Node const& layout_node) const
  1540. {
  1541. return resolve_length(Length::ResolutionContext::for_layout_node(layout_node));
  1542. }
  1543. Optional<Length> CalculatedStyleValue::resolve_length_percentage(Layout::Node const& layout_node, Length const& percentage_basis) const
  1544. {
  1545. auto result = m_calculation->resolve(Length::ResolutionContext::for_layout_node(layout_node), percentage_basis);
  1546. return result.value().visit(
  1547. [&](Length const& length) -> Optional<Length> {
  1548. return length;
  1549. },
  1550. [&](Percentage const& percentage) -> Optional<Length> {
  1551. return percentage_basis.percentage_of(percentage);
  1552. },
  1553. [&](auto const&) -> Optional<Length> {
  1554. return {};
  1555. });
  1556. }
  1557. Optional<Percentage> CalculatedStyleValue::resolve_percentage() const
  1558. {
  1559. auto result = m_calculation->resolve({}, {});
  1560. if (result.value().has<Percentage>())
  1561. return result.value().get<Percentage>();
  1562. return {};
  1563. }
  1564. Optional<Time> CalculatedStyleValue::resolve_time() const
  1565. {
  1566. auto result = m_calculation->resolve({}, {});
  1567. if (result.value().has<Time>())
  1568. return result.value().get<Time>();
  1569. return {};
  1570. }
  1571. Optional<Time> CalculatedStyleValue::resolve_time_percentage(Time const& percentage_basis) const
  1572. {
  1573. auto result = m_calculation->resolve({}, percentage_basis);
  1574. return result.value().visit(
  1575. [&](Time const& time) -> Optional<Time> {
  1576. return time;
  1577. },
  1578. [&](auto const&) -> Optional<Time> {
  1579. return {};
  1580. });
  1581. }
  1582. Optional<double> CalculatedStyleValue::resolve_number() const
  1583. {
  1584. auto result = m_calculation->resolve({}, {});
  1585. if (result.value().has<Number>())
  1586. return result.value().get<Number>().value();
  1587. return {};
  1588. }
  1589. Optional<i64> CalculatedStyleValue::resolve_integer()
  1590. {
  1591. auto result = m_calculation->resolve({}, {});
  1592. if (result.value().has<Number>())
  1593. return result.value().get<Number>().integer_value();
  1594. return {};
  1595. }
  1596. bool CalculatedStyleValue::contains_percentage() const
  1597. {
  1598. return m_calculation->contains_percentage();
  1599. }
  1600. }