CalculatedStyleValue.cpp 93 KB

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