StyleProperties.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  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(const StyleProperties& other)
  16. : m_property_values(other.m_property_values)
  17. {
  18. if (other.m_font) {
  19. m_font = other.m_font->clone();
  20. } else {
  21. m_font = nullptr;
  22. }
  23. }
  24. NonnullRefPtr<StyleProperties> StyleProperties::clone() const
  25. {
  26. return adopt_ref(*new StyleProperties(*this));
  27. }
  28. void StyleProperties::set_property(CSS::PropertyID id, NonnullRefPtr<StyleValue> value)
  29. {
  30. m_property_values[to_underlying(id)] = move(value);
  31. }
  32. Optional<NonnullRefPtr<StyleValue>> StyleProperties::property(CSS::PropertyID property_id) const
  33. {
  34. auto value = m_property_values[to_underlying(property_id)];
  35. if (!value)
  36. return {};
  37. return value.release_nonnull();
  38. }
  39. Length StyleProperties::length_or_fallback(CSS::PropertyID id, Length const& fallback) const
  40. {
  41. auto maybe_value = property(id);
  42. if (!maybe_value.has_value())
  43. return fallback;
  44. auto& value = maybe_value.value();
  45. if (value->is_calculated())
  46. return Length::make_calculated(value->as_calculated());
  47. if (value->has_length())
  48. return value->to_length();
  49. return fallback;
  50. }
  51. LengthPercentage StyleProperties::length_percentage_or_fallback(CSS::PropertyID id, LengthPercentage const& fallback) const
  52. {
  53. return length_percentage(id).value_or(fallback);
  54. }
  55. Optional<LengthPercentage> StyleProperties::length_percentage(CSS::PropertyID id) const
  56. {
  57. auto maybe_value = property(id);
  58. if (!maybe_value.has_value())
  59. return {};
  60. auto& value = maybe_value.value();
  61. if (value->is_calculated())
  62. return LengthPercentage { value->as_calculated() };
  63. if (value->is_percentage())
  64. return value->as_percentage().percentage();
  65. if (value->has_length())
  66. return value->to_length();
  67. return {};
  68. }
  69. LengthBox StyleProperties::length_box(CSS::PropertyID left_id, CSS::PropertyID top_id, CSS::PropertyID right_id, CSS::PropertyID bottom_id, const CSS::Length& default_value) const
  70. {
  71. LengthBox box;
  72. box.left = length_percentage_or_fallback(left_id, default_value);
  73. box.top = length_percentage_or_fallback(top_id, default_value);
  74. box.right = length_percentage_or_fallback(right_id, default_value);
  75. box.bottom = length_percentage_or_fallback(bottom_id, default_value);
  76. return box;
  77. }
  78. Color StyleProperties::color_or_fallback(CSS::PropertyID id, Layout::NodeWithStyle const& node, Color fallback) const
  79. {
  80. auto value = property(id);
  81. if (!value.has_value() || !value.value()->has_color())
  82. return fallback;
  83. return value.value()->to_color(node);
  84. }
  85. NonnullRefPtr<Gfx::Font> StyleProperties::font_fallback(bool monospace, bool bold)
  86. {
  87. if (monospace && bold)
  88. return Gfx::FontDatabase::default_fixed_width_font().bold_variant();
  89. if (monospace)
  90. return Gfx::FontDatabase::default_fixed_width_font();
  91. if (bold)
  92. return Gfx::FontDatabase::default_font().bold_variant();
  93. return Gfx::FontDatabase::default_font();
  94. }
  95. float StyleProperties::line_height(Layout::Node const& layout_node) const
  96. {
  97. if (auto maybe_line_height = property(CSS::PropertyID::LineHeight); maybe_line_height.has_value()) {
  98. auto line_height = maybe_line_height.release_value();
  99. if (line_height->is_identifier() && line_height->to_identifier() == ValueID::Normal)
  100. return layout_node.font().pixel_metrics().line_spacing();
  101. if (line_height->is_length()) {
  102. auto line_height_length = line_height->to_length();
  103. if (!line_height_length.is_auto())
  104. return line_height_length.to_px(layout_node);
  105. }
  106. if (line_height->is_numeric())
  107. return Length(line_height->to_number(), Length::Type::Em).to_px(layout_node);
  108. if (line_height->is_percentage()) {
  109. // Percentages are relative to 1em. https://www.w3.org/TR/css-inline-3/#valdef-line-height-percentage
  110. auto& percentage = line_height->as_percentage().percentage();
  111. return Length(percentage.as_fraction(), Length::Type::Em).to_px(layout_node);
  112. }
  113. }
  114. return layout_node.font().pixel_metrics().line_spacing();
  115. }
  116. Optional<int> StyleProperties::z_index() const
  117. {
  118. auto maybe_value = property(CSS::PropertyID::ZIndex);
  119. if (!maybe_value.has_value())
  120. return {};
  121. auto& value = maybe_value.value();
  122. if (value->has_auto())
  123. return {};
  124. if (value->has_integer())
  125. return value->to_integer();
  126. return {};
  127. }
  128. float StyleProperties::opacity() const
  129. {
  130. auto maybe_value = property(CSS::PropertyID::Opacity);
  131. if (!maybe_value.has_value())
  132. return 1.0f;
  133. auto& value = maybe_value.value();
  134. float unclamped_opacity = 1.0f;
  135. if (value->has_number()) {
  136. unclamped_opacity = value->to_number();
  137. } else if (value->is_calculated()) {
  138. auto& calculated = value->as_calculated();
  139. if (calculated.resolved_type() == CalculatedStyleValue::ResolvedType::Percentage) {
  140. auto maybe_percentage = value->as_calculated().resolve_percentage();
  141. if (maybe_percentage.has_value())
  142. unclamped_opacity = maybe_percentage->as_fraction();
  143. else
  144. dbgln("Unable to resolve calc() as opacity (percentage): {}", value->to_string());
  145. } else {
  146. auto maybe_number = value->as_calculated().resolve_number();
  147. if (maybe_number.has_value())
  148. unclamped_opacity = maybe_number.value();
  149. else
  150. dbgln("Unable to resolve calc() as opacity (number): {}", value->to_string());
  151. }
  152. } else if (value->is_percentage()) {
  153. unclamped_opacity = value->as_percentage().percentage().as_fraction();
  154. }
  155. return clamp(unclamped_opacity, 0.0f, 1.0f);
  156. }
  157. Optional<CSS::FlexDirection> StyleProperties::flex_direction() const
  158. {
  159. auto value = property(CSS::PropertyID::FlexDirection);
  160. if (!value.has_value())
  161. return {};
  162. 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::ImageRendering> StyleProperties::image_rendering() const
  222. {
  223. auto value = property(CSS::PropertyID::ImageRendering);
  224. if (!value.has_value())
  225. return {};
  226. switch (value.value()->to_identifier()) {
  227. case CSS::ValueID::Auto:
  228. return CSS::ImageRendering::Auto;
  229. case CSS::ValueID::CrispEdges:
  230. return CSS::ImageRendering::CrispEdges;
  231. case CSS::ValueID::HighQuality:
  232. return CSS::ImageRendering::HighQuality;
  233. case CSS::ValueID::Pixelated:
  234. return CSS::ImageRendering::Pixelated;
  235. case CSS::ValueID::Smooth:
  236. return CSS::ImageRendering::Smooth;
  237. default:
  238. return {};
  239. }
  240. }
  241. Optional<CSS::JustifyContent> StyleProperties::justify_content() const
  242. {
  243. auto value = property(CSS::PropertyID::JustifyContent);
  244. if (!value.has_value())
  245. return {};
  246. switch (value.value()->to_identifier()) {
  247. case CSS::ValueID::FlexStart:
  248. return CSS::JustifyContent::FlexStart;
  249. case CSS::ValueID::FlexEnd:
  250. return CSS::JustifyContent::FlexEnd;
  251. case CSS::ValueID::Center:
  252. return CSS::JustifyContent::Center;
  253. case CSS::ValueID::SpaceBetween:
  254. return CSS::JustifyContent::SpaceBetween;
  255. case CSS::ValueID::SpaceAround:
  256. return CSS::JustifyContent::SpaceAround;
  257. default:
  258. return {};
  259. }
  260. }
  261. Vector<CSS::Transformation> StyleProperties::transformations() const
  262. {
  263. auto value = property(CSS::PropertyID::Transform);
  264. if (!value.has_value())
  265. return {};
  266. if (value.value()->is_identifier() && value.value()->to_identifier() == CSS::ValueID::None)
  267. return {};
  268. if (!value.value()->is_value_list())
  269. return {};
  270. auto& list = value.value()->as_value_list();
  271. Vector<CSS::Transformation> transformations;
  272. for (auto& it : list.values()) {
  273. if (!it.is_transformation())
  274. return {};
  275. auto& transformation_style_value = it.as_transformation();
  276. CSS::Transformation transformation;
  277. transformation.function = transformation_style_value.transform_function();
  278. Vector<Variant<CSS::LengthPercentage, float>> values;
  279. for (auto& transformation_value : transformation_style_value.values()) {
  280. if (transformation_value.is_length()) {
  281. values.append({ transformation_value.to_length() });
  282. } else if (transformation_value.is_percentage()) {
  283. values.append({ transformation_value.as_percentage().percentage() });
  284. } else if (transformation_value.is_numeric()) {
  285. values.append({ transformation_value.to_number() });
  286. } else if (transformation_value.is_angle()) {
  287. values.append({ transformation_value.as_angle().angle().to_degrees() });
  288. } else {
  289. dbgln("FIXME: Unsupported value in transform!");
  290. }
  291. }
  292. transformation.values = move(values);
  293. transformations.append(move(transformation));
  294. }
  295. return transformations;
  296. }
  297. static Optional<LengthPercentage> length_percentage_for_style_value(StyleValue const& value)
  298. {
  299. if (value.is_length())
  300. return value.to_length();
  301. if (value.is_percentage())
  302. return value.as_percentage().percentage();
  303. return {};
  304. }
  305. CSS::TransformOrigin StyleProperties::transform_origin() const
  306. {
  307. auto value = property(CSS::PropertyID::TransformOrigin);
  308. if (!value.has_value() || !value.value()->is_value_list() || value.value()->as_value_list().size() != 2)
  309. return {};
  310. auto const& list = value.value()->as_value_list();
  311. auto x_value = length_percentage_for_style_value(list.values()[0]);
  312. auto y_value = length_percentage_for_style_value(list.values()[1]);
  313. if (!x_value.has_value() || !y_value.has_value()) {
  314. return {};
  315. }
  316. return { x_value.value(), y_value.value() };
  317. }
  318. Optional<CSS::AlignItems> StyleProperties::align_items() const
  319. {
  320. auto value = property(CSS::PropertyID::AlignItems);
  321. if (!value.has_value())
  322. return {};
  323. switch (value.value()->to_identifier()) {
  324. case CSS::ValueID::FlexStart:
  325. return CSS::AlignItems::FlexStart;
  326. case CSS::ValueID::FlexEnd:
  327. return CSS::AlignItems::FlexEnd;
  328. case CSS::ValueID::Center:
  329. return CSS::AlignItems::Center;
  330. case CSS::ValueID::Baseline:
  331. return CSS::AlignItems::Baseline;
  332. case CSS::ValueID::Stretch:
  333. return CSS::AlignItems::Stretch;
  334. default:
  335. return {};
  336. }
  337. }
  338. Optional<CSS::Position> StyleProperties::position() const
  339. {
  340. auto value = property(CSS::PropertyID::Position);
  341. if (!value.has_value())
  342. return {};
  343. switch (value.value()->to_identifier()) {
  344. case CSS::ValueID::Static:
  345. return CSS::Position::Static;
  346. case CSS::ValueID::Relative:
  347. return CSS::Position::Relative;
  348. case CSS::ValueID::Absolute:
  349. return CSS::Position::Absolute;
  350. case CSS::ValueID::Fixed:
  351. return CSS::Position::Fixed;
  352. case CSS::ValueID::Sticky:
  353. return CSS::Position::Sticky;
  354. default:
  355. return {};
  356. }
  357. }
  358. bool StyleProperties::operator==(const StyleProperties& other) const
  359. {
  360. if (m_property_values.size() != other.m_property_values.size())
  361. return false;
  362. for (size_t i = 0; i < m_property_values.size(); ++i) {
  363. auto const& my_ptr = m_property_values[i];
  364. auto const& other_ptr = other.m_property_values[i];
  365. if (!my_ptr) {
  366. if (other_ptr)
  367. return false;
  368. continue;
  369. }
  370. if (!other_ptr)
  371. return false;
  372. auto const& my_value = *my_ptr;
  373. auto const& other_value = *other_ptr;
  374. if (my_value.type() != other_value.type())
  375. return false;
  376. if (my_value != other_value)
  377. return false;
  378. }
  379. return true;
  380. }
  381. Optional<CSS::TextAlign> StyleProperties::text_align() const
  382. {
  383. auto value = property(CSS::PropertyID::TextAlign);
  384. if (!value.has_value())
  385. return {};
  386. switch (value.value()->to_identifier()) {
  387. case CSS::ValueID::Left:
  388. return CSS::TextAlign::Left;
  389. case CSS::ValueID::Center:
  390. return CSS::TextAlign::Center;
  391. case CSS::ValueID::Right:
  392. return CSS::TextAlign::Right;
  393. case CSS::ValueID::Justify:
  394. return CSS::TextAlign::Justify;
  395. case CSS::ValueID::LibwebCenter:
  396. return CSS::TextAlign::LibwebCenter;
  397. default:
  398. return {};
  399. }
  400. }
  401. Optional<CSS::TextJustify> StyleProperties::text_justify() const
  402. {
  403. auto value = property(CSS::PropertyID::TextJustify);
  404. if (!value.has_value())
  405. return {};
  406. switch (value.value()->to_identifier()) {
  407. case CSS::ValueID::Auto:
  408. return CSS::TextJustify::Auto;
  409. case CSS::ValueID::None:
  410. return CSS::TextJustify::None;
  411. case CSS::ValueID::InterWord:
  412. return CSS::TextJustify::InterWord;
  413. case CSS::ValueID::Distribute:
  414. case CSS::ValueID::InterCharacter:
  415. return CSS::TextJustify::InterCharacter;
  416. default:
  417. return {};
  418. }
  419. }
  420. Optional<CSS::PointerEvents> StyleProperties::pointer_events() const
  421. {
  422. auto value = property(CSS::PropertyID::PointerEvents);
  423. if (!value.has_value())
  424. return {};
  425. switch (value.value()->to_identifier()) {
  426. case CSS::ValueID::Auto:
  427. return CSS::PointerEvents::Auto;
  428. case CSS::ValueID::All:
  429. return CSS::PointerEvents::All;
  430. case CSS::ValueID::None:
  431. return CSS::PointerEvents::None;
  432. default:
  433. return {};
  434. }
  435. }
  436. Optional<CSS::WhiteSpace> StyleProperties::white_space() const
  437. {
  438. auto value = property(CSS::PropertyID::WhiteSpace);
  439. if (!value.has_value())
  440. return {};
  441. switch (value.value()->to_identifier()) {
  442. case CSS::ValueID::Normal:
  443. return CSS::WhiteSpace::Normal;
  444. case CSS::ValueID::Nowrap:
  445. return CSS::WhiteSpace::Nowrap;
  446. case CSS::ValueID::Pre:
  447. return CSS::WhiteSpace::Pre;
  448. case CSS::ValueID::PreLine:
  449. return CSS::WhiteSpace::PreLine;
  450. case CSS::ValueID::PreWrap:
  451. return CSS::WhiteSpace::PreWrap;
  452. default:
  453. return {};
  454. }
  455. }
  456. Optional<CSS::LineStyle> StyleProperties::line_style(CSS::PropertyID property_id) const
  457. {
  458. auto value = property(property_id);
  459. if (!value.has_value())
  460. return {};
  461. switch (value.value()->to_identifier()) {
  462. case CSS::ValueID::None:
  463. return CSS::LineStyle::None;
  464. case CSS::ValueID::Hidden:
  465. return CSS::LineStyle::Hidden;
  466. case CSS::ValueID::Dotted:
  467. return CSS::LineStyle::Dotted;
  468. case CSS::ValueID::Dashed:
  469. return CSS::LineStyle::Dashed;
  470. case CSS::ValueID::Solid:
  471. return CSS::LineStyle::Solid;
  472. case CSS::ValueID::Double:
  473. return CSS::LineStyle::Double;
  474. case CSS::ValueID::Groove:
  475. return CSS::LineStyle::Groove;
  476. case CSS::ValueID::Ridge:
  477. return CSS::LineStyle::Ridge;
  478. case CSS::ValueID::Inset:
  479. return CSS::LineStyle::Inset;
  480. case CSS::ValueID::Outset:
  481. return CSS::LineStyle::Outset;
  482. default:
  483. return {};
  484. }
  485. }
  486. Optional<CSS::Float> StyleProperties::float_() const
  487. {
  488. auto value = property(CSS::PropertyID::Float);
  489. if (!value.has_value())
  490. return {};
  491. switch (value.value()->to_identifier()) {
  492. case CSS::ValueID::None:
  493. return CSS::Float::None;
  494. case CSS::ValueID::Left:
  495. return CSS::Float::Left;
  496. case CSS::ValueID::Right:
  497. return CSS::Float::Right;
  498. default:
  499. return {};
  500. }
  501. }
  502. Optional<CSS::Clear> StyleProperties::clear() const
  503. {
  504. auto value = property(CSS::PropertyID::Clear);
  505. if (!value.has_value())
  506. return {};
  507. switch (value.value()->to_identifier()) {
  508. case CSS::ValueID::None:
  509. return CSS::Clear::None;
  510. case CSS::ValueID::Left:
  511. return CSS::Clear::Left;
  512. case CSS::ValueID::Right:
  513. return CSS::Clear::Right;
  514. case CSS::ValueID::Both:
  515. return CSS::Clear::Both;
  516. default:
  517. return {};
  518. }
  519. }
  520. CSS::ContentData StyleProperties::content() const
  521. {
  522. auto maybe_value = property(CSS::PropertyID::Content);
  523. if (!maybe_value.has_value())
  524. return CSS::ContentData {};
  525. auto& value = maybe_value.value();
  526. if (value->is_content()) {
  527. auto& content_style_value = value->as_content();
  528. CSS::ContentData content_data;
  529. // FIXME: The content is a list of things: strings, identifiers or functions that return strings, and images.
  530. // So it can't always be represented as a single String, but may have to be multiple boxes.
  531. // For now, we'll just assume strings since that is easiest.
  532. StringBuilder builder;
  533. for (auto const& item : content_style_value.content().values()) {
  534. if (item.is_string()) {
  535. builder.append(item.to_string());
  536. } else {
  537. // TODO: Implement quotes, counters, images, and other things.
  538. }
  539. }
  540. content_data.type = ContentData::Type::String;
  541. content_data.data = builder.to_string();
  542. if (content_style_value.has_alt_text()) {
  543. StringBuilder alt_text_builder;
  544. for (auto const& item : content_style_value.alt_text()->values()) {
  545. if (item.is_string()) {
  546. alt_text_builder.append(item.to_string());
  547. } else {
  548. // TODO: Implement counters
  549. }
  550. }
  551. content_data.alt_text = alt_text_builder.to_string();
  552. }
  553. return content_data;
  554. }
  555. switch (value->to_identifier()) {
  556. case ValueID::None:
  557. return { ContentData::Type::None };
  558. case ValueID::Normal:
  559. return { ContentData::Type::Normal };
  560. default:
  561. break;
  562. }
  563. return CSS::ContentData {};
  564. }
  565. Optional<CSS::Cursor> StyleProperties::cursor() const
  566. {
  567. auto value = property(CSS::PropertyID::Cursor);
  568. if (!value.has_value())
  569. return {};
  570. switch (value.value()->to_identifier()) {
  571. case CSS::ValueID::Auto:
  572. return CSS::Cursor::Auto;
  573. case CSS::ValueID::Default:
  574. return CSS::Cursor::Default;
  575. case CSS::ValueID::None:
  576. return CSS::Cursor::None;
  577. case CSS::ValueID::ContextMenu:
  578. return CSS::Cursor::ContextMenu;
  579. case CSS::ValueID::Help:
  580. return CSS::Cursor::Help;
  581. case CSS::ValueID::Pointer:
  582. return CSS::Cursor::Pointer;
  583. case CSS::ValueID::Progress:
  584. return CSS::Cursor::Progress;
  585. case CSS::ValueID::Wait:
  586. return CSS::Cursor::Wait;
  587. case CSS::ValueID::Cell:
  588. return CSS::Cursor::Cell;
  589. case CSS::ValueID::Crosshair:
  590. return CSS::Cursor::Crosshair;
  591. case CSS::ValueID::Text:
  592. return CSS::Cursor::Text;
  593. case CSS::ValueID::VerticalText:
  594. return CSS::Cursor::VerticalText;
  595. case CSS::ValueID::Alias:
  596. return CSS::Cursor::Alias;
  597. case CSS::ValueID::Copy:
  598. return CSS::Cursor::Copy;
  599. case CSS::ValueID::Move:
  600. return CSS::Cursor::Move;
  601. case CSS::ValueID::NoDrop:
  602. return CSS::Cursor::NoDrop;
  603. case CSS::ValueID::NotAllowed:
  604. return CSS::Cursor::NotAllowed;
  605. case CSS::ValueID::Grab:
  606. return CSS::Cursor::Grab;
  607. case CSS::ValueID::Grabbing:
  608. return CSS::Cursor::Grabbing;
  609. case CSS::ValueID::EResize:
  610. return CSS::Cursor::EResize;
  611. case CSS::ValueID::NResize:
  612. return CSS::Cursor::NResize;
  613. case CSS::ValueID::NeResize:
  614. return CSS::Cursor::NeResize;
  615. case CSS::ValueID::NwResize:
  616. return CSS::Cursor::NwResize;
  617. case CSS::ValueID::SResize:
  618. return CSS::Cursor::SResize;
  619. case CSS::ValueID::SeResize:
  620. return CSS::Cursor::SeResize;
  621. case CSS::ValueID::SwResize:
  622. return CSS::Cursor::SwResize;
  623. case CSS::ValueID::WResize:
  624. return CSS::Cursor::WResize;
  625. case CSS::ValueID::EwResize:
  626. return CSS::Cursor::EwResize;
  627. case CSS::ValueID::NsResize:
  628. return CSS::Cursor::NsResize;
  629. case CSS::ValueID::NeswResize:
  630. return CSS::Cursor::NeswResize;
  631. case CSS::ValueID::NwseResize:
  632. return CSS::Cursor::NwseResize;
  633. case CSS::ValueID::ColResize:
  634. return CSS::Cursor::ColResize;
  635. case CSS::ValueID::RowResize:
  636. return CSS::Cursor::RowResize;
  637. case CSS::ValueID::AllScroll:
  638. return CSS::Cursor::AllScroll;
  639. case CSS::ValueID::ZoomIn:
  640. return CSS::Cursor::ZoomIn;
  641. case CSS::ValueID::ZoomOut:
  642. return CSS::Cursor::ZoomOut;
  643. default:
  644. return {};
  645. }
  646. }
  647. Optional<CSS::Visibility> StyleProperties::visibility() const
  648. {
  649. auto value = property(CSS::PropertyID::Visibility);
  650. if (!value.has_value() || !value.value()->is_identifier())
  651. return {};
  652. switch (value.value()->to_identifier()) {
  653. case CSS::ValueID::Visible:
  654. return CSS::Visibility::Visible;
  655. case CSS::ValueID::Hidden:
  656. return CSS::Visibility::Hidden;
  657. case CSS::ValueID::Collapse:
  658. return CSS::Visibility::Collapse;
  659. default:
  660. return {};
  661. }
  662. }
  663. CSS::Display StyleProperties::display() const
  664. {
  665. auto value = property(CSS::PropertyID::Display);
  666. if (!value.has_value() || !value.value()->is_identifier())
  667. return CSS::Display::from_short(CSS::Display::Short::Inline);
  668. switch (value.value()->to_identifier()) {
  669. case CSS::ValueID::None:
  670. return CSS::Display::from_short(CSS::Display::Short::None);
  671. case CSS::ValueID::Block:
  672. return CSS::Display::from_short(CSS::Display::Short::Block);
  673. case CSS::ValueID::Inline:
  674. return CSS::Display::from_short(CSS::Display::Short::Inline);
  675. case CSS::ValueID::InlineBlock:
  676. return CSS::Display::from_short(CSS::Display::Short::InlineBlock);
  677. case CSS::ValueID::ListItem:
  678. return CSS::Display::from_short(CSS::Display::Short::ListItem);
  679. case CSS::ValueID::Table:
  680. return CSS::Display::from_short(CSS::Display::Short::Table);
  681. case CSS::ValueID::TableRow:
  682. return CSS::Display { CSS::Display::Internal::TableRow };
  683. case CSS::ValueID::TableCell:
  684. return CSS::Display { CSS::Display::Internal::TableCell };
  685. case CSS::ValueID::TableColumn:
  686. return CSS::Display { CSS::Display::Internal::TableColumn };
  687. case CSS::ValueID::TableColumnGroup:
  688. return CSS::Display { CSS::Display::Internal::TableColumnGroup };
  689. case CSS::ValueID::TableCaption:
  690. return CSS::Display { CSS::Display::Internal::TableCaption };
  691. case CSS::ValueID::TableRowGroup:
  692. return CSS::Display { CSS::Display::Internal::TableRowGroup };
  693. case CSS::ValueID::TableHeaderGroup:
  694. return CSS::Display { CSS::Display::Internal::TableHeaderGroup };
  695. case CSS::ValueID::TableFooterGroup:
  696. return CSS::Display { CSS::Display::Internal::TableFooterGroup };
  697. case CSS::ValueID::Flex:
  698. return CSS::Display::from_short(CSS::Display::Short::Flex);
  699. case CSS::ValueID::InlineFlex:
  700. return CSS::Display::from_short(CSS::Display::Short::InlineFlex);
  701. default:
  702. return CSS::Display::from_short(CSS::Display::Short::Block);
  703. }
  704. }
  705. Optional<CSS::TextDecorationLine> StyleProperties::text_decoration_line() const
  706. {
  707. auto value = property(CSS::PropertyID::TextDecorationLine);
  708. if (!value.has_value())
  709. return {};
  710. switch (value.value()->to_identifier()) {
  711. case CSS::ValueID::None:
  712. return CSS::TextDecorationLine::None;
  713. case CSS::ValueID::Underline:
  714. return CSS::TextDecorationLine::Underline;
  715. case CSS::ValueID::Overline:
  716. return CSS::TextDecorationLine::Overline;
  717. case CSS::ValueID::LineThrough:
  718. return CSS::TextDecorationLine::LineThrough;
  719. case CSS::ValueID::Blink:
  720. return CSS::TextDecorationLine::Blink;
  721. default:
  722. return {};
  723. }
  724. }
  725. Optional<CSS::TextDecorationStyle> StyleProperties::text_decoration_style() const
  726. {
  727. auto value = property(CSS::PropertyID::TextDecorationStyle);
  728. if (!value.has_value())
  729. return {};
  730. switch (value.value()->to_identifier()) {
  731. case CSS::ValueID::Solid:
  732. return CSS::TextDecorationStyle::Solid;
  733. case CSS::ValueID::Double:
  734. return CSS::TextDecorationStyle::Double;
  735. case CSS::ValueID::Dotted:
  736. return CSS::TextDecorationStyle::Dotted;
  737. case CSS::ValueID::Dashed:
  738. return CSS::TextDecorationStyle::Dashed;
  739. case CSS::ValueID::Wavy:
  740. return CSS::TextDecorationStyle::Wavy;
  741. default:
  742. return {};
  743. }
  744. }
  745. Optional<CSS::TextTransform> StyleProperties::text_transform() const
  746. {
  747. auto value = property(CSS::PropertyID::TextTransform);
  748. if (!value.has_value())
  749. return {};
  750. switch (value.value()->to_identifier()) {
  751. case CSS::ValueID::None:
  752. return CSS::TextTransform::None;
  753. case CSS::ValueID::Lowercase:
  754. return CSS::TextTransform::Lowercase;
  755. case CSS::ValueID::Uppercase:
  756. return CSS::TextTransform::Uppercase;
  757. case CSS::ValueID::Capitalize:
  758. return CSS::TextTransform::Capitalize;
  759. case CSS::ValueID::FullWidth:
  760. return CSS::TextTransform::FullWidth;
  761. case CSS::ValueID::FullSizeKana:
  762. return CSS::TextTransform::FullSizeKana;
  763. default:
  764. return {};
  765. }
  766. }
  767. Optional<CSS::ListStyleType> StyleProperties::list_style_type() const
  768. {
  769. auto value = property(CSS::PropertyID::ListStyleType);
  770. if (!value.has_value())
  771. return {};
  772. switch (value.value()->to_identifier()) {
  773. case CSS::ValueID::None:
  774. return CSS::ListStyleType::None;
  775. case CSS::ValueID::Disc:
  776. return CSS::ListStyleType::Disc;
  777. case CSS::ValueID::Circle:
  778. return CSS::ListStyleType::Circle;
  779. case CSS::ValueID::Square:
  780. return CSS::ListStyleType::Square;
  781. case CSS::ValueID::Decimal:
  782. return CSS::ListStyleType::Decimal;
  783. case CSS::ValueID::DecimalLeadingZero:
  784. return CSS::ListStyleType::DecimalLeadingZero;
  785. case CSS::ValueID::LowerAlpha:
  786. return CSS::ListStyleType::LowerAlpha;
  787. case CSS::ValueID::LowerLatin:
  788. return CSS::ListStyleType::LowerLatin;
  789. case CSS::ValueID::UpperAlpha:
  790. return CSS::ListStyleType::UpperAlpha;
  791. case CSS::ValueID::UpperLatin:
  792. return CSS::ListStyleType::UpperLatin;
  793. case CSS::ValueID::UpperRoman:
  794. return CSS::ListStyleType::UpperRoman;
  795. case CSS::ValueID::LowerRoman:
  796. return CSS::ListStyleType::LowerRoman;
  797. default:
  798. return {};
  799. }
  800. }
  801. Optional<CSS::Overflow> StyleProperties::overflow_x() const
  802. {
  803. return overflow(CSS::PropertyID::OverflowX);
  804. }
  805. Optional<CSS::Overflow> StyleProperties::overflow_y() const
  806. {
  807. return overflow(CSS::PropertyID::OverflowY);
  808. }
  809. Optional<CSS::Overflow> StyleProperties::overflow(CSS::PropertyID property_id) const
  810. {
  811. auto value = property(property_id);
  812. if (!value.has_value())
  813. return {};
  814. switch (value.value()->to_identifier()) {
  815. case CSS::ValueID::Auto:
  816. return CSS::Overflow::Auto;
  817. case CSS::ValueID::Visible:
  818. return CSS::Overflow::Visible;
  819. case CSS::ValueID::Hidden:
  820. return CSS::Overflow::Hidden;
  821. case CSS::ValueID::Clip:
  822. return CSS::Overflow::Clip;
  823. case CSS::ValueID::Scroll:
  824. return CSS::Overflow::Scroll;
  825. default:
  826. return {};
  827. }
  828. }
  829. Vector<ShadowData> StyleProperties::shadow(PropertyID property_id) const
  830. {
  831. auto value_or_error = property(property_id);
  832. if (!value_or_error.has_value())
  833. return {};
  834. auto value = value_or_error.value();
  835. auto make_shadow_data = [](ShadowStyleValue const& value) {
  836. return ShadowData { value.color(), value.offset_x(), value.offset_y(), value.blur_radius(), value.spread_distance(), value.placement() };
  837. };
  838. if (value->is_value_list()) {
  839. auto& value_list = value->as_value_list();
  840. Vector<ShadowData> shadow_data;
  841. shadow_data.ensure_capacity(value_list.size());
  842. for (auto const& layer_value : value_list.values())
  843. shadow_data.append(make_shadow_data(layer_value.as_shadow()));
  844. return shadow_data;
  845. }
  846. if (value->is_shadow()) {
  847. auto& box = value->as_shadow();
  848. return { make_shadow_data(box) };
  849. }
  850. return {};
  851. }
  852. Vector<ShadowData> StyleProperties::box_shadow() const
  853. {
  854. return shadow(PropertyID::BoxShadow);
  855. }
  856. Vector<ShadowData> StyleProperties::text_shadow() const
  857. {
  858. return shadow(PropertyID::TextShadow);
  859. }
  860. CSS::BoxSizing StyleProperties::box_sizing() const
  861. {
  862. auto value = property(CSS::PropertyID::BoxSizing);
  863. if (!value.has_value())
  864. return {};
  865. switch (value.value()->to_identifier()) {
  866. case CSS::ValueID::BorderBox:
  867. return CSS::BoxSizing::BorderBox;
  868. case CSS::ValueID::ContentBox:
  869. return CSS::BoxSizing::ContentBox;
  870. default:
  871. return {};
  872. }
  873. }
  874. Variant<CSS::VerticalAlign, CSS::LengthPercentage> StyleProperties::vertical_align() const
  875. {
  876. auto value = property(CSS::PropertyID::VerticalAlign);
  877. if (!value.has_value())
  878. VERIFY_NOT_REACHED();
  879. if (value.value()->is_identifier()) {
  880. switch (value.value()->to_identifier()) {
  881. case CSS::ValueID::Baseline:
  882. return CSS::VerticalAlign::Baseline;
  883. case CSS::ValueID::Bottom:
  884. return CSS::VerticalAlign::Bottom;
  885. case CSS::ValueID::Middle:
  886. return CSS::VerticalAlign::Middle;
  887. case CSS::ValueID::Sub:
  888. return CSS::VerticalAlign::Sub;
  889. case CSS::ValueID::Super:
  890. return CSS::VerticalAlign::Super;
  891. case CSS::ValueID::TextBottom:
  892. return CSS::VerticalAlign::TextBottom;
  893. case CSS::ValueID::TextTop:
  894. return CSS::VerticalAlign::TextTop;
  895. case CSS::ValueID::Top:
  896. return CSS::VerticalAlign::Top;
  897. default:
  898. VERIFY_NOT_REACHED();
  899. }
  900. }
  901. if (value.value()->is_length())
  902. return CSS::LengthPercentage(value.value()->to_length());
  903. if (value.value()->is_percentage())
  904. return CSS::LengthPercentage(value.value()->as_percentage().percentage());
  905. VERIFY_NOT_REACHED();
  906. }
  907. Optional<CSS::FontVariant> StyleProperties::font_variant() const
  908. {
  909. auto value = property(CSS::PropertyID::FontVariant);
  910. if (!value.has_value())
  911. return {};
  912. switch (value.value()->to_identifier()) {
  913. case CSS::ValueID::Normal:
  914. return CSS::FontVariant::Normal;
  915. case CSS::ValueID::SmallCaps:
  916. return CSS::FontVariant::SmallCaps;
  917. default:
  918. return {};
  919. }
  920. }
  921. }