StyleProperties.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. /*
  2. * Copyright (c) 2018-2023, 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 <LibWeb/CSS/Clip.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. #include <LibWeb/Platform/FontPlugin.h>
  15. namespace Web::CSS {
  16. StyleProperties::StyleProperties(StyleProperties const& other)
  17. : m_property_values(other.m_property_values)
  18. {
  19. if (other.m_font) {
  20. m_font = other.m_font->clone();
  21. } else {
  22. m_font = nullptr;
  23. }
  24. }
  25. NonnullRefPtr<StyleProperties> StyleProperties::clone() const
  26. {
  27. return adopt_ref(*new StyleProperties(*this));
  28. }
  29. void StyleProperties::set_property(CSS::PropertyID id, NonnullRefPtr<StyleValue const> value)
  30. {
  31. m_property_values[to_underlying(id)] = move(value);
  32. }
  33. NonnullRefPtr<StyleValue const> StyleProperties::property(CSS::PropertyID property_id) const
  34. {
  35. auto value = m_property_values[to_underlying(property_id)];
  36. // By the time we call this method, all properties have values assigned.
  37. VERIFY(!value.is_null());
  38. return value.release_nonnull();
  39. }
  40. RefPtr<StyleValue const> StyleProperties::maybe_null_property(CSS::PropertyID property_id) const
  41. {
  42. return m_property_values[to_underlying(property_id)];
  43. }
  44. CSS::Size StyleProperties::size_value(CSS::PropertyID id) const
  45. {
  46. auto value = property(id);
  47. if (value->is_identifier()) {
  48. switch (value->to_identifier()) {
  49. case ValueID::Auto:
  50. return CSS::Size::make_auto();
  51. case ValueID::MinContent:
  52. return CSS::Size::make_min_content();
  53. case ValueID::MaxContent:
  54. return CSS::Size::make_max_content();
  55. case ValueID::None:
  56. return CSS::Size::make_none();
  57. default:
  58. VERIFY_NOT_REACHED();
  59. }
  60. }
  61. if (value->is_calculated())
  62. return CSS::Size::make_length(CSS::Length::make_calculated(const_cast<CalculatedStyleValue&>(value->as_calculated())));
  63. if (value->is_percentage())
  64. return CSS::Size::make_percentage(value->as_percentage().percentage());
  65. if (value->has_length()) {
  66. auto length = value->to_length();
  67. if (length.is_auto())
  68. return CSS::Size::make_auto();
  69. return CSS::Size::make_length(value->to_length());
  70. }
  71. // FIXME: Support `fit-content(<length>)`
  72. dbgln("FIXME: Unsupported size value: `{}`, treating as `auto`", value->to_string());
  73. return CSS::Size::make_auto();
  74. }
  75. LengthPercentage StyleProperties::length_percentage_or_fallback(CSS::PropertyID id, LengthPercentage const& fallback) const
  76. {
  77. return length_percentage(id).value_or(fallback);
  78. }
  79. Optional<LengthPercentage> StyleProperties::length_percentage(CSS::PropertyID id) const
  80. {
  81. auto value = property(id);
  82. if (value->is_calculated())
  83. return LengthPercentage { const_cast<CalculatedStyleValue&>(value->as_calculated()) };
  84. if (value->is_percentage())
  85. return value->as_percentage().percentage();
  86. if (value->has_length())
  87. return value->to_length();
  88. return {};
  89. }
  90. 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
  91. {
  92. LengthBox box;
  93. box.left() = length_percentage_or_fallback(left_id, default_value);
  94. box.top() = length_percentage_or_fallback(top_id, default_value);
  95. box.right() = length_percentage_or_fallback(right_id, default_value);
  96. box.bottom() = length_percentage_or_fallback(bottom_id, default_value);
  97. return box;
  98. }
  99. Color StyleProperties::color_or_fallback(CSS::PropertyID id, Layout::NodeWithStyle const& node, Color fallback) const
  100. {
  101. auto value = property(id);
  102. if (!value->has_color())
  103. return fallback;
  104. return value->to_color(node);
  105. }
  106. NonnullRefPtr<Gfx::Font const> StyleProperties::font_fallback(bool monospace, bool bold)
  107. {
  108. if (monospace && bold)
  109. return Platform::FontPlugin::the().default_fixed_width_font().bold_variant();
  110. if (monospace)
  111. return Platform::FontPlugin::the().default_fixed_width_font();
  112. if (bold)
  113. return Platform::FontPlugin::the().default_font().bold_variant();
  114. return Platform::FontPlugin::the().default_font();
  115. }
  116. // FIXME: This implementation is almost identical to line_height(Layout::Node) below. Maybe they can be combined somehow.
  117. CSSPixels StyleProperties::line_height(CSSPixelRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, CSSPixels font_size, CSSPixels root_font_size, CSSPixels parent_line_height, CSSPixels root_line_height) const
  118. {
  119. auto line_height = property(CSS::PropertyID::LineHeight);
  120. if (line_height->is_identifier() && line_height->to_identifier() == ValueID::Normal)
  121. return font_metrics.line_spacing();
  122. if (line_height->is_length()) {
  123. auto line_height_length = line_height->to_length();
  124. if (!line_height_length.is_auto())
  125. return line_height_length.to_px(viewport_rect, font_metrics, font_size, root_font_size, parent_line_height, root_line_height);
  126. }
  127. if (line_height->is_numeric())
  128. return Length(line_height->to_number(), Length::Type::Em).to_px(viewport_rect, font_metrics, font_size, root_font_size, parent_line_height, root_line_height);
  129. if (line_height->is_percentage()) {
  130. // Percentages are relative to 1em. https://www.w3.org/TR/css-inline-3/#valdef-line-height-percentage
  131. auto& percentage = line_height->as_percentage().percentage();
  132. return Length(percentage.as_fraction(), Length::Type::Em).to_px(viewport_rect, font_metrics, font_size, root_font_size, parent_line_height, root_line_height);
  133. }
  134. if (line_height->is_calculated())
  135. return CSS::Length::make_calculated(const_cast<CalculatedStyleValue&>(line_height->as_calculated())).to_px(viewport_rect, font_metrics, font_size, root_font_size, parent_line_height, root_line_height);
  136. return font_metrics.line_spacing();
  137. }
  138. CSSPixels StyleProperties::line_height(Layout::Node const& layout_node) const
  139. {
  140. auto line_height = property(CSS::PropertyID::LineHeight);
  141. if (line_height->is_identifier() && line_height->to_identifier() == ValueID::Normal)
  142. return layout_node.font().pixel_metrics().line_spacing();
  143. if (line_height->is_length()) {
  144. auto line_height_length = line_height->to_length();
  145. if (!line_height_length.is_auto())
  146. return line_height_length.to_px(layout_node);
  147. }
  148. if (line_height->is_numeric())
  149. return Length(line_height->to_number(), Length::Type::Em).to_px(layout_node);
  150. if (line_height->is_percentage()) {
  151. // Percentages are relative to 1em. https://www.w3.org/TR/css-inline-3/#valdef-line-height-percentage
  152. auto& percentage = line_height->as_percentage().percentage();
  153. return Length(percentage.as_fraction(), Length::Type::Em).to_px(layout_node);
  154. }
  155. if (line_height->is_calculated())
  156. return CSS::Length::make_calculated(const_cast<CalculatedStyleValue&>(line_height->as_calculated())).to_px(layout_node);
  157. return layout_node.font().pixel_metrics().line_spacing();
  158. }
  159. Optional<int> StyleProperties::z_index() const
  160. {
  161. auto value = property(CSS::PropertyID::ZIndex);
  162. if (value->has_auto())
  163. return {};
  164. if (value->has_integer())
  165. return value->to_integer();
  166. return {};
  167. }
  168. float StyleProperties::opacity() const
  169. {
  170. auto value = property(CSS::PropertyID::Opacity);
  171. float unclamped_opacity = 1.0f;
  172. if (value->has_number()) {
  173. unclamped_opacity = value->to_number();
  174. } else if (value->is_calculated()) {
  175. auto& calculated = value->as_calculated();
  176. if (calculated.resolved_type() == CalculatedStyleValue::ResolvedType::Percentage) {
  177. auto maybe_percentage = value->as_calculated().resolve_percentage();
  178. if (maybe_percentage.has_value())
  179. unclamped_opacity = maybe_percentage->as_fraction();
  180. else
  181. dbgln("Unable to resolve calc() as opacity (percentage): {}", value->to_string());
  182. } else {
  183. auto maybe_number = const_cast<CalculatedStyleValue&>(value->as_calculated()).resolve_number();
  184. if (maybe_number.has_value())
  185. unclamped_opacity = maybe_number.value();
  186. else
  187. dbgln("Unable to resolve calc() as opacity (number): {}", value->to_string());
  188. }
  189. } else if (value->is_percentage()) {
  190. unclamped_opacity = value->as_percentage().percentage().as_fraction();
  191. }
  192. return clamp(unclamped_opacity, 0.0f, 1.0f);
  193. }
  194. Optional<CSS::FlexDirection> StyleProperties::flex_direction() const
  195. {
  196. auto value = property(CSS::PropertyID::FlexDirection);
  197. return value_id_to_flex_direction(value->to_identifier());
  198. }
  199. Optional<CSS::FlexWrap> StyleProperties::flex_wrap() const
  200. {
  201. auto value = property(CSS::PropertyID::FlexWrap);
  202. return value_id_to_flex_wrap(value->to_identifier());
  203. }
  204. Optional<CSS::FlexBasisData> StyleProperties::flex_basis() const
  205. {
  206. auto value = property(CSS::PropertyID::FlexBasis);
  207. if (value->is_identifier() && value->to_identifier() == CSS::ValueID::Content)
  208. return { { CSS::FlexBasis::Content, {} } };
  209. if (value->has_auto())
  210. return { { CSS::FlexBasis::Auto, {} } };
  211. if (value->is_percentage())
  212. return { { CSS::FlexBasis::LengthPercentage, value->as_percentage().percentage() } };
  213. if (value->has_length())
  214. return { { CSS::FlexBasis::LengthPercentage, value->to_length() } };
  215. return {};
  216. }
  217. float StyleProperties::flex_grow() const
  218. {
  219. auto value = property(CSS::PropertyID::FlexGrow);
  220. if (!value->has_number())
  221. return 0;
  222. return value->to_number();
  223. }
  224. float StyleProperties::flex_shrink() const
  225. {
  226. auto value = property(CSS::PropertyID::FlexShrink);
  227. if (!value->has_number())
  228. return 1;
  229. return value->to_number();
  230. }
  231. int StyleProperties::order() const
  232. {
  233. auto value = property(CSS::PropertyID::Order);
  234. if (!value->has_integer())
  235. return 0;
  236. return value->to_integer();
  237. }
  238. Optional<CSS::ImageRendering> StyleProperties::image_rendering() const
  239. {
  240. auto value = property(CSS::PropertyID::ImageRendering);
  241. return value_id_to_image_rendering(value->to_identifier());
  242. }
  243. CSS::Clip StyleProperties::clip() const
  244. {
  245. auto value = property(CSS::PropertyID::Clip);
  246. if (!value->has_rect())
  247. return CSS::Clip::make_auto();
  248. return CSS::Clip(value->as_rect().rect());
  249. }
  250. Optional<CSS::JustifyContent> StyleProperties::justify_content() const
  251. {
  252. auto value = property(CSS::PropertyID::JustifyContent);
  253. return value_id_to_justify_content(value->to_identifier());
  254. }
  255. Vector<CSS::Transformation> StyleProperties::transformations() const
  256. {
  257. auto value = property(CSS::PropertyID::Transform);
  258. if (value->is_identifier() && value->to_identifier() == CSS::ValueID::None)
  259. return {};
  260. if (!value->is_value_list())
  261. return {};
  262. auto& list = value->as_value_list();
  263. Vector<CSS::Transformation> transformations;
  264. for (auto& it : list.values()) {
  265. if (!it->is_transformation())
  266. return {};
  267. auto& transformation_style_value = it->as_transformation();
  268. CSS::Transformation transformation;
  269. transformation.function = transformation_style_value.transform_function();
  270. Vector<TransformValue> values;
  271. for (auto& transformation_value : transformation_style_value.values()) {
  272. if (transformation_value->is_length()) {
  273. values.append({ transformation_value->to_length() });
  274. } else if (transformation_value->is_percentage()) {
  275. values.append({ transformation_value->as_percentage().percentage() });
  276. } else if (transformation_value->is_numeric()) {
  277. values.append({ transformation_value->to_number() });
  278. } else if (transformation_value->is_angle()) {
  279. values.append({ transformation_value->as_angle().angle() });
  280. } else {
  281. dbgln("FIXME: Unsupported value in transform!");
  282. }
  283. }
  284. transformation.values = move(values);
  285. transformations.append(move(transformation));
  286. }
  287. return transformations;
  288. }
  289. static Optional<LengthPercentage> length_percentage_for_style_value(StyleValue const& value)
  290. {
  291. if (value.is_length())
  292. return value.to_length();
  293. if (value.is_percentage())
  294. return value.as_percentage().percentage();
  295. return {};
  296. }
  297. CSS::TransformOrigin StyleProperties::transform_origin() const
  298. {
  299. auto value = property(CSS::PropertyID::TransformOrigin);
  300. if (!value->is_value_list() || value->as_value_list().size() != 2)
  301. return {};
  302. auto const& list = value->as_value_list();
  303. auto x_value = length_percentage_for_style_value(list.values()[0]);
  304. auto y_value = length_percentage_for_style_value(list.values()[1]);
  305. if (!x_value.has_value() || !y_value.has_value()) {
  306. return {};
  307. }
  308. return { x_value.value(), y_value.value() };
  309. }
  310. Optional<CSS::AlignContent> StyleProperties::align_content() const
  311. {
  312. auto value = property(CSS::PropertyID::AlignContent);
  313. return value_id_to_align_content(value->to_identifier());
  314. }
  315. Optional<CSS::AlignItems> StyleProperties::align_items() const
  316. {
  317. auto value = property(CSS::PropertyID::AlignItems);
  318. return value_id_to_align_items(value->to_identifier());
  319. }
  320. Optional<CSS::AlignSelf> StyleProperties::align_self() const
  321. {
  322. auto value = property(CSS::PropertyID::AlignSelf);
  323. return value_id_to_align_self(value->to_identifier());
  324. }
  325. Optional<CSS::Appearance> StyleProperties::appearance() const
  326. {
  327. auto value = property(CSS::PropertyID::Appearance);
  328. auto appearance = value_id_to_appearance(value->to_identifier());
  329. if (appearance.has_value()) {
  330. switch (*appearance) {
  331. // Note: All these compatibility values can be treated as 'auto'
  332. case CSS::Appearance::Textfield:
  333. case CSS::Appearance::MenulistButton:
  334. case CSS::Appearance::Searchfield:
  335. case CSS::Appearance::Textarea:
  336. case CSS::Appearance::PushButton:
  337. case CSS::Appearance::SliderHorizontal:
  338. case CSS::Appearance::Checkbox:
  339. case CSS::Appearance::Radio:
  340. case CSS::Appearance::SquareButton:
  341. case CSS::Appearance::Menulist:
  342. case CSS::Appearance::Listbox:
  343. case CSS::Appearance::Meter:
  344. case CSS::Appearance::ProgressBar:
  345. case CSS::Appearance::Button:
  346. appearance = CSS::Appearance::Auto;
  347. break;
  348. default:
  349. break;
  350. }
  351. }
  352. return appearance;
  353. }
  354. CSS::BackdropFilter StyleProperties::backdrop_filter() const
  355. {
  356. auto value = property(CSS::PropertyID::BackdropFilter);
  357. if (value->is_filter_value_list())
  358. return BackdropFilter(value->as_filter_value_list());
  359. return BackdropFilter::make_none();
  360. }
  361. Optional<CSS::Position> StyleProperties::position() const
  362. {
  363. auto value = property(CSS::PropertyID::Position);
  364. return value_id_to_position(value->to_identifier());
  365. }
  366. bool StyleProperties::operator==(StyleProperties const& other) const
  367. {
  368. if (m_property_values.size() != other.m_property_values.size())
  369. return false;
  370. for (size_t i = 0; i < m_property_values.size(); ++i) {
  371. auto const& my_ptr = m_property_values[i];
  372. auto const& other_ptr = other.m_property_values[i];
  373. if (!my_ptr) {
  374. if (other_ptr)
  375. return false;
  376. continue;
  377. }
  378. if (!other_ptr)
  379. return false;
  380. auto const& my_value = *my_ptr;
  381. auto const& other_value = *other_ptr;
  382. if (my_value.type() != other_value.type())
  383. return false;
  384. if (my_value != other_value)
  385. return false;
  386. }
  387. return true;
  388. }
  389. Optional<CSS::TextAlign> StyleProperties::text_align() const
  390. {
  391. auto value = property(CSS::PropertyID::TextAlign);
  392. return value_id_to_text_align(value->to_identifier());
  393. }
  394. Optional<CSS::TextJustify> StyleProperties::text_justify() const
  395. {
  396. auto value = property(CSS::PropertyID::TextJustify);
  397. return value_id_to_text_justify(value->to_identifier());
  398. }
  399. Optional<CSS::PointerEvents> StyleProperties::pointer_events() const
  400. {
  401. auto value = property(CSS::PropertyID::PointerEvents);
  402. return value_id_to_pointer_events(value->to_identifier());
  403. }
  404. Optional<CSS::WhiteSpace> StyleProperties::white_space() const
  405. {
  406. auto value = property(CSS::PropertyID::WhiteSpace);
  407. return value_id_to_white_space(value->to_identifier());
  408. }
  409. Optional<CSS::LineStyle> StyleProperties::line_style(CSS::PropertyID property_id) const
  410. {
  411. auto value = property(property_id);
  412. return value_id_to_line_style(value->to_identifier());
  413. }
  414. Optional<CSS::Float> StyleProperties::float_() const
  415. {
  416. auto value = property(CSS::PropertyID::Float);
  417. return value_id_to_float(value->to_identifier());
  418. }
  419. Optional<CSS::Clear> StyleProperties::clear() const
  420. {
  421. auto value = property(CSS::PropertyID::Clear);
  422. return value_id_to_clear(value->to_identifier());
  423. }
  424. CSS::ContentData StyleProperties::content() const
  425. {
  426. auto value = property(CSS::PropertyID::Content);
  427. if (value->is_content()) {
  428. auto& content_style_value = value->as_content();
  429. CSS::ContentData content_data;
  430. // FIXME: The content is a list of things: strings, identifiers or functions that return strings, and images.
  431. // So it can't always be represented as a single String, but may have to be multiple boxes.
  432. // For now, we'll just assume strings since that is easiest.
  433. StringBuilder builder;
  434. for (auto const& item : content_style_value.content().values()) {
  435. if (item->is_string()) {
  436. builder.append(item->to_string().release_value_but_fixme_should_propagate_errors());
  437. } else {
  438. // TODO: Implement quotes, counters, images, and other things.
  439. }
  440. }
  441. content_data.type = ContentData::Type::String;
  442. content_data.data = builder.to_string().release_value_but_fixme_should_propagate_errors();
  443. if (content_style_value.has_alt_text()) {
  444. StringBuilder alt_text_builder;
  445. for (auto const& item : content_style_value.alt_text()->values()) {
  446. if (item->is_string()) {
  447. alt_text_builder.append(item->to_string().release_value_but_fixme_should_propagate_errors());
  448. } else {
  449. // TODO: Implement counters
  450. }
  451. }
  452. content_data.alt_text = alt_text_builder.to_string().release_value_but_fixme_should_propagate_errors();
  453. }
  454. return content_data;
  455. }
  456. switch (value->to_identifier()) {
  457. case ValueID::None:
  458. return { ContentData::Type::None };
  459. case ValueID::Normal:
  460. return { ContentData::Type::Normal };
  461. default:
  462. break;
  463. }
  464. return CSS::ContentData {};
  465. }
  466. Optional<CSS::Cursor> StyleProperties::cursor() const
  467. {
  468. auto value = property(CSS::PropertyID::Cursor);
  469. return value_id_to_cursor(value->to_identifier());
  470. }
  471. Optional<CSS::Visibility> StyleProperties::visibility() const
  472. {
  473. auto value = property(CSS::PropertyID::Visibility);
  474. if (!value->is_identifier())
  475. return {};
  476. return value_id_to_visibility(value->to_identifier());
  477. }
  478. CSS::Display StyleProperties::display() const
  479. {
  480. auto value = property(CSS::PropertyID::Display);
  481. if (!value->is_identifier())
  482. return CSS::Display::from_short(CSS::Display::Short::Inline);
  483. switch (value->to_identifier()) {
  484. case CSS::ValueID::None:
  485. return CSS::Display::from_short(CSS::Display::Short::None);
  486. case CSS::ValueID::Block:
  487. return CSS::Display::from_short(CSS::Display::Short::Block);
  488. case CSS::ValueID::Inline:
  489. return CSS::Display::from_short(CSS::Display::Short::Inline);
  490. case CSS::ValueID::InlineBlock:
  491. return CSS::Display::from_short(CSS::Display::Short::InlineBlock);
  492. case CSS::ValueID::ListItem:
  493. return CSS::Display::from_short(CSS::Display::Short::ListItem);
  494. case CSS::ValueID::Table:
  495. return CSS::Display::from_short(CSS::Display::Short::Table);
  496. case CSS::ValueID::InlineTable:
  497. return CSS::Display::from_short(CSS::Display::Short::InlineTable);
  498. case CSS::ValueID::TableRow:
  499. return CSS::Display { CSS::Display::Internal::TableRow };
  500. case CSS::ValueID::TableCell:
  501. return CSS::Display { CSS::Display::Internal::TableCell };
  502. case CSS::ValueID::TableColumn:
  503. return CSS::Display { CSS::Display::Internal::TableColumn };
  504. case CSS::ValueID::TableColumnGroup:
  505. return CSS::Display { CSS::Display::Internal::TableColumnGroup };
  506. case CSS::ValueID::TableCaption:
  507. return CSS::Display { CSS::Display::Internal::TableCaption };
  508. case CSS::ValueID::TableRowGroup:
  509. return CSS::Display { CSS::Display::Internal::TableRowGroup };
  510. case CSS::ValueID::TableHeaderGroup:
  511. return CSS::Display { CSS::Display::Internal::TableHeaderGroup };
  512. case CSS::ValueID::TableFooterGroup:
  513. return CSS::Display { CSS::Display::Internal::TableFooterGroup };
  514. case CSS::ValueID::Flex:
  515. return CSS::Display::from_short(CSS::Display::Short::Flex);
  516. case CSS::ValueID::InlineFlex:
  517. return CSS::Display::from_short(CSS::Display::Short::InlineFlex);
  518. case CSS::ValueID::Grid:
  519. return CSS::Display::from_short(CSS::Display::Short::Grid);
  520. default:
  521. return CSS::Display::from_short(CSS::Display::Short::Block);
  522. }
  523. }
  524. Vector<CSS::TextDecorationLine> StyleProperties::text_decoration_line() const
  525. {
  526. auto value = property(CSS::PropertyID::TextDecorationLine);
  527. if (value->is_value_list()) {
  528. Vector<CSS::TextDecorationLine> lines;
  529. auto& values = value->as_value_list().values();
  530. for (auto const& item : values) {
  531. lines.append(value_id_to_text_decoration_line(item->to_identifier()).value());
  532. }
  533. return lines;
  534. }
  535. if (value->is_identifier() && value->to_identifier() == ValueID::None)
  536. return {};
  537. dbgln("FIXME: Unsupported value for text-decoration-line: {}", value->to_string());
  538. return {};
  539. }
  540. Optional<CSS::TextDecorationStyle> StyleProperties::text_decoration_style() const
  541. {
  542. auto value = property(CSS::PropertyID::TextDecorationStyle);
  543. return value_id_to_text_decoration_style(value->to_identifier());
  544. }
  545. Optional<CSS::TextTransform> StyleProperties::text_transform() const
  546. {
  547. auto value = property(CSS::PropertyID::TextTransform);
  548. return value_id_to_text_transform(value->to_identifier());
  549. }
  550. Optional<CSS::ListStyleType> StyleProperties::list_style_type() const
  551. {
  552. auto value = property(CSS::PropertyID::ListStyleType);
  553. return value_id_to_list_style_type(value->to_identifier());
  554. }
  555. Optional<CSS::Overflow> StyleProperties::overflow_x() const
  556. {
  557. return overflow(CSS::PropertyID::OverflowX);
  558. }
  559. Optional<CSS::Overflow> StyleProperties::overflow_y() const
  560. {
  561. return overflow(CSS::PropertyID::OverflowY);
  562. }
  563. Optional<CSS::Overflow> StyleProperties::overflow(CSS::PropertyID property_id) const
  564. {
  565. auto value = property(property_id);
  566. return value_id_to_overflow(value->to_identifier());
  567. }
  568. Vector<ShadowData> StyleProperties::shadow(PropertyID property_id) const
  569. {
  570. auto value = property(property_id);
  571. auto make_shadow_data = [](ShadowStyleValue const& value) {
  572. return ShadowData { value.color(), value.offset_x(), value.offset_y(), value.blur_radius(), value.spread_distance(), value.placement() };
  573. };
  574. if (value->is_value_list()) {
  575. auto& value_list = value->as_value_list();
  576. Vector<ShadowData> shadow_data;
  577. shadow_data.ensure_capacity(value_list.size());
  578. for (auto const& layer_value : value_list.values())
  579. shadow_data.append(make_shadow_data(layer_value->as_shadow()));
  580. return shadow_data;
  581. }
  582. if (value->is_shadow()) {
  583. auto& box = value->as_shadow();
  584. return { make_shadow_data(box) };
  585. }
  586. return {};
  587. }
  588. Vector<ShadowData> StyleProperties::box_shadow() const
  589. {
  590. return shadow(PropertyID::BoxShadow);
  591. }
  592. Vector<ShadowData> StyleProperties::text_shadow() const
  593. {
  594. return shadow(PropertyID::TextShadow);
  595. }
  596. Optional<CSS::BoxSizing> StyleProperties::box_sizing() const
  597. {
  598. auto value = property(CSS::PropertyID::BoxSizing);
  599. return value_id_to_box_sizing(value->to_identifier());
  600. }
  601. Variant<CSS::VerticalAlign, CSS::LengthPercentage> StyleProperties::vertical_align() const
  602. {
  603. auto value = property(CSS::PropertyID::VerticalAlign);
  604. if (value->is_identifier())
  605. return value_id_to_vertical_align(value->to_identifier()).release_value();
  606. if (value->is_length())
  607. return CSS::LengthPercentage(value->to_length());
  608. if (value->is_percentage())
  609. return CSS::LengthPercentage(value->as_percentage().percentage());
  610. VERIFY_NOT_REACHED();
  611. }
  612. Optional<CSS::FontVariant> StyleProperties::font_variant() const
  613. {
  614. auto value = property(CSS::PropertyID::FontVariant);
  615. return value_id_to_font_variant(value->to_identifier());
  616. }
  617. CSS::GridTrackSizeList StyleProperties::grid_template_columns() const
  618. {
  619. auto value = property(CSS::PropertyID::GridTemplateColumns);
  620. return value->as_grid_track_size_list().grid_track_size_list();
  621. }
  622. CSS::GridTrackSizeList StyleProperties::grid_template_rows() const
  623. {
  624. auto value = property(CSS::PropertyID::GridTemplateRows);
  625. return value->as_grid_track_size_list().grid_track_size_list();
  626. }
  627. CSS::GridTrackPlacement StyleProperties::grid_column_end() const
  628. {
  629. auto value = property(CSS::PropertyID::GridColumnEnd);
  630. return value->as_grid_track_placement().grid_track_placement();
  631. }
  632. CSS::GridTrackPlacement StyleProperties::grid_column_start() const
  633. {
  634. auto value = property(CSS::PropertyID::GridColumnStart);
  635. return value->as_grid_track_placement().grid_track_placement();
  636. }
  637. CSS::GridTrackPlacement StyleProperties::grid_row_end() const
  638. {
  639. auto value = property(CSS::PropertyID::GridRowEnd);
  640. return value->as_grid_track_placement().grid_track_placement();
  641. }
  642. CSS::GridTrackPlacement StyleProperties::grid_row_start() const
  643. {
  644. auto value = property(CSS::PropertyID::GridRowStart);
  645. return value->as_grid_track_placement().grid_track_placement();
  646. }
  647. Optional<CSS::BorderCollapse> StyleProperties::border_collapse() const
  648. {
  649. auto value = property(CSS::PropertyID::BorderCollapse);
  650. return value_id_to_border_collapse(value->to_identifier());
  651. }
  652. Vector<Vector<String>> StyleProperties::grid_template_areas() const
  653. {
  654. auto value = property(CSS::PropertyID::GridTemplateAreas);
  655. return value->as_grid_template_area().grid_template_area();
  656. }
  657. String StyleProperties::grid_area() const
  658. {
  659. auto value = property(CSS::PropertyID::GridArea);
  660. return value->as_string().to_string().release_value_but_fixme_should_propagate_errors();
  661. }
  662. }