StyleProperties.cpp 41 KB

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