StyleProperties.cpp 37 KB

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