StyleValue.cpp 48 KB

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