StyleValue.cpp 47 KB

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