StyleProperties.cpp 27 KB

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