StyleProperties.cpp 39 KB

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