CalculatedStyleValue.cpp 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860
  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. namespace Web::CSS {
  12. static bool is_number(CalculatedStyleValue::ResolvedType type)
  13. {
  14. return type == CalculatedStyleValue::ResolvedType::Number || type == CalculatedStyleValue::ResolvedType::Integer;
  15. }
  16. static bool is_dimension(CalculatedStyleValue::ResolvedType type)
  17. {
  18. return type != CalculatedStyleValue::ResolvedType::Number
  19. && type != CalculatedStyleValue::ResolvedType::Integer
  20. && type != CalculatedStyleValue::ResolvedType::Percentage;
  21. }
  22. static double resolve_value_radians(CalculatedStyleValue::CalculationResult::Value value)
  23. {
  24. return value.visit(
  25. [](Number const& number) { return number.value(); },
  26. [](Angle const& angle) { return angle.to_radians(); },
  27. [](auto const&) { VERIFY_NOT_REACHED(); return 0.0; });
  28. };
  29. static double resolve_value(CalculatedStyleValue::CalculationResult::Value value, Optional<Length::ResolutionContext const&> context)
  30. {
  31. return value.visit(
  32. [](Number const& number) { return number.value(); },
  33. [](Angle const& angle) { return angle.to_degrees(); },
  34. [](Frequency const& frequency) { return frequency.to_hertz(); },
  35. [&context](Length const& length) { return length.to_px(*context).to_double(); },
  36. [](Percentage const& percentage) { return percentage.value(); },
  37. [](Time const& time) { return time.to_seconds(); });
  38. };
  39. static CalculatedStyleValue::CalculationResult to_resolved_type(CalculatedStyleValue::ResolvedType type, double value)
  40. {
  41. switch (type) {
  42. case CalculatedStyleValue::ResolvedType::Integer:
  43. return { Number(Number::Type::Integer, value) };
  44. case CalculatedStyleValue::ResolvedType::Number:
  45. return { Number(Number::Type::Number, value) };
  46. case CalculatedStyleValue::ResolvedType::Angle:
  47. return { Angle::make_degrees(value) };
  48. case CalculatedStyleValue::ResolvedType::Frequency:
  49. return { Frequency::make_hertz(value) };
  50. case CalculatedStyleValue::ResolvedType::Length:
  51. return { Length::make_px(value) };
  52. case CalculatedStyleValue::ResolvedType::Percentage:
  53. return { Percentage(value) };
  54. case CalculatedStyleValue::ResolvedType::Time:
  55. return { Time::make_seconds(value) };
  56. }
  57. VERIFY_NOT_REACHED();
  58. };
  59. CalculationNode::CalculationNode(Type type)
  60. : m_type(type)
  61. {
  62. }
  63. CalculationNode::~CalculationNode() = default;
  64. ErrorOr<NonnullOwnPtr<NumericCalculationNode>> NumericCalculationNode::create(NumericValue value)
  65. {
  66. return adopt_nonnull_own_or_enomem(new (nothrow) NumericCalculationNode(move(value)));
  67. }
  68. NumericCalculationNode::NumericCalculationNode(NumericValue value)
  69. : CalculationNode(Type::Numeric)
  70. , m_value(move(value))
  71. {
  72. }
  73. NumericCalculationNode::~NumericCalculationNode() = default;
  74. ErrorOr<String> NumericCalculationNode::to_string() const
  75. {
  76. return m_value.visit([](auto& value) { return value.to_string(); });
  77. }
  78. Optional<CalculatedStyleValue::ResolvedType> NumericCalculationNode::resolved_type() const
  79. {
  80. return m_value.visit(
  81. [](Number const&) { return CalculatedStyleValue::ResolvedType::Number; },
  82. [](Angle const&) { return CalculatedStyleValue::ResolvedType::Angle; },
  83. [](Frequency const&) { return CalculatedStyleValue::ResolvedType::Frequency; },
  84. [](Length const&) { return CalculatedStyleValue::ResolvedType::Length; },
  85. [](Percentage const&) { return CalculatedStyleValue::ResolvedType::Percentage; },
  86. [](Time const&) { return CalculatedStyleValue::ResolvedType::Time; });
  87. }
  88. bool NumericCalculationNode::contains_percentage() const
  89. {
  90. return m_value.has<Percentage>();
  91. }
  92. CalculatedStyleValue::CalculationResult NumericCalculationNode::resolve(Optional<Length::ResolutionContext const&>, CalculatedStyleValue::PercentageBasis const&) const
  93. {
  94. return m_value;
  95. }
  96. ErrorOr<void> NumericCalculationNode::dump(StringBuilder& builder, int indent) const
  97. {
  98. return builder.try_appendff("{: >{}}NUMERIC({})\n", "", indent, TRY(m_value.visit([](auto& it) { return it.to_string(); })));
  99. }
  100. ErrorOr<NonnullOwnPtr<SumCalculationNode>> SumCalculationNode::create(Vector<NonnullOwnPtr<CalculationNode>> values)
  101. {
  102. return adopt_nonnull_own_or_enomem(new (nothrow) SumCalculationNode(move(values)));
  103. }
  104. SumCalculationNode::SumCalculationNode(Vector<NonnullOwnPtr<CalculationNode>> values)
  105. : CalculationNode(Type::Sum)
  106. , m_values(move(values))
  107. {
  108. VERIFY(!m_values.is_empty());
  109. }
  110. SumCalculationNode::~SumCalculationNode() = default;
  111. ErrorOr<String> SumCalculationNode::to_string() const
  112. {
  113. bool first = true;
  114. StringBuilder builder;
  115. for (auto& value : m_values) {
  116. if (!first)
  117. TRY(builder.try_append(" + "sv));
  118. TRY(builder.try_append(TRY(value->to_string())));
  119. first = false;
  120. }
  121. return builder.to_string();
  122. }
  123. Optional<CalculatedStyleValue::ResolvedType> SumCalculationNode::resolved_type() const
  124. {
  125. // FIXME: Implement https://www.w3.org/TR/css-values-4/#determine-the-type-of-a-calculation
  126. // For now, this is just ad-hoc, based on the old implementation.
  127. Optional<CalculatedStyleValue::ResolvedType> type;
  128. for (auto const& value : m_values) {
  129. auto maybe_value_type = value->resolved_type();
  130. if (!maybe_value_type.has_value())
  131. return {};
  132. auto value_type = maybe_value_type.value();
  133. if (!type.has_value()) {
  134. type = value_type;
  135. continue;
  136. }
  137. // At + or -, check that both sides have the same type, or that one side is a <number> and the other is an <integer>.
  138. // If both sides are the same type, resolve to that type.
  139. if (value_type == type)
  140. continue;
  141. // If one side is a <number> and the other is an <integer>, resolve to <number>.
  142. if (is_number(*type) && is_number(value_type)) {
  143. type = CalculatedStyleValue::ResolvedType::Number;
  144. continue;
  145. }
  146. // FIXME: calc() handles <percentage> by allowing them to pretend to be whatever <dimension> type is allowed at this location.
  147. // Since we can't easily check what that type is, we just allow <percentage> to combine with any other <dimension> type.
  148. if (type == CalculatedStyleValue::ResolvedType::Percentage && is_dimension(value_type)) {
  149. type = value_type;
  150. continue;
  151. }
  152. if (is_dimension(*type) && value_type == CalculatedStyleValue::ResolvedType::Percentage)
  153. continue;
  154. return {};
  155. }
  156. return type;
  157. }
  158. bool SumCalculationNode::contains_percentage() const
  159. {
  160. for (auto const& value : m_values) {
  161. if (value->contains_percentage())
  162. return true;
  163. }
  164. return false;
  165. }
  166. CalculatedStyleValue::CalculationResult SumCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  167. {
  168. Optional<CalculatedStyleValue::CalculationResult> total;
  169. for (auto& additional_product : m_values) {
  170. auto additional_value = additional_product->resolve(context, percentage_basis);
  171. if (!total.has_value()) {
  172. total = additional_value;
  173. continue;
  174. }
  175. total->add(additional_value, context, percentage_basis);
  176. }
  177. return total.value();
  178. }
  179. ErrorOr<void> SumCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  180. {
  181. for (auto& item : m_values) {
  182. TRY(item->for_each_child_node(callback));
  183. TRY(callback(item));
  184. }
  185. return {};
  186. }
  187. ErrorOr<void> SumCalculationNode::dump(StringBuilder& builder, int indent) const
  188. {
  189. TRY(builder.try_appendff("{: >{}}SUM:\n", "", indent));
  190. for (auto const& item : m_values)
  191. TRY(item->dump(builder, indent + 2));
  192. return {};
  193. }
  194. ErrorOr<NonnullOwnPtr<ProductCalculationNode>> ProductCalculationNode::create(Vector<NonnullOwnPtr<CalculationNode>> values)
  195. {
  196. return adopt_nonnull_own_or_enomem(new (nothrow) ProductCalculationNode(move(values)));
  197. }
  198. ProductCalculationNode::ProductCalculationNode(Vector<NonnullOwnPtr<CalculationNode>> values)
  199. : CalculationNode(Type::Product)
  200. , m_values(move(values))
  201. {
  202. VERIFY(!m_values.is_empty());
  203. }
  204. ProductCalculationNode::~ProductCalculationNode() = default;
  205. ErrorOr<String> ProductCalculationNode::to_string() const
  206. {
  207. bool first = true;
  208. StringBuilder builder;
  209. for (auto& value : m_values) {
  210. if (!first)
  211. TRY(builder.try_append(" * "sv));
  212. TRY(builder.try_append(TRY(value->to_string())));
  213. first = false;
  214. }
  215. return builder.to_string();
  216. }
  217. Optional<CalculatedStyleValue::ResolvedType> ProductCalculationNode::resolved_type() const
  218. {
  219. // FIXME: Implement https://www.w3.org/TR/css-values-4/#determine-the-type-of-a-calculation
  220. // For now, this is just ad-hoc, based on the old implementation.
  221. Optional<CalculatedStyleValue::ResolvedType> type;
  222. for (auto const& value : m_values) {
  223. auto maybe_value_type = value->resolved_type();
  224. if (!maybe_value_type.has_value())
  225. return {};
  226. auto value_type = maybe_value_type.value();
  227. if (!type.has_value()) {
  228. type = value_type;
  229. continue;
  230. }
  231. // At *, check that at least one side is <number>.
  232. if (!(is_number(*type) || is_number(value_type)))
  233. return {};
  234. // If both sides are <integer>, resolve to <integer>.
  235. if (type == CalculatedStyleValue::ResolvedType::Integer && value_type == CalculatedStyleValue::ResolvedType::Integer) {
  236. type = CalculatedStyleValue::ResolvedType::Integer;
  237. } else {
  238. // Otherwise, resolve to the type of the other side.
  239. if (is_number(*type))
  240. type = value_type;
  241. }
  242. }
  243. return type;
  244. }
  245. bool ProductCalculationNode::contains_percentage() const
  246. {
  247. for (auto const& value : m_values) {
  248. if (value->contains_percentage())
  249. return true;
  250. }
  251. return false;
  252. }
  253. CalculatedStyleValue::CalculationResult ProductCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  254. {
  255. Optional<CalculatedStyleValue::CalculationResult> total;
  256. for (auto& additional_product : m_values) {
  257. auto additional_value = additional_product->resolve(context, percentage_basis);
  258. if (!total.has_value()) {
  259. total = additional_value;
  260. continue;
  261. }
  262. total->multiply_by(additional_value, context);
  263. }
  264. return total.value();
  265. }
  266. ErrorOr<void> ProductCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  267. {
  268. for (auto& item : m_values) {
  269. TRY(item->for_each_child_node(callback));
  270. TRY(callback(item));
  271. }
  272. return {};
  273. }
  274. ErrorOr<void> ProductCalculationNode::dump(StringBuilder& builder, int indent) const
  275. {
  276. TRY(builder.try_appendff("{: >{}}PRODUCT:\n", "", indent));
  277. for (auto const& item : m_values)
  278. TRY(item->dump(builder, indent + 2));
  279. return {};
  280. }
  281. ErrorOr<NonnullOwnPtr<NegateCalculationNode>> NegateCalculationNode::create(NonnullOwnPtr<Web::CSS::CalculationNode> value)
  282. {
  283. return adopt_nonnull_own_or_enomem(new (nothrow) NegateCalculationNode(move(value)));
  284. }
  285. NegateCalculationNode::NegateCalculationNode(NonnullOwnPtr<CalculationNode> value)
  286. : CalculationNode(Type::Negate)
  287. , m_value(move(value))
  288. {
  289. }
  290. NegateCalculationNode::~NegateCalculationNode() = default;
  291. ErrorOr<String> NegateCalculationNode::to_string() const
  292. {
  293. return String::formatted("(0 - {})", TRY(m_value->to_string()));
  294. }
  295. Optional<CalculatedStyleValue::ResolvedType> NegateCalculationNode::resolved_type() const
  296. {
  297. return m_value->resolved_type();
  298. }
  299. bool NegateCalculationNode::contains_percentage() const
  300. {
  301. return m_value->contains_percentage();
  302. }
  303. CalculatedStyleValue::CalculationResult NegateCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  304. {
  305. auto child_value = m_value->resolve(context, percentage_basis);
  306. child_value.negate();
  307. return child_value;
  308. }
  309. ErrorOr<void> NegateCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  310. {
  311. TRY(m_value->for_each_child_node(callback));
  312. TRY(callback(m_value));
  313. return {};
  314. }
  315. ErrorOr<void> NegateCalculationNode::dump(StringBuilder& builder, int indent) const
  316. {
  317. TRY(builder.try_appendff("{: >{}}NEGATE:\n", "", indent));
  318. TRY(m_value->dump(builder, indent + 2));
  319. return {};
  320. }
  321. ErrorOr<NonnullOwnPtr<InvertCalculationNode>> InvertCalculationNode::create(NonnullOwnPtr<Web::CSS::CalculationNode> value)
  322. {
  323. return adopt_nonnull_own_or_enomem(new (nothrow) InvertCalculationNode(move(value)));
  324. }
  325. InvertCalculationNode::InvertCalculationNode(NonnullOwnPtr<CalculationNode> value)
  326. : CalculationNode(Type::Invert)
  327. , m_value(move(value))
  328. {
  329. }
  330. InvertCalculationNode::~InvertCalculationNode() = default;
  331. ErrorOr<String> InvertCalculationNode::to_string() const
  332. {
  333. return String::formatted("(1 / {})", TRY(m_value->to_string()));
  334. }
  335. Optional<CalculatedStyleValue::ResolvedType> InvertCalculationNode::resolved_type() const
  336. {
  337. auto type = m_value->resolved_type();
  338. if (type == CalculatedStyleValue::ResolvedType::Integer)
  339. return CalculatedStyleValue::ResolvedType::Number;
  340. return type;
  341. }
  342. bool InvertCalculationNode::contains_percentage() const
  343. {
  344. return m_value->contains_percentage();
  345. }
  346. CalculatedStyleValue::CalculationResult InvertCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  347. {
  348. auto child_value = m_value->resolve(context, percentage_basis);
  349. child_value.invert();
  350. return child_value;
  351. }
  352. ErrorOr<void> InvertCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  353. {
  354. TRY(m_value->for_each_child_node(callback));
  355. TRY(callback(m_value));
  356. return {};
  357. }
  358. ErrorOr<void> InvertCalculationNode::dump(StringBuilder& builder, int indent) const
  359. {
  360. TRY(builder.try_appendff("{: >{}}INVERT:\n", "", indent));
  361. TRY(m_value->dump(builder, indent + 2));
  362. return {};
  363. }
  364. ErrorOr<NonnullOwnPtr<MinCalculationNode>> MinCalculationNode::create(Vector<NonnullOwnPtr<Web::CSS::CalculationNode>> values)
  365. {
  366. return adopt_nonnull_own_or_enomem(new (nothrow) MinCalculationNode(move(values)));
  367. }
  368. MinCalculationNode::MinCalculationNode(Vector<NonnullOwnPtr<CalculationNode>> values)
  369. : CalculationNode(Type::Min)
  370. , m_values(move(values))
  371. {
  372. }
  373. MinCalculationNode::~MinCalculationNode() = default;
  374. ErrorOr<String> MinCalculationNode::to_string() const
  375. {
  376. StringBuilder builder;
  377. TRY(builder.try_append("min("sv));
  378. for (size_t i = 0; i < m_values.size(); ++i) {
  379. if (i != 0)
  380. TRY(builder.try_append(", "sv));
  381. TRY(builder.try_append(TRY(m_values[i]->to_string())));
  382. }
  383. TRY(builder.try_append(")"sv));
  384. return builder.to_string();
  385. }
  386. Optional<CalculatedStyleValue::ResolvedType> MinCalculationNode::resolved_type() const
  387. {
  388. // NOTE: We check during parsing that all values have the same type.
  389. return m_values[0]->resolved_type();
  390. }
  391. bool MinCalculationNode::contains_percentage() const
  392. {
  393. for (auto const& value : m_values) {
  394. if (value->contains_percentage())
  395. return true;
  396. }
  397. return false;
  398. }
  399. CalculatedStyleValue::CalculationResult MinCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  400. {
  401. CalculatedStyleValue::CalculationResult smallest_node = m_values.first()->resolve(context, percentage_basis);
  402. auto smallest_value = resolve_value(smallest_node.value(), context);
  403. for (size_t i = 1; i < m_values.size(); i++) {
  404. auto child_resolved = m_values[i]->resolve(context, percentage_basis);
  405. auto child_value = resolve_value(child_resolved.value(), context);
  406. if (child_value < smallest_value) {
  407. smallest_value = child_value;
  408. smallest_node = child_resolved;
  409. }
  410. }
  411. return smallest_node;
  412. }
  413. ErrorOr<void> MinCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  414. {
  415. for (auto& value : m_values) {
  416. TRY(value->for_each_child_node(callback));
  417. TRY(callback(value));
  418. }
  419. return {};
  420. }
  421. ErrorOr<void> MinCalculationNode::dump(StringBuilder& builder, int indent) const
  422. {
  423. TRY(builder.try_appendff("{: >{}}MIN:\n", "", indent));
  424. for (auto const& value : m_values)
  425. TRY(value->dump(builder, indent + 2));
  426. return {};
  427. }
  428. ErrorOr<NonnullOwnPtr<MaxCalculationNode>> MaxCalculationNode::create(Vector<NonnullOwnPtr<Web::CSS::CalculationNode>> values)
  429. {
  430. return adopt_nonnull_own_or_enomem(new (nothrow) MaxCalculationNode(move(values)));
  431. }
  432. MaxCalculationNode::MaxCalculationNode(Vector<NonnullOwnPtr<CalculationNode>> values)
  433. : CalculationNode(Type::Max)
  434. , m_values(move(values))
  435. {
  436. }
  437. MaxCalculationNode::~MaxCalculationNode() = default;
  438. ErrorOr<String> MaxCalculationNode::to_string() const
  439. {
  440. StringBuilder builder;
  441. TRY(builder.try_append("max("sv));
  442. for (size_t i = 0; i < m_values.size(); ++i) {
  443. if (i != 0)
  444. TRY(builder.try_append(", "sv));
  445. TRY(builder.try_append(TRY(m_values[i]->to_string())));
  446. }
  447. TRY(builder.try_append(")"sv));
  448. return builder.to_string();
  449. }
  450. Optional<CalculatedStyleValue::ResolvedType> MaxCalculationNode::resolved_type() const
  451. {
  452. // NOTE: We check during parsing that all values have the same type.
  453. return m_values[0]->resolved_type();
  454. }
  455. bool MaxCalculationNode::contains_percentage() const
  456. {
  457. for (auto const& value : m_values) {
  458. if (value->contains_percentage())
  459. return true;
  460. }
  461. return false;
  462. }
  463. CalculatedStyleValue::CalculationResult MaxCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  464. {
  465. CalculatedStyleValue::CalculationResult largest_node = m_values.first()->resolve(context, percentage_basis);
  466. auto largest_value = resolve_value(largest_node.value(), context);
  467. for (size_t i = 1; i < m_values.size(); i++) {
  468. auto child_resolved = m_values[i]->resolve(context, percentage_basis);
  469. auto child_value = resolve_value(child_resolved.value(), context);
  470. if (child_value > largest_value) {
  471. largest_value = child_value;
  472. largest_node = child_resolved;
  473. }
  474. }
  475. return largest_node;
  476. }
  477. ErrorOr<void> MaxCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  478. {
  479. for (auto& value : m_values) {
  480. TRY(value->for_each_child_node(callback));
  481. TRY(callback(value));
  482. }
  483. return {};
  484. }
  485. ErrorOr<void> MaxCalculationNode::dump(StringBuilder& builder, int indent) const
  486. {
  487. TRY(builder.try_appendff("{: >{}}MAX:\n", "", indent));
  488. for (auto const& value : m_values)
  489. TRY(value->dump(builder, indent + 2));
  490. return {};
  491. }
  492. ErrorOr<NonnullOwnPtr<ClampCalculationNode>> ClampCalculationNode::create(NonnullOwnPtr<CalculationNode> min, NonnullOwnPtr<CalculationNode> center, NonnullOwnPtr<CalculationNode> max)
  493. {
  494. return adopt_nonnull_own_or_enomem(new (nothrow) ClampCalculationNode(move(min), move(center), move(max)));
  495. }
  496. ClampCalculationNode::ClampCalculationNode(NonnullOwnPtr<CalculationNode> min, NonnullOwnPtr<CalculationNode> center, NonnullOwnPtr<CalculationNode> max)
  497. : CalculationNode(Type::Clamp)
  498. , m_min_value(move(min))
  499. , m_center_value(move(center))
  500. , m_max_value(move(max))
  501. {
  502. }
  503. ClampCalculationNode::~ClampCalculationNode() = default;
  504. ErrorOr<String> ClampCalculationNode::to_string() const
  505. {
  506. StringBuilder builder;
  507. TRY(builder.try_append("clamp("sv));
  508. TRY(builder.try_append(TRY(m_min_value->to_string())));
  509. TRY(builder.try_append(", "sv));
  510. TRY(builder.try_append(TRY(m_center_value->to_string())));
  511. TRY(builder.try_append(", "sv));
  512. TRY(builder.try_append(TRY(m_max_value->to_string())));
  513. TRY(builder.try_append(")"sv));
  514. return builder.to_string();
  515. }
  516. Optional<CalculatedStyleValue::ResolvedType> ClampCalculationNode::resolved_type() const
  517. {
  518. // NOTE: We check during parsing that all values have the same type.
  519. return m_min_value->resolved_type();
  520. }
  521. bool ClampCalculationNode::contains_percentage() const
  522. {
  523. return m_min_value->contains_percentage() || m_center_value->contains_percentage() || m_max_value->contains_percentage();
  524. }
  525. CalculatedStyleValue::CalculationResult ClampCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  526. {
  527. auto min_node = m_min_value->resolve(context, percentage_basis);
  528. auto center_node = m_center_value->resolve(context, percentage_basis);
  529. auto max_node = m_max_value->resolve(context, percentage_basis);
  530. auto min_value = resolve_value(min_node.value(), context);
  531. auto center_value = resolve_value(center_node.value(), context);
  532. auto max_value = resolve_value(max_node.value(), context);
  533. // NOTE: The value should be returned as "max(MIN, min(VAL, MAX))"
  534. auto chosen_value = max(min_value, min(center_value, max_value));
  535. if (chosen_value == min_value)
  536. return min_node;
  537. if (chosen_value == center_value)
  538. return center_node;
  539. if (chosen_value == max_value)
  540. return max_node;
  541. VERIFY_NOT_REACHED();
  542. }
  543. ErrorOr<void> ClampCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  544. {
  545. TRY(m_min_value->for_each_child_node(callback));
  546. TRY(m_center_value->for_each_child_node(callback));
  547. TRY(m_max_value->for_each_child_node(callback));
  548. TRY(callback(m_min_value));
  549. TRY(callback(m_center_value));
  550. TRY(callback(m_max_value));
  551. return {};
  552. }
  553. ErrorOr<void> ClampCalculationNode::dump(StringBuilder& builder, int indent) const
  554. {
  555. TRY(builder.try_appendff("{: >{}}CLAMP:\n", "", indent));
  556. TRY(m_min_value->dump(builder, indent + 2));
  557. TRY(m_center_value->dump(builder, indent + 2));
  558. TRY(m_max_value->dump(builder, indent + 2));
  559. return {};
  560. }
  561. ErrorOr<NonnullOwnPtr<AbsCalculationNode>> AbsCalculationNode::create(NonnullOwnPtr<CalculationNode> value)
  562. {
  563. return adopt_nonnull_own_or_enomem(new (nothrow) AbsCalculationNode(move(value)));
  564. }
  565. AbsCalculationNode::AbsCalculationNode(NonnullOwnPtr<CalculationNode> value)
  566. : CalculationNode(Type::Abs)
  567. , m_value(move(value))
  568. {
  569. }
  570. AbsCalculationNode::~AbsCalculationNode() = default;
  571. ErrorOr<String> AbsCalculationNode::to_string() const
  572. {
  573. StringBuilder builder;
  574. builder.append("abs("sv);
  575. builder.append(TRY(m_value->to_string()));
  576. builder.append(")"sv);
  577. return builder.to_string();
  578. }
  579. Optional<CalculatedStyleValue::ResolvedType> AbsCalculationNode::resolved_type() const
  580. {
  581. return m_value->resolved_type();
  582. }
  583. bool AbsCalculationNode::contains_percentage() const
  584. {
  585. return m_value->contains_percentage();
  586. }
  587. CalculatedStyleValue::CalculationResult AbsCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  588. {
  589. auto resolved_type = m_value->resolved_type().value();
  590. auto node_a = m_value->resolve(context, percentage_basis);
  591. auto node_a_value = resolve_value(node_a.value(), context);
  592. if (node_a_value < 0)
  593. return to_resolved_type(resolved_type, -node_a_value);
  594. return node_a;
  595. }
  596. ErrorOr<void> AbsCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  597. {
  598. TRY(m_value->for_each_child_node(callback));
  599. TRY(callback(m_value));
  600. return {};
  601. }
  602. ErrorOr<void> AbsCalculationNode::dump(StringBuilder& builder, int indent) const
  603. {
  604. TRY(builder.try_appendff("{: >{}}ABS: {}\n", "", indent, TRY(to_string())));
  605. return {};
  606. }
  607. ErrorOr<NonnullOwnPtr<SignCalculationNode>> SignCalculationNode::create(NonnullOwnPtr<CalculationNode> value)
  608. {
  609. return adopt_nonnull_own_or_enomem(new (nothrow) SignCalculationNode(move(value)));
  610. }
  611. SignCalculationNode::SignCalculationNode(NonnullOwnPtr<CalculationNode> value)
  612. : CalculationNode(Type::Sign)
  613. , m_value(move(value))
  614. {
  615. }
  616. SignCalculationNode::~SignCalculationNode() = default;
  617. ErrorOr<String> SignCalculationNode::to_string() const
  618. {
  619. StringBuilder builder;
  620. builder.append("sign("sv);
  621. builder.append(TRY(m_value->to_string()));
  622. builder.append(")"sv);
  623. return builder.to_string();
  624. }
  625. Optional<CalculatedStyleValue::ResolvedType> SignCalculationNode::resolved_type() const
  626. {
  627. return CalculatedStyleValue::ResolvedType::Integer;
  628. }
  629. bool SignCalculationNode::contains_percentage() const
  630. {
  631. return m_value->contains_percentage();
  632. }
  633. CalculatedStyleValue::CalculationResult SignCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  634. {
  635. auto node_a = m_value->resolve(context, percentage_basis);
  636. auto node_a_value = resolve_value(node_a.value(), context);
  637. if (node_a_value < 0)
  638. return { Number(Number::Type::Integer, -1) };
  639. if (node_a_value > 0)
  640. return { Number(Number::Type::Integer, 1) };
  641. return { Number(Number::Type::Integer, 0) };
  642. }
  643. ErrorOr<void> SignCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  644. {
  645. TRY(m_value->for_each_child_node(callback));
  646. TRY(callback(m_value));
  647. return {};
  648. }
  649. ErrorOr<void> SignCalculationNode::dump(StringBuilder& builder, int indent) const
  650. {
  651. TRY(builder.try_appendff("{: >{}}SIGN: {}\n", "", indent, TRY(to_string())));
  652. return {};
  653. }
  654. ErrorOr<NonnullOwnPtr<ConstantCalculationNode>> ConstantCalculationNode::create(ConstantType constant)
  655. {
  656. return adopt_nonnull_own_or_enomem(new (nothrow) ConstantCalculationNode(constant));
  657. }
  658. ConstantCalculationNode::ConstantCalculationNode(ConstantType constant)
  659. : CalculationNode(Type::Constant)
  660. , m_constant(constant)
  661. {
  662. }
  663. ConstantCalculationNode::~ConstantCalculationNode() = default;
  664. ErrorOr<String> ConstantCalculationNode::to_string() const
  665. {
  666. switch (m_constant) {
  667. case CalculationNode::ConstantType::E:
  668. return "e"_short_string;
  669. case CalculationNode::ConstantType::PI:
  670. return "pi"_short_string;
  671. case CalculationNode::ConstantType::Infinity:
  672. return "infinity"_string;
  673. case CalculationNode::ConstantType::MinusInfinity:
  674. return "-infinity"_string;
  675. case CalculationNode::ConstantType::NaN:
  676. return "NaN"_string;
  677. }
  678. VERIFY_NOT_REACHED();
  679. }
  680. Optional<CalculatedStyleValue::ResolvedType> ConstantCalculationNode::resolved_type() const
  681. {
  682. return CalculatedStyleValue::ResolvedType::Number;
  683. }
  684. CalculatedStyleValue::CalculationResult ConstantCalculationNode::resolve([[maybe_unused]] Optional<Length::ResolutionContext const&> context, [[maybe_unused]] CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  685. {
  686. switch (m_constant) {
  687. case CalculationNode::ConstantType::E:
  688. return { Number(Number::Type::Number, M_E) };
  689. case CalculationNode::ConstantType::PI:
  690. return { Number(Number::Type::Number, M_PI) };
  691. // FIXME: We need to keep track of Infinity and NaN across all nodes, since they require special handling.
  692. case CalculationNode::ConstantType::Infinity:
  693. return { Number(Number::Type::Number, NumericLimits<float>::max()) };
  694. case CalculationNode::ConstantType::MinusInfinity:
  695. return { Number(Number::Type::Number, NumericLimits<float>::lowest()) };
  696. case CalculationNode::ConstantType::NaN:
  697. return { Number(Number::Type::Number, NAN) };
  698. }
  699. VERIFY_NOT_REACHED();
  700. }
  701. ErrorOr<void> ConstantCalculationNode::for_each_child_node([[maybe_unused]] Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  702. {
  703. return {};
  704. }
  705. ErrorOr<void> ConstantCalculationNode::dump(StringBuilder& builder, int indent) const
  706. {
  707. TRY(builder.try_appendff("{: >{}}CONSTANT: {}\n", "", indent, TRY(to_string())));
  708. return {};
  709. }
  710. ErrorOr<NonnullOwnPtr<SinCalculationNode>> SinCalculationNode::create(NonnullOwnPtr<CalculationNode> value)
  711. {
  712. return adopt_nonnull_own_or_enomem(new (nothrow) SinCalculationNode(move(value)));
  713. }
  714. SinCalculationNode::SinCalculationNode(NonnullOwnPtr<CalculationNode> value)
  715. : CalculationNode(Type::Sin)
  716. , m_value(move(value))
  717. {
  718. }
  719. SinCalculationNode::~SinCalculationNode() = default;
  720. ErrorOr<String> SinCalculationNode::to_string() const
  721. {
  722. StringBuilder builder;
  723. builder.append("sin("sv);
  724. builder.append(TRY(m_value->to_string()));
  725. builder.append(")"sv);
  726. return builder.to_string();
  727. }
  728. Optional<CalculatedStyleValue::ResolvedType> SinCalculationNode::resolved_type() const
  729. {
  730. return CalculatedStyleValue::ResolvedType::Number;
  731. }
  732. bool SinCalculationNode::contains_percentage() const
  733. {
  734. return m_value->contains_percentage();
  735. }
  736. CalculatedStyleValue::CalculationResult SinCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  737. {
  738. auto node_a = m_value->resolve(context, percentage_basis);
  739. auto node_a_value = resolve_value_radians(node_a.value());
  740. auto result = sin(node_a_value);
  741. return { Number(Number::Type::Number, result) };
  742. }
  743. ErrorOr<void> SinCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  744. {
  745. TRY(m_value->for_each_child_node(callback));
  746. TRY(callback(m_value));
  747. return {};
  748. }
  749. ErrorOr<void> SinCalculationNode::dump(StringBuilder& builder, int indent) const
  750. {
  751. TRY(builder.try_appendff("{: >{}}SIN: {}\n", "", indent, TRY(to_string())));
  752. return {};
  753. }
  754. ErrorOr<NonnullOwnPtr<CosCalculationNode>> CosCalculationNode::create(NonnullOwnPtr<CalculationNode> value)
  755. {
  756. return adopt_nonnull_own_or_enomem(new (nothrow) CosCalculationNode(move(value)));
  757. }
  758. CosCalculationNode::CosCalculationNode(NonnullOwnPtr<CalculationNode> value)
  759. : CalculationNode(Type::Cos)
  760. , m_value(move(value))
  761. {
  762. }
  763. CosCalculationNode::~CosCalculationNode() = default;
  764. ErrorOr<String> CosCalculationNode::to_string() const
  765. {
  766. StringBuilder builder;
  767. builder.append("cos("sv);
  768. builder.append(TRY(m_value->to_string()));
  769. builder.append(")"sv);
  770. return builder.to_string();
  771. }
  772. Optional<CalculatedStyleValue::ResolvedType> CosCalculationNode::resolved_type() const
  773. {
  774. return CalculatedStyleValue::ResolvedType::Number;
  775. }
  776. bool CosCalculationNode::contains_percentage() const
  777. {
  778. return m_value->contains_percentage();
  779. }
  780. CalculatedStyleValue::CalculationResult CosCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  781. {
  782. auto node_a = m_value->resolve(context, percentage_basis);
  783. auto node_a_value = resolve_value_radians(node_a.value());
  784. auto result = cos(node_a_value);
  785. return { Number(Number::Type::Number, result) };
  786. }
  787. ErrorOr<void> CosCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  788. {
  789. TRY(m_value->for_each_child_node(callback));
  790. TRY(callback(m_value));
  791. return {};
  792. }
  793. ErrorOr<void> CosCalculationNode::dump(StringBuilder& builder, int indent) const
  794. {
  795. TRY(builder.try_appendff("{: >{}}COS: {}\n", "", indent, TRY(to_string())));
  796. return {};
  797. }
  798. ErrorOr<NonnullOwnPtr<TanCalculationNode>> TanCalculationNode::create(NonnullOwnPtr<CalculationNode> value)
  799. {
  800. return adopt_nonnull_own_or_enomem(new (nothrow) TanCalculationNode(move(value)));
  801. }
  802. TanCalculationNode::TanCalculationNode(NonnullOwnPtr<CalculationNode> value)
  803. : CalculationNode(Type::Tan)
  804. , m_value(move(value))
  805. {
  806. }
  807. TanCalculationNode::~TanCalculationNode() = default;
  808. ErrorOr<String> TanCalculationNode::to_string() const
  809. {
  810. StringBuilder builder;
  811. builder.append("tan("sv);
  812. builder.append(TRY(m_value->to_string()));
  813. builder.append(")"sv);
  814. return builder.to_string();
  815. }
  816. Optional<CalculatedStyleValue::ResolvedType> TanCalculationNode::resolved_type() const
  817. {
  818. return CalculatedStyleValue::ResolvedType::Number;
  819. }
  820. bool TanCalculationNode::contains_percentage() const
  821. {
  822. return m_value->contains_percentage();
  823. }
  824. CalculatedStyleValue::CalculationResult TanCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  825. {
  826. auto node_a = m_value->resolve(context, percentage_basis);
  827. auto node_a_value = resolve_value_radians(node_a.value());
  828. auto result = tan(node_a_value);
  829. return { Number(Number::Type::Number, result) };
  830. }
  831. ErrorOr<void> TanCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  832. {
  833. TRY(m_value->for_each_child_node(callback));
  834. TRY(callback(m_value));
  835. return {};
  836. }
  837. ErrorOr<void> TanCalculationNode::dump(StringBuilder& builder, int indent) const
  838. {
  839. TRY(builder.try_appendff("{: >{}}TAN: {}\n", "", indent, TRY(to_string())));
  840. return {};
  841. }
  842. ErrorOr<NonnullOwnPtr<AsinCalculationNode>> AsinCalculationNode::create(NonnullOwnPtr<CalculationNode> value)
  843. {
  844. return adopt_nonnull_own_or_enomem(new (nothrow) AsinCalculationNode(move(value)));
  845. }
  846. AsinCalculationNode::AsinCalculationNode(NonnullOwnPtr<CalculationNode> value)
  847. : CalculationNode(Type::Asin)
  848. , m_value(move(value))
  849. {
  850. }
  851. AsinCalculationNode::~AsinCalculationNode() = default;
  852. ErrorOr<String> AsinCalculationNode::to_string() const
  853. {
  854. StringBuilder builder;
  855. builder.append("asin("sv);
  856. builder.append(TRY(m_value->to_string()));
  857. builder.append(")"sv);
  858. return builder.to_string();
  859. }
  860. Optional<CalculatedStyleValue::ResolvedType> AsinCalculationNode::resolved_type() const
  861. {
  862. return CalculatedStyleValue::ResolvedType::Angle;
  863. }
  864. bool AsinCalculationNode::contains_percentage() const
  865. {
  866. return m_value->contains_percentage();
  867. }
  868. CalculatedStyleValue::CalculationResult AsinCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  869. {
  870. auto node_a = m_value->resolve(context, percentage_basis);
  871. auto node_a_value = resolve_value(node_a.value(), context);
  872. auto result = asin(node_a_value);
  873. return { Angle(result, Angle::Type::Rad) };
  874. }
  875. ErrorOr<void> AsinCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  876. {
  877. TRY(m_value->for_each_child_node(callback));
  878. TRY(callback(m_value));
  879. return {};
  880. }
  881. ErrorOr<void> AsinCalculationNode::dump(StringBuilder& builder, int indent) const
  882. {
  883. TRY(builder.try_appendff("{: >{}}ASIN: {}\n", "", indent, TRY(to_string())));
  884. return {};
  885. }
  886. ErrorOr<NonnullOwnPtr<AcosCalculationNode>> AcosCalculationNode::create(NonnullOwnPtr<CalculationNode> value)
  887. {
  888. return adopt_nonnull_own_or_enomem(new (nothrow) AcosCalculationNode(move(value)));
  889. }
  890. AcosCalculationNode::AcosCalculationNode(NonnullOwnPtr<CalculationNode> value)
  891. : CalculationNode(Type::Acos)
  892. , m_value(move(value))
  893. {
  894. }
  895. AcosCalculationNode::~AcosCalculationNode() = default;
  896. ErrorOr<String> AcosCalculationNode::to_string() const
  897. {
  898. StringBuilder builder;
  899. builder.append("acos("sv);
  900. builder.append(TRY(m_value->to_string()));
  901. builder.append(")"sv);
  902. return builder.to_string();
  903. }
  904. Optional<CalculatedStyleValue::ResolvedType> AcosCalculationNode::resolved_type() const
  905. {
  906. return CalculatedStyleValue::ResolvedType::Angle;
  907. }
  908. bool AcosCalculationNode::contains_percentage() const
  909. {
  910. return m_value->contains_percentage();
  911. }
  912. CalculatedStyleValue::CalculationResult AcosCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  913. {
  914. auto node_a = m_value->resolve(context, percentage_basis);
  915. auto node_a_value = resolve_value(node_a.value(), context);
  916. auto result = acos(node_a_value);
  917. return { Angle(result, Angle::Type::Rad) };
  918. }
  919. ErrorOr<void> AcosCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  920. {
  921. TRY(m_value->for_each_child_node(callback));
  922. TRY(callback(m_value));
  923. return {};
  924. }
  925. ErrorOr<void> AcosCalculationNode::dump(StringBuilder& builder, int indent) const
  926. {
  927. TRY(builder.try_appendff("{: >{}}ACOS: {}\n", "", indent, TRY(to_string())));
  928. return {};
  929. }
  930. ErrorOr<NonnullOwnPtr<AtanCalculationNode>> AtanCalculationNode::create(NonnullOwnPtr<CalculationNode> value)
  931. {
  932. return adopt_nonnull_own_or_enomem(new (nothrow) AtanCalculationNode(move(value)));
  933. }
  934. AtanCalculationNode::AtanCalculationNode(NonnullOwnPtr<CalculationNode> value)
  935. : CalculationNode(Type::Atan)
  936. , m_value(move(value))
  937. {
  938. }
  939. AtanCalculationNode::~AtanCalculationNode() = default;
  940. ErrorOr<String> AtanCalculationNode::to_string() const
  941. {
  942. StringBuilder builder;
  943. builder.append("atan("sv);
  944. builder.append(TRY(m_value->to_string()));
  945. builder.append(")"sv);
  946. return builder.to_string();
  947. }
  948. Optional<CalculatedStyleValue::ResolvedType> AtanCalculationNode::resolved_type() const
  949. {
  950. return CalculatedStyleValue::ResolvedType::Angle;
  951. }
  952. bool AtanCalculationNode::contains_percentage() const
  953. {
  954. return m_value->contains_percentage();
  955. }
  956. CalculatedStyleValue::CalculationResult AtanCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  957. {
  958. auto node_a = m_value->resolve(context, percentage_basis);
  959. auto node_a_value = resolve_value(node_a.value(), context);
  960. auto result = atan(node_a_value);
  961. return { Angle(result, Angle::Type::Rad) };
  962. }
  963. ErrorOr<void> AtanCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  964. {
  965. TRY(m_value->for_each_child_node(callback));
  966. TRY(callback(m_value));
  967. return {};
  968. }
  969. ErrorOr<void> AtanCalculationNode::dump(StringBuilder& builder, int indent) const
  970. {
  971. TRY(builder.try_appendff("{: >{}}ATAN: {}\n", "", indent, TRY(to_string())));
  972. return {};
  973. }
  974. ErrorOr<NonnullOwnPtr<Atan2CalculationNode>> Atan2CalculationNode::create(NonnullOwnPtr<CalculationNode> y, NonnullOwnPtr<CalculationNode> x)
  975. {
  976. return adopt_nonnull_own_or_enomem(new (nothrow) Atan2CalculationNode(move(y), move(x)));
  977. }
  978. Atan2CalculationNode::Atan2CalculationNode(NonnullOwnPtr<CalculationNode> y, NonnullOwnPtr<CalculationNode> x)
  979. : CalculationNode(Type::Atan2)
  980. , m_y(move(y))
  981. , m_x(move(x))
  982. {
  983. }
  984. Atan2CalculationNode::~Atan2CalculationNode() = default;
  985. ErrorOr<String> Atan2CalculationNode::to_string() const
  986. {
  987. StringBuilder builder;
  988. builder.append("atan2("sv);
  989. builder.append(TRY(m_y->to_string()));
  990. builder.append(", "sv);
  991. builder.append(TRY(m_x->to_string()));
  992. builder.append(")"sv);
  993. return builder.to_string();
  994. }
  995. Optional<CalculatedStyleValue::ResolvedType> Atan2CalculationNode::resolved_type() const
  996. {
  997. return CalculatedStyleValue::ResolvedType::Angle;
  998. }
  999. bool Atan2CalculationNode::contains_percentage() const
  1000. {
  1001. return m_y->contains_percentage() || m_x->contains_percentage();
  1002. }
  1003. CalculatedStyleValue::CalculationResult Atan2CalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  1004. {
  1005. auto node_a = m_y->resolve(context, percentage_basis);
  1006. auto node_a_value = resolve_value(node_a.value(), context);
  1007. auto node_b = m_x->resolve(context, percentage_basis);
  1008. auto node_b_value = resolve_value(node_b.value(), context);
  1009. auto result = atan2(node_a_value, node_b_value);
  1010. return { Angle(result, Angle::Type::Rad) };
  1011. }
  1012. ErrorOr<void> Atan2CalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  1013. {
  1014. TRY(m_y->for_each_child_node(callback));
  1015. TRY(m_x->for_each_child_node(callback));
  1016. TRY(callback(m_y));
  1017. TRY(callback(m_x));
  1018. return {};
  1019. }
  1020. ErrorOr<void> Atan2CalculationNode::dump(StringBuilder& builder, int indent) const
  1021. {
  1022. TRY(builder.try_appendff("{: >{}}ATAN2: {}\n", "", indent, TRY(to_string())));
  1023. return {};
  1024. }
  1025. ErrorOr<NonnullOwnPtr<PowCalculationNode>> PowCalculationNode::create(NonnullOwnPtr<CalculationNode> x, NonnullOwnPtr<CalculationNode> y)
  1026. {
  1027. return adopt_nonnull_own_or_enomem(new (nothrow) PowCalculationNode(move(x), move(y)));
  1028. }
  1029. PowCalculationNode::PowCalculationNode(NonnullOwnPtr<CalculationNode> x, NonnullOwnPtr<CalculationNode> y)
  1030. : CalculationNode(Type::Pow)
  1031. , m_x(move(x))
  1032. , m_y(move(y))
  1033. {
  1034. }
  1035. PowCalculationNode::~PowCalculationNode() = default;
  1036. ErrorOr<String> PowCalculationNode::to_string() const
  1037. {
  1038. StringBuilder builder;
  1039. builder.append("pow("sv);
  1040. builder.append(TRY(m_x->to_string()));
  1041. builder.append(", "sv);
  1042. builder.append(TRY(m_y->to_string()));
  1043. builder.append(")"sv);
  1044. return builder.to_string();
  1045. }
  1046. Optional<CalculatedStyleValue::ResolvedType> PowCalculationNode::resolved_type() const
  1047. {
  1048. return CalculatedStyleValue::ResolvedType::Number;
  1049. }
  1050. CalculatedStyleValue::CalculationResult PowCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  1051. {
  1052. auto node_a = m_x->resolve(context, percentage_basis);
  1053. auto node_a_value = resolve_value(node_a.value(), context);
  1054. auto node_b = m_y->resolve(context, percentage_basis);
  1055. auto node_b_value = resolve_value(node_b.value(), context);
  1056. auto result = pow(node_a_value, node_b_value);
  1057. return { Number(Number::Type::Number, result) };
  1058. }
  1059. ErrorOr<void> PowCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  1060. {
  1061. TRY(m_x->for_each_child_node(callback));
  1062. TRY(m_y->for_each_child_node(callback));
  1063. TRY(callback(m_x));
  1064. TRY(callback(m_y));
  1065. return {};
  1066. }
  1067. ErrorOr<void> PowCalculationNode::dump(StringBuilder& builder, int indent) const
  1068. {
  1069. TRY(builder.try_appendff("{: >{}}POW: {}\n", "", indent, TRY(to_string())));
  1070. return {};
  1071. }
  1072. ErrorOr<NonnullOwnPtr<SqrtCalculationNode>> SqrtCalculationNode::create(NonnullOwnPtr<CalculationNode> value)
  1073. {
  1074. return adopt_nonnull_own_or_enomem(new (nothrow) SqrtCalculationNode(move(value)));
  1075. }
  1076. SqrtCalculationNode::SqrtCalculationNode(NonnullOwnPtr<CalculationNode> value)
  1077. : CalculationNode(Type::Sqrt)
  1078. , m_value(move(value))
  1079. {
  1080. }
  1081. SqrtCalculationNode::~SqrtCalculationNode() = default;
  1082. ErrorOr<String> SqrtCalculationNode::to_string() const
  1083. {
  1084. StringBuilder builder;
  1085. builder.append("sqrt("sv);
  1086. builder.append(TRY(m_value->to_string()));
  1087. builder.append(")"sv);
  1088. return builder.to_string();
  1089. }
  1090. Optional<CalculatedStyleValue::ResolvedType> SqrtCalculationNode::resolved_type() const
  1091. {
  1092. return CalculatedStyleValue::ResolvedType::Number;
  1093. }
  1094. CalculatedStyleValue::CalculationResult SqrtCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  1095. {
  1096. auto node_a = m_value->resolve(context, percentage_basis);
  1097. auto node_a_value = resolve_value(node_a.value(), context);
  1098. auto result = sqrt(node_a_value);
  1099. return { Number(Number::Type::Number, result) };
  1100. }
  1101. ErrorOr<void> SqrtCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  1102. {
  1103. TRY(m_value->for_each_child_node(callback));
  1104. TRY(callback(m_value));
  1105. return {};
  1106. }
  1107. ErrorOr<void> SqrtCalculationNode::dump(StringBuilder& builder, int indent) const
  1108. {
  1109. TRY(builder.try_appendff("{: >{}}SQRT: {}\n", "", indent, TRY(to_string())));
  1110. return {};
  1111. }
  1112. ErrorOr<NonnullOwnPtr<HypotCalculationNode>> HypotCalculationNode::create(Vector<NonnullOwnPtr<Web::CSS::CalculationNode>> values)
  1113. {
  1114. return adopt_nonnull_own_or_enomem(new (nothrow) HypotCalculationNode(move(values)));
  1115. }
  1116. HypotCalculationNode::HypotCalculationNode(Vector<NonnullOwnPtr<CalculationNode>> values)
  1117. : CalculationNode(Type::Hypot)
  1118. , m_values(move(values))
  1119. {
  1120. }
  1121. HypotCalculationNode::~HypotCalculationNode() = default;
  1122. ErrorOr<String> HypotCalculationNode::to_string() const
  1123. {
  1124. StringBuilder builder;
  1125. TRY(builder.try_append("hypot("sv));
  1126. for (size_t i = 0; i < m_values.size(); ++i) {
  1127. if (i != 0)
  1128. TRY(builder.try_append(", "sv));
  1129. TRY(builder.try_append(TRY(m_values[i]->to_string())));
  1130. }
  1131. TRY(builder.try_append(")"sv));
  1132. return builder.to_string();
  1133. }
  1134. Optional<CalculatedStyleValue::ResolvedType> HypotCalculationNode::resolved_type() const
  1135. {
  1136. // NOTE: We check during parsing that all values have the same type.
  1137. return m_values[0]->resolved_type();
  1138. }
  1139. bool HypotCalculationNode::contains_percentage() const
  1140. {
  1141. for (auto const& value : m_values) {
  1142. if (value->contains_percentage())
  1143. return true;
  1144. }
  1145. return false;
  1146. }
  1147. CalculatedStyleValue::CalculationResult HypotCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  1148. {
  1149. double square_sum = 0.0;
  1150. for (auto const& value : m_values) {
  1151. auto child_resolved = value->resolve(context, percentage_basis);
  1152. auto child_value = resolve_value(child_resolved.value(), context);
  1153. square_sum += child_value * child_value;
  1154. }
  1155. auto result = sqrt(square_sum);
  1156. return to_resolved_type(resolved_type().value(), result);
  1157. }
  1158. ErrorOr<void> HypotCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  1159. {
  1160. for (auto& value : m_values) {
  1161. TRY(value->for_each_child_node(callback));
  1162. TRY(callback(value));
  1163. }
  1164. return {};
  1165. }
  1166. ErrorOr<void> HypotCalculationNode::dump(StringBuilder& builder, int indent) const
  1167. {
  1168. TRY(builder.try_appendff("{: >{}}HYPOT:\n", "", indent));
  1169. for (auto const& value : m_values)
  1170. TRY(value->dump(builder, indent + 2));
  1171. return {};
  1172. }
  1173. ErrorOr<NonnullOwnPtr<LogCalculationNode>> LogCalculationNode::create(NonnullOwnPtr<CalculationNode> x, NonnullOwnPtr<CalculationNode> y)
  1174. {
  1175. return adopt_nonnull_own_or_enomem(new (nothrow) LogCalculationNode(move(x), move(y)));
  1176. }
  1177. LogCalculationNode::LogCalculationNode(NonnullOwnPtr<CalculationNode> x, NonnullOwnPtr<CalculationNode> y)
  1178. : CalculationNode(Type::Log)
  1179. , m_x(move(x))
  1180. , m_y(move(y))
  1181. {
  1182. }
  1183. LogCalculationNode::~LogCalculationNode() = default;
  1184. ErrorOr<String> LogCalculationNode::to_string() const
  1185. {
  1186. StringBuilder builder;
  1187. builder.append("log("sv);
  1188. builder.append(TRY(m_x->to_string()));
  1189. builder.append(", "sv);
  1190. builder.append(TRY(m_y->to_string()));
  1191. builder.append(")"sv);
  1192. return builder.to_string();
  1193. }
  1194. Optional<CalculatedStyleValue::ResolvedType> LogCalculationNode::resolved_type() const
  1195. {
  1196. return CalculatedStyleValue::ResolvedType::Number;
  1197. }
  1198. CalculatedStyleValue::CalculationResult LogCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  1199. {
  1200. auto node_a = m_x->resolve(context, percentage_basis);
  1201. auto node_a_value = resolve_value(node_a.value(), context);
  1202. auto node_b = m_y->resolve(context, percentage_basis);
  1203. auto node_b_value = resolve_value(node_b.value(), context);
  1204. auto result = log2(node_a_value) / log2(node_b_value);
  1205. return { Number(Number::Type::Number, result) };
  1206. }
  1207. ErrorOr<void> LogCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  1208. {
  1209. TRY(m_x->for_each_child_node(callback));
  1210. TRY(m_y->for_each_child_node(callback));
  1211. TRY(callback(m_x));
  1212. TRY(callback(m_y));
  1213. return {};
  1214. }
  1215. ErrorOr<void> LogCalculationNode::dump(StringBuilder& builder, int indent) const
  1216. {
  1217. TRY(builder.try_appendff("{: >{}}LOG: {}\n", "", indent, TRY(to_string())));
  1218. return {};
  1219. }
  1220. void CalculatedStyleValue::CalculationResult::add(CalculationResult const& other, Optional<Length::ResolutionContext const&> context, PercentageBasis const& percentage_basis)
  1221. {
  1222. add_or_subtract_internal(SumOperation::Add, other, context, percentage_basis);
  1223. }
  1224. void CalculatedStyleValue::CalculationResult::subtract(CalculationResult const& other, Optional<Length::ResolutionContext const&> context, PercentageBasis const& percentage_basis)
  1225. {
  1226. add_or_subtract_internal(SumOperation::Subtract, other, context, percentage_basis);
  1227. }
  1228. void CalculatedStyleValue::CalculationResult::add_or_subtract_internal(SumOperation op, CalculationResult const& other, Optional<Length::ResolutionContext const&> context, PercentageBasis const& percentage_basis)
  1229. {
  1230. // 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>".
  1231. // Though, having the same type may mean that one side is a <dimension> and the other a <percentage>.
  1232. // Note: This is almost identical to ::add()
  1233. m_value.visit(
  1234. [&](Number const& number) {
  1235. auto other_number = other.m_value.get<Number>();
  1236. if (op == SumOperation::Add) {
  1237. m_value = number + other_number;
  1238. } else {
  1239. m_value = number - other_number;
  1240. }
  1241. },
  1242. [&](Angle const& angle) {
  1243. auto this_degrees = angle.to_degrees();
  1244. if (other.m_value.has<Angle>()) {
  1245. auto other_degrees = other.m_value.get<Angle>().to_degrees();
  1246. if (op == SumOperation::Add)
  1247. m_value = Angle::make_degrees(this_degrees + other_degrees);
  1248. else
  1249. m_value = Angle::make_degrees(this_degrees - other_degrees);
  1250. } else {
  1251. VERIFY(percentage_basis.has<Angle>());
  1252. auto other_degrees = percentage_basis.get<Angle>().percentage_of(other.m_value.get<Percentage>()).to_degrees();
  1253. if (op == SumOperation::Add)
  1254. m_value = Angle::make_degrees(this_degrees + other_degrees);
  1255. else
  1256. m_value = Angle::make_degrees(this_degrees - other_degrees);
  1257. }
  1258. },
  1259. [&](Frequency const& frequency) {
  1260. auto this_hertz = frequency.to_hertz();
  1261. if (other.m_value.has<Frequency>()) {
  1262. auto other_hertz = other.m_value.get<Frequency>().to_hertz();
  1263. if (op == SumOperation::Add)
  1264. m_value = Frequency::make_hertz(this_hertz + other_hertz);
  1265. else
  1266. m_value = Frequency::make_hertz(this_hertz - other_hertz);
  1267. } else {
  1268. VERIFY(percentage_basis.has<Frequency>());
  1269. auto other_hertz = percentage_basis.get<Frequency>().percentage_of(other.m_value.get<Percentage>()).to_hertz();
  1270. if (op == SumOperation::Add)
  1271. m_value = Frequency::make_hertz(this_hertz + other_hertz);
  1272. else
  1273. m_value = Frequency::make_hertz(this_hertz - other_hertz);
  1274. }
  1275. },
  1276. [&](Length const& length) {
  1277. auto this_px = length.to_px(*context);
  1278. if (other.m_value.has<Length>()) {
  1279. auto other_px = other.m_value.get<Length>().to_px(*context);
  1280. if (op == SumOperation::Add)
  1281. m_value = Length::make_px(this_px + other_px);
  1282. else
  1283. m_value = Length::make_px(this_px - other_px);
  1284. } else {
  1285. VERIFY(percentage_basis.has<Length>());
  1286. auto other_px = percentage_basis.get<Length>().percentage_of(other.m_value.get<Percentage>()).to_px(*context);
  1287. if (op == SumOperation::Add)
  1288. m_value = Length::make_px(this_px + other_px);
  1289. else
  1290. m_value = Length::make_px(this_px - other_px);
  1291. }
  1292. },
  1293. [&](Time const& time) {
  1294. auto this_seconds = time.to_seconds();
  1295. if (other.m_value.has<Time>()) {
  1296. auto other_seconds = other.m_value.get<Time>().to_seconds();
  1297. if (op == SumOperation::Add)
  1298. m_value = Time::make_seconds(this_seconds + other_seconds);
  1299. else
  1300. m_value = Time::make_seconds(this_seconds - other_seconds);
  1301. } else {
  1302. VERIFY(percentage_basis.has<Time>());
  1303. auto other_seconds = percentage_basis.get<Time>().percentage_of(other.m_value.get<Percentage>()).to_seconds();
  1304. if (op == SumOperation::Add)
  1305. m_value = Time::make_seconds(this_seconds + other_seconds);
  1306. else
  1307. m_value = Time::make_seconds(this_seconds - other_seconds);
  1308. }
  1309. },
  1310. [&](Percentage const& percentage) {
  1311. if (other.m_value.has<Percentage>()) {
  1312. if (op == SumOperation::Add)
  1313. m_value = Percentage { percentage.value() + other.m_value.get<Percentage>().value() };
  1314. else
  1315. m_value = Percentage { percentage.value() - other.m_value.get<Percentage>().value() };
  1316. return;
  1317. }
  1318. // 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`.
  1319. CalculationResult new_value = other;
  1320. if (op == SumOperation::Add) {
  1321. new_value.add(*this, context, percentage_basis);
  1322. } else {
  1323. // Turn 'this - other' into '-other + this', as 'A + B == B + A', but 'A - B != B - A'
  1324. new_value.multiply_by({ Number { Number::Type::Integer, -1.0f } }, context);
  1325. new_value.add(*this, context, percentage_basis);
  1326. }
  1327. *this = new_value;
  1328. });
  1329. }
  1330. void CalculatedStyleValue::CalculationResult::multiply_by(CalculationResult const& other, Optional<Length::ResolutionContext const&> context)
  1331. {
  1332. // We know from validation when resolving the type, that at least one side must be a <number> or <integer>.
  1333. // Both of these are represented as a double.
  1334. VERIFY(m_value.has<Number>() || other.m_value.has<Number>());
  1335. bool other_is_number = other.m_value.has<Number>();
  1336. m_value.visit(
  1337. [&](Number const& number) {
  1338. if (other_is_number) {
  1339. m_value = number * other.m_value.get<Number>();
  1340. } else {
  1341. // Avoid duplicating all the logic by swapping `this` and `other`.
  1342. CalculationResult new_value = other;
  1343. new_value.multiply_by(*this, context);
  1344. *this = new_value;
  1345. }
  1346. },
  1347. [&](Angle const& angle) {
  1348. m_value = Angle::make_degrees(angle.to_degrees() * other.m_value.get<Number>().value());
  1349. },
  1350. [&](Frequency const& frequency) {
  1351. m_value = Frequency::make_hertz(frequency.to_hertz() * other.m_value.get<Number>().value());
  1352. },
  1353. [&](Length const& length) {
  1354. m_value = Length::make_px(length.to_px(*context) * static_cast<double>(other.m_value.get<Number>().value()));
  1355. },
  1356. [&](Time const& time) {
  1357. m_value = Time::make_seconds(time.to_seconds() * other.m_value.get<Number>().value());
  1358. },
  1359. [&](Percentage const& percentage) {
  1360. m_value = Percentage { percentage.value() * other.m_value.get<Number>().value() };
  1361. });
  1362. }
  1363. void CalculatedStyleValue::CalculationResult::divide_by(CalculationResult const& other, Optional<Length::ResolutionContext const&> context)
  1364. {
  1365. // We know from validation when resolving the type, that `other` must be a <number> or <integer>.
  1366. // Both of these are represented as a Number.
  1367. auto denominator = other.m_value.get<Number>().value();
  1368. // FIXME: Dividing by 0 is invalid, and should be caught during parsing.
  1369. VERIFY(denominator != 0.0);
  1370. m_value.visit(
  1371. [&](Number const& number) {
  1372. m_value = Number {
  1373. Number::Type::Number,
  1374. number.value() / denominator
  1375. };
  1376. },
  1377. [&](Angle const& angle) {
  1378. m_value = Angle::make_degrees(angle.to_degrees() / denominator);
  1379. },
  1380. [&](Frequency const& frequency) {
  1381. m_value = Frequency::make_hertz(frequency.to_hertz() / denominator);
  1382. },
  1383. [&](Length const& length) {
  1384. m_value = Length::make_px(length.to_px(*context) / static_cast<double>(denominator));
  1385. },
  1386. [&](Time const& time) {
  1387. m_value = Time::make_seconds(time.to_seconds() / denominator);
  1388. },
  1389. [&](Percentage const& percentage) {
  1390. m_value = Percentage { percentage.value() / denominator };
  1391. });
  1392. }
  1393. void CalculatedStyleValue::CalculationResult::negate()
  1394. {
  1395. m_value.visit(
  1396. [&](Number const& number) {
  1397. m_value = Number { number.type(), 0 - number.value() };
  1398. },
  1399. [&](Angle const& angle) {
  1400. m_value = Angle { 0 - angle.raw_value(), angle.type() };
  1401. },
  1402. [&](Frequency const& frequency) {
  1403. m_value = Frequency { 0 - frequency.raw_value(), frequency.type() };
  1404. },
  1405. [&](Length const& length) {
  1406. m_value = Length { 0 - length.raw_value(), length.type() };
  1407. },
  1408. [&](Time const& time) {
  1409. m_value = Time { 0 - time.raw_value(), time.type() };
  1410. },
  1411. [&](Percentage const& percentage) {
  1412. m_value = Percentage { 0 - percentage.value() };
  1413. });
  1414. }
  1415. void CalculatedStyleValue::CalculationResult::invert()
  1416. {
  1417. // FIXME: Correctly handle division by zero.
  1418. m_value.visit(
  1419. [&](Number const& number) {
  1420. m_value = Number { Number::Type::Number, 1 / number.value() };
  1421. },
  1422. [&](Angle const& angle) {
  1423. m_value = Angle { 1 / angle.raw_value(), angle.type() };
  1424. },
  1425. [&](Frequency const& frequency) {
  1426. m_value = Frequency { 1 / frequency.raw_value(), frequency.type() };
  1427. },
  1428. [&](Length const& length) {
  1429. m_value = Length { 1 / length.raw_value(), length.type() };
  1430. },
  1431. [&](Time const& time) {
  1432. m_value = Time { 1 / time.raw_value(), time.type() };
  1433. },
  1434. [&](Percentage const& percentage) {
  1435. m_value = Percentage { 1 / percentage.value() };
  1436. });
  1437. }
  1438. ErrorOr<String> CalculatedStyleValue::to_string() const
  1439. {
  1440. // FIXME: Implement this according to https://www.w3.org/TR/css-values-4/#calc-serialize once that stabilizes.
  1441. return String::formatted("calc({})", TRY(m_calculation->to_string()));
  1442. }
  1443. bool CalculatedStyleValue::equals(StyleValue const& other) const
  1444. {
  1445. if (type() != other.type())
  1446. return false;
  1447. // This is a case where comparing the strings actually makes sense.
  1448. return to_string().release_value_but_fixme_should_propagate_errors() == other.to_string().release_value_but_fixme_should_propagate_errors();
  1449. }
  1450. Optional<Angle> CalculatedStyleValue::resolve_angle() const
  1451. {
  1452. auto result = m_calculation->resolve({}, {});
  1453. if (result.value().has<Angle>())
  1454. return result.value().get<Angle>();
  1455. return {};
  1456. }
  1457. Optional<Angle> CalculatedStyleValue::resolve_angle_percentage(Angle const& percentage_basis) const
  1458. {
  1459. auto result = m_calculation->resolve({}, percentage_basis);
  1460. return result.value().visit(
  1461. [&](Angle const& angle) -> Optional<Angle> {
  1462. return angle;
  1463. },
  1464. [&](Percentage const& percentage) -> Optional<Angle> {
  1465. return percentage_basis.percentage_of(percentage);
  1466. },
  1467. [&](auto const&) -> Optional<Angle> {
  1468. return {};
  1469. });
  1470. }
  1471. Optional<Frequency> CalculatedStyleValue::resolve_frequency() const
  1472. {
  1473. auto result = m_calculation->resolve({}, {});
  1474. if (result.value().has<Frequency>())
  1475. return result.value().get<Frequency>();
  1476. return {};
  1477. }
  1478. Optional<Frequency> CalculatedStyleValue::resolve_frequency_percentage(Frequency const& percentage_basis) const
  1479. {
  1480. auto result = m_calculation->resolve({}, percentage_basis);
  1481. return result.value().visit(
  1482. [&](Frequency const& frequency) -> Optional<Frequency> {
  1483. return frequency;
  1484. },
  1485. [&](Percentage const& percentage) -> Optional<Frequency> {
  1486. return percentage_basis.percentage_of(percentage);
  1487. },
  1488. [&](auto const&) -> Optional<Frequency> {
  1489. return {};
  1490. });
  1491. }
  1492. Optional<Length> CalculatedStyleValue::resolve_length(Length::ResolutionContext const& context) const
  1493. {
  1494. auto result = m_calculation->resolve(context, {});
  1495. if (result.value().has<Length>())
  1496. return result.value().get<Length>();
  1497. return {};
  1498. }
  1499. Optional<Length> CalculatedStyleValue::resolve_length(Layout::Node const& layout_node) const
  1500. {
  1501. return resolve_length(Length::ResolutionContext::for_layout_node(layout_node));
  1502. }
  1503. Optional<Length> CalculatedStyleValue::resolve_length_percentage(Layout::Node const& layout_node, Length const& percentage_basis) const
  1504. {
  1505. auto result = m_calculation->resolve(Length::ResolutionContext::for_layout_node(layout_node), percentage_basis);
  1506. return result.value().visit(
  1507. [&](Length const& length) -> Optional<Length> {
  1508. return length;
  1509. },
  1510. [&](Percentage const& percentage) -> Optional<Length> {
  1511. return percentage_basis.percentage_of(percentage);
  1512. },
  1513. [&](auto const&) -> Optional<Length> {
  1514. return {};
  1515. });
  1516. }
  1517. Optional<Percentage> CalculatedStyleValue::resolve_percentage() const
  1518. {
  1519. auto result = m_calculation->resolve({}, {});
  1520. if (result.value().has<Percentage>())
  1521. return result.value().get<Percentage>();
  1522. return {};
  1523. }
  1524. Optional<Time> CalculatedStyleValue::resolve_time() const
  1525. {
  1526. auto result = m_calculation->resolve({}, {});
  1527. if (result.value().has<Time>())
  1528. return result.value().get<Time>();
  1529. return {};
  1530. }
  1531. Optional<Time> CalculatedStyleValue::resolve_time_percentage(Time const& percentage_basis) const
  1532. {
  1533. auto result = m_calculation->resolve({}, percentage_basis);
  1534. return result.value().visit(
  1535. [&](Time const& time) -> Optional<Time> {
  1536. return time;
  1537. },
  1538. [&](auto const&) -> Optional<Time> {
  1539. return {};
  1540. });
  1541. }
  1542. Optional<double> CalculatedStyleValue::resolve_number() const
  1543. {
  1544. auto result = m_calculation->resolve({}, {});
  1545. if (result.value().has<Number>())
  1546. return result.value().get<Number>().value();
  1547. return {};
  1548. }
  1549. Optional<i64> CalculatedStyleValue::resolve_integer()
  1550. {
  1551. auto result = m_calculation->resolve({}, {});
  1552. if (result.value().has<Number>())
  1553. return result.value().get<Number>().integer_value();
  1554. return {};
  1555. }
  1556. bool CalculatedStyleValue::contains_percentage() const
  1557. {
  1558. return m_calculation->contains_percentage();
  1559. }
  1560. }