StyleValue.cpp 51 KB

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