StyleValue.cpp 52 KB

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