CSSMathValue.cpp 94 KB

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