StyleProperties.cpp 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  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/GridAutoFlowStyleValue.h>
  15. #include <LibWeb/CSS/StyleValues/GridTemplateAreaStyleValue.h>
  16. #include <LibWeb/CSS/StyleValues/GridTrackPlacementStyleValue.h>
  17. #include <LibWeb/CSS/StyleValues/GridTrackSizeListStyleValue.h>
  18. #include <LibWeb/CSS/StyleValues/IdentifierStyleValue.h>
  19. #include <LibWeb/CSS/StyleValues/IntegerStyleValue.h>
  20. #include <LibWeb/CSS/StyleValues/LengthStyleValue.h>
  21. #include <LibWeb/CSS/StyleValues/MathDepthStyleValue.h>
  22. #include <LibWeb/CSS/StyleValues/NumberStyleValue.h>
  23. #include <LibWeb/CSS/StyleValues/PercentageStyleValue.h>
  24. #include <LibWeb/CSS/StyleValues/RectStyleValue.h>
  25. #include <LibWeb/CSS/StyleValues/ShadowStyleValue.h>
  26. #include <LibWeb/CSS/StyleValues/StringStyleValue.h>
  27. #include <LibWeb/CSS/StyleValues/StyleValueList.h>
  28. #include <LibWeb/CSS/StyleValues/TransformationStyleValue.h>
  29. #include <LibWeb/FontCache.h>
  30. #include <LibWeb/Layout/BlockContainer.h>
  31. #include <LibWeb/Layout/Node.h>
  32. #include <LibWeb/Platform/FontPlugin.h>
  33. namespace Web::CSS {
  34. StyleProperties::StyleProperties(StyleProperties const& other)
  35. : m_property_values(other.m_property_values)
  36. {
  37. if (other.m_font) {
  38. m_font = other.m_font->clone();
  39. } else {
  40. m_font = nullptr;
  41. }
  42. }
  43. NonnullRefPtr<StyleProperties> StyleProperties::clone() const
  44. {
  45. return adopt_ref(*new StyleProperties(*this));
  46. }
  47. void StyleProperties::set_property(CSS::PropertyID id, NonnullRefPtr<StyleValue const> value, CSS::CSSStyleDeclaration const* source_declaration)
  48. {
  49. m_property_values[to_underlying(id)] = StyleAndSourceDeclaration { move(value), source_declaration };
  50. }
  51. NonnullRefPtr<StyleValue const> StyleProperties::property(CSS::PropertyID property_id) const
  52. {
  53. auto value = m_property_values[to_underlying(property_id)];
  54. // By the time we call this method, all properties have values assigned.
  55. VERIFY(value.has_value());
  56. return value->style;
  57. }
  58. RefPtr<StyleValue const> StyleProperties::maybe_null_property(CSS::PropertyID property_id) const
  59. {
  60. auto value = m_property_values[to_underlying(property_id)];
  61. if (value.has_value())
  62. return value->style;
  63. return {};
  64. }
  65. CSS::CSSStyleDeclaration const* StyleProperties::property_source_declaration(CSS::PropertyID property_id) const
  66. {
  67. return m_property_values[to_underlying(property_id)].map([](auto& value) { return value.declaration; }).value_or(nullptr);
  68. }
  69. CSS::Size StyleProperties::size_value(CSS::PropertyID id) const
  70. {
  71. auto value = property(id);
  72. if (value->is_identifier()) {
  73. switch (value->to_identifier()) {
  74. case ValueID::Auto:
  75. return CSS::Size::make_auto();
  76. case ValueID::MinContent:
  77. return CSS::Size::make_min_content();
  78. case ValueID::MaxContent:
  79. return CSS::Size::make_max_content();
  80. case ValueID::FitContent:
  81. return CSS::Size::make_fit_content();
  82. case ValueID::None:
  83. return CSS::Size::make_none();
  84. default:
  85. VERIFY_NOT_REACHED();
  86. }
  87. }
  88. if (value->is_calculated())
  89. return CSS::Size::make_calculated(const_cast<CalculatedStyleValue&>(value->as_calculated()));
  90. if (value->is_percentage())
  91. return CSS::Size::make_percentage(value->as_percentage().percentage());
  92. if (value->is_length()) {
  93. auto length = value->as_length().length();
  94. if (length.is_auto())
  95. return CSS::Size::make_auto();
  96. return CSS::Size::make_length(length);
  97. }
  98. // FIXME: Support `fit-content(<length>)`
  99. dbgln("FIXME: Unsupported size value: `{}`, treating as `auto`", value->to_string());
  100. return CSS::Size::make_auto();
  101. }
  102. LengthPercentage StyleProperties::length_percentage_or_fallback(CSS::PropertyID id, LengthPercentage const& fallback) const
  103. {
  104. return length_percentage(id).value_or(fallback);
  105. }
  106. Optional<LengthPercentage> StyleProperties::length_percentage(CSS::PropertyID id) const
  107. {
  108. auto value = property(id);
  109. if (value->is_calculated())
  110. return LengthPercentage { const_cast<CalculatedStyleValue&>(value->as_calculated()) };
  111. if (value->is_percentage())
  112. return value->as_percentage().percentage();
  113. if (value->is_length())
  114. return value->as_length().length();
  115. if (value->has_auto())
  116. return LengthPercentage { Length::make_auto() };
  117. return {};
  118. }
  119. 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
  120. {
  121. LengthBox box;
  122. box.left() = length_percentage_or_fallback(left_id, default_value);
  123. box.top() = length_percentage_or_fallback(top_id, default_value);
  124. box.right() = length_percentage_or_fallback(right_id, default_value);
  125. box.bottom() = length_percentage_or_fallback(bottom_id, default_value);
  126. return box;
  127. }
  128. Color StyleProperties::color_or_fallback(CSS::PropertyID id, Layout::NodeWithStyle const& node, Color fallback) const
  129. {
  130. auto value = property(id);
  131. if (!value->has_color())
  132. return fallback;
  133. return value->to_color(node);
  134. }
  135. NonnullRefPtr<Gfx::Font const> StyleProperties::font_fallback(bool monospace, bool bold)
  136. {
  137. if (monospace && bold)
  138. return Platform::FontPlugin::the().default_fixed_width_font().bold_variant();
  139. if (monospace)
  140. return Platform::FontPlugin::the().default_fixed_width_font();
  141. if (bold)
  142. return Platform::FontPlugin::the().default_font().bold_variant();
  143. return Platform::FontPlugin::the().default_font();
  144. }
  145. // FIXME: This implementation is almost identical to line_height(Layout::Node) below. Maybe they can be combined somehow.
  146. CSSPixels StyleProperties::line_height(CSSPixelRect const& viewport_rect, Length::FontMetrics const& font_metrics, Length::FontMetrics const& root_font_metrics) const
  147. {
  148. auto line_height = property(CSS::PropertyID::LineHeight);
  149. if (line_height->is_identifier() && line_height->to_identifier() == ValueID::Normal)
  150. return font_metrics.line_height;
  151. if (line_height->is_length()) {
  152. auto line_height_length = line_height->as_length().length();
  153. if (!line_height_length.is_auto())
  154. return line_height_length.to_px(viewport_rect, font_metrics, root_font_metrics);
  155. }
  156. if (line_height->is_number())
  157. return Length(line_height->as_number().number(), Length::Type::Em).to_px(viewport_rect, font_metrics, root_font_metrics);
  158. if (line_height->is_percentage()) {
  159. // Percentages are relative to 1em. https://www.w3.org/TR/css-inline-3/#valdef-line-height-percentage
  160. auto& percentage = line_height->as_percentage().percentage();
  161. return Length(percentage.as_fraction(), Length::Type::Em).to_px(viewport_rect, font_metrics, root_font_metrics);
  162. }
  163. if (line_height->is_calculated()) {
  164. // FIXME: Handle `line-height: calc(...)` despite not having a LayoutNode here.
  165. return font_metrics.line_height;
  166. }
  167. return font_metrics.line_height;
  168. }
  169. CSSPixels StyleProperties::line_height(Layout::Node const& layout_node) const
  170. {
  171. auto line_height = property(CSS::PropertyID::LineHeight);
  172. if (line_height->is_identifier() && line_height->to_identifier() == ValueID::Normal)
  173. return CSSPixels::nearest_value_for(layout_node.font().pixel_metrics().line_spacing());
  174. if (line_height->is_length()) {
  175. auto line_height_length = line_height->as_length().length();
  176. if (!line_height_length.is_auto())
  177. return line_height_length.to_px(layout_node);
  178. }
  179. if (line_height->is_number())
  180. return Length(line_height->as_number().number(), Length::Type::Em).to_px(layout_node);
  181. if (line_height->is_percentage()) {
  182. // Percentages are relative to 1em. https://www.w3.org/TR/css-inline-3/#valdef-line-height-percentage
  183. auto& percentage = line_height->as_percentage().percentage();
  184. return Length(percentage.as_fraction(), Length::Type::Em).to_px(layout_node);
  185. }
  186. if (line_height->is_calculated()) {
  187. if (line_height->as_calculated().resolves_to_number()) {
  188. auto resolved = line_height->as_calculated().resolve_number();
  189. if (!resolved.has_value()) {
  190. dbgln("FIXME: Failed to resolve calc() line-height (number): {}", line_height->as_calculated().to_string());
  191. return CSSPixels::nearest_value_for(layout_node.font().pixel_metrics().line_spacing());
  192. }
  193. return Length(resolved.value(), Length::Type::Em).to_px(layout_node);
  194. }
  195. auto resolved = line_height->as_calculated().resolve_length(layout_node);
  196. if (!resolved.has_value()) {
  197. dbgln("FIXME: Failed to resolve calc() line-height: {}", line_height->as_calculated().to_string());
  198. return CSSPixels::nearest_value_for(layout_node.font().pixel_metrics().line_spacing());
  199. }
  200. return resolved->to_px(layout_node);
  201. }
  202. return CSSPixels::nearest_value_for(layout_node.font().pixel_metrics().line_spacing());
  203. }
  204. Optional<int> StyleProperties::z_index() const
  205. {
  206. auto value = property(CSS::PropertyID::ZIndex);
  207. if (value->has_auto())
  208. return {};
  209. if (value->is_integer()) {
  210. // Clamp z-index to the range of a signed 32-bit integer for consistency with other engines.
  211. auto integer = value->as_integer().integer();
  212. if (integer >= NumericLimits<int>::max())
  213. return NumericLimits<int>::max();
  214. if (integer <= NumericLimits<int>::min())
  215. return NumericLimits<int>::min();
  216. return static_cast<int>(integer);
  217. }
  218. return {};
  219. }
  220. static float resolve_opacity_value(CSS::StyleValue const& value)
  221. {
  222. float unclamped_opacity = 1.0f;
  223. if (value.is_number()) {
  224. unclamped_opacity = value.as_number().number();
  225. } else if (value.is_calculated()) {
  226. auto& calculated = value.as_calculated();
  227. if (calculated.resolves_to_percentage()) {
  228. auto maybe_percentage = value.as_calculated().resolve_percentage();
  229. if (maybe_percentage.has_value())
  230. unclamped_opacity = maybe_percentage->as_fraction();
  231. else
  232. dbgln("Unable to resolve calc() as opacity (percentage): {}", value.to_string());
  233. } else if (calculated.resolves_to_number()) {
  234. auto maybe_number = const_cast<CalculatedStyleValue&>(value.as_calculated()).resolve_number();
  235. if (maybe_number.has_value())
  236. unclamped_opacity = maybe_number.value();
  237. else
  238. dbgln("Unable to resolve calc() as opacity (number): {}", value.to_string());
  239. }
  240. } else if (value.is_percentage()) {
  241. unclamped_opacity = value.as_percentage().percentage().as_fraction();
  242. }
  243. return clamp(unclamped_opacity, 0.0f, 1.0f);
  244. }
  245. float StyleProperties::opacity() const
  246. {
  247. auto value = property(CSS::PropertyID::Opacity);
  248. return resolve_opacity_value(*value);
  249. }
  250. float StyleProperties::fill_opacity() const
  251. {
  252. auto value = property(CSS::PropertyID::FillOpacity);
  253. return resolve_opacity_value(*value);
  254. }
  255. float StyleProperties::stroke_opacity() const
  256. {
  257. auto value = property(CSS::PropertyID::StrokeOpacity);
  258. return resolve_opacity_value(*value);
  259. }
  260. float StyleProperties::stop_opacity() const
  261. {
  262. auto value = property(CSS::PropertyID::StopOpacity);
  263. return resolve_opacity_value(*value);
  264. }
  265. Optional<CSS::FillRule> StyleProperties::fill_rule() const
  266. {
  267. auto value = property(CSS::PropertyID::FillRule);
  268. return value_id_to_fill_rule(value->to_identifier());
  269. }
  270. Optional<CSS::FlexDirection> StyleProperties::flex_direction() const
  271. {
  272. auto value = property(CSS::PropertyID::FlexDirection);
  273. return value_id_to_flex_direction(value->to_identifier());
  274. }
  275. Optional<CSS::FlexWrap> StyleProperties::flex_wrap() const
  276. {
  277. auto value = property(CSS::PropertyID::FlexWrap);
  278. return value_id_to_flex_wrap(value->to_identifier());
  279. }
  280. Optional<CSS::FlexBasis> StyleProperties::flex_basis() const
  281. {
  282. auto value = property(CSS::PropertyID::FlexBasis);
  283. if (value->is_identifier() && value->to_identifier() == CSS::ValueID::Content)
  284. return CSS::FlexBasisContent {};
  285. return size_value(CSS::PropertyID::FlexBasis);
  286. }
  287. float StyleProperties::flex_grow() const
  288. {
  289. auto value = property(CSS::PropertyID::FlexGrow);
  290. if (!value->is_number())
  291. return 0;
  292. return value->as_number().number();
  293. }
  294. float StyleProperties::flex_shrink() const
  295. {
  296. auto value = property(CSS::PropertyID::FlexShrink);
  297. if (!value->is_number())
  298. return 1;
  299. return value->as_number().number();
  300. }
  301. int StyleProperties::order() const
  302. {
  303. auto value = property(CSS::PropertyID::Order);
  304. if (!value->is_integer())
  305. return 0;
  306. return value->as_integer().integer();
  307. }
  308. Optional<CSS::ImageRendering> StyleProperties::image_rendering() const
  309. {
  310. auto value = property(CSS::PropertyID::ImageRendering);
  311. return value_id_to_image_rendering(value->to_identifier());
  312. }
  313. CSS::Length StyleProperties::border_spacing_horizontal() const
  314. {
  315. auto value = property(CSS::PropertyID::BorderSpacing);
  316. if (value->is_length())
  317. return value->as_length().length();
  318. auto const& list = value->as_value_list();
  319. return list.value_at(0, false)->as_length().length();
  320. }
  321. CSS::Length StyleProperties::border_spacing_vertical() const
  322. {
  323. auto value = property(CSS::PropertyID::BorderSpacing);
  324. if (value->is_length())
  325. return value->as_length().length();
  326. auto const& list = value->as_value_list();
  327. return list.value_at(1, false)->as_length().length();
  328. }
  329. Optional<CSS::CaptionSide> StyleProperties::caption_side() const
  330. {
  331. auto value = property(CSS::PropertyID::CaptionSide);
  332. return value_id_to_caption_side(value->to_identifier());
  333. }
  334. CSS::Clip StyleProperties::clip() const
  335. {
  336. auto value = property(CSS::PropertyID::Clip);
  337. if (!value->is_rect())
  338. return CSS::Clip::make_auto();
  339. return CSS::Clip(value->as_rect().rect());
  340. }
  341. Optional<CSS::JustifyContent> StyleProperties::justify_content() const
  342. {
  343. auto value = property(CSS::PropertyID::JustifyContent);
  344. return value_id_to_justify_content(value->to_identifier());
  345. }
  346. Optional<CSS::JustifyItems> StyleProperties::justify_items() const
  347. {
  348. auto value = property(CSS::PropertyID::JustifyItems);
  349. return value_id_to_justify_items(value->to_identifier());
  350. }
  351. Optional<CSS::JustifySelf> StyleProperties::justify_self() const
  352. {
  353. auto value = property(CSS::PropertyID::JustifySelf);
  354. return value_id_to_justify_self(value->to_identifier());
  355. }
  356. Vector<CSS::Transformation> StyleProperties::transformations() const
  357. {
  358. auto value = property(CSS::PropertyID::Transform);
  359. if (value->is_identifier() && value->to_identifier() == CSS::ValueID::None)
  360. return {};
  361. if (!value->is_value_list())
  362. return {};
  363. auto& list = value->as_value_list();
  364. Vector<CSS::Transformation> transformations;
  365. for (auto& it : list.values()) {
  366. if (!it->is_transformation())
  367. return {};
  368. auto& transformation_style_value = it->as_transformation();
  369. CSS::Transformation transformation;
  370. transformation.function = transformation_style_value.transform_function();
  371. Vector<TransformValue> values;
  372. for (auto& transformation_value : transformation_style_value.values()) {
  373. if (transformation_value->is_calculated()) {
  374. auto& calculated = transformation_value->as_calculated();
  375. if (calculated.resolves_to_length()) {
  376. values.append(CSS::LengthPercentage { calculated });
  377. } else if (calculated.resolves_to_percentage()) {
  378. values.append({ calculated.resolve_percentage().value() });
  379. } else if (calculated.resolves_to_number()) {
  380. values.append({ calculated.resolve_number().value() });
  381. } else if (calculated.resolves_to_angle()) {
  382. values.append({ calculated.resolve_angle().value() });
  383. } else {
  384. dbgln("FIXME: Unsupported calc value in transform! {}", calculated.to_string());
  385. }
  386. } else if (transformation_value->is_length()) {
  387. values.append({ transformation_value->as_length().length() });
  388. } else if (transformation_value->is_percentage()) {
  389. values.append({ transformation_value->as_percentage().percentage() });
  390. } else if (transformation_value->is_number()) {
  391. values.append({ transformation_value->as_number().number() });
  392. } else if (transformation_value->is_angle()) {
  393. values.append({ transformation_value->as_angle().angle() });
  394. } else {
  395. dbgln("FIXME: Unsupported value in transform! {}", transformation_value->to_string());
  396. }
  397. }
  398. transformation.values = move(values);
  399. transformations.append(move(transformation));
  400. }
  401. return transformations;
  402. }
  403. static Optional<LengthPercentage> length_percentage_for_style_value(StyleValue const& value)
  404. {
  405. if (value.is_length())
  406. return value.as_length().length();
  407. if (value.is_percentage())
  408. return value.as_percentage().percentage();
  409. return {};
  410. }
  411. CSS::TransformOrigin StyleProperties::transform_origin() const
  412. {
  413. auto value = property(CSS::PropertyID::TransformOrigin);
  414. if (!value->is_value_list() || value->as_value_list().size() != 2)
  415. return {};
  416. auto const& list = value->as_value_list();
  417. auto x_value = length_percentage_for_style_value(list.values()[0]);
  418. auto y_value = length_percentage_for_style_value(list.values()[1]);
  419. if (!x_value.has_value() || !y_value.has_value()) {
  420. return {};
  421. }
  422. return { x_value.value(), y_value.value() };
  423. }
  424. Optional<Color> StyleProperties::accent_color(Layout::NodeWithStyle const& node) const
  425. {
  426. auto value = property(CSS::PropertyID::AccentColor);
  427. if (value->has_color())
  428. return value->to_color(node);
  429. return {};
  430. }
  431. Optional<CSS::AlignContent> StyleProperties::align_content() const
  432. {
  433. auto value = property(CSS::PropertyID::AlignContent);
  434. return value_id_to_align_content(value->to_identifier());
  435. }
  436. Optional<CSS::AlignItems> StyleProperties::align_items() const
  437. {
  438. auto value = property(CSS::PropertyID::AlignItems);
  439. return value_id_to_align_items(value->to_identifier());
  440. }
  441. Optional<CSS::AlignSelf> StyleProperties::align_self() const
  442. {
  443. auto value = property(CSS::PropertyID::AlignSelf);
  444. return value_id_to_align_self(value->to_identifier());
  445. }
  446. Optional<CSS::Appearance> StyleProperties::appearance() const
  447. {
  448. auto value = property(CSS::PropertyID::Appearance);
  449. auto appearance = value_id_to_appearance(value->to_identifier());
  450. if (appearance.has_value()) {
  451. switch (*appearance) {
  452. // Note: All these compatibility values can be treated as 'auto'
  453. case CSS::Appearance::Textfield:
  454. case CSS::Appearance::MenulistButton:
  455. case CSS::Appearance::Searchfield:
  456. case CSS::Appearance::Textarea:
  457. case CSS::Appearance::PushButton:
  458. case CSS::Appearance::SliderHorizontal:
  459. case CSS::Appearance::Checkbox:
  460. case CSS::Appearance::Radio:
  461. case CSS::Appearance::SquareButton:
  462. case CSS::Appearance::Menulist:
  463. case CSS::Appearance::Listbox:
  464. case CSS::Appearance::Meter:
  465. case CSS::Appearance::ProgressBar:
  466. case CSS::Appearance::Button:
  467. appearance = CSS::Appearance::Auto;
  468. break;
  469. default:
  470. break;
  471. }
  472. }
  473. return appearance;
  474. }
  475. CSS::BackdropFilter StyleProperties::backdrop_filter() const
  476. {
  477. auto value = property(CSS::PropertyID::BackdropFilter);
  478. if (value->is_filter_value_list())
  479. return BackdropFilter(value->as_filter_value_list());
  480. return BackdropFilter::make_none();
  481. }
  482. Optional<CSS::Position> StyleProperties::position() const
  483. {
  484. auto value = property(CSS::PropertyID::Position);
  485. return value_id_to_position(value->to_identifier());
  486. }
  487. bool StyleProperties::operator==(StyleProperties const& other) const
  488. {
  489. if (m_property_values.size() != other.m_property_values.size())
  490. return false;
  491. for (size_t i = 0; i < m_property_values.size(); ++i) {
  492. auto const& my_style = m_property_values[i];
  493. auto const& other_style = other.m_property_values[i];
  494. if (!my_style.has_value()) {
  495. if (other_style.has_value())
  496. return false;
  497. continue;
  498. }
  499. if (!other_style.has_value())
  500. return false;
  501. auto const& my_value = *my_style->style;
  502. auto const& other_value = *other_style->style;
  503. if (my_value.type() != other_value.type())
  504. return false;
  505. if (my_value != other_value)
  506. return false;
  507. }
  508. return true;
  509. }
  510. Optional<CSS::TextAnchor> StyleProperties::text_anchor() const
  511. {
  512. auto value = property(CSS::PropertyID::TextAnchor);
  513. return value_id_to_text_anchor(value->to_identifier());
  514. }
  515. Optional<CSS::TextAlign> StyleProperties::text_align() const
  516. {
  517. auto value = property(CSS::PropertyID::TextAlign);
  518. return value_id_to_text_align(value->to_identifier());
  519. }
  520. Optional<CSS::TextJustify> StyleProperties::text_justify() const
  521. {
  522. auto value = property(CSS::PropertyID::TextJustify);
  523. return value_id_to_text_justify(value->to_identifier());
  524. }
  525. Optional<CSS::PointerEvents> StyleProperties::pointer_events() const
  526. {
  527. auto value = property(CSS::PropertyID::PointerEvents);
  528. return value_id_to_pointer_events(value->to_identifier());
  529. }
  530. Optional<CSS::WhiteSpace> StyleProperties::white_space() const
  531. {
  532. auto value = property(CSS::PropertyID::WhiteSpace);
  533. return value_id_to_white_space(value->to_identifier());
  534. }
  535. Optional<CSS::LineStyle> StyleProperties::line_style(CSS::PropertyID property_id) const
  536. {
  537. auto value = property(property_id);
  538. return value_id_to_line_style(value->to_identifier());
  539. }
  540. Optional<CSS::OutlineStyle> StyleProperties::outline_style() const
  541. {
  542. auto value = property(CSS::PropertyID::OutlineStyle);
  543. return value_id_to_outline_style(value->to_identifier());
  544. }
  545. Optional<CSS::Float> StyleProperties::float_() const
  546. {
  547. auto value = property(CSS::PropertyID::Float);
  548. return value_id_to_float(value->to_identifier());
  549. }
  550. Optional<CSS::Clear> StyleProperties::clear() const
  551. {
  552. auto value = property(CSS::PropertyID::Clear);
  553. return value_id_to_clear(value->to_identifier());
  554. }
  555. CSS::ContentData StyleProperties::content() const
  556. {
  557. auto value = property(CSS::PropertyID::Content);
  558. auto quotes_data = quotes();
  559. auto get_quote_string = [&](bool open, auto depth) {
  560. switch (quotes_data.type) {
  561. case QuotesData::Type::None:
  562. return String {};
  563. case QuotesData::Type::Auto:
  564. // FIXME: "A typographically appropriate used value for quotes is automatically chosen by the UA
  565. // based on the content language of the element and/or its parent."
  566. if (open)
  567. return depth % 2 ? "“"_string : "‘"_string;
  568. return depth % 2 ? "”"_string : "’"_string;
  569. case QuotesData::Type::Specified:
  570. auto& level = quotes_data.strings[depth % quotes_data.strings.size()];
  571. return open ? level[0] : level[1];
  572. }
  573. VERIFY_NOT_REACHED();
  574. };
  575. if (value->is_content()) {
  576. auto& content_style_value = value->as_content();
  577. CSS::ContentData content_data;
  578. // FIXME: The content is a list of things: strings, identifiers or functions that return strings, and images.
  579. // So it can't always be represented as a single String, but may have to be multiple boxes.
  580. // For now, we'll just assume strings since that is easiest.
  581. StringBuilder builder;
  582. for (auto const& item : content_style_value.content().values()) {
  583. if (item->is_string()) {
  584. builder.append(item->as_string().string_value());
  585. } else if (item->is_identifier()) {
  586. switch (item->to_identifier()) {
  587. case ValueID::OpenQuote:
  588. // FIXME: Track nesting level and increment it here.
  589. builder.append(get_quote_string(true, 1));
  590. break;
  591. case ValueID::CloseQuote:
  592. // FIXME: Track nesting level and decrement it here.
  593. builder.append(get_quote_string(false, 1));
  594. break;
  595. case ValueID::NoOpenQuote:
  596. // FIXME: Track nesting level and increment it here.
  597. break;
  598. case ValueID::NoCloseQuote:
  599. // FIXME: Track nesting level and decrement it here.
  600. break;
  601. default:
  602. dbgln("`{}` is not supported in `content` (yet?)", item->to_string());
  603. break;
  604. }
  605. } else {
  606. // TODO: Implement counters, images, and other things.
  607. dbgln("`{}` is not supported in `content` (yet?)", item->to_string());
  608. }
  609. }
  610. content_data.type = ContentData::Type::String;
  611. content_data.data = MUST(builder.to_string());
  612. if (content_style_value.has_alt_text()) {
  613. StringBuilder alt_text_builder;
  614. for (auto const& item : content_style_value.alt_text()->values()) {
  615. if (item->is_string()) {
  616. alt_text_builder.append(item->as_string().string_value());
  617. } else {
  618. // TODO: Implement counters
  619. }
  620. }
  621. content_data.alt_text = MUST(alt_text_builder.to_string());
  622. }
  623. return content_data;
  624. }
  625. switch (value->to_identifier()) {
  626. case ValueID::None:
  627. return { ContentData::Type::None };
  628. case ValueID::Normal:
  629. return { ContentData::Type::Normal };
  630. default:
  631. break;
  632. }
  633. return CSS::ContentData {};
  634. }
  635. Optional<CSS::Cursor> StyleProperties::cursor() const
  636. {
  637. auto value = property(CSS::PropertyID::Cursor);
  638. return value_id_to_cursor(value->to_identifier());
  639. }
  640. Optional<CSS::Visibility> StyleProperties::visibility() const
  641. {
  642. auto value = property(CSS::PropertyID::Visibility);
  643. if (!value->is_identifier())
  644. return {};
  645. return value_id_to_visibility(value->to_identifier());
  646. }
  647. Display StyleProperties::display() const
  648. {
  649. auto value = property(PropertyID::Display);
  650. if (value->is_display()) {
  651. return value->as_display().display();
  652. }
  653. return Display::from_short(Display::Short::Inline);
  654. }
  655. Vector<CSS::TextDecorationLine> StyleProperties::text_decoration_line() const
  656. {
  657. auto value = property(CSS::PropertyID::TextDecorationLine);
  658. if (value->is_value_list()) {
  659. Vector<CSS::TextDecorationLine> lines;
  660. auto& values = value->as_value_list().values();
  661. for (auto const& item : values) {
  662. lines.append(value_id_to_text_decoration_line(item->to_identifier()).value());
  663. }
  664. return lines;
  665. }
  666. if (value->is_identifier() && value->to_identifier() == ValueID::None)
  667. return {};
  668. dbgln("FIXME: Unsupported value for text-decoration-line: {}", value->to_string());
  669. return {};
  670. }
  671. Optional<CSS::TextDecorationStyle> StyleProperties::text_decoration_style() const
  672. {
  673. auto value = property(CSS::PropertyID::TextDecorationStyle);
  674. return value_id_to_text_decoration_style(value->to_identifier());
  675. }
  676. Optional<CSS::TextTransform> StyleProperties::text_transform() const
  677. {
  678. auto value = property(CSS::PropertyID::TextTransform);
  679. return value_id_to_text_transform(value->to_identifier());
  680. }
  681. Optional<CSS::ListStyleType> StyleProperties::list_style_type() const
  682. {
  683. auto value = property(CSS::PropertyID::ListStyleType);
  684. return value_id_to_list_style_type(value->to_identifier());
  685. }
  686. Optional<CSS::ListStylePosition> StyleProperties::list_style_position() const
  687. {
  688. auto value = property(CSS::PropertyID::ListStylePosition);
  689. return value_id_to_list_style_position(value->to_identifier());
  690. }
  691. Optional<CSS::Overflow> StyleProperties::overflow_x() const
  692. {
  693. return overflow(CSS::PropertyID::OverflowX);
  694. }
  695. Optional<CSS::Overflow> StyleProperties::overflow_y() const
  696. {
  697. return overflow(CSS::PropertyID::OverflowY);
  698. }
  699. Optional<CSS::Overflow> StyleProperties::overflow(CSS::PropertyID property_id) const
  700. {
  701. auto value = property(property_id);
  702. return value_id_to_overflow(value->to_identifier());
  703. }
  704. Vector<ShadowData> StyleProperties::shadow(PropertyID property_id, Layout::Node const& layout_node) const
  705. {
  706. auto value = property(property_id);
  707. auto resolve_to_length = [&layout_node](NonnullRefPtr<StyleValue const> const& value) -> Optional<Length> {
  708. if (value->is_length())
  709. return value->as_length().length();
  710. if (value->is_calculated())
  711. return value->as_calculated().resolve_length(layout_node);
  712. return {};
  713. };
  714. auto make_shadow_data = [resolve_to_length](ShadowStyleValue const& value) -> Optional<ShadowData> {
  715. auto maybe_offset_x = resolve_to_length(value.offset_x());
  716. if (!maybe_offset_x.has_value())
  717. return {};
  718. auto maybe_offset_y = resolve_to_length(value.offset_y());
  719. if (!maybe_offset_y.has_value())
  720. return {};
  721. auto maybe_blur_radius = resolve_to_length(value.blur_radius());
  722. if (!maybe_blur_radius.has_value())
  723. return {};
  724. auto maybe_spread_distance = resolve_to_length(value.spread_distance());
  725. if (!maybe_spread_distance.has_value())
  726. return {};
  727. return ShadowData {
  728. value.color(),
  729. maybe_offset_x.release_value(),
  730. maybe_offset_y.release_value(),
  731. maybe_blur_radius.release_value(),
  732. maybe_spread_distance.release_value(),
  733. value.placement()
  734. };
  735. };
  736. if (value->is_value_list()) {
  737. auto const& value_list = value->as_value_list();
  738. Vector<ShadowData> shadow_data;
  739. shadow_data.ensure_capacity(value_list.size());
  740. for (auto const& layer_value : value_list.values()) {
  741. auto maybe_shadow_data = make_shadow_data(layer_value->as_shadow());
  742. if (!maybe_shadow_data.has_value())
  743. return {};
  744. shadow_data.append(maybe_shadow_data.release_value());
  745. }
  746. return shadow_data;
  747. }
  748. if (value->is_shadow()) {
  749. auto maybe_shadow_data = make_shadow_data(value->as_shadow());
  750. if (!maybe_shadow_data.has_value())
  751. return {};
  752. return { maybe_shadow_data.release_value() };
  753. }
  754. return {};
  755. }
  756. Vector<ShadowData> StyleProperties::box_shadow(Layout::Node const& layout_node) const
  757. {
  758. return shadow(PropertyID::BoxShadow, layout_node);
  759. }
  760. Vector<ShadowData> StyleProperties::text_shadow(Layout::Node const& layout_node) const
  761. {
  762. return shadow(PropertyID::TextShadow, layout_node);
  763. }
  764. Optional<CSS::BoxSizing> StyleProperties::box_sizing() const
  765. {
  766. auto value = property(CSS::PropertyID::BoxSizing);
  767. return value_id_to_box_sizing(value->to_identifier());
  768. }
  769. Variant<CSS::VerticalAlign, CSS::LengthPercentage> StyleProperties::vertical_align() const
  770. {
  771. auto value = property(CSS::PropertyID::VerticalAlign);
  772. if (value->is_identifier())
  773. return value_id_to_vertical_align(value->to_identifier()).release_value();
  774. if (value->is_length())
  775. return CSS::LengthPercentage(value->as_length().length());
  776. if (value->is_percentage())
  777. return CSS::LengthPercentage(value->as_percentage().percentage());
  778. if (value->is_calculated())
  779. return LengthPercentage { const_cast<CalculatedStyleValue&>(value->as_calculated()) };
  780. VERIFY_NOT_REACHED();
  781. }
  782. Optional<CSS::FontVariant> StyleProperties::font_variant() const
  783. {
  784. auto value = property(CSS::PropertyID::FontVariant);
  785. return value_id_to_font_variant(value->to_identifier());
  786. }
  787. CSS::GridTrackSizeList StyleProperties::grid_auto_columns() const
  788. {
  789. auto value = property(CSS::PropertyID::GridAutoColumns);
  790. return value->as_grid_track_size_list().grid_track_size_list();
  791. }
  792. CSS::GridTrackSizeList StyleProperties::grid_auto_rows() const
  793. {
  794. auto value = property(CSS::PropertyID::GridAutoRows);
  795. return value->as_grid_track_size_list().grid_track_size_list();
  796. }
  797. CSS::GridTrackSizeList StyleProperties::grid_template_columns() const
  798. {
  799. auto value = property(CSS::PropertyID::GridTemplateColumns);
  800. return value->as_grid_track_size_list().grid_track_size_list();
  801. }
  802. CSS::GridTrackSizeList StyleProperties::grid_template_rows() const
  803. {
  804. auto value = property(CSS::PropertyID::GridTemplateRows);
  805. return value->as_grid_track_size_list().grid_track_size_list();
  806. }
  807. CSS::GridAutoFlow StyleProperties::grid_auto_flow() const
  808. {
  809. auto value = property(CSS::PropertyID::GridAutoFlow);
  810. if (!value->is_grid_auto_flow())
  811. return CSS::GridAutoFlow {};
  812. auto& grid_auto_flow_value = value->as_grid_auto_flow();
  813. return CSS::GridAutoFlow { .row = grid_auto_flow_value.is_row(), .dense = grid_auto_flow_value.is_dense() };
  814. }
  815. CSS::GridTrackPlacement StyleProperties::grid_column_end() const
  816. {
  817. auto value = property(CSS::PropertyID::GridColumnEnd);
  818. return value->as_grid_track_placement().grid_track_placement();
  819. }
  820. CSS::GridTrackPlacement StyleProperties::grid_column_start() const
  821. {
  822. auto value = property(CSS::PropertyID::GridColumnStart);
  823. return value->as_grid_track_placement().grid_track_placement();
  824. }
  825. CSS::GridTrackPlacement StyleProperties::grid_row_end() const
  826. {
  827. auto value = property(CSS::PropertyID::GridRowEnd);
  828. return value->as_grid_track_placement().grid_track_placement();
  829. }
  830. CSS::GridTrackPlacement StyleProperties::grid_row_start() const
  831. {
  832. auto value = property(CSS::PropertyID::GridRowStart);
  833. return value->as_grid_track_placement().grid_track_placement();
  834. }
  835. Optional<CSS::BorderCollapse> StyleProperties::border_collapse() const
  836. {
  837. auto value = property(CSS::PropertyID::BorderCollapse);
  838. return value_id_to_border_collapse(value->to_identifier());
  839. }
  840. Vector<Vector<String>> StyleProperties::grid_template_areas() const
  841. {
  842. auto value = property(CSS::PropertyID::GridTemplateAreas);
  843. return value->as_grid_template_area().grid_template_area();
  844. }
  845. String StyleProperties::grid_area() const
  846. {
  847. auto value = property(CSS::PropertyID::GridArea);
  848. return value->as_string().string_value();
  849. }
  850. Optional<CSS::ObjectFit> StyleProperties::object_fit() const
  851. {
  852. auto value = property(CSS::PropertyID::ObjectFit);
  853. return value_id_to_object_fit(value->to_identifier());
  854. }
  855. Optional<CSS::TableLayout> StyleProperties::table_layout() const
  856. {
  857. auto value = property(CSS::PropertyID::TableLayout);
  858. return value_id_to_table_layout(value->to_identifier());
  859. }
  860. Color StyleProperties::stop_color() const
  861. {
  862. auto value = property(CSS::PropertyID::StopColor);
  863. if (value->is_identifier()) {
  864. // Workaround lack of layout node to resolve current color.
  865. auto& ident = value->as_identifier();
  866. if (ident.id() == CSS::ValueID::Currentcolor)
  867. value = property(CSS::PropertyID::Color);
  868. }
  869. if (value->has_color()) {
  870. // FIXME: This is used by the SVGStopElement, which does not participate in layout,
  871. // so can't pass a layout node (so can't resolve some colors, e.g. palette ones)
  872. return value->to_color({});
  873. }
  874. return Color::Black;
  875. }
  876. void StyleProperties::set_math_depth(int math_depth)
  877. {
  878. m_math_depth = math_depth;
  879. // Make our children inherit our computed value, not our specified value.
  880. set_property(PropertyID::MathDepth, MathDepthStyleValue::create_integer(IntegerStyleValue::create(math_depth)));
  881. }
  882. QuotesData StyleProperties::quotes() const
  883. {
  884. auto value = property(CSS::PropertyID::Quotes);
  885. if (value->is_identifier()) {
  886. switch (value->to_identifier()) {
  887. case ValueID::Auto:
  888. return QuotesData { .type = QuotesData::Type::Auto };
  889. case ValueID::None:
  890. return QuotesData { .type = QuotesData::Type::None };
  891. default:
  892. break;
  893. }
  894. }
  895. if (value->is_value_list()) {
  896. auto& value_list = value->as_value_list();
  897. QuotesData quotes_data { .type = QuotesData::Type::Specified };
  898. VERIFY(value_list.size() % 2 == 0);
  899. for (auto i = 0u; i < value_list.size(); i += 2) {
  900. quotes_data.strings.empend(
  901. value_list.value_at(i, false)->as_string().string_value(),
  902. value_list.value_at(i + 1, false)->as_string().string_value());
  903. }
  904. return quotes_data;
  905. }
  906. return InitialValues::quotes();
  907. }
  908. }