StyleValue.cpp 50 KB

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