StyleValue.cpp 54 KB

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