StyleProperties.cpp 24 KB

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