StyleProperties.cpp 26 KB

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