StyleProperties.cpp 49 KB

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