StyleProperties.cpp 27 KB

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