CSSMathValue.cpp 90 KB

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