CalculatedStyleValue.cpp 81 KB

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