StyleValue.cpp 67 KB

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