StyleValue.cpp 49 KB

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