CalculatedStyleValue.cpp 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286
  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. namespace Web::CSS {
  12. static bool is_number(CalculatedStyleValue::ResolvedType type)
  13. {
  14. return type == CalculatedStyleValue::ResolvedType::Number || type == CalculatedStyleValue::ResolvedType::Integer;
  15. }
  16. static bool is_dimension(CalculatedStyleValue::ResolvedType type)
  17. {
  18. return type != CalculatedStyleValue::ResolvedType::Number
  19. && type != CalculatedStyleValue::ResolvedType::Integer
  20. && type != CalculatedStyleValue::ResolvedType::Percentage;
  21. }
  22. static double resolve_value_radians(CalculatedStyleValue::CalculationResult::Value value)
  23. {
  24. return value.visit(
  25. [](Number const& number) { return number.value(); },
  26. [](Angle const& angle) { return angle.to_radians(); },
  27. [](auto const&) { VERIFY_NOT_REACHED(); return 0.0; });
  28. };
  29. static double resolve_value(CalculatedStyleValue::CalculationResult::Value value, Optional<Length::ResolutionContext const&> context)
  30. {
  31. return value.visit(
  32. [](Number const& number) { return number.value(); },
  33. [](Angle const& angle) { return angle.to_degrees(); },
  34. [](Frequency const& frequency) { return frequency.to_hertz(); },
  35. [&context](Length const& length) { return length.to_px(*context).to_double(); },
  36. [](Percentage const& percentage) { return percentage.value(); },
  37. [](Time const& time) { return time.to_seconds(); });
  38. };
  39. static CalculatedStyleValue::CalculationResult to_resolved_type(CalculatedStyleValue::ResolvedType type, double value)
  40. {
  41. switch (type) {
  42. case CalculatedStyleValue::ResolvedType::Integer:
  43. return { Number(Number::Type::Integer, value) };
  44. case CalculatedStyleValue::ResolvedType::Number:
  45. return { Number(Number::Type::Number, value) };
  46. case CalculatedStyleValue::ResolvedType::Angle:
  47. return { Angle::make_degrees(value) };
  48. case CalculatedStyleValue::ResolvedType::Frequency:
  49. return { Frequency::make_hertz(value) };
  50. case CalculatedStyleValue::ResolvedType::Length:
  51. return { Length::make_px(value) };
  52. case CalculatedStyleValue::ResolvedType::Percentage:
  53. return { Percentage(value) };
  54. case CalculatedStyleValue::ResolvedType::Time:
  55. return { Time::make_seconds(value) };
  56. }
  57. VERIFY_NOT_REACHED();
  58. };
  59. CalculationNode::CalculationNode(Type type)
  60. : m_type(type)
  61. {
  62. }
  63. CalculationNode::~CalculationNode() = default;
  64. ErrorOr<NonnullOwnPtr<NumericCalculationNode>> NumericCalculationNode::create(NumericValue value)
  65. {
  66. return adopt_nonnull_own_or_enomem(new (nothrow) NumericCalculationNode(move(value)));
  67. }
  68. NumericCalculationNode::NumericCalculationNode(NumericValue value)
  69. : CalculationNode(Type::Numeric)
  70. , m_value(move(value))
  71. {
  72. }
  73. NumericCalculationNode::~NumericCalculationNode() = default;
  74. ErrorOr<String> NumericCalculationNode::to_string() const
  75. {
  76. return m_value.visit([](auto& value) { return value.to_string(); });
  77. }
  78. Optional<CalculatedStyleValue::ResolvedType> NumericCalculationNode::resolved_type() const
  79. {
  80. return m_value.visit(
  81. [](Number const&) { return CalculatedStyleValue::ResolvedType::Number; },
  82. [](Angle const&) { return CalculatedStyleValue::ResolvedType::Angle; },
  83. [](Frequency const&) { return CalculatedStyleValue::ResolvedType::Frequency; },
  84. [](Length const&) { return CalculatedStyleValue::ResolvedType::Length; },
  85. [](Percentage const&) { return CalculatedStyleValue::ResolvedType::Percentage; },
  86. [](Time const&) { return CalculatedStyleValue::ResolvedType::Time; });
  87. }
  88. bool NumericCalculationNode::contains_percentage() const
  89. {
  90. return m_value.has<Percentage>();
  91. }
  92. CalculatedStyleValue::CalculationResult NumericCalculationNode::resolve(Optional<Length::ResolutionContext const&>, CalculatedStyleValue::PercentageBasis const&) const
  93. {
  94. return m_value;
  95. }
  96. ErrorOr<void> NumericCalculationNode::dump(StringBuilder& builder, int indent) const
  97. {
  98. return builder.try_appendff("{: >{}}NUMERIC({})\n", "", indent, TRY(m_value.visit([](auto& it) { return it.to_string(); })));
  99. }
  100. ErrorOr<NonnullOwnPtr<SumCalculationNode>> SumCalculationNode::create(Vector<NonnullOwnPtr<CalculationNode>> values)
  101. {
  102. return adopt_nonnull_own_or_enomem(new (nothrow) SumCalculationNode(move(values)));
  103. }
  104. SumCalculationNode::SumCalculationNode(Vector<NonnullOwnPtr<CalculationNode>> values)
  105. : CalculationNode(Type::Sum)
  106. , m_values(move(values))
  107. {
  108. VERIFY(!m_values.is_empty());
  109. }
  110. SumCalculationNode::~SumCalculationNode() = default;
  111. ErrorOr<String> SumCalculationNode::to_string() const
  112. {
  113. bool first = true;
  114. StringBuilder builder;
  115. for (auto& value : m_values) {
  116. if (!first)
  117. TRY(builder.try_append(" + "sv));
  118. TRY(builder.try_append(TRY(value->to_string())));
  119. first = false;
  120. }
  121. return builder.to_string();
  122. }
  123. Optional<CalculatedStyleValue::ResolvedType> SumCalculationNode::resolved_type() const
  124. {
  125. // FIXME: Implement https://www.w3.org/TR/css-values-4/#determine-the-type-of-a-calculation
  126. // For now, this is just ad-hoc, based on the old implementation.
  127. Optional<CalculatedStyleValue::ResolvedType> type;
  128. for (auto const& value : m_values) {
  129. auto maybe_value_type = value->resolved_type();
  130. if (!maybe_value_type.has_value())
  131. return {};
  132. auto value_type = maybe_value_type.value();
  133. if (!type.has_value()) {
  134. type = value_type;
  135. continue;
  136. }
  137. // At + or -, check that both sides have the same type, or that one side is a <number> and the other is an <integer>.
  138. // If both sides are the same type, resolve to that type.
  139. if (value_type == type)
  140. continue;
  141. // If one side is a <number> and the other is an <integer>, resolve to <number>.
  142. if (is_number(*type) && is_number(value_type)) {
  143. type = CalculatedStyleValue::ResolvedType::Number;
  144. continue;
  145. }
  146. // FIXME: calc() handles <percentage> by allowing them to pretend to be whatever <dimension> type is allowed at this location.
  147. // Since we can't easily check what that type is, we just allow <percentage> to combine with any other <dimension> type.
  148. if (type == CalculatedStyleValue::ResolvedType::Percentage && is_dimension(value_type)) {
  149. type = value_type;
  150. continue;
  151. }
  152. if (is_dimension(*type) && value_type == CalculatedStyleValue::ResolvedType::Percentage)
  153. continue;
  154. return {};
  155. }
  156. return type;
  157. }
  158. bool SumCalculationNode::contains_percentage() const
  159. {
  160. for (auto const& value : m_values) {
  161. if (value->contains_percentage())
  162. return true;
  163. }
  164. return false;
  165. }
  166. CalculatedStyleValue::CalculationResult SumCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  167. {
  168. Optional<CalculatedStyleValue::CalculationResult> total;
  169. for (auto& additional_product : m_values) {
  170. auto additional_value = additional_product->resolve(context, percentage_basis);
  171. if (!total.has_value()) {
  172. total = additional_value;
  173. continue;
  174. }
  175. total->add(additional_value, context, percentage_basis);
  176. }
  177. return total.value();
  178. }
  179. ErrorOr<void> SumCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  180. {
  181. for (auto& item : m_values) {
  182. TRY(item->for_each_child_node(callback));
  183. TRY(callback(item));
  184. }
  185. return {};
  186. }
  187. ErrorOr<void> SumCalculationNode::dump(StringBuilder& builder, int indent) const
  188. {
  189. TRY(builder.try_appendff("{: >{}}SUM:\n", "", indent));
  190. for (auto const& item : m_values)
  191. TRY(item->dump(builder, indent + 2));
  192. return {};
  193. }
  194. ErrorOr<NonnullOwnPtr<ProductCalculationNode>> ProductCalculationNode::create(Vector<NonnullOwnPtr<CalculationNode>> values)
  195. {
  196. return adopt_nonnull_own_or_enomem(new (nothrow) ProductCalculationNode(move(values)));
  197. }
  198. ProductCalculationNode::ProductCalculationNode(Vector<NonnullOwnPtr<CalculationNode>> values)
  199. : CalculationNode(Type::Product)
  200. , m_values(move(values))
  201. {
  202. VERIFY(!m_values.is_empty());
  203. }
  204. ProductCalculationNode::~ProductCalculationNode() = default;
  205. ErrorOr<String> ProductCalculationNode::to_string() const
  206. {
  207. bool first = true;
  208. StringBuilder builder;
  209. for (auto& value : m_values) {
  210. if (!first)
  211. TRY(builder.try_append(" * "sv));
  212. TRY(builder.try_append(TRY(value->to_string())));
  213. first = false;
  214. }
  215. return builder.to_string();
  216. }
  217. Optional<CalculatedStyleValue::ResolvedType> ProductCalculationNode::resolved_type() const
  218. {
  219. // FIXME: Implement https://www.w3.org/TR/css-values-4/#determine-the-type-of-a-calculation
  220. // For now, this is just ad-hoc, based on the old implementation.
  221. Optional<CalculatedStyleValue::ResolvedType> type;
  222. for (auto const& value : m_values) {
  223. auto maybe_value_type = value->resolved_type();
  224. if (!maybe_value_type.has_value())
  225. return {};
  226. auto value_type = maybe_value_type.value();
  227. if (!type.has_value()) {
  228. type = value_type;
  229. continue;
  230. }
  231. // At *, check that at least one side is <number>.
  232. if (!(is_number(*type) || is_number(value_type)))
  233. return {};
  234. // If both sides are <integer>, resolve to <integer>.
  235. if (type == CalculatedStyleValue::ResolvedType::Integer && value_type == CalculatedStyleValue::ResolvedType::Integer) {
  236. type = CalculatedStyleValue::ResolvedType::Integer;
  237. } else {
  238. // Otherwise, resolve to the type of the other side.
  239. if (is_number(*type))
  240. type = value_type;
  241. }
  242. }
  243. return type;
  244. }
  245. bool ProductCalculationNode::contains_percentage() const
  246. {
  247. for (auto const& value : m_values) {
  248. if (value->contains_percentage())
  249. return true;
  250. }
  251. return false;
  252. }
  253. CalculatedStyleValue::CalculationResult ProductCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  254. {
  255. Optional<CalculatedStyleValue::CalculationResult> total;
  256. for (auto& additional_product : m_values) {
  257. auto additional_value = additional_product->resolve(context, percentage_basis);
  258. if (!total.has_value()) {
  259. total = additional_value;
  260. continue;
  261. }
  262. total->multiply_by(additional_value, context);
  263. }
  264. return total.value();
  265. }
  266. ErrorOr<void> ProductCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  267. {
  268. for (auto& item : m_values) {
  269. TRY(item->for_each_child_node(callback));
  270. TRY(callback(item));
  271. }
  272. return {};
  273. }
  274. ErrorOr<void> ProductCalculationNode::dump(StringBuilder& builder, int indent) const
  275. {
  276. TRY(builder.try_appendff("{: >{}}PRODUCT:\n", "", indent));
  277. for (auto const& item : m_values)
  278. TRY(item->dump(builder, indent + 2));
  279. return {};
  280. }
  281. ErrorOr<NonnullOwnPtr<NegateCalculationNode>> NegateCalculationNode::create(NonnullOwnPtr<Web::CSS::CalculationNode> value)
  282. {
  283. return adopt_nonnull_own_or_enomem(new (nothrow) NegateCalculationNode(move(value)));
  284. }
  285. NegateCalculationNode::NegateCalculationNode(NonnullOwnPtr<CalculationNode> value)
  286. : CalculationNode(Type::Negate)
  287. , m_value(move(value))
  288. {
  289. }
  290. NegateCalculationNode::~NegateCalculationNode() = default;
  291. ErrorOr<String> NegateCalculationNode::to_string() const
  292. {
  293. return String::formatted("(0 - {})", TRY(m_value->to_string()));
  294. }
  295. Optional<CalculatedStyleValue::ResolvedType> NegateCalculationNode::resolved_type() const
  296. {
  297. return m_value->resolved_type();
  298. }
  299. bool NegateCalculationNode::contains_percentage() const
  300. {
  301. return m_value->contains_percentage();
  302. }
  303. CalculatedStyleValue::CalculationResult NegateCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  304. {
  305. auto child_value = m_value->resolve(context, percentage_basis);
  306. child_value.negate();
  307. return child_value;
  308. }
  309. ErrorOr<void> NegateCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  310. {
  311. TRY(m_value->for_each_child_node(callback));
  312. TRY(callback(m_value));
  313. return {};
  314. }
  315. ErrorOr<void> NegateCalculationNode::dump(StringBuilder& builder, int indent) const
  316. {
  317. TRY(builder.try_appendff("{: >{}}NEGATE:\n", "", indent));
  318. TRY(m_value->dump(builder, indent + 2));
  319. return {};
  320. }
  321. ErrorOr<NonnullOwnPtr<InvertCalculationNode>> InvertCalculationNode::create(NonnullOwnPtr<Web::CSS::CalculationNode> value)
  322. {
  323. return adopt_nonnull_own_or_enomem(new (nothrow) InvertCalculationNode(move(value)));
  324. }
  325. InvertCalculationNode::InvertCalculationNode(NonnullOwnPtr<CalculationNode> value)
  326. : CalculationNode(Type::Invert)
  327. , m_value(move(value))
  328. {
  329. }
  330. InvertCalculationNode::~InvertCalculationNode() = default;
  331. ErrorOr<String> InvertCalculationNode::to_string() const
  332. {
  333. return String::formatted("(1 / {})", TRY(m_value->to_string()));
  334. }
  335. Optional<CalculatedStyleValue::ResolvedType> InvertCalculationNode::resolved_type() const
  336. {
  337. auto type = m_value->resolved_type();
  338. if (type == CalculatedStyleValue::ResolvedType::Integer)
  339. return CalculatedStyleValue::ResolvedType::Number;
  340. return type;
  341. }
  342. bool InvertCalculationNode::contains_percentage() const
  343. {
  344. return m_value->contains_percentage();
  345. }
  346. CalculatedStyleValue::CalculationResult InvertCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  347. {
  348. auto child_value = m_value->resolve(context, percentage_basis);
  349. child_value.invert();
  350. return child_value;
  351. }
  352. ErrorOr<void> InvertCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  353. {
  354. TRY(m_value->for_each_child_node(callback));
  355. TRY(callback(m_value));
  356. return {};
  357. }
  358. ErrorOr<void> InvertCalculationNode::dump(StringBuilder& builder, int indent) const
  359. {
  360. TRY(builder.try_appendff("{: >{}}INVERT:\n", "", indent));
  361. TRY(m_value->dump(builder, indent + 2));
  362. return {};
  363. }
  364. ErrorOr<NonnullOwnPtr<MinCalculationNode>> MinCalculationNode::create(Vector<NonnullOwnPtr<Web::CSS::CalculationNode>> values)
  365. {
  366. return adopt_nonnull_own_or_enomem(new (nothrow) MinCalculationNode(move(values)));
  367. }
  368. MinCalculationNode::MinCalculationNode(Vector<NonnullOwnPtr<CalculationNode>> values)
  369. : CalculationNode(Type::Min)
  370. , m_values(move(values))
  371. {
  372. }
  373. MinCalculationNode::~MinCalculationNode() = default;
  374. ErrorOr<String> MinCalculationNode::to_string() const
  375. {
  376. StringBuilder builder;
  377. TRY(builder.try_append("min("sv));
  378. for (size_t i = 0; i < m_values.size(); ++i) {
  379. if (i != 0)
  380. TRY(builder.try_append(", "sv));
  381. TRY(builder.try_append(TRY(m_values[i]->to_string())));
  382. }
  383. TRY(builder.try_append(")"sv));
  384. return builder.to_string();
  385. }
  386. Optional<CalculatedStyleValue::ResolvedType> MinCalculationNode::resolved_type() const
  387. {
  388. // NOTE: We check during parsing that all values have the same type.
  389. return m_values[0]->resolved_type();
  390. }
  391. bool MinCalculationNode::contains_percentage() const
  392. {
  393. for (auto const& value : m_values) {
  394. if (value->contains_percentage())
  395. return true;
  396. }
  397. return false;
  398. }
  399. CalculatedStyleValue::CalculationResult MinCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  400. {
  401. CalculatedStyleValue::CalculationResult smallest_node = m_values.first()->resolve(context, percentage_basis);
  402. auto smallest_value = resolve_value(smallest_node.value(), context);
  403. for (size_t i = 1; i < m_values.size(); i++) {
  404. auto child_resolved = m_values[i]->resolve(context, percentage_basis);
  405. auto child_value = resolve_value(child_resolved.value(), context);
  406. if (child_value < smallest_value) {
  407. smallest_value = child_value;
  408. smallest_node = child_resolved;
  409. }
  410. }
  411. return smallest_node;
  412. }
  413. ErrorOr<void> MinCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  414. {
  415. for (auto& value : m_values) {
  416. TRY(value->for_each_child_node(callback));
  417. TRY(callback(value));
  418. }
  419. return {};
  420. }
  421. ErrorOr<void> MinCalculationNode::dump(StringBuilder& builder, int indent) const
  422. {
  423. TRY(builder.try_appendff("{: >{}}MIN:\n", "", indent));
  424. for (auto const& value : m_values)
  425. TRY(value->dump(builder, indent + 2));
  426. return {};
  427. }
  428. ErrorOr<NonnullOwnPtr<MaxCalculationNode>> MaxCalculationNode::create(Vector<NonnullOwnPtr<Web::CSS::CalculationNode>> values)
  429. {
  430. return adopt_nonnull_own_or_enomem(new (nothrow) MaxCalculationNode(move(values)));
  431. }
  432. MaxCalculationNode::MaxCalculationNode(Vector<NonnullOwnPtr<CalculationNode>> values)
  433. : CalculationNode(Type::Max)
  434. , m_values(move(values))
  435. {
  436. }
  437. MaxCalculationNode::~MaxCalculationNode() = default;
  438. ErrorOr<String> MaxCalculationNode::to_string() const
  439. {
  440. StringBuilder builder;
  441. TRY(builder.try_append("max("sv));
  442. for (size_t i = 0; i < m_values.size(); ++i) {
  443. if (i != 0)
  444. TRY(builder.try_append(", "sv));
  445. TRY(builder.try_append(TRY(m_values[i]->to_string())));
  446. }
  447. TRY(builder.try_append(")"sv));
  448. return builder.to_string();
  449. }
  450. Optional<CalculatedStyleValue::ResolvedType> MaxCalculationNode::resolved_type() const
  451. {
  452. // NOTE: We check during parsing that all values have the same type.
  453. return m_values[0]->resolved_type();
  454. }
  455. bool MaxCalculationNode::contains_percentage() const
  456. {
  457. for (auto const& value : m_values) {
  458. if (value->contains_percentage())
  459. return true;
  460. }
  461. return false;
  462. }
  463. CalculatedStyleValue::CalculationResult MaxCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  464. {
  465. CalculatedStyleValue::CalculationResult largest_node = m_values.first()->resolve(context, percentage_basis);
  466. auto largest_value = resolve_value(largest_node.value(), context);
  467. for (size_t i = 1; i < m_values.size(); i++) {
  468. auto child_resolved = m_values[i]->resolve(context, percentage_basis);
  469. auto child_value = resolve_value(child_resolved.value(), context);
  470. if (child_value > largest_value) {
  471. largest_value = child_value;
  472. largest_node = child_resolved;
  473. }
  474. }
  475. return largest_node;
  476. }
  477. ErrorOr<void> MaxCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  478. {
  479. for (auto& value : m_values) {
  480. TRY(value->for_each_child_node(callback));
  481. TRY(callback(value));
  482. }
  483. return {};
  484. }
  485. ErrorOr<void> MaxCalculationNode::dump(StringBuilder& builder, int indent) const
  486. {
  487. TRY(builder.try_appendff("{: >{}}MAX:\n", "", indent));
  488. for (auto const& value : m_values)
  489. TRY(value->dump(builder, indent + 2));
  490. return {};
  491. }
  492. ErrorOr<NonnullOwnPtr<ClampCalculationNode>> ClampCalculationNode::create(NonnullOwnPtr<CalculationNode> min, NonnullOwnPtr<CalculationNode> center, NonnullOwnPtr<CalculationNode> max)
  493. {
  494. return adopt_nonnull_own_or_enomem(new (nothrow) ClampCalculationNode(move(min), move(center), move(max)));
  495. }
  496. ClampCalculationNode::ClampCalculationNode(NonnullOwnPtr<CalculationNode> min, NonnullOwnPtr<CalculationNode> center, NonnullOwnPtr<CalculationNode> max)
  497. : CalculationNode(Type::Clamp)
  498. , m_min_value(move(min))
  499. , m_center_value(move(center))
  500. , m_max_value(move(max))
  501. {
  502. }
  503. ClampCalculationNode::~ClampCalculationNode() = default;
  504. ErrorOr<String> ClampCalculationNode::to_string() const
  505. {
  506. StringBuilder builder;
  507. TRY(builder.try_append("clamp("sv));
  508. TRY(builder.try_append(TRY(m_min_value->to_string())));
  509. TRY(builder.try_append(", "sv));
  510. TRY(builder.try_append(TRY(m_center_value->to_string())));
  511. TRY(builder.try_append(", "sv));
  512. TRY(builder.try_append(TRY(m_max_value->to_string())));
  513. TRY(builder.try_append(")"sv));
  514. return builder.to_string();
  515. }
  516. Optional<CalculatedStyleValue::ResolvedType> ClampCalculationNode::resolved_type() const
  517. {
  518. // NOTE: We check during parsing that all values have the same type.
  519. return m_min_value->resolved_type();
  520. }
  521. bool ClampCalculationNode::contains_percentage() const
  522. {
  523. return m_min_value->contains_percentage() || m_center_value->contains_percentage() || m_max_value->contains_percentage();
  524. }
  525. CalculatedStyleValue::CalculationResult ClampCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  526. {
  527. auto min_node = m_min_value->resolve(context, percentage_basis);
  528. auto center_node = m_center_value->resolve(context, percentage_basis);
  529. auto max_node = m_max_value->resolve(context, percentage_basis);
  530. auto min_value = resolve_value(min_node.value(), context);
  531. auto center_value = resolve_value(center_node.value(), context);
  532. auto max_value = resolve_value(max_node.value(), context);
  533. // NOTE: The value should be returned as "max(MIN, min(VAL, MAX))"
  534. auto chosen_value = max(min_value, min(center_value, max_value));
  535. if (chosen_value == min_value)
  536. return min_node;
  537. if (chosen_value == center_value)
  538. return center_node;
  539. if (chosen_value == max_value)
  540. return max_node;
  541. VERIFY_NOT_REACHED();
  542. }
  543. ErrorOr<void> ClampCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  544. {
  545. TRY(m_min_value->for_each_child_node(callback));
  546. TRY(m_center_value->for_each_child_node(callback));
  547. TRY(m_max_value->for_each_child_node(callback));
  548. TRY(callback(m_min_value));
  549. TRY(callback(m_center_value));
  550. TRY(callback(m_max_value));
  551. return {};
  552. }
  553. ErrorOr<void> ClampCalculationNode::dump(StringBuilder& builder, int indent) const
  554. {
  555. TRY(builder.try_appendff("{: >{}}CLAMP:\n", "", indent));
  556. TRY(m_min_value->dump(builder, indent + 2));
  557. TRY(m_center_value->dump(builder, indent + 2));
  558. TRY(m_max_value->dump(builder, indent + 2));
  559. return {};
  560. }
  561. ErrorOr<NonnullOwnPtr<AbsCalculationNode>> AbsCalculationNode::create(NonnullOwnPtr<CalculationNode> value)
  562. {
  563. return adopt_nonnull_own_or_enomem(new (nothrow) AbsCalculationNode(move(value)));
  564. }
  565. AbsCalculationNode::AbsCalculationNode(NonnullOwnPtr<CalculationNode> value)
  566. : CalculationNode(Type::Abs)
  567. , m_value(move(value))
  568. {
  569. }
  570. AbsCalculationNode::~AbsCalculationNode() = default;
  571. ErrorOr<String> AbsCalculationNode::to_string() const
  572. {
  573. StringBuilder builder;
  574. builder.append("abs("sv);
  575. builder.append(TRY(m_value->to_string()));
  576. builder.append(")"sv);
  577. return builder.to_string();
  578. }
  579. Optional<CalculatedStyleValue::ResolvedType> AbsCalculationNode::resolved_type() const
  580. {
  581. return m_value->resolved_type();
  582. }
  583. bool AbsCalculationNode::contains_percentage() const
  584. {
  585. return m_value->contains_percentage();
  586. }
  587. CalculatedStyleValue::CalculationResult AbsCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  588. {
  589. auto resolved_type = m_value->resolved_type().value();
  590. auto node_a = m_value->resolve(context, percentage_basis);
  591. auto node_a_value = resolve_value(node_a.value(), context);
  592. if (node_a_value < 0)
  593. return to_resolved_type(resolved_type, -node_a_value);
  594. return node_a;
  595. }
  596. ErrorOr<void> AbsCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  597. {
  598. TRY(m_value->for_each_child_node(callback));
  599. TRY(callback(m_value));
  600. return {};
  601. }
  602. ErrorOr<void> AbsCalculationNode::dump(StringBuilder& builder, int indent) const
  603. {
  604. TRY(builder.try_appendff("{: >{}}ABS: {}\n", "", indent, TRY(to_string())));
  605. return {};
  606. }
  607. ErrorOr<NonnullOwnPtr<SignCalculationNode>> SignCalculationNode::create(NonnullOwnPtr<CalculationNode> value)
  608. {
  609. return adopt_nonnull_own_or_enomem(new (nothrow) SignCalculationNode(move(value)));
  610. }
  611. SignCalculationNode::SignCalculationNode(NonnullOwnPtr<CalculationNode> value)
  612. : CalculationNode(Type::Sign)
  613. , m_value(move(value))
  614. {
  615. }
  616. SignCalculationNode::~SignCalculationNode() = default;
  617. ErrorOr<String> SignCalculationNode::to_string() const
  618. {
  619. StringBuilder builder;
  620. builder.append("sign("sv);
  621. builder.append(TRY(m_value->to_string()));
  622. builder.append(")"sv);
  623. return builder.to_string();
  624. }
  625. Optional<CalculatedStyleValue::ResolvedType> SignCalculationNode::resolved_type() const
  626. {
  627. return CalculatedStyleValue::ResolvedType::Integer;
  628. }
  629. bool SignCalculationNode::contains_percentage() const
  630. {
  631. return m_value->contains_percentage();
  632. }
  633. CalculatedStyleValue::CalculationResult SignCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  634. {
  635. auto node_a = m_value->resolve(context, percentage_basis);
  636. auto node_a_value = resolve_value(node_a.value(), context);
  637. if (node_a_value < 0)
  638. return { Number(Number::Type::Integer, -1) };
  639. if (node_a_value > 0)
  640. return { Number(Number::Type::Integer, 1) };
  641. return { Number(Number::Type::Integer, 0) };
  642. }
  643. ErrorOr<void> SignCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  644. {
  645. TRY(m_value->for_each_child_node(callback));
  646. TRY(callback(m_value));
  647. return {};
  648. }
  649. ErrorOr<void> SignCalculationNode::dump(StringBuilder& builder, int indent) const
  650. {
  651. TRY(builder.try_appendff("{: >{}}SIGN: {}\n", "", indent, TRY(to_string())));
  652. return {};
  653. }
  654. ErrorOr<NonnullOwnPtr<ConstantCalculationNode>> ConstantCalculationNode::create(ConstantType constant)
  655. {
  656. return adopt_nonnull_own_or_enomem(new (nothrow) ConstantCalculationNode(constant));
  657. }
  658. ConstantCalculationNode::ConstantCalculationNode(ConstantType constant)
  659. : CalculationNode(Type::Constant)
  660. , m_constant(constant)
  661. {
  662. }
  663. ConstantCalculationNode::~ConstantCalculationNode() = default;
  664. ErrorOr<String> ConstantCalculationNode::to_string() const
  665. {
  666. switch (m_constant) {
  667. case CalculationNode::ConstantType::E:
  668. return "e"_short_string;
  669. case CalculationNode::ConstantType::PI:
  670. return "pi"_short_string;
  671. case CalculationNode::ConstantType::Infinity:
  672. return "infinity"_string;
  673. case CalculationNode::ConstantType::MinusInfinity:
  674. return "-infinity"_string;
  675. case CalculationNode::ConstantType::NaN:
  676. return "NaN"_string;
  677. }
  678. VERIFY_NOT_REACHED();
  679. }
  680. Optional<CalculatedStyleValue::ResolvedType> ConstantCalculationNode::resolved_type() const
  681. {
  682. return CalculatedStyleValue::ResolvedType::Number;
  683. }
  684. CalculatedStyleValue::CalculationResult ConstantCalculationNode::resolve([[maybe_unused]] Optional<Length::ResolutionContext const&> context, [[maybe_unused]] CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  685. {
  686. switch (m_constant) {
  687. case CalculationNode::ConstantType::E:
  688. return { Number(Number::Type::Number, M_E) };
  689. case CalculationNode::ConstantType::PI:
  690. return { Number(Number::Type::Number, M_PI) };
  691. // FIXME: We need to keep track of Infinity and NaN across all nodes, since they require special handling.
  692. case CalculationNode::ConstantType::Infinity:
  693. return { Number(Number::Type::Number, NumericLimits<float>::max()) };
  694. case CalculationNode::ConstantType::MinusInfinity:
  695. return { Number(Number::Type::Number, NumericLimits<float>::lowest()) };
  696. case CalculationNode::ConstantType::NaN:
  697. return { Number(Number::Type::Number, NAN) };
  698. }
  699. VERIFY_NOT_REACHED();
  700. }
  701. ErrorOr<void> ConstantCalculationNode::for_each_child_node([[maybe_unused]] Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  702. {
  703. return {};
  704. }
  705. ErrorOr<void> ConstantCalculationNode::dump(StringBuilder& builder, int indent) const
  706. {
  707. TRY(builder.try_appendff("{: >{}}CONSTANT: {}\n", "", indent, TRY(to_string())));
  708. return {};
  709. }
  710. ErrorOr<NonnullOwnPtr<SinCalculationNode>> SinCalculationNode::create(NonnullOwnPtr<CalculationNode> value)
  711. {
  712. return adopt_nonnull_own_or_enomem(new (nothrow) SinCalculationNode(move(value)));
  713. }
  714. SinCalculationNode::SinCalculationNode(NonnullOwnPtr<CalculationNode> value)
  715. : CalculationNode(Type::Sin)
  716. , m_value(move(value))
  717. {
  718. }
  719. SinCalculationNode::~SinCalculationNode() = default;
  720. ErrorOr<String> SinCalculationNode::to_string() const
  721. {
  722. StringBuilder builder;
  723. builder.append("sin("sv);
  724. builder.append(TRY(m_value->to_string()));
  725. builder.append(")"sv);
  726. return builder.to_string();
  727. }
  728. Optional<CalculatedStyleValue::ResolvedType> SinCalculationNode::resolved_type() const
  729. {
  730. return CalculatedStyleValue::ResolvedType::Number;
  731. }
  732. bool SinCalculationNode::contains_percentage() const
  733. {
  734. return m_value->contains_percentage();
  735. }
  736. CalculatedStyleValue::CalculationResult SinCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  737. {
  738. auto node_a = m_value->resolve(context, percentage_basis);
  739. auto node_a_value = resolve_value_radians(node_a.value());
  740. auto result = sin(node_a_value);
  741. return { Number(Number::Type::Number, result) };
  742. }
  743. ErrorOr<void> SinCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  744. {
  745. TRY(m_value->for_each_child_node(callback));
  746. TRY(callback(m_value));
  747. return {};
  748. }
  749. ErrorOr<void> SinCalculationNode::dump(StringBuilder& builder, int indent) const
  750. {
  751. TRY(builder.try_appendff("{: >{}}SIN: {}\n", "", indent, TRY(to_string())));
  752. return {};
  753. }
  754. void CalculatedStyleValue::CalculationResult::add(CalculationResult const& other, Optional<Length::ResolutionContext const&> context, PercentageBasis const& percentage_basis)
  755. {
  756. add_or_subtract_internal(SumOperation::Add, other, context, percentage_basis);
  757. }
  758. void CalculatedStyleValue::CalculationResult::subtract(CalculationResult const& other, Optional<Length::ResolutionContext const&> context, PercentageBasis const& percentage_basis)
  759. {
  760. add_or_subtract_internal(SumOperation::Subtract, other, context, percentage_basis);
  761. }
  762. void CalculatedStyleValue::CalculationResult::add_or_subtract_internal(SumOperation op, CalculationResult const& other, Optional<Length::ResolutionContext const&> context, PercentageBasis const& percentage_basis)
  763. {
  764. // 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>".
  765. // Though, having the same type may mean that one side is a <dimension> and the other a <percentage>.
  766. // Note: This is almost identical to ::add()
  767. m_value.visit(
  768. [&](Number const& number) {
  769. auto other_number = other.m_value.get<Number>();
  770. if (op == SumOperation::Add) {
  771. m_value = number + other_number;
  772. } else {
  773. m_value = number - other_number;
  774. }
  775. },
  776. [&](Angle const& angle) {
  777. auto this_degrees = angle.to_degrees();
  778. if (other.m_value.has<Angle>()) {
  779. auto other_degrees = other.m_value.get<Angle>().to_degrees();
  780. if (op == SumOperation::Add)
  781. m_value = Angle::make_degrees(this_degrees + other_degrees);
  782. else
  783. m_value = Angle::make_degrees(this_degrees - other_degrees);
  784. } else {
  785. VERIFY(percentage_basis.has<Angle>());
  786. auto other_degrees = percentage_basis.get<Angle>().percentage_of(other.m_value.get<Percentage>()).to_degrees();
  787. if (op == SumOperation::Add)
  788. m_value = Angle::make_degrees(this_degrees + other_degrees);
  789. else
  790. m_value = Angle::make_degrees(this_degrees - other_degrees);
  791. }
  792. },
  793. [&](Frequency const& frequency) {
  794. auto this_hertz = frequency.to_hertz();
  795. if (other.m_value.has<Frequency>()) {
  796. auto other_hertz = other.m_value.get<Frequency>().to_hertz();
  797. if (op == SumOperation::Add)
  798. m_value = Frequency::make_hertz(this_hertz + other_hertz);
  799. else
  800. m_value = Frequency::make_hertz(this_hertz - other_hertz);
  801. } else {
  802. VERIFY(percentage_basis.has<Frequency>());
  803. auto other_hertz = percentage_basis.get<Frequency>().percentage_of(other.m_value.get<Percentage>()).to_hertz();
  804. if (op == SumOperation::Add)
  805. m_value = Frequency::make_hertz(this_hertz + other_hertz);
  806. else
  807. m_value = Frequency::make_hertz(this_hertz - other_hertz);
  808. }
  809. },
  810. [&](Length const& length) {
  811. auto this_px = length.to_px(*context);
  812. if (other.m_value.has<Length>()) {
  813. auto other_px = other.m_value.get<Length>().to_px(*context);
  814. if (op == SumOperation::Add)
  815. m_value = Length::make_px(this_px + other_px);
  816. else
  817. m_value = Length::make_px(this_px - other_px);
  818. } else {
  819. VERIFY(percentage_basis.has<Length>());
  820. auto other_px = percentage_basis.get<Length>().percentage_of(other.m_value.get<Percentage>()).to_px(*context);
  821. if (op == SumOperation::Add)
  822. m_value = Length::make_px(this_px + other_px);
  823. else
  824. m_value = Length::make_px(this_px - other_px);
  825. }
  826. },
  827. [&](Time const& time) {
  828. auto this_seconds = time.to_seconds();
  829. if (other.m_value.has<Time>()) {
  830. auto other_seconds = other.m_value.get<Time>().to_seconds();
  831. if (op == SumOperation::Add)
  832. m_value = Time::make_seconds(this_seconds + other_seconds);
  833. else
  834. m_value = Time::make_seconds(this_seconds - other_seconds);
  835. } else {
  836. VERIFY(percentage_basis.has<Time>());
  837. auto other_seconds = percentage_basis.get<Time>().percentage_of(other.m_value.get<Percentage>()).to_seconds();
  838. if (op == SumOperation::Add)
  839. m_value = Time::make_seconds(this_seconds + other_seconds);
  840. else
  841. m_value = Time::make_seconds(this_seconds - other_seconds);
  842. }
  843. },
  844. [&](Percentage const& percentage) {
  845. if (other.m_value.has<Percentage>()) {
  846. if (op == SumOperation::Add)
  847. m_value = Percentage { percentage.value() + other.m_value.get<Percentage>().value() };
  848. else
  849. m_value = Percentage { percentage.value() - other.m_value.get<Percentage>().value() };
  850. return;
  851. }
  852. // 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`.
  853. CalculationResult new_value = other;
  854. if (op == SumOperation::Add) {
  855. new_value.add(*this, context, percentage_basis);
  856. } else {
  857. // Turn 'this - other' into '-other + this', as 'A + B == B + A', but 'A - B != B - A'
  858. new_value.multiply_by({ Number { Number::Type::Integer, -1.0f } }, context);
  859. new_value.add(*this, context, percentage_basis);
  860. }
  861. *this = new_value;
  862. });
  863. }
  864. void CalculatedStyleValue::CalculationResult::multiply_by(CalculationResult const& other, Optional<Length::ResolutionContext const&> context)
  865. {
  866. // We know from validation when resolving the type, that at least one side must be a <number> or <integer>.
  867. // Both of these are represented as a double.
  868. VERIFY(m_value.has<Number>() || other.m_value.has<Number>());
  869. bool other_is_number = other.m_value.has<Number>();
  870. m_value.visit(
  871. [&](Number const& number) {
  872. if (other_is_number) {
  873. m_value = number * other.m_value.get<Number>();
  874. } else {
  875. // Avoid duplicating all the logic by swapping `this` and `other`.
  876. CalculationResult new_value = other;
  877. new_value.multiply_by(*this, context);
  878. *this = new_value;
  879. }
  880. },
  881. [&](Angle const& angle) {
  882. m_value = Angle::make_degrees(angle.to_degrees() * other.m_value.get<Number>().value());
  883. },
  884. [&](Frequency const& frequency) {
  885. m_value = Frequency::make_hertz(frequency.to_hertz() * other.m_value.get<Number>().value());
  886. },
  887. [&](Length const& length) {
  888. m_value = Length::make_px(length.to_px(*context) * static_cast<double>(other.m_value.get<Number>().value()));
  889. },
  890. [&](Time const& time) {
  891. m_value = Time::make_seconds(time.to_seconds() * other.m_value.get<Number>().value());
  892. },
  893. [&](Percentage const& percentage) {
  894. m_value = Percentage { percentage.value() * other.m_value.get<Number>().value() };
  895. });
  896. }
  897. void CalculatedStyleValue::CalculationResult::divide_by(CalculationResult const& other, Optional<Length::ResolutionContext const&> context)
  898. {
  899. // We know from validation when resolving the type, that `other` must be a <number> or <integer>.
  900. // Both of these are represented as a Number.
  901. auto denominator = other.m_value.get<Number>().value();
  902. // FIXME: Dividing by 0 is invalid, and should be caught during parsing.
  903. VERIFY(denominator != 0.0);
  904. m_value.visit(
  905. [&](Number const& number) {
  906. m_value = Number {
  907. Number::Type::Number,
  908. number.value() / denominator
  909. };
  910. },
  911. [&](Angle const& angle) {
  912. m_value = Angle::make_degrees(angle.to_degrees() / denominator);
  913. },
  914. [&](Frequency const& frequency) {
  915. m_value = Frequency::make_hertz(frequency.to_hertz() / denominator);
  916. },
  917. [&](Length const& length) {
  918. m_value = Length::make_px(length.to_px(*context) / static_cast<double>(denominator));
  919. },
  920. [&](Time const& time) {
  921. m_value = Time::make_seconds(time.to_seconds() / denominator);
  922. },
  923. [&](Percentage const& percentage) {
  924. m_value = Percentage { percentage.value() / denominator };
  925. });
  926. }
  927. void CalculatedStyleValue::CalculationResult::negate()
  928. {
  929. m_value.visit(
  930. [&](Number const& number) {
  931. m_value = Number { number.type(), 0 - number.value() };
  932. },
  933. [&](Angle const& angle) {
  934. m_value = Angle { 0 - angle.raw_value(), angle.type() };
  935. },
  936. [&](Frequency const& frequency) {
  937. m_value = Frequency { 0 - frequency.raw_value(), frequency.type() };
  938. },
  939. [&](Length const& length) {
  940. m_value = Length { 0 - length.raw_value(), length.type() };
  941. },
  942. [&](Time const& time) {
  943. m_value = Time { 0 - time.raw_value(), time.type() };
  944. },
  945. [&](Percentage const& percentage) {
  946. m_value = Percentage { 0 - percentage.value() };
  947. });
  948. }
  949. void CalculatedStyleValue::CalculationResult::invert()
  950. {
  951. // FIXME: Correctly handle division by zero.
  952. m_value.visit(
  953. [&](Number const& number) {
  954. m_value = Number { Number::Type::Number, 1 / number.value() };
  955. },
  956. [&](Angle const& angle) {
  957. m_value = Angle { 1 / angle.raw_value(), angle.type() };
  958. },
  959. [&](Frequency const& frequency) {
  960. m_value = Frequency { 1 / frequency.raw_value(), frequency.type() };
  961. },
  962. [&](Length const& length) {
  963. m_value = Length { 1 / length.raw_value(), length.type() };
  964. },
  965. [&](Time const& time) {
  966. m_value = Time { 1 / time.raw_value(), time.type() };
  967. },
  968. [&](Percentage const& percentage) {
  969. m_value = Percentage { 1 / percentage.value() };
  970. });
  971. }
  972. ErrorOr<String> CalculatedStyleValue::to_string() const
  973. {
  974. // FIXME: Implement this according to https://www.w3.org/TR/css-values-4/#calc-serialize once that stabilizes.
  975. return String::formatted("calc({})", TRY(m_calculation->to_string()));
  976. }
  977. bool CalculatedStyleValue::equals(StyleValue const& other) const
  978. {
  979. if (type() != other.type())
  980. return false;
  981. // This is a case where comparing the strings actually makes sense.
  982. return to_string().release_value_but_fixme_should_propagate_errors() == other.to_string().release_value_but_fixme_should_propagate_errors();
  983. }
  984. Optional<Angle> CalculatedStyleValue::resolve_angle() const
  985. {
  986. auto result = m_calculation->resolve({}, {});
  987. if (result.value().has<Angle>())
  988. return result.value().get<Angle>();
  989. return {};
  990. }
  991. Optional<Angle> CalculatedStyleValue::resolve_angle_percentage(Angle const& percentage_basis) const
  992. {
  993. auto result = m_calculation->resolve({}, percentage_basis);
  994. return result.value().visit(
  995. [&](Angle const& angle) -> Optional<Angle> {
  996. return angle;
  997. },
  998. [&](Percentage const& percentage) -> Optional<Angle> {
  999. return percentage_basis.percentage_of(percentage);
  1000. },
  1001. [&](auto const&) -> Optional<Angle> {
  1002. return {};
  1003. });
  1004. }
  1005. Optional<Frequency> CalculatedStyleValue::resolve_frequency() const
  1006. {
  1007. auto result = m_calculation->resolve({}, {});
  1008. if (result.value().has<Frequency>())
  1009. return result.value().get<Frequency>();
  1010. return {};
  1011. }
  1012. Optional<Frequency> CalculatedStyleValue::resolve_frequency_percentage(Frequency const& percentage_basis) const
  1013. {
  1014. auto result = m_calculation->resolve({}, percentage_basis);
  1015. return result.value().visit(
  1016. [&](Frequency const& frequency) -> Optional<Frequency> {
  1017. return frequency;
  1018. },
  1019. [&](Percentage const& percentage) -> Optional<Frequency> {
  1020. return percentage_basis.percentage_of(percentage);
  1021. },
  1022. [&](auto const&) -> Optional<Frequency> {
  1023. return {};
  1024. });
  1025. }
  1026. Optional<Length> CalculatedStyleValue::resolve_length(Length::ResolutionContext const& context) const
  1027. {
  1028. auto result = m_calculation->resolve(context, {});
  1029. if (result.value().has<Length>())
  1030. return result.value().get<Length>();
  1031. return {};
  1032. }
  1033. Optional<Length> CalculatedStyleValue::resolve_length(Layout::Node const& layout_node) const
  1034. {
  1035. return resolve_length(Length::ResolutionContext::for_layout_node(layout_node));
  1036. }
  1037. Optional<Length> CalculatedStyleValue::resolve_length_percentage(Layout::Node const& layout_node, Length const& percentage_basis) const
  1038. {
  1039. auto result = m_calculation->resolve(Length::ResolutionContext::for_layout_node(layout_node), percentage_basis);
  1040. return result.value().visit(
  1041. [&](Length const& length) -> Optional<Length> {
  1042. return length;
  1043. },
  1044. [&](Percentage const& percentage) -> Optional<Length> {
  1045. return percentage_basis.percentage_of(percentage);
  1046. },
  1047. [&](auto const&) -> Optional<Length> {
  1048. return {};
  1049. });
  1050. }
  1051. Optional<Percentage> CalculatedStyleValue::resolve_percentage() const
  1052. {
  1053. auto result = m_calculation->resolve({}, {});
  1054. if (result.value().has<Percentage>())
  1055. return result.value().get<Percentage>();
  1056. return {};
  1057. }
  1058. Optional<Time> CalculatedStyleValue::resolve_time() const
  1059. {
  1060. auto result = m_calculation->resolve({}, {});
  1061. if (result.value().has<Time>())
  1062. return result.value().get<Time>();
  1063. return {};
  1064. }
  1065. Optional<Time> CalculatedStyleValue::resolve_time_percentage(Time const& percentage_basis) const
  1066. {
  1067. auto result = m_calculation->resolve({}, percentage_basis);
  1068. return result.value().visit(
  1069. [&](Time const& time) -> Optional<Time> {
  1070. return time;
  1071. },
  1072. [&](auto const&) -> Optional<Time> {
  1073. return {};
  1074. });
  1075. }
  1076. Optional<double> CalculatedStyleValue::resolve_number() const
  1077. {
  1078. auto result = m_calculation->resolve({}, {});
  1079. if (result.value().has<Number>())
  1080. return result.value().get<Number>().value();
  1081. return {};
  1082. }
  1083. Optional<i64> CalculatedStyleValue::resolve_integer()
  1084. {
  1085. auto result = m_calculation->resolve({}, {});
  1086. if (result.value().has<Number>())
  1087. return result.value().get<Number>().integer_value();
  1088. return {};
  1089. }
  1090. bool CalculatedStyleValue::contains_percentage() const
  1091. {
  1092. return m_calculation->contains_percentage();
  1093. }
  1094. }