StyleProperties.cpp 40 KB

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