StyleProperties.cpp 41 KB

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