CalculatedStyleValue.cpp 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452
  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. #include <LibWeb/CSS/PropertyID.h>
  12. namespace Web::CSS {
  13. static bool is_number(CalculatedStyleValue::ResolvedType type)
  14. {
  15. return type == CalculatedStyleValue::ResolvedType::Number || type == CalculatedStyleValue::ResolvedType::Integer;
  16. }
  17. static bool is_dimension(CalculatedStyleValue::ResolvedType type)
  18. {
  19. return type != CalculatedStyleValue::ResolvedType::Number
  20. && type != CalculatedStyleValue::ResolvedType::Integer
  21. && type != CalculatedStyleValue::ResolvedType::Percentage;
  22. }
  23. static double resolve_value_radians(CalculatedStyleValue::CalculationResult::Value value)
  24. {
  25. return value.visit(
  26. [](Number const& number) { return number.value(); },
  27. [](Angle const& angle) { return angle.to_radians(); },
  28. [](auto const&) { VERIFY_NOT_REACHED(); return 0.0; });
  29. }
  30. static double resolve_value(CalculatedStyleValue::CalculationResult::Value value, Optional<Length::ResolutionContext const&> context)
  31. {
  32. return value.visit(
  33. [](Number const& number) { return number.value(); },
  34. [](Angle const& angle) { return angle.to_degrees(); },
  35. [](Frequency const& frequency) { return frequency.to_hertz(); },
  36. [&context](Length const& length) { return length.to_px(*context).to_double(); },
  37. [](Percentage const& percentage) { return percentage.value(); },
  38. [](Time const& time) { return time.to_seconds(); });
  39. }
  40. static Optional<CSSNumericType> add_the_types(Vector<NonnullOwnPtr<CalculationNode>> const& nodes, PropertyID property_id)
  41. {
  42. Optional<CSSNumericType> left_type;
  43. for (auto const& value : nodes) {
  44. auto right_type = value->determine_type(property_id);
  45. if (!right_type.has_value())
  46. return {};
  47. if (left_type.has_value()) {
  48. left_type = left_type->added_to(right_type.value());
  49. } else {
  50. left_type = right_type;
  51. }
  52. if (!left_type.has_value())
  53. return {};
  54. }
  55. return left_type;
  56. }
  57. static CalculatedStyleValue::CalculationResult to_resolved_type(CalculatedStyleValue::ResolvedType type, double value)
  58. {
  59. switch (type) {
  60. case CalculatedStyleValue::ResolvedType::Integer:
  61. return { Number(Number::Type::Integer, value) };
  62. case CalculatedStyleValue::ResolvedType::Number:
  63. return { Number(Number::Type::Number, value) };
  64. case CalculatedStyleValue::ResolvedType::Angle:
  65. return { Angle::make_degrees(value) };
  66. case CalculatedStyleValue::ResolvedType::Frequency:
  67. return { Frequency::make_hertz(value) };
  68. case CalculatedStyleValue::ResolvedType::Length:
  69. return { Length::make_px(value) };
  70. case CalculatedStyleValue::ResolvedType::Percentage:
  71. return { Percentage(value) };
  72. case CalculatedStyleValue::ResolvedType::Time:
  73. return { Time::make_seconds(value) };
  74. }
  75. VERIFY_NOT_REACHED();
  76. }
  77. Optional<CalculationNode::ConstantType> CalculationNode::constant_type_from_string(StringView string)
  78. {
  79. if (string.equals_ignoring_ascii_case("e"sv))
  80. return CalculationNode::ConstantType::E;
  81. if (string.equals_ignoring_ascii_case("pi"sv))
  82. return CalculationNode::ConstantType::Pi;
  83. if (string.equals_ignoring_ascii_case("infinity"sv))
  84. return CalculationNode::ConstantType::Infinity;
  85. if (string.equals_ignoring_ascii_case("-infinity"sv))
  86. return CalculationNode::ConstantType::MinusInfinity;
  87. if (string.equals_ignoring_ascii_case("NaN"sv))
  88. return CalculationNode::ConstantType::NaN;
  89. return {};
  90. }
  91. CalculationNode::CalculationNode(Type type)
  92. : m_type(type)
  93. {
  94. }
  95. CalculationNode::~CalculationNode() = default;
  96. ErrorOr<NonnullOwnPtr<NumericCalculationNode>> NumericCalculationNode::create(NumericValue value)
  97. {
  98. return adopt_nonnull_own_or_enomem(new (nothrow) NumericCalculationNode(move(value)));
  99. }
  100. NumericCalculationNode::NumericCalculationNode(NumericValue value)
  101. : CalculationNode(Type::Numeric)
  102. , m_value(move(value))
  103. {
  104. }
  105. NumericCalculationNode::~NumericCalculationNode() = default;
  106. ErrorOr<String> NumericCalculationNode::to_string() const
  107. {
  108. return m_value.visit([](auto& value) { return value.to_string(); });
  109. }
  110. Optional<CalculatedStyleValue::ResolvedType> NumericCalculationNode::resolved_type() const
  111. {
  112. return m_value.visit(
  113. [](Number const&) { return CalculatedStyleValue::ResolvedType::Number; },
  114. [](Angle const&) { return CalculatedStyleValue::ResolvedType::Angle; },
  115. [](Frequency const&) { return CalculatedStyleValue::ResolvedType::Frequency; },
  116. [](Length const&) { return CalculatedStyleValue::ResolvedType::Length; },
  117. [](Percentage const&) { return CalculatedStyleValue::ResolvedType::Percentage; },
  118. [](Time const&) { return CalculatedStyleValue::ResolvedType::Time; });
  119. }
  120. // https://www.w3.org/TR/css-values-4/#determine-the-type-of-a-calculation
  121. Optional<CSSNumericType> NumericCalculationNode::determine_type(PropertyID property_id) const
  122. {
  123. // Anything else is a terminal value, whose type is determined based on its CSS type:
  124. return m_value.visit(
  125. [](Number const&) {
  126. // -> <number>
  127. // -> <integer>
  128. // the type is «[ ]» (empty map)
  129. return CSSNumericType {};
  130. },
  131. [](Length const&) {
  132. // -> <length>
  133. // the type is «[ "length" → 1 ]»
  134. return CSSNumericType { CSSNumericType::BaseType::Length, 1 };
  135. },
  136. [](Angle const&) {
  137. // -> <angle>
  138. // the type is «[ "angle" → 1 ]»
  139. return CSSNumericType { CSSNumericType::BaseType::Angle, 1 };
  140. },
  141. [](Time const&) {
  142. // -> <time>
  143. // the type is «[ "time" → 1 ]»
  144. return CSSNumericType { CSSNumericType::BaseType::Time, 1 };
  145. },
  146. [](Frequency const&) {
  147. // -> <frequency>
  148. // the type is «[ "frequency" → 1 ]»
  149. return CSSNumericType { CSSNumericType::BaseType::Frequency, 1 };
  150. },
  151. // FIXME: <resolution>
  152. // FIXME: <flex>
  153. // NOTE: <calc-constant> is a separate node type. (FIXME: Should it be?)
  154. [property_id](Percentage const&) {
  155. // -> <percentage>
  156. // If, in the context in which the math function containing this calculation is placed,
  157. // <percentage>s are resolved relative to another type of value (such as in width,
  158. // where <percentage> is resolved against a <length>), and that other type is not <number>,
  159. // the type is determined as the other type.
  160. auto percentage_resolved_type = property_resolves_percentages_relative_to(property_id);
  161. if (percentage_resolved_type.has_value() && percentage_resolved_type != ValueType::Number && percentage_resolved_type != ValueType::Percentage) {
  162. auto base_type = CSSNumericType::base_type_from_value_type(*percentage_resolved_type);
  163. VERIFY(base_type.has_value());
  164. return CSSNumericType { base_type.value(), 1 };
  165. }
  166. // Otherwise, the type is «[ "percent" → 1 ]».
  167. return CSSNumericType { CSSNumericType::BaseType::Percent, 1 };
  168. });
  169. // In all cases, the associated percent hint is null.
  170. }
  171. bool NumericCalculationNode::contains_percentage() const
  172. {
  173. return m_value.has<Percentage>();
  174. }
  175. CalculatedStyleValue::CalculationResult NumericCalculationNode::resolve(Optional<Length::ResolutionContext const&>, CalculatedStyleValue::PercentageBasis const&) const
  176. {
  177. return m_value;
  178. }
  179. ErrorOr<void> NumericCalculationNode::dump(StringBuilder& builder, int indent) const
  180. {
  181. return builder.try_appendff("{: >{}}NUMERIC({})\n", "", indent, TRY(m_value.visit([](auto& it) { return it.to_string(); })));
  182. }
  183. ErrorOr<NonnullOwnPtr<SumCalculationNode>> SumCalculationNode::create(Vector<NonnullOwnPtr<CalculationNode>> values)
  184. {
  185. return adopt_nonnull_own_or_enomem(new (nothrow) SumCalculationNode(move(values)));
  186. }
  187. SumCalculationNode::SumCalculationNode(Vector<NonnullOwnPtr<CalculationNode>> values)
  188. : CalculationNode(Type::Sum)
  189. , m_values(move(values))
  190. {
  191. VERIFY(!m_values.is_empty());
  192. }
  193. SumCalculationNode::~SumCalculationNode() = default;
  194. ErrorOr<String> SumCalculationNode::to_string() const
  195. {
  196. bool first = true;
  197. StringBuilder builder;
  198. for (auto& value : m_values) {
  199. if (!first)
  200. TRY(builder.try_append(" + "sv));
  201. TRY(builder.try_append(TRY(value->to_string())));
  202. first = false;
  203. }
  204. return builder.to_string();
  205. }
  206. Optional<CalculatedStyleValue::ResolvedType> SumCalculationNode::resolved_type() const
  207. {
  208. // FIXME: Implement https://www.w3.org/TR/css-values-4/#determine-the-type-of-a-calculation
  209. // For now, this is just ad-hoc, based on the old implementation.
  210. Optional<CalculatedStyleValue::ResolvedType> type;
  211. for (auto const& value : m_values) {
  212. auto maybe_value_type = value->resolved_type();
  213. if (!maybe_value_type.has_value())
  214. return {};
  215. auto value_type = maybe_value_type.value();
  216. if (!type.has_value()) {
  217. type = value_type;
  218. continue;
  219. }
  220. // At + or -, check that both sides have the same type, or that one side is a <number> and the other is an <integer>.
  221. // If both sides are the same type, resolve to that type.
  222. if (value_type == type)
  223. continue;
  224. // If one side is a <number> and the other is an <integer>, resolve to <number>.
  225. if (is_number(*type) && is_number(value_type)) {
  226. type = CalculatedStyleValue::ResolvedType::Number;
  227. continue;
  228. }
  229. // FIXME: calc() handles <percentage> by allowing them to pretend to be whatever <dimension> type is allowed at this location.
  230. // Since we can't easily check what that type is, we just allow <percentage> to combine with any other <dimension> type.
  231. if (type == CalculatedStyleValue::ResolvedType::Percentage && is_dimension(value_type)) {
  232. type = value_type;
  233. continue;
  234. }
  235. if (is_dimension(*type) && value_type == CalculatedStyleValue::ResolvedType::Percentage)
  236. continue;
  237. return {};
  238. }
  239. return type;
  240. }
  241. // https://www.w3.org/TR/css-values-4/#determine-the-type-of-a-calculation
  242. Optional<CSSNumericType> SumCalculationNode::determine_type(PropertyID property_id) const
  243. {
  244. // At a + or - sub-expression, attempt to add the types of the left and right arguments.
  245. // If this returns failure, the entire calculation’s type is failure.
  246. // Otherwise, the sub-expression’s type is the returned type.
  247. return add_the_types(m_values, property_id);
  248. }
  249. bool SumCalculationNode::contains_percentage() const
  250. {
  251. for (auto const& value : m_values) {
  252. if (value->contains_percentage())
  253. return true;
  254. }
  255. return false;
  256. }
  257. CalculatedStyleValue::CalculationResult SumCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  258. {
  259. Optional<CalculatedStyleValue::CalculationResult> total;
  260. for (auto& additional_product : m_values) {
  261. auto additional_value = additional_product->resolve(context, percentage_basis);
  262. if (!total.has_value()) {
  263. total = additional_value;
  264. continue;
  265. }
  266. total->add(additional_value, context, percentage_basis);
  267. }
  268. return total.value();
  269. }
  270. ErrorOr<void> SumCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  271. {
  272. for (auto& item : m_values) {
  273. TRY(item->for_each_child_node(callback));
  274. TRY(callback(item));
  275. }
  276. return {};
  277. }
  278. ErrorOr<void> SumCalculationNode::dump(StringBuilder& builder, int indent) const
  279. {
  280. TRY(builder.try_appendff("{: >{}}SUM:\n", "", indent));
  281. for (auto const& item : m_values)
  282. TRY(item->dump(builder, indent + 2));
  283. return {};
  284. }
  285. ErrorOr<NonnullOwnPtr<ProductCalculationNode>> ProductCalculationNode::create(Vector<NonnullOwnPtr<CalculationNode>> values)
  286. {
  287. return adopt_nonnull_own_or_enomem(new (nothrow) ProductCalculationNode(move(values)));
  288. }
  289. ProductCalculationNode::ProductCalculationNode(Vector<NonnullOwnPtr<CalculationNode>> values)
  290. : CalculationNode(Type::Product)
  291. , m_values(move(values))
  292. {
  293. VERIFY(!m_values.is_empty());
  294. }
  295. ProductCalculationNode::~ProductCalculationNode() = default;
  296. ErrorOr<String> ProductCalculationNode::to_string() const
  297. {
  298. bool first = true;
  299. StringBuilder builder;
  300. for (auto& value : m_values) {
  301. if (!first)
  302. TRY(builder.try_append(" * "sv));
  303. TRY(builder.try_append(TRY(value->to_string())));
  304. first = false;
  305. }
  306. return builder.to_string();
  307. }
  308. Optional<CalculatedStyleValue::ResolvedType> ProductCalculationNode::resolved_type() const
  309. {
  310. // FIXME: Implement https://www.w3.org/TR/css-values-4/#determine-the-type-of-a-calculation
  311. // For now, this is just ad-hoc, based on the old implementation.
  312. Optional<CalculatedStyleValue::ResolvedType> type;
  313. for (auto const& value : m_values) {
  314. auto maybe_value_type = value->resolved_type();
  315. if (!maybe_value_type.has_value())
  316. return {};
  317. auto value_type = maybe_value_type.value();
  318. if (!type.has_value()) {
  319. type = value_type;
  320. continue;
  321. }
  322. // At *, check that at least one side is <number>.
  323. if (!(is_number(*type) || is_number(value_type)))
  324. return {};
  325. // If both sides are <integer>, resolve to <integer>.
  326. if (type == CalculatedStyleValue::ResolvedType::Integer && value_type == CalculatedStyleValue::ResolvedType::Integer) {
  327. type = CalculatedStyleValue::ResolvedType::Integer;
  328. } else {
  329. // Otherwise, resolve to the type of the other side.
  330. if (is_number(*type))
  331. type = value_type;
  332. }
  333. }
  334. return type;
  335. }
  336. // https://www.w3.org/TR/css-values-4/#determine-the-type-of-a-calculation
  337. Optional<CSSNumericType> ProductCalculationNode::determine_type(PropertyID property_id) const
  338. {
  339. // At a * sub-expression, multiply the types of the left and right arguments.
  340. // The sub-expression’s type is the returned result.
  341. Optional<CSSNumericType> left_type;
  342. for (auto const& value : m_values) {
  343. auto right_type = value->determine_type(property_id);
  344. if (!right_type.has_value())
  345. return {};
  346. if (left_type.has_value()) {
  347. left_type = left_type->multiplied_by(right_type.value());
  348. } else {
  349. left_type = right_type;
  350. }
  351. if (!left_type.has_value())
  352. return {};
  353. }
  354. return left_type;
  355. }
  356. bool ProductCalculationNode::contains_percentage() const
  357. {
  358. for (auto const& value : m_values) {
  359. if (value->contains_percentage())
  360. return true;
  361. }
  362. return false;
  363. }
  364. CalculatedStyleValue::CalculationResult ProductCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  365. {
  366. Optional<CalculatedStyleValue::CalculationResult> total;
  367. for (auto& additional_product : m_values) {
  368. auto additional_value = additional_product->resolve(context, percentage_basis);
  369. if (!total.has_value()) {
  370. total = additional_value;
  371. continue;
  372. }
  373. total->multiply_by(additional_value, context);
  374. }
  375. return total.value();
  376. }
  377. ErrorOr<void> ProductCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  378. {
  379. for (auto& item : m_values) {
  380. TRY(item->for_each_child_node(callback));
  381. TRY(callback(item));
  382. }
  383. return {};
  384. }
  385. ErrorOr<void> ProductCalculationNode::dump(StringBuilder& builder, int indent) const
  386. {
  387. TRY(builder.try_appendff("{: >{}}PRODUCT:\n", "", indent));
  388. for (auto const& item : m_values)
  389. TRY(item->dump(builder, indent + 2));
  390. return {};
  391. }
  392. ErrorOr<NonnullOwnPtr<NegateCalculationNode>> NegateCalculationNode::create(NonnullOwnPtr<Web::CSS::CalculationNode> value)
  393. {
  394. return adopt_nonnull_own_or_enomem(new (nothrow) NegateCalculationNode(move(value)));
  395. }
  396. NegateCalculationNode::NegateCalculationNode(NonnullOwnPtr<CalculationNode> value)
  397. : CalculationNode(Type::Negate)
  398. , m_value(move(value))
  399. {
  400. }
  401. NegateCalculationNode::~NegateCalculationNode() = default;
  402. ErrorOr<String> NegateCalculationNode::to_string() const
  403. {
  404. return String::formatted("(0 - {})", TRY(m_value->to_string()));
  405. }
  406. Optional<CalculatedStyleValue::ResolvedType> NegateCalculationNode::resolved_type() const
  407. {
  408. return m_value->resolved_type();
  409. }
  410. // https://www.w3.org/TR/css-values-4/#determine-the-type-of-a-calculation
  411. Optional<CSSNumericType> NegateCalculationNode::determine_type(PropertyID property_id) const
  412. {
  413. // NOTE: `- foo` doesn't change the type
  414. return m_value->determine_type(property_id);
  415. }
  416. bool NegateCalculationNode::contains_percentage() const
  417. {
  418. return m_value->contains_percentage();
  419. }
  420. CalculatedStyleValue::CalculationResult NegateCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  421. {
  422. auto child_value = m_value->resolve(context, percentage_basis);
  423. child_value.negate();
  424. return child_value;
  425. }
  426. ErrorOr<void> NegateCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  427. {
  428. TRY(m_value->for_each_child_node(callback));
  429. TRY(callback(m_value));
  430. return {};
  431. }
  432. ErrorOr<void> NegateCalculationNode::dump(StringBuilder& builder, int indent) const
  433. {
  434. TRY(builder.try_appendff("{: >{}}NEGATE:\n", "", indent));
  435. TRY(m_value->dump(builder, indent + 2));
  436. return {};
  437. }
  438. ErrorOr<NonnullOwnPtr<InvertCalculationNode>> InvertCalculationNode::create(NonnullOwnPtr<Web::CSS::CalculationNode> value)
  439. {
  440. return adopt_nonnull_own_or_enomem(new (nothrow) InvertCalculationNode(move(value)));
  441. }
  442. InvertCalculationNode::InvertCalculationNode(NonnullOwnPtr<CalculationNode> value)
  443. : CalculationNode(Type::Invert)
  444. , m_value(move(value))
  445. {
  446. }
  447. InvertCalculationNode::~InvertCalculationNode() = default;
  448. ErrorOr<String> InvertCalculationNode::to_string() const
  449. {
  450. return String::formatted("(1 / {})", TRY(m_value->to_string()));
  451. }
  452. Optional<CalculatedStyleValue::ResolvedType> InvertCalculationNode::resolved_type() const
  453. {
  454. auto type = m_value->resolved_type();
  455. if (type == CalculatedStyleValue::ResolvedType::Integer)
  456. return CalculatedStyleValue::ResolvedType::Number;
  457. return type;
  458. }
  459. // https://www.w3.org/TR/css-values-4/#determine-the-type-of-a-calculation
  460. Optional<CSSNumericType> InvertCalculationNode::determine_type(PropertyID property_id) const
  461. {
  462. // At a / sub-expression, let left type be the result of finding the types of its left argument,
  463. // and right type be the result of finding the types of its right argument and then inverting it.
  464. // The sub-expression’s type is the result of multiplying the left type and right type.
  465. // NOTE: An InvertCalculationNode only represents the right argument here, and the multiplication
  466. // is handled in the parent ProductCalculationNode.
  467. return m_value->determine_type(property_id).map([](auto& it) { return it.inverted(); });
  468. }
  469. bool InvertCalculationNode::contains_percentage() const
  470. {
  471. return m_value->contains_percentage();
  472. }
  473. CalculatedStyleValue::CalculationResult InvertCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  474. {
  475. auto child_value = m_value->resolve(context, percentage_basis);
  476. child_value.invert();
  477. return child_value;
  478. }
  479. ErrorOr<void> InvertCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  480. {
  481. TRY(m_value->for_each_child_node(callback));
  482. TRY(callback(m_value));
  483. return {};
  484. }
  485. ErrorOr<void> InvertCalculationNode::dump(StringBuilder& builder, int indent) const
  486. {
  487. TRY(builder.try_appendff("{: >{}}INVERT:\n", "", indent));
  488. TRY(m_value->dump(builder, indent + 2));
  489. return {};
  490. }
  491. ErrorOr<NonnullOwnPtr<MinCalculationNode>> MinCalculationNode::create(Vector<NonnullOwnPtr<Web::CSS::CalculationNode>> values)
  492. {
  493. return adopt_nonnull_own_or_enomem(new (nothrow) MinCalculationNode(move(values)));
  494. }
  495. MinCalculationNode::MinCalculationNode(Vector<NonnullOwnPtr<CalculationNode>> values)
  496. : CalculationNode(Type::Min)
  497. , m_values(move(values))
  498. {
  499. }
  500. MinCalculationNode::~MinCalculationNode() = default;
  501. ErrorOr<String> MinCalculationNode::to_string() const
  502. {
  503. StringBuilder builder;
  504. TRY(builder.try_append("min("sv));
  505. for (size_t i = 0; i < m_values.size(); ++i) {
  506. if (i != 0)
  507. TRY(builder.try_append(", "sv));
  508. TRY(builder.try_append(TRY(m_values[i]->to_string())));
  509. }
  510. TRY(builder.try_append(")"sv));
  511. return builder.to_string();
  512. }
  513. Optional<CalculatedStyleValue::ResolvedType> MinCalculationNode::resolved_type() const
  514. {
  515. // NOTE: We check during parsing that all values have the same type.
  516. return m_values[0]->resolved_type();
  517. }
  518. // https://www.w3.org/TR/css-values-4/#determine-the-type-of-a-calculation
  519. Optional<CSSNumericType> MinCalculationNode::determine_type(PropertyID property_id) const
  520. {
  521. // The result of adding the types of its comma-separated calculations.
  522. return add_the_types(m_values, property_id);
  523. }
  524. bool MinCalculationNode::contains_percentage() const
  525. {
  526. for (auto const& value : m_values) {
  527. if (value->contains_percentage())
  528. return true;
  529. }
  530. return false;
  531. }
  532. CalculatedStyleValue::CalculationResult MinCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  533. {
  534. CalculatedStyleValue::CalculationResult smallest_node = m_values.first()->resolve(context, percentage_basis);
  535. auto smallest_value = resolve_value(smallest_node.value(), context);
  536. for (size_t i = 1; i < m_values.size(); i++) {
  537. auto child_resolved = m_values[i]->resolve(context, percentage_basis);
  538. auto child_value = resolve_value(child_resolved.value(), context);
  539. if (child_value < smallest_value) {
  540. smallest_value = child_value;
  541. smallest_node = child_resolved;
  542. }
  543. }
  544. return smallest_node;
  545. }
  546. ErrorOr<void> MinCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  547. {
  548. for (auto& value : m_values) {
  549. TRY(value->for_each_child_node(callback));
  550. TRY(callback(value));
  551. }
  552. return {};
  553. }
  554. ErrorOr<void> MinCalculationNode::dump(StringBuilder& builder, int indent) const
  555. {
  556. TRY(builder.try_appendff("{: >{}}MIN:\n", "", indent));
  557. for (auto const& value : m_values)
  558. TRY(value->dump(builder, indent + 2));
  559. return {};
  560. }
  561. ErrorOr<NonnullOwnPtr<MaxCalculationNode>> MaxCalculationNode::create(Vector<NonnullOwnPtr<Web::CSS::CalculationNode>> values)
  562. {
  563. return adopt_nonnull_own_or_enomem(new (nothrow) MaxCalculationNode(move(values)));
  564. }
  565. MaxCalculationNode::MaxCalculationNode(Vector<NonnullOwnPtr<CalculationNode>> values)
  566. : CalculationNode(Type::Max)
  567. , m_values(move(values))
  568. {
  569. }
  570. MaxCalculationNode::~MaxCalculationNode() = default;
  571. ErrorOr<String> MaxCalculationNode::to_string() const
  572. {
  573. StringBuilder builder;
  574. TRY(builder.try_append("max("sv));
  575. for (size_t i = 0; i < m_values.size(); ++i) {
  576. if (i != 0)
  577. TRY(builder.try_append(", "sv));
  578. TRY(builder.try_append(TRY(m_values[i]->to_string())));
  579. }
  580. TRY(builder.try_append(")"sv));
  581. return builder.to_string();
  582. }
  583. Optional<CalculatedStyleValue::ResolvedType> MaxCalculationNode::resolved_type() const
  584. {
  585. // NOTE: We check during parsing that all values have the same type.
  586. return m_values[0]->resolved_type();
  587. }
  588. // https://www.w3.org/TR/css-values-4/#determine-the-type-of-a-calculation
  589. Optional<CSSNumericType> MaxCalculationNode::determine_type(PropertyID property_id) const
  590. {
  591. // The result of adding the types of its comma-separated calculations.
  592. return add_the_types(m_values, property_id);
  593. }
  594. bool MaxCalculationNode::contains_percentage() const
  595. {
  596. for (auto const& value : m_values) {
  597. if (value->contains_percentage())
  598. return true;
  599. }
  600. return false;
  601. }
  602. CalculatedStyleValue::CalculationResult MaxCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  603. {
  604. CalculatedStyleValue::CalculationResult largest_node = m_values.first()->resolve(context, percentage_basis);
  605. auto largest_value = resolve_value(largest_node.value(), context);
  606. for (size_t i = 1; i < m_values.size(); i++) {
  607. auto child_resolved = m_values[i]->resolve(context, percentage_basis);
  608. auto child_value = resolve_value(child_resolved.value(), context);
  609. if (child_value > largest_value) {
  610. largest_value = child_value;
  611. largest_node = child_resolved;
  612. }
  613. }
  614. return largest_node;
  615. }
  616. ErrorOr<void> MaxCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  617. {
  618. for (auto& value : m_values) {
  619. TRY(value->for_each_child_node(callback));
  620. TRY(callback(value));
  621. }
  622. return {};
  623. }
  624. ErrorOr<void> MaxCalculationNode::dump(StringBuilder& builder, int indent) const
  625. {
  626. TRY(builder.try_appendff("{: >{}}MAX:\n", "", indent));
  627. for (auto const& value : m_values)
  628. TRY(value->dump(builder, indent + 2));
  629. return {};
  630. }
  631. ErrorOr<NonnullOwnPtr<ClampCalculationNode>> ClampCalculationNode::create(NonnullOwnPtr<CalculationNode> min, NonnullOwnPtr<CalculationNode> center, NonnullOwnPtr<CalculationNode> max)
  632. {
  633. return adopt_nonnull_own_or_enomem(new (nothrow) ClampCalculationNode(move(min), move(center), move(max)));
  634. }
  635. ClampCalculationNode::ClampCalculationNode(NonnullOwnPtr<CalculationNode> min, NonnullOwnPtr<CalculationNode> center, NonnullOwnPtr<CalculationNode> max)
  636. : CalculationNode(Type::Clamp)
  637. , m_min_value(move(min))
  638. , m_center_value(move(center))
  639. , m_max_value(move(max))
  640. {
  641. }
  642. ClampCalculationNode::~ClampCalculationNode() = default;
  643. ErrorOr<String> ClampCalculationNode::to_string() const
  644. {
  645. StringBuilder builder;
  646. TRY(builder.try_append("clamp("sv));
  647. TRY(builder.try_append(TRY(m_min_value->to_string())));
  648. TRY(builder.try_append(", "sv));
  649. TRY(builder.try_append(TRY(m_center_value->to_string())));
  650. TRY(builder.try_append(", "sv));
  651. TRY(builder.try_append(TRY(m_max_value->to_string())));
  652. TRY(builder.try_append(")"sv));
  653. return builder.to_string();
  654. }
  655. Optional<CalculatedStyleValue::ResolvedType> ClampCalculationNode::resolved_type() const
  656. {
  657. // NOTE: We check during parsing that all values have the same type.
  658. return m_min_value->resolved_type();
  659. }
  660. // https://www.w3.org/TR/css-values-4/#determine-the-type-of-a-calculation
  661. Optional<CSSNumericType> ClampCalculationNode::determine_type(PropertyID property_id) const
  662. {
  663. // The result of adding the types of its comma-separated calculations.
  664. auto min_type = m_min_value->determine_type(property_id);
  665. auto center_type = m_center_value->determine_type(property_id);
  666. auto max_type = m_max_value->determine_type(property_id);
  667. if (!min_type.has_value() || !center_type.has_value() || !max_type.has_value())
  668. return {};
  669. auto result = min_type->added_to(*center_type);
  670. if (!result.has_value())
  671. return {};
  672. return result->added_to(*max_type);
  673. }
  674. bool ClampCalculationNode::contains_percentage() const
  675. {
  676. return m_min_value->contains_percentage() || m_center_value->contains_percentage() || m_max_value->contains_percentage();
  677. }
  678. CalculatedStyleValue::CalculationResult ClampCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  679. {
  680. auto min_node = m_min_value->resolve(context, percentage_basis);
  681. auto center_node = m_center_value->resolve(context, percentage_basis);
  682. auto max_node = m_max_value->resolve(context, percentage_basis);
  683. auto min_value = resolve_value(min_node.value(), context);
  684. auto center_value = resolve_value(center_node.value(), context);
  685. auto max_value = resolve_value(max_node.value(), context);
  686. // NOTE: The value should be returned as "max(MIN, min(VAL, MAX))"
  687. auto chosen_value = max(min_value, min(center_value, max_value));
  688. if (chosen_value == min_value)
  689. return min_node;
  690. if (chosen_value == center_value)
  691. return center_node;
  692. if (chosen_value == max_value)
  693. return max_node;
  694. VERIFY_NOT_REACHED();
  695. }
  696. ErrorOr<void> ClampCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  697. {
  698. TRY(m_min_value->for_each_child_node(callback));
  699. TRY(m_center_value->for_each_child_node(callback));
  700. TRY(m_max_value->for_each_child_node(callback));
  701. TRY(callback(m_min_value));
  702. TRY(callback(m_center_value));
  703. TRY(callback(m_max_value));
  704. return {};
  705. }
  706. ErrorOr<void> ClampCalculationNode::dump(StringBuilder& builder, int indent) const
  707. {
  708. TRY(builder.try_appendff("{: >{}}CLAMP:\n", "", indent));
  709. TRY(m_min_value->dump(builder, indent + 2));
  710. TRY(m_center_value->dump(builder, indent + 2));
  711. TRY(m_max_value->dump(builder, indent + 2));
  712. return {};
  713. }
  714. ErrorOr<NonnullOwnPtr<AbsCalculationNode>> AbsCalculationNode::create(NonnullOwnPtr<CalculationNode> value)
  715. {
  716. return adopt_nonnull_own_or_enomem(new (nothrow) AbsCalculationNode(move(value)));
  717. }
  718. AbsCalculationNode::AbsCalculationNode(NonnullOwnPtr<CalculationNode> value)
  719. : CalculationNode(Type::Abs)
  720. , m_value(move(value))
  721. {
  722. }
  723. AbsCalculationNode::~AbsCalculationNode() = default;
  724. ErrorOr<String> AbsCalculationNode::to_string() const
  725. {
  726. StringBuilder builder;
  727. builder.append("abs("sv);
  728. builder.append(TRY(m_value->to_string()));
  729. builder.append(")"sv);
  730. return builder.to_string();
  731. }
  732. Optional<CalculatedStyleValue::ResolvedType> AbsCalculationNode::resolved_type() const
  733. {
  734. return m_value->resolved_type();
  735. }
  736. // https://www.w3.org/TR/css-values-4/#determine-the-type-of-a-calculation
  737. Optional<CSSNumericType> AbsCalculationNode::determine_type(PropertyID property_id) const
  738. {
  739. // The type of its contained calculation.
  740. return m_value->determine_type(property_id);
  741. }
  742. bool AbsCalculationNode::contains_percentage() const
  743. {
  744. return m_value->contains_percentage();
  745. }
  746. CalculatedStyleValue::CalculationResult AbsCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  747. {
  748. auto resolved_type = m_value->resolved_type().value();
  749. auto node_a = m_value->resolve(context, percentage_basis);
  750. auto node_a_value = resolve_value(node_a.value(), context);
  751. if (node_a_value < 0)
  752. return to_resolved_type(resolved_type, -node_a_value);
  753. return node_a;
  754. }
  755. ErrorOr<void> AbsCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  756. {
  757. TRY(m_value->for_each_child_node(callback));
  758. TRY(callback(m_value));
  759. return {};
  760. }
  761. ErrorOr<void> AbsCalculationNode::dump(StringBuilder& builder, int indent) const
  762. {
  763. TRY(builder.try_appendff("{: >{}}ABS: {}\n", "", indent, TRY(to_string())));
  764. return {};
  765. }
  766. ErrorOr<NonnullOwnPtr<SignCalculationNode>> SignCalculationNode::create(NonnullOwnPtr<CalculationNode> value)
  767. {
  768. return adopt_nonnull_own_or_enomem(new (nothrow) SignCalculationNode(move(value)));
  769. }
  770. SignCalculationNode::SignCalculationNode(NonnullOwnPtr<CalculationNode> value)
  771. : CalculationNode(Type::Sign)
  772. , m_value(move(value))
  773. {
  774. }
  775. SignCalculationNode::~SignCalculationNode() = default;
  776. ErrorOr<String> SignCalculationNode::to_string() const
  777. {
  778. StringBuilder builder;
  779. builder.append("sign("sv);
  780. builder.append(TRY(m_value->to_string()));
  781. builder.append(")"sv);
  782. return builder.to_string();
  783. }
  784. Optional<CalculatedStyleValue::ResolvedType> SignCalculationNode::resolved_type() const
  785. {
  786. return CalculatedStyleValue::ResolvedType::Integer;
  787. }
  788. // https://www.w3.org/TR/css-values-4/#determine-the-type-of-a-calculation
  789. Optional<CSSNumericType> SignCalculationNode::determine_type(PropertyID) const
  790. {
  791. // «[ ]» (empty map).
  792. return CSSNumericType {};
  793. }
  794. bool SignCalculationNode::contains_percentage() const
  795. {
  796. return m_value->contains_percentage();
  797. }
  798. CalculatedStyleValue::CalculationResult SignCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  799. {
  800. auto node_a = m_value->resolve(context, percentage_basis);
  801. auto node_a_value = resolve_value(node_a.value(), context);
  802. if (node_a_value < 0)
  803. return { Number(Number::Type::Integer, -1) };
  804. if (node_a_value > 0)
  805. return { Number(Number::Type::Integer, 1) };
  806. return { Number(Number::Type::Integer, 0) };
  807. }
  808. ErrorOr<void> SignCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  809. {
  810. TRY(m_value->for_each_child_node(callback));
  811. TRY(callback(m_value));
  812. return {};
  813. }
  814. ErrorOr<void> SignCalculationNode::dump(StringBuilder& builder, int indent) const
  815. {
  816. TRY(builder.try_appendff("{: >{}}SIGN: {}\n", "", indent, TRY(to_string())));
  817. return {};
  818. }
  819. ErrorOr<NonnullOwnPtr<ConstantCalculationNode>> ConstantCalculationNode::create(ConstantType constant)
  820. {
  821. return adopt_nonnull_own_or_enomem(new (nothrow) ConstantCalculationNode(constant));
  822. }
  823. ConstantCalculationNode::ConstantCalculationNode(ConstantType constant)
  824. : CalculationNode(Type::Constant)
  825. , m_constant(constant)
  826. {
  827. }
  828. ConstantCalculationNode::~ConstantCalculationNode() = default;
  829. ErrorOr<String> ConstantCalculationNode::to_string() const
  830. {
  831. switch (m_constant) {
  832. case CalculationNode::ConstantType::E:
  833. return "e"_string;
  834. case CalculationNode::ConstantType::Pi:
  835. return "pi"_string;
  836. case CalculationNode::ConstantType::Infinity:
  837. return "infinity"_string;
  838. case CalculationNode::ConstantType::MinusInfinity:
  839. return "-infinity"_string;
  840. case CalculationNode::ConstantType::NaN:
  841. return "NaN"_string;
  842. }
  843. VERIFY_NOT_REACHED();
  844. }
  845. Optional<CalculatedStyleValue::ResolvedType> ConstantCalculationNode::resolved_type() const
  846. {
  847. return CalculatedStyleValue::ResolvedType::Number;
  848. }
  849. // https://www.w3.org/TR/css-values-4/#determine-the-type-of-a-calculation
  850. Optional<CSSNumericType> ConstantCalculationNode::determine_type(PropertyID) const
  851. {
  852. // Anything else is a terminal value, whose type is determined based on its CSS type:
  853. // -> <calc-constant>
  854. // the type is «[ ]» (empty map)
  855. return CSSNumericType {};
  856. }
  857. CalculatedStyleValue::CalculationResult ConstantCalculationNode::resolve([[maybe_unused]] Optional<Length::ResolutionContext const&> context, [[maybe_unused]] CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  858. {
  859. switch (m_constant) {
  860. case CalculationNode::ConstantType::E:
  861. return { Number(Number::Type::Number, M_E) };
  862. case CalculationNode::ConstantType::Pi:
  863. return { Number(Number::Type::Number, M_PI) };
  864. // FIXME: We need to keep track of Infinity and NaN across all nodes, since they require special handling.
  865. case CalculationNode::ConstantType::Infinity:
  866. return { Number(Number::Type::Number, NumericLimits<float>::max()) };
  867. case CalculationNode::ConstantType::MinusInfinity:
  868. return { Number(Number::Type::Number, NumericLimits<float>::lowest()) };
  869. case CalculationNode::ConstantType::NaN:
  870. return { Number(Number::Type::Number, NAN) };
  871. }
  872. VERIFY_NOT_REACHED();
  873. }
  874. ErrorOr<void> ConstantCalculationNode::for_each_child_node([[maybe_unused]] Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  875. {
  876. return {};
  877. }
  878. ErrorOr<void> ConstantCalculationNode::dump(StringBuilder& builder, int indent) const
  879. {
  880. TRY(builder.try_appendff("{: >{}}CONSTANT: {}\n", "", indent, TRY(to_string())));
  881. return {};
  882. }
  883. ErrorOr<NonnullOwnPtr<SinCalculationNode>> SinCalculationNode::create(NonnullOwnPtr<CalculationNode> value)
  884. {
  885. return adopt_nonnull_own_or_enomem(new (nothrow) SinCalculationNode(move(value)));
  886. }
  887. SinCalculationNode::SinCalculationNode(NonnullOwnPtr<CalculationNode> value)
  888. : CalculationNode(Type::Sin)
  889. , m_value(move(value))
  890. {
  891. }
  892. SinCalculationNode::~SinCalculationNode() = default;
  893. ErrorOr<String> SinCalculationNode::to_string() const
  894. {
  895. StringBuilder builder;
  896. builder.append("sin("sv);
  897. builder.append(TRY(m_value->to_string()));
  898. builder.append(")"sv);
  899. return builder.to_string();
  900. }
  901. Optional<CalculatedStyleValue::ResolvedType> SinCalculationNode::resolved_type() const
  902. {
  903. return CalculatedStyleValue::ResolvedType::Number;
  904. }
  905. // https://www.w3.org/TR/css-values-4/#determine-the-type-of-a-calculation
  906. Optional<CSSNumericType> SinCalculationNode::determine_type(PropertyID) const
  907. {
  908. // «[ ]» (empty map).
  909. return CSSNumericType {};
  910. }
  911. bool SinCalculationNode::contains_percentage() const
  912. {
  913. return m_value->contains_percentage();
  914. }
  915. CalculatedStyleValue::CalculationResult SinCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  916. {
  917. auto node_a = m_value->resolve(context, percentage_basis);
  918. auto node_a_value = resolve_value_radians(node_a.value());
  919. auto result = sin(node_a_value);
  920. return { Number(Number::Type::Number, result) };
  921. }
  922. ErrorOr<void> SinCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  923. {
  924. TRY(m_value->for_each_child_node(callback));
  925. TRY(callback(m_value));
  926. return {};
  927. }
  928. ErrorOr<void> SinCalculationNode::dump(StringBuilder& builder, int indent) const
  929. {
  930. TRY(builder.try_appendff("{: >{}}SIN: {}\n", "", indent, TRY(to_string())));
  931. return {};
  932. }
  933. ErrorOr<NonnullOwnPtr<CosCalculationNode>> CosCalculationNode::create(NonnullOwnPtr<CalculationNode> value)
  934. {
  935. return adopt_nonnull_own_or_enomem(new (nothrow) CosCalculationNode(move(value)));
  936. }
  937. CosCalculationNode::CosCalculationNode(NonnullOwnPtr<CalculationNode> value)
  938. : CalculationNode(Type::Cos)
  939. , m_value(move(value))
  940. {
  941. }
  942. CosCalculationNode::~CosCalculationNode() = default;
  943. ErrorOr<String> CosCalculationNode::to_string() const
  944. {
  945. StringBuilder builder;
  946. builder.append("cos("sv);
  947. builder.append(TRY(m_value->to_string()));
  948. builder.append(")"sv);
  949. return builder.to_string();
  950. }
  951. Optional<CalculatedStyleValue::ResolvedType> CosCalculationNode::resolved_type() const
  952. {
  953. return CalculatedStyleValue::ResolvedType::Number;
  954. }
  955. // https://www.w3.org/TR/css-values-4/#determine-the-type-of-a-calculation
  956. Optional<CSSNumericType> CosCalculationNode::determine_type(PropertyID) const
  957. {
  958. // «[ ]» (empty map).
  959. return CSSNumericType {};
  960. }
  961. bool CosCalculationNode::contains_percentage() const
  962. {
  963. return m_value->contains_percentage();
  964. }
  965. CalculatedStyleValue::CalculationResult CosCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  966. {
  967. auto node_a = m_value->resolve(context, percentage_basis);
  968. auto node_a_value = resolve_value_radians(node_a.value());
  969. auto result = cos(node_a_value);
  970. return { Number(Number::Type::Number, result) };
  971. }
  972. ErrorOr<void> CosCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  973. {
  974. TRY(m_value->for_each_child_node(callback));
  975. TRY(callback(m_value));
  976. return {};
  977. }
  978. ErrorOr<void> CosCalculationNode::dump(StringBuilder& builder, int indent) const
  979. {
  980. TRY(builder.try_appendff("{: >{}}COS: {}\n", "", indent, TRY(to_string())));
  981. return {};
  982. }
  983. ErrorOr<NonnullOwnPtr<TanCalculationNode>> TanCalculationNode::create(NonnullOwnPtr<CalculationNode> value)
  984. {
  985. return adopt_nonnull_own_or_enomem(new (nothrow) TanCalculationNode(move(value)));
  986. }
  987. TanCalculationNode::TanCalculationNode(NonnullOwnPtr<CalculationNode> value)
  988. : CalculationNode(Type::Tan)
  989. , m_value(move(value))
  990. {
  991. }
  992. TanCalculationNode::~TanCalculationNode() = default;
  993. ErrorOr<String> TanCalculationNode::to_string() const
  994. {
  995. StringBuilder builder;
  996. builder.append("tan("sv);
  997. builder.append(TRY(m_value->to_string()));
  998. builder.append(")"sv);
  999. return builder.to_string();
  1000. }
  1001. Optional<CalculatedStyleValue::ResolvedType> TanCalculationNode::resolved_type() const
  1002. {
  1003. return CalculatedStyleValue::ResolvedType::Number;
  1004. }
  1005. // https://www.w3.org/TR/css-values-4/#determine-the-type-of-a-calculation
  1006. Optional<CSSNumericType> TanCalculationNode::determine_type(PropertyID) const
  1007. {
  1008. // «[ ]» (empty map).
  1009. return CSSNumericType {};
  1010. }
  1011. bool TanCalculationNode::contains_percentage() const
  1012. {
  1013. return m_value->contains_percentage();
  1014. }
  1015. CalculatedStyleValue::CalculationResult TanCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  1016. {
  1017. auto node_a = m_value->resolve(context, percentage_basis);
  1018. auto node_a_value = resolve_value_radians(node_a.value());
  1019. auto result = tan(node_a_value);
  1020. return { Number(Number::Type::Number, result) };
  1021. }
  1022. ErrorOr<void> TanCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  1023. {
  1024. TRY(m_value->for_each_child_node(callback));
  1025. TRY(callback(m_value));
  1026. return {};
  1027. }
  1028. ErrorOr<void> TanCalculationNode::dump(StringBuilder& builder, int indent) const
  1029. {
  1030. TRY(builder.try_appendff("{: >{}}TAN: {}\n", "", indent, TRY(to_string())));
  1031. return {};
  1032. }
  1033. ErrorOr<NonnullOwnPtr<AsinCalculationNode>> AsinCalculationNode::create(NonnullOwnPtr<CalculationNode> value)
  1034. {
  1035. return adopt_nonnull_own_or_enomem(new (nothrow) AsinCalculationNode(move(value)));
  1036. }
  1037. AsinCalculationNode::AsinCalculationNode(NonnullOwnPtr<CalculationNode> value)
  1038. : CalculationNode(Type::Asin)
  1039. , m_value(move(value))
  1040. {
  1041. }
  1042. AsinCalculationNode::~AsinCalculationNode() = default;
  1043. ErrorOr<String> AsinCalculationNode::to_string() const
  1044. {
  1045. StringBuilder builder;
  1046. builder.append("asin("sv);
  1047. builder.append(TRY(m_value->to_string()));
  1048. builder.append(")"sv);
  1049. return builder.to_string();
  1050. }
  1051. Optional<CalculatedStyleValue::ResolvedType> AsinCalculationNode::resolved_type() const
  1052. {
  1053. return CalculatedStyleValue::ResolvedType::Angle;
  1054. }
  1055. // https://www.w3.org/TR/css-values-4/#determine-the-type-of-a-calculation
  1056. Optional<CSSNumericType> AsinCalculationNode::determine_type(PropertyID) const
  1057. {
  1058. // «[ "angle" → 1 ]».
  1059. return CSSNumericType { CSSNumericType::BaseType::Angle, 1 };
  1060. }
  1061. bool AsinCalculationNode::contains_percentage() const
  1062. {
  1063. return m_value->contains_percentage();
  1064. }
  1065. CalculatedStyleValue::CalculationResult AsinCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  1066. {
  1067. auto node_a = m_value->resolve(context, percentage_basis);
  1068. auto node_a_value = resolve_value(node_a.value(), context);
  1069. auto result = asin(node_a_value);
  1070. return { Angle(result, Angle::Type::Rad) };
  1071. }
  1072. ErrorOr<void> AsinCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  1073. {
  1074. TRY(m_value->for_each_child_node(callback));
  1075. TRY(callback(m_value));
  1076. return {};
  1077. }
  1078. ErrorOr<void> AsinCalculationNode::dump(StringBuilder& builder, int indent) const
  1079. {
  1080. TRY(builder.try_appendff("{: >{}}ASIN: {}\n", "", indent, TRY(to_string())));
  1081. return {};
  1082. }
  1083. ErrorOr<NonnullOwnPtr<AcosCalculationNode>> AcosCalculationNode::create(NonnullOwnPtr<CalculationNode> value)
  1084. {
  1085. return adopt_nonnull_own_or_enomem(new (nothrow) AcosCalculationNode(move(value)));
  1086. }
  1087. AcosCalculationNode::AcosCalculationNode(NonnullOwnPtr<CalculationNode> value)
  1088. : CalculationNode(Type::Acos)
  1089. , m_value(move(value))
  1090. {
  1091. }
  1092. AcosCalculationNode::~AcosCalculationNode() = default;
  1093. ErrorOr<String> AcosCalculationNode::to_string() const
  1094. {
  1095. StringBuilder builder;
  1096. builder.append("acos("sv);
  1097. builder.append(TRY(m_value->to_string()));
  1098. builder.append(")"sv);
  1099. return builder.to_string();
  1100. }
  1101. Optional<CalculatedStyleValue::ResolvedType> AcosCalculationNode::resolved_type() const
  1102. {
  1103. return CalculatedStyleValue::ResolvedType::Angle;
  1104. }
  1105. // https://www.w3.org/TR/css-values-4/#determine-the-type-of-a-calculation
  1106. Optional<CSSNumericType> AcosCalculationNode::determine_type(PropertyID) const
  1107. {
  1108. // «[ "angle" → 1 ]».
  1109. return CSSNumericType { CSSNumericType::BaseType::Angle, 1 };
  1110. }
  1111. bool AcosCalculationNode::contains_percentage() const
  1112. {
  1113. return m_value->contains_percentage();
  1114. }
  1115. CalculatedStyleValue::CalculationResult AcosCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  1116. {
  1117. auto node_a = m_value->resolve(context, percentage_basis);
  1118. auto node_a_value = resolve_value(node_a.value(), context);
  1119. auto result = acos(node_a_value);
  1120. return { Angle(result, Angle::Type::Rad) };
  1121. }
  1122. ErrorOr<void> AcosCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  1123. {
  1124. TRY(m_value->for_each_child_node(callback));
  1125. TRY(callback(m_value));
  1126. return {};
  1127. }
  1128. ErrorOr<void> AcosCalculationNode::dump(StringBuilder& builder, int indent) const
  1129. {
  1130. TRY(builder.try_appendff("{: >{}}ACOS: {}\n", "", indent, TRY(to_string())));
  1131. return {};
  1132. }
  1133. ErrorOr<NonnullOwnPtr<AtanCalculationNode>> AtanCalculationNode::create(NonnullOwnPtr<CalculationNode> value)
  1134. {
  1135. return adopt_nonnull_own_or_enomem(new (nothrow) AtanCalculationNode(move(value)));
  1136. }
  1137. AtanCalculationNode::AtanCalculationNode(NonnullOwnPtr<CalculationNode> value)
  1138. : CalculationNode(Type::Atan)
  1139. , m_value(move(value))
  1140. {
  1141. }
  1142. AtanCalculationNode::~AtanCalculationNode() = default;
  1143. ErrorOr<String> AtanCalculationNode::to_string() const
  1144. {
  1145. StringBuilder builder;
  1146. builder.append("atan("sv);
  1147. builder.append(TRY(m_value->to_string()));
  1148. builder.append(")"sv);
  1149. return builder.to_string();
  1150. }
  1151. Optional<CalculatedStyleValue::ResolvedType> AtanCalculationNode::resolved_type() const
  1152. {
  1153. return CalculatedStyleValue::ResolvedType::Angle;
  1154. }
  1155. // https://www.w3.org/TR/css-values-4/#determine-the-type-of-a-calculation
  1156. Optional<CSSNumericType> AtanCalculationNode::determine_type(PropertyID) const
  1157. {
  1158. // «[ "angle" → 1 ]».
  1159. return CSSNumericType { CSSNumericType::BaseType::Angle, 1 };
  1160. }
  1161. bool AtanCalculationNode::contains_percentage() const
  1162. {
  1163. return m_value->contains_percentage();
  1164. }
  1165. CalculatedStyleValue::CalculationResult AtanCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  1166. {
  1167. auto node_a = m_value->resolve(context, percentage_basis);
  1168. auto node_a_value = resolve_value(node_a.value(), context);
  1169. auto result = atan(node_a_value);
  1170. return { Angle(result, Angle::Type::Rad) };
  1171. }
  1172. ErrorOr<void> AtanCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  1173. {
  1174. TRY(m_value->for_each_child_node(callback));
  1175. TRY(callback(m_value));
  1176. return {};
  1177. }
  1178. ErrorOr<void> AtanCalculationNode::dump(StringBuilder& builder, int indent) const
  1179. {
  1180. TRY(builder.try_appendff("{: >{}}ATAN: {}\n", "", indent, TRY(to_string())));
  1181. return {};
  1182. }
  1183. ErrorOr<NonnullOwnPtr<Atan2CalculationNode>> Atan2CalculationNode::create(NonnullOwnPtr<CalculationNode> y, NonnullOwnPtr<CalculationNode> x)
  1184. {
  1185. return adopt_nonnull_own_or_enomem(new (nothrow) Atan2CalculationNode(move(y), move(x)));
  1186. }
  1187. Atan2CalculationNode::Atan2CalculationNode(NonnullOwnPtr<CalculationNode> y, NonnullOwnPtr<CalculationNode> x)
  1188. : CalculationNode(Type::Atan2)
  1189. , m_y(move(y))
  1190. , m_x(move(x))
  1191. {
  1192. }
  1193. Atan2CalculationNode::~Atan2CalculationNode() = default;
  1194. ErrorOr<String> Atan2CalculationNode::to_string() const
  1195. {
  1196. StringBuilder builder;
  1197. builder.append("atan2("sv);
  1198. builder.append(TRY(m_y->to_string()));
  1199. builder.append(", "sv);
  1200. builder.append(TRY(m_x->to_string()));
  1201. builder.append(")"sv);
  1202. return builder.to_string();
  1203. }
  1204. Optional<CalculatedStyleValue::ResolvedType> Atan2CalculationNode::resolved_type() const
  1205. {
  1206. return CalculatedStyleValue::ResolvedType::Angle;
  1207. }
  1208. // https://www.w3.org/TR/css-values-4/#determine-the-type-of-a-calculation
  1209. Optional<CSSNumericType> Atan2CalculationNode::determine_type(PropertyID) const
  1210. {
  1211. // «[ "angle" → 1 ]».
  1212. return CSSNumericType { CSSNumericType::BaseType::Angle, 1 };
  1213. }
  1214. bool Atan2CalculationNode::contains_percentage() const
  1215. {
  1216. return m_y->contains_percentage() || m_x->contains_percentage();
  1217. }
  1218. CalculatedStyleValue::CalculationResult Atan2CalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  1219. {
  1220. auto node_a = m_y->resolve(context, percentage_basis);
  1221. auto node_a_value = resolve_value(node_a.value(), context);
  1222. auto node_b = m_x->resolve(context, percentage_basis);
  1223. auto node_b_value = resolve_value(node_b.value(), context);
  1224. auto result = atan2(node_a_value, node_b_value);
  1225. return { Angle(result, Angle::Type::Rad) };
  1226. }
  1227. ErrorOr<void> Atan2CalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  1228. {
  1229. TRY(m_y->for_each_child_node(callback));
  1230. TRY(m_x->for_each_child_node(callback));
  1231. TRY(callback(m_y));
  1232. TRY(callback(m_x));
  1233. return {};
  1234. }
  1235. ErrorOr<void> Atan2CalculationNode::dump(StringBuilder& builder, int indent) const
  1236. {
  1237. TRY(builder.try_appendff("{: >{}}ATAN2: {}\n", "", indent, TRY(to_string())));
  1238. return {};
  1239. }
  1240. ErrorOr<NonnullOwnPtr<PowCalculationNode>> PowCalculationNode::create(NonnullOwnPtr<CalculationNode> x, NonnullOwnPtr<CalculationNode> y)
  1241. {
  1242. return adopt_nonnull_own_or_enomem(new (nothrow) PowCalculationNode(move(x), move(y)));
  1243. }
  1244. PowCalculationNode::PowCalculationNode(NonnullOwnPtr<CalculationNode> x, NonnullOwnPtr<CalculationNode> y)
  1245. : CalculationNode(Type::Pow)
  1246. , m_x(move(x))
  1247. , m_y(move(y))
  1248. {
  1249. }
  1250. PowCalculationNode::~PowCalculationNode() = default;
  1251. ErrorOr<String> PowCalculationNode::to_string() const
  1252. {
  1253. StringBuilder builder;
  1254. builder.append("pow("sv);
  1255. builder.append(TRY(m_x->to_string()));
  1256. builder.append(", "sv);
  1257. builder.append(TRY(m_y->to_string()));
  1258. builder.append(")"sv);
  1259. return builder.to_string();
  1260. }
  1261. Optional<CalculatedStyleValue::ResolvedType> PowCalculationNode::resolved_type() const
  1262. {
  1263. return CalculatedStyleValue::ResolvedType::Number;
  1264. }
  1265. // https://www.w3.org/TR/css-values-4/#determine-the-type-of-a-calculation
  1266. Optional<CSSNumericType> PowCalculationNode::determine_type(PropertyID) const
  1267. {
  1268. // «[ ]» (empty map).
  1269. return CSSNumericType {};
  1270. }
  1271. CalculatedStyleValue::CalculationResult PowCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  1272. {
  1273. auto node_a = m_x->resolve(context, percentage_basis);
  1274. auto node_a_value = resolve_value(node_a.value(), context);
  1275. auto node_b = m_y->resolve(context, percentage_basis);
  1276. auto node_b_value = resolve_value(node_b.value(), context);
  1277. auto result = pow(node_a_value, node_b_value);
  1278. return { Number(Number::Type::Number, result) };
  1279. }
  1280. ErrorOr<void> PowCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  1281. {
  1282. TRY(m_x->for_each_child_node(callback));
  1283. TRY(m_y->for_each_child_node(callback));
  1284. TRY(callback(m_x));
  1285. TRY(callback(m_y));
  1286. return {};
  1287. }
  1288. ErrorOr<void> PowCalculationNode::dump(StringBuilder& builder, int indent) const
  1289. {
  1290. TRY(builder.try_appendff("{: >{}}POW: {}\n", "", indent, TRY(to_string())));
  1291. return {};
  1292. }
  1293. ErrorOr<NonnullOwnPtr<SqrtCalculationNode>> SqrtCalculationNode::create(NonnullOwnPtr<CalculationNode> value)
  1294. {
  1295. return adopt_nonnull_own_or_enomem(new (nothrow) SqrtCalculationNode(move(value)));
  1296. }
  1297. SqrtCalculationNode::SqrtCalculationNode(NonnullOwnPtr<CalculationNode> value)
  1298. : CalculationNode(Type::Sqrt)
  1299. , m_value(move(value))
  1300. {
  1301. }
  1302. SqrtCalculationNode::~SqrtCalculationNode() = default;
  1303. ErrorOr<String> SqrtCalculationNode::to_string() const
  1304. {
  1305. StringBuilder builder;
  1306. builder.append("sqrt("sv);
  1307. builder.append(TRY(m_value->to_string()));
  1308. builder.append(")"sv);
  1309. return builder.to_string();
  1310. }
  1311. Optional<CalculatedStyleValue::ResolvedType> SqrtCalculationNode::resolved_type() const
  1312. {
  1313. return CalculatedStyleValue::ResolvedType::Number;
  1314. }
  1315. // https://www.w3.org/TR/css-values-4/#determine-the-type-of-a-calculation
  1316. Optional<CSSNumericType> SqrtCalculationNode::determine_type(PropertyID) const
  1317. {
  1318. // «[ ]» (empty map).
  1319. return CSSNumericType {};
  1320. }
  1321. CalculatedStyleValue::CalculationResult SqrtCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  1322. {
  1323. auto node_a = m_value->resolve(context, percentage_basis);
  1324. auto node_a_value = resolve_value(node_a.value(), context);
  1325. auto result = sqrt(node_a_value);
  1326. return { Number(Number::Type::Number, result) };
  1327. }
  1328. ErrorOr<void> SqrtCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  1329. {
  1330. TRY(m_value->for_each_child_node(callback));
  1331. TRY(callback(m_value));
  1332. return {};
  1333. }
  1334. ErrorOr<void> SqrtCalculationNode::dump(StringBuilder& builder, int indent) const
  1335. {
  1336. TRY(builder.try_appendff("{: >{}}SQRT: {}\n", "", indent, TRY(to_string())));
  1337. return {};
  1338. }
  1339. ErrorOr<NonnullOwnPtr<HypotCalculationNode>> HypotCalculationNode::create(Vector<NonnullOwnPtr<Web::CSS::CalculationNode>> values)
  1340. {
  1341. return adopt_nonnull_own_or_enomem(new (nothrow) HypotCalculationNode(move(values)));
  1342. }
  1343. HypotCalculationNode::HypotCalculationNode(Vector<NonnullOwnPtr<CalculationNode>> values)
  1344. : CalculationNode(Type::Hypot)
  1345. , m_values(move(values))
  1346. {
  1347. }
  1348. HypotCalculationNode::~HypotCalculationNode() = default;
  1349. ErrorOr<String> HypotCalculationNode::to_string() const
  1350. {
  1351. StringBuilder builder;
  1352. TRY(builder.try_append("hypot("sv));
  1353. for (size_t i = 0; i < m_values.size(); ++i) {
  1354. if (i != 0)
  1355. TRY(builder.try_append(", "sv));
  1356. TRY(builder.try_append(TRY(m_values[i]->to_string())));
  1357. }
  1358. TRY(builder.try_append(")"sv));
  1359. return builder.to_string();
  1360. }
  1361. Optional<CalculatedStyleValue::ResolvedType> HypotCalculationNode::resolved_type() const
  1362. {
  1363. // NOTE: We check during parsing that all values have the same type.
  1364. return m_values[0]->resolved_type();
  1365. }
  1366. // https://www.w3.org/TR/css-values-4/#determine-the-type-of-a-calculation
  1367. Optional<CSSNumericType> HypotCalculationNode::determine_type(PropertyID property_id) const
  1368. {
  1369. // The result of adding the types of its comma-separated calculations.
  1370. return add_the_types(m_values, property_id);
  1371. }
  1372. bool HypotCalculationNode::contains_percentage() const
  1373. {
  1374. for (auto const& value : m_values) {
  1375. if (value->contains_percentage())
  1376. return true;
  1377. }
  1378. return false;
  1379. }
  1380. CalculatedStyleValue::CalculationResult HypotCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  1381. {
  1382. double square_sum = 0.0;
  1383. for (auto const& value : m_values) {
  1384. auto child_resolved = value->resolve(context, percentage_basis);
  1385. auto child_value = resolve_value(child_resolved.value(), context);
  1386. square_sum += child_value * child_value;
  1387. }
  1388. auto result = sqrt(square_sum);
  1389. return to_resolved_type(resolved_type().value(), result);
  1390. }
  1391. ErrorOr<void> HypotCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  1392. {
  1393. for (auto& value : m_values) {
  1394. TRY(value->for_each_child_node(callback));
  1395. TRY(callback(value));
  1396. }
  1397. return {};
  1398. }
  1399. ErrorOr<void> HypotCalculationNode::dump(StringBuilder& builder, int indent) const
  1400. {
  1401. TRY(builder.try_appendff("{: >{}}HYPOT:\n", "", indent));
  1402. for (auto const& value : m_values)
  1403. TRY(value->dump(builder, indent + 2));
  1404. return {};
  1405. }
  1406. ErrorOr<NonnullOwnPtr<LogCalculationNode>> LogCalculationNode::create(NonnullOwnPtr<CalculationNode> x, NonnullOwnPtr<CalculationNode> y)
  1407. {
  1408. return adopt_nonnull_own_or_enomem(new (nothrow) LogCalculationNode(move(x), move(y)));
  1409. }
  1410. LogCalculationNode::LogCalculationNode(NonnullOwnPtr<CalculationNode> x, NonnullOwnPtr<CalculationNode> y)
  1411. : CalculationNode(Type::Log)
  1412. , m_x(move(x))
  1413. , m_y(move(y))
  1414. {
  1415. }
  1416. LogCalculationNode::~LogCalculationNode() = default;
  1417. ErrorOr<String> LogCalculationNode::to_string() const
  1418. {
  1419. StringBuilder builder;
  1420. builder.append("log("sv);
  1421. builder.append(TRY(m_x->to_string()));
  1422. builder.append(", "sv);
  1423. builder.append(TRY(m_y->to_string()));
  1424. builder.append(")"sv);
  1425. return builder.to_string();
  1426. }
  1427. Optional<CalculatedStyleValue::ResolvedType> LogCalculationNode::resolved_type() const
  1428. {
  1429. return CalculatedStyleValue::ResolvedType::Number;
  1430. }
  1431. // https://www.w3.org/TR/css-values-4/#determine-the-type-of-a-calculation
  1432. Optional<CSSNumericType> LogCalculationNode::determine_type(PropertyID) const
  1433. {
  1434. // «[ ]» (empty map).
  1435. return CSSNumericType {};
  1436. }
  1437. CalculatedStyleValue::CalculationResult LogCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  1438. {
  1439. auto node_a = m_x->resolve(context, percentage_basis);
  1440. auto node_a_value = resolve_value(node_a.value(), context);
  1441. auto node_b = m_y->resolve(context, percentage_basis);
  1442. auto node_b_value = resolve_value(node_b.value(), context);
  1443. auto result = log2(node_a_value) / log2(node_b_value);
  1444. return { Number(Number::Type::Number, result) };
  1445. }
  1446. ErrorOr<void> LogCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  1447. {
  1448. TRY(m_x->for_each_child_node(callback));
  1449. TRY(m_y->for_each_child_node(callback));
  1450. TRY(callback(m_x));
  1451. TRY(callback(m_y));
  1452. return {};
  1453. }
  1454. ErrorOr<void> LogCalculationNode::dump(StringBuilder& builder, int indent) const
  1455. {
  1456. TRY(builder.try_appendff("{: >{}}LOG: {}\n", "", indent, TRY(to_string())));
  1457. return {};
  1458. }
  1459. ErrorOr<NonnullOwnPtr<ExpCalculationNode>> ExpCalculationNode::create(NonnullOwnPtr<CalculationNode> value)
  1460. {
  1461. return adopt_nonnull_own_or_enomem(new (nothrow) ExpCalculationNode(move(value)));
  1462. }
  1463. ExpCalculationNode::ExpCalculationNode(NonnullOwnPtr<CalculationNode> value)
  1464. : CalculationNode(Type::Exp)
  1465. , m_value(move(value))
  1466. {
  1467. }
  1468. ExpCalculationNode::~ExpCalculationNode() = default;
  1469. ErrorOr<String> ExpCalculationNode::to_string() const
  1470. {
  1471. StringBuilder builder;
  1472. builder.append("exp("sv);
  1473. builder.append(TRY(m_value->to_string()));
  1474. builder.append(")"sv);
  1475. return builder.to_string();
  1476. }
  1477. Optional<CalculatedStyleValue::ResolvedType> ExpCalculationNode::resolved_type() const
  1478. {
  1479. return CalculatedStyleValue::ResolvedType::Number;
  1480. }
  1481. // https://www.w3.org/TR/css-values-4/#determine-the-type-of-a-calculation
  1482. Optional<CSSNumericType> ExpCalculationNode::determine_type(PropertyID) const
  1483. {
  1484. // «[ ]» (empty map).
  1485. return CSSNumericType {};
  1486. }
  1487. CalculatedStyleValue::CalculationResult ExpCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  1488. {
  1489. auto node_a = m_value->resolve(context, percentage_basis);
  1490. auto node_a_value = resolve_value(node_a.value(), context);
  1491. auto result = exp(node_a_value);
  1492. return { Number(Number::Type::Number, result) };
  1493. }
  1494. ErrorOr<void> ExpCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  1495. {
  1496. TRY(m_value->for_each_child_node(callback));
  1497. TRY(callback(m_value));
  1498. return {};
  1499. }
  1500. ErrorOr<void> ExpCalculationNode::dump(StringBuilder& builder, int indent) const
  1501. {
  1502. TRY(builder.try_appendff("{: >{}}EXP: {}\n", "", indent, TRY(to_string())));
  1503. return {};
  1504. }
  1505. ErrorOr<NonnullOwnPtr<RoundCalculationNode>> RoundCalculationNode::create(RoundingStrategy strategy, NonnullOwnPtr<CalculationNode> x, NonnullOwnPtr<CalculationNode> y)
  1506. {
  1507. return adopt_nonnull_own_or_enomem(new (nothrow) RoundCalculationNode(strategy, move(x), move(y)));
  1508. }
  1509. RoundCalculationNode::RoundCalculationNode(RoundingStrategy mode, NonnullOwnPtr<CalculationNode> x, NonnullOwnPtr<CalculationNode> y)
  1510. : CalculationNode(Type::Round)
  1511. , m_strategy(mode)
  1512. , m_x(move(x))
  1513. , m_y(move(y))
  1514. {
  1515. }
  1516. RoundCalculationNode::~RoundCalculationNode() = default;
  1517. ErrorOr<String> RoundCalculationNode::to_string() const
  1518. {
  1519. StringBuilder builder;
  1520. builder.append("round("sv);
  1521. builder.append(CSS::to_string(m_strategy));
  1522. builder.append(", "sv);
  1523. builder.append(TRY(m_x->to_string()));
  1524. builder.append(", "sv);
  1525. builder.append(TRY(m_y->to_string()));
  1526. builder.append(")"sv);
  1527. return builder.to_string();
  1528. }
  1529. Optional<CalculatedStyleValue::ResolvedType> RoundCalculationNode::resolved_type() const
  1530. {
  1531. // Note: We check during parsing that all values have the same type
  1532. return m_x->resolved_type();
  1533. }
  1534. // https://www.w3.org/TR/css-values-4/#determine-the-type-of-a-calculation
  1535. Optional<CSSNumericType> RoundCalculationNode::determine_type(PropertyID property_id) const
  1536. {
  1537. // The result of adding the types of its comma-separated calculations.
  1538. auto x_type = m_x->determine_type(property_id);
  1539. auto y_type = m_y->determine_type(property_id);
  1540. if (!x_type.has_value() || !y_type.has_value())
  1541. return {};
  1542. return x_type->added_to(*y_type);
  1543. }
  1544. bool RoundCalculationNode::contains_percentage() const
  1545. {
  1546. return m_x->contains_percentage() || m_y->contains_percentage();
  1547. }
  1548. CalculatedStyleValue::CalculationResult RoundCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  1549. {
  1550. auto resolved_type = m_x->resolved_type().value();
  1551. auto node_a = m_x->resolve(context, percentage_basis);
  1552. auto node_b = m_y->resolve(context, percentage_basis);
  1553. auto node_a_value = resolve_value(node_a.value(), context);
  1554. auto node_b_value = resolve_value(node_b.value(), context);
  1555. auto upper_b = ceil(node_a_value / node_b_value) * node_b_value;
  1556. auto lower_b = floor(node_a_value / node_b_value) * node_b_value;
  1557. if (m_strategy == RoundingStrategy::Nearest) {
  1558. auto upper_diff = fabs(upper_b - node_a_value);
  1559. auto lower_diff = fabs(node_a_value - lower_b);
  1560. auto rounded_value = upper_diff < lower_diff ? upper_b : lower_b;
  1561. return to_resolved_type(resolved_type, rounded_value);
  1562. }
  1563. if (m_strategy == RoundingStrategy::Up) {
  1564. return to_resolved_type(resolved_type, upper_b);
  1565. }
  1566. if (m_strategy == RoundingStrategy::Down) {
  1567. return to_resolved_type(resolved_type, lower_b);
  1568. }
  1569. if (m_strategy == RoundingStrategy::ToZero) {
  1570. auto upper_diff = fabs(upper_b);
  1571. auto lower_diff = fabs(lower_b);
  1572. auto rounded_value = upper_diff < lower_diff ? upper_b : lower_b;
  1573. return to_resolved_type(resolved_type, rounded_value);
  1574. }
  1575. VERIFY_NOT_REACHED();
  1576. }
  1577. ErrorOr<void> RoundCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  1578. {
  1579. TRY(m_x->for_each_child_node(callback));
  1580. TRY(m_y->for_each_child_node(callback));
  1581. TRY(callback(m_x));
  1582. TRY(callback(m_y));
  1583. return {};
  1584. }
  1585. ErrorOr<void> RoundCalculationNode::dump(StringBuilder& builder, int indent) const
  1586. {
  1587. TRY(builder.try_appendff("{: >{}}ROUND: {}\n", "", indent, TRY(to_string())));
  1588. return {};
  1589. }
  1590. ErrorOr<NonnullOwnPtr<ModCalculationNode>> ModCalculationNode::create(NonnullOwnPtr<CalculationNode> x, NonnullOwnPtr<CalculationNode> y)
  1591. {
  1592. return adopt_nonnull_own_or_enomem(new (nothrow) ModCalculationNode(move(x), move(y)));
  1593. }
  1594. ModCalculationNode::ModCalculationNode(NonnullOwnPtr<CalculationNode> x, NonnullOwnPtr<CalculationNode> y)
  1595. : CalculationNode(Type::Mod)
  1596. , m_x(move(x))
  1597. , m_y(move(y))
  1598. {
  1599. }
  1600. ModCalculationNode::~ModCalculationNode() = default;
  1601. ErrorOr<String> ModCalculationNode::to_string() const
  1602. {
  1603. StringBuilder builder;
  1604. builder.append("mod("sv);
  1605. builder.append(TRY(m_x->to_string()));
  1606. builder.append(", "sv);
  1607. builder.append(TRY(m_y->to_string()));
  1608. builder.append(")"sv);
  1609. return builder.to_string();
  1610. }
  1611. Optional<CalculatedStyleValue::ResolvedType> ModCalculationNode::resolved_type() const
  1612. {
  1613. // Note: We check during parsing that all values have the same type
  1614. return m_x->resolved_type();
  1615. }
  1616. // https://www.w3.org/TR/css-values-4/#determine-the-type-of-a-calculation
  1617. Optional<CSSNumericType> ModCalculationNode::determine_type(PropertyID property_id) const
  1618. {
  1619. // The result of adding the types of its comma-separated calculations.
  1620. auto x_type = m_x->determine_type(property_id);
  1621. auto y_type = m_y->determine_type(property_id);
  1622. if (!x_type.has_value() || !y_type.has_value())
  1623. return {};
  1624. return x_type->added_to(*y_type);
  1625. }
  1626. bool ModCalculationNode::contains_percentage() const
  1627. {
  1628. return m_x->contains_percentage() || m_y->contains_percentage();
  1629. }
  1630. CalculatedStyleValue::CalculationResult ModCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  1631. {
  1632. auto resolved_type = m_x->resolved_type().value();
  1633. auto node_a = m_x->resolve(context, percentage_basis);
  1634. auto node_b = m_y->resolve(context, percentage_basis);
  1635. auto node_a_value = resolve_value(node_a.value(), context);
  1636. auto node_b_value = resolve_value(node_b.value(), context);
  1637. auto quotient = floor(node_a_value / node_b_value);
  1638. auto value = node_a_value - (node_b_value * quotient);
  1639. return to_resolved_type(resolved_type, value);
  1640. }
  1641. ErrorOr<void> ModCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  1642. {
  1643. TRY(m_x->for_each_child_node(callback));
  1644. TRY(m_y->for_each_child_node(callback));
  1645. TRY(callback(m_x));
  1646. TRY(callback(m_y));
  1647. return {};
  1648. }
  1649. ErrorOr<void> ModCalculationNode::dump(StringBuilder& builder, int indent) const
  1650. {
  1651. TRY(builder.try_appendff("{: >{}}MOD: {}\n", "", indent, TRY(to_string())));
  1652. return {};
  1653. }
  1654. ErrorOr<NonnullOwnPtr<RemCalculationNode>> RemCalculationNode::create(NonnullOwnPtr<CalculationNode> x, NonnullOwnPtr<CalculationNode> y)
  1655. {
  1656. return adopt_nonnull_own_or_enomem(new (nothrow) RemCalculationNode(move(x), move(y)));
  1657. }
  1658. RemCalculationNode::RemCalculationNode(NonnullOwnPtr<CalculationNode> x, NonnullOwnPtr<CalculationNode> y)
  1659. : CalculationNode(Type::Rem)
  1660. , m_x(move(x))
  1661. , m_y(move(y))
  1662. {
  1663. }
  1664. RemCalculationNode::~RemCalculationNode() = default;
  1665. ErrorOr<String> RemCalculationNode::to_string() const
  1666. {
  1667. StringBuilder builder;
  1668. builder.append("rem("sv);
  1669. builder.append(TRY(m_x->to_string()));
  1670. builder.append(", "sv);
  1671. builder.append(TRY(m_y->to_string()));
  1672. builder.append(")"sv);
  1673. return builder.to_string();
  1674. }
  1675. Optional<CalculatedStyleValue::ResolvedType> RemCalculationNode::resolved_type() const
  1676. {
  1677. // Note: We check during parsing that all values have the same type
  1678. return m_x->resolved_type();
  1679. }
  1680. // https://www.w3.org/TR/css-values-4/#determine-the-type-of-a-calculation
  1681. Optional<CSSNumericType> RemCalculationNode::determine_type(PropertyID property_id) const
  1682. {
  1683. // The result of adding the types of its comma-separated calculations.
  1684. auto x_type = m_x->determine_type(property_id);
  1685. auto y_type = m_y->determine_type(property_id);
  1686. if (!x_type.has_value() || !y_type.has_value())
  1687. return {};
  1688. return x_type->added_to(*y_type);
  1689. }
  1690. bool RemCalculationNode::contains_percentage() const
  1691. {
  1692. return m_x->contains_percentage() || m_y->contains_percentage();
  1693. }
  1694. CalculatedStyleValue::CalculationResult RemCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  1695. {
  1696. auto resolved_type = m_x->resolved_type().value();
  1697. auto node_a = m_x->resolve(context, percentage_basis);
  1698. auto node_b = m_y->resolve(context, percentage_basis);
  1699. auto node_a_value = resolve_value(node_a.value(), context);
  1700. auto node_b_value = resolve_value(node_b.value(), context);
  1701. auto value = fmod(node_a_value, node_b_value);
  1702. return to_resolved_type(resolved_type, value);
  1703. }
  1704. ErrorOr<void> RemCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  1705. {
  1706. TRY(m_x->for_each_child_node(callback));
  1707. TRY(m_y->for_each_child_node(callback));
  1708. TRY(callback(m_x));
  1709. TRY(callback(m_y));
  1710. return {};
  1711. }
  1712. ErrorOr<void> RemCalculationNode::dump(StringBuilder& builder, int indent) const
  1713. {
  1714. TRY(builder.try_appendff("{: >{}}REM: {}\n", "", indent, TRY(to_string())));
  1715. return {};
  1716. }
  1717. void CalculatedStyleValue::CalculationResult::add(CalculationResult const& other, Optional<Length::ResolutionContext const&> context, PercentageBasis const& percentage_basis)
  1718. {
  1719. add_or_subtract_internal(SumOperation::Add, other, context, percentage_basis);
  1720. }
  1721. void CalculatedStyleValue::CalculationResult::subtract(CalculationResult const& other, Optional<Length::ResolutionContext const&> context, PercentageBasis const& percentage_basis)
  1722. {
  1723. add_or_subtract_internal(SumOperation::Subtract, other, context, percentage_basis);
  1724. }
  1725. void CalculatedStyleValue::CalculationResult::add_or_subtract_internal(SumOperation op, CalculationResult const& other, Optional<Length::ResolutionContext const&> context, PercentageBasis const& percentage_basis)
  1726. {
  1727. // 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>".
  1728. // Though, having the same type may mean that one side is a <dimension> and the other a <percentage>.
  1729. // Note: This is almost identical to ::add()
  1730. m_value.visit(
  1731. [&](Number const& number) {
  1732. auto other_number = other.m_value.get<Number>();
  1733. if (op == SumOperation::Add) {
  1734. m_value = number + other_number;
  1735. } else {
  1736. m_value = number - other_number;
  1737. }
  1738. },
  1739. [&](Angle const& angle) {
  1740. auto this_degrees = angle.to_degrees();
  1741. if (other.m_value.has<Angle>()) {
  1742. auto other_degrees = other.m_value.get<Angle>().to_degrees();
  1743. if (op == SumOperation::Add)
  1744. m_value = Angle::make_degrees(this_degrees + other_degrees);
  1745. else
  1746. m_value = Angle::make_degrees(this_degrees - other_degrees);
  1747. } else {
  1748. VERIFY(percentage_basis.has<Angle>());
  1749. auto other_degrees = percentage_basis.get<Angle>().percentage_of(other.m_value.get<Percentage>()).to_degrees();
  1750. if (op == SumOperation::Add)
  1751. m_value = Angle::make_degrees(this_degrees + other_degrees);
  1752. else
  1753. m_value = Angle::make_degrees(this_degrees - other_degrees);
  1754. }
  1755. },
  1756. [&](Frequency const& frequency) {
  1757. auto this_hertz = frequency.to_hertz();
  1758. if (other.m_value.has<Frequency>()) {
  1759. auto other_hertz = other.m_value.get<Frequency>().to_hertz();
  1760. if (op == SumOperation::Add)
  1761. m_value = Frequency::make_hertz(this_hertz + other_hertz);
  1762. else
  1763. m_value = Frequency::make_hertz(this_hertz - other_hertz);
  1764. } else {
  1765. VERIFY(percentage_basis.has<Frequency>());
  1766. auto other_hertz = percentage_basis.get<Frequency>().percentage_of(other.m_value.get<Percentage>()).to_hertz();
  1767. if (op == SumOperation::Add)
  1768. m_value = Frequency::make_hertz(this_hertz + other_hertz);
  1769. else
  1770. m_value = Frequency::make_hertz(this_hertz - other_hertz);
  1771. }
  1772. },
  1773. [&](Length const& length) {
  1774. auto this_px = length.to_px(*context);
  1775. if (other.m_value.has<Length>()) {
  1776. auto other_px = other.m_value.get<Length>().to_px(*context);
  1777. if (op == SumOperation::Add)
  1778. m_value = Length::make_px(this_px + other_px);
  1779. else
  1780. m_value = Length::make_px(this_px - other_px);
  1781. } else {
  1782. VERIFY(percentage_basis.has<Length>());
  1783. auto other_px = percentage_basis.get<Length>().percentage_of(other.m_value.get<Percentage>()).to_px(*context);
  1784. if (op == SumOperation::Add)
  1785. m_value = Length::make_px(this_px + other_px);
  1786. else
  1787. m_value = Length::make_px(this_px - other_px);
  1788. }
  1789. },
  1790. [&](Time const& time) {
  1791. auto this_seconds = time.to_seconds();
  1792. if (other.m_value.has<Time>()) {
  1793. auto other_seconds = other.m_value.get<Time>().to_seconds();
  1794. if (op == SumOperation::Add)
  1795. m_value = Time::make_seconds(this_seconds + other_seconds);
  1796. else
  1797. m_value = Time::make_seconds(this_seconds - other_seconds);
  1798. } else {
  1799. VERIFY(percentage_basis.has<Time>());
  1800. auto other_seconds = percentage_basis.get<Time>().percentage_of(other.m_value.get<Percentage>()).to_seconds();
  1801. if (op == SumOperation::Add)
  1802. m_value = Time::make_seconds(this_seconds + other_seconds);
  1803. else
  1804. m_value = Time::make_seconds(this_seconds - other_seconds);
  1805. }
  1806. },
  1807. [&](Percentage const& percentage) {
  1808. if (other.m_value.has<Percentage>()) {
  1809. if (op == SumOperation::Add)
  1810. m_value = Percentage { percentage.value() + other.m_value.get<Percentage>().value() };
  1811. else
  1812. m_value = Percentage { percentage.value() - other.m_value.get<Percentage>().value() };
  1813. return;
  1814. }
  1815. // 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`.
  1816. CalculationResult new_value = other;
  1817. if (op == SumOperation::Add) {
  1818. new_value.add(*this, context, percentage_basis);
  1819. } else {
  1820. // Turn 'this - other' into '-other + this', as 'A + B == B + A', but 'A - B != B - A'
  1821. new_value.multiply_by({ Number { Number::Type::Integer, -1.0f } }, context);
  1822. new_value.add(*this, context, percentage_basis);
  1823. }
  1824. *this = new_value;
  1825. });
  1826. }
  1827. void CalculatedStyleValue::CalculationResult::multiply_by(CalculationResult const& other, Optional<Length::ResolutionContext const&> context)
  1828. {
  1829. // We know from validation when resolving the type, that at least one side must be a <number> or <integer>.
  1830. // Both of these are represented as a double.
  1831. VERIFY(m_value.has<Number>() || other.m_value.has<Number>());
  1832. bool other_is_number = other.m_value.has<Number>();
  1833. m_value.visit(
  1834. [&](Number const& number) {
  1835. if (other_is_number) {
  1836. m_value = number * other.m_value.get<Number>();
  1837. } else {
  1838. // Avoid duplicating all the logic by swapping `this` and `other`.
  1839. CalculationResult new_value = other;
  1840. new_value.multiply_by(*this, context);
  1841. *this = new_value;
  1842. }
  1843. },
  1844. [&](Angle const& angle) {
  1845. m_value = Angle::make_degrees(angle.to_degrees() * other.m_value.get<Number>().value());
  1846. },
  1847. [&](Frequency const& frequency) {
  1848. m_value = Frequency::make_hertz(frequency.to_hertz() * other.m_value.get<Number>().value());
  1849. },
  1850. [&](Length const& length) {
  1851. m_value = Length::make_px(length.to_px(*context) * static_cast<double>(other.m_value.get<Number>().value()));
  1852. },
  1853. [&](Time const& time) {
  1854. m_value = Time::make_seconds(time.to_seconds() * other.m_value.get<Number>().value());
  1855. },
  1856. [&](Percentage const& percentage) {
  1857. m_value = Percentage { percentage.value() * other.m_value.get<Number>().value() };
  1858. });
  1859. }
  1860. void CalculatedStyleValue::CalculationResult::divide_by(CalculationResult const& other, Optional<Length::ResolutionContext const&> context)
  1861. {
  1862. // We know from validation when resolving the type, that `other` must be a <number> or <integer>.
  1863. // Both of these are represented as a Number.
  1864. auto denominator = other.m_value.get<Number>().value();
  1865. // FIXME: Dividing by 0 is invalid, and should be caught during parsing.
  1866. VERIFY(denominator != 0.0);
  1867. m_value.visit(
  1868. [&](Number const& number) {
  1869. m_value = Number {
  1870. Number::Type::Number,
  1871. number.value() / denominator
  1872. };
  1873. },
  1874. [&](Angle const& angle) {
  1875. m_value = Angle::make_degrees(angle.to_degrees() / denominator);
  1876. },
  1877. [&](Frequency const& frequency) {
  1878. m_value = Frequency::make_hertz(frequency.to_hertz() / denominator);
  1879. },
  1880. [&](Length const& length) {
  1881. m_value = Length::make_px(length.to_px(*context) / static_cast<double>(denominator));
  1882. },
  1883. [&](Time const& time) {
  1884. m_value = Time::make_seconds(time.to_seconds() / denominator);
  1885. },
  1886. [&](Percentage const& percentage) {
  1887. m_value = Percentage { percentage.value() / denominator };
  1888. });
  1889. }
  1890. void CalculatedStyleValue::CalculationResult::negate()
  1891. {
  1892. m_value.visit(
  1893. [&](Number const& number) {
  1894. m_value = Number { number.type(), 0 - number.value() };
  1895. },
  1896. [&](Angle const& angle) {
  1897. m_value = Angle { 0 - angle.raw_value(), angle.type() };
  1898. },
  1899. [&](Frequency const& frequency) {
  1900. m_value = Frequency { 0 - frequency.raw_value(), frequency.type() };
  1901. },
  1902. [&](Length const& length) {
  1903. m_value = Length { 0 - length.raw_value(), length.type() };
  1904. },
  1905. [&](Time const& time) {
  1906. m_value = Time { 0 - time.raw_value(), time.type() };
  1907. },
  1908. [&](Percentage const& percentage) {
  1909. m_value = Percentage { 0 - percentage.value() };
  1910. });
  1911. }
  1912. void CalculatedStyleValue::CalculationResult::invert()
  1913. {
  1914. // FIXME: Correctly handle division by zero.
  1915. m_value.visit(
  1916. [&](Number const& number) {
  1917. m_value = Number { Number::Type::Number, 1 / number.value() };
  1918. },
  1919. [&](Angle const& angle) {
  1920. m_value = Angle { 1 / angle.raw_value(), angle.type() };
  1921. },
  1922. [&](Frequency const& frequency) {
  1923. m_value = Frequency { 1 / frequency.raw_value(), frequency.type() };
  1924. },
  1925. [&](Length const& length) {
  1926. m_value = Length { 1 / length.raw_value(), length.type() };
  1927. },
  1928. [&](Time const& time) {
  1929. m_value = Time { 1 / time.raw_value(), time.type() };
  1930. },
  1931. [&](Percentage const& percentage) {
  1932. m_value = Percentage { 1 / percentage.value() };
  1933. });
  1934. }
  1935. ErrorOr<String> CalculatedStyleValue::to_string() const
  1936. {
  1937. // FIXME: Implement this according to https://www.w3.org/TR/css-values-4/#calc-serialize once that stabilizes.
  1938. return String::formatted("calc({})", TRY(m_calculation->to_string()));
  1939. }
  1940. bool CalculatedStyleValue::equals(StyleValue const& other) const
  1941. {
  1942. if (type() != other.type())
  1943. return false;
  1944. // This is a case where comparing the strings actually makes sense.
  1945. return to_string().release_value_but_fixme_should_propagate_errors() == other.to_string().release_value_but_fixme_should_propagate_errors();
  1946. }
  1947. Optional<Angle> CalculatedStyleValue::resolve_angle() const
  1948. {
  1949. auto result = m_calculation->resolve({}, {});
  1950. if (result.value().has<Angle>())
  1951. return result.value().get<Angle>();
  1952. return {};
  1953. }
  1954. Optional<Angle> CalculatedStyleValue::resolve_angle_percentage(Angle const& percentage_basis) const
  1955. {
  1956. auto result = m_calculation->resolve({}, percentage_basis);
  1957. return result.value().visit(
  1958. [&](Angle const& angle) -> Optional<Angle> {
  1959. return angle;
  1960. },
  1961. [&](Percentage const& percentage) -> Optional<Angle> {
  1962. return percentage_basis.percentage_of(percentage);
  1963. },
  1964. [&](auto const&) -> Optional<Angle> {
  1965. return {};
  1966. });
  1967. }
  1968. Optional<Frequency> CalculatedStyleValue::resolve_frequency() const
  1969. {
  1970. auto result = m_calculation->resolve({}, {});
  1971. if (result.value().has<Frequency>())
  1972. return result.value().get<Frequency>();
  1973. return {};
  1974. }
  1975. Optional<Frequency> CalculatedStyleValue::resolve_frequency_percentage(Frequency const& percentage_basis) const
  1976. {
  1977. auto result = m_calculation->resolve({}, percentage_basis);
  1978. return result.value().visit(
  1979. [&](Frequency const& frequency) -> Optional<Frequency> {
  1980. return frequency;
  1981. },
  1982. [&](Percentage const& percentage) -> Optional<Frequency> {
  1983. return percentage_basis.percentage_of(percentage);
  1984. },
  1985. [&](auto const&) -> Optional<Frequency> {
  1986. return {};
  1987. });
  1988. }
  1989. Optional<Length> CalculatedStyleValue::resolve_length(Length::ResolutionContext const& context) const
  1990. {
  1991. auto result = m_calculation->resolve(context, {});
  1992. if (result.value().has<Length>())
  1993. return result.value().get<Length>();
  1994. return {};
  1995. }
  1996. Optional<Length> CalculatedStyleValue::resolve_length(Layout::Node const& layout_node) const
  1997. {
  1998. return resolve_length(Length::ResolutionContext::for_layout_node(layout_node));
  1999. }
  2000. Optional<Length> CalculatedStyleValue::resolve_length_percentage(Layout::Node const& layout_node, Length const& percentage_basis) const
  2001. {
  2002. auto result = m_calculation->resolve(Length::ResolutionContext::for_layout_node(layout_node), percentage_basis);
  2003. return result.value().visit(
  2004. [&](Length const& length) -> Optional<Length> {
  2005. return length;
  2006. },
  2007. [&](Percentage const& percentage) -> Optional<Length> {
  2008. return percentage_basis.percentage_of(percentage);
  2009. },
  2010. [&](auto const&) -> Optional<Length> {
  2011. return {};
  2012. });
  2013. }
  2014. Optional<Percentage> CalculatedStyleValue::resolve_percentage() const
  2015. {
  2016. auto result = m_calculation->resolve({}, {});
  2017. if (result.value().has<Percentage>())
  2018. return result.value().get<Percentage>();
  2019. return {};
  2020. }
  2021. Optional<Time> CalculatedStyleValue::resolve_time() const
  2022. {
  2023. auto result = m_calculation->resolve({}, {});
  2024. if (result.value().has<Time>())
  2025. return result.value().get<Time>();
  2026. return {};
  2027. }
  2028. Optional<Time> CalculatedStyleValue::resolve_time_percentage(Time const& percentage_basis) const
  2029. {
  2030. auto result = m_calculation->resolve({}, percentage_basis);
  2031. return result.value().visit(
  2032. [&](Time const& time) -> Optional<Time> {
  2033. return time;
  2034. },
  2035. [&](auto const&) -> Optional<Time> {
  2036. return {};
  2037. });
  2038. }
  2039. Optional<double> CalculatedStyleValue::resolve_number() const
  2040. {
  2041. auto result = m_calculation->resolve({}, {});
  2042. if (result.value().has<Number>())
  2043. return result.value().get<Number>().value();
  2044. return {};
  2045. }
  2046. Optional<i64> CalculatedStyleValue::resolve_integer()
  2047. {
  2048. auto result = m_calculation->resolve({}, {});
  2049. if (result.value().has<Number>())
  2050. return result.value().get<Number>().integer_value();
  2051. return {};
  2052. }
  2053. bool CalculatedStyleValue::contains_percentage() const
  2054. {
  2055. return m_calculation->contains_percentage();
  2056. }
  2057. }