StyleValue.cpp 44 KB

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