StyleProperties.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  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/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()
  16. {
  17. }
  18. StyleProperties::StyleProperties(const StyleProperties& other)
  19. : m_property_values(other.m_property_values)
  20. {
  21. if (other.m_font) {
  22. m_font = other.m_font->clone();
  23. } else {
  24. m_font = nullptr;
  25. }
  26. }
  27. NonnullRefPtr<StyleProperties> StyleProperties::clone() const
  28. {
  29. return adopt_ref(*new StyleProperties(*this));
  30. }
  31. void StyleProperties::set_property(CSS::PropertyID id, NonnullRefPtr<StyleValue> value)
  32. {
  33. m_property_values[to_underlying(id)] = move(value);
  34. }
  35. Optional<NonnullRefPtr<StyleValue>> StyleProperties::property(CSS::PropertyID property_id) const
  36. {
  37. auto value = m_property_values[to_underlying(property_id)];
  38. if (!value)
  39. return {};
  40. return value.release_nonnull();
  41. }
  42. Length StyleProperties::length_or_fallback(CSS::PropertyID id, Length const& fallback) const
  43. {
  44. auto maybe_value = property(id);
  45. if (!maybe_value.has_value())
  46. return fallback;
  47. auto& value = maybe_value.value();
  48. if (value->is_calculated())
  49. return Length::make_calculated(value->as_calculated());
  50. if (value->has_length())
  51. return value->to_length();
  52. return fallback;
  53. }
  54. LengthPercentage StyleProperties::length_percentage_or_fallback(CSS::PropertyID id, LengthPercentage const& fallback) const
  55. {
  56. return length_percentage(id).value_or(fallback);
  57. }
  58. Optional<LengthPercentage> StyleProperties::length_percentage(CSS::PropertyID id) const
  59. {
  60. auto maybe_value = property(id);
  61. if (!maybe_value.has_value())
  62. return {};
  63. auto& value = maybe_value.value();
  64. if (value->is_calculated())
  65. return LengthPercentage { value->as_calculated() };
  66. if (value->is_percentage())
  67. return value->as_percentage().percentage();
  68. if (value->has_length())
  69. return value->to_length();
  70. return {};
  71. }
  72. 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
  73. {
  74. LengthBox box;
  75. box.left = length_percentage_or_fallback(left_id, default_value);
  76. box.top = length_percentage_or_fallback(top_id, default_value);
  77. box.right = length_percentage_or_fallback(right_id, default_value);
  78. box.bottom = length_percentage_or_fallback(bottom_id, default_value);
  79. return box;
  80. }
  81. Color StyleProperties::color_or_fallback(CSS::PropertyID id, Layout::NodeWithStyle const& node, Color fallback) const
  82. {
  83. auto value = property(id);
  84. if (!value.has_value() || !value.value()->has_color())
  85. return fallback;
  86. return value.value()->to_color(node);
  87. }
  88. NonnullRefPtr<Gfx::Font> StyleProperties::font_fallback(bool monospace, bool bold)
  89. {
  90. if (monospace && bold)
  91. return Gfx::FontDatabase::default_fixed_width_font().bold_variant();
  92. if (monospace)
  93. return Gfx::FontDatabase::default_fixed_width_font();
  94. if (bold)
  95. return Gfx::FontDatabase::default_font().bold_variant();
  96. return Gfx::FontDatabase::default_font();
  97. }
  98. float StyleProperties::line_height(Layout::Node const& layout_node) const
  99. {
  100. constexpr float font_height_to_line_height_multiplier = 1.4f;
  101. if (auto maybe_line_height = property(CSS::PropertyID::LineHeight); maybe_line_height.has_value()) {
  102. auto line_height = maybe_line_height.release_value();
  103. if (line_height->is_identifier() && line_height->to_identifier() == ValueID::Normal)
  104. return Length(1, Length::Type::Em).to_px(layout_node) * font_height_to_line_height_multiplier;
  105. if (line_height->is_length()) {
  106. auto line_height_length = line_height->to_length();
  107. if (!line_height_length.is_auto())
  108. return line_height_length.to_px(layout_node);
  109. }
  110. if (line_height->is_numeric())
  111. return Length(line_height->to_number(), Length::Type::Em).to_px(layout_node);
  112. if (line_height->is_percentage()) {
  113. // Percentages are relative to 1em. https://www.w3.org/TR/css-inline-3/#valdef-line-height-percentage
  114. auto& percentage = line_height->as_percentage().percentage();
  115. return Length(percentage.as_fraction(), Length::Type::Em).to_px(layout_node);
  116. }
  117. }
  118. return Length(font_height_to_line_height_multiplier, Length::Type::Em).to_px(layout_node);
  119. }
  120. Optional<int> StyleProperties::z_index() const
  121. {
  122. auto maybe_value = property(CSS::PropertyID::ZIndex);
  123. if (!maybe_value.has_value())
  124. return {};
  125. auto& value = maybe_value.value();
  126. if (value->has_auto())
  127. return 0;
  128. if (value->has_integer())
  129. return value->to_integer();
  130. return {};
  131. }
  132. float StyleProperties::opacity() const
  133. {
  134. auto maybe_value = property(CSS::PropertyID::Opacity);
  135. if (!maybe_value.has_value())
  136. return 1.0f;
  137. auto& value = maybe_value.value();
  138. float unclamped_opacity = 1.0f;
  139. if (value->has_number()) {
  140. unclamped_opacity = value->to_number();
  141. } else if (value->is_calculated()) {
  142. auto& calculated = value->as_calculated();
  143. if (calculated.resolved_type() == CalculatedStyleValue::ResolvedType::Percentage) {
  144. auto maybe_percentage = value->as_calculated().resolve_percentage();
  145. if (maybe_percentage.has_value())
  146. unclamped_opacity = maybe_percentage->as_fraction();
  147. else
  148. dbgln("Unable to resolve calc() as opacity (percentage): {}", value->to_string());
  149. } else {
  150. auto maybe_number = value->as_calculated().resolve_number();
  151. if (maybe_number.has_value())
  152. unclamped_opacity = maybe_number.value();
  153. else
  154. dbgln("Unable to resolve calc() as opacity (number): {}", value->to_string());
  155. }
  156. } else if (value->is_percentage()) {
  157. unclamped_opacity = value->as_percentage().percentage().as_fraction();
  158. }
  159. return clamp(unclamped_opacity, 0.0f, 1.0f);
  160. }
  161. Optional<CSS::FlexDirection> StyleProperties::flex_direction() const
  162. {
  163. auto value = property(CSS::PropertyID::FlexDirection);
  164. if (!value.has_value())
  165. return {};
  166. switch (value.value()->to_identifier()) {
  167. case CSS::ValueID::Row:
  168. return CSS::FlexDirection::Row;
  169. case CSS::ValueID::RowReverse:
  170. return CSS::FlexDirection::RowReverse;
  171. case CSS::ValueID::Column:
  172. return CSS::FlexDirection::Column;
  173. case CSS::ValueID::ColumnReverse:
  174. return CSS::FlexDirection::ColumnReverse;
  175. default:
  176. return {};
  177. }
  178. }
  179. Optional<CSS::FlexWrap> StyleProperties::flex_wrap() const
  180. {
  181. auto value = property(CSS::PropertyID::FlexWrap);
  182. if (!value.has_value())
  183. return {};
  184. switch (value.value()->to_identifier()) {
  185. case CSS::ValueID::Wrap:
  186. return CSS::FlexWrap::Wrap;
  187. case CSS::ValueID::Nowrap:
  188. return CSS::FlexWrap::Nowrap;
  189. case CSS::ValueID::WrapReverse:
  190. return CSS::FlexWrap::WrapReverse;
  191. default:
  192. return {};
  193. }
  194. }
  195. Optional<CSS::FlexBasisData> StyleProperties::flex_basis() const
  196. {
  197. auto maybe_value = property(CSS::PropertyID::FlexBasis);
  198. if (!maybe_value.has_value())
  199. return {};
  200. auto& value = maybe_value.value();
  201. if (value->is_identifier() && value->to_identifier() == CSS::ValueID::Content)
  202. return { { CSS::FlexBasis::Content, {} } };
  203. if (value->has_auto())
  204. return { { CSS::FlexBasis::Auto, {} } };
  205. if (value->is_percentage())
  206. return { { CSS::FlexBasis::LengthPercentage, value->as_percentage().percentage() } };
  207. if (value->has_length())
  208. return { { CSS::FlexBasis::LengthPercentage, value->to_length() } };
  209. return {};
  210. }
  211. float StyleProperties::flex_grow() const
  212. {
  213. auto value = property(CSS::PropertyID::FlexGrow);
  214. if (!value.has_value() || !value.value()->has_number())
  215. return 0;
  216. return value.value()->to_number();
  217. }
  218. float StyleProperties::flex_shrink() const
  219. {
  220. auto value = property(CSS::PropertyID::FlexShrink);
  221. if (!value.has_value() || !value.value()->has_number())
  222. return 1;
  223. return value.value()->to_number();
  224. }
  225. Optional<CSS::ImageRendering> StyleProperties::image_rendering() const
  226. {
  227. auto value = property(CSS::PropertyID::ImageRendering);
  228. if (!value.has_value())
  229. return {};
  230. switch (value.value()->to_identifier()) {
  231. case CSS::ValueID::Auto:
  232. return CSS::ImageRendering::Auto;
  233. case CSS::ValueID::Pixelated:
  234. return CSS::ImageRendering::Pixelated;
  235. default:
  236. return {};
  237. }
  238. }
  239. Optional<CSS::JustifyContent> StyleProperties::justify_content() const
  240. {
  241. auto value = property(CSS::PropertyID::JustifyContent);
  242. if (!value.has_value())
  243. return {};
  244. switch (value.value()->to_identifier()) {
  245. case CSS::ValueID::FlexStart:
  246. return CSS::JustifyContent::FlexStart;
  247. case CSS::ValueID::FlexEnd:
  248. return CSS::JustifyContent::FlexEnd;
  249. case CSS::ValueID::Center:
  250. return CSS::JustifyContent::Center;
  251. case CSS::ValueID::SpaceBetween:
  252. return CSS::JustifyContent::SpaceBetween;
  253. case CSS::ValueID::SpaceAround:
  254. return CSS::JustifyContent::SpaceAround;
  255. default:
  256. return {};
  257. }
  258. }
  259. Vector<CSS::Transformation> StyleProperties::transformations() const
  260. {
  261. auto value = property(CSS::PropertyID::Transform);
  262. if (!value.has_value())
  263. return {};
  264. if (value.value()->is_identifier() && value.value()->to_identifier() == CSS::ValueID::None)
  265. return {};
  266. if (!value.value()->is_value_list())
  267. return {};
  268. auto& list = value.value()->as_value_list();
  269. Vector<CSS::Transformation> transformations;
  270. for (auto& it : list.values()) {
  271. if (!it.is_transformation())
  272. return {};
  273. auto& transformation_style_value = it.as_transformation();
  274. CSS::Transformation transformation;
  275. transformation.function = transformation_style_value.transform_function();
  276. Vector<Variant<CSS::Length, float>> values;
  277. for (auto& transformation_value : transformation_style_value.values()) {
  278. if (transformation_value.is_length()) {
  279. values.append({ transformation_value.to_length() });
  280. } else if (transformation_value.is_numeric()) {
  281. values.append({ transformation_value.to_number() });
  282. } else {
  283. dbgln("FIXME: Unsupported value in transform!");
  284. }
  285. }
  286. transformation.values = move(values);
  287. transformations.append(move(transformation));
  288. }
  289. return transformations;
  290. }
  291. Optional<CSS::AlignItems> StyleProperties::align_items() const
  292. {
  293. auto value = property(CSS::PropertyID::AlignItems);
  294. if (!value.has_value())
  295. return {};
  296. switch (value.value()->to_identifier()) {
  297. case CSS::ValueID::FlexStart:
  298. return CSS::AlignItems::FlexStart;
  299. case CSS::ValueID::FlexEnd:
  300. return CSS::AlignItems::FlexEnd;
  301. case CSS::ValueID::Center:
  302. return CSS::AlignItems::Center;
  303. case CSS::ValueID::Baseline:
  304. return CSS::AlignItems::Baseline;
  305. case CSS::ValueID::Stretch:
  306. return CSS::AlignItems::Stretch;
  307. default:
  308. return {};
  309. }
  310. }
  311. Optional<CSS::Position> StyleProperties::position() const
  312. {
  313. auto value = property(CSS::PropertyID::Position);
  314. if (!value.has_value())
  315. return {};
  316. switch (value.value()->to_identifier()) {
  317. case CSS::ValueID::Static:
  318. return CSS::Position::Static;
  319. case CSS::ValueID::Relative:
  320. return CSS::Position::Relative;
  321. case CSS::ValueID::Absolute:
  322. return CSS::Position::Absolute;
  323. case CSS::ValueID::Fixed:
  324. return CSS::Position::Fixed;
  325. case CSS::ValueID::Sticky:
  326. return CSS::Position::Sticky;
  327. default:
  328. return {};
  329. }
  330. }
  331. bool StyleProperties::operator==(const StyleProperties& other) const
  332. {
  333. if (m_property_values.size() != other.m_property_values.size())
  334. return false;
  335. for (size_t i = 0; i < m_property_values.size(); ++i) {
  336. auto const& my_ptr = m_property_values[i];
  337. auto const& other_ptr = m_property_values[i];
  338. if (!my_ptr) {
  339. if (other_ptr)
  340. return false;
  341. continue;
  342. }
  343. if (!other_ptr)
  344. return false;
  345. auto const& my_value = *my_ptr;
  346. auto const& other_value = *other_ptr;
  347. if (my_value.type() != other_value.type())
  348. return false;
  349. if (my_value != other_value)
  350. return false;
  351. }
  352. return true;
  353. }
  354. Optional<CSS::TextAlign> StyleProperties::text_align() const
  355. {
  356. auto value = property(CSS::PropertyID::TextAlign);
  357. if (!value.has_value())
  358. return {};
  359. switch (value.value()->to_identifier()) {
  360. case CSS::ValueID::Left:
  361. return CSS::TextAlign::Left;
  362. case CSS::ValueID::Center:
  363. return CSS::TextAlign::Center;
  364. case CSS::ValueID::Right:
  365. return CSS::TextAlign::Right;
  366. case CSS::ValueID::Justify:
  367. return CSS::TextAlign::Justify;
  368. case CSS::ValueID::LibwebCenter:
  369. return CSS::TextAlign::LibwebCenter;
  370. default:
  371. return {};
  372. }
  373. }
  374. Optional<CSS::PointerEvents> StyleProperties::pointer_events() const
  375. {
  376. auto value = property(CSS::PropertyID::PointerEvents);
  377. if (!value.has_value())
  378. return {};
  379. switch (value.value()->to_identifier()) {
  380. case CSS::ValueID::Auto:
  381. return CSS::PointerEvents::Auto;
  382. case CSS::ValueID::All:
  383. return CSS::PointerEvents::All;
  384. case CSS::ValueID::None:
  385. return CSS::PointerEvents::None;
  386. default:
  387. return {};
  388. }
  389. }
  390. Optional<CSS::WhiteSpace> StyleProperties::white_space() const
  391. {
  392. auto value = property(CSS::PropertyID::WhiteSpace);
  393. if (!value.has_value())
  394. return {};
  395. switch (value.value()->to_identifier()) {
  396. case CSS::ValueID::Normal:
  397. return CSS::WhiteSpace::Normal;
  398. case CSS::ValueID::Nowrap:
  399. return CSS::WhiteSpace::Nowrap;
  400. case CSS::ValueID::Pre:
  401. return CSS::WhiteSpace::Pre;
  402. case CSS::ValueID::PreLine:
  403. return CSS::WhiteSpace::PreLine;
  404. case CSS::ValueID::PreWrap:
  405. return CSS::WhiteSpace::PreWrap;
  406. default:
  407. return {};
  408. }
  409. }
  410. Optional<CSS::LineStyle> StyleProperties::line_style(CSS::PropertyID property_id) const
  411. {
  412. auto value = property(property_id);
  413. if (!value.has_value())
  414. return {};
  415. switch (value.value()->to_identifier()) {
  416. case CSS::ValueID::None:
  417. return CSS::LineStyle::None;
  418. case CSS::ValueID::Hidden:
  419. return CSS::LineStyle::Hidden;
  420. case CSS::ValueID::Dotted:
  421. return CSS::LineStyle::Dotted;
  422. case CSS::ValueID::Dashed:
  423. return CSS::LineStyle::Dashed;
  424. case CSS::ValueID::Solid:
  425. return CSS::LineStyle::Solid;
  426. case CSS::ValueID::Double:
  427. return CSS::LineStyle::Double;
  428. case CSS::ValueID::Groove:
  429. return CSS::LineStyle::Groove;
  430. case CSS::ValueID::Ridge:
  431. return CSS::LineStyle::Ridge;
  432. case CSS::ValueID::Inset:
  433. return CSS::LineStyle::Inset;
  434. case CSS::ValueID::Outset:
  435. return CSS::LineStyle::Outset;
  436. default:
  437. return {};
  438. }
  439. }
  440. Optional<CSS::Float> StyleProperties::float_() const
  441. {
  442. auto value = property(CSS::PropertyID::Float);
  443. if (!value.has_value())
  444. return {};
  445. switch (value.value()->to_identifier()) {
  446. case CSS::ValueID::None:
  447. return CSS::Float::None;
  448. case CSS::ValueID::Left:
  449. return CSS::Float::Left;
  450. case CSS::ValueID::Right:
  451. return CSS::Float::Right;
  452. default:
  453. return {};
  454. }
  455. }
  456. Optional<CSS::Clear> StyleProperties::clear() const
  457. {
  458. auto value = property(CSS::PropertyID::Clear);
  459. if (!value.has_value())
  460. return {};
  461. switch (value.value()->to_identifier()) {
  462. case CSS::ValueID::None:
  463. return CSS::Clear::None;
  464. case CSS::ValueID::Left:
  465. return CSS::Clear::Left;
  466. case CSS::ValueID::Right:
  467. return CSS::Clear::Right;
  468. case CSS::ValueID::Both:
  469. return CSS::Clear::Both;
  470. default:
  471. return {};
  472. }
  473. }
  474. Optional<CSS::Cursor> StyleProperties::cursor() const
  475. {
  476. auto value = property(CSS::PropertyID::Cursor);
  477. if (!value.has_value())
  478. return {};
  479. switch (value.value()->to_identifier()) {
  480. case CSS::ValueID::Auto:
  481. return CSS::Cursor::Auto;
  482. case CSS::ValueID::Default:
  483. return CSS::Cursor::Default;
  484. case CSS::ValueID::None:
  485. return CSS::Cursor::None;
  486. case CSS::ValueID::ContextMenu:
  487. return CSS::Cursor::ContextMenu;
  488. case CSS::ValueID::Help:
  489. return CSS::Cursor::Help;
  490. case CSS::ValueID::Pointer:
  491. return CSS::Cursor::Pointer;
  492. case CSS::ValueID::Progress:
  493. return CSS::Cursor::Progress;
  494. case CSS::ValueID::Wait:
  495. return CSS::Cursor::Wait;
  496. case CSS::ValueID::Cell:
  497. return CSS::Cursor::Cell;
  498. case CSS::ValueID::Crosshair:
  499. return CSS::Cursor::Crosshair;
  500. case CSS::ValueID::Text:
  501. return CSS::Cursor::Text;
  502. case CSS::ValueID::VerticalText:
  503. return CSS::Cursor::VerticalText;
  504. case CSS::ValueID::Alias:
  505. return CSS::Cursor::Alias;
  506. case CSS::ValueID::Copy:
  507. return CSS::Cursor::Copy;
  508. case CSS::ValueID::Move:
  509. return CSS::Cursor::Move;
  510. case CSS::ValueID::NoDrop:
  511. return CSS::Cursor::NoDrop;
  512. case CSS::ValueID::NotAllowed:
  513. return CSS::Cursor::NotAllowed;
  514. case CSS::ValueID::Grab:
  515. return CSS::Cursor::Grab;
  516. case CSS::ValueID::Grabbing:
  517. return CSS::Cursor::Grabbing;
  518. case CSS::ValueID::EResize:
  519. return CSS::Cursor::EResize;
  520. case CSS::ValueID::NResize:
  521. return CSS::Cursor::NResize;
  522. case CSS::ValueID::NeResize:
  523. return CSS::Cursor::NeResize;
  524. case CSS::ValueID::NwResize:
  525. return CSS::Cursor::NwResize;
  526. case CSS::ValueID::SResize:
  527. return CSS::Cursor::SResize;
  528. case CSS::ValueID::SeResize:
  529. return CSS::Cursor::SeResize;
  530. case CSS::ValueID::SwResize:
  531. return CSS::Cursor::SwResize;
  532. case CSS::ValueID::WResize:
  533. return CSS::Cursor::WResize;
  534. case CSS::ValueID::EwResize:
  535. return CSS::Cursor::EwResize;
  536. case CSS::ValueID::NsResize:
  537. return CSS::Cursor::NsResize;
  538. case CSS::ValueID::NeswResize:
  539. return CSS::Cursor::NeswResize;
  540. case CSS::ValueID::NwseResize:
  541. return CSS::Cursor::NwseResize;
  542. case CSS::ValueID::ColResize:
  543. return CSS::Cursor::ColResize;
  544. case CSS::ValueID::RowResize:
  545. return CSS::Cursor::RowResize;
  546. case CSS::ValueID::AllScroll:
  547. return CSS::Cursor::AllScroll;
  548. case CSS::ValueID::ZoomIn:
  549. return CSS::Cursor::ZoomIn;
  550. case CSS::ValueID::ZoomOut:
  551. return CSS::Cursor::ZoomOut;
  552. default:
  553. return {};
  554. }
  555. }
  556. CSS::Display StyleProperties::display() const
  557. {
  558. auto value = property(CSS::PropertyID::Display);
  559. if (!value.has_value() || !value.value()->is_identifier())
  560. return CSS::Display::from_short(CSS::Display::Short::Inline);
  561. switch (value.value()->to_identifier()) {
  562. case CSS::ValueID::None:
  563. return CSS::Display::from_short(CSS::Display::Short::None);
  564. case CSS::ValueID::Block:
  565. return CSS::Display::from_short(CSS::Display::Short::Block);
  566. case CSS::ValueID::Inline:
  567. return CSS::Display::from_short(CSS::Display::Short::Inline);
  568. case CSS::ValueID::InlineBlock:
  569. return CSS::Display::from_short(CSS::Display::Short::InlineBlock);
  570. case CSS::ValueID::ListItem:
  571. return CSS::Display::from_short(CSS::Display::Short::ListItem);
  572. case CSS::ValueID::Table:
  573. return CSS::Display::from_short(CSS::Display::Short::Table);
  574. case CSS::ValueID::TableRow:
  575. return CSS::Display { CSS::Display::Internal::TableRow };
  576. case CSS::ValueID::TableCell:
  577. return CSS::Display { CSS::Display::Internal::TableCell };
  578. case CSS::ValueID::TableColumn:
  579. return CSS::Display { CSS::Display::Internal::TableColumn };
  580. case CSS::ValueID::TableColumnGroup:
  581. return CSS::Display { CSS::Display::Internal::TableColumnGroup };
  582. case CSS::ValueID::TableCaption:
  583. return CSS::Display { CSS::Display::Internal::TableCaption };
  584. case CSS::ValueID::TableRowGroup:
  585. return CSS::Display { CSS::Display::Internal::TableRowGroup };
  586. case CSS::ValueID::TableHeaderGroup:
  587. return CSS::Display { CSS::Display::Internal::TableHeaderGroup };
  588. case CSS::ValueID::TableFooterGroup:
  589. return CSS::Display { CSS::Display::Internal::TableFooterGroup };
  590. case CSS::ValueID::Flex:
  591. return CSS::Display::from_short(CSS::Display::Short::Flex);
  592. case CSS::ValueID::InlineFlex:
  593. return CSS::Display::from_short(CSS::Display::Short::InlineFlex);
  594. default:
  595. return CSS::Display::from_short(CSS::Display::Short::Block);
  596. }
  597. }
  598. Optional<CSS::TextDecorationLine> StyleProperties::text_decoration_line() const
  599. {
  600. auto value = property(CSS::PropertyID::TextDecorationLine);
  601. if (!value.has_value())
  602. return {};
  603. switch (value.value()->to_identifier()) {
  604. case CSS::ValueID::None:
  605. return CSS::TextDecorationLine::None;
  606. case CSS::ValueID::Underline:
  607. return CSS::TextDecorationLine::Underline;
  608. case CSS::ValueID::Overline:
  609. return CSS::TextDecorationLine::Overline;
  610. case CSS::ValueID::LineThrough:
  611. return CSS::TextDecorationLine::LineThrough;
  612. case CSS::ValueID::Blink:
  613. return CSS::TextDecorationLine::Blink;
  614. default:
  615. return {};
  616. }
  617. }
  618. Optional<CSS::TextDecorationStyle> StyleProperties::text_decoration_style() const
  619. {
  620. auto value = property(CSS::PropertyID::TextDecorationStyle);
  621. if (!value.has_value())
  622. return {};
  623. switch (value.value()->to_identifier()) {
  624. case CSS::ValueID::Solid:
  625. return CSS::TextDecorationStyle::Solid;
  626. case CSS::ValueID::Double:
  627. return CSS::TextDecorationStyle::Double;
  628. case CSS::ValueID::Dotted:
  629. return CSS::TextDecorationStyle::Dotted;
  630. case CSS::ValueID::Dashed:
  631. return CSS::TextDecorationStyle::Dashed;
  632. case CSS::ValueID::Wavy:
  633. return CSS::TextDecorationStyle::Wavy;
  634. default:
  635. return {};
  636. }
  637. }
  638. Optional<CSS::TextTransform> StyleProperties::text_transform() const
  639. {
  640. auto value = property(CSS::PropertyID::TextTransform);
  641. if (!value.has_value())
  642. return {};
  643. switch (value.value()->to_identifier()) {
  644. case CSS::ValueID::None:
  645. return CSS::TextTransform::None;
  646. case CSS::ValueID::Lowercase:
  647. return CSS::TextTransform::Lowercase;
  648. case CSS::ValueID::Uppercase:
  649. return CSS::TextTransform::Uppercase;
  650. case CSS::ValueID::Capitalize:
  651. return CSS::TextTransform::Capitalize;
  652. case CSS::ValueID::FullWidth:
  653. return CSS::TextTransform::FullWidth;
  654. case CSS::ValueID::FullSizeKana:
  655. return CSS::TextTransform::FullSizeKana;
  656. default:
  657. return {};
  658. }
  659. }
  660. Optional<CSS::ListStyleType> StyleProperties::list_style_type() const
  661. {
  662. auto value = property(CSS::PropertyID::ListStyleType);
  663. if (!value.has_value())
  664. return {};
  665. switch (value.value()->to_identifier()) {
  666. case CSS::ValueID::None:
  667. return CSS::ListStyleType::None;
  668. case CSS::ValueID::Disc:
  669. return CSS::ListStyleType::Disc;
  670. case CSS::ValueID::Circle:
  671. return CSS::ListStyleType::Circle;
  672. case CSS::ValueID::Square:
  673. return CSS::ListStyleType::Square;
  674. case CSS::ValueID::Decimal:
  675. return CSS::ListStyleType::Decimal;
  676. case CSS::ValueID::DecimalLeadingZero:
  677. return CSS::ListStyleType::DecimalLeadingZero;
  678. case CSS::ValueID::LowerAlpha:
  679. return CSS::ListStyleType::LowerAlpha;
  680. case CSS::ValueID::LowerLatin:
  681. return CSS::ListStyleType::LowerLatin;
  682. case CSS::ValueID::UpperAlpha:
  683. return CSS::ListStyleType::UpperAlpha;
  684. case CSS::ValueID::UpperLatin:
  685. return CSS::ListStyleType::UpperLatin;
  686. case CSS::ValueID::UpperRoman:
  687. return CSS::ListStyleType::UpperRoman;
  688. case CSS::ValueID::LowerRoman:
  689. return CSS::ListStyleType::LowerRoman;
  690. default:
  691. return {};
  692. }
  693. }
  694. Optional<CSS::Overflow> StyleProperties::overflow_x() const
  695. {
  696. return overflow(CSS::PropertyID::OverflowX);
  697. }
  698. Optional<CSS::Overflow> StyleProperties::overflow_y() const
  699. {
  700. return overflow(CSS::PropertyID::OverflowY);
  701. }
  702. Optional<CSS::Overflow> StyleProperties::overflow(CSS::PropertyID property_id) const
  703. {
  704. auto value = property(property_id);
  705. if (!value.has_value())
  706. return {};
  707. switch (value.value()->to_identifier()) {
  708. case CSS::ValueID::Auto:
  709. return CSS::Overflow::Auto;
  710. case CSS::ValueID::Visible:
  711. return CSS::Overflow::Visible;
  712. case CSS::ValueID::Hidden:
  713. return CSS::Overflow::Hidden;
  714. case CSS::ValueID::Clip:
  715. return CSS::Overflow::Clip;
  716. case CSS::ValueID::Scroll:
  717. return CSS::Overflow::Scroll;
  718. default:
  719. return {};
  720. }
  721. }
  722. Vector<BoxShadowData> StyleProperties::box_shadow() const
  723. {
  724. auto value_or_error = property(PropertyID::BoxShadow);
  725. if (!value_or_error.has_value())
  726. return {};
  727. auto value = value_or_error.value();
  728. auto make_box_shadow_data = [](BoxShadowStyleValue const& box) {
  729. return BoxShadowData { box.color(), box.offset_x(), box.offset_y(), box.blur_radius(), box.spread_distance(), box.placement() };
  730. };
  731. if (value->is_value_list()) {
  732. auto& value_list = value->as_value_list();
  733. Vector<BoxShadowData> box_shadow_data;
  734. box_shadow_data.ensure_capacity(value_list.size());
  735. for (auto const& layer_value : value_list.values())
  736. box_shadow_data.append(make_box_shadow_data(layer_value.as_box_shadow()));
  737. return box_shadow_data;
  738. }
  739. if (value->is_box_shadow()) {
  740. auto& box = value->as_box_shadow();
  741. return { make_box_shadow_data(box) };
  742. }
  743. return {};
  744. }
  745. CSS::BoxSizing StyleProperties::box_sizing() const
  746. {
  747. auto value = property(CSS::PropertyID::BoxSizing);
  748. if (!value.has_value())
  749. return {};
  750. switch (value.value()->to_identifier()) {
  751. case CSS::ValueID::BorderBox:
  752. return CSS::BoxSizing::BorderBox;
  753. case CSS::ValueID::ContentBox:
  754. return CSS::BoxSizing::ContentBox;
  755. default:
  756. return {};
  757. }
  758. }
  759. }