CalculatedStyleValue.cpp 83 KB

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