StyleValue.cpp 44 KB

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