StyleProperties.cpp 40 KB

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