StyleValue.cpp 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
  4. * Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
  5. * Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #include <LibWeb/CSS/StyleValue.h>
  10. #include <LibWeb/CSS/StyleValues/AbstractImageStyleValue.h>
  11. #include <LibWeb/CSS/StyleValues/AngleStyleValue.h>
  12. #include <LibWeb/CSS/StyleValues/BackgroundRepeatStyleValue.h>
  13. #include <LibWeb/CSS/StyleValues/BackgroundSizeStyleValue.h>
  14. #include <LibWeb/CSS/StyleValues/BackgroundStyleValue.h>
  15. #include <LibWeb/CSS/StyleValues/BorderRadiusShorthandStyleValue.h>
  16. #include <LibWeb/CSS/StyleValues/BorderRadiusStyleValue.h>
  17. #include <LibWeb/CSS/StyleValues/BorderStyleValue.h>
  18. #include <LibWeb/CSS/StyleValues/ColorStyleValue.h>
  19. #include <LibWeb/CSS/StyleValues/ConicGradientStyleValue.h>
  20. #include <LibWeb/CSS/StyleValues/ContentStyleValue.h>
  21. #include <LibWeb/CSS/StyleValues/FilterValueListStyleValue.h>
  22. #include <LibWeb/CSS/StyleValues/FlexFlowStyleValue.h>
  23. #include <LibWeb/CSS/StyleValues/FlexStyleValue.h>
  24. #include <LibWeb/CSS/StyleValues/FontStyleValue.h>
  25. #include <LibWeb/CSS/StyleValues/FrequencyStyleValue.h>
  26. #include <LibWeb/CSS/StyleValues/GridAreaShorthandStyleValue.h>
  27. #include <LibWeb/CSS/StyleValues/GridTemplateAreaStyleValue.h>
  28. #include <LibWeb/CSS/StyleValues/GridTrackPlacementShorthandStyleValue.h>
  29. #include <LibWeb/CSS/StyleValues/GridTrackPlacementStyleValue.h>
  30. #include <LibWeb/CSS/StyleValues/GridTrackSizeStyleValue.h>
  31. #include <LibWeb/CSS/StyleValues/IdentifierStyleValue.h>
  32. #include <LibWeb/CSS/StyleValues/ImageStyleValue.h>
  33. #include <LibWeb/CSS/StyleValues/InheritStyleValue.h>
  34. #include <LibWeb/CSS/StyleValues/InitialStyleValue.h>
  35. #include <LibWeb/CSS/StyleValues/LengthStyleValue.h>
  36. #include <LibWeb/CSS/StyleValues/LinearGradientStyleValue.h>
  37. #include <LibWeb/CSS/StyleValues/ListStyleStyleValue.h>
  38. #include <LibWeb/CSS/StyleValues/NumericStyleValue.h>
  39. #include <LibWeb/CSS/StyleValues/OverflowStyleValue.h>
  40. #include <LibWeb/CSS/StyleValues/PercentageStyleValue.h>
  41. #include <LibWeb/CSS/StyleValues/PositionStyleValue.h>
  42. #include <LibWeb/CSS/StyleValues/RadialGradientStyleValue.h>
  43. #include <LibWeb/CSS/StyleValues/RectStyleValue.h>
  44. #include <LibWeb/CSS/StyleValues/ResolutionStyleValue.h>
  45. #include <LibWeb/CSS/StyleValues/ShadowStyleValue.h>
  46. #include <LibWeb/CSS/StyleValues/StringStyleValue.h>
  47. #include <LibWeb/CSS/StyleValues/StyleValueList.h>
  48. #include <LibWeb/CSS/StyleValues/TextDecorationStyleValue.h>
  49. #include <LibWeb/CSS/StyleValues/TimeStyleValue.h>
  50. #include <LibWeb/CSS/StyleValues/TransformationStyleValue.h>
  51. #include <LibWeb/CSS/StyleValues/UnresolvedStyleValue.h>
  52. #include <LibWeb/CSS/StyleValues/UnsetStyleValue.h>
  53. namespace Web::CSS {
  54. StyleValue::StyleValue(Type type)
  55. : m_type(type)
  56. {
  57. }
  58. AbstractImageStyleValue const& StyleValue::as_abstract_image() const
  59. {
  60. VERIFY(is_abstract_image());
  61. return static_cast<AbstractImageStyleValue const&>(*this);
  62. }
  63. AngleStyleValue const& StyleValue::as_angle() const
  64. {
  65. VERIFY(is_angle());
  66. return static_cast<AngleStyleValue const&>(*this);
  67. }
  68. BackgroundStyleValue const& StyleValue::as_background() const
  69. {
  70. VERIFY(is_background());
  71. return static_cast<BackgroundStyleValue const&>(*this);
  72. }
  73. BackgroundRepeatStyleValue const& StyleValue::as_background_repeat() const
  74. {
  75. VERIFY(is_background_repeat());
  76. return static_cast<BackgroundRepeatStyleValue const&>(*this);
  77. }
  78. BackgroundSizeStyleValue const& StyleValue::as_background_size() const
  79. {
  80. VERIFY(is_background_size());
  81. return static_cast<BackgroundSizeStyleValue const&>(*this);
  82. }
  83. BorderStyleValue const& StyleValue::as_border() const
  84. {
  85. VERIFY(is_border());
  86. return static_cast<BorderStyleValue const&>(*this);
  87. }
  88. BorderRadiusStyleValue const& StyleValue::as_border_radius() const
  89. {
  90. VERIFY(is_border_radius());
  91. return static_cast<BorderRadiusStyleValue const&>(*this);
  92. }
  93. BorderRadiusShorthandStyleValue const& StyleValue::as_border_radius_shorthand() const
  94. {
  95. VERIFY(is_border_radius_shorthand());
  96. return static_cast<BorderRadiusShorthandStyleValue const&>(*this);
  97. }
  98. ShadowStyleValue const& StyleValue::as_shadow() const
  99. {
  100. VERIFY(is_shadow());
  101. return static_cast<ShadowStyleValue const&>(*this);
  102. }
  103. CalculatedStyleValue const& StyleValue::as_calculated() const
  104. {
  105. VERIFY(is_calculated());
  106. return static_cast<CalculatedStyleValue const&>(*this);
  107. }
  108. ColorStyleValue const& StyleValue::as_color() const
  109. {
  110. VERIFY(is_color());
  111. return static_cast<ColorStyleValue const&>(*this);
  112. }
  113. ConicGradientStyleValue const& StyleValue::as_conic_gradient() const
  114. {
  115. VERIFY(is_conic_gradient());
  116. return static_cast<ConicGradientStyleValue const&>(*this);
  117. }
  118. ContentStyleValue const& StyleValue::as_content() const
  119. {
  120. VERIFY(is_content());
  121. return static_cast<ContentStyleValue const&>(*this);
  122. }
  123. FilterValueListStyleValue const& StyleValue::as_filter_value_list() const
  124. {
  125. VERIFY(is_filter_value_list());
  126. return static_cast<FilterValueListStyleValue const&>(*this);
  127. }
  128. FlexStyleValue const& StyleValue::as_flex() const
  129. {
  130. VERIFY(is_flex());
  131. return static_cast<FlexStyleValue const&>(*this);
  132. }
  133. FlexFlowStyleValue const& StyleValue::as_flex_flow() const
  134. {
  135. VERIFY(is_flex_flow());
  136. return static_cast<FlexFlowStyleValue const&>(*this);
  137. }
  138. FontStyleValue const& StyleValue::as_font() const
  139. {
  140. VERIFY(is_font());
  141. return static_cast<FontStyleValue const&>(*this);
  142. }
  143. FrequencyStyleValue const& StyleValue::as_frequency() const
  144. {
  145. VERIFY(is_frequency());
  146. return static_cast<FrequencyStyleValue const&>(*this);
  147. }
  148. GridTrackPlacementShorthandStyleValue const& StyleValue::as_grid_track_placement_shorthand() const
  149. {
  150. VERIFY(is_grid_track_placement_shorthand());
  151. return static_cast<GridTrackPlacementShorthandStyleValue const&>(*this);
  152. }
  153. GridAreaShorthandStyleValue const& StyleValue::as_grid_area_shorthand() const
  154. {
  155. VERIFY(is_grid_area_shorthand());
  156. return static_cast<GridAreaShorthandStyleValue const&>(*this);
  157. }
  158. GridTemplateAreaStyleValue const& StyleValue::as_grid_template_area() const
  159. {
  160. VERIFY(is_grid_template_area());
  161. return static_cast<GridTemplateAreaStyleValue const&>(*this);
  162. }
  163. GridTrackPlacementStyleValue const& StyleValue::as_grid_track_placement() const
  164. {
  165. VERIFY(is_grid_track_placement());
  166. return static_cast<GridTrackPlacementStyleValue const&>(*this);
  167. }
  168. IdentifierStyleValue const& StyleValue::as_identifier() const
  169. {
  170. VERIFY(is_identifier());
  171. return static_cast<IdentifierStyleValue const&>(*this);
  172. }
  173. ImageStyleValue const& StyleValue::as_image() const
  174. {
  175. VERIFY(is_image());
  176. return static_cast<ImageStyleValue const&>(*this);
  177. }
  178. InheritStyleValue const& StyleValue::as_inherit() const
  179. {
  180. VERIFY(is_inherit());
  181. return static_cast<InheritStyleValue const&>(*this);
  182. }
  183. InitialStyleValue const& StyleValue::as_initial() const
  184. {
  185. VERIFY(is_initial());
  186. return static_cast<InitialStyleValue const&>(*this);
  187. }
  188. LengthStyleValue const& StyleValue::as_length() const
  189. {
  190. VERIFY(is_length());
  191. return static_cast<LengthStyleValue const&>(*this);
  192. }
  193. GridTrackSizeStyleValue const& StyleValue::as_grid_track_size_list() const
  194. {
  195. VERIFY(is_grid_track_size_list());
  196. return static_cast<GridTrackSizeStyleValue const&>(*this);
  197. }
  198. LinearGradientStyleValue const& StyleValue::as_linear_gradient() const
  199. {
  200. VERIFY(is_linear_gradient());
  201. return static_cast<LinearGradientStyleValue const&>(*this);
  202. }
  203. ListStyleStyleValue const& StyleValue::as_list_style() const
  204. {
  205. VERIFY(is_list_style());
  206. return static_cast<ListStyleStyleValue const&>(*this);
  207. }
  208. NumericStyleValue const& StyleValue::as_numeric() const
  209. {
  210. VERIFY(is_numeric());
  211. return static_cast<NumericStyleValue const&>(*this);
  212. }
  213. OverflowStyleValue const& StyleValue::as_overflow() const
  214. {
  215. VERIFY(is_overflow());
  216. return static_cast<OverflowStyleValue const&>(*this);
  217. }
  218. PercentageStyleValue const& StyleValue::as_percentage() const
  219. {
  220. VERIFY(is_percentage());
  221. return static_cast<PercentageStyleValue const&>(*this);
  222. }
  223. PositionStyleValue const& StyleValue::as_position() const
  224. {
  225. VERIFY(is_position());
  226. return static_cast<PositionStyleValue const&>(*this);
  227. }
  228. RadialGradientStyleValue const& StyleValue::as_radial_gradient() const
  229. {
  230. VERIFY(is_radial_gradient());
  231. return static_cast<RadialGradientStyleValue const&>(*this);
  232. }
  233. RectStyleValue const& StyleValue::as_rect() const
  234. {
  235. VERIFY(is_rect());
  236. return static_cast<RectStyleValue const&>(*this);
  237. }
  238. ResolutionStyleValue const& StyleValue::as_resolution() const
  239. {
  240. VERIFY(is_resolution());
  241. return static_cast<ResolutionStyleValue const&>(*this);
  242. }
  243. StringStyleValue const& StyleValue::as_string() const
  244. {
  245. VERIFY(is_string());
  246. return static_cast<StringStyleValue const&>(*this);
  247. }
  248. TextDecorationStyleValue const& StyleValue::as_text_decoration() const
  249. {
  250. VERIFY(is_text_decoration());
  251. return static_cast<TextDecorationStyleValue const&>(*this);
  252. }
  253. TimeStyleValue const& StyleValue::as_time() const
  254. {
  255. VERIFY(is_time());
  256. return static_cast<TimeStyleValue const&>(*this);
  257. }
  258. TransformationStyleValue const& StyleValue::as_transformation() const
  259. {
  260. VERIFY(is_transformation());
  261. return static_cast<TransformationStyleValue const&>(*this);
  262. }
  263. UnresolvedStyleValue const& StyleValue::as_unresolved() const
  264. {
  265. VERIFY(is_unresolved());
  266. return static_cast<UnresolvedStyleValue const&>(*this);
  267. }
  268. UnsetStyleValue const& StyleValue::as_unset() const
  269. {
  270. VERIFY(is_unset());
  271. return static_cast<UnsetStyleValue const&>(*this);
  272. }
  273. StyleValueList const& StyleValue::as_value_list() const
  274. {
  275. VERIFY(is_value_list());
  276. return static_cast<StyleValueList const&>(*this);
  277. }
  278. void CalculatedStyleValue::CalculationResult::add(CalculationResult const& other, Layout::Node const* layout_node, PercentageBasis const& percentage_basis)
  279. {
  280. add_or_subtract_internal(SumOperation::Add, other, layout_node, percentage_basis);
  281. }
  282. void CalculatedStyleValue::CalculationResult::subtract(CalculationResult const& other, Layout::Node const* layout_node, PercentageBasis const& percentage_basis)
  283. {
  284. add_or_subtract_internal(SumOperation::Subtract, other, layout_node, percentage_basis);
  285. }
  286. void CalculatedStyleValue::CalculationResult::add_or_subtract_internal(SumOperation op, CalculationResult const& other, Layout::Node const* layout_node, PercentageBasis const& percentage_basis)
  287. {
  288. // 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>".
  289. // Though, having the same type may mean that one side is a <dimension> and the other a <percentage>.
  290. // Note: This is almost identical to ::add()
  291. m_value.visit(
  292. [&](Number const& number) {
  293. auto other_number = other.m_value.get<Number>();
  294. if (op == SumOperation::Add) {
  295. m_value = number + other_number;
  296. } else {
  297. m_value = number - other_number;
  298. }
  299. },
  300. [&](Angle const& angle) {
  301. auto this_degrees = angle.to_degrees();
  302. if (other.m_value.has<Angle>()) {
  303. auto other_degrees = other.m_value.get<Angle>().to_degrees();
  304. if (op == SumOperation::Add)
  305. m_value = Angle::make_degrees(this_degrees + other_degrees);
  306. else
  307. m_value = Angle::make_degrees(this_degrees - other_degrees);
  308. } else {
  309. VERIFY(percentage_basis.has<Angle>());
  310. auto other_degrees = percentage_basis.get<Angle>().percentage_of(other.m_value.get<Percentage>()).to_degrees();
  311. if (op == SumOperation::Add)
  312. m_value = Angle::make_degrees(this_degrees + other_degrees);
  313. else
  314. m_value = Angle::make_degrees(this_degrees - other_degrees);
  315. }
  316. },
  317. [&](Frequency const& frequency) {
  318. auto this_hertz = frequency.to_hertz();
  319. if (other.m_value.has<Frequency>()) {
  320. auto other_hertz = other.m_value.get<Frequency>().to_hertz();
  321. if (op == SumOperation::Add)
  322. m_value = Frequency::make_hertz(this_hertz + other_hertz);
  323. else
  324. m_value = Frequency::make_hertz(this_hertz - other_hertz);
  325. } else {
  326. VERIFY(percentage_basis.has<Frequency>());
  327. auto other_hertz = percentage_basis.get<Frequency>().percentage_of(other.m_value.get<Percentage>()).to_hertz();
  328. if (op == SumOperation::Add)
  329. m_value = Frequency::make_hertz(this_hertz + other_hertz);
  330. else
  331. m_value = Frequency::make_hertz(this_hertz - other_hertz);
  332. }
  333. },
  334. [&](Length const& length) {
  335. auto this_px = length.to_px(*layout_node);
  336. if (other.m_value.has<Length>()) {
  337. auto other_px = other.m_value.get<Length>().to_px(*layout_node);
  338. if (op == SumOperation::Add)
  339. m_value = Length::make_px(this_px + other_px);
  340. else
  341. m_value = Length::make_px(this_px - other_px);
  342. } else {
  343. VERIFY(percentage_basis.has<Length>());
  344. auto other_px = percentage_basis.get<Length>().percentage_of(other.m_value.get<Percentage>()).to_px(*layout_node);
  345. if (op == SumOperation::Add)
  346. m_value = Length::make_px(this_px + other_px);
  347. else
  348. m_value = Length::make_px(this_px - other_px);
  349. }
  350. },
  351. [&](Time const& time) {
  352. auto this_seconds = time.to_seconds();
  353. if (other.m_value.has<Time>()) {
  354. auto other_seconds = other.m_value.get<Time>().to_seconds();
  355. if (op == SumOperation::Add)
  356. m_value = Time::make_seconds(this_seconds + other_seconds);
  357. else
  358. m_value = Time::make_seconds(this_seconds - other_seconds);
  359. } else {
  360. VERIFY(percentage_basis.has<Time>());
  361. auto other_seconds = percentage_basis.get<Time>().percentage_of(other.m_value.get<Percentage>()).to_seconds();
  362. if (op == SumOperation::Add)
  363. m_value = Time::make_seconds(this_seconds + other_seconds);
  364. else
  365. m_value = Time::make_seconds(this_seconds - other_seconds);
  366. }
  367. },
  368. [&](Percentage const& percentage) {
  369. if (other.m_value.has<Percentage>()) {
  370. if (op == SumOperation::Add)
  371. m_value = Percentage { percentage.value() + other.m_value.get<Percentage>().value() };
  372. else
  373. m_value = Percentage { percentage.value() - other.m_value.get<Percentage>().value() };
  374. return;
  375. }
  376. // 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`.
  377. CalculationResult new_value = other;
  378. if (op == SumOperation::Add) {
  379. new_value.add(*this, layout_node, percentage_basis);
  380. } else {
  381. // Turn 'this - other' into '-other + this', as 'A + B == B + A', but 'A - B != B - A'
  382. new_value.multiply_by({ Number { Number::Type::Integer, -1.0f } }, layout_node);
  383. new_value.add(*this, layout_node, percentage_basis);
  384. }
  385. *this = new_value;
  386. });
  387. }
  388. void CalculatedStyleValue::CalculationResult::multiply_by(CalculationResult const& other, Layout::Node const* layout_node)
  389. {
  390. // We know from validation when resolving the type, that at least one side must be a <number> or <integer>.
  391. // Both of these are represented as a float.
  392. VERIFY(m_value.has<Number>() || other.m_value.has<Number>());
  393. bool other_is_number = other.m_value.has<Number>();
  394. m_value.visit(
  395. [&](Number const& number) {
  396. if (other_is_number) {
  397. m_value = number * other.m_value.get<Number>();
  398. } else {
  399. // Avoid duplicating all the logic by swapping `this` and `other`.
  400. CalculationResult new_value = other;
  401. new_value.multiply_by(*this, layout_node);
  402. *this = new_value;
  403. }
  404. },
  405. [&](Angle const& angle) {
  406. m_value = Angle::make_degrees(angle.to_degrees() * other.m_value.get<Number>().value());
  407. },
  408. [&](Frequency const& frequency) {
  409. m_value = Frequency::make_hertz(frequency.to_hertz() * other.m_value.get<Number>().value());
  410. },
  411. [&](Length const& length) {
  412. VERIFY(layout_node);
  413. m_value = Length::make_px(length.to_px(*layout_node) * other.m_value.get<Number>().value());
  414. },
  415. [&](Time const& time) {
  416. m_value = Time::make_seconds(time.to_seconds() * other.m_value.get<Number>().value());
  417. },
  418. [&](Percentage const& percentage) {
  419. m_value = Percentage { percentage.value() * other.m_value.get<Number>().value() };
  420. });
  421. }
  422. void CalculatedStyleValue::CalculationResult::divide_by(CalculationResult const& other, Layout::Node const* layout_node)
  423. {
  424. // We know from validation when resolving the type, that `other` must be a <number> or <integer>.
  425. // Both of these are represented as a Number.
  426. auto denominator = other.m_value.get<Number>().value();
  427. // FIXME: Dividing by 0 is invalid, and should be caught during parsing.
  428. VERIFY(denominator != 0.0f);
  429. m_value.visit(
  430. [&](Number const& number) {
  431. m_value = Number {
  432. Number::Type::Number,
  433. number.value() / denominator
  434. };
  435. },
  436. [&](Angle const& angle) {
  437. m_value = Angle::make_degrees(angle.to_degrees() / denominator);
  438. },
  439. [&](Frequency const& frequency) {
  440. m_value = Frequency::make_hertz(frequency.to_hertz() / denominator);
  441. },
  442. [&](Length const& length) {
  443. VERIFY(layout_node);
  444. m_value = Length::make_px(length.to_px(*layout_node) / denominator);
  445. },
  446. [&](Time const& time) {
  447. m_value = Time::make_seconds(time.to_seconds() / denominator);
  448. },
  449. [&](Percentage const& percentage) {
  450. m_value = Percentage { percentage.value() / denominator };
  451. });
  452. }
  453. ErrorOr<String> CalculatedStyleValue::to_string() const
  454. {
  455. return String::formatted("calc({})", TRY(m_expression->to_string()));
  456. }
  457. bool CalculatedStyleValue::equals(StyleValue const& other) const
  458. {
  459. if (type() != other.type())
  460. return false;
  461. // This is a case where comparing the strings actually makes sense.
  462. return to_string().release_value_but_fixme_should_propagate_errors() == other.to_string().release_value_but_fixme_should_propagate_errors();
  463. }
  464. ErrorOr<String> CalculatedStyleValue::CalcNumberValue::to_string() const
  465. {
  466. return value.visit(
  467. [](Number const& number) -> ErrorOr<String> { return String::number(number.value()); },
  468. [](NonnullOwnPtr<CalcNumberSum> const& sum) -> ErrorOr<String> { return String::formatted("({})", TRY(sum->to_string())); });
  469. }
  470. ErrorOr<String> CalculatedStyleValue::CalcValue::to_string() const
  471. {
  472. return value.visit(
  473. [](Number const& number) -> ErrorOr<String> { return String::number(number.value()); },
  474. [](NonnullOwnPtr<CalcSum> const& sum) -> ErrorOr<String> { return String::formatted("({})", TRY(sum->to_string())); },
  475. [](auto const& v) -> ErrorOr<String> { return v.to_string(); });
  476. }
  477. ErrorOr<String> CalculatedStyleValue::CalcSum::to_string() const
  478. {
  479. StringBuilder builder;
  480. TRY(builder.try_append(TRY(first_calc_product->to_string())));
  481. for (auto const& item : zero_or_more_additional_calc_products)
  482. TRY(builder.try_append(TRY(item->to_string())));
  483. return builder.to_string();
  484. }
  485. ErrorOr<String> CalculatedStyleValue::CalcNumberSum::to_string() const
  486. {
  487. StringBuilder builder;
  488. TRY(builder.try_append(TRY(first_calc_number_product->to_string())));
  489. for (auto const& item : zero_or_more_additional_calc_number_products)
  490. TRY(builder.try_append(TRY(item->to_string())));
  491. return builder.to_string();
  492. }
  493. ErrorOr<String> CalculatedStyleValue::CalcProduct::to_string() const
  494. {
  495. StringBuilder builder;
  496. TRY(builder.try_append(TRY(first_calc_value.to_string())));
  497. for (auto const& item : zero_or_more_additional_calc_values)
  498. TRY(builder.try_append(TRY(item->to_string())));
  499. return builder.to_string();
  500. }
  501. ErrorOr<String> CalculatedStyleValue::CalcSumPartWithOperator::to_string() const
  502. {
  503. return String::formatted(" {} {}", op == SumOperation::Add ? "+"sv : "-"sv, TRY(value->to_string()));
  504. }
  505. ErrorOr<String> CalculatedStyleValue::CalcProductPartWithOperator::to_string() const
  506. {
  507. auto value_string = TRY(value.visit(
  508. [](CalcValue const& v) { return v.to_string(); },
  509. [](CalcNumberValue const& v) { return v.to_string(); }));
  510. return String::formatted(" {} {}", op == ProductOperation::Multiply ? "*"sv : "/"sv, value_string);
  511. }
  512. ErrorOr<String> CalculatedStyleValue::CalcNumberProduct::to_string() const
  513. {
  514. StringBuilder builder;
  515. TRY(builder.try_append(TRY(first_calc_number_value.to_string())));
  516. for (auto const& item : zero_or_more_additional_calc_number_values)
  517. TRY(builder.try_append(TRY(item->to_string())));
  518. return builder.to_string();
  519. }
  520. ErrorOr<String> CalculatedStyleValue::CalcNumberProductPartWithOperator::to_string() const
  521. {
  522. return String::formatted(" {} {}", op == ProductOperation::Multiply ? "*"sv : "/"sv, TRY(value.to_string()));
  523. }
  524. ErrorOr<String> CalculatedStyleValue::CalcNumberSumPartWithOperator::to_string() const
  525. {
  526. return String::formatted(" {} {}", op == SumOperation::Add ? "+"sv : "-"sv, TRY(value->to_string()));
  527. }
  528. Optional<Angle> CalculatedStyleValue::resolve_angle() const
  529. {
  530. auto result = m_expression->resolve(nullptr, {});
  531. if (result.value().has<Angle>())
  532. return result.value().get<Angle>();
  533. return {};
  534. }
  535. Optional<Angle> CalculatedStyleValue::resolve_angle_percentage(Angle const& percentage_basis) const
  536. {
  537. auto result = m_expression->resolve(nullptr, percentage_basis);
  538. return result.value().visit(
  539. [&](Angle const& angle) -> Optional<Angle> {
  540. return angle;
  541. },
  542. [&](Percentage const& percentage) -> Optional<Angle> {
  543. return percentage_basis.percentage_of(percentage);
  544. },
  545. [&](auto const&) -> Optional<Angle> {
  546. return {};
  547. });
  548. }
  549. Optional<Frequency> CalculatedStyleValue::resolve_frequency() const
  550. {
  551. auto result = m_expression->resolve(nullptr, {});
  552. if (result.value().has<Frequency>())
  553. return result.value().get<Frequency>();
  554. return {};
  555. }
  556. Optional<Frequency> CalculatedStyleValue::resolve_frequency_percentage(Frequency const& percentage_basis) const
  557. {
  558. auto result = m_expression->resolve(nullptr, percentage_basis);
  559. return result.value().visit(
  560. [&](Frequency const& frequency) -> Optional<Frequency> {
  561. return frequency;
  562. },
  563. [&](Percentage const& percentage) -> Optional<Frequency> {
  564. return percentage_basis.percentage_of(percentage);
  565. },
  566. [&](auto const&) -> Optional<Frequency> {
  567. return {};
  568. });
  569. }
  570. Optional<Length> CalculatedStyleValue::resolve_length(Layout::Node const& layout_node) const
  571. {
  572. auto result = m_expression->resolve(&layout_node, {});
  573. if (result.value().has<Length>())
  574. return result.value().get<Length>();
  575. return {};
  576. }
  577. Optional<Length> CalculatedStyleValue::resolve_length_percentage(Layout::Node const& layout_node, Length const& percentage_basis) const
  578. {
  579. auto result = m_expression->resolve(&layout_node, percentage_basis);
  580. return result.value().visit(
  581. [&](Length const& length) -> Optional<Length> {
  582. return length;
  583. },
  584. [&](Percentage const& percentage) -> Optional<Length> {
  585. return percentage_basis.percentage_of(percentage);
  586. },
  587. [&](auto const&) -> Optional<Length> {
  588. return {};
  589. });
  590. }
  591. Optional<Percentage> CalculatedStyleValue::resolve_percentage() const
  592. {
  593. auto result = m_expression->resolve(nullptr, {});
  594. if (result.value().has<Percentage>())
  595. return result.value().get<Percentage>();
  596. return {};
  597. }
  598. Optional<Time> CalculatedStyleValue::resolve_time() const
  599. {
  600. auto result = m_expression->resolve(nullptr, {});
  601. if (result.value().has<Time>())
  602. return result.value().get<Time>();
  603. return {};
  604. }
  605. Optional<Time> CalculatedStyleValue::resolve_time_percentage(Time const& percentage_basis) const
  606. {
  607. auto result = m_expression->resolve(nullptr, percentage_basis);
  608. return result.value().visit(
  609. [&](Time const& time) -> Optional<Time> {
  610. return time;
  611. },
  612. [&](auto const&) -> Optional<Time> {
  613. return {};
  614. });
  615. }
  616. Optional<float> CalculatedStyleValue::resolve_number()
  617. {
  618. auto result = m_expression->resolve(nullptr, {});
  619. if (result.value().has<Number>())
  620. return result.value().get<Number>().value();
  621. return {};
  622. }
  623. Optional<i64> CalculatedStyleValue::resolve_integer()
  624. {
  625. auto result = m_expression->resolve(nullptr, {});
  626. if (result.value().has<Number>())
  627. return result.value().get<Number>().integer_value();
  628. return {};
  629. }
  630. static bool is_number(CalculatedStyleValue::ResolvedType type)
  631. {
  632. return type == CalculatedStyleValue::ResolvedType::Number || type == CalculatedStyleValue::ResolvedType::Integer;
  633. }
  634. static bool is_dimension(CalculatedStyleValue::ResolvedType type)
  635. {
  636. return type != CalculatedStyleValue::ResolvedType::Number
  637. && type != CalculatedStyleValue::ResolvedType::Integer
  638. && type != CalculatedStyleValue::ResolvedType::Percentage;
  639. }
  640. template<typename SumWithOperator>
  641. static Optional<CalculatedStyleValue::ResolvedType> resolve_sum_type(CalculatedStyleValue::ResolvedType first_type, Vector<NonnullOwnPtr<SumWithOperator>> const& zero_or_more_additional_products)
  642. {
  643. auto type = first_type;
  644. for (auto const& product : zero_or_more_additional_products) {
  645. auto maybe_product_type = product->resolved_type();
  646. if (!maybe_product_type.has_value())
  647. return {};
  648. auto product_type = maybe_product_type.value();
  649. // At + or -, check that both sides have the same type, or that one side is a <number> and the other is an <integer>.
  650. // If both sides are the same type, resolve to that type.
  651. if (product_type == type)
  652. continue;
  653. // If one side is a <number> and the other is an <integer>, resolve to <number>.
  654. if (is_number(type) && is_number(product_type)) {
  655. type = CalculatedStyleValue::ResolvedType::Number;
  656. continue;
  657. }
  658. // FIXME: calc() handles <percentage> by allowing them to pretend to be whatever <dimension> type is allowed at this location.
  659. // Since we can't easily check what that type is, we just allow <percentage> to combine with any other <dimension> type.
  660. if (type == CalculatedStyleValue::ResolvedType::Percentage && is_dimension(product_type)) {
  661. type = product_type;
  662. continue;
  663. }
  664. if (is_dimension(type) && product_type == CalculatedStyleValue::ResolvedType::Percentage)
  665. continue;
  666. return {};
  667. }
  668. return type;
  669. }
  670. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcSum::resolved_type() const
  671. {
  672. auto maybe_type = first_calc_product->resolved_type();
  673. if (!maybe_type.has_value())
  674. return {};
  675. auto type = maybe_type.value();
  676. return resolve_sum_type(type, zero_or_more_additional_calc_products);
  677. }
  678. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcNumberSum::resolved_type() const
  679. {
  680. auto maybe_type = first_calc_number_product->resolved_type();
  681. if (!maybe_type.has_value())
  682. return {};
  683. auto type = maybe_type.value();
  684. return resolve_sum_type(type, zero_or_more_additional_calc_number_products);
  685. }
  686. template<typename ProductWithOperator>
  687. static Optional<CalculatedStyleValue::ResolvedType> resolve_product_type(CalculatedStyleValue::ResolvedType first_type, Vector<NonnullOwnPtr<ProductWithOperator>> const& zero_or_more_additional_values)
  688. {
  689. auto type = first_type;
  690. for (auto const& value : zero_or_more_additional_values) {
  691. auto maybe_value_type = value->resolved_type();
  692. if (!maybe_value_type.has_value())
  693. return {};
  694. auto value_type = maybe_value_type.value();
  695. if (value->op == CalculatedStyleValue::ProductOperation::Multiply) {
  696. // At *, check that at least one side is <number>.
  697. if (!(is_number(type) || is_number(value_type)))
  698. return {};
  699. // If both sides are <integer>, resolve to <integer>.
  700. if (type == CalculatedStyleValue::ResolvedType::Integer && value_type == CalculatedStyleValue::ResolvedType::Integer) {
  701. type = CalculatedStyleValue::ResolvedType::Integer;
  702. } else {
  703. // Otherwise, resolve to the type of the other side.
  704. if (is_number(type))
  705. type = value_type;
  706. }
  707. continue;
  708. } else {
  709. VERIFY(value->op == CalculatedStyleValue::ProductOperation::Divide);
  710. // At /, check that the right side is <number>.
  711. if (!is_number(value_type))
  712. return {};
  713. // If the left side is <integer>, resolve to <number>.
  714. if (type == CalculatedStyleValue::ResolvedType::Integer) {
  715. type = CalculatedStyleValue::ResolvedType::Number;
  716. } else {
  717. // Otherwise, resolve to the type of the left side.
  718. }
  719. // FIXME: Division by zero makes the whole calc() expression invalid.
  720. }
  721. }
  722. return type;
  723. }
  724. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcProduct::resolved_type() const
  725. {
  726. auto maybe_type = first_calc_value.resolved_type();
  727. if (!maybe_type.has_value())
  728. return {};
  729. auto type = maybe_type.value();
  730. return resolve_product_type(type, zero_or_more_additional_calc_values);
  731. }
  732. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcSumPartWithOperator::resolved_type() const
  733. {
  734. return value->resolved_type();
  735. }
  736. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcNumberProduct::resolved_type() const
  737. {
  738. auto maybe_type = first_calc_number_value.resolved_type();
  739. if (!maybe_type.has_value())
  740. return {};
  741. auto type = maybe_type.value();
  742. return resolve_product_type(type, zero_or_more_additional_calc_number_values);
  743. }
  744. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcNumberProductPartWithOperator::resolved_type() const
  745. {
  746. return value.resolved_type();
  747. }
  748. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcNumberSumPartWithOperator::resolved_type() const
  749. {
  750. return value->resolved_type();
  751. }
  752. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcProductPartWithOperator::resolved_type() const
  753. {
  754. return value.visit(
  755. [](CalcValue const& calc_value) {
  756. return calc_value.resolved_type();
  757. },
  758. [](CalcNumberValue const& calc_number_value) {
  759. return calc_number_value.resolved_type();
  760. });
  761. }
  762. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcValue::resolved_type() const
  763. {
  764. return value.visit(
  765. [](Number const& number) -> Optional<CalculatedStyleValue::ResolvedType> {
  766. return { number.is_integer() ? ResolvedType::Integer : ResolvedType::Number };
  767. },
  768. [](Angle const&) -> Optional<CalculatedStyleValue::ResolvedType> { return { ResolvedType::Angle }; },
  769. [](Frequency const&) -> Optional<CalculatedStyleValue::ResolvedType> { return { ResolvedType::Frequency }; },
  770. [](Length const&) -> Optional<CalculatedStyleValue::ResolvedType> { return { ResolvedType::Length }; },
  771. [](Percentage const&) -> Optional<CalculatedStyleValue::ResolvedType> { return { ResolvedType::Percentage }; },
  772. [](Time const&) -> Optional<CalculatedStyleValue::ResolvedType> { return { ResolvedType::Time }; },
  773. [](NonnullOwnPtr<CalcSum> const& sum) { return sum->resolved_type(); });
  774. }
  775. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcNumberValue::resolved_type() const
  776. {
  777. return value.visit(
  778. [](Number const& number) -> Optional<CalculatedStyleValue::ResolvedType> {
  779. return { number.is_integer() ? ResolvedType::Integer : ResolvedType::Number };
  780. },
  781. [](NonnullOwnPtr<CalcNumberSum> const& sum) { return sum->resolved_type(); });
  782. }
  783. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcNumberValue::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  784. {
  785. return value.visit(
  786. [&](Number const& number) -> CalculatedStyleValue::CalculationResult {
  787. return CalculatedStyleValue::CalculationResult { number };
  788. },
  789. [&](NonnullOwnPtr<CalcNumberSum> const& sum) -> CalculatedStyleValue::CalculationResult {
  790. return sum->resolve(layout_node, percentage_basis);
  791. });
  792. }
  793. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcValue::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  794. {
  795. return value.visit(
  796. [&](NonnullOwnPtr<CalcSum> const& sum) -> CalculatedStyleValue::CalculationResult {
  797. return sum->resolve(layout_node, percentage_basis);
  798. },
  799. [&](auto const& v) -> CalculatedStyleValue::CalculationResult {
  800. return CalculatedStyleValue::CalculationResult { v };
  801. });
  802. }
  803. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcSum::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  804. {
  805. auto value = first_calc_product->resolve(layout_node, percentage_basis);
  806. for (auto& additional_product : zero_or_more_additional_calc_products) {
  807. auto additional_value = additional_product->resolve(layout_node, percentage_basis);
  808. if (additional_product->op == CalculatedStyleValue::SumOperation::Add)
  809. value.add(additional_value, layout_node, percentage_basis);
  810. else if (additional_product->op == CalculatedStyleValue::SumOperation::Subtract)
  811. value.subtract(additional_value, layout_node, percentage_basis);
  812. else
  813. VERIFY_NOT_REACHED();
  814. }
  815. return value;
  816. }
  817. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcNumberSum::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  818. {
  819. auto value = first_calc_number_product->resolve(layout_node, percentage_basis);
  820. for (auto& additional_product : zero_or_more_additional_calc_number_products) {
  821. auto additional_value = additional_product->resolve(layout_node, percentage_basis);
  822. if (additional_product->op == CSS::CalculatedStyleValue::SumOperation::Add)
  823. value.add(additional_value, layout_node, percentage_basis);
  824. else if (additional_product->op == CalculatedStyleValue::SumOperation::Subtract)
  825. value.subtract(additional_value, layout_node, percentage_basis);
  826. else
  827. VERIFY_NOT_REACHED();
  828. }
  829. return value;
  830. }
  831. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcProduct::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  832. {
  833. auto value = first_calc_value.resolve(layout_node, percentage_basis);
  834. for (auto& additional_value : zero_or_more_additional_calc_values) {
  835. additional_value->value.visit(
  836. [&](CalculatedStyleValue::CalcValue const& calc_value) {
  837. VERIFY(additional_value->op == CalculatedStyleValue::ProductOperation::Multiply);
  838. auto resolved_value = calc_value.resolve(layout_node, percentage_basis);
  839. value.multiply_by(resolved_value, layout_node);
  840. },
  841. [&](CalculatedStyleValue::CalcNumberValue const& calc_number_value) {
  842. VERIFY(additional_value->op == CalculatedStyleValue::ProductOperation::Divide);
  843. auto resolved_calc_number_value = calc_number_value.resolve(layout_node, percentage_basis);
  844. // FIXME: Checking for division by 0 should happen during parsing.
  845. VERIFY(resolved_calc_number_value.value().get<Number>().value() != 0.0f);
  846. value.divide_by(resolved_calc_number_value, layout_node);
  847. });
  848. }
  849. return value;
  850. }
  851. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcNumberProduct::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  852. {
  853. auto value = first_calc_number_value.resolve(layout_node, percentage_basis);
  854. for (auto& additional_number_value : zero_or_more_additional_calc_number_values) {
  855. auto additional_value = additional_number_value->resolve(layout_node, percentage_basis);
  856. if (additional_number_value->op == CalculatedStyleValue::ProductOperation::Multiply)
  857. value.multiply_by(additional_value, layout_node);
  858. else if (additional_number_value->op == CalculatedStyleValue::ProductOperation::Divide)
  859. value.divide_by(additional_value, layout_node);
  860. else
  861. VERIFY_NOT_REACHED();
  862. }
  863. return value;
  864. }
  865. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcProductPartWithOperator::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  866. {
  867. return value.visit(
  868. [&](CalcValue const& calc_value) {
  869. return calc_value.resolve(layout_node, percentage_basis);
  870. },
  871. [&](CalcNumberValue const& calc_number_value) {
  872. return calc_number_value.resolve(layout_node, percentage_basis);
  873. });
  874. }
  875. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcSumPartWithOperator::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  876. {
  877. return value->resolve(layout_node, percentage_basis);
  878. }
  879. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcNumberProductPartWithOperator::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  880. {
  881. return value.resolve(layout_node, percentage_basis);
  882. }
  883. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcNumberSumPartWithOperator::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  884. {
  885. return value->resolve(layout_node, percentage_basis);
  886. }
  887. Optional<CSS::Length> absolutized_length(CSS::Length const& length, CSSPixelRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, CSSPixels font_size, CSSPixels root_font_size, CSSPixels line_height, CSSPixels root_line_height)
  888. {
  889. if (length.is_px())
  890. return {};
  891. if (length.is_absolute() || length.is_relative()) {
  892. auto px = length.to_px(viewport_rect, font_metrics, font_size, root_font_size, line_height, root_line_height);
  893. return CSS::Length::make_px(px);
  894. }
  895. return {};
  896. }
  897. ValueComparingNonnullRefPtr<StyleValue const> StyleValue::absolutized(CSSPixelRect const&, Gfx::FontPixelMetrics const&, CSSPixels, CSSPixels, CSSPixels, CSSPixels) const
  898. {
  899. return *this;
  900. }
  901. bool CalculatedStyleValue::contains_percentage() const
  902. {
  903. return m_expression->contains_percentage();
  904. }
  905. bool CalculatedStyleValue::CalcSum::contains_percentage() const
  906. {
  907. if (first_calc_product->contains_percentage())
  908. return true;
  909. for (auto& part : zero_or_more_additional_calc_products) {
  910. if (part->contains_percentage())
  911. return true;
  912. }
  913. return false;
  914. }
  915. bool CalculatedStyleValue::CalcSumPartWithOperator::contains_percentage() const
  916. {
  917. return value->contains_percentage();
  918. }
  919. bool CalculatedStyleValue::CalcProduct::contains_percentage() const
  920. {
  921. if (first_calc_value.contains_percentage())
  922. return true;
  923. for (auto& part : zero_or_more_additional_calc_values) {
  924. if (part->contains_percentage())
  925. return true;
  926. }
  927. return false;
  928. }
  929. bool CalculatedStyleValue::CalcProductPartWithOperator::contains_percentage() const
  930. {
  931. return value.visit(
  932. [](CalcValue const& value) { return value.contains_percentage(); },
  933. [](CalcNumberValue const&) { return false; });
  934. }
  935. bool CalculatedStyleValue::CalcValue::contains_percentage() const
  936. {
  937. return value.visit(
  938. [](Percentage const&) { return true; },
  939. [](NonnullOwnPtr<CalcSum> const& sum) { return sum->contains_percentage(); },
  940. [](auto const&) { return false; });
  941. }
  942. bool calculated_style_value_contains_percentage(CalculatedStyleValue const& value)
  943. {
  944. return value.contains_percentage();
  945. }
  946. }