StyleValue.cpp 46 KB

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