StyleValue.cpp 48 KB

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