CSSMathValue.cpp 93 KB

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