StyleValue.cpp 55 KB

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