StyleProperties.cpp 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  1. /*
  2. * Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.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/ContentStyleValue.h>
  13. #include <LibWeb/CSS/StyleValues/DisplayStyleValue.h>
  14. #include <LibWeb/CSS/StyleValues/GridAutoFlowStyleValue.h>
  15. #include <LibWeb/CSS/StyleValues/GridTemplateAreaStyleValue.h>
  16. #include <LibWeb/CSS/StyleValues/GridTrackPlacementStyleValue.h>
  17. #include <LibWeb/CSS/StyleValues/GridTrackSizeListStyleValue.h>
  18. #include <LibWeb/CSS/StyleValues/IdentifierStyleValue.h>
  19. #include <LibWeb/CSS/StyleValues/IntegerStyleValue.h>
  20. #include <LibWeb/CSS/StyleValues/LengthStyleValue.h>
  21. #include <LibWeb/CSS/StyleValues/MathDepthStyleValue.h>
  22. #include <LibWeb/CSS/StyleValues/NumberStyleValue.h>
  23. #include <LibWeb/CSS/StyleValues/PercentageStyleValue.h>
  24. #include <LibWeb/CSS/StyleValues/PositionStyleValue.h>
  25. #include <LibWeb/CSS/StyleValues/RectStyleValue.h>
  26. #include <LibWeb/CSS/StyleValues/ShadowStyleValue.h>
  27. #include <LibWeb/CSS/StyleValues/StringStyleValue.h>
  28. #include <LibWeb/CSS/StyleValues/StyleValueList.h>
  29. #include <LibWeb/CSS/StyleValues/TransformationStyleValue.h>
  30. #include <LibWeb/FontCache.h>
  31. #include <LibWeb/Layout/BlockContainer.h>
  32. #include <LibWeb/Layout/Node.h>
  33. #include <LibWeb/Platform/FontPlugin.h>
  34. namespace Web::CSS {
  35. StyleProperties::StyleProperties(StyleProperties const& other)
  36. : m_property_values(other.m_property_values)
  37. {
  38. if (other.m_font) {
  39. m_font = other.m_font->clone();
  40. } else {
  41. m_font = nullptr;
  42. }
  43. }
  44. NonnullRefPtr<StyleProperties> StyleProperties::clone() const
  45. {
  46. return adopt_ref(*new StyleProperties(*this));
  47. }
  48. void StyleProperties::set_property(CSS::PropertyID id, NonnullRefPtr<StyleValue const> value, CSS::CSSStyleDeclaration const* source_declaration)
  49. {
  50. m_property_values[to_underlying(id)] = StyleAndSourceDeclaration { move(value), source_declaration };
  51. }
  52. NonnullRefPtr<StyleValue const> StyleProperties::property(CSS::PropertyID property_id) const
  53. {
  54. auto value = m_property_values[to_underlying(property_id)];
  55. // By the time we call this method, all properties have values assigned.
  56. VERIFY(value.has_value());
  57. return value->style;
  58. }
  59. RefPtr<StyleValue const> StyleProperties::maybe_null_property(CSS::PropertyID property_id) const
  60. {
  61. auto value = m_property_values[to_underlying(property_id)];
  62. if (value.has_value())
  63. return value->style;
  64. return {};
  65. }
  66. CSS::CSSStyleDeclaration const* StyleProperties::property_source_declaration(CSS::PropertyID property_id) const
  67. {
  68. return m_property_values[to_underlying(property_id)].map([](auto& value) { return value.declaration; }).value_or(nullptr);
  69. }
  70. CSS::Size StyleProperties::size_value(CSS::PropertyID id) const
  71. {
  72. auto value = property(id);
  73. if (value->is_identifier()) {
  74. switch (value->to_identifier()) {
  75. case ValueID::Auto:
  76. return CSS::Size::make_auto();
  77. case ValueID::MinContent:
  78. return CSS::Size::make_min_content();
  79. case ValueID::MaxContent:
  80. return CSS::Size::make_max_content();
  81. case ValueID::FitContent:
  82. return CSS::Size::make_fit_content();
  83. case ValueID::None:
  84. return CSS::Size::make_none();
  85. default:
  86. VERIFY_NOT_REACHED();
  87. }
  88. }
  89. if (value->is_calculated())
  90. return CSS::Size::make_calculated(const_cast<CalculatedStyleValue&>(value->as_calculated()));
  91. if (value->is_percentage())
  92. return CSS::Size::make_percentage(value->as_percentage().percentage());
  93. if (value->is_length()) {
  94. auto length = value->as_length().length();
  95. if (length.is_auto())
  96. return CSS::Size::make_auto();
  97. return CSS::Size::make_length(length);
  98. }
  99. // FIXME: Support `fit-content(<length>)`
  100. dbgln("FIXME: Unsupported size value: `{}`, treating as `auto`", value->to_string());
  101. return CSS::Size::make_auto();
  102. }
  103. LengthPercentage StyleProperties::length_percentage_or_fallback(CSS::PropertyID id, LengthPercentage const& fallback) const
  104. {
  105. return length_percentage(id).value_or(fallback);
  106. }
  107. Optional<LengthPercentage> StyleProperties::length_percentage(CSS::PropertyID id) const
  108. {
  109. auto value = property(id);
  110. if (value->is_calculated())
  111. return LengthPercentage { const_cast<CalculatedStyleValue&>(value->as_calculated()) };
  112. if (value->is_percentage())
  113. return value->as_percentage().percentage();
  114. if (value->is_length())
  115. return value->as_length().length();
  116. if (value->has_auto())
  117. return LengthPercentage { Length::make_auto() };
  118. return {};
  119. }
  120. 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
  121. {
  122. LengthBox box;
  123. box.left() = length_percentage_or_fallback(left_id, default_value);
  124. box.top() = length_percentage_or_fallback(top_id, default_value);
  125. box.right() = length_percentage_or_fallback(right_id, default_value);
  126. box.bottom() = length_percentage_or_fallback(bottom_id, default_value);
  127. return box;
  128. }
  129. Color StyleProperties::color_or_fallback(CSS::PropertyID id, Layout::NodeWithStyle const& node, Color fallback) const
  130. {
  131. auto value = property(id);
  132. if (!value->has_color())
  133. return fallback;
  134. return value->to_color(node);
  135. }
  136. NonnullRefPtr<Gfx::Font const> StyleProperties::font_fallback(bool monospace, bool bold)
  137. {
  138. if (monospace && bold)
  139. return Platform::FontPlugin::the().default_fixed_width_font().bold_variant();
  140. if (monospace)
  141. return Platform::FontPlugin::the().default_fixed_width_font();
  142. if (bold)
  143. return Platform::FontPlugin::the().default_font().bold_variant();
  144. return Platform::FontPlugin::the().default_font();
  145. }
  146. // FIXME: This implementation is almost identical to line_height(Layout::Node) below. Maybe they can be combined somehow.
  147. CSSPixels StyleProperties::line_height(CSSPixelRect const& viewport_rect, Length::FontMetrics const& font_metrics, Length::FontMetrics const& root_font_metrics) const
  148. {
  149. auto line_height = property(CSS::PropertyID::LineHeight);
  150. if (line_height->is_identifier() && line_height->to_identifier() == ValueID::Normal)
  151. return font_metrics.line_height;
  152. if (line_height->is_length()) {
  153. auto line_height_length = line_height->as_length().length();
  154. if (!line_height_length.is_auto())
  155. return line_height_length.to_px(viewport_rect, font_metrics, root_font_metrics);
  156. }
  157. if (line_height->is_number())
  158. return Length(line_height->as_number().number(), Length::Type::Em).to_px(viewport_rect, font_metrics, root_font_metrics);
  159. if (line_height->is_percentage()) {
  160. // Percentages are relative to 1em. https://www.w3.org/TR/css-inline-3/#valdef-line-height-percentage
  161. auto& percentage = line_height->as_percentage().percentage();
  162. return Length(percentage.as_fraction(), Length::Type::Em).to_px(viewport_rect, font_metrics, root_font_metrics);
  163. }
  164. if (line_height->is_calculated()) {
  165. // FIXME: Handle `line-height: calc(...)` despite not having a LayoutNode here.
  166. return font_metrics.line_height;
  167. }
  168. return font_metrics.line_height;
  169. }
  170. CSSPixels StyleProperties::line_height(Layout::Node const& layout_node) const
  171. {
  172. auto line_height = property(CSS::PropertyID::LineHeight);
  173. if (line_height->is_identifier() && line_height->to_identifier() == ValueID::Normal)
  174. return CSSPixels::nearest_value_for(layout_node.font().pixel_metrics().line_spacing());
  175. if (line_height->is_length()) {
  176. auto line_height_length = line_height->as_length().length();
  177. if (!line_height_length.is_auto())
  178. return line_height_length.to_px(layout_node);
  179. }
  180. if (line_height->is_number())
  181. return Length(line_height->as_number().number(), Length::Type::Em).to_px(layout_node);
  182. if (line_height->is_percentage()) {
  183. // Percentages are relative to 1em. https://www.w3.org/TR/css-inline-3/#valdef-line-height-percentage
  184. auto& percentage = line_height->as_percentage().percentage();
  185. return Length(percentage.as_fraction(), Length::Type::Em).to_px(layout_node);
  186. }
  187. if (line_height->is_calculated()) {
  188. if (line_height->as_calculated().resolves_to_number()) {
  189. auto resolved = line_height->as_calculated().resolve_number();
  190. if (!resolved.has_value()) {
  191. dbgln("FIXME: Failed to resolve calc() line-height (number): {}", line_height->as_calculated().to_string());
  192. return CSSPixels::nearest_value_for(layout_node.font().pixel_metrics().line_spacing());
  193. }
  194. return Length(resolved.value(), Length::Type::Em).to_px(layout_node);
  195. }
  196. auto resolved = line_height->as_calculated().resolve_length(layout_node);
  197. if (!resolved.has_value()) {
  198. dbgln("FIXME: Failed to resolve calc() line-height: {}", line_height->as_calculated().to_string());
  199. return CSSPixels::nearest_value_for(layout_node.font().pixel_metrics().line_spacing());
  200. }
  201. return resolved->to_px(layout_node);
  202. }
  203. return CSSPixels::nearest_value_for(layout_node.font().pixel_metrics().line_spacing());
  204. }
  205. Optional<int> StyleProperties::z_index() const
  206. {
  207. auto value = property(CSS::PropertyID::ZIndex);
  208. if (value->has_auto())
  209. return {};
  210. if (value->is_integer()) {
  211. // Clamp z-index to the range of a signed 32-bit integer for consistency with other engines.
  212. auto integer = value->as_integer().integer();
  213. if (integer >= NumericLimits<int>::max())
  214. return NumericLimits<int>::max();
  215. if (integer <= NumericLimits<int>::min())
  216. return NumericLimits<int>::min();
  217. return static_cast<int>(integer);
  218. }
  219. return {};
  220. }
  221. static float resolve_opacity_value(CSS::StyleValue const& value)
  222. {
  223. float unclamped_opacity = 1.0f;
  224. if (value.is_number()) {
  225. unclamped_opacity = value.as_number().number();
  226. } else if (value.is_calculated()) {
  227. auto& calculated = value.as_calculated();
  228. if (calculated.resolves_to_percentage()) {
  229. auto maybe_percentage = value.as_calculated().resolve_percentage();
  230. if (maybe_percentage.has_value())
  231. unclamped_opacity = maybe_percentage->as_fraction();
  232. else
  233. dbgln("Unable to resolve calc() as opacity (percentage): {}", value.to_string());
  234. } else if (calculated.resolves_to_number()) {
  235. auto maybe_number = const_cast<CalculatedStyleValue&>(value.as_calculated()).resolve_number();
  236. if (maybe_number.has_value())
  237. unclamped_opacity = maybe_number.value();
  238. else
  239. dbgln("Unable to resolve calc() as opacity (number): {}", value.to_string());
  240. }
  241. } else if (value.is_percentage()) {
  242. unclamped_opacity = value.as_percentage().percentage().as_fraction();
  243. }
  244. return clamp(unclamped_opacity, 0.0f, 1.0f);
  245. }
  246. float StyleProperties::opacity() const
  247. {
  248. auto value = property(CSS::PropertyID::Opacity);
  249. return resolve_opacity_value(*value);
  250. }
  251. float StyleProperties::fill_opacity() const
  252. {
  253. auto value = property(CSS::PropertyID::FillOpacity);
  254. return resolve_opacity_value(*value);
  255. }
  256. float StyleProperties::stroke_opacity() const
  257. {
  258. auto value = property(CSS::PropertyID::StrokeOpacity);
  259. return resolve_opacity_value(*value);
  260. }
  261. float StyleProperties::stop_opacity() const
  262. {
  263. auto value = property(CSS::PropertyID::StopOpacity);
  264. return resolve_opacity_value(*value);
  265. }
  266. Optional<CSS::FillRule> StyleProperties::fill_rule() const
  267. {
  268. auto value = property(CSS::PropertyID::FillRule);
  269. return value_id_to_fill_rule(value->to_identifier());
  270. }
  271. Optional<CSS::FlexDirection> StyleProperties::flex_direction() const
  272. {
  273. auto value = property(CSS::PropertyID::FlexDirection);
  274. return value_id_to_flex_direction(value->to_identifier());
  275. }
  276. Optional<CSS::FlexWrap> StyleProperties::flex_wrap() const
  277. {
  278. auto value = property(CSS::PropertyID::FlexWrap);
  279. return value_id_to_flex_wrap(value->to_identifier());
  280. }
  281. Optional<CSS::FlexBasis> StyleProperties::flex_basis() const
  282. {
  283. auto value = property(CSS::PropertyID::FlexBasis);
  284. if (value->is_identifier() && value->to_identifier() == CSS::ValueID::Content)
  285. return CSS::FlexBasisContent {};
  286. return size_value(CSS::PropertyID::FlexBasis);
  287. }
  288. float StyleProperties::flex_grow() const
  289. {
  290. auto value = property(CSS::PropertyID::FlexGrow);
  291. if (!value->is_number())
  292. return 0;
  293. return value->as_number().number();
  294. }
  295. float StyleProperties::flex_shrink() const
  296. {
  297. auto value = property(CSS::PropertyID::FlexShrink);
  298. if (!value->is_number())
  299. return 1;
  300. return value->as_number().number();
  301. }
  302. int StyleProperties::order() const
  303. {
  304. auto value = property(CSS::PropertyID::Order);
  305. if (!value->is_integer())
  306. return 0;
  307. return value->as_integer().integer();
  308. }
  309. Optional<CSS::ImageRendering> StyleProperties::image_rendering() const
  310. {
  311. auto value = property(CSS::PropertyID::ImageRendering);
  312. return value_id_to_image_rendering(value->to_identifier());
  313. }
  314. CSS::Length StyleProperties::border_spacing_horizontal() const
  315. {
  316. auto value = property(CSS::PropertyID::BorderSpacing);
  317. if (value->is_length())
  318. return value->as_length().length();
  319. auto const& list = value->as_value_list();
  320. return list.value_at(0, false)->as_length().length();
  321. }
  322. CSS::Length StyleProperties::border_spacing_vertical() const
  323. {
  324. auto value = property(CSS::PropertyID::BorderSpacing);
  325. if (value->is_length())
  326. return value->as_length().length();
  327. auto const& list = value->as_value_list();
  328. return list.value_at(1, false)->as_length().length();
  329. }
  330. Optional<CSS::CaptionSide> StyleProperties::caption_side() const
  331. {
  332. auto value = property(CSS::PropertyID::CaptionSide);
  333. return value_id_to_caption_side(value->to_identifier());
  334. }
  335. CSS::Clip StyleProperties::clip() const
  336. {
  337. auto value = property(CSS::PropertyID::Clip);
  338. if (!value->is_rect())
  339. return CSS::Clip::make_auto();
  340. return CSS::Clip(value->as_rect().rect());
  341. }
  342. Optional<CSS::JustifyContent> StyleProperties::justify_content() const
  343. {
  344. auto value = property(CSS::PropertyID::JustifyContent);
  345. return value_id_to_justify_content(value->to_identifier());
  346. }
  347. Optional<CSS::JustifyItems> StyleProperties::justify_items() const
  348. {
  349. auto value = property(CSS::PropertyID::JustifyItems);
  350. return value_id_to_justify_items(value->to_identifier());
  351. }
  352. Optional<CSS::JustifySelf> StyleProperties::justify_self() const
  353. {
  354. auto value = property(CSS::PropertyID::JustifySelf);
  355. return value_id_to_justify_self(value->to_identifier());
  356. }
  357. Vector<CSS::Transformation> StyleProperties::transformations() const
  358. {
  359. auto value = property(CSS::PropertyID::Transform);
  360. if (value->is_identifier() && value->to_identifier() == CSS::ValueID::None)
  361. return {};
  362. if (!value->is_value_list())
  363. return {};
  364. auto& list = value->as_value_list();
  365. Vector<CSS::Transformation> transformations;
  366. for (auto& it : list.values()) {
  367. if (!it->is_transformation())
  368. return {};
  369. auto& transformation_style_value = it->as_transformation();
  370. auto function = transformation_style_value.transform_function();
  371. Vector<TransformValue> values;
  372. for (auto& transformation_value : transformation_style_value.values()) {
  373. if (transformation_value->is_calculated()) {
  374. auto& calculated = transformation_value->as_calculated();
  375. if (calculated.resolves_to_length()) {
  376. values.append(CSS::LengthPercentage { calculated });
  377. } else if (calculated.resolves_to_percentage()) {
  378. values.append({ calculated.resolve_percentage().value() });
  379. } else if (calculated.resolves_to_number()) {
  380. values.append({ calculated.resolve_number().value() });
  381. } else if (calculated.resolves_to_angle()) {
  382. values.append({ calculated.resolve_angle().value() });
  383. } else {
  384. dbgln("FIXME: Unsupported calc value in transform! {}", calculated.to_string());
  385. }
  386. } else if (transformation_value->is_length()) {
  387. values.append({ transformation_value->as_length().length() });
  388. } else if (transformation_value->is_percentage()) {
  389. values.append({ transformation_value->as_percentage().percentage() });
  390. } else if (transformation_value->is_number()) {
  391. values.append({ transformation_value->as_number().number() });
  392. } else if (transformation_value->is_angle()) {
  393. values.append({ transformation_value->as_angle().angle() });
  394. } else {
  395. dbgln("FIXME: Unsupported value in transform! {}", transformation_value->to_string());
  396. }
  397. }
  398. transformations.empend(function, move(values));
  399. }
  400. return transformations;
  401. }
  402. static Optional<LengthPercentage> length_percentage_for_style_value(StyleValue const& value)
  403. {
  404. if (value.is_length())
  405. return value.as_length().length();
  406. if (value.is_percentage())
  407. return value.as_percentage().percentage();
  408. return {};
  409. }
  410. CSS::TransformOrigin StyleProperties::transform_origin() const
  411. {
  412. auto value = property(CSS::PropertyID::TransformOrigin);
  413. if (!value->is_value_list() || value->as_value_list().size() != 2)
  414. return {};
  415. auto const& list = value->as_value_list();
  416. auto x_value = length_percentage_for_style_value(list.values()[0]);
  417. auto y_value = length_percentage_for_style_value(list.values()[1]);
  418. if (!x_value.has_value() || !y_value.has_value()) {
  419. return {};
  420. }
  421. return { x_value.value(), y_value.value() };
  422. }
  423. Optional<Color> StyleProperties::accent_color(Layout::NodeWithStyle const& node) const
  424. {
  425. auto value = property(CSS::PropertyID::AccentColor);
  426. if (value->has_color())
  427. return value->to_color(node);
  428. return {};
  429. }
  430. Optional<CSS::AlignContent> StyleProperties::align_content() const
  431. {
  432. auto value = property(CSS::PropertyID::AlignContent);
  433. return value_id_to_align_content(value->to_identifier());
  434. }
  435. Optional<CSS::AlignItems> StyleProperties::align_items() const
  436. {
  437. auto value = property(CSS::PropertyID::AlignItems);
  438. return value_id_to_align_items(value->to_identifier());
  439. }
  440. Optional<CSS::AlignSelf> StyleProperties::align_self() const
  441. {
  442. auto value = property(CSS::PropertyID::AlignSelf);
  443. return value_id_to_align_self(value->to_identifier());
  444. }
  445. Optional<CSS::Appearance> StyleProperties::appearance() const
  446. {
  447. auto value = property(CSS::PropertyID::Appearance);
  448. auto appearance = value_id_to_appearance(value->to_identifier());
  449. if (appearance.has_value()) {
  450. switch (*appearance) {
  451. // Note: All these compatibility values can be treated as 'auto'
  452. case CSS::Appearance::Textfield:
  453. case CSS::Appearance::MenulistButton:
  454. case CSS::Appearance::Searchfield:
  455. case CSS::Appearance::Textarea:
  456. case CSS::Appearance::PushButton:
  457. case CSS::Appearance::SliderHorizontal:
  458. case CSS::Appearance::Checkbox:
  459. case CSS::Appearance::Radio:
  460. case CSS::Appearance::SquareButton:
  461. case CSS::Appearance::Menulist:
  462. case CSS::Appearance::Listbox:
  463. case CSS::Appearance::Meter:
  464. case CSS::Appearance::ProgressBar:
  465. case CSS::Appearance::Button:
  466. appearance = CSS::Appearance::Auto;
  467. break;
  468. default:
  469. break;
  470. }
  471. }
  472. return appearance;
  473. }
  474. CSS::BackdropFilter StyleProperties::backdrop_filter() const
  475. {
  476. auto value = property(CSS::PropertyID::BackdropFilter);
  477. if (value->is_filter_value_list())
  478. return BackdropFilter(value->as_filter_value_list());
  479. return BackdropFilter::make_none();
  480. }
  481. Optional<CSS::Positioning> StyleProperties::position() const
  482. {
  483. auto value = property(CSS::PropertyID::Position);
  484. return value_id_to_positioning(value->to_identifier());
  485. }
  486. bool StyleProperties::operator==(StyleProperties const& other) const
  487. {
  488. if (m_property_values.size() != other.m_property_values.size())
  489. return false;
  490. for (size_t i = 0; i < m_property_values.size(); ++i) {
  491. auto const& my_style = m_property_values[i];
  492. auto const& other_style = other.m_property_values[i];
  493. if (!my_style.has_value()) {
  494. if (other_style.has_value())
  495. return false;
  496. continue;
  497. }
  498. if (!other_style.has_value())
  499. return false;
  500. auto const& my_value = *my_style->style;
  501. auto const& other_value = *other_style->style;
  502. if (my_value.type() != other_value.type())
  503. return false;
  504. if (my_value != other_value)
  505. return false;
  506. }
  507. return true;
  508. }
  509. Optional<CSS::TextAnchor> StyleProperties::text_anchor() const
  510. {
  511. auto value = property(CSS::PropertyID::TextAnchor);
  512. return value_id_to_text_anchor(value->to_identifier());
  513. }
  514. Optional<CSS::TextAlign> StyleProperties::text_align() const
  515. {
  516. auto value = property(CSS::PropertyID::TextAlign);
  517. return value_id_to_text_align(value->to_identifier());
  518. }
  519. Optional<CSS::TextJustify> StyleProperties::text_justify() const
  520. {
  521. auto value = property(CSS::PropertyID::TextJustify);
  522. return value_id_to_text_justify(value->to_identifier());
  523. }
  524. Optional<CSS::PointerEvents> StyleProperties::pointer_events() const
  525. {
  526. auto value = property(CSS::PropertyID::PointerEvents);
  527. return value_id_to_pointer_events(value->to_identifier());
  528. }
  529. Optional<CSS::WhiteSpace> StyleProperties::white_space() const
  530. {
  531. auto value = property(CSS::PropertyID::WhiteSpace);
  532. return value_id_to_white_space(value->to_identifier());
  533. }
  534. Optional<CSS::LineStyle> StyleProperties::line_style(CSS::PropertyID property_id) const
  535. {
  536. auto value = property(property_id);
  537. return value_id_to_line_style(value->to_identifier());
  538. }
  539. Optional<CSS::OutlineStyle> StyleProperties::outline_style() const
  540. {
  541. auto value = property(CSS::PropertyID::OutlineStyle);
  542. return value_id_to_outline_style(value->to_identifier());
  543. }
  544. Optional<CSS::Float> StyleProperties::float_() const
  545. {
  546. auto value = property(CSS::PropertyID::Float);
  547. return value_id_to_float(value->to_identifier());
  548. }
  549. Optional<CSS::Clear> StyleProperties::clear() const
  550. {
  551. auto value = property(CSS::PropertyID::Clear);
  552. return value_id_to_clear(value->to_identifier());
  553. }
  554. StyleProperties::ContentDataAndQuoteNestingLevel StyleProperties::content(u32 initial_quote_nesting_level) const
  555. {
  556. auto value = property(CSS::PropertyID::Content);
  557. auto quotes_data = quotes();
  558. auto quote_nesting_level = initial_quote_nesting_level;
  559. auto get_quote_string = [&](bool open, auto depth) {
  560. switch (quotes_data.type) {
  561. case QuotesData::Type::None:
  562. return String {};
  563. case QuotesData::Type::Auto:
  564. // FIXME: "A typographically appropriate used value for quotes is automatically chosen by the UA
  565. // based on the content language of the element and/or its parent."
  566. if (open)
  567. return depth == 0 ? "“"_string : "‘"_string;
  568. return depth == 0 ? "”"_string : "’"_string;
  569. case QuotesData::Type::Specified:
  570. // If the depth is greater than the number of pairs, the last pair is repeated.
  571. auto& level = quotes_data.strings[min(depth, quotes_data.strings.size() - 1)];
  572. return open ? level[0] : level[1];
  573. }
  574. VERIFY_NOT_REACHED();
  575. };
  576. if (value->is_content()) {
  577. auto& content_style_value = value->as_content();
  578. CSS::ContentData content_data;
  579. // FIXME: The content is a list of things: strings, identifiers or functions that return strings, and images.
  580. // So it can't always be represented as a single String, but may have to be multiple boxes.
  581. // For now, we'll just assume strings since that is easiest.
  582. StringBuilder builder;
  583. for (auto const& item : content_style_value.content().values()) {
  584. if (item->is_string()) {
  585. builder.append(item->as_string().string_value());
  586. } else if (item->is_identifier()) {
  587. switch (item->to_identifier()) {
  588. case ValueID::OpenQuote:
  589. builder.append(get_quote_string(true, quote_nesting_level++));
  590. break;
  591. case ValueID::CloseQuote:
  592. // A 'close-quote' or 'no-close-quote' that would make the depth negative is in error and is ignored
  593. // (at rendering time): the depth stays at 0 and no quote mark is rendered (although the rest of the
  594. // 'content' property's value is still inserted).
  595. // - https://www.w3.org/TR/CSS21/generate.html#quotes-insert
  596. // (This is missing from the CONTENT-3 spec.)
  597. if (quote_nesting_level > 0)
  598. builder.append(get_quote_string(false, --quote_nesting_level));
  599. break;
  600. case ValueID::NoOpenQuote:
  601. quote_nesting_level++;
  602. break;
  603. case ValueID::NoCloseQuote:
  604. // NOTE: See CloseQuote
  605. if (quote_nesting_level > 0)
  606. quote_nesting_level--;
  607. break;
  608. default:
  609. dbgln("`{}` is not supported in `content` (yet?)", item->to_string());
  610. break;
  611. }
  612. } else {
  613. // TODO: Implement counters, images, and other things.
  614. dbgln("`{}` is not supported in `content` (yet?)", item->to_string());
  615. }
  616. }
  617. content_data.type = ContentData::Type::String;
  618. content_data.data = MUST(builder.to_string());
  619. if (content_style_value.has_alt_text()) {
  620. StringBuilder alt_text_builder;
  621. for (auto const& item : content_style_value.alt_text()->values()) {
  622. if (item->is_string()) {
  623. alt_text_builder.append(item->as_string().string_value());
  624. } else {
  625. // TODO: Implement counters
  626. }
  627. }
  628. content_data.alt_text = MUST(alt_text_builder.to_string());
  629. }
  630. return { content_data, quote_nesting_level };
  631. }
  632. switch (value->to_identifier()) {
  633. case ValueID::None:
  634. return { { ContentData::Type::None }, quote_nesting_level };
  635. case ValueID::Normal:
  636. return { { ContentData::Type::Normal }, quote_nesting_level };
  637. default:
  638. break;
  639. }
  640. return { {}, quote_nesting_level };
  641. }
  642. Optional<CSS::Cursor> StyleProperties::cursor() const
  643. {
  644. auto value = property(CSS::PropertyID::Cursor);
  645. return value_id_to_cursor(value->to_identifier());
  646. }
  647. Optional<CSS::Visibility> StyleProperties::visibility() const
  648. {
  649. auto value = property(CSS::PropertyID::Visibility);
  650. if (!value->is_identifier())
  651. return {};
  652. return value_id_to_visibility(value->to_identifier());
  653. }
  654. Display StyleProperties::display() const
  655. {
  656. auto value = property(PropertyID::Display);
  657. if (value->is_display()) {
  658. return value->as_display().display();
  659. }
  660. return Display::from_short(Display::Short::Inline);
  661. }
  662. Vector<CSS::TextDecorationLine> StyleProperties::text_decoration_line() const
  663. {
  664. auto value = property(CSS::PropertyID::TextDecorationLine);
  665. if (value->is_value_list()) {
  666. Vector<CSS::TextDecorationLine> lines;
  667. auto& values = value->as_value_list().values();
  668. for (auto const& item : values) {
  669. lines.append(value_id_to_text_decoration_line(item->to_identifier()).value());
  670. }
  671. return lines;
  672. }
  673. if (value->is_identifier() && value->to_identifier() == ValueID::None)
  674. return {};
  675. dbgln("FIXME: Unsupported value for text-decoration-line: {}", value->to_string());
  676. return {};
  677. }
  678. Optional<CSS::TextDecorationStyle> StyleProperties::text_decoration_style() const
  679. {
  680. auto value = property(CSS::PropertyID::TextDecorationStyle);
  681. return value_id_to_text_decoration_style(value->to_identifier());
  682. }
  683. Optional<CSS::TextTransform> StyleProperties::text_transform() const
  684. {
  685. auto value = property(CSS::PropertyID::TextTransform);
  686. return value_id_to_text_transform(value->to_identifier());
  687. }
  688. Optional<CSS::ListStyleType> StyleProperties::list_style_type() const
  689. {
  690. auto value = property(CSS::PropertyID::ListStyleType);
  691. return value_id_to_list_style_type(value->to_identifier());
  692. }
  693. Optional<CSS::ListStylePosition> StyleProperties::list_style_position() const
  694. {
  695. auto value = property(CSS::PropertyID::ListStylePosition);
  696. return value_id_to_list_style_position(value->to_identifier());
  697. }
  698. Optional<CSS::Overflow> StyleProperties::overflow_x() const
  699. {
  700. return overflow(CSS::PropertyID::OverflowX);
  701. }
  702. Optional<CSS::Overflow> StyleProperties::overflow_y() const
  703. {
  704. return overflow(CSS::PropertyID::OverflowY);
  705. }
  706. Optional<CSS::Overflow> StyleProperties::overflow(CSS::PropertyID property_id) const
  707. {
  708. auto value = property(property_id);
  709. return value_id_to_overflow(value->to_identifier());
  710. }
  711. Vector<ShadowData> StyleProperties::shadow(PropertyID property_id, Layout::Node const& layout_node) const
  712. {
  713. auto value = property(property_id);
  714. auto resolve_to_length = [&layout_node](NonnullRefPtr<StyleValue const> const& value) -> Optional<Length> {
  715. if (value->is_length())
  716. return value->as_length().length();
  717. if (value->is_calculated())
  718. return value->as_calculated().resolve_length(layout_node);
  719. return {};
  720. };
  721. auto make_shadow_data = [resolve_to_length](ShadowStyleValue const& value) -> Optional<ShadowData> {
  722. auto maybe_offset_x = resolve_to_length(value.offset_x());
  723. if (!maybe_offset_x.has_value())
  724. return {};
  725. auto maybe_offset_y = resolve_to_length(value.offset_y());
  726. if (!maybe_offset_y.has_value())
  727. return {};
  728. auto maybe_blur_radius = resolve_to_length(value.blur_radius());
  729. if (!maybe_blur_radius.has_value())
  730. return {};
  731. auto maybe_spread_distance = resolve_to_length(value.spread_distance());
  732. if (!maybe_spread_distance.has_value())
  733. return {};
  734. return ShadowData {
  735. value.color(),
  736. maybe_offset_x.release_value(),
  737. maybe_offset_y.release_value(),
  738. maybe_blur_radius.release_value(),
  739. maybe_spread_distance.release_value(),
  740. value.placement()
  741. };
  742. };
  743. if (value->is_value_list()) {
  744. auto const& value_list = value->as_value_list();
  745. Vector<ShadowData> shadow_data;
  746. shadow_data.ensure_capacity(value_list.size());
  747. for (auto const& layer_value : value_list.values()) {
  748. auto maybe_shadow_data = make_shadow_data(layer_value->as_shadow());
  749. if (!maybe_shadow_data.has_value())
  750. return {};
  751. shadow_data.append(maybe_shadow_data.release_value());
  752. }
  753. return shadow_data;
  754. }
  755. if (value->is_shadow()) {
  756. auto maybe_shadow_data = make_shadow_data(value->as_shadow());
  757. if (!maybe_shadow_data.has_value())
  758. return {};
  759. return { maybe_shadow_data.release_value() };
  760. }
  761. return {};
  762. }
  763. Vector<ShadowData> StyleProperties::box_shadow(Layout::Node const& layout_node) const
  764. {
  765. return shadow(PropertyID::BoxShadow, layout_node);
  766. }
  767. Vector<ShadowData> StyleProperties::text_shadow(Layout::Node const& layout_node) const
  768. {
  769. return shadow(PropertyID::TextShadow, layout_node);
  770. }
  771. Optional<CSS::BoxSizing> StyleProperties::box_sizing() const
  772. {
  773. auto value = property(CSS::PropertyID::BoxSizing);
  774. return value_id_to_box_sizing(value->to_identifier());
  775. }
  776. Variant<CSS::VerticalAlign, CSS::LengthPercentage> StyleProperties::vertical_align() const
  777. {
  778. auto value = property(CSS::PropertyID::VerticalAlign);
  779. if (value->is_identifier())
  780. return value_id_to_vertical_align(value->to_identifier()).release_value();
  781. if (value->is_length())
  782. return CSS::LengthPercentage(value->as_length().length());
  783. if (value->is_percentage())
  784. return CSS::LengthPercentage(value->as_percentage().percentage());
  785. if (value->is_calculated())
  786. return LengthPercentage { const_cast<CalculatedStyleValue&>(value->as_calculated()) };
  787. VERIFY_NOT_REACHED();
  788. }
  789. Optional<CSS::FontVariant> StyleProperties::font_variant() const
  790. {
  791. auto value = property(CSS::PropertyID::FontVariant);
  792. return value_id_to_font_variant(value->to_identifier());
  793. }
  794. CSS::GridTrackSizeList StyleProperties::grid_auto_columns() const
  795. {
  796. auto value = property(CSS::PropertyID::GridAutoColumns);
  797. return value->as_grid_track_size_list().grid_track_size_list();
  798. }
  799. CSS::GridTrackSizeList StyleProperties::grid_auto_rows() const
  800. {
  801. auto value = property(CSS::PropertyID::GridAutoRows);
  802. return value->as_grid_track_size_list().grid_track_size_list();
  803. }
  804. CSS::GridTrackSizeList StyleProperties::grid_template_columns() const
  805. {
  806. auto value = property(CSS::PropertyID::GridTemplateColumns);
  807. return value->as_grid_track_size_list().grid_track_size_list();
  808. }
  809. CSS::GridTrackSizeList StyleProperties::grid_template_rows() const
  810. {
  811. auto value = property(CSS::PropertyID::GridTemplateRows);
  812. return value->as_grid_track_size_list().grid_track_size_list();
  813. }
  814. CSS::GridAutoFlow StyleProperties::grid_auto_flow() const
  815. {
  816. auto value = property(CSS::PropertyID::GridAutoFlow);
  817. if (!value->is_grid_auto_flow())
  818. return CSS::GridAutoFlow {};
  819. auto& grid_auto_flow_value = value->as_grid_auto_flow();
  820. return CSS::GridAutoFlow { .row = grid_auto_flow_value.is_row(), .dense = grid_auto_flow_value.is_dense() };
  821. }
  822. CSS::GridTrackPlacement StyleProperties::grid_column_end() const
  823. {
  824. auto value = property(CSS::PropertyID::GridColumnEnd);
  825. return value->as_grid_track_placement().grid_track_placement();
  826. }
  827. CSS::GridTrackPlacement StyleProperties::grid_column_start() const
  828. {
  829. auto value = property(CSS::PropertyID::GridColumnStart);
  830. return value->as_grid_track_placement().grid_track_placement();
  831. }
  832. CSS::GridTrackPlacement StyleProperties::grid_row_end() const
  833. {
  834. auto value = property(CSS::PropertyID::GridRowEnd);
  835. return value->as_grid_track_placement().grid_track_placement();
  836. }
  837. CSS::GridTrackPlacement StyleProperties::grid_row_start() const
  838. {
  839. auto value = property(CSS::PropertyID::GridRowStart);
  840. return value->as_grid_track_placement().grid_track_placement();
  841. }
  842. Optional<CSS::BorderCollapse> StyleProperties::border_collapse() const
  843. {
  844. auto value = property(CSS::PropertyID::BorderCollapse);
  845. return value_id_to_border_collapse(value->to_identifier());
  846. }
  847. Vector<Vector<String>> StyleProperties::grid_template_areas() const
  848. {
  849. auto value = property(CSS::PropertyID::GridTemplateAreas);
  850. return value->as_grid_template_area().grid_template_area();
  851. }
  852. String StyleProperties::grid_area() const
  853. {
  854. auto value = property(CSS::PropertyID::GridArea);
  855. return value->as_string().string_value();
  856. }
  857. Optional<CSS::ObjectFit> StyleProperties::object_fit() const
  858. {
  859. auto value = property(CSS::PropertyID::ObjectFit);
  860. return value_id_to_object_fit(value->to_identifier());
  861. }
  862. CSS::PositionStyleValue const& StyleProperties::object_position() const
  863. {
  864. auto value = property(CSS::PropertyID::ObjectPosition);
  865. return value->as_position();
  866. }
  867. Optional<CSS::TableLayout> StyleProperties::table_layout() const
  868. {
  869. auto value = property(CSS::PropertyID::TableLayout);
  870. return value_id_to_table_layout(value->to_identifier());
  871. }
  872. Optional<CSS::MaskType> StyleProperties::mask_type() const
  873. {
  874. auto value = property(CSS::PropertyID::MaskType);
  875. return value_id_to_mask_type(value->to_identifier());
  876. }
  877. Color StyleProperties::stop_color() const
  878. {
  879. auto value = property(CSS::PropertyID::StopColor);
  880. if (value->is_identifier()) {
  881. // Workaround lack of layout node to resolve current color.
  882. auto& ident = value->as_identifier();
  883. if (ident.id() == CSS::ValueID::Currentcolor)
  884. value = property(CSS::PropertyID::Color);
  885. }
  886. if (value->has_color()) {
  887. // FIXME: This is used by the SVGStopElement, which does not participate in layout,
  888. // so can't pass a layout node (so can't resolve some colors, e.g. palette ones)
  889. return value->to_color({});
  890. }
  891. return Color::Black;
  892. }
  893. void StyleProperties::set_math_depth(int math_depth)
  894. {
  895. m_math_depth = math_depth;
  896. // Make our children inherit our computed value, not our specified value.
  897. set_property(PropertyID::MathDepth, MathDepthStyleValue::create_integer(IntegerStyleValue::create(math_depth)));
  898. }
  899. QuotesData StyleProperties::quotes() const
  900. {
  901. auto value = property(CSS::PropertyID::Quotes);
  902. if (value->is_identifier()) {
  903. switch (value->to_identifier()) {
  904. case ValueID::Auto:
  905. return QuotesData { .type = QuotesData::Type::Auto };
  906. case ValueID::None:
  907. return QuotesData { .type = QuotesData::Type::None };
  908. default:
  909. break;
  910. }
  911. }
  912. if (value->is_value_list()) {
  913. auto& value_list = value->as_value_list();
  914. QuotesData quotes_data { .type = QuotesData::Type::Specified };
  915. VERIFY(value_list.size() % 2 == 0);
  916. for (auto i = 0u; i < value_list.size(); i += 2) {
  917. quotes_data.strings.empend(
  918. value_list.value_at(i, false)->as_string().string_value(),
  919. value_list.value_at(i + 1, false)->as_string().string_value());
  920. }
  921. return quotes_data;
  922. }
  923. return InitialValues::quotes();
  924. }
  925. }