StyleProperties.cpp 42 KB

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