StyleProperties.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  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/PercentageStyleValue.h>
  18. #include <LibWeb/CSS/StyleValues/RectStyleValue.h>
  19. #include <LibWeb/CSS/StyleValues/ShadowStyleValue.h>
  20. #include <LibWeb/CSS/StyleValues/StringStyleValue.h>
  21. #include <LibWeb/CSS/StyleValues/StyleValueList.h>
  22. #include <LibWeb/CSS/StyleValues/TransformationStyleValue.h>
  23. #include <LibWeb/FontCache.h>
  24. #include <LibWeb/Layout/BlockContainer.h>
  25. #include <LibWeb/Layout/Node.h>
  26. #include <LibWeb/Platform/FontPlugin.h>
  27. namespace Web::CSS {
  28. StyleProperties::StyleProperties(StyleProperties const& other)
  29. : m_property_values(other.m_property_values)
  30. {
  31. if (other.m_font) {
  32. m_font = other.m_font->clone();
  33. } else {
  34. m_font = nullptr;
  35. }
  36. }
  37. NonnullRefPtr<StyleProperties> StyleProperties::clone() const
  38. {
  39. return adopt_ref(*new StyleProperties(*this));
  40. }
  41. void StyleProperties::set_property(CSS::PropertyID id, NonnullRefPtr<StyleValue const> value)
  42. {
  43. m_property_values[to_underlying(id)] = move(value);
  44. }
  45. NonnullRefPtr<StyleValue const> StyleProperties::property(CSS::PropertyID property_id) const
  46. {
  47. auto value = m_property_values[to_underlying(property_id)];
  48. // By the time we call this method, all properties have values assigned.
  49. VERIFY(!value.is_null());
  50. return value.release_nonnull();
  51. }
  52. RefPtr<StyleValue const> StyleProperties::maybe_null_property(CSS::PropertyID property_id) const
  53. {
  54. return m_property_values[to_underlying(property_id)];
  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::None:
  68. return CSS::Size::make_none();
  69. default:
  70. VERIFY_NOT_REACHED();
  71. }
  72. }
  73. if (value->is_calculated())
  74. return CSS::Size::make_calculated(const_cast<CalculatedStyleValue&>(value->as_calculated()));
  75. if (value->is_percentage())
  76. return CSS::Size::make_percentage(value->as_percentage().percentage());
  77. if (value->has_length()) {
  78. auto length = value->to_length();
  79. if (length.is_auto())
  80. return CSS::Size::make_auto();
  81. return CSS::Size::make_length(value->to_length());
  82. }
  83. // FIXME: Support `fit-content(<length>)`
  84. dbgln("FIXME: Unsupported size value: `{}`, treating as `auto`", value->to_string());
  85. return CSS::Size::make_auto();
  86. }
  87. LengthPercentage StyleProperties::length_percentage_or_fallback(CSS::PropertyID id, LengthPercentage const& fallback) const
  88. {
  89. return length_percentage(id).value_or(fallback);
  90. }
  91. Optional<LengthPercentage> StyleProperties::length_percentage(CSS::PropertyID id) const
  92. {
  93. auto value = property(id);
  94. if (value->is_calculated())
  95. return LengthPercentage { const_cast<CalculatedStyleValue&>(value->as_calculated()) };
  96. if (value->is_percentage())
  97. return value->as_percentage().percentage();
  98. if (value->has_length())
  99. return value->to_length();
  100. if (value->has_auto())
  101. return LengthPercentage { Length::make_auto() };
  102. return {};
  103. }
  104. 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
  105. {
  106. LengthBox box;
  107. box.left() = length_percentage_or_fallback(left_id, default_value);
  108. box.top() = length_percentage_or_fallback(top_id, default_value);
  109. box.right() = length_percentage_or_fallback(right_id, default_value);
  110. box.bottom() = length_percentage_or_fallback(bottom_id, default_value);
  111. return box;
  112. }
  113. Color StyleProperties::color_or_fallback(CSS::PropertyID id, Layout::NodeWithStyle const& node, Color fallback) const
  114. {
  115. auto value = property(id);
  116. if (!value->has_color())
  117. return fallback;
  118. return value->to_color(node);
  119. }
  120. NonnullRefPtr<Gfx::Font const> StyleProperties::font_fallback(bool monospace, bool bold)
  121. {
  122. if (monospace && bold)
  123. return Platform::FontPlugin::the().default_fixed_width_font().bold_variant();
  124. if (monospace)
  125. return Platform::FontPlugin::the().default_fixed_width_font();
  126. if (bold)
  127. return Platform::FontPlugin::the().default_font().bold_variant();
  128. return Platform::FontPlugin::the().default_font();
  129. }
  130. // FIXME: This implementation is almost identical to line_height(Layout::Node) below. Maybe they can be combined somehow.
  131. CSSPixels StyleProperties::line_height(CSSPixelRect const& viewport_rect, Length::FontMetrics const& font_metrics, Length::FontMetrics const& root_font_metrics) const
  132. {
  133. auto line_height = property(CSS::PropertyID::LineHeight);
  134. if (line_height->is_identifier() && line_height->to_identifier() == ValueID::Normal)
  135. return font_metrics.line_height;
  136. if (line_height->is_length()) {
  137. auto line_height_length = line_height->to_length();
  138. if (!line_height_length.is_auto())
  139. return line_height_length.to_px(viewport_rect, font_metrics, root_font_metrics);
  140. }
  141. if (line_height->is_numeric())
  142. return Length(line_height->to_number(), Length::Type::Em).to_px(viewport_rect, font_metrics, root_font_metrics);
  143. if (line_height->is_percentage()) {
  144. // Percentages are relative to 1em. https://www.w3.org/TR/css-inline-3/#valdef-line-height-percentage
  145. auto& percentage = line_height->as_percentage().percentage();
  146. return Length(percentage.as_fraction(), Length::Type::Em).to_px(viewport_rect, font_metrics, root_font_metrics);
  147. }
  148. if (line_height->is_calculated()) {
  149. // FIXME: Handle `line-height: calc(...)` despite not having a LayoutNode here.
  150. return font_metrics.line_height;
  151. }
  152. return font_metrics.line_height;
  153. }
  154. CSSPixels StyleProperties::line_height(Layout::Node const& layout_node) const
  155. {
  156. auto line_height = property(CSS::PropertyID::LineHeight);
  157. if (line_height->is_identifier() && line_height->to_identifier() == ValueID::Normal)
  158. return layout_node.font().pixel_metrics().line_spacing();
  159. if (line_height->is_length()) {
  160. auto line_height_length = line_height->to_length();
  161. if (!line_height_length.is_auto())
  162. return line_height_length.to_px(layout_node);
  163. }
  164. if (line_height->is_numeric())
  165. return Length(line_height->to_number(), Length::Type::Em).to_px(layout_node);
  166. if (line_height->is_percentage()) {
  167. // Percentages are relative to 1em. https://www.w3.org/TR/css-inline-3/#valdef-line-height-percentage
  168. auto& percentage = line_height->as_percentage().percentage();
  169. return Length(percentage.as_fraction(), Length::Type::Em).to_px(layout_node);
  170. }
  171. if (line_height->is_calculated()) {
  172. auto resolved = line_height->as_calculated().resolve_length(layout_node);
  173. if (!resolved.has_value()) {
  174. dbgln("FIXME: Failed to resolve calc() line-height: {}", line_height->as_calculated().to_string().release_value_but_fixme_should_propagate_errors());
  175. return layout_node.font().pixel_metrics().line_spacing();
  176. }
  177. return resolved->to_px(layout_node);
  178. }
  179. return layout_node.font().pixel_metrics().line_spacing();
  180. }
  181. Optional<int> StyleProperties::z_index() const
  182. {
  183. auto value = property(CSS::PropertyID::ZIndex);
  184. if (value->has_auto())
  185. return {};
  186. if (value->has_integer()) {
  187. // Clamp z-index to the range of a signed 32-bit integer for consistency with other engines.
  188. // NOTE: Casting between 32-bit float and 32-bit integer is finicky here, since INT32_MAX is not representable as a 32-bit float!
  189. auto integer = value->to_integer();
  190. if (integer >= static_cast<float>(NumericLimits<int>::max()))
  191. return NumericLimits<int>::max();
  192. if (integer <= static_cast<float>(NumericLimits<int>::min()))
  193. return NumericLimits<int>::min();
  194. return static_cast<int>(integer);
  195. }
  196. return {};
  197. }
  198. float StyleProperties::opacity() const
  199. {
  200. auto value = property(CSS::PropertyID::Opacity);
  201. float unclamped_opacity = 1.0f;
  202. if (value->has_number()) {
  203. unclamped_opacity = value->to_number();
  204. } else if (value->is_calculated()) {
  205. auto& calculated = value->as_calculated();
  206. if (calculated.resolved_type() == CalculatedStyleValue::ResolvedType::Percentage) {
  207. auto maybe_percentage = value->as_calculated().resolve_percentage();
  208. if (maybe_percentage.has_value())
  209. unclamped_opacity = maybe_percentage->as_fraction();
  210. else
  211. dbgln("Unable to resolve calc() as opacity (percentage): {}", value->to_string());
  212. } else {
  213. auto maybe_number = const_cast<CalculatedStyleValue&>(value->as_calculated()).resolve_number();
  214. if (maybe_number.has_value())
  215. unclamped_opacity = maybe_number.value();
  216. else
  217. dbgln("Unable to resolve calc() as opacity (number): {}", value->to_string());
  218. }
  219. } else if (value->is_percentage()) {
  220. unclamped_opacity = value->as_percentage().percentage().as_fraction();
  221. }
  222. return clamp(unclamped_opacity, 0.0f, 1.0f);
  223. }
  224. Optional<CSS::FlexDirection> StyleProperties::flex_direction() const
  225. {
  226. auto value = property(CSS::PropertyID::FlexDirection);
  227. return value_id_to_flex_direction(value->to_identifier());
  228. }
  229. Optional<CSS::FlexWrap> StyleProperties::flex_wrap() const
  230. {
  231. auto value = property(CSS::PropertyID::FlexWrap);
  232. return value_id_to_flex_wrap(value->to_identifier());
  233. }
  234. Optional<CSS::FlexBasisData> StyleProperties::flex_basis() const
  235. {
  236. auto value = property(CSS::PropertyID::FlexBasis);
  237. if (value->is_identifier() && value->to_identifier() == CSS::ValueID::Content)
  238. return { { CSS::FlexBasis::Content, {} } };
  239. if (value->has_auto())
  240. return { { CSS::FlexBasis::Auto, {} } };
  241. if (value->is_percentage())
  242. return { { CSS::FlexBasis::LengthPercentage, value->as_percentage().percentage() } };
  243. if (value->has_length())
  244. return { { CSS::FlexBasis::LengthPercentage, value->to_length() } };
  245. if (value->is_calculated())
  246. return { { CSS::FlexBasis::LengthPercentage, CSS::LengthPercentage { value->as_calculated() } } };
  247. return {};
  248. }
  249. float StyleProperties::flex_grow() const
  250. {
  251. auto value = property(CSS::PropertyID::FlexGrow);
  252. if (!value->has_number())
  253. return 0;
  254. return value->to_number();
  255. }
  256. float StyleProperties::flex_shrink() const
  257. {
  258. auto value = property(CSS::PropertyID::FlexShrink);
  259. if (!value->has_number())
  260. return 1;
  261. return value->to_number();
  262. }
  263. int StyleProperties::order() const
  264. {
  265. auto value = property(CSS::PropertyID::Order);
  266. if (!value->has_integer())
  267. return 0;
  268. return value->to_integer();
  269. }
  270. Optional<CSS::ImageRendering> StyleProperties::image_rendering() const
  271. {
  272. auto value = property(CSS::PropertyID::ImageRendering);
  273. return value_id_to_image_rendering(value->to_identifier());
  274. }
  275. CSS::Clip StyleProperties::clip() const
  276. {
  277. auto value = property(CSS::PropertyID::Clip);
  278. if (!value->has_rect())
  279. return CSS::Clip::make_auto();
  280. return CSS::Clip(value->as_rect().rect());
  281. }
  282. Optional<CSS::JustifyContent> StyleProperties::justify_content() const
  283. {
  284. auto value = property(CSS::PropertyID::JustifyContent);
  285. return value_id_to_justify_content(value->to_identifier());
  286. }
  287. Vector<CSS::Transformation> StyleProperties::transformations() const
  288. {
  289. auto value = property(CSS::PropertyID::Transform);
  290. if (value->is_identifier() && value->to_identifier() == CSS::ValueID::None)
  291. return {};
  292. if (!value->is_value_list())
  293. return {};
  294. auto& list = value->as_value_list();
  295. Vector<CSS::Transformation> transformations;
  296. for (auto& it : list.values()) {
  297. if (!it->is_transformation())
  298. return {};
  299. auto& transformation_style_value = it->as_transformation();
  300. CSS::Transformation transformation;
  301. transformation.function = transformation_style_value.transform_function();
  302. Vector<TransformValue> values;
  303. for (auto& transformation_value : transformation_style_value.values()) {
  304. if (transformation_value->is_length()) {
  305. values.append({ transformation_value->to_length() });
  306. } else if (transformation_value->is_percentage()) {
  307. values.append({ transformation_value->as_percentage().percentage() });
  308. } else if (transformation_value->is_numeric()) {
  309. values.append({ transformation_value->to_number() });
  310. } else if (transformation_value->is_angle()) {
  311. values.append({ transformation_value->as_angle().angle() });
  312. } else {
  313. dbgln("FIXME: Unsupported value in transform!");
  314. }
  315. }
  316. transformation.values = move(values);
  317. transformations.append(move(transformation));
  318. }
  319. return transformations;
  320. }
  321. static Optional<LengthPercentage> length_percentage_for_style_value(StyleValue const& value)
  322. {
  323. if (value.is_length())
  324. return value.to_length();
  325. if (value.is_percentage())
  326. return value.as_percentage().percentage();
  327. return {};
  328. }
  329. CSS::TransformOrigin StyleProperties::transform_origin() const
  330. {
  331. auto value = property(CSS::PropertyID::TransformOrigin);
  332. if (!value->is_value_list() || value->as_value_list().size() != 2)
  333. return {};
  334. auto const& list = value->as_value_list();
  335. auto x_value = length_percentage_for_style_value(list.values()[0]);
  336. auto y_value = length_percentage_for_style_value(list.values()[1]);
  337. if (!x_value.has_value() || !y_value.has_value()) {
  338. return {};
  339. }
  340. return { x_value.value(), y_value.value() };
  341. }
  342. Optional<Color> StyleProperties::accent_color(Layout::NodeWithStyle const& node) const
  343. {
  344. auto value = property(CSS::PropertyID::AccentColor);
  345. if (value->has_color())
  346. return value->to_color(node);
  347. return {};
  348. }
  349. Optional<CSS::AlignContent> StyleProperties::align_content() const
  350. {
  351. auto value = property(CSS::PropertyID::AlignContent);
  352. return value_id_to_align_content(value->to_identifier());
  353. }
  354. Optional<CSS::AlignItems> StyleProperties::align_items() const
  355. {
  356. auto value = property(CSS::PropertyID::AlignItems);
  357. return value_id_to_align_items(value->to_identifier());
  358. }
  359. Optional<CSS::AlignSelf> StyleProperties::align_self() const
  360. {
  361. auto value = property(CSS::PropertyID::AlignSelf);
  362. return value_id_to_align_self(value->to_identifier());
  363. }
  364. Optional<CSS::Appearance> StyleProperties::appearance() const
  365. {
  366. auto value = property(CSS::PropertyID::Appearance);
  367. auto appearance = value_id_to_appearance(value->to_identifier());
  368. if (appearance.has_value()) {
  369. switch (*appearance) {
  370. // Note: All these compatibility values can be treated as 'auto'
  371. case CSS::Appearance::Textfield:
  372. case CSS::Appearance::MenulistButton:
  373. case CSS::Appearance::Searchfield:
  374. case CSS::Appearance::Textarea:
  375. case CSS::Appearance::PushButton:
  376. case CSS::Appearance::SliderHorizontal:
  377. case CSS::Appearance::Checkbox:
  378. case CSS::Appearance::Radio:
  379. case CSS::Appearance::SquareButton:
  380. case CSS::Appearance::Menulist:
  381. case CSS::Appearance::Listbox:
  382. case CSS::Appearance::Meter:
  383. case CSS::Appearance::ProgressBar:
  384. case CSS::Appearance::Button:
  385. appearance = CSS::Appearance::Auto;
  386. break;
  387. default:
  388. break;
  389. }
  390. }
  391. return appearance;
  392. }
  393. CSS::BackdropFilter StyleProperties::backdrop_filter() const
  394. {
  395. auto value = property(CSS::PropertyID::BackdropFilter);
  396. if (value->is_filter_value_list())
  397. return BackdropFilter(value->as_filter_value_list());
  398. return BackdropFilter::make_none();
  399. }
  400. Optional<CSS::Position> StyleProperties::position() const
  401. {
  402. auto value = property(CSS::PropertyID::Position);
  403. return value_id_to_position(value->to_identifier());
  404. }
  405. bool StyleProperties::operator==(StyleProperties const& other) const
  406. {
  407. if (m_property_values.size() != other.m_property_values.size())
  408. return false;
  409. for (size_t i = 0; i < m_property_values.size(); ++i) {
  410. auto const& my_ptr = m_property_values[i];
  411. auto const& other_ptr = other.m_property_values[i];
  412. if (!my_ptr) {
  413. if (other_ptr)
  414. return false;
  415. continue;
  416. }
  417. if (!other_ptr)
  418. return false;
  419. auto const& my_value = *my_ptr;
  420. auto const& other_value = *other_ptr;
  421. if (my_value.type() != other_value.type())
  422. return false;
  423. if (my_value != other_value)
  424. return false;
  425. }
  426. return true;
  427. }
  428. Optional<CSS::TextAlign> StyleProperties::text_align() const
  429. {
  430. auto value = property(CSS::PropertyID::TextAlign);
  431. return value_id_to_text_align(value->to_identifier());
  432. }
  433. Optional<CSS::TextJustify> StyleProperties::text_justify() const
  434. {
  435. auto value = property(CSS::PropertyID::TextJustify);
  436. return value_id_to_text_justify(value->to_identifier());
  437. }
  438. Optional<CSS::PointerEvents> StyleProperties::pointer_events() const
  439. {
  440. auto value = property(CSS::PropertyID::PointerEvents);
  441. return value_id_to_pointer_events(value->to_identifier());
  442. }
  443. Optional<CSS::WhiteSpace> StyleProperties::white_space() const
  444. {
  445. auto value = property(CSS::PropertyID::WhiteSpace);
  446. return value_id_to_white_space(value->to_identifier());
  447. }
  448. Optional<CSS::LineStyle> StyleProperties::line_style(CSS::PropertyID property_id) const
  449. {
  450. auto value = property(property_id);
  451. return value_id_to_line_style(value->to_identifier());
  452. }
  453. Optional<CSS::Float> StyleProperties::float_() const
  454. {
  455. auto value = property(CSS::PropertyID::Float);
  456. return value_id_to_float(value->to_identifier());
  457. }
  458. Optional<CSS::Clear> StyleProperties::clear() const
  459. {
  460. auto value = property(CSS::PropertyID::Clear);
  461. return value_id_to_clear(value->to_identifier());
  462. }
  463. CSS::ContentData StyleProperties::content() const
  464. {
  465. auto value = property(CSS::PropertyID::Content);
  466. if (value->is_content()) {
  467. auto& content_style_value = value->as_content();
  468. CSS::ContentData content_data;
  469. // FIXME: The content is a list of things: strings, identifiers or functions that return strings, and images.
  470. // So it can't always be represented as a single String, but may have to be multiple boxes.
  471. // For now, we'll just assume strings since that is easiest.
  472. StringBuilder builder;
  473. for (auto const& item : content_style_value.content().values()) {
  474. if (item->is_string()) {
  475. builder.append(item->to_string().release_value_but_fixme_should_propagate_errors());
  476. } else {
  477. // TODO: Implement quotes, counters, images, and other things.
  478. }
  479. }
  480. content_data.type = ContentData::Type::String;
  481. content_data.data = builder.to_string().release_value_but_fixme_should_propagate_errors();
  482. if (content_style_value.has_alt_text()) {
  483. StringBuilder alt_text_builder;
  484. for (auto const& item : content_style_value.alt_text()->values()) {
  485. if (item->is_string()) {
  486. alt_text_builder.append(item->to_string().release_value_but_fixme_should_propagate_errors());
  487. } else {
  488. // TODO: Implement counters
  489. }
  490. }
  491. content_data.alt_text = alt_text_builder.to_string().release_value_but_fixme_should_propagate_errors();
  492. }
  493. return content_data;
  494. }
  495. switch (value->to_identifier()) {
  496. case ValueID::None:
  497. return { ContentData::Type::None };
  498. case ValueID::Normal:
  499. return { ContentData::Type::Normal };
  500. default:
  501. break;
  502. }
  503. return CSS::ContentData {};
  504. }
  505. Optional<CSS::Cursor> StyleProperties::cursor() const
  506. {
  507. auto value = property(CSS::PropertyID::Cursor);
  508. return value_id_to_cursor(value->to_identifier());
  509. }
  510. Optional<CSS::Visibility> StyleProperties::visibility() const
  511. {
  512. auto value = property(CSS::PropertyID::Visibility);
  513. if (!value->is_identifier())
  514. return {};
  515. return value_id_to_visibility(value->to_identifier());
  516. }
  517. Display StyleProperties::display() const
  518. {
  519. auto value = property(PropertyID::Display);
  520. if (value->is_display()) {
  521. return value->as_display().display();
  522. }
  523. return Display::from_short(Display::Short::Inline);
  524. }
  525. Vector<CSS::TextDecorationLine> StyleProperties::text_decoration_line() const
  526. {
  527. auto value = property(CSS::PropertyID::TextDecorationLine);
  528. if (value->is_value_list()) {
  529. Vector<CSS::TextDecorationLine> lines;
  530. auto& values = value->as_value_list().values();
  531. for (auto const& item : values) {
  532. lines.append(value_id_to_text_decoration_line(item->to_identifier()).value());
  533. }
  534. return lines;
  535. }
  536. if (value->is_identifier() && value->to_identifier() == ValueID::None)
  537. return {};
  538. dbgln("FIXME: Unsupported value for text-decoration-line: {}", value->to_string());
  539. return {};
  540. }
  541. Optional<CSS::TextDecorationStyle> StyleProperties::text_decoration_style() const
  542. {
  543. auto value = property(CSS::PropertyID::TextDecorationStyle);
  544. return value_id_to_text_decoration_style(value->to_identifier());
  545. }
  546. Optional<CSS::TextTransform> StyleProperties::text_transform() const
  547. {
  548. auto value = property(CSS::PropertyID::TextTransform);
  549. return value_id_to_text_transform(value->to_identifier());
  550. }
  551. Optional<CSS::ListStyleType> StyleProperties::list_style_type() const
  552. {
  553. auto value = property(CSS::PropertyID::ListStyleType);
  554. return value_id_to_list_style_type(value->to_identifier());
  555. }
  556. Optional<CSS::Overflow> StyleProperties::overflow_x() const
  557. {
  558. return overflow(CSS::PropertyID::OverflowX);
  559. }
  560. Optional<CSS::Overflow> StyleProperties::overflow_y() const
  561. {
  562. return overflow(CSS::PropertyID::OverflowY);
  563. }
  564. Optional<CSS::Overflow> StyleProperties::overflow(CSS::PropertyID property_id) const
  565. {
  566. auto value = property(property_id);
  567. return value_id_to_overflow(value->to_identifier());
  568. }
  569. Vector<ShadowData> StyleProperties::shadow(PropertyID property_id) const
  570. {
  571. auto value = property(property_id);
  572. auto make_shadow_data = [](ShadowStyleValue const& value) {
  573. return ShadowData { value.color(), value.offset_x(), value.offset_y(), value.blur_radius(), value.spread_distance(), value.placement() };
  574. };
  575. if (value->is_value_list()) {
  576. auto& value_list = value->as_value_list();
  577. Vector<ShadowData> shadow_data;
  578. shadow_data.ensure_capacity(value_list.size());
  579. for (auto const& layer_value : value_list.values())
  580. shadow_data.append(make_shadow_data(layer_value->as_shadow()));
  581. return shadow_data;
  582. }
  583. if (value->is_shadow()) {
  584. auto& box = value->as_shadow();
  585. return { make_shadow_data(box) };
  586. }
  587. return {};
  588. }
  589. Vector<ShadowData> StyleProperties::box_shadow() const
  590. {
  591. return shadow(PropertyID::BoxShadow);
  592. }
  593. Vector<ShadowData> StyleProperties::text_shadow() const
  594. {
  595. return shadow(PropertyID::TextShadow);
  596. }
  597. Optional<CSS::BoxSizing> StyleProperties::box_sizing() const
  598. {
  599. auto value = property(CSS::PropertyID::BoxSizing);
  600. return value_id_to_box_sizing(value->to_identifier());
  601. }
  602. Variant<CSS::VerticalAlign, CSS::LengthPercentage> StyleProperties::vertical_align() const
  603. {
  604. auto value = property(CSS::PropertyID::VerticalAlign);
  605. if (value->is_identifier())
  606. return value_id_to_vertical_align(value->to_identifier()).release_value();
  607. if (value->is_length())
  608. return CSS::LengthPercentage(value->to_length());
  609. if (value->is_percentage())
  610. return CSS::LengthPercentage(value->as_percentage().percentage());
  611. VERIFY_NOT_REACHED();
  612. }
  613. Optional<CSS::FontVariant> StyleProperties::font_variant() const
  614. {
  615. auto value = property(CSS::PropertyID::FontVariant);
  616. return value_id_to_font_variant(value->to_identifier());
  617. }
  618. CSS::GridTrackSizeList StyleProperties::grid_template_columns() const
  619. {
  620. auto value = property(CSS::PropertyID::GridTemplateColumns);
  621. return value->as_grid_track_size_list().grid_track_size_list();
  622. }
  623. CSS::GridTrackSizeList StyleProperties::grid_template_rows() const
  624. {
  625. auto value = property(CSS::PropertyID::GridTemplateRows);
  626. return value->as_grid_track_size_list().grid_track_size_list();
  627. }
  628. CSS::GridTrackPlacement StyleProperties::grid_column_end() const
  629. {
  630. auto value = property(CSS::PropertyID::GridColumnEnd);
  631. return value->as_grid_track_placement().grid_track_placement();
  632. }
  633. CSS::GridTrackPlacement StyleProperties::grid_column_start() const
  634. {
  635. auto value = property(CSS::PropertyID::GridColumnStart);
  636. return value->as_grid_track_placement().grid_track_placement();
  637. }
  638. CSS::GridTrackPlacement StyleProperties::grid_row_end() const
  639. {
  640. auto value = property(CSS::PropertyID::GridRowEnd);
  641. return value->as_grid_track_placement().grid_track_placement();
  642. }
  643. CSS::GridTrackPlacement StyleProperties::grid_row_start() const
  644. {
  645. auto value = property(CSS::PropertyID::GridRowStart);
  646. return value->as_grid_track_placement().grid_track_placement();
  647. }
  648. Optional<CSS::BorderCollapse> StyleProperties::border_collapse() const
  649. {
  650. auto value = property(CSS::PropertyID::BorderCollapse);
  651. return value_id_to_border_collapse(value->to_identifier());
  652. }
  653. Vector<Vector<String>> StyleProperties::grid_template_areas() const
  654. {
  655. auto value = property(CSS::PropertyID::GridTemplateAreas);
  656. return value->as_grid_template_area().grid_template_area();
  657. }
  658. String StyleProperties::grid_area() const
  659. {
  660. auto value = property(CSS::PropertyID::GridArea);
  661. return value->as_string().to_string().release_value_but_fixme_should_propagate_errors();
  662. }
  663. Color StyleProperties::stop_color() const
  664. {
  665. auto value = property(CSS::PropertyID::StopColor);
  666. if (value->has_color()) {
  667. // FIXME: This is used by the SVGStopElement, which does not participate in layout,
  668. // so can't pass a layout node (so can't resolve some colors, e.g. palette ones or currentColor)
  669. return value->to_color({});
  670. }
  671. return Color::Black;
  672. }
  673. }