StyleValue.cpp 82 KB

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