StyleProperties.cpp 27 KB

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