StyleValue.cpp 49 KB

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