CalculatedStyleValue.cpp 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432
  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(RoundingStrategy strategy, NonnullOwnPtr<CalculationNode> x, NonnullOwnPtr<CalculationNode> y)
  1492. {
  1493. return adopt_nonnull_own_or_enomem(new (nothrow) RoundCalculationNode(strategy, move(x), move(y)));
  1494. }
  1495. RoundCalculationNode::RoundCalculationNode(RoundingStrategy mode, NonnullOwnPtr<CalculationNode> x, NonnullOwnPtr<CalculationNode> y)
  1496. : CalculationNode(Type::Round)
  1497. , m_strategy(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. builder.append(CSS::to_string(m_strategy));
  1508. builder.append(", "sv);
  1509. builder.append(TRY(m_x->to_string()));
  1510. builder.append(", "sv);
  1511. builder.append(TRY(m_y->to_string()));
  1512. builder.append(")"sv);
  1513. return builder.to_string();
  1514. }
  1515. Optional<CalculatedStyleValue::ResolvedType> RoundCalculationNode::resolved_type() const
  1516. {
  1517. // Note: We check during parsing that all values have the same type
  1518. return m_x->resolved_type();
  1519. }
  1520. // https://www.w3.org/TR/css-values-4/#determine-the-type-of-a-calculation
  1521. Optional<CSSNumericType> RoundCalculationNode::determine_type(PropertyID property_id) const
  1522. {
  1523. // The result of adding the types of its comma-separated calculations.
  1524. auto x_type = m_x->determine_type(property_id);
  1525. auto y_type = m_y->determine_type(property_id);
  1526. if (!x_type.has_value() || !y_type.has_value())
  1527. return {};
  1528. return x_type->added_to(*y_type);
  1529. }
  1530. bool RoundCalculationNode::contains_percentage() const
  1531. {
  1532. return m_x->contains_percentage() || m_y->contains_percentage();
  1533. }
  1534. CalculatedStyleValue::CalculationResult RoundCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  1535. {
  1536. auto resolved_type = m_x->resolved_type().value();
  1537. auto node_a = m_x->resolve(context, percentage_basis);
  1538. auto node_b = m_y->resolve(context, percentage_basis);
  1539. auto node_a_value = resolve_value(node_a.value(), context);
  1540. auto node_b_value = resolve_value(node_b.value(), context);
  1541. auto upper_b = ceil(node_a_value / node_b_value) * node_b_value;
  1542. auto lower_b = floor(node_a_value / node_b_value) * node_b_value;
  1543. if (m_strategy == RoundingStrategy::Nearest) {
  1544. auto upper_diff = fabs(upper_b - node_a_value);
  1545. auto lower_diff = fabs(node_a_value - lower_b);
  1546. auto rounded_value = upper_diff < lower_diff ? upper_b : lower_b;
  1547. return to_resolved_type(resolved_type, rounded_value);
  1548. }
  1549. if (m_strategy == RoundingStrategy::Up) {
  1550. return to_resolved_type(resolved_type, upper_b);
  1551. }
  1552. if (m_strategy == RoundingStrategy::Down) {
  1553. return to_resolved_type(resolved_type, lower_b);
  1554. }
  1555. if (m_strategy == RoundingStrategy::ToZero) {
  1556. auto upper_diff = fabs(upper_b);
  1557. auto lower_diff = fabs(lower_b);
  1558. auto rounded_value = upper_diff < lower_diff ? upper_b : lower_b;
  1559. return to_resolved_type(resolved_type, rounded_value);
  1560. }
  1561. VERIFY_NOT_REACHED();
  1562. }
  1563. ErrorOr<void> RoundCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  1564. {
  1565. TRY(m_x->for_each_child_node(callback));
  1566. TRY(m_y->for_each_child_node(callback));
  1567. TRY(callback(m_x));
  1568. TRY(callback(m_y));
  1569. return {};
  1570. }
  1571. ErrorOr<void> RoundCalculationNode::dump(StringBuilder& builder, int indent) const
  1572. {
  1573. TRY(builder.try_appendff("{: >{}}ROUND: {}\n", "", indent, TRY(to_string())));
  1574. return {};
  1575. }
  1576. ErrorOr<NonnullOwnPtr<ModCalculationNode>> ModCalculationNode::create(NonnullOwnPtr<CalculationNode> x, NonnullOwnPtr<CalculationNode> y)
  1577. {
  1578. return adopt_nonnull_own_or_enomem(new (nothrow) ModCalculationNode(move(x), move(y)));
  1579. }
  1580. ModCalculationNode::ModCalculationNode(NonnullOwnPtr<CalculationNode> x, NonnullOwnPtr<CalculationNode> y)
  1581. : CalculationNode(Type::Mod)
  1582. , m_x(move(x))
  1583. , m_y(move(y))
  1584. {
  1585. }
  1586. ModCalculationNode::~ModCalculationNode() = default;
  1587. ErrorOr<String> ModCalculationNode::to_string() const
  1588. {
  1589. StringBuilder builder;
  1590. builder.append("mod("sv);
  1591. builder.append(TRY(m_x->to_string()));
  1592. builder.append(", "sv);
  1593. builder.append(TRY(m_y->to_string()));
  1594. builder.append(")"sv);
  1595. return builder.to_string();
  1596. }
  1597. Optional<CalculatedStyleValue::ResolvedType> ModCalculationNode::resolved_type() const
  1598. {
  1599. // Note: We check during parsing that all values have the same type
  1600. return m_x->resolved_type();
  1601. }
  1602. // https://www.w3.org/TR/css-values-4/#determine-the-type-of-a-calculation
  1603. Optional<CSSNumericType> ModCalculationNode::determine_type(PropertyID property_id) const
  1604. {
  1605. // The result of adding the types of its comma-separated calculations.
  1606. auto x_type = m_x->determine_type(property_id);
  1607. auto y_type = m_y->determine_type(property_id);
  1608. if (!x_type.has_value() || !y_type.has_value())
  1609. return {};
  1610. return x_type->added_to(*y_type);
  1611. }
  1612. bool ModCalculationNode::contains_percentage() const
  1613. {
  1614. return m_x->contains_percentage() || m_y->contains_percentage();
  1615. }
  1616. CalculatedStyleValue::CalculationResult ModCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  1617. {
  1618. auto resolved_type = m_x->resolved_type().value();
  1619. auto node_a = m_x->resolve(context, percentage_basis);
  1620. auto node_b = m_y->resolve(context, percentage_basis);
  1621. auto node_a_value = resolve_value(node_a.value(), context);
  1622. auto node_b_value = resolve_value(node_b.value(), context);
  1623. auto quotient = floor(node_a_value / node_b_value);
  1624. auto value = node_a_value - (node_b_value * quotient);
  1625. return to_resolved_type(resolved_type, value);
  1626. }
  1627. ErrorOr<void> ModCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  1628. {
  1629. TRY(m_x->for_each_child_node(callback));
  1630. TRY(m_y->for_each_child_node(callback));
  1631. TRY(callback(m_x));
  1632. TRY(callback(m_y));
  1633. return {};
  1634. }
  1635. ErrorOr<void> ModCalculationNode::dump(StringBuilder& builder, int indent) const
  1636. {
  1637. TRY(builder.try_appendff("{: >{}}MOD: {}\n", "", indent, TRY(to_string())));
  1638. return {};
  1639. }
  1640. ErrorOr<NonnullOwnPtr<RemCalculationNode>> RemCalculationNode::create(NonnullOwnPtr<CalculationNode> x, NonnullOwnPtr<CalculationNode> y)
  1641. {
  1642. return adopt_nonnull_own_or_enomem(new (nothrow) RemCalculationNode(move(x), move(y)));
  1643. }
  1644. RemCalculationNode::RemCalculationNode(NonnullOwnPtr<CalculationNode> x, NonnullOwnPtr<CalculationNode> y)
  1645. : CalculationNode(Type::Rem)
  1646. , m_x(move(x))
  1647. , m_y(move(y))
  1648. {
  1649. }
  1650. RemCalculationNode::~RemCalculationNode() = default;
  1651. ErrorOr<String> RemCalculationNode::to_string() const
  1652. {
  1653. StringBuilder builder;
  1654. builder.append("rem("sv);
  1655. builder.append(TRY(m_x->to_string()));
  1656. builder.append(", "sv);
  1657. builder.append(TRY(m_y->to_string()));
  1658. builder.append(")"sv);
  1659. return builder.to_string();
  1660. }
  1661. Optional<CalculatedStyleValue::ResolvedType> RemCalculationNode::resolved_type() const
  1662. {
  1663. // Note: We check during parsing that all values have the same type
  1664. return m_x->resolved_type();
  1665. }
  1666. // https://www.w3.org/TR/css-values-4/#determine-the-type-of-a-calculation
  1667. Optional<CSSNumericType> RemCalculationNode::determine_type(PropertyID property_id) const
  1668. {
  1669. // The result of adding the types of its comma-separated calculations.
  1670. auto x_type = m_x->determine_type(property_id);
  1671. auto y_type = m_y->determine_type(property_id);
  1672. if (!x_type.has_value() || !y_type.has_value())
  1673. return {};
  1674. return x_type->added_to(*y_type);
  1675. }
  1676. bool RemCalculationNode::contains_percentage() const
  1677. {
  1678. return m_x->contains_percentage() || m_y->contains_percentage();
  1679. }
  1680. CalculatedStyleValue::CalculationResult RemCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  1681. {
  1682. auto resolved_type = m_x->resolved_type().value();
  1683. auto node_a = m_x->resolve(context, percentage_basis);
  1684. auto node_b = m_y->resolve(context, percentage_basis);
  1685. auto node_a_value = resolve_value(node_a.value(), context);
  1686. auto node_b_value = resolve_value(node_b.value(), context);
  1687. auto value = fmod(node_a_value, node_b_value);
  1688. return to_resolved_type(resolved_type, value);
  1689. }
  1690. ErrorOr<void> RemCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  1691. {
  1692. TRY(m_x->for_each_child_node(callback));
  1693. TRY(m_y->for_each_child_node(callback));
  1694. TRY(callback(m_x));
  1695. TRY(callback(m_y));
  1696. return {};
  1697. }
  1698. ErrorOr<void> RemCalculationNode::dump(StringBuilder& builder, int indent) const
  1699. {
  1700. TRY(builder.try_appendff("{: >{}}REM: {}\n", "", indent, TRY(to_string())));
  1701. return {};
  1702. }
  1703. void CalculatedStyleValue::CalculationResult::add(CalculationResult const& other, Optional<Length::ResolutionContext const&> context, PercentageBasis const& percentage_basis)
  1704. {
  1705. add_or_subtract_internal(SumOperation::Add, other, context, percentage_basis);
  1706. }
  1707. void CalculatedStyleValue::CalculationResult::subtract(CalculationResult const& other, Optional<Length::ResolutionContext const&> context, PercentageBasis const& percentage_basis)
  1708. {
  1709. add_or_subtract_internal(SumOperation::Subtract, other, context, percentage_basis);
  1710. }
  1711. void CalculatedStyleValue::CalculationResult::add_or_subtract_internal(SumOperation op, CalculationResult const& other, Optional<Length::ResolutionContext const&> context, PercentageBasis const& percentage_basis)
  1712. {
  1713. // 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>".
  1714. // Though, having the same type may mean that one side is a <dimension> and the other a <percentage>.
  1715. // Note: This is almost identical to ::add()
  1716. m_value.visit(
  1717. [&](Number const& number) {
  1718. auto other_number = other.m_value.get<Number>();
  1719. if (op == SumOperation::Add) {
  1720. m_value = number + other_number;
  1721. } else {
  1722. m_value = number - other_number;
  1723. }
  1724. },
  1725. [&](Angle const& angle) {
  1726. auto this_degrees = angle.to_degrees();
  1727. if (other.m_value.has<Angle>()) {
  1728. auto other_degrees = other.m_value.get<Angle>().to_degrees();
  1729. if (op == SumOperation::Add)
  1730. m_value = Angle::make_degrees(this_degrees + other_degrees);
  1731. else
  1732. m_value = Angle::make_degrees(this_degrees - other_degrees);
  1733. } else {
  1734. VERIFY(percentage_basis.has<Angle>());
  1735. auto other_degrees = percentage_basis.get<Angle>().percentage_of(other.m_value.get<Percentage>()).to_degrees();
  1736. if (op == SumOperation::Add)
  1737. m_value = Angle::make_degrees(this_degrees + other_degrees);
  1738. else
  1739. m_value = Angle::make_degrees(this_degrees - other_degrees);
  1740. }
  1741. },
  1742. [&](Frequency const& frequency) {
  1743. auto this_hertz = frequency.to_hertz();
  1744. if (other.m_value.has<Frequency>()) {
  1745. auto other_hertz = other.m_value.get<Frequency>().to_hertz();
  1746. if (op == SumOperation::Add)
  1747. m_value = Frequency::make_hertz(this_hertz + other_hertz);
  1748. else
  1749. m_value = Frequency::make_hertz(this_hertz - other_hertz);
  1750. } else {
  1751. VERIFY(percentage_basis.has<Frequency>());
  1752. auto other_hertz = percentage_basis.get<Frequency>().percentage_of(other.m_value.get<Percentage>()).to_hertz();
  1753. if (op == SumOperation::Add)
  1754. m_value = Frequency::make_hertz(this_hertz + other_hertz);
  1755. else
  1756. m_value = Frequency::make_hertz(this_hertz - other_hertz);
  1757. }
  1758. },
  1759. [&](Length const& length) {
  1760. auto this_px = length.to_px(*context);
  1761. if (other.m_value.has<Length>()) {
  1762. auto other_px = other.m_value.get<Length>().to_px(*context);
  1763. if (op == SumOperation::Add)
  1764. m_value = Length::make_px(this_px + other_px);
  1765. else
  1766. m_value = Length::make_px(this_px - other_px);
  1767. } else {
  1768. VERIFY(percentage_basis.has<Length>());
  1769. auto other_px = percentage_basis.get<Length>().percentage_of(other.m_value.get<Percentage>()).to_px(*context);
  1770. if (op == SumOperation::Add)
  1771. m_value = Length::make_px(this_px + other_px);
  1772. else
  1773. m_value = Length::make_px(this_px - other_px);
  1774. }
  1775. },
  1776. [&](Time const& time) {
  1777. auto this_seconds = time.to_seconds();
  1778. if (other.m_value.has<Time>()) {
  1779. auto other_seconds = other.m_value.get<Time>().to_seconds();
  1780. if (op == SumOperation::Add)
  1781. m_value = Time::make_seconds(this_seconds + other_seconds);
  1782. else
  1783. m_value = Time::make_seconds(this_seconds - other_seconds);
  1784. } else {
  1785. VERIFY(percentage_basis.has<Time>());
  1786. auto other_seconds = percentage_basis.get<Time>().percentage_of(other.m_value.get<Percentage>()).to_seconds();
  1787. if (op == SumOperation::Add)
  1788. m_value = Time::make_seconds(this_seconds + other_seconds);
  1789. else
  1790. m_value = Time::make_seconds(this_seconds - other_seconds);
  1791. }
  1792. },
  1793. [&](Percentage const& percentage) {
  1794. if (other.m_value.has<Percentage>()) {
  1795. if (op == SumOperation::Add)
  1796. m_value = Percentage { percentage.value() + other.m_value.get<Percentage>().value() };
  1797. else
  1798. m_value = Percentage { percentage.value() - other.m_value.get<Percentage>().value() };
  1799. return;
  1800. }
  1801. // 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`.
  1802. CalculationResult new_value = other;
  1803. if (op == SumOperation::Add) {
  1804. new_value.add(*this, context, percentage_basis);
  1805. } else {
  1806. // Turn 'this - other' into '-other + this', as 'A + B == B + A', but 'A - B != B - A'
  1807. new_value.multiply_by({ Number { Number::Type::Integer, -1.0f } }, context);
  1808. new_value.add(*this, context, percentage_basis);
  1809. }
  1810. *this = new_value;
  1811. });
  1812. }
  1813. void CalculatedStyleValue::CalculationResult::multiply_by(CalculationResult const& other, Optional<Length::ResolutionContext const&> context)
  1814. {
  1815. // We know from validation when resolving the type, that at least one side must be a <number> or <integer>.
  1816. // Both of these are represented as a double.
  1817. VERIFY(m_value.has<Number>() || other.m_value.has<Number>());
  1818. bool other_is_number = other.m_value.has<Number>();
  1819. m_value.visit(
  1820. [&](Number const& number) {
  1821. if (other_is_number) {
  1822. m_value = number * other.m_value.get<Number>();
  1823. } else {
  1824. // Avoid duplicating all the logic by swapping `this` and `other`.
  1825. CalculationResult new_value = other;
  1826. new_value.multiply_by(*this, context);
  1827. *this = new_value;
  1828. }
  1829. },
  1830. [&](Angle const& angle) {
  1831. m_value = Angle::make_degrees(angle.to_degrees() * other.m_value.get<Number>().value());
  1832. },
  1833. [&](Frequency const& frequency) {
  1834. m_value = Frequency::make_hertz(frequency.to_hertz() * other.m_value.get<Number>().value());
  1835. },
  1836. [&](Length const& length) {
  1837. m_value = Length::make_px(length.to_px(*context) * static_cast<double>(other.m_value.get<Number>().value()));
  1838. },
  1839. [&](Time const& time) {
  1840. m_value = Time::make_seconds(time.to_seconds() * other.m_value.get<Number>().value());
  1841. },
  1842. [&](Percentage const& percentage) {
  1843. m_value = Percentage { percentage.value() * other.m_value.get<Number>().value() };
  1844. });
  1845. }
  1846. void CalculatedStyleValue::CalculationResult::divide_by(CalculationResult const& other, Optional<Length::ResolutionContext const&> context)
  1847. {
  1848. // We know from validation when resolving the type, that `other` must be a <number> or <integer>.
  1849. // Both of these are represented as a Number.
  1850. auto denominator = other.m_value.get<Number>().value();
  1851. // FIXME: Dividing by 0 is invalid, and should be caught during parsing.
  1852. VERIFY(denominator != 0.0);
  1853. m_value.visit(
  1854. [&](Number const& number) {
  1855. m_value = Number {
  1856. Number::Type::Number,
  1857. number.value() / denominator
  1858. };
  1859. },
  1860. [&](Angle const& angle) {
  1861. m_value = Angle::make_degrees(angle.to_degrees() / denominator);
  1862. },
  1863. [&](Frequency const& frequency) {
  1864. m_value = Frequency::make_hertz(frequency.to_hertz() / denominator);
  1865. },
  1866. [&](Length const& length) {
  1867. m_value = Length::make_px(length.to_px(*context) / static_cast<double>(denominator));
  1868. },
  1869. [&](Time const& time) {
  1870. m_value = Time::make_seconds(time.to_seconds() / denominator);
  1871. },
  1872. [&](Percentage const& percentage) {
  1873. m_value = Percentage { percentage.value() / denominator };
  1874. });
  1875. }
  1876. void CalculatedStyleValue::CalculationResult::negate()
  1877. {
  1878. m_value.visit(
  1879. [&](Number const& number) {
  1880. m_value = Number { number.type(), 0 - number.value() };
  1881. },
  1882. [&](Angle const& angle) {
  1883. m_value = Angle { 0 - angle.raw_value(), angle.type() };
  1884. },
  1885. [&](Frequency const& frequency) {
  1886. m_value = Frequency { 0 - frequency.raw_value(), frequency.type() };
  1887. },
  1888. [&](Length const& length) {
  1889. m_value = Length { 0 - length.raw_value(), length.type() };
  1890. },
  1891. [&](Time const& time) {
  1892. m_value = Time { 0 - time.raw_value(), time.type() };
  1893. },
  1894. [&](Percentage const& percentage) {
  1895. m_value = Percentage { 0 - percentage.value() };
  1896. });
  1897. }
  1898. void CalculatedStyleValue::CalculationResult::invert()
  1899. {
  1900. // FIXME: Correctly handle division by zero.
  1901. m_value.visit(
  1902. [&](Number const& number) {
  1903. m_value = Number { Number::Type::Number, 1 / number.value() };
  1904. },
  1905. [&](Angle const& angle) {
  1906. m_value = Angle { 1 / angle.raw_value(), angle.type() };
  1907. },
  1908. [&](Frequency const& frequency) {
  1909. m_value = Frequency { 1 / frequency.raw_value(), frequency.type() };
  1910. },
  1911. [&](Length const& length) {
  1912. m_value = Length { 1 / length.raw_value(), length.type() };
  1913. },
  1914. [&](Time const& time) {
  1915. m_value = Time { 1 / time.raw_value(), time.type() };
  1916. },
  1917. [&](Percentage const& percentage) {
  1918. m_value = Percentage { 1 / percentage.value() };
  1919. });
  1920. }
  1921. ErrorOr<String> CalculatedStyleValue::to_string() const
  1922. {
  1923. // FIXME: Implement this according to https://www.w3.org/TR/css-values-4/#calc-serialize once that stabilizes.
  1924. return String::formatted("calc({})", TRY(m_calculation->to_string()));
  1925. }
  1926. bool CalculatedStyleValue::equals(StyleValue const& other) const
  1927. {
  1928. if (type() != other.type())
  1929. return false;
  1930. // This is a case where comparing the strings actually makes sense.
  1931. return to_string().release_value_but_fixme_should_propagate_errors() == other.to_string().release_value_but_fixme_should_propagate_errors();
  1932. }
  1933. Optional<Angle> CalculatedStyleValue::resolve_angle() const
  1934. {
  1935. auto result = m_calculation->resolve({}, {});
  1936. if (result.value().has<Angle>())
  1937. return result.value().get<Angle>();
  1938. return {};
  1939. }
  1940. Optional<Angle> CalculatedStyleValue::resolve_angle_percentage(Angle const& percentage_basis) const
  1941. {
  1942. auto result = m_calculation->resolve({}, percentage_basis);
  1943. return result.value().visit(
  1944. [&](Angle const& angle) -> Optional<Angle> {
  1945. return angle;
  1946. },
  1947. [&](Percentage const& percentage) -> Optional<Angle> {
  1948. return percentage_basis.percentage_of(percentage);
  1949. },
  1950. [&](auto const&) -> Optional<Angle> {
  1951. return {};
  1952. });
  1953. }
  1954. Optional<Frequency> CalculatedStyleValue::resolve_frequency() const
  1955. {
  1956. auto result = m_calculation->resolve({}, {});
  1957. if (result.value().has<Frequency>())
  1958. return result.value().get<Frequency>();
  1959. return {};
  1960. }
  1961. Optional<Frequency> CalculatedStyleValue::resolve_frequency_percentage(Frequency const& percentage_basis) const
  1962. {
  1963. auto result = m_calculation->resolve({}, percentage_basis);
  1964. return result.value().visit(
  1965. [&](Frequency const& frequency) -> Optional<Frequency> {
  1966. return frequency;
  1967. },
  1968. [&](Percentage const& percentage) -> Optional<Frequency> {
  1969. return percentage_basis.percentage_of(percentage);
  1970. },
  1971. [&](auto const&) -> Optional<Frequency> {
  1972. return {};
  1973. });
  1974. }
  1975. Optional<Length> CalculatedStyleValue::resolve_length(Length::ResolutionContext const& context) const
  1976. {
  1977. auto result = m_calculation->resolve(context, {});
  1978. if (result.value().has<Length>())
  1979. return result.value().get<Length>();
  1980. return {};
  1981. }
  1982. Optional<Length> CalculatedStyleValue::resolve_length(Layout::Node const& layout_node) const
  1983. {
  1984. return resolve_length(Length::ResolutionContext::for_layout_node(layout_node));
  1985. }
  1986. Optional<Length> CalculatedStyleValue::resolve_length_percentage(Layout::Node const& layout_node, Length const& percentage_basis) const
  1987. {
  1988. auto result = m_calculation->resolve(Length::ResolutionContext::for_layout_node(layout_node), percentage_basis);
  1989. return result.value().visit(
  1990. [&](Length const& length) -> Optional<Length> {
  1991. return length;
  1992. },
  1993. [&](Percentage const& percentage) -> Optional<Length> {
  1994. return percentage_basis.percentage_of(percentage);
  1995. },
  1996. [&](auto const&) -> Optional<Length> {
  1997. return {};
  1998. });
  1999. }
  2000. Optional<Percentage> CalculatedStyleValue::resolve_percentage() const
  2001. {
  2002. auto result = m_calculation->resolve({}, {});
  2003. if (result.value().has<Percentage>())
  2004. return result.value().get<Percentage>();
  2005. return {};
  2006. }
  2007. Optional<Time> CalculatedStyleValue::resolve_time() const
  2008. {
  2009. auto result = m_calculation->resolve({}, {});
  2010. if (result.value().has<Time>())
  2011. return result.value().get<Time>();
  2012. return {};
  2013. }
  2014. Optional<Time> CalculatedStyleValue::resolve_time_percentage(Time const& percentage_basis) const
  2015. {
  2016. auto result = m_calculation->resolve({}, percentage_basis);
  2017. return result.value().visit(
  2018. [&](Time const& time) -> Optional<Time> {
  2019. return time;
  2020. },
  2021. [&](auto const&) -> Optional<Time> {
  2022. return {};
  2023. });
  2024. }
  2025. Optional<double> CalculatedStyleValue::resolve_number() const
  2026. {
  2027. auto result = m_calculation->resolve({}, {});
  2028. if (result.value().has<Number>())
  2029. return result.value().get<Number>().value();
  2030. return {};
  2031. }
  2032. Optional<i64> CalculatedStyleValue::resolve_integer()
  2033. {
  2034. auto result = m_calculation->resolve({}, {});
  2035. if (result.value().has<Number>())
  2036. return result.value().get<Number>().integer_value();
  2037. return {};
  2038. }
  2039. bool CalculatedStyleValue::contains_percentage() const
  2040. {
  2041. return m_calculation->contains_percentage();
  2042. }
  2043. }