CalculatedStyleValue.cpp 92 KB

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