ComputedProperties.cpp 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498
  1. /*
  2. * Copyright (c) 2018-2024, Andreas Kling <andreas@ladybird.org>
  3. * Copyright (c) 2021-2024, Sam Atkins <sam@ladybird.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/NonnullRawPtr.h>
  8. #include <AK/TypeCasts.h>
  9. #include <LibCore/DirIterator.h>
  10. #include <LibGC/CellAllocator.h>
  11. #include <LibWeb/CSS/Clip.h>
  12. #include <LibWeb/CSS/ComputedProperties.h>
  13. #include <LibWeb/CSS/StyleValues/AngleStyleValue.h>
  14. #include <LibWeb/CSS/StyleValues/CSSKeywordValue.h>
  15. #include <LibWeb/CSS/StyleValues/ContentStyleValue.h>
  16. #include <LibWeb/CSS/StyleValues/CounterDefinitionsStyleValue.h>
  17. #include <LibWeb/CSS/StyleValues/CounterStyleValue.h>
  18. #include <LibWeb/CSS/StyleValues/DisplayStyleValue.h>
  19. #include <LibWeb/CSS/StyleValues/GridAutoFlowStyleValue.h>
  20. #include <LibWeb/CSS/StyleValues/GridTemplateAreaStyleValue.h>
  21. #include <LibWeb/CSS/StyleValues/GridTrackPlacementStyleValue.h>
  22. #include <LibWeb/CSS/StyleValues/GridTrackSizeListStyleValue.h>
  23. #include <LibWeb/CSS/StyleValues/IntegerStyleValue.h>
  24. #include <LibWeb/CSS/StyleValues/LengthStyleValue.h>
  25. #include <LibWeb/CSS/StyleValues/MathDepthStyleValue.h>
  26. #include <LibWeb/CSS/StyleValues/NumberStyleValue.h>
  27. #include <LibWeb/CSS/StyleValues/OpenTypeTaggedStyleValue.h>
  28. #include <LibWeb/CSS/StyleValues/PercentageStyleValue.h>
  29. #include <LibWeb/CSS/StyleValues/PositionStyleValue.h>
  30. #include <LibWeb/CSS/StyleValues/RectStyleValue.h>
  31. #include <LibWeb/CSS/StyleValues/RotationStyleValue.h>
  32. #include <LibWeb/CSS/StyleValues/ScaleStyleValue.h>
  33. #include <LibWeb/CSS/StyleValues/ScrollbarGutterStyleValue.h>
  34. #include <LibWeb/CSS/StyleValues/ShadowStyleValue.h>
  35. #include <LibWeb/CSS/StyleValues/StringStyleValue.h>
  36. #include <LibWeb/CSS/StyleValues/StyleValueList.h>
  37. #include <LibWeb/CSS/StyleValues/TransformationStyleValue.h>
  38. #include <LibWeb/CSS/StyleValues/TranslationStyleValue.h>
  39. #include <LibWeb/Layout/BlockContainer.h>
  40. #include <LibWeb/Layout/Node.h>
  41. #include <LibWeb/Platform/FontPlugin.h>
  42. namespace Web::CSS {
  43. GC_DEFINE_ALLOCATOR(ComputedProperties);
  44. ComputedProperties::ComputedProperties() = default;
  45. ComputedProperties::~ComputedProperties() = default;
  46. void ComputedProperties::visit_edges(Visitor& visitor)
  47. {
  48. Base::visit_edges(visitor);
  49. visitor.visit(m_animation_name_source);
  50. visitor.visit(m_transition_property_source);
  51. }
  52. bool ComputedProperties::is_property_important(CSS::PropertyID property_id) const
  53. {
  54. size_t n = to_underlying(property_id);
  55. return m_property_important[n / 8] & (1 << (n % 8));
  56. }
  57. void ComputedProperties::set_property_important(CSS::PropertyID property_id, Important important)
  58. {
  59. size_t n = to_underlying(property_id);
  60. if (important == Important::Yes)
  61. m_property_important[n / 8] |= (1 << (n % 8));
  62. else
  63. m_property_important[n / 8] &= ~(1 << (n % 8));
  64. }
  65. bool ComputedProperties::is_property_inherited(CSS::PropertyID property_id) const
  66. {
  67. size_t n = to_underlying(property_id);
  68. return m_property_inherited[n / 8] & (1 << (n % 8));
  69. }
  70. void ComputedProperties::set_property_inherited(CSS::PropertyID property_id, Inherited inherited)
  71. {
  72. size_t n = to_underlying(property_id);
  73. if (inherited == Inherited::Yes)
  74. m_property_inherited[n / 8] |= (1 << (n % 8));
  75. else
  76. m_property_inherited[n / 8] &= ~(1 << (n % 8));
  77. }
  78. void ComputedProperties::set_property(CSS::PropertyID id, NonnullRefPtr<CSSStyleValue const> value, Inherited inherited, Important important)
  79. {
  80. m_property_values[to_underlying(id)] = move(value);
  81. set_property_important(id, important);
  82. set_property_inherited(id, inherited);
  83. }
  84. void ComputedProperties::revert_property(CSS::PropertyID id, ComputedProperties const& style_for_revert)
  85. {
  86. m_property_values[to_underlying(id)] = style_for_revert.m_property_values[to_underlying(id)];
  87. set_property_important(id, style_for_revert.is_property_important(id) ? Important::Yes : Important::No);
  88. set_property_inherited(id, style_for_revert.is_property_inherited(id) ? Inherited::Yes : Inherited::No);
  89. }
  90. void ComputedProperties::set_animated_property(CSS::PropertyID id, NonnullRefPtr<CSSStyleValue const> value)
  91. {
  92. m_animated_property_values.set(id, move(value));
  93. }
  94. void ComputedProperties::reset_animated_properties()
  95. {
  96. m_animated_property_values.clear();
  97. }
  98. CSSStyleValue const& ComputedProperties::property(CSS::PropertyID property_id, WithAnimationsApplied return_animated_value) const
  99. {
  100. if (return_animated_value == WithAnimationsApplied::Yes) {
  101. if (auto animated_value = m_animated_property_values.get(property_id); animated_value.has_value())
  102. return *animated_value.value();
  103. }
  104. // By the time we call this method, all properties have values assigned.
  105. return *m_property_values[to_underlying(property_id)];
  106. }
  107. CSSStyleValue const* ComputedProperties::maybe_null_property(CSS::PropertyID property_id) const
  108. {
  109. if (auto animated_value = m_animated_property_values.get(property_id); animated_value.has_value())
  110. return animated_value.value();
  111. return m_property_values[to_underlying(property_id)];
  112. }
  113. Variant<LengthPercentage, NormalGap> ComputedProperties::gap_value(CSS::PropertyID id) const
  114. {
  115. auto const& value = property(id);
  116. if (value.is_keyword()) {
  117. VERIFY(value.as_keyword().keyword() == CSS::Keyword::Normal);
  118. return NormalGap {};
  119. }
  120. if (value.is_calculated())
  121. return LengthPercentage { value.as_calculated() };
  122. if (value.is_percentage())
  123. return LengthPercentage { value.as_percentage().percentage() };
  124. if (value.is_length())
  125. return LengthPercentage { value.as_length().length() };
  126. VERIFY_NOT_REACHED();
  127. }
  128. CSS::Size ComputedProperties::size_value(CSS::PropertyID id) const
  129. {
  130. auto const& value = property(id);
  131. if (value.is_keyword()) {
  132. switch (value.to_keyword()) {
  133. case Keyword::Auto:
  134. return CSS::Size::make_auto();
  135. case Keyword::MinContent:
  136. return CSS::Size::make_min_content();
  137. case Keyword::MaxContent:
  138. return CSS::Size::make_max_content();
  139. case Keyword::FitContent:
  140. return CSS::Size::make_fit_content();
  141. case Keyword::None:
  142. return CSS::Size::make_none();
  143. default:
  144. VERIFY_NOT_REACHED();
  145. }
  146. }
  147. if (value.is_calculated())
  148. return CSS::Size::make_calculated(value.as_calculated());
  149. if (value.is_percentage())
  150. return CSS::Size::make_percentage(value.as_percentage().percentage());
  151. if (value.is_length()) {
  152. auto length = value.as_length().length();
  153. if (length.is_auto())
  154. return CSS::Size::make_auto();
  155. return CSS::Size::make_length(length);
  156. }
  157. // FIXME: Support `fit-content(<length>)`
  158. dbgln("FIXME: Unsupported size value: `{}`, treating as `auto`", value.to_string(CSSStyleValue::SerializationMode::Normal));
  159. return CSS::Size::make_auto();
  160. }
  161. LengthPercentage ComputedProperties::length_percentage_or_fallback(CSS::PropertyID id, LengthPercentage const& fallback) const
  162. {
  163. return length_percentage(id).value_or(fallback);
  164. }
  165. Optional<LengthPercentage> ComputedProperties::length_percentage(CSS::PropertyID id) const
  166. {
  167. auto const& value = property(id);
  168. if (value.is_calculated())
  169. return LengthPercentage { value.as_calculated() };
  170. if (value.is_percentage())
  171. return value.as_percentage().percentage();
  172. if (value.is_length())
  173. return value.as_length().length();
  174. if (value.has_auto())
  175. return LengthPercentage { Length::make_auto() };
  176. return {};
  177. }
  178. LengthBox ComputedProperties::length_box(CSS::PropertyID left_id, CSS::PropertyID top_id, CSS::PropertyID right_id, CSS::PropertyID bottom_id, const CSS::Length& default_value) const
  179. {
  180. LengthBox box;
  181. box.left() = length_percentage_or_fallback(left_id, default_value);
  182. box.top() = length_percentage_or_fallback(top_id, default_value);
  183. box.right() = length_percentage_or_fallback(right_id, default_value);
  184. box.bottom() = length_percentage_or_fallback(bottom_id, default_value);
  185. return box;
  186. }
  187. Color ComputedProperties::color_or_fallback(CSS::PropertyID id, Layout::NodeWithStyle const& node, Color fallback) const
  188. {
  189. auto const& value = property(id);
  190. if (!value.has_color())
  191. return fallback;
  192. return value.to_color(node);
  193. }
  194. NonnullRefPtr<Gfx::Font const> ComputedProperties::font_fallback(bool monospace, bool bold)
  195. {
  196. if (monospace && bold)
  197. return Platform::FontPlugin::the().default_fixed_width_font().bold_variant();
  198. if (monospace)
  199. return Platform::FontPlugin::the().default_fixed_width_font();
  200. if (bold)
  201. return Platform::FontPlugin::the().default_font().bold_variant();
  202. return Platform::FontPlugin::the().default_font();
  203. }
  204. CSSPixels ComputedProperties::compute_line_height(CSSPixelRect const& viewport_rect, Length::FontMetrics const& font_metrics, Length::FontMetrics const& root_font_metrics) const
  205. {
  206. auto const& line_height = property(CSS::PropertyID::LineHeight);
  207. if (line_height.is_keyword() && line_height.to_keyword() == Keyword::Normal)
  208. return font_metrics.line_height;
  209. if (line_height.is_length()) {
  210. auto line_height_length = line_height.as_length().length();
  211. if (!line_height_length.is_auto())
  212. return line_height_length.to_px(viewport_rect, font_metrics, root_font_metrics);
  213. }
  214. if (line_height.is_number())
  215. return Length(line_height.as_number().number(), Length::Type::Em).to_px(viewport_rect, font_metrics, root_font_metrics);
  216. if (line_height.is_percentage()) {
  217. // Percentages are relative to 1em. https://www.w3.org/TR/css-inline-3/#valdef-line-height-percentage
  218. auto const& percentage = line_height.as_percentage().percentage();
  219. return Length(percentage.as_fraction(), Length::Type::Em).to_px(viewport_rect, font_metrics, root_font_metrics);
  220. }
  221. if (line_height.is_calculated()) {
  222. if (line_height.as_calculated().resolves_to_number()) {
  223. auto resolved = line_height.as_calculated().resolve_number();
  224. if (!resolved.has_value()) {
  225. dbgln("FIXME: Failed to resolve calc() line-height (number): {}", line_height.as_calculated().to_string(CSSStyleValue::SerializationMode::Normal));
  226. return CSSPixels::nearest_value_for(m_font_list->first().pixel_metrics().line_spacing());
  227. }
  228. return Length(resolved.value(), Length::Type::Em).to_px(viewport_rect, font_metrics, root_font_metrics);
  229. }
  230. auto resolved = line_height.as_calculated().resolve_length(Length::ResolutionContext { viewport_rect, font_metrics, root_font_metrics });
  231. if (!resolved.has_value()) {
  232. dbgln("FIXME: Failed to resolve calc() line-height: {}", line_height.as_calculated().to_string(CSSStyleValue::SerializationMode::Normal));
  233. return CSSPixels::nearest_value_for(m_font_list->first().pixel_metrics().line_spacing());
  234. }
  235. return resolved->to_px(viewport_rect, font_metrics, root_font_metrics);
  236. }
  237. return font_metrics.line_height;
  238. }
  239. Optional<int> ComputedProperties::z_index() const
  240. {
  241. auto const& value = property(CSS::PropertyID::ZIndex);
  242. if (value.has_auto())
  243. return {};
  244. if (value.is_integer()) {
  245. // Clamp z-index to the range of a signed 32-bit integer for consistency with other engines.
  246. auto integer = value.as_integer().integer();
  247. if (integer >= NumericLimits<int>::max())
  248. return NumericLimits<int>::max();
  249. if (integer <= NumericLimits<int>::min())
  250. return NumericLimits<int>::min();
  251. return static_cast<int>(integer);
  252. }
  253. return {};
  254. }
  255. float ComputedProperties::resolve_opacity_value(CSSStyleValue const& value)
  256. {
  257. float unclamped_opacity = 1.0f;
  258. if (value.is_number()) {
  259. unclamped_opacity = value.as_number().number();
  260. } else if (value.is_calculated()) {
  261. auto const& calculated = value.as_calculated();
  262. if (calculated.resolves_to_percentage()) {
  263. auto maybe_percentage = value.as_calculated().resolve_percentage();
  264. if (maybe_percentage.has_value())
  265. unclamped_opacity = maybe_percentage->as_fraction();
  266. else
  267. dbgln("Unable to resolve calc() as opacity (percentage): {}", value.to_string(CSSStyleValue::SerializationMode::Normal));
  268. } else if (calculated.resolves_to_number()) {
  269. auto maybe_number = value.as_calculated().resolve_number();
  270. if (maybe_number.has_value())
  271. unclamped_opacity = maybe_number.value();
  272. else
  273. dbgln("Unable to resolve calc() as opacity (number): {}", value.to_string(CSSStyleValue::SerializationMode::Normal));
  274. }
  275. } else if (value.is_percentage()) {
  276. unclamped_opacity = value.as_percentage().percentage().as_fraction();
  277. }
  278. return clamp(unclamped_opacity, 0.0f, 1.0f);
  279. }
  280. float ComputedProperties::opacity() const
  281. {
  282. auto const& value = property(CSS::PropertyID::Opacity);
  283. return resolve_opacity_value(value);
  284. }
  285. float ComputedProperties::fill_opacity() const
  286. {
  287. auto const& value = property(CSS::PropertyID::FillOpacity);
  288. return resolve_opacity_value(value);
  289. }
  290. Optional<CSS::StrokeLinecap> ComputedProperties::stroke_linecap() const
  291. {
  292. auto const& value = property(CSS::PropertyID::StrokeLinecap);
  293. return keyword_to_stroke_linecap(value.to_keyword());
  294. }
  295. Optional<CSS::StrokeLinejoin> ComputedProperties::stroke_linejoin() const
  296. {
  297. auto const& value = property(CSS::PropertyID::StrokeLinejoin);
  298. return keyword_to_stroke_linejoin(value.to_keyword());
  299. }
  300. NumberOrCalculated ComputedProperties::stroke_miterlimit() const
  301. {
  302. auto const& value = property(CSS::PropertyID::StrokeMiterlimit);
  303. if (value.is_calculated()) {
  304. auto const& math_value = value.as_calculated();
  305. VERIFY(math_value.resolves_to_number());
  306. return NumberOrCalculated { math_value };
  307. }
  308. return NumberOrCalculated { value.as_number().number() };
  309. }
  310. float ComputedProperties::stroke_opacity() const
  311. {
  312. auto const& value = property(CSS::PropertyID::StrokeOpacity);
  313. return resolve_opacity_value(value);
  314. }
  315. float ComputedProperties::stop_opacity() const
  316. {
  317. auto const& value = property(CSS::PropertyID::StopOpacity);
  318. return resolve_opacity_value(value);
  319. }
  320. Optional<CSS::FillRule> ComputedProperties::fill_rule() const
  321. {
  322. auto const& value = property(CSS::PropertyID::FillRule);
  323. return keyword_to_fill_rule(value.to_keyword());
  324. }
  325. Optional<CSS::ClipRule> ComputedProperties::clip_rule() const
  326. {
  327. auto const& value = property(CSS::PropertyID::ClipRule);
  328. return keyword_to_fill_rule(value.to_keyword());
  329. }
  330. Optional<CSS::FlexDirection> ComputedProperties::flex_direction() const
  331. {
  332. auto const& value = property(CSS::PropertyID::FlexDirection);
  333. return keyword_to_flex_direction(value.to_keyword());
  334. }
  335. Optional<CSS::FlexWrap> ComputedProperties::flex_wrap() const
  336. {
  337. auto const& value = property(CSS::PropertyID::FlexWrap);
  338. return keyword_to_flex_wrap(value.to_keyword());
  339. }
  340. Optional<CSS::FlexBasis> ComputedProperties::flex_basis() const
  341. {
  342. auto const& value = property(CSS::PropertyID::FlexBasis);
  343. if (value.is_keyword() && value.to_keyword() == CSS::Keyword::Content)
  344. return CSS::FlexBasisContent {};
  345. return size_value(CSS::PropertyID::FlexBasis);
  346. }
  347. float ComputedProperties::flex_grow() const
  348. {
  349. auto const& value = property(CSS::PropertyID::FlexGrow);
  350. if (!value.is_number())
  351. return 0;
  352. return value.as_number().number();
  353. }
  354. float ComputedProperties::flex_shrink() const
  355. {
  356. auto const& value = property(CSS::PropertyID::FlexShrink);
  357. if (!value.is_number())
  358. return 1;
  359. return value.as_number().number();
  360. }
  361. int ComputedProperties::order() const
  362. {
  363. auto const& value = property(CSS::PropertyID::Order);
  364. if (!value.is_integer())
  365. return 0;
  366. return value.as_integer().integer();
  367. }
  368. Optional<CSS::ImageRendering> ComputedProperties::image_rendering() const
  369. {
  370. auto const& value = property(CSS::PropertyID::ImageRendering);
  371. return keyword_to_image_rendering(value.to_keyword());
  372. }
  373. CSS::Length ComputedProperties::border_spacing_horizontal(Layout::Node const& layout_node) const
  374. {
  375. auto const& value = property(CSS::PropertyID::BorderSpacing);
  376. if (value.is_length())
  377. return value.as_length().length();
  378. if (value.is_calculated())
  379. return value.as_calculated().resolve_length(layout_node).value_or(CSS::Length(0, CSS::Length::Type::Px));
  380. auto const& list = value.as_value_list();
  381. return list.value_at(0, false)->as_length().length();
  382. }
  383. CSS::Length ComputedProperties::border_spacing_vertical(Layout::Node const& layout_node) const
  384. {
  385. auto const& value = property(CSS::PropertyID::BorderSpacing);
  386. if (value.is_length())
  387. return value.as_length().length();
  388. if (value.is_calculated())
  389. return value.as_calculated().resolve_length(layout_node).value_or(CSS::Length(0, CSS::Length::Type::Px));
  390. auto const& list = value.as_value_list();
  391. return list.value_at(1, false)->as_length().length();
  392. }
  393. Optional<CSS::CaptionSide> ComputedProperties::caption_side() const
  394. {
  395. auto const& value = property(CSS::PropertyID::CaptionSide);
  396. return keyword_to_caption_side(value.to_keyword());
  397. }
  398. CSS::Clip ComputedProperties::clip() const
  399. {
  400. auto const& value = property(CSS::PropertyID::Clip);
  401. if (!value.is_rect())
  402. return CSS::Clip::make_auto();
  403. return CSS::Clip(value.as_rect().rect());
  404. }
  405. Optional<CSS::JustifyContent> ComputedProperties::justify_content() const
  406. {
  407. auto const& value = property(CSS::PropertyID::JustifyContent);
  408. return keyword_to_justify_content(value.to_keyword());
  409. }
  410. Optional<CSS::JustifyItems> ComputedProperties::justify_items() const
  411. {
  412. auto const& value = property(CSS::PropertyID::JustifyItems);
  413. return keyword_to_justify_items(value.to_keyword());
  414. }
  415. Optional<CSS::JustifySelf> ComputedProperties::justify_self() const
  416. {
  417. auto const& value = property(CSS::PropertyID::JustifySelf);
  418. return keyword_to_justify_self(value.to_keyword());
  419. }
  420. Vector<CSS::Transformation> ComputedProperties::transformations_for_style_value(CSSStyleValue const& value)
  421. {
  422. if (value.is_keyword() && value.to_keyword() == CSS::Keyword::None)
  423. return {};
  424. if (!value.is_value_list())
  425. return {};
  426. auto& list = value.as_value_list();
  427. Vector<CSS::Transformation> transformations;
  428. for (auto& it : list.values()) {
  429. if (!it->is_transformation())
  430. return {};
  431. auto& transformation_style_value = it->as_transformation();
  432. auto function = transformation_style_value.transform_function();
  433. auto function_metadata = transform_function_metadata(function);
  434. Vector<TransformValue> values;
  435. size_t argument_index = 0;
  436. for (auto& transformation_value : transformation_style_value.values()) {
  437. if (transformation_value->is_calculated()) {
  438. auto& calculated = transformation_value->as_calculated();
  439. if (calculated.resolves_to_length_percentage()) {
  440. values.append(CSS::LengthPercentage { calculated });
  441. } else if (calculated.resolves_to_percentage()) {
  442. // FIXME: Maybe transform this for loop to always check the metadata for the correct types
  443. if (function_metadata.parameters[argument_index].type == TransformFunctionParameterType::NumberPercentage) {
  444. values.append(NumberPercentage { calculated.resolve_percentage().value() });
  445. } else {
  446. values.append(LengthPercentage { calculated.resolve_percentage().value() });
  447. }
  448. } else if (calculated.resolves_to_number()) {
  449. values.append({ Number(Number::Type::Number, calculated.resolve_number().value()) });
  450. } else if (calculated.resolves_to_angle()) {
  451. values.append({ calculated.resolve_angle().value() });
  452. } else {
  453. dbgln("FIXME: Unsupported calc value in transform! {}", calculated.to_string(CSSStyleValue::SerializationMode::Normal));
  454. }
  455. } else if (transformation_value->is_length()) {
  456. values.append({ transformation_value->as_length().length() });
  457. } else if (transformation_value->is_percentage()) {
  458. if (function_metadata.parameters[argument_index].type == TransformFunctionParameterType::NumberPercentage) {
  459. values.append(NumberPercentage { transformation_value->as_percentage().percentage() });
  460. } else {
  461. values.append(LengthPercentage { transformation_value->as_percentage().percentage() });
  462. }
  463. } else if (transformation_value->is_number()) {
  464. values.append({ Number(Number::Type::Number, transformation_value->as_number().number()) });
  465. } else if (transformation_value->is_angle()) {
  466. values.append({ transformation_value->as_angle().angle() });
  467. } else {
  468. dbgln("FIXME: Unsupported value in transform! {}", transformation_value->to_string(CSSStyleValue::SerializationMode::Normal));
  469. }
  470. argument_index++;
  471. }
  472. transformations.empend(function, move(values));
  473. }
  474. return transformations;
  475. }
  476. Vector<CSS::Transformation> ComputedProperties::transformations() const
  477. {
  478. return transformations_for_style_value(property(CSS::PropertyID::Transform));
  479. }
  480. Optional<CSS::Transformation> ComputedProperties::rotate(Layout::Node const& layout_node) const
  481. {
  482. auto const& value = property(CSS::PropertyID::Rotate);
  483. if (!value.is_rotation())
  484. return {};
  485. auto& rotation = value.as_rotation();
  486. auto resolve_angle = [&layout_node](CSSStyleValue const& value) -> Optional<Angle> {
  487. if (value.is_angle())
  488. return value.as_angle().angle();
  489. if (value.is_calculated() && value.as_calculated().resolves_to_angle())
  490. return value.as_calculated().resolve_angle(layout_node);
  491. return {};
  492. };
  493. auto resolve_number = [&](CSSStyleValue const& value) -> Optional<double> {
  494. if (value.is_number())
  495. return value.as_number().number();
  496. if (value.is_calculated() && value.as_calculated().resolves_to_number())
  497. return value.as_calculated().resolve_number();
  498. return {};
  499. };
  500. auto x = resolve_number(rotation.rotation_x()).value_or(0);
  501. auto y = resolve_number(rotation.rotation_y()).value_or(0);
  502. auto z = resolve_number(rotation.rotation_z()).value_or(0);
  503. auto angle = resolve_angle(rotation.angle()).value_or(Angle::make_degrees(0));
  504. Vector<TransformValue> values;
  505. values.append({ Number(Number::Type::Number, x) });
  506. values.append({ Number(Number::Type::Number, y) });
  507. values.append({ Number(Number::Type::Number, z) });
  508. values.append({ angle });
  509. return CSS::Transformation(CSS::TransformFunction::Rotate3d, move(values));
  510. }
  511. Optional<CSS::Transformation> ComputedProperties::translate() const
  512. {
  513. auto const& value = property(CSS::PropertyID::Translate);
  514. if (!value.is_translation())
  515. return {};
  516. auto const& translation = value.as_translation();
  517. Vector<TransformValue> values;
  518. values.append(translation.x());
  519. values.append(translation.y());
  520. return CSS::Transformation(CSS::TransformFunction::Translate, move(values));
  521. }
  522. Optional<CSS::Transformation> ComputedProperties::scale() const
  523. {
  524. auto const& value = property(CSS::PropertyID::Scale);
  525. if (!value.is_scale())
  526. return {};
  527. auto const& scale = value.as_scale();
  528. Vector<TransformValue> values;
  529. values.append(scale.x());
  530. values.append(scale.y());
  531. return CSS::Transformation(CSS::TransformFunction::Scale, move(values));
  532. }
  533. static Optional<LengthPercentage> length_percentage_for_style_value(CSSStyleValue const& value)
  534. {
  535. if (value.is_length())
  536. return value.as_length().length();
  537. if (value.is_percentage())
  538. return value.as_percentage().percentage();
  539. if (value.is_calculated())
  540. return LengthPercentage { value.as_calculated() };
  541. return {};
  542. }
  543. Optional<CSS::TransformBox> ComputedProperties::transform_box() const
  544. {
  545. auto const& value = property(CSS::PropertyID::TransformBox);
  546. return keyword_to_transform_box(value.to_keyword());
  547. }
  548. CSS::TransformOrigin ComputedProperties::transform_origin() const
  549. {
  550. auto const& value = property(CSS::PropertyID::TransformOrigin);
  551. if (!value.is_value_list() || value.as_value_list().size() != 2)
  552. return {};
  553. auto const& list = value.as_value_list();
  554. auto x_value = length_percentage_for_style_value(list.values()[0]);
  555. auto y_value = length_percentage_for_style_value(list.values()[1]);
  556. if (!x_value.has_value() || !y_value.has_value()) {
  557. return {};
  558. }
  559. return { x_value.value(), y_value.value() };
  560. }
  561. Optional<Color> ComputedProperties::accent_color(Layout::NodeWithStyle const& node) const
  562. {
  563. auto const& value = property(CSS::PropertyID::AccentColor);
  564. if (value.has_color())
  565. return value.to_color(node);
  566. return {};
  567. }
  568. Optional<CSS::AlignContent> ComputedProperties::align_content() const
  569. {
  570. auto const& value = property(CSS::PropertyID::AlignContent);
  571. return keyword_to_align_content(value.to_keyword());
  572. }
  573. Optional<CSS::AlignItems> ComputedProperties::align_items() const
  574. {
  575. auto const& value = property(CSS::PropertyID::AlignItems);
  576. return keyword_to_align_items(value.to_keyword());
  577. }
  578. Optional<CSS::AlignSelf> ComputedProperties::align_self() const
  579. {
  580. auto const& value = property(CSS::PropertyID::AlignSelf);
  581. return keyword_to_align_self(value.to_keyword());
  582. }
  583. Optional<CSS::Appearance> ComputedProperties::appearance() const
  584. {
  585. auto const& value = property(CSS::PropertyID::Appearance);
  586. auto appearance = keyword_to_appearance(value.to_keyword());
  587. if (appearance.has_value()) {
  588. switch (*appearance) {
  589. // Note: All these compatibility values can be treated as 'auto'
  590. case CSS::Appearance::Textfield:
  591. case CSS::Appearance::MenulistButton:
  592. case CSS::Appearance::Searchfield:
  593. case CSS::Appearance::Textarea:
  594. case CSS::Appearance::PushButton:
  595. case CSS::Appearance::SliderHorizontal:
  596. case CSS::Appearance::Checkbox:
  597. case CSS::Appearance::Radio:
  598. case CSS::Appearance::SquareButton:
  599. case CSS::Appearance::Menulist:
  600. case CSS::Appearance::Listbox:
  601. case CSS::Appearance::Meter:
  602. case CSS::Appearance::ProgressBar:
  603. case CSS::Appearance::Button:
  604. appearance = CSS::Appearance::Auto;
  605. break;
  606. default:
  607. break;
  608. }
  609. }
  610. return appearance;
  611. }
  612. CSS::Filter ComputedProperties::backdrop_filter() const
  613. {
  614. auto const& value = property(CSS::PropertyID::BackdropFilter);
  615. if (value.is_filter_value_list())
  616. return Filter(value.as_filter_value_list());
  617. return Filter::make_none();
  618. }
  619. CSS::Filter ComputedProperties::filter() const
  620. {
  621. auto const& value = property(CSS::PropertyID::Filter);
  622. if (value.is_filter_value_list())
  623. return Filter(value.as_filter_value_list());
  624. return Filter::make_none();
  625. }
  626. Optional<CSS::Positioning> ComputedProperties::position() const
  627. {
  628. auto const& value = property(CSS::PropertyID::Position);
  629. return keyword_to_positioning(value.to_keyword());
  630. }
  631. bool ComputedProperties::operator==(ComputedProperties const& other) const
  632. {
  633. if (m_property_values.size() != other.m_property_values.size())
  634. return false;
  635. for (size_t i = 0; i < m_property_values.size(); ++i) {
  636. auto const& my_style = m_property_values[i];
  637. auto const& other_style = other.m_property_values[i];
  638. if (!my_style) {
  639. if (other_style)
  640. return false;
  641. continue;
  642. }
  643. if (!other_style)
  644. return false;
  645. auto const& my_value = *my_style;
  646. auto const& other_value = *other_style;
  647. if (my_value.type() != other_value.type())
  648. return false;
  649. if (my_value != other_value)
  650. return false;
  651. }
  652. return true;
  653. }
  654. Optional<CSS::TextAnchor> ComputedProperties::text_anchor() const
  655. {
  656. auto const& value = property(CSS::PropertyID::TextAnchor);
  657. return keyword_to_text_anchor(value.to_keyword());
  658. }
  659. Optional<CSS::TextAlign> ComputedProperties::text_align() const
  660. {
  661. auto const& value = property(CSS::PropertyID::TextAlign);
  662. return keyword_to_text_align(value.to_keyword());
  663. }
  664. Optional<CSS::TextJustify> ComputedProperties::text_justify() const
  665. {
  666. auto const& value = property(CSS::PropertyID::TextJustify);
  667. return keyword_to_text_justify(value.to_keyword());
  668. }
  669. Optional<CSS::TextOverflow> ComputedProperties::text_overflow() const
  670. {
  671. auto const& value = property(CSS::PropertyID::TextOverflow);
  672. return keyword_to_text_overflow(value.to_keyword());
  673. }
  674. Optional<CSS::PointerEvents> ComputedProperties::pointer_events() const
  675. {
  676. auto const& value = property(CSS::PropertyID::PointerEvents);
  677. return keyword_to_pointer_events(value.to_keyword());
  678. }
  679. Variant<LengthOrCalculated, NumberOrCalculated> ComputedProperties::tab_size() const
  680. {
  681. auto const& value = property(CSS::PropertyID::TabSize);
  682. if (value.is_calculated()) {
  683. auto const& math_value = value.as_calculated();
  684. if (math_value.resolves_to_length()) {
  685. return LengthOrCalculated { math_value };
  686. }
  687. if (math_value.resolves_to_number()) {
  688. return NumberOrCalculated { math_value };
  689. }
  690. }
  691. if (value.is_length())
  692. return LengthOrCalculated { value.as_length().length() };
  693. return NumberOrCalculated { value.as_number().number() };
  694. }
  695. Optional<CSS::WordBreak> ComputedProperties::word_break() const
  696. {
  697. auto const& value = property(CSS::PropertyID::WordBreak);
  698. return keyword_to_word_break(value.to_keyword());
  699. }
  700. Optional<CSS::LengthOrCalculated> ComputedProperties::word_spacing() const
  701. {
  702. auto const& value = property(CSS::PropertyID::WordSpacing);
  703. if (value.is_calculated()) {
  704. auto& math_value = value.as_calculated();
  705. if (math_value.resolves_to_length()) {
  706. return LengthOrCalculated { math_value };
  707. }
  708. }
  709. if (value.is_length())
  710. return LengthOrCalculated { value.as_length().length() };
  711. return {};
  712. }
  713. Optional<CSS::WhiteSpace> ComputedProperties::white_space() const
  714. {
  715. auto const& value = property(CSS::PropertyID::WhiteSpace);
  716. return keyword_to_white_space(value.to_keyword());
  717. }
  718. Optional<LengthOrCalculated> ComputedProperties::letter_spacing() const
  719. {
  720. auto const& value = property(CSS::PropertyID::LetterSpacing);
  721. if (value.is_calculated()) {
  722. auto const& math_value = value.as_calculated();
  723. if (math_value.resolves_to_length()) {
  724. return LengthOrCalculated { math_value };
  725. }
  726. }
  727. if (value.is_length())
  728. return LengthOrCalculated { value.as_length().length() };
  729. return {};
  730. }
  731. Optional<CSS::LineStyle> ComputedProperties::line_style(CSS::PropertyID property_id) const
  732. {
  733. auto const& value = property(property_id);
  734. return keyword_to_line_style(value.to_keyword());
  735. }
  736. Optional<CSS::OutlineStyle> ComputedProperties::outline_style() const
  737. {
  738. auto const& value = property(CSS::PropertyID::OutlineStyle);
  739. return keyword_to_outline_style(value.to_keyword());
  740. }
  741. Optional<CSS::Float> ComputedProperties::float_() const
  742. {
  743. auto const& value = property(CSS::PropertyID::Float);
  744. return keyword_to_float(value.to_keyword());
  745. }
  746. Optional<CSS::Clear> ComputedProperties::clear() const
  747. {
  748. auto const& value = property(CSS::PropertyID::Clear);
  749. return keyword_to_clear(value.to_keyword());
  750. }
  751. Optional<CSS::ColumnSpan> ComputedProperties::column_span() const
  752. {
  753. auto const& value = property(CSS::PropertyID::ColumnSpan);
  754. return keyword_to_column_span(value.to_keyword());
  755. }
  756. ComputedProperties::ContentDataAndQuoteNestingLevel ComputedProperties::content(DOM::Element& element, u32 initial_quote_nesting_level) const
  757. {
  758. auto const& value = property(CSS::PropertyID::Content);
  759. auto quotes_data = quotes();
  760. auto quote_nesting_level = initial_quote_nesting_level;
  761. auto get_quote_string = [&](bool open, auto depth) {
  762. switch (quotes_data.type) {
  763. case QuotesData::Type::None:
  764. return FlyString {};
  765. case QuotesData::Type::Auto:
  766. // FIXME: "A typographically appropriate used value for quotes is automatically chosen by the UA
  767. // based on the content language of the element and/or its parent."
  768. if (open)
  769. return depth == 0 ? "“"_fly_string : "‘"_fly_string;
  770. return depth == 0 ? "”"_fly_string : "’"_fly_string;
  771. case QuotesData::Type::Specified:
  772. // If the depth is greater than the number of pairs, the last pair is repeated.
  773. auto& level = quotes_data.strings[min(depth, quotes_data.strings.size() - 1)];
  774. return open ? level[0] : level[1];
  775. }
  776. VERIFY_NOT_REACHED();
  777. };
  778. if (value.is_content()) {
  779. auto& content_style_value = value.as_content();
  780. CSS::ContentData content_data;
  781. // FIXME: The content is a list of things: strings, identifiers or functions that return strings, and images.
  782. // So it can't always be represented as a single String, but may have to be multiple boxes.
  783. // For now, we'll just assume strings since that is easiest.
  784. StringBuilder builder;
  785. for (auto const& item : content_style_value.content().values()) {
  786. if (item->is_string()) {
  787. builder.append(item->as_string().string_value());
  788. } else if (item->is_keyword()) {
  789. switch (item->to_keyword()) {
  790. case Keyword::OpenQuote:
  791. builder.append(get_quote_string(true, quote_nesting_level++));
  792. break;
  793. case Keyword::CloseQuote:
  794. // A 'close-quote' or 'no-close-quote' that would make the depth negative is in error and is ignored
  795. // (at rendering time): the depth stays at 0 and no quote mark is rendered (although the rest of the
  796. // 'content' property's value is still inserted).
  797. // - https://www.w3.org/TR/CSS21/generate.html#quotes-insert
  798. // (This is missing from the CONTENT-3 spec.)
  799. if (quote_nesting_level > 0)
  800. builder.append(get_quote_string(false, --quote_nesting_level));
  801. break;
  802. case Keyword::NoOpenQuote:
  803. quote_nesting_level++;
  804. break;
  805. case Keyword::NoCloseQuote:
  806. // NOTE: See CloseQuote
  807. if (quote_nesting_level > 0)
  808. quote_nesting_level--;
  809. break;
  810. default:
  811. dbgln("`{}` is not supported in `content` (yet?)", item->to_string(CSSStyleValue::SerializationMode::Normal));
  812. break;
  813. }
  814. } else if (item->is_counter()) {
  815. builder.append(item->as_counter().resolve(element));
  816. } else {
  817. // TODO: Implement images, and other things.
  818. dbgln("`{}` is not supported in `content` (yet?)", item->to_string(CSSStyleValue::SerializationMode::Normal));
  819. }
  820. }
  821. content_data.type = ContentData::Type::String;
  822. content_data.data = MUST(builder.to_string());
  823. if (content_style_value.has_alt_text()) {
  824. StringBuilder alt_text_builder;
  825. for (auto const& item : content_style_value.alt_text()->values()) {
  826. if (item->is_string()) {
  827. alt_text_builder.append(item->as_string().string_value());
  828. } else if (item->is_counter()) {
  829. alt_text_builder.append(item->as_counter().resolve(element));
  830. } else {
  831. dbgln("`{}` is not supported in `content` alt-text (yet?)", item->to_string(CSSStyleValue::SerializationMode::Normal));
  832. }
  833. }
  834. content_data.alt_text = MUST(alt_text_builder.to_string());
  835. }
  836. return { content_data, quote_nesting_level };
  837. }
  838. switch (value.to_keyword()) {
  839. case Keyword::None:
  840. return { { ContentData::Type::None }, quote_nesting_level };
  841. case Keyword::Normal:
  842. return { { ContentData::Type::Normal }, quote_nesting_level };
  843. default:
  844. break;
  845. }
  846. return { {}, quote_nesting_level };
  847. }
  848. Optional<CSS::ContentVisibility> ComputedProperties::content_visibility() const
  849. {
  850. auto const& value = property(CSS::PropertyID::ContentVisibility);
  851. return keyword_to_content_visibility(value.to_keyword());
  852. }
  853. Optional<CSS::Cursor> ComputedProperties::cursor() const
  854. {
  855. auto const& value = property(CSS::PropertyID::Cursor);
  856. return keyword_to_cursor(value.to_keyword());
  857. }
  858. Optional<CSS::Visibility> ComputedProperties::visibility() const
  859. {
  860. auto const& value = property(CSS::PropertyID::Visibility);
  861. if (!value.is_keyword())
  862. return {};
  863. return keyword_to_visibility(value.to_keyword());
  864. }
  865. Display ComputedProperties::display() const
  866. {
  867. auto const& value = property(PropertyID::Display);
  868. if (value.is_display()) {
  869. return value.as_display().display();
  870. }
  871. return Display::from_short(Display::Short::Inline);
  872. }
  873. Vector<CSS::TextDecorationLine> ComputedProperties::text_decoration_line() const
  874. {
  875. auto const& value = property(CSS::PropertyID::TextDecorationLine);
  876. if (value.is_value_list()) {
  877. Vector<CSS::TextDecorationLine> lines;
  878. auto& values = value.as_value_list().values();
  879. for (auto const& item : values) {
  880. lines.append(keyword_to_text_decoration_line(item->to_keyword()).value());
  881. }
  882. return lines;
  883. }
  884. if (value.is_keyword() && value.to_keyword() == Keyword::None)
  885. return {};
  886. dbgln("FIXME: Unsupported value for text-decoration-line: {}", value.to_string(CSSStyleValue::SerializationMode::Normal));
  887. return {};
  888. }
  889. Optional<CSS::TextDecorationStyle> ComputedProperties::text_decoration_style() const
  890. {
  891. auto const& value = property(CSS::PropertyID::TextDecorationStyle);
  892. return keyword_to_text_decoration_style(value.to_keyword());
  893. }
  894. Optional<CSS::TextTransform> ComputedProperties::text_transform() const
  895. {
  896. auto const& value = property(CSS::PropertyID::TextTransform);
  897. return keyword_to_text_transform(value.to_keyword());
  898. }
  899. Optional<CSS::ListStyleType> ComputedProperties::list_style_type() const
  900. {
  901. auto const& value = property(CSS::PropertyID::ListStyleType);
  902. return keyword_to_list_style_type(value.to_keyword());
  903. }
  904. Optional<CSS::ListStylePosition> ComputedProperties::list_style_position() const
  905. {
  906. auto const& value = property(CSS::PropertyID::ListStylePosition);
  907. return keyword_to_list_style_position(value.to_keyword());
  908. }
  909. Optional<CSS::Overflow> ComputedProperties::overflow_x() const
  910. {
  911. return overflow(CSS::PropertyID::OverflowX);
  912. }
  913. Optional<CSS::Overflow> ComputedProperties::overflow_y() const
  914. {
  915. return overflow(CSS::PropertyID::OverflowY);
  916. }
  917. Optional<CSS::Overflow> ComputedProperties::overflow(CSS::PropertyID property_id) const
  918. {
  919. auto const& value = property(property_id);
  920. return keyword_to_overflow(value.to_keyword());
  921. }
  922. Vector<ShadowData> ComputedProperties::shadow(PropertyID property_id, Layout::Node const& layout_node) const
  923. {
  924. auto const& value = property(property_id);
  925. auto resolve_to_length = [&layout_node](NonnullRefPtr<CSSStyleValue const> const& value) -> Optional<Length> {
  926. if (value->is_length())
  927. return value->as_length().length();
  928. if (value->is_calculated())
  929. return value->as_calculated().resolve_length(layout_node);
  930. return {};
  931. };
  932. auto make_shadow_data = [resolve_to_length, &layout_node](ShadowStyleValue const& value) -> Optional<ShadowData> {
  933. auto maybe_offset_x = resolve_to_length(value.offset_x());
  934. if (!maybe_offset_x.has_value())
  935. return {};
  936. auto maybe_offset_y = resolve_to_length(value.offset_y());
  937. if (!maybe_offset_y.has_value())
  938. return {};
  939. auto maybe_blur_radius = resolve_to_length(value.blur_radius());
  940. if (!maybe_blur_radius.has_value())
  941. return {};
  942. auto maybe_spread_distance = resolve_to_length(value.spread_distance());
  943. if (!maybe_spread_distance.has_value())
  944. return {};
  945. return ShadowData {
  946. maybe_offset_x.release_value(),
  947. maybe_offset_y.release_value(),
  948. maybe_blur_radius.release_value(),
  949. maybe_spread_distance.release_value(),
  950. value.color()->to_color(verify_cast<Layout::NodeWithStyle>(layout_node)),
  951. value.placement()
  952. };
  953. };
  954. if (value.is_value_list()) {
  955. auto const& value_list = value.as_value_list();
  956. Vector<ShadowData> shadow_data;
  957. shadow_data.ensure_capacity(value_list.size());
  958. for (auto const& layer_value : value_list.values()) {
  959. auto maybe_shadow_data = make_shadow_data(layer_value->as_shadow());
  960. if (!maybe_shadow_data.has_value())
  961. return {};
  962. shadow_data.append(maybe_shadow_data.release_value());
  963. }
  964. return shadow_data;
  965. }
  966. if (value.is_shadow()) {
  967. auto maybe_shadow_data = make_shadow_data(value.as_shadow());
  968. if (!maybe_shadow_data.has_value())
  969. return {};
  970. return { maybe_shadow_data.release_value() };
  971. }
  972. return {};
  973. }
  974. Vector<ShadowData> ComputedProperties::box_shadow(Layout::Node const& layout_node) const
  975. {
  976. return shadow(PropertyID::BoxShadow, layout_node);
  977. }
  978. Vector<ShadowData> ComputedProperties::text_shadow(Layout::Node const& layout_node) const
  979. {
  980. return shadow(PropertyID::TextShadow, layout_node);
  981. }
  982. Optional<CSS::BoxSizing> ComputedProperties::box_sizing() const
  983. {
  984. auto const& value = property(CSS::PropertyID::BoxSizing);
  985. return keyword_to_box_sizing(value.to_keyword());
  986. }
  987. Variant<CSS::VerticalAlign, CSS::LengthPercentage> ComputedProperties::vertical_align() const
  988. {
  989. auto const& value = property(CSS::PropertyID::VerticalAlign);
  990. if (value.is_keyword())
  991. return keyword_to_vertical_align(value.to_keyword()).release_value();
  992. if (value.is_length())
  993. return CSS::LengthPercentage(value.as_length().length());
  994. if (value.is_percentage())
  995. return CSS::LengthPercentage(value.as_percentage().percentage());
  996. if (value.is_calculated())
  997. return LengthPercentage { value.as_calculated() };
  998. VERIFY_NOT_REACHED();
  999. }
  1000. Optional<FlyString> ComputedProperties::font_language_override() const
  1001. {
  1002. auto const& value = property(CSS::PropertyID::FontLanguageOverride);
  1003. if (value.is_string())
  1004. return value.as_string().string_value();
  1005. return {};
  1006. }
  1007. Optional<Gfx::FontVariantAlternates> ComputedProperties::font_variant_alternates() const
  1008. {
  1009. auto const& value = property(CSS::PropertyID::FontVariantAlternates);
  1010. return value.to_font_variant_alternates();
  1011. }
  1012. Optional<FontVariantCaps> ComputedProperties::font_variant_caps() const
  1013. {
  1014. auto const& value = property(CSS::PropertyID::FontVariantCaps);
  1015. return value.to_font_variant_caps();
  1016. }
  1017. Optional<Gfx::FontVariantEastAsian> ComputedProperties::font_variant_east_asian() const
  1018. {
  1019. auto const& value = property(CSS::PropertyID::FontVariantEastAsian);
  1020. return value.to_font_variant_east_asian();
  1021. }
  1022. Optional<FontVariantEmoji> ComputedProperties::font_variant_emoji() const
  1023. {
  1024. auto const& value = property(CSS::PropertyID::FontVariantEmoji);
  1025. return value.to_font_variant_emoji();
  1026. }
  1027. Optional<Gfx::FontVariantLigatures> ComputedProperties::font_variant_ligatures() const
  1028. {
  1029. auto const& value = property(CSS::PropertyID::FontVariantLigatures);
  1030. return value.to_font_variant_ligatures();
  1031. }
  1032. Optional<Gfx::FontVariantNumeric> ComputedProperties::font_variant_numeric() const
  1033. {
  1034. auto const& value = property(CSS::PropertyID::FontVariantNumeric);
  1035. return value.to_font_variant_numeric();
  1036. }
  1037. Optional<FontVariantPosition> ComputedProperties::font_variant_position() const
  1038. {
  1039. auto const& value = property(CSS::PropertyID::FontVariantPosition);
  1040. return value.to_font_variant_position();
  1041. }
  1042. Optional<HashMap<FlyString, IntegerOrCalculated>> ComputedProperties::font_feature_settings() const
  1043. {
  1044. auto const& value = property(PropertyID::FontFeatureSettings);
  1045. if (value.is_keyword())
  1046. return {}; // normal
  1047. if (value.is_value_list()) {
  1048. auto const& feature_tags = value.as_value_list().values();
  1049. HashMap<FlyString, IntegerOrCalculated> result;
  1050. result.ensure_capacity(feature_tags.size());
  1051. for (auto const& tag_value : feature_tags) {
  1052. auto const& feature_tag = tag_value->as_open_type_tagged();
  1053. if (feature_tag.value()->is_integer()) {
  1054. result.set(feature_tag.tag(), feature_tag.value()->as_integer().value());
  1055. } else {
  1056. VERIFY(feature_tag.value()->is_calculated());
  1057. result.set(feature_tag.tag(), IntegerOrCalculated { feature_tag.value()->as_calculated() });
  1058. }
  1059. }
  1060. return result;
  1061. }
  1062. return {};
  1063. }
  1064. Optional<HashMap<FlyString, NumberOrCalculated>> ComputedProperties::font_variation_settings() const
  1065. {
  1066. auto const& value = property(CSS::PropertyID::FontVariationSettings);
  1067. if (value.is_keyword())
  1068. return {}; // normal
  1069. if (value.is_value_list()) {
  1070. auto const& axis_tags = value.as_value_list().values();
  1071. HashMap<FlyString, NumberOrCalculated> result;
  1072. result.ensure_capacity(axis_tags.size());
  1073. for (auto const& tag_value : axis_tags) {
  1074. auto const& axis_tag = tag_value->as_open_type_tagged();
  1075. if (axis_tag.value()->is_number()) {
  1076. result.set(axis_tag.tag(), axis_tag.value()->as_number().value());
  1077. } else {
  1078. VERIFY(axis_tag.value()->is_calculated());
  1079. result.set(axis_tag.tag(), NumberOrCalculated { axis_tag.value()->as_calculated() });
  1080. }
  1081. }
  1082. return result;
  1083. }
  1084. return {};
  1085. }
  1086. CSS::GridTrackSizeList ComputedProperties::grid_auto_columns() const
  1087. {
  1088. auto const& value = property(CSS::PropertyID::GridAutoColumns);
  1089. return value.as_grid_track_size_list().grid_track_size_list();
  1090. }
  1091. CSS::GridTrackSizeList ComputedProperties::grid_auto_rows() const
  1092. {
  1093. auto const& value = property(CSS::PropertyID::GridAutoRows);
  1094. return value.as_grid_track_size_list().grid_track_size_list();
  1095. }
  1096. CSS::GridTrackSizeList ComputedProperties::grid_template_columns() const
  1097. {
  1098. auto const& value = property(CSS::PropertyID::GridTemplateColumns);
  1099. return value.as_grid_track_size_list().grid_track_size_list();
  1100. }
  1101. CSS::GridTrackSizeList ComputedProperties::grid_template_rows() const
  1102. {
  1103. auto const& value = property(CSS::PropertyID::GridTemplateRows);
  1104. return value.as_grid_track_size_list().grid_track_size_list();
  1105. }
  1106. CSS::GridAutoFlow ComputedProperties::grid_auto_flow() const
  1107. {
  1108. auto const& value = property(CSS::PropertyID::GridAutoFlow);
  1109. if (!value.is_grid_auto_flow())
  1110. return CSS::GridAutoFlow {};
  1111. auto& grid_auto_flow_value = value.as_grid_auto_flow();
  1112. return CSS::GridAutoFlow { .row = grid_auto_flow_value.is_row(), .dense = grid_auto_flow_value.is_dense() };
  1113. }
  1114. CSS::GridTrackPlacement ComputedProperties::grid_column_end() const
  1115. {
  1116. auto const& value = property(CSS::PropertyID::GridColumnEnd);
  1117. return value.as_grid_track_placement().grid_track_placement();
  1118. }
  1119. CSS::GridTrackPlacement ComputedProperties::grid_column_start() const
  1120. {
  1121. auto const& value = property(CSS::PropertyID::GridColumnStart);
  1122. return value.as_grid_track_placement().grid_track_placement();
  1123. }
  1124. CSS::GridTrackPlacement ComputedProperties::grid_row_end() const
  1125. {
  1126. auto const& value = property(CSS::PropertyID::GridRowEnd);
  1127. return value.as_grid_track_placement().grid_track_placement();
  1128. }
  1129. CSS::GridTrackPlacement ComputedProperties::grid_row_start() const
  1130. {
  1131. auto const& value = property(CSS::PropertyID::GridRowStart);
  1132. return value.as_grid_track_placement().grid_track_placement();
  1133. }
  1134. Optional<CSS::BorderCollapse> ComputedProperties::border_collapse() const
  1135. {
  1136. auto const& value = property(CSS::PropertyID::BorderCollapse);
  1137. return keyword_to_border_collapse(value.to_keyword());
  1138. }
  1139. Vector<Vector<String>> ComputedProperties::grid_template_areas() const
  1140. {
  1141. auto const& value = property(CSS::PropertyID::GridTemplateAreas);
  1142. return value.as_grid_template_area().grid_template_area();
  1143. }
  1144. Optional<CSS::ObjectFit> ComputedProperties::object_fit() const
  1145. {
  1146. auto const& value = property(CSS::PropertyID::ObjectFit);
  1147. return keyword_to_object_fit(value.to_keyword());
  1148. }
  1149. CSS::ObjectPosition ComputedProperties::object_position() const
  1150. {
  1151. auto const& value = property(CSS::PropertyID::ObjectPosition);
  1152. auto const& position = value.as_position();
  1153. CSS::ObjectPosition object_position;
  1154. auto const& edge_x = position.edge_x();
  1155. auto const& edge_y = position.edge_y();
  1156. if (edge_x->is_edge()) {
  1157. auto const& edge = edge_x->as_edge();
  1158. object_position.edge_x = edge.edge().value_or(PositionEdge::Left);
  1159. object_position.offset_x = edge.offset();
  1160. }
  1161. if (edge_y->is_edge()) {
  1162. auto const& edge = edge_y->as_edge();
  1163. object_position.edge_y = edge.edge().value_or(PositionEdge::Top);
  1164. object_position.offset_y = edge.offset();
  1165. }
  1166. return object_position;
  1167. }
  1168. Optional<CSS::TableLayout> ComputedProperties::table_layout() const
  1169. {
  1170. auto const& value = property(CSS::PropertyID::TableLayout);
  1171. return keyword_to_table_layout(value.to_keyword());
  1172. }
  1173. Optional<CSS::Direction> ComputedProperties::direction() const
  1174. {
  1175. auto const& value = property(CSS::PropertyID::Direction);
  1176. return keyword_to_direction(value.to_keyword());
  1177. }
  1178. Optional<CSS::UnicodeBidi> ComputedProperties::unicode_bidi() const
  1179. {
  1180. auto const& value = property(CSS::PropertyID::UnicodeBidi);
  1181. return keyword_to_unicode_bidi(value.to_keyword());
  1182. }
  1183. Optional<CSS::WritingMode> ComputedProperties::writing_mode() const
  1184. {
  1185. auto const& value = property(CSS::PropertyID::WritingMode);
  1186. return keyword_to_writing_mode(value.to_keyword());
  1187. }
  1188. Optional<CSS::MaskType> ComputedProperties::mask_type() const
  1189. {
  1190. auto const& value = property(CSS::PropertyID::MaskType);
  1191. return keyword_to_mask_type(value.to_keyword());
  1192. }
  1193. Color ComputedProperties::stop_color() const
  1194. {
  1195. NonnullRawPtr<CSSStyleValue const> value = property(CSS::PropertyID::StopColor);
  1196. if (value->is_keyword()) {
  1197. // Workaround lack of layout node to resolve current color.
  1198. auto const& keyword = value->as_keyword();
  1199. if (keyword.keyword() == CSS::Keyword::Currentcolor)
  1200. value = property(CSS::PropertyID::Color);
  1201. }
  1202. if (value->has_color()) {
  1203. // FIXME: This is used by the SVGStopElement, which does not participate in layout,
  1204. // so can't pass a layout node (so can't resolve some colors, e.g. palette ones)
  1205. return value->to_color({});
  1206. }
  1207. return Color::Black;
  1208. }
  1209. void ComputedProperties::set_math_depth(int math_depth)
  1210. {
  1211. m_math_depth = math_depth;
  1212. // Make our children inherit our computed value, not our specified value.
  1213. set_property(PropertyID::MathDepth, MathDepthStyleValue::create_integer(IntegerStyleValue::create(math_depth)));
  1214. }
  1215. QuotesData ComputedProperties::quotes() const
  1216. {
  1217. auto const& value = property(CSS::PropertyID::Quotes);
  1218. if (value.is_keyword()) {
  1219. switch (value.to_keyword()) {
  1220. case Keyword::Auto:
  1221. return QuotesData { .type = QuotesData::Type::Auto };
  1222. case Keyword::None:
  1223. return QuotesData { .type = QuotesData::Type::None };
  1224. default:
  1225. break;
  1226. }
  1227. }
  1228. if (value.is_value_list()) {
  1229. auto& value_list = value.as_value_list();
  1230. QuotesData quotes_data { .type = QuotesData::Type::Specified };
  1231. VERIFY(value_list.size() % 2 == 0);
  1232. for (auto i = 0u; i < value_list.size(); i += 2) {
  1233. quotes_data.strings.empend(
  1234. value_list.value_at(i, false)->as_string().string_value(),
  1235. value_list.value_at(i + 1, false)->as_string().string_value());
  1236. }
  1237. return quotes_data;
  1238. }
  1239. return InitialValues::quotes();
  1240. }
  1241. Vector<CounterData> ComputedProperties::counter_data(PropertyID property_id) const
  1242. {
  1243. auto const& value = property(property_id);
  1244. if (value.is_counter_definitions()) {
  1245. auto& counter_definitions = value.as_counter_definitions().counter_definitions();
  1246. Vector<CounterData> result;
  1247. for (auto& counter : counter_definitions) {
  1248. CounterData data {
  1249. .name = counter.name,
  1250. .is_reversed = counter.is_reversed,
  1251. .value = {},
  1252. };
  1253. if (counter.value) {
  1254. if (counter.value->is_integer()) {
  1255. data.value = AK::clamp_to<i32>(counter.value->as_integer().integer());
  1256. } else if (counter.value->is_calculated()) {
  1257. auto maybe_int = counter.value->as_calculated().resolve_integer();
  1258. if (maybe_int.has_value())
  1259. data.value = AK::clamp_to<i32>(*maybe_int);
  1260. } else {
  1261. dbgln("Unimplemented type for {} integer value: '{}'", string_from_property_id(property_id), counter.value->to_string(CSSStyleValue::SerializationMode::Normal));
  1262. }
  1263. }
  1264. result.append(move(data));
  1265. }
  1266. return result;
  1267. }
  1268. if (value.to_keyword() == Keyword::None)
  1269. return {};
  1270. dbgln("Unhandled type for {} value: '{}'", string_from_property_id(property_id), value.to_string(CSSStyleValue::SerializationMode::Normal));
  1271. return {};
  1272. }
  1273. Optional<CSS::ScrollbarWidth> ComputedProperties::scrollbar_width() const
  1274. {
  1275. auto const& value = property(CSS::PropertyID::ScrollbarWidth);
  1276. return keyword_to_scrollbar_width(value.to_keyword());
  1277. }
  1278. }