StyleValue.cpp 47 KB

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