StyleProperties.cpp 22 KB

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