StyleValue.cpp 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395
  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. StyleValue::~StyleValue()
  23. {
  24. }
  25. AngleStyleValue const& StyleValue::as_angle() const
  26. {
  27. VERIFY(is_angle());
  28. return static_cast<AngleStyleValue const&>(*this);
  29. }
  30. BackgroundStyleValue const& StyleValue::as_background() const
  31. {
  32. VERIFY(is_background());
  33. return static_cast<BackgroundStyleValue const&>(*this);
  34. }
  35. BackgroundRepeatStyleValue const& StyleValue::as_background_repeat() const
  36. {
  37. VERIFY(is_background_repeat());
  38. return static_cast<BackgroundRepeatStyleValue const&>(*this);
  39. }
  40. BackgroundSizeStyleValue const& StyleValue::as_background_size() const
  41. {
  42. VERIFY(is_background_size());
  43. return static_cast<BackgroundSizeStyleValue const&>(*this);
  44. }
  45. BorderStyleValue const& StyleValue::as_border() const
  46. {
  47. VERIFY(is_border());
  48. return static_cast<BorderStyleValue const&>(*this);
  49. }
  50. BorderRadiusStyleValue const& StyleValue::as_border_radius() const
  51. {
  52. VERIFY(is_border_radius());
  53. return static_cast<BorderRadiusStyleValue const&>(*this);
  54. }
  55. BoxShadowStyleValue const& StyleValue::as_box_shadow() const
  56. {
  57. VERIFY(is_box_shadow());
  58. return static_cast<BoxShadowStyleValue const&>(*this);
  59. }
  60. CalculatedStyleValue const& StyleValue::as_calculated() const
  61. {
  62. VERIFY(is_calculated());
  63. return static_cast<CalculatedStyleValue const&>(*this);
  64. }
  65. ColorStyleValue const& StyleValue::as_color() const
  66. {
  67. VERIFY(is_color());
  68. return static_cast<ColorStyleValue const&>(*this);
  69. }
  70. FlexStyleValue const& StyleValue::as_flex() const
  71. {
  72. VERIFY(is_flex());
  73. return static_cast<FlexStyleValue const&>(*this);
  74. }
  75. FlexFlowStyleValue const& StyleValue::as_flex_flow() const
  76. {
  77. VERIFY(is_flex_flow());
  78. return static_cast<FlexFlowStyleValue const&>(*this);
  79. }
  80. FontStyleValue const& StyleValue::as_font() const
  81. {
  82. VERIFY(is_font());
  83. return static_cast<FontStyleValue const&>(*this);
  84. }
  85. FrequencyStyleValue const& StyleValue::as_frequency() const
  86. {
  87. VERIFY(is_frequency());
  88. return static_cast<FrequencyStyleValue const&>(*this);
  89. }
  90. IdentifierStyleValue const& StyleValue::as_identifier() const
  91. {
  92. VERIFY(is_identifier());
  93. return static_cast<IdentifierStyleValue const&>(*this);
  94. }
  95. ImageStyleValue const& StyleValue::as_image() const
  96. {
  97. VERIFY(is_image());
  98. return static_cast<ImageStyleValue const&>(*this);
  99. }
  100. InheritStyleValue const& StyleValue::as_inherit() const
  101. {
  102. VERIFY(is_inherit());
  103. return static_cast<InheritStyleValue const&>(*this);
  104. }
  105. InitialStyleValue const& StyleValue::as_initial() const
  106. {
  107. VERIFY(is_initial());
  108. return static_cast<InitialStyleValue const&>(*this);
  109. }
  110. LengthStyleValue const& StyleValue::as_length() const
  111. {
  112. VERIFY(is_length());
  113. return static_cast<LengthStyleValue const&>(*this);
  114. }
  115. ListStyleStyleValue const& StyleValue::as_list_style() const
  116. {
  117. VERIFY(is_list_style());
  118. return static_cast<ListStyleStyleValue const&>(*this);
  119. }
  120. NumericStyleValue const& StyleValue::as_numeric() const
  121. {
  122. VERIFY(is_numeric());
  123. return static_cast<NumericStyleValue const&>(*this);
  124. }
  125. OverflowStyleValue const& StyleValue::as_overflow() const
  126. {
  127. VERIFY(is_overflow());
  128. return static_cast<OverflowStyleValue const&>(*this);
  129. }
  130. PercentageStyleValue const& StyleValue::as_percentage() const
  131. {
  132. VERIFY(is_percentage());
  133. return static_cast<PercentageStyleValue const&>(*this);
  134. }
  135. PositionStyleValue const& StyleValue::as_position() const
  136. {
  137. VERIFY(is_position());
  138. return static_cast<PositionStyleValue const&>(*this);
  139. }
  140. ResolutionStyleValue const& StyleValue::as_resolution() const
  141. {
  142. VERIFY(is_resolution());
  143. return static_cast<ResolutionStyleValue const&>(*this);
  144. }
  145. StringStyleValue const& StyleValue::as_string() const
  146. {
  147. VERIFY(is_string());
  148. return static_cast<StringStyleValue const&>(*this);
  149. }
  150. TextDecorationStyleValue const& StyleValue::as_text_decoration() const
  151. {
  152. VERIFY(is_text_decoration());
  153. return static_cast<TextDecorationStyleValue const&>(*this);
  154. }
  155. TimeStyleValue const& StyleValue::as_time() const
  156. {
  157. VERIFY(is_time());
  158. return static_cast<TimeStyleValue const&>(*this);
  159. }
  160. TransformationStyleValue const& StyleValue::as_transformation() const
  161. {
  162. VERIFY(is_transformation());
  163. return static_cast<TransformationStyleValue const&>(*this);
  164. }
  165. UnresolvedStyleValue const& StyleValue::as_unresolved() const
  166. {
  167. VERIFY(is_unresolved());
  168. return static_cast<UnresolvedStyleValue const&>(*this);
  169. }
  170. UnsetStyleValue const& StyleValue::as_unset() const
  171. {
  172. VERIFY(is_unset());
  173. return static_cast<UnsetStyleValue const&>(*this);
  174. }
  175. StyleValueList const& StyleValue::as_value_list() const
  176. {
  177. VERIFY(is_value_list());
  178. return static_cast<StyleValueList const&>(*this);
  179. }
  180. BackgroundStyleValue::BackgroundStyleValue(
  181. NonnullRefPtr<StyleValue> color,
  182. NonnullRefPtr<StyleValue> image,
  183. NonnullRefPtr<StyleValue> position,
  184. NonnullRefPtr<StyleValue> size,
  185. NonnullRefPtr<StyleValue> repeat,
  186. NonnullRefPtr<StyleValue> attachment,
  187. NonnullRefPtr<StyleValue> origin,
  188. NonnullRefPtr<StyleValue> clip)
  189. : StyleValue(Type::Background)
  190. , m_color(color)
  191. , m_image(image)
  192. , m_position(position)
  193. , m_size(size)
  194. , m_repeat(repeat)
  195. , m_attachment(attachment)
  196. , m_origin(origin)
  197. , m_clip(clip)
  198. {
  199. auto layer_count = [](auto style_value) -> size_t {
  200. if (style_value->is_value_list())
  201. return style_value->as_value_list().size();
  202. else
  203. return 1;
  204. };
  205. m_layer_count = max(layer_count(m_image), layer_count(m_position));
  206. m_layer_count = max(m_layer_count, layer_count(m_size));
  207. m_layer_count = max(m_layer_count, layer_count(m_repeat));
  208. m_layer_count = max(m_layer_count, layer_count(m_attachment));
  209. m_layer_count = max(m_layer_count, layer_count(m_origin));
  210. m_layer_count = max(m_layer_count, layer_count(m_clip));
  211. VERIFY(!m_color->is_value_list());
  212. }
  213. String BackgroundStyleValue::to_string() const
  214. {
  215. if (m_layer_count == 1) {
  216. 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());
  217. }
  218. auto get_layer_value_string = [](NonnullRefPtr<StyleValue> const& style_value, size_t index) {
  219. if (style_value->is_value_list())
  220. return style_value->as_value_list().value_at(index, true)->to_string();
  221. return style_value->to_string();
  222. };
  223. StringBuilder builder;
  224. for (size_t i = 0; i < m_layer_count; i++) {
  225. if (i)
  226. builder.append(", ");
  227. if (i == m_layer_count - 1)
  228. builder.appendff("{} ", m_color->to_string());
  229. 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));
  230. }
  231. return builder.to_string();
  232. }
  233. String BackgroundRepeatStyleValue::to_string() const
  234. {
  235. return String::formatted("{} {}", CSS::to_string(m_repeat_x), CSS::to_string(m_repeat_y));
  236. }
  237. String BackgroundSizeStyleValue::to_string() const
  238. {
  239. return String::formatted("{} {}", m_size_x.to_string(), m_size_y.to_string());
  240. }
  241. String BorderStyleValue::to_string() const
  242. {
  243. return String::formatted("{} {} {}", m_border_width->to_string(), m_border_style->to_string(), m_border_color->to_string());
  244. }
  245. String BorderRadiusStyleValue::to_string() const
  246. {
  247. return String::formatted("{} / {}", m_horizontal_radius.to_string(), m_vertical_radius.to_string());
  248. }
  249. String BoxShadowStyleValue::to_string() const
  250. {
  251. StringBuilder builder;
  252. 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());
  253. if (m_placement == BoxShadowPlacement::Inner)
  254. builder.append(" inset");
  255. return builder.to_string();
  256. }
  257. void CalculatedStyleValue::CalculationResult::add(CalculationResult const& other, Layout::Node const* layout_node, PercentageBasis const& percentage_basis)
  258. {
  259. add_or_subtract_internal(SumOperation::Add, other, layout_node, percentage_basis);
  260. }
  261. void CalculatedStyleValue::CalculationResult::subtract(CalculationResult const& other, Layout::Node const* layout_node, PercentageBasis const& percentage_basis)
  262. {
  263. add_or_subtract_internal(SumOperation::Subtract, other, layout_node, percentage_basis);
  264. }
  265. void CalculatedStyleValue::CalculationResult::add_or_subtract_internal(SumOperation op, CalculationResult const& other, Layout::Node const* layout_node, PercentageBasis const& percentage_basis)
  266. {
  267. // 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>".
  268. // Though, having the same type may mean that one side is a <dimension> and the other a <percentage>.
  269. // Note: This is almost identical to ::add()
  270. m_value.visit(
  271. [&](Number const& number) {
  272. auto other_number = other.m_value.get<Number>();
  273. if (op == SumOperation::Add) {
  274. m_value = Number {
  275. .is_integer = number.is_integer && other_number.is_integer,
  276. .value = number.value + other_number.value
  277. };
  278. } else {
  279. m_value = Number {
  280. .is_integer = number.is_integer && other_number.is_integer,
  281. .value = number.value - other_number.value
  282. };
  283. }
  284. },
  285. [&](Angle const& angle) {
  286. auto this_degrees = angle.to_degrees();
  287. if (other.m_value.has<Angle>()) {
  288. auto other_degrees = other.m_value.get<Angle>().to_degrees();
  289. if (op == SumOperation::Add)
  290. m_value = Angle::make_degrees(this_degrees + other_degrees);
  291. else
  292. m_value = Angle::make_degrees(this_degrees - other_degrees);
  293. } else {
  294. VERIFY(percentage_basis.has<Angle>());
  295. auto other_degrees = percentage_basis.get<Angle>().percentage_of(other.m_value.get<Percentage>()).to_degrees();
  296. if (op == SumOperation::Add)
  297. m_value = Angle::make_degrees(this_degrees + other_degrees);
  298. else
  299. m_value = Angle::make_degrees(this_degrees - other_degrees);
  300. }
  301. },
  302. [&](Frequency const& frequency) {
  303. auto this_hertz = frequency.to_hertz();
  304. if (other.m_value.has<Frequency>()) {
  305. auto other_hertz = other.m_value.get<Frequency>().to_hertz();
  306. if (op == SumOperation::Add)
  307. m_value = Frequency::make_hertz(this_hertz + other_hertz);
  308. else
  309. m_value = Frequency::make_hertz(this_hertz - other_hertz);
  310. } else {
  311. VERIFY(percentage_basis.has<Frequency>());
  312. auto other_hertz = percentage_basis.get<Frequency>().percentage_of(other.m_value.get<Percentage>()).to_hertz();
  313. if (op == SumOperation::Add)
  314. m_value = Frequency::make_hertz(this_hertz + other_hertz);
  315. else
  316. m_value = Frequency::make_hertz(this_hertz - other_hertz);
  317. }
  318. },
  319. [&](Length const& length) {
  320. auto this_px = length.to_px(*layout_node);
  321. if (other.m_value.has<Length>()) {
  322. auto other_px = other.m_value.get<Length>().to_px(*layout_node);
  323. if (op == SumOperation::Add)
  324. m_value = Length::make_px(this_px + other_px);
  325. else
  326. m_value = Length::make_px(this_px - other_px);
  327. } else {
  328. VERIFY(percentage_basis.has<Length>());
  329. auto other_px = percentage_basis.get<Length>().percentage_of(other.m_value.get<Percentage>()).to_px(*layout_node);
  330. if (op == SumOperation::Add)
  331. m_value = Length::make_px(this_px + other_px);
  332. else
  333. m_value = Length::make_px(this_px - other_px);
  334. }
  335. },
  336. [&](Time const& time) {
  337. auto this_seconds = time.to_seconds();
  338. if (other.m_value.has<Time>()) {
  339. auto other_seconds = other.m_value.get<Time>().to_seconds();
  340. if (op == SumOperation::Add)
  341. m_value = Time::make_seconds(this_seconds + other_seconds);
  342. else
  343. m_value = Time::make_seconds(this_seconds - other_seconds);
  344. } else {
  345. VERIFY(percentage_basis.has<Time>());
  346. auto other_seconds = percentage_basis.get<Time>().percentage_of(other.m_value.get<Percentage>()).to_seconds();
  347. if (op == SumOperation::Add)
  348. m_value = Time::make_seconds(this_seconds + other_seconds);
  349. else
  350. m_value = Time::make_seconds(this_seconds - other_seconds);
  351. }
  352. },
  353. [&](Percentage const& percentage) {
  354. if (other.m_value.has<Percentage>()) {
  355. if (op == SumOperation::Add)
  356. m_value = Percentage { percentage.value() + other.m_value.get<Percentage>().value() };
  357. else
  358. m_value = Percentage { percentage.value() - other.m_value.get<Percentage>().value() };
  359. return;
  360. }
  361. // 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`.
  362. CalculationResult new_value = other;
  363. if (op == SumOperation::Add)
  364. new_value.add(*this, layout_node, percentage_basis);
  365. else
  366. new_value.subtract(*this, layout_node, percentage_basis);
  367. *this = new_value;
  368. });
  369. }
  370. void CalculatedStyleValue::CalculationResult::multiply_by(CalculationResult const& other, Layout::Node const* layout_node)
  371. {
  372. // We know from validation when resolving the type, that at least one side must be a <number> or <integer>.
  373. // Both of these are represented as a float.
  374. VERIFY(m_value.has<Number>() || other.m_value.has<Number>());
  375. bool other_is_number = other.m_value.has<Number>();
  376. m_value.visit(
  377. [&](Number const& number) {
  378. if (other_is_number) {
  379. auto other_number = other.m_value.get<Number>();
  380. m_value = Number {
  381. .is_integer = number.is_integer && other_number.is_integer,
  382. .value = number.value * other_number.value
  383. };
  384. } else {
  385. // Avoid duplicating all the logic by swapping `this` and `other`.
  386. CalculationResult new_value = other;
  387. new_value.multiply_by(*this, layout_node);
  388. *this = new_value;
  389. }
  390. },
  391. [&](Angle const& angle) {
  392. m_value = Angle::make_degrees(angle.to_degrees() * other.m_value.get<Number>().value);
  393. },
  394. [&](Frequency const& frequency) {
  395. m_value = Frequency::make_hertz(frequency.to_hertz() * other.m_value.get<Number>().value);
  396. },
  397. [&](Length const& length) {
  398. VERIFY(layout_node);
  399. m_value = Length::make_px(length.to_px(*layout_node) * other.m_value.get<Number>().value);
  400. },
  401. [&](Time const& time) {
  402. m_value = Time::make_seconds(time.to_seconds() * other.m_value.get<Number>().value);
  403. },
  404. [&](Percentage const& percentage) {
  405. m_value = Percentage { percentage.value() * other.m_value.get<Number>().value };
  406. });
  407. }
  408. void CalculatedStyleValue::CalculationResult::divide_by(CalculationResult const& other, Layout::Node const* layout_node)
  409. {
  410. // We know from validation when resolving the type, that `other` must be a <number> or <integer>.
  411. // Both of these are represented as a Number.
  412. auto denominator = other.m_value.get<Number>().value;
  413. // FIXME: Dividing by 0 is invalid, and should be caught during parsing.
  414. VERIFY(denominator != 0.0f);
  415. m_value.visit(
  416. [&](Number const& number) {
  417. m_value = Number {
  418. .is_integer = false,
  419. .value = number.value / denominator
  420. };
  421. },
  422. [&](Angle const& angle) {
  423. m_value = Angle::make_degrees(angle.to_degrees() / denominator);
  424. },
  425. [&](Frequency const& frequency) {
  426. m_value = Frequency::make_hertz(frequency.to_hertz() / denominator);
  427. },
  428. [&](Length const& length) {
  429. VERIFY(layout_node);
  430. m_value = Length::make_px(length.to_px(*layout_node) / denominator);
  431. },
  432. [&](Time const& time) {
  433. m_value = Time::make_seconds(time.to_seconds() / denominator);
  434. },
  435. [&](Percentage const& percentage) {
  436. m_value = Percentage { percentage.value() / denominator };
  437. });
  438. }
  439. String CalculatedStyleValue::to_string() const
  440. {
  441. return String::formatted("calc({})", m_expression->to_string());
  442. }
  443. String CalculatedStyleValue::CalcNumberValue::to_string() const
  444. {
  445. return value.visit(
  446. [](Number const& number) { return String::number(number.value); },
  447. [](NonnullOwnPtr<CalcNumberSum> const& sum) { return String::formatted("({})", sum->to_string()); });
  448. }
  449. String CalculatedStyleValue::CalcValue::to_string() const
  450. {
  451. return value.visit(
  452. [](Number const& number) { return String::number(number.value); },
  453. [](NonnullOwnPtr<CalcSum> const& sum) { return String::formatted("({})", sum->to_string()); },
  454. [](auto const& v) { return v.to_string(); });
  455. }
  456. String CalculatedStyleValue::CalcSum::to_string() const
  457. {
  458. StringBuilder builder;
  459. builder.append(first_calc_product->to_string());
  460. for (auto const& item : zero_or_more_additional_calc_products)
  461. builder.append(item.to_string());
  462. return builder.to_string();
  463. }
  464. String CalculatedStyleValue::CalcNumberSum::to_string() const
  465. {
  466. StringBuilder builder;
  467. builder.append(first_calc_number_product->to_string());
  468. for (auto const& item : zero_or_more_additional_calc_number_products)
  469. builder.append(item.to_string());
  470. return builder.to_string();
  471. }
  472. String CalculatedStyleValue::CalcProduct::to_string() const
  473. {
  474. StringBuilder builder;
  475. builder.append(first_calc_value.to_string());
  476. for (auto const& item : zero_or_more_additional_calc_values)
  477. builder.append(item.to_string());
  478. return builder.to_string();
  479. }
  480. String CalculatedStyleValue::CalcSumPartWithOperator::to_string() const
  481. {
  482. return String::formatted(" {} {}", op == SumOperation::Add ? "+"sv : "-"sv, value->to_string());
  483. }
  484. String CalculatedStyleValue::CalcProductPartWithOperator::to_string() const
  485. {
  486. auto value_string = value.visit(
  487. [](CalcValue const& v) { return v.to_string(); },
  488. [](CalcNumberValue const& v) { return v.to_string(); });
  489. return String::formatted(" {} {}", op == ProductOperation::Multiply ? "*"sv : "/"sv, value_string);
  490. }
  491. String CalculatedStyleValue::CalcNumberProduct::to_string() const
  492. {
  493. StringBuilder builder;
  494. builder.append(first_calc_number_value.to_string());
  495. for (auto const& item : zero_or_more_additional_calc_number_values)
  496. builder.append(item.to_string());
  497. return builder.to_string();
  498. }
  499. String CalculatedStyleValue::CalcNumberProductPartWithOperator::to_string() const
  500. {
  501. return String::formatted(" {} {}", op == ProductOperation::Multiply ? "*"sv : "/"sv, value.to_string());
  502. }
  503. String CalculatedStyleValue::CalcNumberSumPartWithOperator::to_string() const
  504. {
  505. return String::formatted(" {} {}", op == SumOperation::Add ? "+"sv : "-"sv, value->to_string());
  506. }
  507. Optional<Angle> CalculatedStyleValue::resolve_angle() const
  508. {
  509. auto result = m_expression->resolve(nullptr, {});
  510. if (result.value().has<Angle>())
  511. return result.value().get<Angle>();
  512. return {};
  513. }
  514. Optional<AnglePercentage> CalculatedStyleValue::resolve_angle_percentage(Angle const& percentage_basis) const
  515. {
  516. auto result = m_expression->resolve(nullptr, percentage_basis);
  517. return result.value().visit(
  518. [&](Angle const& angle) -> Optional<AnglePercentage> {
  519. return angle;
  520. },
  521. [&](Percentage const& percentage) -> Optional<AnglePercentage> {
  522. return percentage;
  523. },
  524. [&](auto const&) -> Optional<AnglePercentage> {
  525. return {};
  526. });
  527. }
  528. Optional<Frequency> CalculatedStyleValue::resolve_frequency() const
  529. {
  530. auto result = m_expression->resolve(nullptr, {});
  531. if (result.value().has<Frequency>())
  532. return result.value().get<Frequency>();
  533. return {};
  534. }
  535. Optional<FrequencyPercentage> CalculatedStyleValue::resolve_frequency_percentage(Frequency const& percentage_basis) const
  536. {
  537. auto result = m_expression->resolve(nullptr, percentage_basis);
  538. return result.value().visit(
  539. [&](Frequency const& frequency) -> Optional<FrequencyPercentage> {
  540. return frequency;
  541. },
  542. [&](Percentage const& percentage) -> Optional<FrequencyPercentage> {
  543. return percentage;
  544. },
  545. [&](auto const&) -> Optional<FrequencyPercentage> {
  546. return {};
  547. });
  548. }
  549. Optional<Length> CalculatedStyleValue::resolve_length(Layout::Node const& layout_node) const
  550. {
  551. auto result = m_expression->resolve(&layout_node, {});
  552. if (result.value().has<Length>())
  553. return result.value().get<Length>();
  554. return {};
  555. }
  556. Optional<LengthPercentage> CalculatedStyleValue::resolve_length_percentage(Layout::Node const& layout_node, Length const& percentage_basis) const
  557. {
  558. auto result = m_expression->resolve(&layout_node, percentage_basis);
  559. return result.value().visit(
  560. [&](Length const& length) -> Optional<LengthPercentage> {
  561. return length;
  562. },
  563. [&](Percentage const& percentage) -> Optional<LengthPercentage> {
  564. return percentage;
  565. },
  566. [&](auto const&) -> Optional<LengthPercentage> {
  567. return {};
  568. });
  569. }
  570. Optional<Percentage> CalculatedStyleValue::resolve_percentage() const
  571. {
  572. auto result = m_expression->resolve(nullptr, {});
  573. if (result.value().has<Percentage>())
  574. return result.value().get<Percentage>();
  575. return {};
  576. }
  577. Optional<Time> CalculatedStyleValue::resolve_time() const
  578. {
  579. auto result = m_expression->resolve(nullptr, {});
  580. if (result.value().has<Time>())
  581. return result.value().get<Time>();
  582. return {};
  583. }
  584. Optional<TimePercentage> CalculatedStyleValue::resolve_time_percentage(Time const& percentage_basis) const
  585. {
  586. auto result = m_expression->resolve(nullptr, percentage_basis);
  587. return result.value().visit(
  588. [&](Time const& time) -> Optional<TimePercentage> {
  589. return time;
  590. },
  591. [&](Percentage const& percentage) -> Optional<TimePercentage> {
  592. return percentage;
  593. },
  594. [&](auto const&) -> Optional<TimePercentage> {
  595. return {};
  596. });
  597. }
  598. Optional<float> CalculatedStyleValue::resolve_number()
  599. {
  600. auto result = m_expression->resolve(nullptr, {});
  601. if (result.value().has<Number>())
  602. return result.value().get<Number>().value;
  603. return {};
  604. }
  605. Optional<i64> CalculatedStyleValue::resolve_integer()
  606. {
  607. auto result = m_expression->resolve(nullptr, {});
  608. if (result.value().has<Number>())
  609. return lroundf(result.value().get<Number>().value);
  610. return {};
  611. }
  612. static bool is_number(CalculatedStyleValue::ResolvedType type)
  613. {
  614. return type == CalculatedStyleValue::ResolvedType::Number || type == CalculatedStyleValue::ResolvedType::Integer;
  615. }
  616. static bool is_dimension(CalculatedStyleValue::ResolvedType type)
  617. {
  618. return type != CalculatedStyleValue::ResolvedType::Number
  619. && type != CalculatedStyleValue::ResolvedType::Integer
  620. && type != CalculatedStyleValue::ResolvedType::Percentage;
  621. }
  622. template<typename SumWithOperator>
  623. static Optional<CalculatedStyleValue::ResolvedType> resolve_sum_type(CalculatedStyleValue::ResolvedType first_type, NonnullOwnPtrVector<SumWithOperator> const& zero_or_more_additional_products)
  624. {
  625. auto type = first_type;
  626. for (auto const& product : zero_or_more_additional_products) {
  627. auto maybe_product_type = product.resolved_type();
  628. if (!maybe_product_type.has_value())
  629. return {};
  630. auto product_type = maybe_product_type.value();
  631. // At + or -, check that both sides have the same type, or that one side is a <number> and the other is an <integer>.
  632. // If both sides are the same type, resolve to that type.
  633. if (product_type == type)
  634. continue;
  635. // If one side is a <number> and the other is an <integer>, resolve to <number>.
  636. if (is_number(type) && is_number(product_type)) {
  637. type = CalculatedStyleValue::ResolvedType::Number;
  638. continue;
  639. }
  640. // FIXME: calc() handles <percentage> by allowing them to pretend to be whatever <dimension> type is allowed at this location.
  641. // Since we can't easily check what that type is, we just allow <percentage> to combine with any other <dimension> type.
  642. if (type == CalculatedStyleValue::ResolvedType::Percentage && is_dimension(product_type)) {
  643. type = product_type;
  644. continue;
  645. }
  646. if (is_dimension(type) && product_type == CalculatedStyleValue::ResolvedType::Percentage)
  647. continue;
  648. return {};
  649. }
  650. return type;
  651. }
  652. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcSum::resolved_type() const
  653. {
  654. auto maybe_type = first_calc_product->resolved_type();
  655. if (!maybe_type.has_value())
  656. return {};
  657. auto type = maybe_type.value();
  658. return resolve_sum_type(type, zero_or_more_additional_calc_products);
  659. }
  660. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcNumberSum::resolved_type() const
  661. {
  662. auto maybe_type = first_calc_number_product->resolved_type();
  663. if (!maybe_type.has_value())
  664. return {};
  665. auto type = maybe_type.value();
  666. return resolve_sum_type(type, zero_or_more_additional_calc_number_products);
  667. }
  668. template<typename ProductWithOperator>
  669. static Optional<CalculatedStyleValue::ResolvedType> resolve_product_type(CalculatedStyleValue::ResolvedType first_type, NonnullOwnPtrVector<ProductWithOperator> const& zero_or_more_additional_values)
  670. {
  671. auto type = first_type;
  672. for (auto const& value : zero_or_more_additional_values) {
  673. auto maybe_value_type = value.resolved_type();
  674. if (!maybe_value_type.has_value())
  675. return {};
  676. auto value_type = maybe_value_type.value();
  677. if (value.op == CalculatedStyleValue::ProductOperation::Multiply) {
  678. // At *, check that at least one side is <number>.
  679. if (!(is_number(type) || is_number(value_type)))
  680. return {};
  681. // If both sides are <integer>, resolve to <integer>.
  682. if (type == CalculatedStyleValue::ResolvedType::Integer && value_type == CalculatedStyleValue::ResolvedType::Integer) {
  683. type = CalculatedStyleValue::ResolvedType::Integer;
  684. } else {
  685. // Otherwise, resolve to the type of the other side.
  686. if (is_number(type))
  687. type = value_type;
  688. }
  689. continue;
  690. } else {
  691. VERIFY(value.op == CalculatedStyleValue::ProductOperation::Divide);
  692. // At /, check that the right side is <number>.
  693. if (!is_number(value_type))
  694. return {};
  695. // If the left side is <integer>, resolve to <number>.
  696. if (type == CalculatedStyleValue::ResolvedType::Integer) {
  697. type = CalculatedStyleValue::ResolvedType::Number;
  698. } else {
  699. // Otherwise, resolve to the type of the left side.
  700. }
  701. // FIXME: Division by zero makes the whole calc() expression invalid.
  702. }
  703. }
  704. return type;
  705. }
  706. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcProduct::resolved_type() const
  707. {
  708. auto maybe_type = first_calc_value.resolved_type();
  709. if (!maybe_type.has_value())
  710. return {};
  711. auto type = maybe_type.value();
  712. return resolve_product_type(type, zero_or_more_additional_calc_values);
  713. }
  714. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcSumPartWithOperator::resolved_type() const
  715. {
  716. return value->resolved_type();
  717. }
  718. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcNumberProduct::resolved_type() const
  719. {
  720. auto maybe_type = first_calc_number_value.resolved_type();
  721. if (!maybe_type.has_value())
  722. return {};
  723. auto type = maybe_type.value();
  724. return resolve_product_type(type, zero_or_more_additional_calc_number_values);
  725. }
  726. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcNumberProductPartWithOperator::resolved_type() const
  727. {
  728. return value.resolved_type();
  729. }
  730. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcNumberSumPartWithOperator::resolved_type() const
  731. {
  732. return value->resolved_type();
  733. }
  734. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcProductPartWithOperator::resolved_type() const
  735. {
  736. return value.visit(
  737. [](CalcValue const& calc_value) {
  738. return calc_value.resolved_type();
  739. },
  740. [](CalcNumberValue const& calc_number_value) {
  741. return calc_number_value.resolved_type();
  742. });
  743. }
  744. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcValue::resolved_type() const
  745. {
  746. return value.visit(
  747. [](Number const& number) -> Optional<CalculatedStyleValue::ResolvedType> {
  748. return { number.is_integer ? ResolvedType::Integer : ResolvedType::Number };
  749. },
  750. [](Angle const&) -> Optional<CalculatedStyleValue::ResolvedType> { return { ResolvedType::Angle }; },
  751. [](Frequency const&) -> Optional<CalculatedStyleValue::ResolvedType> { return { ResolvedType::Frequency }; },
  752. [](Length const&) -> Optional<CalculatedStyleValue::ResolvedType> { return { ResolvedType::Length }; },
  753. [](Percentage const&) -> Optional<CalculatedStyleValue::ResolvedType> { return { ResolvedType::Percentage }; },
  754. [](Time const&) -> Optional<CalculatedStyleValue::ResolvedType> { return { ResolvedType::Time }; },
  755. [](NonnullOwnPtr<CalcSum> const& sum) { return sum->resolved_type(); });
  756. }
  757. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcNumberValue::resolved_type() const
  758. {
  759. return value.visit(
  760. [](Number const& number) -> Optional<CalculatedStyleValue::ResolvedType> {
  761. return { number.is_integer ? ResolvedType::Integer : ResolvedType::Number };
  762. },
  763. [](NonnullOwnPtr<CalcNumberSum> const& sum) { return sum->resolved_type(); });
  764. }
  765. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcNumberValue::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  766. {
  767. return value.visit(
  768. [&](Number const& number) -> CalculatedStyleValue::CalculationResult {
  769. return CalculatedStyleValue::CalculationResult { number };
  770. },
  771. [&](NonnullOwnPtr<CalcNumberSum> const& sum) -> CalculatedStyleValue::CalculationResult {
  772. return sum->resolve(layout_node, percentage_basis);
  773. });
  774. }
  775. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcValue::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  776. {
  777. return value.visit(
  778. [&](NonnullOwnPtr<CalcSum> const& sum) -> CalculatedStyleValue::CalculationResult {
  779. return sum->resolve(layout_node, percentage_basis);
  780. },
  781. [&](auto const& v) -> CalculatedStyleValue::CalculationResult {
  782. return CalculatedStyleValue::CalculationResult { v };
  783. });
  784. }
  785. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcSum::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  786. {
  787. auto value = first_calc_product->resolve(layout_node, percentage_basis);
  788. for (auto& additional_product : zero_or_more_additional_calc_products) {
  789. auto additional_value = additional_product.resolve(layout_node, percentage_basis);
  790. if (additional_product.op == CalculatedStyleValue::SumOperation::Add)
  791. value.add(additional_value, layout_node, percentage_basis);
  792. else if (additional_product.op == CalculatedStyleValue::SumOperation::Subtract)
  793. value.subtract(additional_value, layout_node, percentage_basis);
  794. else
  795. VERIFY_NOT_REACHED();
  796. }
  797. return value;
  798. }
  799. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcNumberSum::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  800. {
  801. auto value = first_calc_number_product->resolve(layout_node, percentage_basis);
  802. for (auto& additional_product : zero_or_more_additional_calc_number_products) {
  803. auto additional_value = additional_product.resolve(layout_node, percentage_basis);
  804. if (additional_product.op == CSS::CalculatedStyleValue::SumOperation::Add)
  805. value.add(additional_value, layout_node, percentage_basis);
  806. else if (additional_product.op == CalculatedStyleValue::SumOperation::Subtract)
  807. value.subtract(additional_value, layout_node, percentage_basis);
  808. else
  809. VERIFY_NOT_REACHED();
  810. }
  811. return value;
  812. }
  813. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcProduct::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  814. {
  815. auto value = first_calc_value.resolve(layout_node, percentage_basis);
  816. for (auto& additional_value : zero_or_more_additional_calc_values) {
  817. additional_value.value.visit(
  818. [&](CalculatedStyleValue::CalcValue const& calc_value) {
  819. VERIFY(additional_value.op == CalculatedStyleValue::ProductOperation::Multiply);
  820. auto resolved_value = calc_value.resolve(layout_node, percentage_basis);
  821. value.multiply_by(resolved_value, layout_node);
  822. },
  823. [&](CalculatedStyleValue::CalcNumberValue const& calc_number_value) {
  824. VERIFY(additional_value.op == CalculatedStyleValue::ProductOperation::Divide);
  825. auto resolved_calc_number_value = calc_number_value.resolve(layout_node, percentage_basis);
  826. // FIXME: Checking for division by 0 should happen during parsing.
  827. VERIFY(resolved_calc_number_value.value().get<Number>().value != 0.0f);
  828. value.divide_by(resolved_calc_number_value, layout_node);
  829. });
  830. }
  831. return value;
  832. }
  833. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcNumberProduct::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  834. {
  835. auto value = first_calc_number_value.resolve(layout_node, percentage_basis);
  836. for (auto& additional_number_value : zero_or_more_additional_calc_number_values) {
  837. auto additional_value = additional_number_value.resolve(layout_node, percentage_basis);
  838. if (additional_number_value.op == CalculatedStyleValue::ProductOperation::Multiply)
  839. value.multiply_by(additional_value, layout_node);
  840. else if (additional_number_value.op == CalculatedStyleValue::ProductOperation::Divide)
  841. value.divide_by(additional_value, layout_node);
  842. else
  843. VERIFY_NOT_REACHED();
  844. }
  845. return value;
  846. }
  847. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcProductPartWithOperator::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  848. {
  849. return value.visit(
  850. [&](CalcValue const& calc_value) {
  851. return calc_value.resolve(layout_node, percentage_basis);
  852. },
  853. [&](CalcNumberValue const& calc_number_value) {
  854. return calc_number_value.resolve(layout_node, percentage_basis);
  855. });
  856. }
  857. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcSumPartWithOperator::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  858. {
  859. return value->resolve(layout_node, percentage_basis);
  860. }
  861. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcNumberProductPartWithOperator::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  862. {
  863. return value.resolve(layout_node, percentage_basis);
  864. }
  865. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcNumberSumPartWithOperator::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  866. {
  867. return value->resolve(layout_node, percentage_basis);
  868. }
  869. // https://www.w3.org/TR/css-color-4/#serializing-sRGB-values
  870. String ColorStyleValue::to_string() const
  871. {
  872. if (m_color.alpha() == 1)
  873. return String::formatted("rgb({}, {}, {})", m_color.red(), m_color.green(), m_color.blue());
  874. return String::formatted("rgba({}, {}, {}, {})", m_color.red(), m_color.green(), m_color.blue(), (float)(m_color.alpha()) / 255.0f);
  875. }
  876. String CombinedBorderRadiusStyleValue::to_string() const
  877. {
  878. 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());
  879. }
  880. String FlexStyleValue::to_string() const
  881. {
  882. return String::formatted("{} {} {}", m_grow->to_string(), m_shrink->to_string(), m_basis->to_string());
  883. }
  884. String FlexFlowStyleValue::to_string() const
  885. {
  886. return String::formatted("{} {}", m_flex_direction->to_string(), m_flex_wrap->to_string());
  887. }
  888. String FontStyleValue::to_string() const
  889. {
  890. 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());
  891. }
  892. String IdentifierStyleValue::to_string() const
  893. {
  894. return CSS::string_from_value_id(m_id);
  895. }
  896. bool IdentifierStyleValue::has_color() const
  897. {
  898. switch (m_id) {
  899. case ValueID::Currentcolor:
  900. case ValueID::LibwebLink:
  901. case ValueID::LibwebPaletteActiveLink:
  902. case ValueID::LibwebPaletteActiveWindowBorder1:
  903. case ValueID::LibwebPaletteActiveWindowBorder2:
  904. case ValueID::LibwebPaletteActiveWindowTitle:
  905. case ValueID::LibwebPaletteBase:
  906. case ValueID::LibwebPaletteBaseText:
  907. case ValueID::LibwebPaletteButton:
  908. case ValueID::LibwebPaletteButtonText:
  909. case ValueID::LibwebPaletteDesktopBackground:
  910. case ValueID::LibwebPaletteFocusOutline:
  911. case ValueID::LibwebPaletteHighlightWindowBorder1:
  912. case ValueID::LibwebPaletteHighlightWindowBorder2:
  913. case ValueID::LibwebPaletteHighlightWindowTitle:
  914. case ValueID::LibwebPaletteHoverHighlight:
  915. case ValueID::LibwebPaletteInactiveSelection:
  916. case ValueID::LibwebPaletteInactiveSelectionText:
  917. case ValueID::LibwebPaletteInactiveWindowBorder1:
  918. case ValueID::LibwebPaletteInactiveWindowBorder2:
  919. case ValueID::LibwebPaletteInactiveWindowTitle:
  920. case ValueID::LibwebPaletteLink:
  921. case ValueID::LibwebPaletteMenuBase:
  922. case ValueID::LibwebPaletteMenuBaseText:
  923. case ValueID::LibwebPaletteMenuSelection:
  924. case ValueID::LibwebPaletteMenuSelectionText:
  925. case ValueID::LibwebPaletteMenuStripe:
  926. case ValueID::LibwebPaletteMovingWindowBorder1:
  927. case ValueID::LibwebPaletteMovingWindowBorder2:
  928. case ValueID::LibwebPaletteMovingWindowTitle:
  929. case ValueID::LibwebPaletteRubberBandBorder:
  930. case ValueID::LibwebPaletteRubberBandFill:
  931. case ValueID::LibwebPaletteRuler:
  932. case ValueID::LibwebPaletteRulerActiveText:
  933. case ValueID::LibwebPaletteRulerBorder:
  934. case ValueID::LibwebPaletteRulerInactiveText:
  935. case ValueID::LibwebPaletteSelection:
  936. case ValueID::LibwebPaletteSelectionText:
  937. case ValueID::LibwebPaletteSyntaxComment:
  938. case ValueID::LibwebPaletteSyntaxControlKeyword:
  939. case ValueID::LibwebPaletteSyntaxIdentifier:
  940. case ValueID::LibwebPaletteSyntaxKeyword:
  941. case ValueID::LibwebPaletteSyntaxNumber:
  942. case ValueID::LibwebPaletteSyntaxOperator:
  943. case ValueID::LibwebPaletteSyntaxPreprocessorStatement:
  944. case ValueID::LibwebPaletteSyntaxPreprocessorValue:
  945. case ValueID::LibwebPaletteSyntaxPunctuation:
  946. case ValueID::LibwebPaletteSyntaxString:
  947. case ValueID::LibwebPaletteSyntaxType:
  948. case ValueID::LibwebPaletteTextCursor:
  949. case ValueID::LibwebPaletteThreedHighlight:
  950. case ValueID::LibwebPaletteThreedShadow1:
  951. case ValueID::LibwebPaletteThreedShadow2:
  952. case ValueID::LibwebPaletteVisitedLink:
  953. case ValueID::LibwebPaletteWindow:
  954. case ValueID::LibwebPaletteWindowText:
  955. return true;
  956. default:
  957. return false;
  958. }
  959. }
  960. Color IdentifierStyleValue::to_color(Layout::NodeWithStyle const& node) const
  961. {
  962. if (id() == CSS::ValueID::Currentcolor) {
  963. if (!node.has_style())
  964. return Color::Black;
  965. return node.computed_values().color();
  966. }
  967. auto& document = node.document();
  968. if (id() == CSS::ValueID::LibwebLink)
  969. return document.link_color();
  970. VERIFY(document.page());
  971. auto palette = document.page()->palette();
  972. switch (id()) {
  973. case CSS::ValueID::LibwebPaletteDesktopBackground:
  974. return palette.color(ColorRole::DesktopBackground);
  975. case CSS::ValueID::LibwebPaletteActiveWindowBorder1:
  976. return palette.color(ColorRole::ActiveWindowBorder1);
  977. case CSS::ValueID::LibwebPaletteActiveWindowBorder2:
  978. return palette.color(ColorRole::ActiveWindowBorder2);
  979. case CSS::ValueID::LibwebPaletteActiveWindowTitle:
  980. return palette.color(ColorRole::ActiveWindowTitle);
  981. case CSS::ValueID::LibwebPaletteInactiveWindowBorder1:
  982. return palette.color(ColorRole::InactiveWindowBorder1);
  983. case CSS::ValueID::LibwebPaletteInactiveWindowBorder2:
  984. return palette.color(ColorRole::InactiveWindowBorder2);
  985. case CSS::ValueID::LibwebPaletteInactiveWindowTitle:
  986. return palette.color(ColorRole::InactiveWindowTitle);
  987. case CSS::ValueID::LibwebPaletteMovingWindowBorder1:
  988. return palette.color(ColorRole::MovingWindowBorder1);
  989. case CSS::ValueID::LibwebPaletteMovingWindowBorder2:
  990. return palette.color(ColorRole::MovingWindowBorder2);
  991. case CSS::ValueID::LibwebPaletteMovingWindowTitle:
  992. return palette.color(ColorRole::MovingWindowTitle);
  993. case CSS::ValueID::LibwebPaletteHighlightWindowBorder1:
  994. return palette.color(ColorRole::HighlightWindowBorder1);
  995. case CSS::ValueID::LibwebPaletteHighlightWindowBorder2:
  996. return palette.color(ColorRole::HighlightWindowBorder2);
  997. case CSS::ValueID::LibwebPaletteHighlightWindowTitle:
  998. return palette.color(ColorRole::HighlightWindowTitle);
  999. case CSS::ValueID::LibwebPaletteMenuStripe:
  1000. return palette.color(ColorRole::MenuStripe);
  1001. case CSS::ValueID::LibwebPaletteMenuBase:
  1002. return palette.color(ColorRole::MenuBase);
  1003. case CSS::ValueID::LibwebPaletteMenuBaseText:
  1004. return palette.color(ColorRole::MenuBaseText);
  1005. case CSS::ValueID::LibwebPaletteMenuSelection:
  1006. return palette.color(ColorRole::MenuSelection);
  1007. case CSS::ValueID::LibwebPaletteMenuSelectionText:
  1008. return palette.color(ColorRole::MenuSelectionText);
  1009. case CSS::ValueID::LibwebPaletteWindow:
  1010. return palette.color(ColorRole::Window);
  1011. case CSS::ValueID::LibwebPaletteWindowText:
  1012. return palette.color(ColorRole::WindowText);
  1013. case CSS::ValueID::LibwebPaletteButton:
  1014. return palette.color(ColorRole::Button);
  1015. case CSS::ValueID::LibwebPaletteButtonText:
  1016. return palette.color(ColorRole::ButtonText);
  1017. case CSS::ValueID::LibwebPaletteBase:
  1018. return palette.color(ColorRole::Base);
  1019. case CSS::ValueID::LibwebPaletteBaseText:
  1020. return palette.color(ColorRole::BaseText);
  1021. case CSS::ValueID::LibwebPaletteThreedHighlight:
  1022. return palette.color(ColorRole::ThreedHighlight);
  1023. case CSS::ValueID::LibwebPaletteThreedShadow1:
  1024. return palette.color(ColorRole::ThreedShadow1);
  1025. case CSS::ValueID::LibwebPaletteThreedShadow2:
  1026. return palette.color(ColorRole::ThreedShadow2);
  1027. case CSS::ValueID::LibwebPaletteHoverHighlight:
  1028. return palette.color(ColorRole::HoverHighlight);
  1029. case CSS::ValueID::LibwebPaletteSelection:
  1030. return palette.color(ColorRole::Selection);
  1031. case CSS::ValueID::LibwebPaletteSelectionText:
  1032. return palette.color(ColorRole::SelectionText);
  1033. case CSS::ValueID::LibwebPaletteInactiveSelection:
  1034. return palette.color(ColorRole::InactiveSelection);
  1035. case CSS::ValueID::LibwebPaletteInactiveSelectionText:
  1036. return palette.color(ColorRole::InactiveSelectionText);
  1037. case CSS::ValueID::LibwebPaletteRubberBandFill:
  1038. return palette.color(ColorRole::RubberBandFill);
  1039. case CSS::ValueID::LibwebPaletteRubberBandBorder:
  1040. return palette.color(ColorRole::RubberBandBorder);
  1041. case CSS::ValueID::LibwebPaletteLink:
  1042. return palette.color(ColorRole::Link);
  1043. case CSS::ValueID::LibwebPaletteActiveLink:
  1044. return palette.color(ColorRole::ActiveLink);
  1045. case CSS::ValueID::LibwebPaletteVisitedLink:
  1046. return palette.color(ColorRole::VisitedLink);
  1047. case CSS::ValueID::LibwebPaletteRuler:
  1048. return palette.color(ColorRole::Ruler);
  1049. case CSS::ValueID::LibwebPaletteRulerBorder:
  1050. return palette.color(ColorRole::RulerBorder);
  1051. case CSS::ValueID::LibwebPaletteRulerActiveText:
  1052. return palette.color(ColorRole::RulerActiveText);
  1053. case CSS::ValueID::LibwebPaletteRulerInactiveText:
  1054. return palette.color(ColorRole::RulerInactiveText);
  1055. case CSS::ValueID::LibwebPaletteTextCursor:
  1056. return palette.color(ColorRole::TextCursor);
  1057. case CSS::ValueID::LibwebPaletteFocusOutline:
  1058. return palette.color(ColorRole::FocusOutline);
  1059. case CSS::ValueID::LibwebPaletteSyntaxComment:
  1060. return palette.color(ColorRole::SyntaxComment);
  1061. case CSS::ValueID::LibwebPaletteSyntaxNumber:
  1062. return palette.color(ColorRole::SyntaxNumber);
  1063. case CSS::ValueID::LibwebPaletteSyntaxString:
  1064. return palette.color(ColorRole::SyntaxString);
  1065. case CSS::ValueID::LibwebPaletteSyntaxType:
  1066. return palette.color(ColorRole::SyntaxType);
  1067. case CSS::ValueID::LibwebPaletteSyntaxPunctuation:
  1068. return palette.color(ColorRole::SyntaxPunctuation);
  1069. case CSS::ValueID::LibwebPaletteSyntaxOperator:
  1070. return palette.color(ColorRole::SyntaxOperator);
  1071. case CSS::ValueID::LibwebPaletteSyntaxKeyword:
  1072. return palette.color(ColorRole::SyntaxKeyword);
  1073. case CSS::ValueID::LibwebPaletteSyntaxControlKeyword:
  1074. return palette.color(ColorRole::SyntaxControlKeyword);
  1075. case CSS::ValueID::LibwebPaletteSyntaxIdentifier:
  1076. return palette.color(ColorRole::SyntaxIdentifier);
  1077. case CSS::ValueID::LibwebPaletteSyntaxPreprocessorStatement:
  1078. return palette.color(ColorRole::SyntaxPreprocessorStatement);
  1079. case CSS::ValueID::LibwebPaletteSyntaxPreprocessorValue:
  1080. return palette.color(ColorRole::SyntaxPreprocessorValue);
  1081. default:
  1082. return {};
  1083. }
  1084. }
  1085. ImageStyleValue::ImageStyleValue(AK::URL const& url)
  1086. : StyleValue(Type::Image)
  1087. , m_url(url)
  1088. {
  1089. }
  1090. void ImageStyleValue::load_bitmap(DOM::Document& document)
  1091. {
  1092. if (m_bitmap)
  1093. return;
  1094. m_document = &document;
  1095. auto request = LoadRequest::create_for_url_on_page(m_url, document.page());
  1096. set_resource(ResourceLoader::the().load_resource(Resource::Type::Image, request));
  1097. }
  1098. void ImageStyleValue::resource_did_load()
  1099. {
  1100. if (!m_document)
  1101. return;
  1102. m_bitmap = resource()->bitmap();
  1103. // FIXME: Do less than a full repaint if possible?
  1104. if (m_document && m_document->browsing_context())
  1105. m_document->browsing_context()->set_needs_display({});
  1106. }
  1107. String ImageStyleValue::to_string() const
  1108. {
  1109. return serialize_a_url(m_url.to_string());
  1110. }
  1111. String ListStyleStyleValue::to_string() const
  1112. {
  1113. return String::formatted("{} {} {}", m_position->to_string(), m_image->to_string(), m_style_type->to_string());
  1114. }
  1115. String NumericStyleValue::to_string() const
  1116. {
  1117. return m_value.visit(
  1118. [](float value) {
  1119. return String::formatted("{}", value);
  1120. },
  1121. [](i64 value) {
  1122. return String::formatted("{}", value);
  1123. });
  1124. }
  1125. String OverflowStyleValue::to_string() const
  1126. {
  1127. return String::formatted("{} {}", m_overflow_x->to_string(), m_overflow_y->to_string());
  1128. }
  1129. String PercentageStyleValue::to_string() const
  1130. {
  1131. return m_percentage.to_string();
  1132. }
  1133. String PositionStyleValue::to_string() const
  1134. {
  1135. auto to_string = [](PositionEdge edge) {
  1136. switch (edge) {
  1137. case PositionEdge::Left:
  1138. return "left";
  1139. case PositionEdge::Right:
  1140. return "right";
  1141. case PositionEdge::Top:
  1142. return "top";
  1143. case PositionEdge::Bottom:
  1144. return "bottom";
  1145. }
  1146. VERIFY_NOT_REACHED();
  1147. };
  1148. return String::formatted("{} {} {} {}", to_string(m_edge_x), m_offset_x.to_string(), to_string(m_edge_y), m_offset_y.to_string());
  1149. }
  1150. String TextDecorationStyleValue::to_string() const
  1151. {
  1152. return String::formatted("{} {} {}", m_line->to_string(), m_style->to_string(), m_color->to_string());
  1153. }
  1154. String TransformationStyleValue::to_string() const
  1155. {
  1156. StringBuilder builder;
  1157. switch (m_transform_function) {
  1158. case TransformFunction::TranslateY:
  1159. builder.append("translateY");
  1160. break;
  1161. default:
  1162. VERIFY_NOT_REACHED();
  1163. }
  1164. builder.append('(');
  1165. builder.join(", ", m_values);
  1166. builder.append(')');
  1167. return builder.to_string();
  1168. }
  1169. String UnresolvedStyleValue::to_string() const
  1170. {
  1171. StringBuilder builder;
  1172. for (auto& value : m_values)
  1173. builder.append(value.to_string());
  1174. return builder.to_string();
  1175. }
  1176. String StyleValueList::to_string() const
  1177. {
  1178. String separator = "";
  1179. switch (m_separator) {
  1180. case Separator::Space:
  1181. separator = " ";
  1182. break;
  1183. case Separator::Comma:
  1184. separator = ", ";
  1185. break;
  1186. default:
  1187. VERIFY_NOT_REACHED();
  1188. }
  1189. return String::join(separator, m_values);
  1190. }
  1191. NonnullRefPtr<ColorStyleValue> ColorStyleValue::create(Color color)
  1192. {
  1193. if (color.value() == 0) {
  1194. static auto transparent = adopt_ref(*new ColorStyleValue(color));
  1195. return transparent;
  1196. }
  1197. if (color == Color::from_rgb(0x000000)) {
  1198. static auto black = adopt_ref(*new ColorStyleValue(color));
  1199. return black;
  1200. }
  1201. if (color == Color::from_rgb(0xffffff)) {
  1202. static auto white = adopt_ref(*new ColorStyleValue(color));
  1203. return white;
  1204. }
  1205. return adopt_ref(*new ColorStyleValue(color));
  1206. }
  1207. NonnullRefPtr<LengthStyleValue> LengthStyleValue::create(Length const& length)
  1208. {
  1209. if (length.is_auto()) {
  1210. static auto value = adopt_ref(*new LengthStyleValue(CSS::Length::make_auto()));
  1211. return value;
  1212. }
  1213. if (length.is_px()) {
  1214. if (length.raw_value() == 0) {
  1215. static auto value = adopt_ref(*new LengthStyleValue(CSS::Length::make_px(0)));
  1216. return value;
  1217. }
  1218. if (length.raw_value() == 1) {
  1219. static auto value = adopt_ref(*new LengthStyleValue(CSS::Length::make_px(1)));
  1220. return value;
  1221. }
  1222. }
  1223. return adopt_ref(*new LengthStyleValue(length));
  1224. }
  1225. }