StyleValue.cpp 55 KB

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