StyleProperties.cpp 22 KB

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