CalculatedStyleValue.cpp 93 KB

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