StyleProperties.cpp 37 KB

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