StyleValue.cpp 74 KB

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