StyleValue.cpp 87 KB

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