StyleValue.cpp 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  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. return String::formatted("{} {} {} {}", m_offset_x.to_string(), m_offset_y.to_string(), m_blur_radius.to_string(), m_color.to_string());
  232. }
  233. void CalculatedStyleValue::CalculationResult::add(CalculationResult const& other, Layout::Node const* layout_node, Length const& percentage_basis)
  234. {
  235. add_or_subtract_internal(SumOperation::Add, other, layout_node, percentage_basis);
  236. }
  237. void CalculatedStyleValue::CalculationResult::subtract(CalculationResult const& other, Layout::Node const* layout_node, Length const& percentage_basis)
  238. {
  239. add_or_subtract_internal(SumOperation::Subtract, other, layout_node, percentage_basis);
  240. }
  241. void CalculatedStyleValue::CalculationResult::add_or_subtract_internal(SumOperation op, CalculationResult const& other, Layout::Node const* layout_node, Length const& percentage_basis)
  242. {
  243. // 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>".
  244. // Though, having the same type may mean that one side is a <dimension> and the other a <percentage>.
  245. // Note: This is almost identical to ::add()
  246. m_value.visit(
  247. [&](float f) {
  248. if (op == SumOperation::Add)
  249. m_value = f + other.m_value.get<float>();
  250. else
  251. m_value = f - other.m_value.get<float>();
  252. },
  253. [&](Length const& length) {
  254. auto this_px = length.to_px(*layout_node);
  255. if (other.m_value.has<Length>()) {
  256. auto other_px = other.m_value.get<Length>().to_px(*layout_node);
  257. if (op == SumOperation::Add)
  258. m_value = Length::make_px(this_px + other_px);
  259. else
  260. m_value = Length::make_px(this_px - other_px);
  261. } else {
  262. VERIFY(!percentage_basis.is_undefined());
  263. auto other_px = percentage_basis.percentage_of(other.m_value.get<Percentage>()).to_px(*layout_node);
  264. if (op == SumOperation::Add)
  265. m_value = Length::make_px(this_px + other_px);
  266. else
  267. m_value = Length::make_px(this_px - other_px);
  268. }
  269. },
  270. [&](Percentage const& percentage) {
  271. if (other.m_value.has<Percentage>()) {
  272. if (op == SumOperation::Add)
  273. m_value = Percentage { percentage.value() + other.m_value.get<Percentage>().value() };
  274. else
  275. m_value = Percentage { percentage.value() - other.m_value.get<Percentage>().value() };
  276. return;
  277. }
  278. // 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`.
  279. CalculationResult new_value = other;
  280. if (op == SumOperation::Add)
  281. new_value.add(*this, layout_node, percentage_basis);
  282. else
  283. new_value.subtract(*this, layout_node, percentage_basis);
  284. *this = new_value;
  285. });
  286. }
  287. void CalculatedStyleValue::CalculationResult::multiply_by(CalculationResult const& other, Layout::Node const* layout_node)
  288. {
  289. // We know from validation when resolving the type, that at least one side must be a <number> or <integer>.
  290. // Both of these are represented as a float.
  291. VERIFY(m_value.has<float>() || other.m_value.has<float>());
  292. bool other_is_number = other.m_value.has<float>();
  293. m_value.visit(
  294. [&](float f) {
  295. if (other_is_number) {
  296. m_value = f * other.m_value.get<float>();
  297. } else {
  298. // Avoid duplicating all the logic by swapping `this` and `other`.
  299. CalculationResult new_value = other;
  300. new_value.multiply_by(*this, layout_node);
  301. *this = new_value;
  302. }
  303. },
  304. [&](Length const& length) {
  305. VERIFY(layout_node);
  306. m_value = Length::make_px(length.to_px(*layout_node) * other.m_value.get<float>());
  307. },
  308. [&](Percentage const& percentage) {
  309. m_value = Percentage { percentage.value() * other.m_value.get<float>() };
  310. });
  311. }
  312. void CalculatedStyleValue::CalculationResult::divide_by(CalculationResult const& other, Layout::Node const* layout_node)
  313. {
  314. // We know from validation when resolving the type, that `other` must be a <number> or <integer>.
  315. // Both of these are represented as a float.
  316. float denominator = other.m_value.get<float>();
  317. // FIXME: Dividing by 0 is invalid, and should be caught during parsing.
  318. VERIFY(denominator != 0.0f);
  319. m_value.visit(
  320. [&](float f) {
  321. m_value = f / denominator;
  322. },
  323. [&](Length const& length) {
  324. VERIFY(layout_node);
  325. m_value = Length::make_px(length.to_px(*layout_node) / denominator);
  326. },
  327. [&](Percentage const& percentage) {
  328. m_value = Percentage { percentage.value() / denominator };
  329. });
  330. }
  331. Optional<Length> CalculatedStyleValue::resolve_length(Layout::Node const& layout_node) const
  332. {
  333. auto result = m_expression->resolve(&layout_node, {});
  334. return result.value().visit(
  335. [&](float) -> Optional<Length> {
  336. return {};
  337. },
  338. [&](Length const& length) -> Optional<Length> {
  339. return length;
  340. },
  341. [&](Percentage const&) -> Optional<Length> {
  342. return {};
  343. });
  344. }
  345. Optional<LengthPercentage> CalculatedStyleValue::resolve_length_percentage(Layout::Node const& layout_node, Length const& percentage_basis) const
  346. {
  347. VERIFY(!percentage_basis.is_undefined());
  348. auto result = m_expression->resolve(&layout_node, percentage_basis);
  349. return result.value().visit(
  350. [&](float) -> Optional<LengthPercentage> {
  351. return {};
  352. },
  353. [&](Length const& length) -> Optional<LengthPercentage> {
  354. return length;
  355. },
  356. [&](Percentage const& percentage) -> Optional<LengthPercentage> {
  357. return percentage;
  358. });
  359. }
  360. Optional<Percentage> CalculatedStyleValue::resolve_percentage() const
  361. {
  362. auto result = m_expression->resolve(nullptr, {});
  363. if (result.value().has<Percentage>())
  364. return result.value().get<Percentage>();
  365. return {};
  366. }
  367. Optional<float> CalculatedStyleValue::resolve_number()
  368. {
  369. auto result = m_expression->resolve(nullptr, {});
  370. if (result.value().has<float>())
  371. return result.value().get<float>();
  372. return {};
  373. }
  374. Optional<i64> CalculatedStyleValue::resolve_integer()
  375. {
  376. auto result = m_expression->resolve(nullptr, {});
  377. if (result.value().has<float>())
  378. return lroundf(result.value().get<float>());
  379. return {};
  380. }
  381. static bool is_number(CalculatedStyleValue::ResolvedType type)
  382. {
  383. return type == CalculatedStyleValue::ResolvedType::Number || type == CalculatedStyleValue::ResolvedType::Integer;
  384. }
  385. static bool is_dimension(CalculatedStyleValue::ResolvedType type)
  386. {
  387. return type != CalculatedStyleValue::ResolvedType::Number
  388. && type != CalculatedStyleValue::ResolvedType::Integer
  389. && type != CalculatedStyleValue::ResolvedType::Percentage;
  390. }
  391. template<typename SumWithOperator>
  392. static Optional<CalculatedStyleValue::ResolvedType> resolve_sum_type(CalculatedStyleValue::ResolvedType first_type, NonnullOwnPtrVector<SumWithOperator> const& zero_or_more_additional_products)
  393. {
  394. auto type = first_type;
  395. for (auto const& product : zero_or_more_additional_products) {
  396. auto maybe_product_type = product.resolved_type();
  397. if (!maybe_product_type.has_value())
  398. return {};
  399. auto product_type = maybe_product_type.value();
  400. // At + or -, check that both sides have the same type, or that one side is a <number> and the other is an <integer>.
  401. // If both sides are the same type, resolve to that type.
  402. if (product_type == type)
  403. continue;
  404. // If one side is a <number> and the other is an <integer>, resolve to <number>.
  405. if (is_number(type) && is_number(product_type)) {
  406. type = CalculatedStyleValue::ResolvedType::Number;
  407. continue;
  408. }
  409. // FIXME: calc() handles <percentage> by allowing them to pretend to be whatever <dimension> type is allowed at this location.
  410. // Since we can't easily check what that type is, we just allow <percentage> to combine with any other <dimension> type.
  411. if (type == CalculatedStyleValue::ResolvedType::Percentage && is_dimension(product_type)) {
  412. type = product_type;
  413. continue;
  414. }
  415. if (is_dimension(type) && product_type == CalculatedStyleValue::ResolvedType::Percentage)
  416. continue;
  417. return {};
  418. }
  419. return type;
  420. }
  421. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcSum::resolved_type() const
  422. {
  423. auto maybe_type = first_calc_product->resolved_type();
  424. if (!maybe_type.has_value())
  425. return {};
  426. auto type = maybe_type.value();
  427. return resolve_sum_type(type, zero_or_more_additional_calc_products);
  428. }
  429. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcNumberSum::resolved_type() const
  430. {
  431. auto maybe_type = first_calc_number_product->resolved_type();
  432. if (!maybe_type.has_value())
  433. return {};
  434. auto type = maybe_type.value();
  435. return resolve_sum_type(type, zero_or_more_additional_calc_number_products);
  436. }
  437. template<typename ProductWithOperator>
  438. static Optional<CalculatedStyleValue::ResolvedType> resolve_product_type(CalculatedStyleValue::ResolvedType first_type, NonnullOwnPtrVector<ProductWithOperator> const& zero_or_more_additional_values)
  439. {
  440. auto type = first_type;
  441. for (auto const& value : zero_or_more_additional_values) {
  442. auto maybe_value_type = value.resolved_type();
  443. if (!maybe_value_type.has_value())
  444. return {};
  445. auto value_type = maybe_value_type.value();
  446. if (value.op == CalculatedStyleValue::ProductOperation::Multiply) {
  447. // At *, check that at least one side is <number>.
  448. if (!(is_number(type) || is_number(value_type)))
  449. return {};
  450. // If both sides are <integer>, resolve to <integer>.
  451. if (type == CalculatedStyleValue::ResolvedType::Integer && value_type == CalculatedStyleValue::ResolvedType::Integer) {
  452. type = CalculatedStyleValue::ResolvedType::Integer;
  453. } else {
  454. // Otherwise, resolve to the type of the other side.
  455. if (is_number(type))
  456. type = value_type;
  457. }
  458. continue;
  459. } else {
  460. VERIFY(value.op == CalculatedStyleValue::ProductOperation::Divide);
  461. // At /, check that the right side is <number>.
  462. if (!is_number(value_type))
  463. return {};
  464. // If the left side is <integer>, resolve to <number>.
  465. if (type == CalculatedStyleValue::ResolvedType::Integer) {
  466. type = CalculatedStyleValue::ResolvedType::Number;
  467. } else {
  468. // Otherwise, resolve to the type of the left side.
  469. }
  470. // FIXME: Division by zero makes the whole calc() expression invalid.
  471. }
  472. }
  473. return type;
  474. }
  475. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcProduct::resolved_type() const
  476. {
  477. auto maybe_type = first_calc_value.resolved_type();
  478. if (!maybe_type.has_value())
  479. return {};
  480. auto type = maybe_type.value();
  481. return resolve_product_type(type, zero_or_more_additional_calc_values);
  482. }
  483. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcSumPartWithOperator::resolved_type() const
  484. {
  485. return value->resolved_type();
  486. }
  487. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcNumberProduct::resolved_type() const
  488. {
  489. auto maybe_type = first_calc_number_value.resolved_type();
  490. if (!maybe_type.has_value())
  491. return {};
  492. auto type = maybe_type.value();
  493. return resolve_product_type(type, zero_or_more_additional_calc_number_values);
  494. }
  495. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcNumberProductPartWithOperator::resolved_type() const
  496. {
  497. return value.resolved_type();
  498. }
  499. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcNumberSumPartWithOperator::resolved_type() const
  500. {
  501. return value->resolved_type();
  502. }
  503. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcProductPartWithOperator::resolved_type() const
  504. {
  505. return value.visit(
  506. [](CalcValue const& calc_value) {
  507. return calc_value.resolved_type();
  508. },
  509. [](CalcNumberValue const& calc_number_value) {
  510. return calc_number_value.resolved_type();
  511. });
  512. }
  513. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcValue::resolved_type() const
  514. {
  515. return value.visit(
  516. [](float) -> Optional<CalculatedStyleValue::ResolvedType> { return { ResolvedType::Number }; },
  517. [](Length const&) -> Optional<CalculatedStyleValue::ResolvedType> { return { ResolvedType::Length }; },
  518. [](Percentage const&) -> Optional<CalculatedStyleValue::ResolvedType> { return { ResolvedType::Percentage }; },
  519. [](NonnullOwnPtr<CalcSum> const& sum) { return sum->resolved_type(); });
  520. }
  521. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcNumberValue::resolved_type() const
  522. {
  523. return value.visit(
  524. [](float) -> Optional<CalculatedStyleValue::ResolvedType> { return { ResolvedType::Number }; },
  525. [](NonnullOwnPtr<CalcNumberSum> const& sum) { return sum->resolved_type(); });
  526. }
  527. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcNumberValue::resolve(Layout::Node const* layout_node, Length const& percentage_basis) const
  528. {
  529. return value.visit(
  530. [&](float f) -> CalculatedStyleValue::CalculationResult {
  531. return CalculatedStyleValue::CalculationResult { f };
  532. },
  533. [&](NonnullOwnPtr<CalcNumberSum> const& sum) -> CalculatedStyleValue::CalculationResult {
  534. return sum->resolve(layout_node, percentage_basis);
  535. });
  536. }
  537. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcValue::resolve(Layout::Node const* layout_node, Length const& percentage_basis) const
  538. {
  539. return value.visit(
  540. [&](float f) -> CalculatedStyleValue::CalculationResult {
  541. return CalculatedStyleValue::CalculationResult { f };
  542. },
  543. [&](Length const& length) -> CalculatedStyleValue::CalculationResult {
  544. return CalculatedStyleValue::CalculationResult { length };
  545. },
  546. [&](Percentage const& percentage) -> CalculatedStyleValue::CalculationResult {
  547. return CalculatedStyleValue::CalculationResult { percentage };
  548. },
  549. [&](NonnullOwnPtr<CalcSum> const& sum) -> CalculatedStyleValue::CalculationResult {
  550. return sum->resolve(layout_node, percentage_basis);
  551. });
  552. }
  553. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcSum::resolve(Layout::Node const* layout_node, Length const& percentage_basis) const
  554. {
  555. auto value = first_calc_product->resolve(layout_node, percentage_basis);
  556. for (auto& additional_product : zero_or_more_additional_calc_products) {
  557. auto additional_value = additional_product.resolve(layout_node, percentage_basis);
  558. if (additional_product.op == CalculatedStyleValue::SumOperation::Add)
  559. value.add(additional_value, layout_node, percentage_basis);
  560. else if (additional_product.op == CalculatedStyleValue::SumOperation::Subtract)
  561. value.subtract(additional_value, layout_node, percentage_basis);
  562. else
  563. VERIFY_NOT_REACHED();
  564. }
  565. return value;
  566. }
  567. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcNumberSum::resolve(Layout::Node const* layout_node, Length const& percentage_basis) const
  568. {
  569. auto value = first_calc_number_product->resolve(layout_node, percentage_basis);
  570. for (auto& additional_product : zero_or_more_additional_calc_number_products) {
  571. auto additional_value = additional_product.resolve(layout_node, percentage_basis);
  572. if (additional_product.op == CSS::CalculatedStyleValue::SumOperation::Add)
  573. value.add(additional_value, layout_node, percentage_basis);
  574. else if (additional_product.op == CalculatedStyleValue::SumOperation::Subtract)
  575. value.subtract(additional_value, layout_node, percentage_basis);
  576. else
  577. VERIFY_NOT_REACHED();
  578. }
  579. return value;
  580. }
  581. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcProduct::resolve(Layout::Node const* layout_node, Length const& percentage_basis) const
  582. {
  583. auto value = first_calc_value.resolve(layout_node, percentage_basis);
  584. for (auto& additional_value : zero_or_more_additional_calc_values) {
  585. additional_value.value.visit(
  586. [&](CalculatedStyleValue::CalcValue const& calc_value) {
  587. VERIFY(additional_value.op == CalculatedStyleValue::ProductOperation::Multiply);
  588. auto resolved_value = calc_value.resolve(layout_node, percentage_basis);
  589. value.multiply_by(resolved_value, layout_node);
  590. },
  591. [&](CalculatedStyleValue::CalcNumberValue const& calc_number_value) {
  592. VERIFY(additional_value.op == CalculatedStyleValue::ProductOperation::Divide);
  593. auto resolved_calc_number_value = calc_number_value.resolve(layout_node, percentage_basis);
  594. value.divide_by(resolved_calc_number_value, layout_node);
  595. });
  596. }
  597. return value;
  598. }
  599. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcNumberProduct::resolve(Layout::Node const* layout_node, Length const& percentage_basis) const
  600. {
  601. auto value = first_calc_number_value.resolve(layout_node, percentage_basis);
  602. for (auto& additional_number_value : zero_or_more_additional_calc_number_values) {
  603. auto additional_value = additional_number_value.resolve(layout_node, percentage_basis);
  604. if (additional_number_value.op == CalculatedStyleValue::ProductOperation::Multiply)
  605. value.multiply_by(additional_value, layout_node);
  606. else if (additional_number_value.op == CalculatedStyleValue::ProductOperation::Divide)
  607. value.divide_by(additional_value, layout_node);
  608. else
  609. VERIFY_NOT_REACHED();
  610. }
  611. return value;
  612. }
  613. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcProductPartWithOperator::resolve(Layout::Node const* layout_node, Length const& percentage_basis) const
  614. {
  615. return value.visit(
  616. [&](CalcValue const& calc_value) {
  617. return calc_value.resolve(layout_node, percentage_basis);
  618. },
  619. [&](CalcNumberValue const& calc_number_value) {
  620. return calc_number_value.resolve(layout_node, percentage_basis);
  621. });
  622. }
  623. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcSumPartWithOperator::resolve(Layout::Node const* layout_node, Length const& percentage_basis) const
  624. {
  625. return value->resolve(layout_node, percentage_basis);
  626. }
  627. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcNumberProductPartWithOperator::resolve(Layout::Node const* layout_node, Length const& percentage_basis) const
  628. {
  629. return value.resolve(layout_node, percentage_basis);
  630. }
  631. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcNumberSumPartWithOperator::resolve(Layout::Node const* layout_node, Length const& percentage_basis) const
  632. {
  633. return value->resolve(layout_node, percentage_basis);
  634. }
  635. // https://www.w3.org/TR/css-color-4/#serializing-sRGB-values
  636. String ColorStyleValue::to_string() const
  637. {
  638. if (m_color.alpha() == 1)
  639. return String::formatted("rgb({}, {}, {})", m_color.red(), m_color.green(), m_color.blue());
  640. return String::formatted("rgba({}, {}, {}, {})", m_color.red(), m_color.green(), m_color.blue(), (float)(m_color.alpha()) / 255.0f);
  641. }
  642. String CombinedBorderRadiusStyleValue::to_string() const
  643. {
  644. 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());
  645. }
  646. String FlexStyleValue::to_string() const
  647. {
  648. return String::formatted("{} {} {}", m_grow->to_string(), m_shrink->to_string(), m_basis->to_string());
  649. }
  650. String FlexFlowStyleValue::to_string() const
  651. {
  652. return String::formatted("{} {}", m_flex_direction->to_string(), m_flex_wrap->to_string());
  653. }
  654. String FontStyleValue::to_string() const
  655. {
  656. 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());
  657. }
  658. String IdentifierStyleValue::to_string() const
  659. {
  660. return CSS::string_from_value_id(m_id);
  661. }
  662. bool IdentifierStyleValue::has_color() const
  663. {
  664. switch (m_id) {
  665. case ValueID::Currentcolor:
  666. case ValueID::LibwebLink:
  667. case ValueID::LibwebPaletteActiveLink:
  668. case ValueID::LibwebPaletteActiveWindowBorder1:
  669. case ValueID::LibwebPaletteActiveWindowBorder2:
  670. case ValueID::LibwebPaletteActiveWindowTitle:
  671. case ValueID::LibwebPaletteBase:
  672. case ValueID::LibwebPaletteBaseText:
  673. case ValueID::LibwebPaletteButton:
  674. case ValueID::LibwebPaletteButtonText:
  675. case ValueID::LibwebPaletteDesktopBackground:
  676. case ValueID::LibwebPaletteFocusOutline:
  677. case ValueID::LibwebPaletteHighlightWindowBorder1:
  678. case ValueID::LibwebPaletteHighlightWindowBorder2:
  679. case ValueID::LibwebPaletteHighlightWindowTitle:
  680. case ValueID::LibwebPaletteHoverHighlight:
  681. case ValueID::LibwebPaletteInactiveSelection:
  682. case ValueID::LibwebPaletteInactiveSelectionText:
  683. case ValueID::LibwebPaletteInactiveWindowBorder1:
  684. case ValueID::LibwebPaletteInactiveWindowBorder2:
  685. case ValueID::LibwebPaletteInactiveWindowTitle:
  686. case ValueID::LibwebPaletteLink:
  687. case ValueID::LibwebPaletteMenuBase:
  688. case ValueID::LibwebPaletteMenuBaseText:
  689. case ValueID::LibwebPaletteMenuSelection:
  690. case ValueID::LibwebPaletteMenuSelectionText:
  691. case ValueID::LibwebPaletteMenuStripe:
  692. case ValueID::LibwebPaletteMovingWindowBorder1:
  693. case ValueID::LibwebPaletteMovingWindowBorder2:
  694. case ValueID::LibwebPaletteMovingWindowTitle:
  695. case ValueID::LibwebPaletteRubberBandBorder:
  696. case ValueID::LibwebPaletteRubberBandFill:
  697. case ValueID::LibwebPaletteRuler:
  698. case ValueID::LibwebPaletteRulerActiveText:
  699. case ValueID::LibwebPaletteRulerBorder:
  700. case ValueID::LibwebPaletteRulerInactiveText:
  701. case ValueID::LibwebPaletteSelection:
  702. case ValueID::LibwebPaletteSelectionText:
  703. case ValueID::LibwebPaletteSyntaxComment:
  704. case ValueID::LibwebPaletteSyntaxControlKeyword:
  705. case ValueID::LibwebPaletteSyntaxIdentifier:
  706. case ValueID::LibwebPaletteSyntaxKeyword:
  707. case ValueID::LibwebPaletteSyntaxNumber:
  708. case ValueID::LibwebPaletteSyntaxOperator:
  709. case ValueID::LibwebPaletteSyntaxPreprocessorStatement:
  710. case ValueID::LibwebPaletteSyntaxPreprocessorValue:
  711. case ValueID::LibwebPaletteSyntaxPunctuation:
  712. case ValueID::LibwebPaletteSyntaxString:
  713. case ValueID::LibwebPaletteSyntaxType:
  714. case ValueID::LibwebPaletteTextCursor:
  715. case ValueID::LibwebPaletteThreedHighlight:
  716. case ValueID::LibwebPaletteThreedShadow1:
  717. case ValueID::LibwebPaletteThreedShadow2:
  718. case ValueID::LibwebPaletteVisitedLink:
  719. case ValueID::LibwebPaletteWindow:
  720. case ValueID::LibwebPaletteWindowText:
  721. return true;
  722. default:
  723. return false;
  724. }
  725. }
  726. Color IdentifierStyleValue::to_color(Layout::NodeWithStyle const& node) const
  727. {
  728. if (id() == CSS::ValueID::Currentcolor) {
  729. if (!node.has_style())
  730. return Color::Black;
  731. return node.computed_values().color();
  732. }
  733. auto& document = node.document();
  734. if (id() == CSS::ValueID::LibwebLink)
  735. return document.link_color();
  736. VERIFY(document.page());
  737. auto palette = document.page()->palette();
  738. switch (id()) {
  739. case CSS::ValueID::LibwebPaletteDesktopBackground:
  740. return palette.color(ColorRole::DesktopBackground);
  741. case CSS::ValueID::LibwebPaletteActiveWindowBorder1:
  742. return palette.color(ColorRole::ActiveWindowBorder1);
  743. case CSS::ValueID::LibwebPaletteActiveWindowBorder2:
  744. return palette.color(ColorRole::ActiveWindowBorder2);
  745. case CSS::ValueID::LibwebPaletteActiveWindowTitle:
  746. return palette.color(ColorRole::ActiveWindowTitle);
  747. case CSS::ValueID::LibwebPaletteInactiveWindowBorder1:
  748. return palette.color(ColorRole::InactiveWindowBorder1);
  749. case CSS::ValueID::LibwebPaletteInactiveWindowBorder2:
  750. return palette.color(ColorRole::InactiveWindowBorder2);
  751. case CSS::ValueID::LibwebPaletteInactiveWindowTitle:
  752. return palette.color(ColorRole::InactiveWindowTitle);
  753. case CSS::ValueID::LibwebPaletteMovingWindowBorder1:
  754. return palette.color(ColorRole::MovingWindowBorder1);
  755. case CSS::ValueID::LibwebPaletteMovingWindowBorder2:
  756. return palette.color(ColorRole::MovingWindowBorder2);
  757. case CSS::ValueID::LibwebPaletteMovingWindowTitle:
  758. return palette.color(ColorRole::MovingWindowTitle);
  759. case CSS::ValueID::LibwebPaletteHighlightWindowBorder1:
  760. return palette.color(ColorRole::HighlightWindowBorder1);
  761. case CSS::ValueID::LibwebPaletteHighlightWindowBorder2:
  762. return palette.color(ColorRole::HighlightWindowBorder2);
  763. case CSS::ValueID::LibwebPaletteHighlightWindowTitle:
  764. return palette.color(ColorRole::HighlightWindowTitle);
  765. case CSS::ValueID::LibwebPaletteMenuStripe:
  766. return palette.color(ColorRole::MenuStripe);
  767. case CSS::ValueID::LibwebPaletteMenuBase:
  768. return palette.color(ColorRole::MenuBase);
  769. case CSS::ValueID::LibwebPaletteMenuBaseText:
  770. return palette.color(ColorRole::MenuBaseText);
  771. case CSS::ValueID::LibwebPaletteMenuSelection:
  772. return palette.color(ColorRole::MenuSelection);
  773. case CSS::ValueID::LibwebPaletteMenuSelectionText:
  774. return palette.color(ColorRole::MenuSelectionText);
  775. case CSS::ValueID::LibwebPaletteWindow:
  776. return palette.color(ColorRole::Window);
  777. case CSS::ValueID::LibwebPaletteWindowText:
  778. return palette.color(ColorRole::WindowText);
  779. case CSS::ValueID::LibwebPaletteButton:
  780. return palette.color(ColorRole::Button);
  781. case CSS::ValueID::LibwebPaletteButtonText:
  782. return palette.color(ColorRole::ButtonText);
  783. case CSS::ValueID::LibwebPaletteBase:
  784. return palette.color(ColorRole::Base);
  785. case CSS::ValueID::LibwebPaletteBaseText:
  786. return palette.color(ColorRole::BaseText);
  787. case CSS::ValueID::LibwebPaletteThreedHighlight:
  788. return palette.color(ColorRole::ThreedHighlight);
  789. case CSS::ValueID::LibwebPaletteThreedShadow1:
  790. return palette.color(ColorRole::ThreedShadow1);
  791. case CSS::ValueID::LibwebPaletteThreedShadow2:
  792. return palette.color(ColorRole::ThreedShadow2);
  793. case CSS::ValueID::LibwebPaletteHoverHighlight:
  794. return palette.color(ColorRole::HoverHighlight);
  795. case CSS::ValueID::LibwebPaletteSelection:
  796. return palette.color(ColorRole::Selection);
  797. case CSS::ValueID::LibwebPaletteSelectionText:
  798. return palette.color(ColorRole::SelectionText);
  799. case CSS::ValueID::LibwebPaletteInactiveSelection:
  800. return palette.color(ColorRole::InactiveSelection);
  801. case CSS::ValueID::LibwebPaletteInactiveSelectionText:
  802. return palette.color(ColorRole::InactiveSelectionText);
  803. case CSS::ValueID::LibwebPaletteRubberBandFill:
  804. return palette.color(ColorRole::RubberBandFill);
  805. case CSS::ValueID::LibwebPaletteRubberBandBorder:
  806. return palette.color(ColorRole::RubberBandBorder);
  807. case CSS::ValueID::LibwebPaletteLink:
  808. return palette.color(ColorRole::Link);
  809. case CSS::ValueID::LibwebPaletteActiveLink:
  810. return palette.color(ColorRole::ActiveLink);
  811. case CSS::ValueID::LibwebPaletteVisitedLink:
  812. return palette.color(ColorRole::VisitedLink);
  813. case CSS::ValueID::LibwebPaletteRuler:
  814. return palette.color(ColorRole::Ruler);
  815. case CSS::ValueID::LibwebPaletteRulerBorder:
  816. return palette.color(ColorRole::RulerBorder);
  817. case CSS::ValueID::LibwebPaletteRulerActiveText:
  818. return palette.color(ColorRole::RulerActiveText);
  819. case CSS::ValueID::LibwebPaletteRulerInactiveText:
  820. return palette.color(ColorRole::RulerInactiveText);
  821. case CSS::ValueID::LibwebPaletteTextCursor:
  822. return palette.color(ColorRole::TextCursor);
  823. case CSS::ValueID::LibwebPaletteFocusOutline:
  824. return palette.color(ColorRole::FocusOutline);
  825. case CSS::ValueID::LibwebPaletteSyntaxComment:
  826. return palette.color(ColorRole::SyntaxComment);
  827. case CSS::ValueID::LibwebPaletteSyntaxNumber:
  828. return palette.color(ColorRole::SyntaxNumber);
  829. case CSS::ValueID::LibwebPaletteSyntaxString:
  830. return palette.color(ColorRole::SyntaxString);
  831. case CSS::ValueID::LibwebPaletteSyntaxType:
  832. return palette.color(ColorRole::SyntaxType);
  833. case CSS::ValueID::LibwebPaletteSyntaxPunctuation:
  834. return palette.color(ColorRole::SyntaxPunctuation);
  835. case CSS::ValueID::LibwebPaletteSyntaxOperator:
  836. return palette.color(ColorRole::SyntaxOperator);
  837. case CSS::ValueID::LibwebPaletteSyntaxKeyword:
  838. return palette.color(ColorRole::SyntaxKeyword);
  839. case CSS::ValueID::LibwebPaletteSyntaxControlKeyword:
  840. return palette.color(ColorRole::SyntaxControlKeyword);
  841. case CSS::ValueID::LibwebPaletteSyntaxIdentifier:
  842. return palette.color(ColorRole::SyntaxIdentifier);
  843. case CSS::ValueID::LibwebPaletteSyntaxPreprocessorStatement:
  844. return palette.color(ColorRole::SyntaxPreprocessorStatement);
  845. case CSS::ValueID::LibwebPaletteSyntaxPreprocessorValue:
  846. return palette.color(ColorRole::SyntaxPreprocessorValue);
  847. default:
  848. return {};
  849. }
  850. }
  851. ImageStyleValue::ImageStyleValue(AK::URL const& url)
  852. : StyleValue(Type::Image)
  853. , m_url(url)
  854. {
  855. }
  856. void ImageStyleValue::load_bitmap(DOM::Document& document)
  857. {
  858. if (m_bitmap)
  859. return;
  860. m_document = &document;
  861. auto request = LoadRequest::create_for_url_on_page(m_url, document.page());
  862. set_resource(ResourceLoader::the().load_resource(Resource::Type::Image, request));
  863. }
  864. void ImageStyleValue::resource_did_load()
  865. {
  866. if (!m_document)
  867. return;
  868. m_bitmap = resource()->bitmap();
  869. // FIXME: Do less than a full repaint if possible?
  870. if (m_document && m_document->browsing_context())
  871. m_document->browsing_context()->set_needs_display({});
  872. }
  873. String ImageStyleValue::to_string() const
  874. {
  875. return serialize_a_url(m_url.to_string());
  876. }
  877. String ListStyleStyleValue::to_string() const
  878. {
  879. return String::formatted("{} {} {}", m_position->to_string(), m_image->to_string(), m_style_type->to_string());
  880. }
  881. String NumericStyleValue::to_string() const
  882. {
  883. return m_value.visit(
  884. [](float value) {
  885. return String::formatted("{}", value);
  886. },
  887. [](i64 value) {
  888. return String::formatted("{}", value);
  889. });
  890. }
  891. String OverflowStyleValue::to_string() const
  892. {
  893. return String::formatted("{} {}", m_overflow_x->to_string(), m_overflow_y->to_string());
  894. }
  895. String PercentageStyleValue::to_string() const
  896. {
  897. return m_percentage.to_string();
  898. }
  899. String PositionStyleValue::to_string() const
  900. {
  901. auto to_string = [](PositionEdge edge) {
  902. switch (edge) {
  903. case PositionEdge::Left:
  904. return "left";
  905. case PositionEdge::Right:
  906. return "right";
  907. case PositionEdge::Top:
  908. return "top";
  909. case PositionEdge::Bottom:
  910. return "bottom";
  911. }
  912. VERIFY_NOT_REACHED();
  913. };
  914. return String::formatted("{} {} {} {}", to_string(m_edge_x), m_offset_x.to_string(), to_string(m_edge_y), m_offset_y.to_string());
  915. }
  916. String TextDecorationStyleValue::to_string() const
  917. {
  918. return String::formatted("{} {} {}", m_line->to_string(), m_style->to_string(), m_color->to_string());
  919. }
  920. String TransformationStyleValue::to_string() const
  921. {
  922. StringBuilder builder;
  923. switch (m_transform_function) {
  924. case TransformFunction::TranslateY:
  925. builder.append("translateY");
  926. break;
  927. default:
  928. VERIFY_NOT_REACHED();
  929. }
  930. builder.append('(');
  931. builder.join(", ", m_values);
  932. builder.append(')');
  933. return builder.to_string();
  934. }
  935. String UnresolvedStyleValue::to_string() const
  936. {
  937. StringBuilder builder;
  938. for (auto& value : m_values)
  939. builder.append(value.to_string());
  940. return builder.to_string();
  941. }
  942. String StyleValueList::to_string() const
  943. {
  944. String separator = "";
  945. switch (m_separator) {
  946. case Separator::Space:
  947. separator = " ";
  948. break;
  949. case Separator::Comma:
  950. separator = ", ";
  951. break;
  952. default:
  953. VERIFY_NOT_REACHED();
  954. }
  955. return String::join(separator, m_values);
  956. }
  957. }