StyleProperties.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  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.set(id, move(value));
  34. }
  35. Optional<NonnullRefPtr<StyleValue>> StyleProperties::property(CSS::PropertyID property_id) const
  36. {
  37. auto it = m_property_values.find(property_id);
  38. if (it == m_property_values.end())
  39. return {};
  40. return it->value;
  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_undefined_or_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::JustifyContent> StyleProperties::justify_content() const
  226. {
  227. auto value = property(CSS::PropertyID::JustifyContent);
  228. if (!value.has_value())
  229. return {};
  230. switch (value.value()->to_identifier()) {
  231. case CSS::ValueID::FlexStart:
  232. return CSS::JustifyContent::FlexStart;
  233. case CSS::ValueID::FlexEnd:
  234. return CSS::JustifyContent::FlexEnd;
  235. case CSS::ValueID::Center:
  236. return CSS::JustifyContent::Center;
  237. case CSS::ValueID::SpaceBetween:
  238. return CSS::JustifyContent::SpaceBetween;
  239. case CSS::ValueID::SpaceAround:
  240. return CSS::JustifyContent::SpaceAround;
  241. default:
  242. return {};
  243. }
  244. }
  245. Vector<CSS::Transformation> StyleProperties::transformations() const
  246. {
  247. auto value = property(CSS::PropertyID::Transform);
  248. if (!value.has_value())
  249. return {};
  250. if (value.value()->is_identifier() && value.value()->to_identifier() == CSS::ValueID::None)
  251. return {};
  252. if (!value.value()->is_value_list())
  253. return {};
  254. auto& list = value.value()->as_value_list();
  255. Vector<CSS::Transformation> transformations;
  256. for (auto& it : list.values()) {
  257. if (!it.is_transformation())
  258. return {};
  259. auto& transformation_style_value = it.as_transformation();
  260. CSS::Transformation transformation;
  261. transformation.function = transformation_style_value.transform_function();
  262. Vector<Variant<CSS::Length, float>> values;
  263. for (auto& transformation_value : transformation_style_value.values()) {
  264. if (transformation_value.is_length()) {
  265. values.append({ transformation_value.to_length() });
  266. } else if (transformation_value.is_numeric()) {
  267. values.append({ transformation_value.to_number() });
  268. } else {
  269. dbgln("FIXME: Unsupported value in transform!");
  270. }
  271. }
  272. transformation.values = move(values);
  273. transformations.append(move(transformation));
  274. }
  275. return transformations;
  276. }
  277. Optional<CSS::AlignItems> StyleProperties::align_items() const
  278. {
  279. auto value = property(CSS::PropertyID::AlignItems);
  280. if (!value.has_value())
  281. return {};
  282. switch (value.value()->to_identifier()) {
  283. case CSS::ValueID::FlexStart:
  284. return CSS::AlignItems::FlexStart;
  285. case CSS::ValueID::FlexEnd:
  286. return CSS::AlignItems::FlexEnd;
  287. case CSS::ValueID::Center:
  288. return CSS::AlignItems::Center;
  289. case CSS::ValueID::Baseline:
  290. return CSS::AlignItems::Baseline;
  291. case CSS::ValueID::Stretch:
  292. return CSS::AlignItems::Stretch;
  293. default:
  294. return {};
  295. }
  296. }
  297. Optional<CSS::Position> StyleProperties::position() const
  298. {
  299. auto value = property(CSS::PropertyID::Position);
  300. if (!value.has_value())
  301. return {};
  302. switch (value.value()->to_identifier()) {
  303. case CSS::ValueID::Static:
  304. return CSS::Position::Static;
  305. case CSS::ValueID::Relative:
  306. return CSS::Position::Relative;
  307. case CSS::ValueID::Absolute:
  308. return CSS::Position::Absolute;
  309. case CSS::ValueID::Fixed:
  310. return CSS::Position::Fixed;
  311. case CSS::ValueID::Sticky:
  312. return CSS::Position::Sticky;
  313. default:
  314. return {};
  315. }
  316. }
  317. bool StyleProperties::operator==(const StyleProperties& other) const
  318. {
  319. if (m_property_values.size() != other.m_property_values.size())
  320. return false;
  321. for (auto& it : m_property_values) {
  322. auto jt = other.m_property_values.find(it.key);
  323. if (jt == other.m_property_values.end())
  324. return false;
  325. auto& my_value = *it.value;
  326. auto& other_value = *jt->value;
  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. if (!value.has_value())
  338. return {};
  339. switch (value.value()->to_identifier()) {
  340. case CSS::ValueID::Left:
  341. return CSS::TextAlign::Left;
  342. case CSS::ValueID::Center:
  343. return CSS::TextAlign::Center;
  344. case CSS::ValueID::Right:
  345. return CSS::TextAlign::Right;
  346. case CSS::ValueID::Justify:
  347. return CSS::TextAlign::Justify;
  348. case CSS::ValueID::LibwebCenter:
  349. return CSS::TextAlign::LibwebCenter;
  350. default:
  351. return {};
  352. }
  353. }
  354. Optional<CSS::PointerEvents> StyleProperties::pointer_events() const
  355. {
  356. auto value = property(CSS::PropertyID::PointerEvents);
  357. if (!value.has_value())
  358. return {};
  359. switch (value.value()->to_identifier()) {
  360. case CSS::ValueID::Auto:
  361. return CSS::PointerEvents::Auto;
  362. case CSS::ValueID::All:
  363. return CSS::PointerEvents::All;
  364. case CSS::ValueID::None:
  365. return CSS::PointerEvents::None;
  366. default:
  367. return {};
  368. }
  369. }
  370. Optional<CSS::WhiteSpace> StyleProperties::white_space() const
  371. {
  372. auto value = property(CSS::PropertyID::WhiteSpace);
  373. if (!value.has_value())
  374. return {};
  375. switch (value.value()->to_identifier()) {
  376. case CSS::ValueID::Normal:
  377. return CSS::WhiteSpace::Normal;
  378. case CSS::ValueID::Nowrap:
  379. return CSS::WhiteSpace::Nowrap;
  380. case CSS::ValueID::Pre:
  381. return CSS::WhiteSpace::Pre;
  382. case CSS::ValueID::PreLine:
  383. return CSS::WhiteSpace::PreLine;
  384. case CSS::ValueID::PreWrap:
  385. return CSS::WhiteSpace::PreWrap;
  386. default:
  387. return {};
  388. }
  389. }
  390. Optional<CSS::LineStyle> StyleProperties::line_style(CSS::PropertyID property_id) const
  391. {
  392. auto value = property(property_id);
  393. if (!value.has_value())
  394. return {};
  395. switch (value.value()->to_identifier()) {
  396. case CSS::ValueID::None:
  397. return CSS::LineStyle::None;
  398. case CSS::ValueID::Hidden:
  399. return CSS::LineStyle::Hidden;
  400. case CSS::ValueID::Dotted:
  401. return CSS::LineStyle::Dotted;
  402. case CSS::ValueID::Dashed:
  403. return CSS::LineStyle::Dashed;
  404. case CSS::ValueID::Solid:
  405. return CSS::LineStyle::Solid;
  406. case CSS::ValueID::Double:
  407. return CSS::LineStyle::Double;
  408. case CSS::ValueID::Groove:
  409. return CSS::LineStyle::Groove;
  410. case CSS::ValueID::Ridge:
  411. return CSS::LineStyle::Ridge;
  412. case CSS::ValueID::Inset:
  413. return CSS::LineStyle::Inset;
  414. case CSS::ValueID::Outset:
  415. return CSS::LineStyle::Outset;
  416. default:
  417. return {};
  418. }
  419. }
  420. Optional<CSS::Float> StyleProperties::float_() const
  421. {
  422. auto value = property(CSS::PropertyID::Float);
  423. if (!value.has_value())
  424. return {};
  425. switch (value.value()->to_identifier()) {
  426. case CSS::ValueID::None:
  427. return CSS::Float::None;
  428. case CSS::ValueID::Left:
  429. return CSS::Float::Left;
  430. case CSS::ValueID::Right:
  431. return CSS::Float::Right;
  432. default:
  433. return {};
  434. }
  435. }
  436. Optional<CSS::Clear> StyleProperties::clear() const
  437. {
  438. auto value = property(CSS::PropertyID::Clear);
  439. if (!value.has_value())
  440. return {};
  441. switch (value.value()->to_identifier()) {
  442. case CSS::ValueID::None:
  443. return CSS::Clear::None;
  444. case CSS::ValueID::Left:
  445. return CSS::Clear::Left;
  446. case CSS::ValueID::Right:
  447. return CSS::Clear::Right;
  448. case CSS::ValueID::Both:
  449. return CSS::Clear::Both;
  450. default:
  451. return {};
  452. }
  453. }
  454. Optional<CSS::Cursor> StyleProperties::cursor() const
  455. {
  456. auto value = property(CSS::PropertyID::Cursor);
  457. if (!value.has_value())
  458. return {};
  459. switch (value.value()->to_identifier()) {
  460. case CSS::ValueID::Auto:
  461. return CSS::Cursor::Auto;
  462. case CSS::ValueID::Default:
  463. return CSS::Cursor::Default;
  464. case CSS::ValueID::None:
  465. return CSS::Cursor::None;
  466. case CSS::ValueID::ContextMenu:
  467. return CSS::Cursor::ContextMenu;
  468. case CSS::ValueID::Help:
  469. return CSS::Cursor::Help;
  470. case CSS::ValueID::Pointer:
  471. return CSS::Cursor::Pointer;
  472. case CSS::ValueID::Progress:
  473. return CSS::Cursor::Progress;
  474. case CSS::ValueID::Wait:
  475. return CSS::Cursor::Wait;
  476. case CSS::ValueID::Cell:
  477. return CSS::Cursor::Cell;
  478. case CSS::ValueID::Crosshair:
  479. return CSS::Cursor::Crosshair;
  480. case CSS::ValueID::Text:
  481. return CSS::Cursor::Text;
  482. case CSS::ValueID::VerticalText:
  483. return CSS::Cursor::VerticalText;
  484. case CSS::ValueID::Alias:
  485. return CSS::Cursor::Alias;
  486. case CSS::ValueID::Copy:
  487. return CSS::Cursor::Copy;
  488. case CSS::ValueID::Move:
  489. return CSS::Cursor::Move;
  490. case CSS::ValueID::NoDrop:
  491. return CSS::Cursor::NoDrop;
  492. case CSS::ValueID::NotAllowed:
  493. return CSS::Cursor::NotAllowed;
  494. case CSS::ValueID::Grab:
  495. return CSS::Cursor::Grab;
  496. case CSS::ValueID::Grabbing:
  497. return CSS::Cursor::Grabbing;
  498. case CSS::ValueID::EResize:
  499. return CSS::Cursor::EResize;
  500. case CSS::ValueID::NResize:
  501. return CSS::Cursor::NResize;
  502. case CSS::ValueID::NeResize:
  503. return CSS::Cursor::NeResize;
  504. case CSS::ValueID::NwResize:
  505. return CSS::Cursor::NwResize;
  506. case CSS::ValueID::SResize:
  507. return CSS::Cursor::SResize;
  508. case CSS::ValueID::SeResize:
  509. return CSS::Cursor::SeResize;
  510. case CSS::ValueID::SwResize:
  511. return CSS::Cursor::SwResize;
  512. case CSS::ValueID::WResize:
  513. return CSS::Cursor::WResize;
  514. case CSS::ValueID::EwResize:
  515. return CSS::Cursor::EwResize;
  516. case CSS::ValueID::NsResize:
  517. return CSS::Cursor::NsResize;
  518. case CSS::ValueID::NeswResize:
  519. return CSS::Cursor::NeswResize;
  520. case CSS::ValueID::NwseResize:
  521. return CSS::Cursor::NwseResize;
  522. case CSS::ValueID::ColResize:
  523. return CSS::Cursor::ColResize;
  524. case CSS::ValueID::RowResize:
  525. return CSS::Cursor::RowResize;
  526. case CSS::ValueID::AllScroll:
  527. return CSS::Cursor::AllScroll;
  528. case CSS::ValueID::ZoomIn:
  529. return CSS::Cursor::ZoomIn;
  530. case CSS::ValueID::ZoomOut:
  531. return CSS::Cursor::ZoomOut;
  532. default:
  533. return {};
  534. }
  535. }
  536. CSS::Display StyleProperties::display() const
  537. {
  538. auto value = property(CSS::PropertyID::Display);
  539. if (!value.has_value() || !value.value()->is_identifier())
  540. return CSS::Display::from_short(CSS::Display::Short::Inline);
  541. switch (value.value()->to_identifier()) {
  542. case CSS::ValueID::None:
  543. return CSS::Display::from_short(CSS::Display::Short::None);
  544. case CSS::ValueID::Block:
  545. return CSS::Display::from_short(CSS::Display::Short::Block);
  546. case CSS::ValueID::Inline:
  547. return CSS::Display::from_short(CSS::Display::Short::Inline);
  548. case CSS::ValueID::InlineBlock:
  549. return CSS::Display::from_short(CSS::Display::Short::InlineBlock);
  550. case CSS::ValueID::ListItem:
  551. return CSS::Display::from_short(CSS::Display::Short::ListItem);
  552. case CSS::ValueID::Table:
  553. return CSS::Display::from_short(CSS::Display::Short::Table);
  554. case CSS::ValueID::TableRow:
  555. return CSS::Display { CSS::Display::Internal::TableRow };
  556. case CSS::ValueID::TableCell:
  557. return CSS::Display { CSS::Display::Internal::TableCell };
  558. case CSS::ValueID::TableColumn:
  559. return CSS::Display { CSS::Display::Internal::TableColumn };
  560. case CSS::ValueID::TableColumnGroup:
  561. return CSS::Display { CSS::Display::Internal::TableColumnGroup };
  562. case CSS::ValueID::TableCaption:
  563. return CSS::Display { CSS::Display::Internal::TableCaption };
  564. case CSS::ValueID::TableRowGroup:
  565. return CSS::Display { CSS::Display::Internal::TableRowGroup };
  566. case CSS::ValueID::TableHeaderGroup:
  567. return CSS::Display { CSS::Display::Internal::TableHeaderGroup };
  568. case CSS::ValueID::TableFooterGroup:
  569. return CSS::Display { CSS::Display::Internal::TableFooterGroup };
  570. case CSS::ValueID::Flex:
  571. return CSS::Display::from_short(CSS::Display::Short::Flex);
  572. case CSS::ValueID::InlineFlex:
  573. return CSS::Display::from_short(CSS::Display::Short::InlineFlex);
  574. default:
  575. return CSS::Display::from_short(CSS::Display::Short::Block);
  576. }
  577. }
  578. Optional<CSS::TextDecorationLine> StyleProperties::text_decoration_line() const
  579. {
  580. auto value = property(CSS::PropertyID::TextDecorationLine);
  581. if (!value.has_value())
  582. return {};
  583. switch (value.value()->to_identifier()) {
  584. case CSS::ValueID::None:
  585. return CSS::TextDecorationLine::None;
  586. case CSS::ValueID::Underline:
  587. return CSS::TextDecorationLine::Underline;
  588. case CSS::ValueID::Overline:
  589. return CSS::TextDecorationLine::Overline;
  590. case CSS::ValueID::LineThrough:
  591. return CSS::TextDecorationLine::LineThrough;
  592. case CSS::ValueID::Blink:
  593. return CSS::TextDecorationLine::Blink;
  594. default:
  595. return {};
  596. }
  597. }
  598. Optional<CSS::TextDecorationStyle> StyleProperties::text_decoration_style() const
  599. {
  600. auto value = property(CSS::PropertyID::TextDecorationStyle);
  601. if (!value.has_value())
  602. return {};
  603. switch (value.value()->to_identifier()) {
  604. case CSS::ValueID::Solid:
  605. return CSS::TextDecorationStyle::Solid;
  606. case CSS::ValueID::Double:
  607. return CSS::TextDecorationStyle::Double;
  608. case CSS::ValueID::Dotted:
  609. return CSS::TextDecorationStyle::Dotted;
  610. case CSS::ValueID::Dashed:
  611. return CSS::TextDecorationStyle::Dashed;
  612. case CSS::ValueID::Wavy:
  613. return CSS::TextDecorationStyle::Wavy;
  614. default:
  615. return {};
  616. }
  617. }
  618. Optional<CSS::TextTransform> StyleProperties::text_transform() const
  619. {
  620. auto value = property(CSS::PropertyID::TextTransform);
  621. if (!value.has_value())
  622. return {};
  623. switch (value.value()->to_identifier()) {
  624. case CSS::ValueID::None:
  625. return CSS::TextTransform::None;
  626. case CSS::ValueID::Lowercase:
  627. return CSS::TextTransform::Lowercase;
  628. case CSS::ValueID::Uppercase:
  629. return CSS::TextTransform::Uppercase;
  630. case CSS::ValueID::Capitalize:
  631. return CSS::TextTransform::Capitalize;
  632. case CSS::ValueID::FullWidth:
  633. return CSS::TextTransform::FullWidth;
  634. case CSS::ValueID::FullSizeKana:
  635. return CSS::TextTransform::FullSizeKana;
  636. default:
  637. return {};
  638. }
  639. }
  640. Optional<CSS::ListStyleType> StyleProperties::list_style_type() const
  641. {
  642. auto value = property(CSS::PropertyID::ListStyleType);
  643. if (!value.has_value())
  644. return {};
  645. switch (value.value()->to_identifier()) {
  646. case CSS::ValueID::None:
  647. return CSS::ListStyleType::None;
  648. case CSS::ValueID::Disc:
  649. return CSS::ListStyleType::Disc;
  650. case CSS::ValueID::Circle:
  651. return CSS::ListStyleType::Circle;
  652. case CSS::ValueID::Square:
  653. return CSS::ListStyleType::Square;
  654. case CSS::ValueID::Decimal:
  655. return CSS::ListStyleType::Decimal;
  656. case CSS::ValueID::DecimalLeadingZero:
  657. return CSS::ListStyleType::DecimalLeadingZero;
  658. case CSS::ValueID::LowerAlpha:
  659. return CSS::ListStyleType::LowerAlpha;
  660. case CSS::ValueID::LowerLatin:
  661. return CSS::ListStyleType::LowerLatin;
  662. case CSS::ValueID::UpperAlpha:
  663. return CSS::ListStyleType::UpperAlpha;
  664. case CSS::ValueID::UpperLatin:
  665. return CSS::ListStyleType::UpperLatin;
  666. case CSS::ValueID::UpperRoman:
  667. return CSS::ListStyleType::UpperRoman;
  668. case CSS::ValueID::LowerRoman:
  669. return CSS::ListStyleType::LowerRoman;
  670. default:
  671. return {};
  672. }
  673. }
  674. Optional<CSS::Overflow> StyleProperties::overflow_x() const
  675. {
  676. return overflow(CSS::PropertyID::OverflowX);
  677. }
  678. Optional<CSS::Overflow> StyleProperties::overflow_y() const
  679. {
  680. return overflow(CSS::PropertyID::OverflowY);
  681. }
  682. Optional<CSS::Overflow> StyleProperties::overflow(CSS::PropertyID property_id) const
  683. {
  684. auto value = property(property_id);
  685. if (!value.has_value())
  686. return {};
  687. switch (value.value()->to_identifier()) {
  688. case CSS::ValueID::Auto:
  689. return CSS::Overflow::Auto;
  690. case CSS::ValueID::Visible:
  691. return CSS::Overflow::Visible;
  692. case CSS::ValueID::Hidden:
  693. return CSS::Overflow::Hidden;
  694. case CSS::ValueID::Clip:
  695. return CSS::Overflow::Clip;
  696. case CSS::ValueID::Scroll:
  697. return CSS::Overflow::Scroll;
  698. default:
  699. return {};
  700. }
  701. }
  702. Vector<BoxShadowData> StyleProperties::box_shadow() const
  703. {
  704. auto value_or_error = property(PropertyID::BoxShadow);
  705. if (!value_or_error.has_value())
  706. return {};
  707. auto value = value_or_error.value();
  708. auto make_box_shadow_data = [](BoxShadowStyleValue const& box) {
  709. return BoxShadowData { box.color(), box.offset_x(), box.offset_y(), box.blur_radius(), box.spread_distance(), box.placement() };
  710. };
  711. if (value->is_value_list()) {
  712. auto& value_list = value->as_value_list();
  713. Vector<BoxShadowData> box_shadow_data;
  714. box_shadow_data.ensure_capacity(value_list.size());
  715. for (auto const& layer_value : value_list.values())
  716. box_shadow_data.append(make_box_shadow_data(layer_value.as_box_shadow()));
  717. return box_shadow_data;
  718. }
  719. if (value->is_box_shadow()) {
  720. auto& box = value->as_box_shadow();
  721. return { make_box_shadow_data(box) };
  722. }
  723. return {};
  724. }
  725. CSS::BoxSizing StyleProperties::box_sizing() const
  726. {
  727. auto value = property(CSS::PropertyID::BoxSizing);
  728. if (!value.has_value())
  729. return {};
  730. switch (value.value()->to_identifier()) {
  731. case CSS::ValueID::BorderBox:
  732. return CSS::BoxSizing::BorderBox;
  733. case CSS::ValueID::ContentBox:
  734. return CSS::BoxSizing::ContentBox;
  735. default:
  736. return {};
  737. }
  738. }
  739. }