StyleProperties.cpp 27 KB

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