CalculatedStyleValue.cpp 82 KB

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