StyleValue.cpp 47 KB

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