StyleValue.cpp 47 KB

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