StyleProperties.cpp 33 KB

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