StyleProperties.cpp 28 KB

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