StyleValue.cpp 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099
  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/AngleStyleValue.h>
  14. #include <LibWeb/CSS/StyleValues/BackgroundRepeatStyleValue.h>
  15. #include <LibWeb/CSS/StyleValues/BackgroundSizeStyleValue.h>
  16. #include <LibWeb/CSS/StyleValues/BackgroundStyleValue.h>
  17. #include <LibWeb/CSS/StyleValues/BorderRadiusShorthandStyleValue.h>
  18. #include <LibWeb/CSS/StyleValues/BorderRadiusStyleValue.h>
  19. #include <LibWeb/CSS/StyleValues/BorderStyleValue.h>
  20. #include <LibWeb/CSS/StyleValues/ColorStyleValue.h>
  21. #include <LibWeb/CSS/StyleValues/ContentStyleValue.h>
  22. #include <LibWeb/CSS/StyleValues/FilterValueListStyleValue.h>
  23. #include <LibWeb/DOM/Document.h>
  24. #include <LibWeb/HTML/BrowsingContext.h>
  25. #include <LibWeb/Loader/LoadRequest.h>
  26. #include <LibWeb/Loader/ResourceLoader.h>
  27. #include <LibWeb/Page/Page.h>
  28. #include <LibWeb/Painting/GradientPainting.h>
  29. #include <LibWeb/Platform/Timer.h>
  30. namespace Web::CSS {
  31. StyleValue::StyleValue(Type type)
  32. : m_type(type)
  33. {
  34. }
  35. AbstractImageStyleValue const& StyleValue::as_abstract_image() const
  36. {
  37. VERIFY(is_abstract_image());
  38. return static_cast<AbstractImageStyleValue const&>(*this);
  39. }
  40. AngleStyleValue const& StyleValue::as_angle() const
  41. {
  42. VERIFY(is_angle());
  43. return static_cast<AngleStyleValue const&>(*this);
  44. }
  45. BackgroundStyleValue const& StyleValue::as_background() const
  46. {
  47. VERIFY(is_background());
  48. return static_cast<BackgroundStyleValue const&>(*this);
  49. }
  50. BackgroundRepeatStyleValue const& StyleValue::as_background_repeat() const
  51. {
  52. VERIFY(is_background_repeat());
  53. return static_cast<BackgroundRepeatStyleValue const&>(*this);
  54. }
  55. BackgroundSizeStyleValue const& StyleValue::as_background_size() const
  56. {
  57. VERIFY(is_background_size());
  58. return static_cast<BackgroundSizeStyleValue const&>(*this);
  59. }
  60. BorderStyleValue const& StyleValue::as_border() const
  61. {
  62. VERIFY(is_border());
  63. return static_cast<BorderStyleValue const&>(*this);
  64. }
  65. BorderRadiusStyleValue const& StyleValue::as_border_radius() const
  66. {
  67. VERIFY(is_border_radius());
  68. return static_cast<BorderRadiusStyleValue const&>(*this);
  69. }
  70. BorderRadiusShorthandStyleValue const& StyleValue::as_border_radius_shorthand() const
  71. {
  72. VERIFY(is_border_radius_shorthand());
  73. return static_cast<BorderRadiusShorthandStyleValue const&>(*this);
  74. }
  75. ShadowStyleValue const& StyleValue::as_shadow() const
  76. {
  77. VERIFY(is_shadow());
  78. return static_cast<ShadowStyleValue const&>(*this);
  79. }
  80. CalculatedStyleValue const& StyleValue::as_calculated() const
  81. {
  82. VERIFY(is_calculated());
  83. return static_cast<CalculatedStyleValue const&>(*this);
  84. }
  85. ColorStyleValue const& StyleValue::as_color() const
  86. {
  87. VERIFY(is_color());
  88. return static_cast<ColorStyleValue const&>(*this);
  89. }
  90. ConicGradientStyleValue const& StyleValue::as_conic_gradient() const
  91. {
  92. VERIFY(is_conic_gradient());
  93. return static_cast<ConicGradientStyleValue const&>(*this);
  94. }
  95. ContentStyleValue const& StyleValue::as_content() const
  96. {
  97. VERIFY(is_content());
  98. return static_cast<ContentStyleValue const&>(*this);
  99. }
  100. FilterValueListStyleValue const& StyleValue::as_filter_value_list() const
  101. {
  102. VERIFY(is_filter_value_list());
  103. return static_cast<FilterValueListStyleValue const&>(*this);
  104. }
  105. FlexStyleValue const& StyleValue::as_flex() const
  106. {
  107. VERIFY(is_flex());
  108. return static_cast<FlexStyleValue const&>(*this);
  109. }
  110. FlexFlowStyleValue const& StyleValue::as_flex_flow() const
  111. {
  112. VERIFY(is_flex_flow());
  113. return static_cast<FlexFlowStyleValue const&>(*this);
  114. }
  115. FontStyleValue const& StyleValue::as_font() const
  116. {
  117. VERIFY(is_font());
  118. return static_cast<FontStyleValue const&>(*this);
  119. }
  120. FrequencyStyleValue const& StyleValue::as_frequency() const
  121. {
  122. VERIFY(is_frequency());
  123. return static_cast<FrequencyStyleValue const&>(*this);
  124. }
  125. GridTrackPlacementShorthandStyleValue const& StyleValue::as_grid_track_placement_shorthand() const
  126. {
  127. VERIFY(is_grid_track_placement_shorthand());
  128. return static_cast<GridTrackPlacementShorthandStyleValue const&>(*this);
  129. }
  130. GridAreaShorthandStyleValue const& StyleValue::as_grid_area_shorthand() const
  131. {
  132. VERIFY(is_grid_area_shorthand());
  133. return static_cast<GridAreaShorthandStyleValue const&>(*this);
  134. }
  135. GridTemplateAreaStyleValue const& StyleValue::as_grid_template_area() const
  136. {
  137. VERIFY(is_grid_template_area());
  138. return static_cast<GridTemplateAreaStyleValue const&>(*this);
  139. }
  140. GridTrackPlacementStyleValue const& StyleValue::as_grid_track_placement() const
  141. {
  142. VERIFY(is_grid_track_placement());
  143. return static_cast<GridTrackPlacementStyleValue const&>(*this);
  144. }
  145. IdentifierStyleValue const& StyleValue::as_identifier() const
  146. {
  147. VERIFY(is_identifier());
  148. return static_cast<IdentifierStyleValue const&>(*this);
  149. }
  150. ImageStyleValue const& StyleValue::as_image() const
  151. {
  152. VERIFY(is_image());
  153. return static_cast<ImageStyleValue const&>(*this);
  154. }
  155. InheritStyleValue const& StyleValue::as_inherit() const
  156. {
  157. VERIFY(is_inherit());
  158. return static_cast<InheritStyleValue const&>(*this);
  159. }
  160. InitialStyleValue const& StyleValue::as_initial() const
  161. {
  162. VERIFY(is_initial());
  163. return static_cast<InitialStyleValue const&>(*this);
  164. }
  165. LengthStyleValue const& StyleValue::as_length() const
  166. {
  167. VERIFY(is_length());
  168. return static_cast<LengthStyleValue const&>(*this);
  169. }
  170. GridTrackSizeStyleValue const& StyleValue::as_grid_track_size_list() const
  171. {
  172. VERIFY(is_grid_track_size_list());
  173. return static_cast<GridTrackSizeStyleValue const&>(*this);
  174. }
  175. LinearGradientStyleValue const& StyleValue::as_linear_gradient() const
  176. {
  177. VERIFY(is_linear_gradient());
  178. return static_cast<LinearGradientStyleValue const&>(*this);
  179. }
  180. ListStyleStyleValue const& StyleValue::as_list_style() const
  181. {
  182. VERIFY(is_list_style());
  183. return static_cast<ListStyleStyleValue const&>(*this);
  184. }
  185. NumericStyleValue const& StyleValue::as_numeric() const
  186. {
  187. VERIFY(is_numeric());
  188. return static_cast<NumericStyleValue const&>(*this);
  189. }
  190. OverflowStyleValue const& StyleValue::as_overflow() const
  191. {
  192. VERIFY(is_overflow());
  193. return static_cast<OverflowStyleValue const&>(*this);
  194. }
  195. PercentageStyleValue const& StyleValue::as_percentage() const
  196. {
  197. VERIFY(is_percentage());
  198. return static_cast<PercentageStyleValue const&>(*this);
  199. }
  200. PositionStyleValue const& StyleValue::as_position() const
  201. {
  202. VERIFY(is_position());
  203. return static_cast<PositionStyleValue const&>(*this);
  204. }
  205. RadialGradientStyleValue const& StyleValue::as_radial_gradient() const
  206. {
  207. VERIFY(is_radial_gradient());
  208. return static_cast<RadialGradientStyleValue const&>(*this);
  209. }
  210. RectStyleValue const& StyleValue::as_rect() const
  211. {
  212. VERIFY(is_rect());
  213. return static_cast<RectStyleValue const&>(*this);
  214. }
  215. ResolutionStyleValue const& StyleValue::as_resolution() const
  216. {
  217. VERIFY(is_resolution());
  218. return static_cast<ResolutionStyleValue const&>(*this);
  219. }
  220. StringStyleValue const& StyleValue::as_string() const
  221. {
  222. VERIFY(is_string());
  223. return static_cast<StringStyleValue const&>(*this);
  224. }
  225. TextDecorationStyleValue const& StyleValue::as_text_decoration() const
  226. {
  227. VERIFY(is_text_decoration());
  228. return static_cast<TextDecorationStyleValue const&>(*this);
  229. }
  230. TimeStyleValue const& StyleValue::as_time() const
  231. {
  232. VERIFY(is_time());
  233. return static_cast<TimeStyleValue const&>(*this);
  234. }
  235. TransformationStyleValue const& StyleValue::as_transformation() const
  236. {
  237. VERIFY(is_transformation());
  238. return static_cast<TransformationStyleValue const&>(*this);
  239. }
  240. UnresolvedStyleValue const& StyleValue::as_unresolved() const
  241. {
  242. VERIFY(is_unresolved());
  243. return static_cast<UnresolvedStyleValue const&>(*this);
  244. }
  245. UnsetStyleValue const& StyleValue::as_unset() const
  246. {
  247. VERIFY(is_unset());
  248. return static_cast<UnsetStyleValue const&>(*this);
  249. }
  250. StyleValueList const& StyleValue::as_value_list() const
  251. {
  252. VERIFY(is_value_list());
  253. return static_cast<StyleValueList const&>(*this);
  254. }
  255. void CalculatedStyleValue::CalculationResult::add(CalculationResult const& other, Layout::Node const* layout_node, PercentageBasis const& percentage_basis)
  256. {
  257. add_or_subtract_internal(SumOperation::Add, other, layout_node, percentage_basis);
  258. }
  259. void CalculatedStyleValue::CalculationResult::subtract(CalculationResult const& other, Layout::Node const* layout_node, PercentageBasis const& percentage_basis)
  260. {
  261. add_or_subtract_internal(SumOperation::Subtract, other, layout_node, percentage_basis);
  262. }
  263. void CalculatedStyleValue::CalculationResult::add_or_subtract_internal(SumOperation op, CalculationResult const& other, Layout::Node const* layout_node, PercentageBasis const& percentage_basis)
  264. {
  265. // 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>".
  266. // Though, having the same type may mean that one side is a <dimension> and the other a <percentage>.
  267. // Note: This is almost identical to ::add()
  268. m_value.visit(
  269. [&](Number const& number) {
  270. auto other_number = other.m_value.get<Number>();
  271. if (op == SumOperation::Add) {
  272. m_value = number + other_number;
  273. } else {
  274. m_value = number - other_number;
  275. }
  276. },
  277. [&](Angle const& angle) {
  278. auto this_degrees = angle.to_degrees();
  279. if (other.m_value.has<Angle>()) {
  280. auto other_degrees = other.m_value.get<Angle>().to_degrees();
  281. if (op == SumOperation::Add)
  282. m_value = Angle::make_degrees(this_degrees + other_degrees);
  283. else
  284. m_value = Angle::make_degrees(this_degrees - other_degrees);
  285. } else {
  286. VERIFY(percentage_basis.has<Angle>());
  287. auto other_degrees = percentage_basis.get<Angle>().percentage_of(other.m_value.get<Percentage>()).to_degrees();
  288. if (op == SumOperation::Add)
  289. m_value = Angle::make_degrees(this_degrees + other_degrees);
  290. else
  291. m_value = Angle::make_degrees(this_degrees - other_degrees);
  292. }
  293. },
  294. [&](Frequency const& frequency) {
  295. auto this_hertz = frequency.to_hertz();
  296. if (other.m_value.has<Frequency>()) {
  297. auto other_hertz = other.m_value.get<Frequency>().to_hertz();
  298. if (op == SumOperation::Add)
  299. m_value = Frequency::make_hertz(this_hertz + other_hertz);
  300. else
  301. m_value = Frequency::make_hertz(this_hertz - other_hertz);
  302. } else {
  303. VERIFY(percentage_basis.has<Frequency>());
  304. auto other_hertz = percentage_basis.get<Frequency>().percentage_of(other.m_value.get<Percentage>()).to_hertz();
  305. if (op == SumOperation::Add)
  306. m_value = Frequency::make_hertz(this_hertz + other_hertz);
  307. else
  308. m_value = Frequency::make_hertz(this_hertz - other_hertz);
  309. }
  310. },
  311. [&](Length const& length) {
  312. auto this_px = length.to_px(*layout_node);
  313. if (other.m_value.has<Length>()) {
  314. auto other_px = other.m_value.get<Length>().to_px(*layout_node);
  315. if (op == SumOperation::Add)
  316. m_value = Length::make_px(this_px + other_px);
  317. else
  318. m_value = Length::make_px(this_px - other_px);
  319. } else {
  320. VERIFY(percentage_basis.has<Length>());
  321. auto other_px = percentage_basis.get<Length>().percentage_of(other.m_value.get<Percentage>()).to_px(*layout_node);
  322. if (op == SumOperation::Add)
  323. m_value = Length::make_px(this_px + other_px);
  324. else
  325. m_value = Length::make_px(this_px - other_px);
  326. }
  327. },
  328. [&](Time const& time) {
  329. auto this_seconds = time.to_seconds();
  330. if (other.m_value.has<Time>()) {
  331. auto other_seconds = other.m_value.get<Time>().to_seconds();
  332. if (op == SumOperation::Add)
  333. m_value = Time::make_seconds(this_seconds + other_seconds);
  334. else
  335. m_value = Time::make_seconds(this_seconds - other_seconds);
  336. } else {
  337. VERIFY(percentage_basis.has<Time>());
  338. auto other_seconds = percentage_basis.get<Time>().percentage_of(other.m_value.get<Percentage>()).to_seconds();
  339. if (op == SumOperation::Add)
  340. m_value = Time::make_seconds(this_seconds + other_seconds);
  341. else
  342. m_value = Time::make_seconds(this_seconds - other_seconds);
  343. }
  344. },
  345. [&](Percentage const& percentage) {
  346. if (other.m_value.has<Percentage>()) {
  347. if (op == SumOperation::Add)
  348. m_value = Percentage { percentage.value() + other.m_value.get<Percentage>().value() };
  349. else
  350. m_value = Percentage { percentage.value() - other.m_value.get<Percentage>().value() };
  351. return;
  352. }
  353. // 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`.
  354. CalculationResult new_value = other;
  355. if (op == SumOperation::Add) {
  356. new_value.add(*this, layout_node, percentage_basis);
  357. } else {
  358. // Turn 'this - other' into '-other + this', as 'A + B == B + A', but 'A - B != B - A'
  359. new_value.multiply_by({ Number { Number::Type::Integer, -1.0f } }, layout_node);
  360. new_value.add(*this, layout_node, percentage_basis);
  361. }
  362. *this = new_value;
  363. });
  364. }
  365. void CalculatedStyleValue::CalculationResult::multiply_by(CalculationResult const& other, Layout::Node const* layout_node)
  366. {
  367. // We know from validation when resolving the type, that at least one side must be a <number> or <integer>.
  368. // Both of these are represented as a float.
  369. VERIFY(m_value.has<Number>() || other.m_value.has<Number>());
  370. bool other_is_number = other.m_value.has<Number>();
  371. m_value.visit(
  372. [&](Number const& number) {
  373. if (other_is_number) {
  374. m_value = number * other.m_value.get<Number>();
  375. } else {
  376. // Avoid duplicating all the logic by swapping `this` and `other`.
  377. CalculationResult new_value = other;
  378. new_value.multiply_by(*this, layout_node);
  379. *this = new_value;
  380. }
  381. },
  382. [&](Angle const& angle) {
  383. m_value = Angle::make_degrees(angle.to_degrees() * other.m_value.get<Number>().value());
  384. },
  385. [&](Frequency const& frequency) {
  386. m_value = Frequency::make_hertz(frequency.to_hertz() * other.m_value.get<Number>().value());
  387. },
  388. [&](Length const& length) {
  389. VERIFY(layout_node);
  390. m_value = Length::make_px(length.to_px(*layout_node) * other.m_value.get<Number>().value());
  391. },
  392. [&](Time const& time) {
  393. m_value = Time::make_seconds(time.to_seconds() * other.m_value.get<Number>().value());
  394. },
  395. [&](Percentage const& percentage) {
  396. m_value = Percentage { percentage.value() * other.m_value.get<Number>().value() };
  397. });
  398. }
  399. void CalculatedStyleValue::CalculationResult::divide_by(CalculationResult const& other, Layout::Node const* layout_node)
  400. {
  401. // We know from validation when resolving the type, that `other` must be a <number> or <integer>.
  402. // Both of these are represented as a Number.
  403. auto denominator = other.m_value.get<Number>().value();
  404. // FIXME: Dividing by 0 is invalid, and should be caught during parsing.
  405. VERIFY(denominator != 0.0f);
  406. m_value.visit(
  407. [&](Number const& number) {
  408. m_value = Number {
  409. Number::Type::Number,
  410. number.value() / denominator
  411. };
  412. },
  413. [&](Angle const& angle) {
  414. m_value = Angle::make_degrees(angle.to_degrees() / denominator);
  415. },
  416. [&](Frequency const& frequency) {
  417. m_value = Frequency::make_hertz(frequency.to_hertz() / denominator);
  418. },
  419. [&](Length const& length) {
  420. VERIFY(layout_node);
  421. m_value = Length::make_px(length.to_px(*layout_node) / denominator);
  422. },
  423. [&](Time const& time) {
  424. m_value = Time::make_seconds(time.to_seconds() / denominator);
  425. },
  426. [&](Percentage const& percentage) {
  427. m_value = Percentage { percentage.value() / denominator };
  428. });
  429. }
  430. ErrorOr<String> CalculatedStyleValue::to_string() const
  431. {
  432. return String::formatted("calc({})", TRY(m_expression->to_string()));
  433. }
  434. bool CalculatedStyleValue::equals(StyleValue const& other) const
  435. {
  436. if (type() != other.type())
  437. return false;
  438. // This is a case where comparing the strings actually makes sense.
  439. return to_string().release_value_but_fixme_should_propagate_errors() == other.to_string().release_value_but_fixme_should_propagate_errors();
  440. }
  441. ErrorOr<String> CalculatedStyleValue::CalcNumberValue::to_string() const
  442. {
  443. return value.visit(
  444. [](Number const& number) -> ErrorOr<String> { return String::number(number.value()); },
  445. [](NonnullOwnPtr<CalcNumberSum> const& sum) -> ErrorOr<String> { return String::formatted("({})", TRY(sum->to_string())); });
  446. }
  447. ErrorOr<String> CalculatedStyleValue::CalcValue::to_string() const
  448. {
  449. return value.visit(
  450. [](Number const& number) -> ErrorOr<String> { return String::number(number.value()); },
  451. [](NonnullOwnPtr<CalcSum> const& sum) -> ErrorOr<String> { return String::formatted("({})", TRY(sum->to_string())); },
  452. [](auto const& v) -> ErrorOr<String> { return v.to_string(); });
  453. }
  454. ErrorOr<String> CalculatedStyleValue::CalcSum::to_string() const
  455. {
  456. StringBuilder builder;
  457. TRY(builder.try_append(TRY(first_calc_product->to_string())));
  458. for (auto const& item : zero_or_more_additional_calc_products)
  459. TRY(builder.try_append(TRY(item->to_string())));
  460. return builder.to_string();
  461. }
  462. ErrorOr<String> CalculatedStyleValue::CalcNumberSum::to_string() const
  463. {
  464. StringBuilder builder;
  465. TRY(builder.try_append(TRY(first_calc_number_product->to_string())));
  466. for (auto const& item : zero_or_more_additional_calc_number_products)
  467. TRY(builder.try_append(TRY(item->to_string())));
  468. return builder.to_string();
  469. }
  470. ErrorOr<String> CalculatedStyleValue::CalcProduct::to_string() const
  471. {
  472. StringBuilder builder;
  473. TRY(builder.try_append(TRY(first_calc_value.to_string())));
  474. for (auto const& item : zero_or_more_additional_calc_values)
  475. TRY(builder.try_append(TRY(item->to_string())));
  476. return builder.to_string();
  477. }
  478. ErrorOr<String> CalculatedStyleValue::CalcSumPartWithOperator::to_string() const
  479. {
  480. return String::formatted(" {} {}", op == SumOperation::Add ? "+"sv : "-"sv, TRY(value->to_string()));
  481. }
  482. ErrorOr<String> CalculatedStyleValue::CalcProductPartWithOperator::to_string() const
  483. {
  484. auto value_string = TRY(value.visit(
  485. [](CalcValue const& v) { return v.to_string(); },
  486. [](CalcNumberValue const& v) { return v.to_string(); }));
  487. return String::formatted(" {} {}", op == ProductOperation::Multiply ? "*"sv : "/"sv, value_string);
  488. }
  489. ErrorOr<String> CalculatedStyleValue::CalcNumberProduct::to_string() const
  490. {
  491. StringBuilder builder;
  492. TRY(builder.try_append(TRY(first_calc_number_value.to_string())));
  493. for (auto const& item : zero_or_more_additional_calc_number_values)
  494. TRY(builder.try_append(TRY(item->to_string())));
  495. return builder.to_string();
  496. }
  497. ErrorOr<String> CalculatedStyleValue::CalcNumberProductPartWithOperator::to_string() const
  498. {
  499. return String::formatted(" {} {}", op == ProductOperation::Multiply ? "*"sv : "/"sv, TRY(value.to_string()));
  500. }
  501. ErrorOr<String> CalculatedStyleValue::CalcNumberSumPartWithOperator::to_string() const
  502. {
  503. return String::formatted(" {} {}", op == SumOperation::Add ? "+"sv : "-"sv, TRY(value->to_string()));
  504. }
  505. Optional<Angle> CalculatedStyleValue::resolve_angle() const
  506. {
  507. auto result = m_expression->resolve(nullptr, {});
  508. if (result.value().has<Angle>())
  509. return result.value().get<Angle>();
  510. return {};
  511. }
  512. Optional<Angle> CalculatedStyleValue::resolve_angle_percentage(Angle const& percentage_basis) const
  513. {
  514. auto result = m_expression->resolve(nullptr, percentage_basis);
  515. return result.value().visit(
  516. [&](Angle const& angle) -> Optional<Angle> {
  517. return angle;
  518. },
  519. [&](Percentage const& percentage) -> Optional<Angle> {
  520. return percentage_basis.percentage_of(percentage);
  521. },
  522. [&](auto const&) -> Optional<Angle> {
  523. return {};
  524. });
  525. }
  526. Optional<Frequency> CalculatedStyleValue::resolve_frequency() const
  527. {
  528. auto result = m_expression->resolve(nullptr, {});
  529. if (result.value().has<Frequency>())
  530. return result.value().get<Frequency>();
  531. return {};
  532. }
  533. Optional<Frequency> CalculatedStyleValue::resolve_frequency_percentage(Frequency const& percentage_basis) const
  534. {
  535. auto result = m_expression->resolve(nullptr, percentage_basis);
  536. return result.value().visit(
  537. [&](Frequency const& frequency) -> Optional<Frequency> {
  538. return frequency;
  539. },
  540. [&](Percentage const& percentage) -> Optional<Frequency> {
  541. return percentage_basis.percentage_of(percentage);
  542. },
  543. [&](auto const&) -> Optional<Frequency> {
  544. return {};
  545. });
  546. }
  547. Optional<Length> CalculatedStyleValue::resolve_length(Layout::Node const& layout_node) const
  548. {
  549. auto result = m_expression->resolve(&layout_node, {});
  550. if (result.value().has<Length>())
  551. return result.value().get<Length>();
  552. return {};
  553. }
  554. Optional<Length> CalculatedStyleValue::resolve_length_percentage(Layout::Node const& layout_node, Length const& percentage_basis) const
  555. {
  556. auto result = m_expression->resolve(&layout_node, percentage_basis);
  557. return result.value().visit(
  558. [&](Length const& length) -> Optional<Length> {
  559. return length;
  560. },
  561. [&](Percentage const& percentage) -> Optional<Length> {
  562. return percentage_basis.percentage_of(percentage);
  563. },
  564. [&](auto const&) -> Optional<Length> {
  565. return {};
  566. });
  567. }
  568. Optional<Percentage> CalculatedStyleValue::resolve_percentage() const
  569. {
  570. auto result = m_expression->resolve(nullptr, {});
  571. if (result.value().has<Percentage>())
  572. return result.value().get<Percentage>();
  573. return {};
  574. }
  575. Optional<Time> CalculatedStyleValue::resolve_time() const
  576. {
  577. auto result = m_expression->resolve(nullptr, {});
  578. if (result.value().has<Time>())
  579. return result.value().get<Time>();
  580. return {};
  581. }
  582. Optional<Time> CalculatedStyleValue::resolve_time_percentage(Time const& percentage_basis) const
  583. {
  584. auto result = m_expression->resolve(nullptr, percentage_basis);
  585. return result.value().visit(
  586. [&](Time const& time) -> Optional<Time> {
  587. return time;
  588. },
  589. [&](auto const&) -> Optional<Time> {
  590. return {};
  591. });
  592. }
  593. Optional<float> CalculatedStyleValue::resolve_number()
  594. {
  595. auto result = m_expression->resolve(nullptr, {});
  596. if (result.value().has<Number>())
  597. return result.value().get<Number>().value();
  598. return {};
  599. }
  600. Optional<i64> CalculatedStyleValue::resolve_integer()
  601. {
  602. auto result = m_expression->resolve(nullptr, {});
  603. if (result.value().has<Number>())
  604. return result.value().get<Number>().integer_value();
  605. return {};
  606. }
  607. static bool is_number(CalculatedStyleValue::ResolvedType type)
  608. {
  609. return type == CalculatedStyleValue::ResolvedType::Number || type == CalculatedStyleValue::ResolvedType::Integer;
  610. }
  611. static bool is_dimension(CalculatedStyleValue::ResolvedType type)
  612. {
  613. return type != CalculatedStyleValue::ResolvedType::Number
  614. && type != CalculatedStyleValue::ResolvedType::Integer
  615. && type != CalculatedStyleValue::ResolvedType::Percentage;
  616. }
  617. template<typename SumWithOperator>
  618. static Optional<CalculatedStyleValue::ResolvedType> resolve_sum_type(CalculatedStyleValue::ResolvedType first_type, Vector<NonnullOwnPtr<SumWithOperator>> const& zero_or_more_additional_products)
  619. {
  620. auto type = first_type;
  621. for (auto const& product : zero_or_more_additional_products) {
  622. auto maybe_product_type = product->resolved_type();
  623. if (!maybe_product_type.has_value())
  624. return {};
  625. auto product_type = maybe_product_type.value();
  626. // At + or -, check that both sides have the same type, or that one side is a <number> and the other is an <integer>.
  627. // If both sides are the same type, resolve to that type.
  628. if (product_type == type)
  629. continue;
  630. // If one side is a <number> and the other is an <integer>, resolve to <number>.
  631. if (is_number(type) && is_number(product_type)) {
  632. type = CalculatedStyleValue::ResolvedType::Number;
  633. continue;
  634. }
  635. // FIXME: calc() handles <percentage> by allowing them to pretend to be whatever <dimension> type is allowed at this location.
  636. // Since we can't easily check what that type is, we just allow <percentage> to combine with any other <dimension> type.
  637. if (type == CalculatedStyleValue::ResolvedType::Percentage && is_dimension(product_type)) {
  638. type = product_type;
  639. continue;
  640. }
  641. if (is_dimension(type) && product_type == CalculatedStyleValue::ResolvedType::Percentage)
  642. continue;
  643. return {};
  644. }
  645. return type;
  646. }
  647. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcSum::resolved_type() const
  648. {
  649. auto maybe_type = first_calc_product->resolved_type();
  650. if (!maybe_type.has_value())
  651. return {};
  652. auto type = maybe_type.value();
  653. return resolve_sum_type(type, zero_or_more_additional_calc_products);
  654. }
  655. // https://www.w3.org/TR/CSS2/visufx.html#value-def-shape
  656. Gfx::FloatRect EdgeRect::resolved(Layout::Node const& layout_node, Gfx::FloatRect border_box) const
  657. {
  658. // In CSS 2.1, the only valid <shape> value is: rect(<top>, <right>, <bottom>, <left>) where
  659. // <top> and <bottom> specify offsets from the top border edge of the box, and <right>, and
  660. // <left> specify offsets from the left border edge of the box.
  661. // The value 'auto' means that a given edge of the clipping region will be the same as the edge
  662. // of the element's generated border box (i.e., 'auto' means the same as '0' for <top> and
  663. // <left>, the same as the used value of the height plus the sum of vertical padding and border
  664. // widths for <bottom>, and the same as the used value of the width plus the sum of the
  665. // horizontal padding and border widths for <right>, such that four 'auto' values result in the
  666. // clipping region being the same as the element's border box).
  667. auto left = border_box.left() + (left_edge.is_auto() ? 0 : left_edge.to_px(layout_node)).value();
  668. auto top = border_box.top() + (top_edge.is_auto() ? 0 : top_edge.to_px(layout_node)).value();
  669. auto right = border_box.left() + (right_edge.is_auto() ? border_box.width() : right_edge.to_px(layout_node)).value();
  670. auto bottom = border_box.top() + (bottom_edge.is_auto() ? border_box.height() : bottom_edge.to_px(layout_node)).value();
  671. return Gfx::FloatRect {
  672. left,
  673. top,
  674. right - left,
  675. bottom - top
  676. };
  677. }
  678. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcNumberSum::resolved_type() const
  679. {
  680. auto maybe_type = first_calc_number_product->resolved_type();
  681. if (!maybe_type.has_value())
  682. return {};
  683. auto type = maybe_type.value();
  684. return resolve_sum_type(type, zero_or_more_additional_calc_number_products);
  685. }
  686. template<typename ProductWithOperator>
  687. static Optional<CalculatedStyleValue::ResolvedType> resolve_product_type(CalculatedStyleValue::ResolvedType first_type, Vector<NonnullOwnPtr<ProductWithOperator>> const& zero_or_more_additional_values)
  688. {
  689. auto type = first_type;
  690. for (auto const& value : zero_or_more_additional_values) {
  691. auto maybe_value_type = value->resolved_type();
  692. if (!maybe_value_type.has_value())
  693. return {};
  694. auto value_type = maybe_value_type.value();
  695. if (value->op == CalculatedStyleValue::ProductOperation::Multiply) {
  696. // At *, check that at least one side is <number>.
  697. if (!(is_number(type) || is_number(value_type)))
  698. return {};
  699. // If both sides are <integer>, resolve to <integer>.
  700. if (type == CalculatedStyleValue::ResolvedType::Integer && value_type == CalculatedStyleValue::ResolvedType::Integer) {
  701. type = CalculatedStyleValue::ResolvedType::Integer;
  702. } else {
  703. // Otherwise, resolve to the type of the other side.
  704. if (is_number(type))
  705. type = value_type;
  706. }
  707. continue;
  708. } else {
  709. VERIFY(value->op == CalculatedStyleValue::ProductOperation::Divide);
  710. // At /, check that the right side is <number>.
  711. if (!is_number(value_type))
  712. return {};
  713. // If the left side is <integer>, resolve to <number>.
  714. if (type == CalculatedStyleValue::ResolvedType::Integer) {
  715. type = CalculatedStyleValue::ResolvedType::Number;
  716. } else {
  717. // Otherwise, resolve to the type of the left side.
  718. }
  719. // FIXME: Division by zero makes the whole calc() expression invalid.
  720. }
  721. }
  722. return type;
  723. }
  724. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcProduct::resolved_type() const
  725. {
  726. auto maybe_type = first_calc_value.resolved_type();
  727. if (!maybe_type.has_value())
  728. return {};
  729. auto type = maybe_type.value();
  730. return resolve_product_type(type, zero_or_more_additional_calc_values);
  731. }
  732. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcSumPartWithOperator::resolved_type() const
  733. {
  734. return value->resolved_type();
  735. }
  736. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcNumberProduct::resolved_type() const
  737. {
  738. auto maybe_type = first_calc_number_value.resolved_type();
  739. if (!maybe_type.has_value())
  740. return {};
  741. auto type = maybe_type.value();
  742. return resolve_product_type(type, zero_or_more_additional_calc_number_values);
  743. }
  744. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcNumberProductPartWithOperator::resolved_type() const
  745. {
  746. return value.resolved_type();
  747. }
  748. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcNumberSumPartWithOperator::resolved_type() const
  749. {
  750. return value->resolved_type();
  751. }
  752. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcProductPartWithOperator::resolved_type() const
  753. {
  754. return value.visit(
  755. [](CalcValue const& calc_value) {
  756. return calc_value.resolved_type();
  757. },
  758. [](CalcNumberValue const& calc_number_value) {
  759. return calc_number_value.resolved_type();
  760. });
  761. }
  762. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcValue::resolved_type() const
  763. {
  764. return value.visit(
  765. [](Number const& number) -> Optional<CalculatedStyleValue::ResolvedType> {
  766. return { number.is_integer() ? ResolvedType::Integer : ResolvedType::Number };
  767. },
  768. [](Angle const&) -> Optional<CalculatedStyleValue::ResolvedType> { return { ResolvedType::Angle }; },
  769. [](Frequency const&) -> Optional<CalculatedStyleValue::ResolvedType> { return { ResolvedType::Frequency }; },
  770. [](Length const&) -> Optional<CalculatedStyleValue::ResolvedType> { return { ResolvedType::Length }; },
  771. [](Percentage const&) -> Optional<CalculatedStyleValue::ResolvedType> { return { ResolvedType::Percentage }; },
  772. [](Time const&) -> Optional<CalculatedStyleValue::ResolvedType> { return { ResolvedType::Time }; },
  773. [](NonnullOwnPtr<CalcSum> const& sum) { return sum->resolved_type(); });
  774. }
  775. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcNumberValue::resolved_type() const
  776. {
  777. return value.visit(
  778. [](Number const& number) -> Optional<CalculatedStyleValue::ResolvedType> {
  779. return { number.is_integer() ? ResolvedType::Integer : ResolvedType::Number };
  780. },
  781. [](NonnullOwnPtr<CalcNumberSum> const& sum) { return sum->resolved_type(); });
  782. }
  783. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcNumberValue::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  784. {
  785. return value.visit(
  786. [&](Number const& number) -> CalculatedStyleValue::CalculationResult {
  787. return CalculatedStyleValue::CalculationResult { number };
  788. },
  789. [&](NonnullOwnPtr<CalcNumberSum> const& sum) -> CalculatedStyleValue::CalculationResult {
  790. return sum->resolve(layout_node, percentage_basis);
  791. });
  792. }
  793. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcValue::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  794. {
  795. return value.visit(
  796. [&](NonnullOwnPtr<CalcSum> const& sum) -> CalculatedStyleValue::CalculationResult {
  797. return sum->resolve(layout_node, percentage_basis);
  798. },
  799. [&](auto const& v) -> CalculatedStyleValue::CalculationResult {
  800. return CalculatedStyleValue::CalculationResult { v };
  801. });
  802. }
  803. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcSum::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  804. {
  805. auto value = first_calc_product->resolve(layout_node, percentage_basis);
  806. for (auto& additional_product : zero_or_more_additional_calc_products) {
  807. auto additional_value = additional_product->resolve(layout_node, percentage_basis);
  808. if (additional_product->op == CalculatedStyleValue::SumOperation::Add)
  809. value.add(additional_value, layout_node, percentage_basis);
  810. else if (additional_product->op == CalculatedStyleValue::SumOperation::Subtract)
  811. value.subtract(additional_value, layout_node, percentage_basis);
  812. else
  813. VERIFY_NOT_REACHED();
  814. }
  815. return value;
  816. }
  817. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcNumberSum::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  818. {
  819. auto value = first_calc_number_product->resolve(layout_node, percentage_basis);
  820. for (auto& additional_product : zero_or_more_additional_calc_number_products) {
  821. auto additional_value = additional_product->resolve(layout_node, percentage_basis);
  822. if (additional_product->op == CSS::CalculatedStyleValue::SumOperation::Add)
  823. value.add(additional_value, layout_node, percentage_basis);
  824. else if (additional_product->op == CalculatedStyleValue::SumOperation::Subtract)
  825. value.subtract(additional_value, layout_node, percentage_basis);
  826. else
  827. VERIFY_NOT_REACHED();
  828. }
  829. return value;
  830. }
  831. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcProduct::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  832. {
  833. auto value = first_calc_value.resolve(layout_node, percentage_basis);
  834. for (auto& additional_value : zero_or_more_additional_calc_values) {
  835. additional_value->value.visit(
  836. [&](CalculatedStyleValue::CalcValue const& calc_value) {
  837. VERIFY(additional_value->op == CalculatedStyleValue::ProductOperation::Multiply);
  838. auto resolved_value = calc_value.resolve(layout_node, percentage_basis);
  839. value.multiply_by(resolved_value, layout_node);
  840. },
  841. [&](CalculatedStyleValue::CalcNumberValue const& calc_number_value) {
  842. VERIFY(additional_value->op == CalculatedStyleValue::ProductOperation::Divide);
  843. auto resolved_calc_number_value = calc_number_value.resolve(layout_node, percentage_basis);
  844. // FIXME: Checking for division by 0 should happen during parsing.
  845. VERIFY(resolved_calc_number_value.value().get<Number>().value() != 0.0f);
  846. value.divide_by(resolved_calc_number_value, layout_node);
  847. });
  848. }
  849. return value;
  850. }
  851. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcNumberProduct::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  852. {
  853. auto value = first_calc_number_value.resolve(layout_node, percentage_basis);
  854. for (auto& additional_number_value : zero_or_more_additional_calc_number_values) {
  855. auto additional_value = additional_number_value->resolve(layout_node, percentage_basis);
  856. if (additional_number_value->op == CalculatedStyleValue::ProductOperation::Multiply)
  857. value.multiply_by(additional_value, layout_node);
  858. else if (additional_number_value->op == CalculatedStyleValue::ProductOperation::Divide)
  859. value.divide_by(additional_value, layout_node);
  860. else
  861. VERIFY_NOT_REACHED();
  862. }
  863. return value;
  864. }
  865. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcProductPartWithOperator::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  866. {
  867. return value.visit(
  868. [&](CalcValue const& calc_value) {
  869. return calc_value.resolve(layout_node, percentage_basis);
  870. },
  871. [&](CalcNumberValue const& calc_number_value) {
  872. return calc_number_value.resolve(layout_node, percentage_basis);
  873. });
  874. }
  875. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcSumPartWithOperator::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  876. {
  877. return value->resolve(layout_node, percentage_basis);
  878. }
  879. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcNumberProductPartWithOperator::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  880. {
  881. return value.resolve(layout_node, percentage_basis);
  882. }
  883. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcNumberSumPartWithOperator::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  884. {
  885. return value->resolve(layout_node, percentage_basis);
  886. }
  887. ErrorOr<String> FlexStyleValue::to_string() const
  888. {
  889. return String::formatted("{} {} {}", TRY(m_properties.grow->to_string()), TRY(m_properties.shrink->to_string()), TRY(m_properties.basis->to_string()));
  890. }
  891. ErrorOr<String> FlexFlowStyleValue::to_string() const
  892. {
  893. return String::formatted("{} {}", TRY(m_properties.flex_direction->to_string()), TRY(m_properties.flex_wrap->to_string()));
  894. }
  895. ErrorOr<String> FontStyleValue::to_string() const
  896. {
  897. return String::formatted("{} {} {} / {} {}", TRY(m_properties.font_style->to_string()), TRY(m_properties.font_weight->to_string()), TRY(m_properties.font_size->to_string()), TRY(m_properties.line_height->to_string()), TRY(m_properties.font_families->to_string()));
  898. }
  899. ErrorOr<String> GridTrackPlacementShorthandStyleValue::to_string() const
  900. {
  901. if (m_properties.end->grid_track_placement().is_auto())
  902. return String::formatted("{}", TRY(m_properties.start->grid_track_placement().to_string()));
  903. return String::formatted("{} / {}", TRY(m_properties.start->grid_track_placement().to_string()), TRY(m_properties.end->grid_track_placement().to_string()));
  904. }
  905. ErrorOr<String> GridAreaShorthandStyleValue::to_string() const
  906. {
  907. StringBuilder builder;
  908. if (!m_properties.row_start->as_grid_track_placement().grid_track_placement().is_auto())
  909. TRY(builder.try_appendff("{}", TRY(m_properties.row_start->as_grid_track_placement().grid_track_placement().to_string())));
  910. if (!m_properties.column_start->as_grid_track_placement().grid_track_placement().is_auto())
  911. TRY(builder.try_appendff(" / {}", TRY(m_properties.column_start->as_grid_track_placement().grid_track_placement().to_string())));
  912. if (!m_properties.row_end->as_grid_track_placement().grid_track_placement().is_auto())
  913. TRY(builder.try_appendff(" / {}", TRY(m_properties.row_end->as_grid_track_placement().grid_track_placement().to_string())));
  914. if (!m_properties.column_end->as_grid_track_placement().grid_track_placement().is_auto())
  915. TRY(builder.try_appendff(" / {}", TRY(m_properties.column_end->as_grid_track_placement().grid_track_placement().to_string())));
  916. return builder.to_string();
  917. }
  918. ErrorOr<String> GridTrackPlacementStyleValue::to_string() const
  919. {
  920. return m_grid_track_placement.to_string();
  921. }
  922. ErrorOr<String> GridTemplateAreaStyleValue::to_string() const
  923. {
  924. StringBuilder builder;
  925. for (size_t y = 0; y < m_grid_template_area.size(); ++y) {
  926. for (size_t x = 0; x < m_grid_template_area[y].size(); ++x) {
  927. TRY(builder.try_appendff("{}", m_grid_template_area[y][x]));
  928. if (x < m_grid_template_area[y].size() - 1)
  929. TRY(builder.try_append(" "sv));
  930. }
  931. if (y < m_grid_template_area.size() - 1)
  932. TRY(builder.try_append(", "sv));
  933. }
  934. return builder.to_string();
  935. }
  936. ErrorOr<String> GridTrackSizeStyleValue::to_string() const
  937. {
  938. return m_grid_track_size_list.to_string();
  939. }
  940. ErrorOr<String> IdentifierStyleValue::to_string() const
  941. {
  942. return String::from_utf8(CSS::string_from_value_id(m_id));
  943. }
  944. bool IdentifierStyleValue::has_color() const
  945. {
  946. switch (m_id) {
  947. case ValueID::Currentcolor:
  948. case ValueID::LibwebLink:
  949. case ValueID::LibwebPaletteActiveLink:
  950. case ValueID::LibwebPaletteActiveWindowBorder1:
  951. case ValueID::LibwebPaletteActiveWindowBorder2:
  952. case ValueID::LibwebPaletteActiveWindowTitle:
  953. case ValueID::LibwebPaletteBase:
  954. case ValueID::LibwebPaletteBaseText:
  955. case ValueID::LibwebPaletteButton:
  956. case ValueID::LibwebPaletteButtonText:
  957. case ValueID::LibwebPaletteDesktopBackground:
  958. case ValueID::LibwebPaletteFocusOutline:
  959. case ValueID::LibwebPaletteHighlightWindowBorder1:
  960. case ValueID::LibwebPaletteHighlightWindowBorder2:
  961. case ValueID::LibwebPaletteHighlightWindowTitle:
  962. case ValueID::LibwebPaletteHoverHighlight:
  963. case ValueID::LibwebPaletteInactiveSelection:
  964. case ValueID::LibwebPaletteInactiveSelectionText:
  965. case ValueID::LibwebPaletteInactiveWindowBorder1:
  966. case ValueID::LibwebPaletteInactiveWindowBorder2:
  967. case ValueID::LibwebPaletteInactiveWindowTitle:
  968. case ValueID::LibwebPaletteLink:
  969. case ValueID::LibwebPaletteMenuBase:
  970. case ValueID::LibwebPaletteMenuBaseText:
  971. case ValueID::LibwebPaletteMenuSelection:
  972. case ValueID::LibwebPaletteMenuSelectionText:
  973. case ValueID::LibwebPaletteMenuStripe:
  974. case ValueID::LibwebPaletteMovingWindowBorder1:
  975. case ValueID::LibwebPaletteMovingWindowBorder2:
  976. case ValueID::LibwebPaletteMovingWindowTitle:
  977. case ValueID::LibwebPaletteRubberBandBorder:
  978. case ValueID::LibwebPaletteRubberBandFill:
  979. case ValueID::LibwebPaletteRuler:
  980. case ValueID::LibwebPaletteRulerActiveText:
  981. case ValueID::LibwebPaletteRulerBorder:
  982. case ValueID::LibwebPaletteRulerInactiveText:
  983. case ValueID::LibwebPaletteSelection:
  984. case ValueID::LibwebPaletteSelectionText:
  985. case ValueID::LibwebPaletteSyntaxComment:
  986. case ValueID::LibwebPaletteSyntaxControlKeyword:
  987. case ValueID::LibwebPaletteSyntaxIdentifier:
  988. case ValueID::LibwebPaletteSyntaxKeyword:
  989. case ValueID::LibwebPaletteSyntaxNumber:
  990. case ValueID::LibwebPaletteSyntaxOperator:
  991. case ValueID::LibwebPaletteSyntaxPreprocessorStatement:
  992. case ValueID::LibwebPaletteSyntaxPreprocessorValue:
  993. case ValueID::LibwebPaletteSyntaxPunctuation:
  994. case ValueID::LibwebPaletteSyntaxString:
  995. case ValueID::LibwebPaletteSyntaxType:
  996. case ValueID::LibwebPaletteTextCursor:
  997. case ValueID::LibwebPaletteThreedHighlight:
  998. case ValueID::LibwebPaletteThreedShadow1:
  999. case ValueID::LibwebPaletteThreedShadow2:
  1000. case ValueID::LibwebPaletteVisitedLink:
  1001. case ValueID::LibwebPaletteWindow:
  1002. case ValueID::LibwebPaletteWindowText:
  1003. return true;
  1004. default:
  1005. return false;
  1006. }
  1007. }
  1008. Color IdentifierStyleValue::to_color(Layout::NodeWithStyle const& node) const
  1009. {
  1010. if (id() == CSS::ValueID::Currentcolor) {
  1011. if (!node.has_style())
  1012. return Color::Black;
  1013. return node.computed_values().color();
  1014. }
  1015. auto& document = node.document();
  1016. if (id() == CSS::ValueID::LibwebLink)
  1017. return document.link_color();
  1018. if (!document.page())
  1019. return {};
  1020. auto palette = document.page()->palette();
  1021. switch (id()) {
  1022. case CSS::ValueID::LibwebPaletteDesktopBackground:
  1023. return palette.color(ColorRole::DesktopBackground);
  1024. case CSS::ValueID::LibwebPaletteActiveWindowBorder1:
  1025. return palette.color(ColorRole::ActiveWindowBorder1);
  1026. case CSS::ValueID::LibwebPaletteActiveWindowBorder2:
  1027. return palette.color(ColorRole::ActiveWindowBorder2);
  1028. case CSS::ValueID::LibwebPaletteActiveWindowTitle:
  1029. return palette.color(ColorRole::ActiveWindowTitle);
  1030. case CSS::ValueID::LibwebPaletteInactiveWindowBorder1:
  1031. return palette.color(ColorRole::InactiveWindowBorder1);
  1032. case CSS::ValueID::LibwebPaletteInactiveWindowBorder2:
  1033. return palette.color(ColorRole::InactiveWindowBorder2);
  1034. case CSS::ValueID::LibwebPaletteInactiveWindowTitle:
  1035. return palette.color(ColorRole::InactiveWindowTitle);
  1036. case CSS::ValueID::LibwebPaletteMovingWindowBorder1:
  1037. return palette.color(ColorRole::MovingWindowBorder1);
  1038. case CSS::ValueID::LibwebPaletteMovingWindowBorder2:
  1039. return palette.color(ColorRole::MovingWindowBorder2);
  1040. case CSS::ValueID::LibwebPaletteMovingWindowTitle:
  1041. return palette.color(ColorRole::MovingWindowTitle);
  1042. case CSS::ValueID::LibwebPaletteHighlightWindowBorder1:
  1043. return palette.color(ColorRole::HighlightWindowBorder1);
  1044. case CSS::ValueID::LibwebPaletteHighlightWindowBorder2:
  1045. return palette.color(ColorRole::HighlightWindowBorder2);
  1046. case CSS::ValueID::LibwebPaletteHighlightWindowTitle:
  1047. return palette.color(ColorRole::HighlightWindowTitle);
  1048. case CSS::ValueID::LibwebPaletteMenuStripe:
  1049. return palette.color(ColorRole::MenuStripe);
  1050. case CSS::ValueID::LibwebPaletteMenuBase:
  1051. return palette.color(ColorRole::MenuBase);
  1052. case CSS::ValueID::LibwebPaletteMenuBaseText:
  1053. return palette.color(ColorRole::MenuBaseText);
  1054. case CSS::ValueID::LibwebPaletteMenuSelection:
  1055. return palette.color(ColorRole::MenuSelection);
  1056. case CSS::ValueID::LibwebPaletteMenuSelectionText:
  1057. return palette.color(ColorRole::MenuSelectionText);
  1058. case CSS::ValueID::LibwebPaletteWindow:
  1059. return palette.color(ColorRole::Window);
  1060. case CSS::ValueID::LibwebPaletteWindowText:
  1061. return palette.color(ColorRole::WindowText);
  1062. case CSS::ValueID::LibwebPaletteButton:
  1063. return palette.color(ColorRole::Button);
  1064. case CSS::ValueID::LibwebPaletteButtonText:
  1065. return palette.color(ColorRole::ButtonText);
  1066. case CSS::ValueID::LibwebPaletteBase:
  1067. return palette.color(ColorRole::Base);
  1068. case CSS::ValueID::LibwebPaletteBaseText:
  1069. return palette.color(ColorRole::BaseText);
  1070. case CSS::ValueID::LibwebPaletteThreedHighlight:
  1071. return palette.color(ColorRole::ThreedHighlight);
  1072. case CSS::ValueID::LibwebPaletteThreedShadow1:
  1073. return palette.color(ColorRole::ThreedShadow1);
  1074. case CSS::ValueID::LibwebPaletteThreedShadow2:
  1075. return palette.color(ColorRole::ThreedShadow2);
  1076. case CSS::ValueID::LibwebPaletteHoverHighlight:
  1077. return palette.color(ColorRole::HoverHighlight);
  1078. case CSS::ValueID::LibwebPaletteSelection:
  1079. return palette.color(ColorRole::Selection);
  1080. case CSS::ValueID::LibwebPaletteSelectionText:
  1081. return palette.color(ColorRole::SelectionText);
  1082. case CSS::ValueID::LibwebPaletteInactiveSelection:
  1083. return palette.color(ColorRole::InactiveSelection);
  1084. case CSS::ValueID::LibwebPaletteInactiveSelectionText:
  1085. return palette.color(ColorRole::InactiveSelectionText);
  1086. case CSS::ValueID::LibwebPaletteRubberBandFill:
  1087. return palette.color(ColorRole::RubberBandFill);
  1088. case CSS::ValueID::LibwebPaletteRubberBandBorder:
  1089. return palette.color(ColorRole::RubberBandBorder);
  1090. case CSS::ValueID::LibwebPaletteLink:
  1091. return palette.color(ColorRole::Link);
  1092. case CSS::ValueID::LibwebPaletteActiveLink:
  1093. return palette.color(ColorRole::ActiveLink);
  1094. case CSS::ValueID::LibwebPaletteVisitedLink:
  1095. return palette.color(ColorRole::VisitedLink);
  1096. case CSS::ValueID::LibwebPaletteRuler:
  1097. return palette.color(ColorRole::Ruler);
  1098. case CSS::ValueID::LibwebPaletteRulerBorder:
  1099. return palette.color(ColorRole::RulerBorder);
  1100. case CSS::ValueID::LibwebPaletteRulerActiveText:
  1101. return palette.color(ColorRole::RulerActiveText);
  1102. case CSS::ValueID::LibwebPaletteRulerInactiveText:
  1103. return palette.color(ColorRole::RulerInactiveText);
  1104. case CSS::ValueID::LibwebPaletteTextCursor:
  1105. return palette.color(ColorRole::TextCursor);
  1106. case CSS::ValueID::LibwebPaletteFocusOutline:
  1107. return palette.color(ColorRole::FocusOutline);
  1108. case CSS::ValueID::LibwebPaletteSyntaxComment:
  1109. return palette.color(ColorRole::SyntaxComment);
  1110. case CSS::ValueID::LibwebPaletteSyntaxNumber:
  1111. return palette.color(ColorRole::SyntaxNumber);
  1112. case CSS::ValueID::LibwebPaletteSyntaxString:
  1113. return palette.color(ColorRole::SyntaxString);
  1114. case CSS::ValueID::LibwebPaletteSyntaxType:
  1115. return palette.color(ColorRole::SyntaxType);
  1116. case CSS::ValueID::LibwebPaletteSyntaxPunctuation:
  1117. return palette.color(ColorRole::SyntaxPunctuation);
  1118. case CSS::ValueID::LibwebPaletteSyntaxOperator:
  1119. return palette.color(ColorRole::SyntaxOperator);
  1120. case CSS::ValueID::LibwebPaletteSyntaxKeyword:
  1121. return palette.color(ColorRole::SyntaxKeyword);
  1122. case CSS::ValueID::LibwebPaletteSyntaxControlKeyword:
  1123. return palette.color(ColorRole::SyntaxControlKeyword);
  1124. case CSS::ValueID::LibwebPaletteSyntaxIdentifier:
  1125. return palette.color(ColorRole::SyntaxIdentifier);
  1126. case CSS::ValueID::LibwebPaletteSyntaxPreprocessorStatement:
  1127. return palette.color(ColorRole::SyntaxPreprocessorStatement);
  1128. case CSS::ValueID::LibwebPaletteSyntaxPreprocessorValue:
  1129. return palette.color(ColorRole::SyntaxPreprocessorValue);
  1130. default:
  1131. return {};
  1132. }
  1133. }
  1134. ImageStyleValue::ImageStyleValue(AK::URL const& url)
  1135. : AbstractImageStyleValue(Type::Image)
  1136. , m_url(url)
  1137. {
  1138. }
  1139. void ImageStyleValue::load_any_resources(DOM::Document& document)
  1140. {
  1141. if (resource())
  1142. return;
  1143. m_document = &document;
  1144. auto request = LoadRequest::create_for_url_on_page(m_url, document.page());
  1145. set_resource(ResourceLoader::the().load_resource(Resource::Type::Image, request));
  1146. }
  1147. void ImageStyleValue::resource_did_load()
  1148. {
  1149. if (!m_document)
  1150. return;
  1151. // FIXME: Do less than a full repaint if possible?
  1152. if (m_document && m_document->browsing_context())
  1153. m_document->browsing_context()->set_needs_display();
  1154. if (resource()->is_animated() && resource()->frame_count() > 1) {
  1155. m_timer = Platform::Timer::create();
  1156. m_timer->set_interval(resource()->frame_duration(0));
  1157. m_timer->on_timeout = [this] { animate(); };
  1158. m_timer->start();
  1159. }
  1160. }
  1161. void ImageStyleValue::animate()
  1162. {
  1163. m_current_frame_index = (m_current_frame_index + 1) % resource()->frame_count();
  1164. auto current_frame_duration = resource()->frame_duration(m_current_frame_index);
  1165. if (current_frame_duration != m_timer->interval())
  1166. m_timer->restart(current_frame_duration);
  1167. if (m_current_frame_index == resource()->frame_count() - 1) {
  1168. ++m_loops_completed;
  1169. if (m_loops_completed > 0 && m_loops_completed == resource()->loop_count())
  1170. m_timer->stop();
  1171. }
  1172. if (on_animate)
  1173. on_animate();
  1174. }
  1175. Gfx::Bitmap const* ImageStyleValue::bitmap(size_t frame_index) const
  1176. {
  1177. if (!resource())
  1178. return nullptr;
  1179. return resource()->bitmap(frame_index);
  1180. }
  1181. ErrorOr<String> ImageStyleValue::to_string() const
  1182. {
  1183. return serialize_a_url(m_url.to_deprecated_string());
  1184. }
  1185. bool ImageStyleValue::equals(StyleValue const& other) const
  1186. {
  1187. if (type() != other.type())
  1188. return false;
  1189. return m_url == other.as_image().m_url;
  1190. }
  1191. Optional<CSSPixels> ImageStyleValue::natural_width() const
  1192. {
  1193. if (auto* b = bitmap(0); b != nullptr)
  1194. return b->width();
  1195. return {};
  1196. }
  1197. Optional<CSSPixels> ImageStyleValue::natural_height() const
  1198. {
  1199. if (auto* b = bitmap(0); b != nullptr)
  1200. return b->height();
  1201. return {};
  1202. }
  1203. void ImageStyleValue::paint(PaintContext& context, DevicePixelRect const& dest_rect, CSS::ImageRendering image_rendering) const
  1204. {
  1205. if (auto* b = bitmap(m_current_frame_index); b != nullptr)
  1206. context.painter().draw_scaled_bitmap(dest_rect.to_type<int>(), *b, bitmap(0)->rect(), 1.0f, to_gfx_scaling_mode(image_rendering));
  1207. }
  1208. static ErrorOr<void> serialize_color_stop_list(StringBuilder& builder, auto const& color_stop_list)
  1209. {
  1210. bool first = true;
  1211. for (auto const& element : color_stop_list) {
  1212. if (!first)
  1213. TRY(builder.try_append(", "sv));
  1214. if (element.transition_hint.has_value())
  1215. TRY(builder.try_appendff("{}, "sv, TRY(element.transition_hint->value.to_string())));
  1216. TRY(serialize_a_srgb_value(builder, element.color_stop.color));
  1217. for (auto position : Array { &element.color_stop.position, &element.color_stop.second_position }) {
  1218. if (position->has_value())
  1219. TRY(builder.try_appendff(" {}"sv, TRY((*position)->to_string())));
  1220. }
  1221. first = false;
  1222. }
  1223. return {};
  1224. }
  1225. ErrorOr<String> LinearGradientStyleValue::to_string() const
  1226. {
  1227. StringBuilder builder;
  1228. auto side_or_corner_to_string = [](SideOrCorner value) {
  1229. switch (value) {
  1230. case SideOrCorner::Top:
  1231. return "top"sv;
  1232. case SideOrCorner::Bottom:
  1233. return "bottom"sv;
  1234. case SideOrCorner::Left:
  1235. return "left"sv;
  1236. case SideOrCorner::Right:
  1237. return "right"sv;
  1238. case SideOrCorner::TopLeft:
  1239. return "top left"sv;
  1240. case SideOrCorner::TopRight:
  1241. return "top right"sv;
  1242. case SideOrCorner::BottomLeft:
  1243. return "bottom left"sv;
  1244. case SideOrCorner::BottomRight:
  1245. return "bottom right"sv;
  1246. default:
  1247. VERIFY_NOT_REACHED();
  1248. }
  1249. };
  1250. if (m_properties.gradient_type == GradientType::WebKit)
  1251. TRY(builder.try_append("-webkit-"sv));
  1252. if (is_repeating())
  1253. TRY(builder.try_append("repeating-"sv));
  1254. TRY(builder.try_append("linear-gradient("sv));
  1255. TRY(m_properties.direction.visit(
  1256. [&](SideOrCorner side_or_corner) -> ErrorOr<void> {
  1257. return builder.try_appendff("{}{}, "sv, m_properties.gradient_type == GradientType::Standard ? "to "sv : ""sv, side_or_corner_to_string(side_or_corner));
  1258. },
  1259. [&](Angle const& angle) -> ErrorOr<void> {
  1260. return builder.try_appendff("{}, "sv, TRY(angle.to_string()));
  1261. }));
  1262. TRY(serialize_color_stop_list(builder, m_properties.color_stop_list));
  1263. TRY(builder.try_append(")"sv));
  1264. return builder.to_string();
  1265. }
  1266. bool LinearGradientStyleValue::equals(StyleValue const& other_) const
  1267. {
  1268. if (type() != other_.type())
  1269. return false;
  1270. auto& other = other_.as_linear_gradient();
  1271. return m_properties == other.m_properties;
  1272. }
  1273. float LinearGradientStyleValue::angle_degrees(CSSPixelSize gradient_size) const
  1274. {
  1275. auto corner_angle_degrees = [&] {
  1276. return static_cast<float>(atan2(gradient_size.height().value(), gradient_size.width().value())) * 180 / AK::Pi<float>;
  1277. };
  1278. return m_properties.direction.visit(
  1279. [&](SideOrCorner side_or_corner) {
  1280. auto angle = [&] {
  1281. switch (side_or_corner) {
  1282. case SideOrCorner::Top:
  1283. return 0.0f;
  1284. case SideOrCorner::Bottom:
  1285. return 180.0f;
  1286. case SideOrCorner::Left:
  1287. return 270.0f;
  1288. case SideOrCorner::Right:
  1289. return 90.0f;
  1290. case SideOrCorner::TopRight:
  1291. return corner_angle_degrees();
  1292. case SideOrCorner::BottomLeft:
  1293. return corner_angle_degrees() + 180.0f;
  1294. case SideOrCorner::TopLeft:
  1295. return -corner_angle_degrees();
  1296. case SideOrCorner::BottomRight:
  1297. return -(corner_angle_degrees() + 180.0f);
  1298. default:
  1299. VERIFY_NOT_REACHED();
  1300. }
  1301. }();
  1302. // Note: For unknowable reasons the angles are opposite on the -webkit- version
  1303. if (m_properties.gradient_type == GradientType::WebKit)
  1304. return angle + 180.0f;
  1305. return angle;
  1306. },
  1307. [&](Angle const& angle) {
  1308. return angle.to_degrees();
  1309. });
  1310. }
  1311. void LinearGradientStyleValue::resolve_for_size(Layout::Node const& node, CSSPixelSize size) const
  1312. {
  1313. if (m_resolved.has_value() && m_resolved->size == size)
  1314. return;
  1315. m_resolved = ResolvedData { Painting::resolve_linear_gradient_data(node, size, *this), size };
  1316. }
  1317. void LinearGradientStyleValue::paint(PaintContext& context, DevicePixelRect const& dest_rect, CSS::ImageRendering) const
  1318. {
  1319. VERIFY(m_resolved.has_value());
  1320. Painting::paint_linear_gradient(context, dest_rect, m_resolved->data);
  1321. }
  1322. CSSPixelPoint PositionValue::resolved(Layout::Node const& node, CSSPixelRect const& rect) const
  1323. {
  1324. // Note: A preset + a none default x/y_relative_to is impossible in the syntax (and makes little sense)
  1325. CSSPixels x = horizontal_position.visit(
  1326. [&](HorizontalPreset preset) -> CSSPixels {
  1327. return rect.width() * [&] {
  1328. switch (preset) {
  1329. case HorizontalPreset::Left:
  1330. return 0.0f;
  1331. case HorizontalPreset::Center:
  1332. return 0.5f;
  1333. case HorizontalPreset::Right:
  1334. return 1.0f;
  1335. default:
  1336. VERIFY_NOT_REACHED();
  1337. }
  1338. }();
  1339. },
  1340. [&](LengthPercentage length_percentage) -> CSSPixels {
  1341. return length_percentage.resolved(node, Length::make_px(rect.width())).to_px(node);
  1342. });
  1343. CSSPixels y = vertical_position.visit(
  1344. [&](VerticalPreset preset) -> CSSPixels {
  1345. return rect.height() * [&] {
  1346. switch (preset) {
  1347. case VerticalPreset::Top:
  1348. return 0.0f;
  1349. case VerticalPreset::Center:
  1350. return 0.5f;
  1351. case VerticalPreset::Bottom:
  1352. return 1.0f;
  1353. default:
  1354. VERIFY_NOT_REACHED();
  1355. }
  1356. }();
  1357. },
  1358. [&](LengthPercentage length_percentage) -> CSSPixels {
  1359. return length_percentage.resolved(node, Length::make_px(rect.height())).to_px(node);
  1360. });
  1361. if (x_relative_to == HorizontalEdge::Right)
  1362. x = rect.width() - x;
  1363. if (y_relative_to == VerticalEdge::Bottom)
  1364. y = rect.height() - y;
  1365. return CSSPixelPoint { rect.x() + x, rect.y() + y };
  1366. }
  1367. ErrorOr<void> PositionValue::serialize(StringBuilder& builder) const
  1368. {
  1369. // Note: This means our serialization with simplify any with explicit edges that are just `top left`.
  1370. bool has_relative_edges = x_relative_to == HorizontalEdge::Right || y_relative_to == VerticalEdge::Bottom;
  1371. if (has_relative_edges)
  1372. TRY(builder.try_append(x_relative_to == HorizontalEdge::Left ? "left "sv : "right "sv));
  1373. TRY(horizontal_position.visit(
  1374. [&](HorizontalPreset preset) -> ErrorOr<void> {
  1375. return builder.try_append([&] {
  1376. switch (preset) {
  1377. case HorizontalPreset::Left:
  1378. return "left"sv;
  1379. case HorizontalPreset::Center:
  1380. return "center"sv;
  1381. case HorizontalPreset::Right:
  1382. return "right"sv;
  1383. default:
  1384. VERIFY_NOT_REACHED();
  1385. }
  1386. }());
  1387. },
  1388. [&](LengthPercentage length_percentage) -> ErrorOr<void> {
  1389. return builder.try_appendff(TRY(length_percentage.to_string()));
  1390. }));
  1391. TRY(builder.try_append(' '));
  1392. if (has_relative_edges)
  1393. TRY(builder.try_append(y_relative_to == VerticalEdge::Top ? "top "sv : "bottom "sv));
  1394. TRY(vertical_position.visit(
  1395. [&](VerticalPreset preset) -> ErrorOr<void> {
  1396. return builder.try_append([&] {
  1397. switch (preset) {
  1398. case VerticalPreset::Top:
  1399. return "top"sv;
  1400. case VerticalPreset::Center:
  1401. return "center"sv;
  1402. case VerticalPreset::Bottom:
  1403. return "bottom"sv;
  1404. default:
  1405. VERIFY_NOT_REACHED();
  1406. }
  1407. }());
  1408. },
  1409. [&](LengthPercentage length_percentage) -> ErrorOr<void> {
  1410. return builder.try_append(TRY(length_percentage.to_string()));
  1411. }));
  1412. return {};
  1413. }
  1414. ErrorOr<String> RadialGradientStyleValue::to_string() const
  1415. {
  1416. StringBuilder builder;
  1417. if (is_repeating())
  1418. TRY(builder.try_append("repeating-"sv));
  1419. TRY(builder.try_appendff("radial-gradient({} "sv,
  1420. m_properties.ending_shape == EndingShape::Circle ? "circle"sv : "ellipse"sv));
  1421. TRY(m_properties.size.visit(
  1422. [&](Extent extent) -> ErrorOr<void> {
  1423. return builder.try_append([&] {
  1424. switch (extent) {
  1425. case Extent::ClosestCorner:
  1426. return "closest-corner"sv;
  1427. case Extent::ClosestSide:
  1428. return "closest-side"sv;
  1429. case Extent::FarthestCorner:
  1430. return "farthest-corner"sv;
  1431. case Extent::FarthestSide:
  1432. return "farthest-side"sv;
  1433. default:
  1434. VERIFY_NOT_REACHED();
  1435. }
  1436. }());
  1437. },
  1438. [&](CircleSize const& circle_size) -> ErrorOr<void> {
  1439. return builder.try_append(TRY(circle_size.radius.to_string()));
  1440. },
  1441. [&](EllipseSize const& ellipse_size) -> ErrorOr<void> {
  1442. return builder.try_appendff("{} {}", TRY(ellipse_size.radius_a.to_string()), TRY(ellipse_size.radius_b.to_string()));
  1443. }));
  1444. if (m_properties.position != PositionValue::center()) {
  1445. TRY(builder.try_appendff(" at "sv));
  1446. TRY(m_properties.position.serialize(builder));
  1447. }
  1448. TRY(builder.try_append(", "sv));
  1449. TRY(serialize_color_stop_list(builder, m_properties.color_stop_list));
  1450. TRY(builder.try_append(')'));
  1451. return builder.to_string();
  1452. }
  1453. Gfx::FloatSize RadialGradientStyleValue::resolve_size(Layout::Node const& node, Gfx::FloatPoint center, Gfx::FloatRect const& size) const
  1454. {
  1455. auto const side_shape = [&](auto distance_function) {
  1456. auto const distance_from = [&](float v, float a, float b, auto distance_function) {
  1457. return distance_function(fabs(a - v), fabs(b - v));
  1458. };
  1459. auto x_dist = distance_from(center.x(), size.left(), size.right(), distance_function);
  1460. auto y_dist = distance_from(center.y(), size.top(), size.bottom(), distance_function);
  1461. if (m_properties.ending_shape == EndingShape::Circle) {
  1462. auto dist = distance_function(x_dist, y_dist);
  1463. return Gfx::FloatSize { dist, dist };
  1464. } else {
  1465. return Gfx::FloatSize { x_dist, y_dist };
  1466. }
  1467. };
  1468. auto const closest_side_shape = [&] {
  1469. return side_shape(AK::min<float>);
  1470. };
  1471. auto const farthest_side_shape = [&] {
  1472. return side_shape(AK::max<float>);
  1473. };
  1474. auto const corner_distance = [&](auto distance_compare, Gfx::FloatPoint& corner) {
  1475. auto top_left_distance = size.top_left().distance_from(center);
  1476. auto top_right_distance = size.top_right().distance_from(center);
  1477. auto bottom_right_distance = size.bottom_right().distance_from(center);
  1478. auto bottom_left_distance = size.bottom_left().distance_from(center);
  1479. auto distance = top_left_distance;
  1480. if (distance_compare(top_right_distance, distance)) {
  1481. corner = size.top_right();
  1482. distance = top_right_distance;
  1483. }
  1484. if (distance_compare(bottom_right_distance, distance)) {
  1485. corner = size.top_right();
  1486. distance = bottom_right_distance;
  1487. }
  1488. if (distance_compare(bottom_left_distance, distance)) {
  1489. corner = size.top_right();
  1490. distance = bottom_left_distance;
  1491. }
  1492. return distance;
  1493. };
  1494. auto const closest_corner_distance = [&](Gfx::FloatPoint& corner) {
  1495. return corner_distance([](float a, float b) { return a < b; }, corner);
  1496. };
  1497. auto const farthest_corner_distance = [&](Gfx::FloatPoint& corner) {
  1498. return corner_distance([](float a, float b) { return a > b; }, corner);
  1499. };
  1500. auto const corner_shape = [&](auto corner_distance, auto get_shape) {
  1501. Gfx::FloatPoint corner {};
  1502. auto distance = corner_distance(corner);
  1503. if (m_properties.ending_shape == EndingShape::Ellipse) {
  1504. auto shape = get_shape();
  1505. auto aspect_ratio = shape.width() / shape.height();
  1506. auto p = corner - center;
  1507. auto radius_a = AK::sqrt(p.y() * p.y() * aspect_ratio * aspect_ratio + p.x() * p.x());
  1508. auto radius_b = radius_a / aspect_ratio;
  1509. return Gfx::FloatSize { radius_a, radius_b };
  1510. }
  1511. return Gfx::FloatSize { distance, distance };
  1512. };
  1513. // https://w3c.github.io/csswg-drafts/css-images/#radial-gradient-syntax
  1514. auto resolved_size = m_properties.size.visit(
  1515. [&](Extent extent) {
  1516. switch (extent) {
  1517. case Extent::ClosestSide:
  1518. // The ending shape is sized so that it exactly meets the side of the gradient box closest to the gradient’s center.
  1519. // If the shape is an ellipse, it exactly meets the closest side in each dimension.
  1520. return closest_side_shape();
  1521. case Extent::ClosestCorner:
  1522. // The ending shape is sized so that it passes through the corner of the gradient box closest to the gradient’s center.
  1523. // If the shape is an ellipse, the ending shape is given the same aspect-ratio it would have if closest-side were specified
  1524. return corner_shape(closest_corner_distance, closest_side_shape);
  1525. case Extent::FarthestCorner:
  1526. // Same as closest-corner, except the ending shape is sized based on the farthest corner.
  1527. // If the shape is an ellipse, the ending shape is given the same aspect ratio it would have if farthest-side were specified.
  1528. return corner_shape(farthest_corner_distance, farthest_side_shape);
  1529. case Extent::FarthestSide:
  1530. // Same as closest-side, except the ending shape is sized based on the farthest side(s).
  1531. return farthest_side_shape();
  1532. default:
  1533. VERIFY_NOT_REACHED();
  1534. }
  1535. },
  1536. [&](CircleSize const& circle_size) {
  1537. auto radius = circle_size.radius.to_px(node);
  1538. return Gfx::FloatSize { radius, radius };
  1539. },
  1540. [&](EllipseSize const& ellipse_size) {
  1541. auto radius_a = ellipse_size.radius_a.resolved(node, CSS::Length::make_px(size.width())).to_px(node);
  1542. auto radius_b = ellipse_size.radius_b.resolved(node, CSS::Length::make_px(size.height())).to_px(node);
  1543. return Gfx::FloatSize { radius_a, radius_b };
  1544. });
  1545. // Handle degenerate cases
  1546. // https://w3c.github.io/csswg-drafts/css-images/#degenerate-radials
  1547. constexpr auto arbitrary_small_number = 1e-10;
  1548. constexpr auto arbitrary_large_number = 1e10;
  1549. // If the ending shape is a circle with zero radius:
  1550. if (m_properties.ending_shape == EndingShape::Circle && resolved_size.is_empty()) {
  1551. // Render as if the ending shape was a circle whose radius was an arbitrary very small number greater than zero.
  1552. // This will make the gradient continue to look like a circle.
  1553. return Gfx::FloatSize { arbitrary_small_number, arbitrary_small_number };
  1554. }
  1555. // If the ending shape has zero width (regardless of the height):
  1556. if (resolved_size.width() <= 0) {
  1557. // Render as if the ending shape was an ellipse whose height was an arbitrary very large number
  1558. // and whose width was an arbitrary very small number greater than zero.
  1559. // This will make the gradient look similar to a horizontal linear gradient that is mirrored across the center of the ellipse.
  1560. // It also means that all color-stop positions specified with a percentage resolve to 0px.
  1561. return Gfx::FloatSize { arbitrary_small_number, arbitrary_large_number };
  1562. }
  1563. // Otherwise, if the ending shape has zero height:
  1564. if (resolved_size.height() <= 0) {
  1565. // Render as if the ending shape was an ellipse whose width was an arbitrary very large number and whose height
  1566. // was an arbitrary very small number greater than zero. This will make the gradient look like a solid-color image equal
  1567. // to the color of the last color-stop, or equal to the average color of the gradient if it’s repeating.
  1568. return Gfx::FloatSize { arbitrary_large_number, arbitrary_small_number };
  1569. }
  1570. return resolved_size;
  1571. }
  1572. void RadialGradientStyleValue::resolve_for_size(Layout::Node const& node, CSSPixelSize paint_size) const
  1573. {
  1574. CSSPixelRect gradient_box { { 0, 0 }, paint_size };
  1575. auto center = m_properties.position.resolved(node, gradient_box).to_type<float>();
  1576. auto gradient_size = resolve_size(node, center, gradient_box.to_type<float>());
  1577. if (m_resolved.has_value() && m_resolved->gradient_size == gradient_size)
  1578. return;
  1579. m_resolved = ResolvedData {
  1580. Painting::resolve_radial_gradient_data(node, gradient_size.to_type<CSSPixels>(), *this),
  1581. gradient_size,
  1582. center,
  1583. };
  1584. }
  1585. bool RadialGradientStyleValue::equals(StyleValue const& other) const
  1586. {
  1587. if (type() != other.type())
  1588. return false;
  1589. auto& other_gradient = other.as_radial_gradient();
  1590. return m_properties == other_gradient.m_properties;
  1591. }
  1592. void RadialGradientStyleValue::paint(PaintContext& context, DevicePixelRect const& dest_rect, CSS::ImageRendering) const
  1593. {
  1594. VERIFY(m_resolved.has_value());
  1595. Painting::paint_radial_gradient(context, dest_rect, m_resolved->data,
  1596. context.rounded_device_point(m_resolved->center.to_type<CSSPixels>()),
  1597. context.rounded_device_size(m_resolved->gradient_size.to_type<CSSPixels>()));
  1598. }
  1599. ErrorOr<String> ConicGradientStyleValue::to_string() const
  1600. {
  1601. StringBuilder builder;
  1602. if (is_repeating())
  1603. TRY(builder.try_append("repeating-"sv));
  1604. TRY(builder.try_append("conic-gradient("sv));
  1605. bool has_from_angle = false;
  1606. bool has_at_position = false;
  1607. if ((has_from_angle = m_properties.from_angle.to_degrees() != 0))
  1608. TRY(builder.try_appendff("from {}", TRY(m_properties.from_angle.to_string())));
  1609. if ((has_at_position = m_properties.position != PositionValue::center())) {
  1610. if (has_from_angle)
  1611. TRY(builder.try_append(' '));
  1612. TRY(builder.try_appendff("at "sv));
  1613. TRY(m_properties.position.serialize(builder));
  1614. }
  1615. if (has_from_angle || has_at_position)
  1616. TRY(builder.try_append(", "sv));
  1617. TRY(serialize_color_stop_list(builder, m_properties.color_stop_list));
  1618. TRY(builder.try_append(')'));
  1619. return builder.to_string();
  1620. }
  1621. void ConicGradientStyleValue::resolve_for_size(Layout::Node const& node, CSSPixelSize size) const
  1622. {
  1623. if (!m_resolved.has_value())
  1624. m_resolved = ResolvedData { Painting::resolve_conic_gradient_data(node, *this), {} };
  1625. m_resolved->position = m_properties.position.resolved(node, CSSPixelRect { { 0, 0 }, size });
  1626. }
  1627. void ConicGradientStyleValue::paint(PaintContext& context, DevicePixelRect const& dest_rect, CSS::ImageRendering) const
  1628. {
  1629. VERIFY(m_resolved.has_value());
  1630. Painting::paint_conic_gradient(context, dest_rect, m_resolved->data, context.rounded_device_point(m_resolved->position));
  1631. }
  1632. bool ConicGradientStyleValue::equals(StyleValue const& other) const
  1633. {
  1634. if (type() != other.type())
  1635. return false;
  1636. auto& other_gradient = other.as_conic_gradient();
  1637. return m_properties == other_gradient.m_properties;
  1638. }
  1639. float ConicGradientStyleValue::angle_degrees() const
  1640. {
  1641. return m_properties.from_angle.to_degrees();
  1642. }
  1643. ErrorOr<String> ListStyleStyleValue::to_string() const
  1644. {
  1645. return String::formatted("{} {} {}", TRY(m_properties.position->to_string()), TRY(m_properties.image->to_string()), TRY(m_properties.style_type->to_string()));
  1646. }
  1647. ErrorOr<String> NumericStyleValue::to_string() const
  1648. {
  1649. return m_value.visit(
  1650. [](auto value) {
  1651. return String::formatted("{}", value);
  1652. });
  1653. }
  1654. ErrorOr<String> OverflowStyleValue::to_string() const
  1655. {
  1656. return String::formatted("{} {}", TRY(m_properties.overflow_x->to_string()), TRY(m_properties.overflow_y->to_string()));
  1657. }
  1658. ErrorOr<String> PercentageStyleValue::to_string() const
  1659. {
  1660. return m_percentage.to_string();
  1661. }
  1662. ErrorOr<String> PositionStyleValue::to_string() const
  1663. {
  1664. auto to_string = [](PositionEdge edge) {
  1665. switch (edge) {
  1666. case PositionEdge::Left:
  1667. return "left";
  1668. case PositionEdge::Right:
  1669. return "right";
  1670. case PositionEdge::Top:
  1671. return "top";
  1672. case PositionEdge::Bottom:
  1673. return "bottom";
  1674. }
  1675. VERIFY_NOT_REACHED();
  1676. };
  1677. 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()));
  1678. }
  1679. ErrorOr<String> RectStyleValue::to_string() const
  1680. {
  1681. return String::formatted("rect({} {} {} {})", m_rect.top_edge, m_rect.right_edge, m_rect.bottom_edge, m_rect.left_edge);
  1682. }
  1683. ErrorOr<String> ShadowStyleValue::to_string() const
  1684. {
  1685. StringBuilder builder;
  1686. 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())));
  1687. if (m_properties.placement == ShadowPlacement::Inner)
  1688. TRY(builder.try_append(" inset"sv));
  1689. return builder.to_string();
  1690. }
  1691. ErrorOr<String> TextDecorationStyleValue::to_string() const
  1692. {
  1693. 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()));
  1694. }
  1695. ErrorOr<String> TransformationStyleValue::to_string() const
  1696. {
  1697. StringBuilder builder;
  1698. TRY(builder.try_append(CSS::to_string(m_properties.transform_function)));
  1699. TRY(builder.try_append('('));
  1700. for (size_t i = 0; i < m_properties.values.size(); ++i) {
  1701. TRY(builder.try_append(TRY(m_properties.values[i]->to_string())));
  1702. if (i != m_properties.values.size() - 1)
  1703. TRY(builder.try_append(", "sv));
  1704. }
  1705. TRY(builder.try_append(')'));
  1706. return builder.to_string();
  1707. }
  1708. bool TransformationStyleValue::Properties::operator==(Properties const& other) const
  1709. {
  1710. return transform_function == other.transform_function && values.span() == other.values.span();
  1711. }
  1712. ErrorOr<String> UnresolvedStyleValue::to_string() const
  1713. {
  1714. StringBuilder builder;
  1715. for (auto& value : m_values)
  1716. TRY(builder.try_append(TRY(value.to_string())));
  1717. return builder.to_string();
  1718. }
  1719. bool UnresolvedStyleValue::equals(StyleValue const& other) const
  1720. {
  1721. if (type() != other.type())
  1722. return false;
  1723. // This is a case where comparing the strings actually makes sense.
  1724. return to_string().release_value_but_fixme_should_propagate_errors() == other.to_string().release_value_but_fixme_should_propagate_errors();
  1725. }
  1726. bool StyleValueList::Properties::operator==(Properties const& other) const
  1727. {
  1728. return separator == other.separator && values.span() == other.values.span();
  1729. }
  1730. ErrorOr<String> StyleValueList::to_string() const
  1731. {
  1732. auto separator = ""sv;
  1733. switch (m_properties.separator) {
  1734. case Separator::Space:
  1735. separator = " "sv;
  1736. break;
  1737. case Separator::Comma:
  1738. separator = ", "sv;
  1739. break;
  1740. default:
  1741. VERIFY_NOT_REACHED();
  1742. }
  1743. StringBuilder builder;
  1744. for (size_t i = 0; i < m_properties.values.size(); ++i) {
  1745. TRY(builder.try_append(TRY(m_properties.values[i]->to_string())));
  1746. if (i != m_properties.values.size() - 1)
  1747. TRY(builder.try_append(separator));
  1748. }
  1749. return builder.to_string();
  1750. }
  1751. ValueComparingNonnullRefPtr<GridTemplateAreaStyleValue> GridTemplateAreaStyleValue::create(Vector<Vector<String>> grid_template_area)
  1752. {
  1753. return adopt_ref(*new GridTemplateAreaStyleValue(grid_template_area));
  1754. }
  1755. ValueComparingNonnullRefPtr<GridTrackPlacementStyleValue> GridTrackPlacementStyleValue::create(CSS::GridTrackPlacement grid_track_placement)
  1756. {
  1757. return adopt_ref(*new GridTrackPlacementStyleValue(grid_track_placement));
  1758. }
  1759. ValueComparingNonnullRefPtr<GridTrackSizeStyleValue> GridTrackSizeStyleValue::create(CSS::GridTrackSizeList grid_track_size_list)
  1760. {
  1761. return adopt_ref(*new GridTrackSizeStyleValue(grid_track_size_list));
  1762. }
  1763. ValueComparingNonnullRefPtr<GridTrackSizeStyleValue> GridTrackSizeStyleValue::make_auto()
  1764. {
  1765. return adopt_ref(*new GridTrackSizeStyleValue(CSS::GridTrackSizeList()));
  1766. }
  1767. ValueComparingNonnullRefPtr<RectStyleValue> RectStyleValue::create(EdgeRect rect)
  1768. {
  1769. return adopt_ref(*new RectStyleValue(rect));
  1770. }
  1771. ValueComparingNonnullRefPtr<LengthStyleValue> LengthStyleValue::create(Length const& length)
  1772. {
  1773. if (length.is_auto()) {
  1774. static auto value = adopt_ref(*new LengthStyleValue(CSS::Length::make_auto()));
  1775. return value;
  1776. }
  1777. if (length.is_px()) {
  1778. if (length.raw_value() == 0) {
  1779. static auto value = adopt_ref(*new LengthStyleValue(CSS::Length::make_px(0)));
  1780. return value;
  1781. }
  1782. if (length.raw_value() == 1) {
  1783. static auto value = adopt_ref(*new LengthStyleValue(CSS::Length::make_px(1)));
  1784. return value;
  1785. }
  1786. }
  1787. return adopt_ref(*new LengthStyleValue(length));
  1788. }
  1789. 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)
  1790. {
  1791. if (length.is_px())
  1792. return {};
  1793. if (length.is_absolute() || length.is_relative()) {
  1794. auto px = length.to_px(viewport_rect, font_metrics, font_size, root_font_size, line_height, root_line_height);
  1795. return CSS::Length::make_px(px);
  1796. }
  1797. return {};
  1798. }
  1799. ValueComparingNonnullRefPtr<StyleValue const> StyleValue::absolutized(CSSPixelRect const&, Gfx::FontPixelMetrics const&, CSSPixels, CSSPixels, CSSPixels, CSSPixels) const
  1800. {
  1801. return *this;
  1802. }
  1803. 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
  1804. {
  1805. if (auto length = absolutized_length(m_length, viewport_rect, font_metrics, font_size, root_font_size, line_height, root_line_height); length.has_value())
  1806. return LengthStyleValue::create(length.release_value());
  1807. return *this;
  1808. }
  1809. 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
  1810. {
  1811. 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);
  1812. 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);
  1813. 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);
  1814. 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);
  1815. return ShadowStyleValue::create(m_properties.color, absolutized_offset_x, absolutized_offset_y, absolutized_blur_radius, absolutized_spread_distance, m_properties.placement);
  1816. }
  1817. bool CalculatedStyleValue::contains_percentage() const
  1818. {
  1819. return m_expression->contains_percentage();
  1820. }
  1821. bool CalculatedStyleValue::CalcSum::contains_percentage() const
  1822. {
  1823. if (first_calc_product->contains_percentage())
  1824. return true;
  1825. for (auto& part : zero_or_more_additional_calc_products) {
  1826. if (part->contains_percentage())
  1827. return true;
  1828. }
  1829. return false;
  1830. }
  1831. bool CalculatedStyleValue::CalcSumPartWithOperator::contains_percentage() const
  1832. {
  1833. return value->contains_percentage();
  1834. }
  1835. bool CalculatedStyleValue::CalcProduct::contains_percentage() const
  1836. {
  1837. if (first_calc_value.contains_percentage())
  1838. return true;
  1839. for (auto& part : zero_or_more_additional_calc_values) {
  1840. if (part->contains_percentage())
  1841. return true;
  1842. }
  1843. return false;
  1844. }
  1845. bool CalculatedStyleValue::CalcProductPartWithOperator::contains_percentage() const
  1846. {
  1847. return value.visit(
  1848. [](CalcValue const& value) { return value.contains_percentage(); },
  1849. [](CalcNumberValue const&) { return false; });
  1850. }
  1851. bool CalculatedStyleValue::CalcValue::contains_percentage() const
  1852. {
  1853. return value.visit(
  1854. [](Percentage const&) { return true; },
  1855. [](NonnullOwnPtr<CalcSum> const& sum) { return sum->contains_percentage(); },
  1856. [](auto const&) { return false; });
  1857. }
  1858. bool calculated_style_value_contains_percentage(CalculatedStyleValue const& value)
  1859. {
  1860. return value.contains_percentage();
  1861. }
  1862. }