StyleProperties.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  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. CSS::ContentData StyleProperties::content() const
  475. {
  476. auto maybe_value = property(CSS::PropertyID::Content);
  477. if (!maybe_value.has_value())
  478. return CSS::ContentData {};
  479. auto& value = maybe_value.value();
  480. if (value->is_content()) {
  481. auto& content_style_value = value->as_content();
  482. CSS::ContentData content_data;
  483. // FIXME: The content is a list of things: strings, identifiers or functions that return strings, and images.
  484. // So it can't always be represented as a single String, but may have to be multiple boxes.
  485. // For now, we'll just assume strings since that is easiest.
  486. StringBuilder builder;
  487. for (auto const& item : content_style_value.content().values()) {
  488. if (item.is_string()) {
  489. builder.append(item.to_string());
  490. } else {
  491. // TODO: Implement quotes, counters, images, and other things.
  492. }
  493. }
  494. content_data.type = ContentData::Type::String;
  495. content_data.data = builder.to_string();
  496. if (content_style_value.has_alt_text()) {
  497. StringBuilder alt_text_builder;
  498. for (auto const& item : content_style_value.alt_text()->values()) {
  499. if (item.is_string()) {
  500. alt_text_builder.append(item.to_string());
  501. } else {
  502. // TODO: Implement counters
  503. }
  504. }
  505. content_data.alt_text = alt_text_builder.to_string();
  506. }
  507. return content_data;
  508. }
  509. switch (value->to_identifier()) {
  510. case ValueID::None:
  511. return { ContentData::Type::None };
  512. case ValueID::Normal:
  513. return { ContentData::Type::Normal };
  514. default:
  515. break;
  516. }
  517. return CSS::ContentData {};
  518. }
  519. Optional<CSS::Cursor> StyleProperties::cursor() const
  520. {
  521. auto value = property(CSS::PropertyID::Cursor);
  522. if (!value.has_value())
  523. return {};
  524. switch (value.value()->to_identifier()) {
  525. case CSS::ValueID::Auto:
  526. return CSS::Cursor::Auto;
  527. case CSS::ValueID::Default:
  528. return CSS::Cursor::Default;
  529. case CSS::ValueID::None:
  530. return CSS::Cursor::None;
  531. case CSS::ValueID::ContextMenu:
  532. return CSS::Cursor::ContextMenu;
  533. case CSS::ValueID::Help:
  534. return CSS::Cursor::Help;
  535. case CSS::ValueID::Pointer:
  536. return CSS::Cursor::Pointer;
  537. case CSS::ValueID::Progress:
  538. return CSS::Cursor::Progress;
  539. case CSS::ValueID::Wait:
  540. return CSS::Cursor::Wait;
  541. case CSS::ValueID::Cell:
  542. return CSS::Cursor::Cell;
  543. case CSS::ValueID::Crosshair:
  544. return CSS::Cursor::Crosshair;
  545. case CSS::ValueID::Text:
  546. return CSS::Cursor::Text;
  547. case CSS::ValueID::VerticalText:
  548. return CSS::Cursor::VerticalText;
  549. case CSS::ValueID::Alias:
  550. return CSS::Cursor::Alias;
  551. case CSS::ValueID::Copy:
  552. return CSS::Cursor::Copy;
  553. case CSS::ValueID::Move:
  554. return CSS::Cursor::Move;
  555. case CSS::ValueID::NoDrop:
  556. return CSS::Cursor::NoDrop;
  557. case CSS::ValueID::NotAllowed:
  558. return CSS::Cursor::NotAllowed;
  559. case CSS::ValueID::Grab:
  560. return CSS::Cursor::Grab;
  561. case CSS::ValueID::Grabbing:
  562. return CSS::Cursor::Grabbing;
  563. case CSS::ValueID::EResize:
  564. return CSS::Cursor::EResize;
  565. case CSS::ValueID::NResize:
  566. return CSS::Cursor::NResize;
  567. case CSS::ValueID::NeResize:
  568. return CSS::Cursor::NeResize;
  569. case CSS::ValueID::NwResize:
  570. return CSS::Cursor::NwResize;
  571. case CSS::ValueID::SResize:
  572. return CSS::Cursor::SResize;
  573. case CSS::ValueID::SeResize:
  574. return CSS::Cursor::SeResize;
  575. case CSS::ValueID::SwResize:
  576. return CSS::Cursor::SwResize;
  577. case CSS::ValueID::WResize:
  578. return CSS::Cursor::WResize;
  579. case CSS::ValueID::EwResize:
  580. return CSS::Cursor::EwResize;
  581. case CSS::ValueID::NsResize:
  582. return CSS::Cursor::NsResize;
  583. case CSS::ValueID::NeswResize:
  584. return CSS::Cursor::NeswResize;
  585. case CSS::ValueID::NwseResize:
  586. return CSS::Cursor::NwseResize;
  587. case CSS::ValueID::ColResize:
  588. return CSS::Cursor::ColResize;
  589. case CSS::ValueID::RowResize:
  590. return CSS::Cursor::RowResize;
  591. case CSS::ValueID::AllScroll:
  592. return CSS::Cursor::AllScroll;
  593. case CSS::ValueID::ZoomIn:
  594. return CSS::Cursor::ZoomIn;
  595. case CSS::ValueID::ZoomOut:
  596. return CSS::Cursor::ZoomOut;
  597. default:
  598. return {};
  599. }
  600. }
  601. CSS::Display StyleProperties::display() const
  602. {
  603. auto value = property(CSS::PropertyID::Display);
  604. if (!value.has_value() || !value.value()->is_identifier())
  605. return CSS::Display::from_short(CSS::Display::Short::Inline);
  606. switch (value.value()->to_identifier()) {
  607. case CSS::ValueID::None:
  608. return CSS::Display::from_short(CSS::Display::Short::None);
  609. case CSS::ValueID::Block:
  610. return CSS::Display::from_short(CSS::Display::Short::Block);
  611. case CSS::ValueID::Inline:
  612. return CSS::Display::from_short(CSS::Display::Short::Inline);
  613. case CSS::ValueID::InlineBlock:
  614. return CSS::Display::from_short(CSS::Display::Short::InlineBlock);
  615. case CSS::ValueID::ListItem:
  616. return CSS::Display::from_short(CSS::Display::Short::ListItem);
  617. case CSS::ValueID::Table:
  618. return CSS::Display::from_short(CSS::Display::Short::Table);
  619. case CSS::ValueID::TableRow:
  620. return CSS::Display { CSS::Display::Internal::TableRow };
  621. case CSS::ValueID::TableCell:
  622. return CSS::Display { CSS::Display::Internal::TableCell };
  623. case CSS::ValueID::TableColumn:
  624. return CSS::Display { CSS::Display::Internal::TableColumn };
  625. case CSS::ValueID::TableColumnGroup:
  626. return CSS::Display { CSS::Display::Internal::TableColumnGroup };
  627. case CSS::ValueID::TableCaption:
  628. return CSS::Display { CSS::Display::Internal::TableCaption };
  629. case CSS::ValueID::TableRowGroup:
  630. return CSS::Display { CSS::Display::Internal::TableRowGroup };
  631. case CSS::ValueID::TableHeaderGroup:
  632. return CSS::Display { CSS::Display::Internal::TableHeaderGroup };
  633. case CSS::ValueID::TableFooterGroup:
  634. return CSS::Display { CSS::Display::Internal::TableFooterGroup };
  635. case CSS::ValueID::Flex:
  636. return CSS::Display::from_short(CSS::Display::Short::Flex);
  637. case CSS::ValueID::InlineFlex:
  638. return CSS::Display::from_short(CSS::Display::Short::InlineFlex);
  639. default:
  640. return CSS::Display::from_short(CSS::Display::Short::Block);
  641. }
  642. }
  643. Optional<CSS::TextDecorationLine> StyleProperties::text_decoration_line() const
  644. {
  645. auto value = property(CSS::PropertyID::TextDecorationLine);
  646. if (!value.has_value())
  647. return {};
  648. switch (value.value()->to_identifier()) {
  649. case CSS::ValueID::None:
  650. return CSS::TextDecorationLine::None;
  651. case CSS::ValueID::Underline:
  652. return CSS::TextDecorationLine::Underline;
  653. case CSS::ValueID::Overline:
  654. return CSS::TextDecorationLine::Overline;
  655. case CSS::ValueID::LineThrough:
  656. return CSS::TextDecorationLine::LineThrough;
  657. case CSS::ValueID::Blink:
  658. return CSS::TextDecorationLine::Blink;
  659. default:
  660. return {};
  661. }
  662. }
  663. Optional<CSS::TextDecorationStyle> StyleProperties::text_decoration_style() const
  664. {
  665. auto value = property(CSS::PropertyID::TextDecorationStyle);
  666. if (!value.has_value())
  667. return {};
  668. switch (value.value()->to_identifier()) {
  669. case CSS::ValueID::Solid:
  670. return CSS::TextDecorationStyle::Solid;
  671. case CSS::ValueID::Double:
  672. return CSS::TextDecorationStyle::Double;
  673. case CSS::ValueID::Dotted:
  674. return CSS::TextDecorationStyle::Dotted;
  675. case CSS::ValueID::Dashed:
  676. return CSS::TextDecorationStyle::Dashed;
  677. case CSS::ValueID::Wavy:
  678. return CSS::TextDecorationStyle::Wavy;
  679. default:
  680. return {};
  681. }
  682. }
  683. Optional<CSS::TextTransform> StyleProperties::text_transform() const
  684. {
  685. auto value = property(CSS::PropertyID::TextTransform);
  686. if (!value.has_value())
  687. return {};
  688. switch (value.value()->to_identifier()) {
  689. case CSS::ValueID::None:
  690. return CSS::TextTransform::None;
  691. case CSS::ValueID::Lowercase:
  692. return CSS::TextTransform::Lowercase;
  693. case CSS::ValueID::Uppercase:
  694. return CSS::TextTransform::Uppercase;
  695. case CSS::ValueID::Capitalize:
  696. return CSS::TextTransform::Capitalize;
  697. case CSS::ValueID::FullWidth:
  698. return CSS::TextTransform::FullWidth;
  699. case CSS::ValueID::FullSizeKana:
  700. return CSS::TextTransform::FullSizeKana;
  701. default:
  702. return {};
  703. }
  704. }
  705. Optional<CSS::ListStyleType> StyleProperties::list_style_type() const
  706. {
  707. auto value = property(CSS::PropertyID::ListStyleType);
  708. if (!value.has_value())
  709. return {};
  710. switch (value.value()->to_identifier()) {
  711. case CSS::ValueID::None:
  712. return CSS::ListStyleType::None;
  713. case CSS::ValueID::Disc:
  714. return CSS::ListStyleType::Disc;
  715. case CSS::ValueID::Circle:
  716. return CSS::ListStyleType::Circle;
  717. case CSS::ValueID::Square:
  718. return CSS::ListStyleType::Square;
  719. case CSS::ValueID::Decimal:
  720. return CSS::ListStyleType::Decimal;
  721. case CSS::ValueID::DecimalLeadingZero:
  722. return CSS::ListStyleType::DecimalLeadingZero;
  723. case CSS::ValueID::LowerAlpha:
  724. return CSS::ListStyleType::LowerAlpha;
  725. case CSS::ValueID::LowerLatin:
  726. return CSS::ListStyleType::LowerLatin;
  727. case CSS::ValueID::UpperAlpha:
  728. return CSS::ListStyleType::UpperAlpha;
  729. case CSS::ValueID::UpperLatin:
  730. return CSS::ListStyleType::UpperLatin;
  731. case CSS::ValueID::UpperRoman:
  732. return CSS::ListStyleType::UpperRoman;
  733. case CSS::ValueID::LowerRoman:
  734. return CSS::ListStyleType::LowerRoman;
  735. default:
  736. return {};
  737. }
  738. }
  739. Optional<CSS::Overflow> StyleProperties::overflow_x() const
  740. {
  741. return overflow(CSS::PropertyID::OverflowX);
  742. }
  743. Optional<CSS::Overflow> StyleProperties::overflow_y() const
  744. {
  745. return overflow(CSS::PropertyID::OverflowY);
  746. }
  747. Optional<CSS::Overflow> StyleProperties::overflow(CSS::PropertyID property_id) const
  748. {
  749. auto value = property(property_id);
  750. if (!value.has_value())
  751. return {};
  752. switch (value.value()->to_identifier()) {
  753. case CSS::ValueID::Auto:
  754. return CSS::Overflow::Auto;
  755. case CSS::ValueID::Visible:
  756. return CSS::Overflow::Visible;
  757. case CSS::ValueID::Hidden:
  758. return CSS::Overflow::Hidden;
  759. case CSS::ValueID::Clip:
  760. return CSS::Overflow::Clip;
  761. case CSS::ValueID::Scroll:
  762. return CSS::Overflow::Scroll;
  763. default:
  764. return {};
  765. }
  766. }
  767. Vector<BoxShadowData> StyleProperties::box_shadow() const
  768. {
  769. auto value_or_error = property(PropertyID::BoxShadow);
  770. if (!value_or_error.has_value())
  771. return {};
  772. auto value = value_or_error.value();
  773. auto make_box_shadow_data = [](BoxShadowStyleValue const& box) {
  774. return BoxShadowData { box.color(), box.offset_x(), box.offset_y(), box.blur_radius(), box.spread_distance(), box.placement() };
  775. };
  776. if (value->is_value_list()) {
  777. auto& value_list = value->as_value_list();
  778. Vector<BoxShadowData> box_shadow_data;
  779. box_shadow_data.ensure_capacity(value_list.size());
  780. for (auto const& layer_value : value_list.values())
  781. box_shadow_data.append(make_box_shadow_data(layer_value.as_box_shadow()));
  782. return box_shadow_data;
  783. }
  784. if (value->is_box_shadow()) {
  785. auto& box = value->as_box_shadow();
  786. return { make_box_shadow_data(box) };
  787. }
  788. return {};
  789. }
  790. CSS::BoxSizing StyleProperties::box_sizing() const
  791. {
  792. auto value = property(CSS::PropertyID::BoxSizing);
  793. if (!value.has_value())
  794. return {};
  795. switch (value.value()->to_identifier()) {
  796. case CSS::ValueID::BorderBox:
  797. return CSS::BoxSizing::BorderBox;
  798. case CSS::ValueID::ContentBox:
  799. return CSS::BoxSizing::ContentBox;
  800. default:
  801. return {};
  802. }
  803. }
  804. }