StyleProperties.cpp 41 KB

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