CalculatedStyleValue.cpp 84 KB

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