ResolvedCSSStyleDeclaration.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  1. /*
  2. * Copyright (c) 2021-2022, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Debug.h>
  8. #include <AK/NonnullRefPtr.h>
  9. #include <LibWeb/CSS/ResolvedCSSStyleDeclaration.h>
  10. #include <LibWeb/CSS/StyleComputer.h>
  11. #include <LibWeb/DOM/Document.h>
  12. #include <LibWeb/DOM/Element.h>
  13. namespace Web::CSS {
  14. ResolvedCSSStyleDeclaration::ResolvedCSSStyleDeclaration(DOM::Element& element)
  15. : m_element(element)
  16. {
  17. }
  18. ResolvedCSSStyleDeclaration::~ResolvedCSSStyleDeclaration()
  19. {
  20. }
  21. size_t ResolvedCSSStyleDeclaration::length() const
  22. {
  23. return 0;
  24. }
  25. String ResolvedCSSStyleDeclaration::item(size_t index) const
  26. {
  27. (void)index;
  28. return {};
  29. }
  30. static CSS::ValueID to_css_value_id(CSS::BoxSizing value)
  31. {
  32. switch (value) {
  33. case CSS::BoxSizing::BorderBox:
  34. return CSS::ValueID::BorderBox;
  35. case CSS::BoxSizing::ContentBox:
  36. return CSS::ValueID::ContentBox;
  37. }
  38. VERIFY_NOT_REACHED();
  39. }
  40. static RefPtr<StyleValue> style_value_for_display(CSS::Display display)
  41. {
  42. if (display.is_none())
  43. return IdentifierStyleValue::create(CSS::ValueID::None);
  44. if (display.it_outside_and_inside()) {
  45. NonnullRefPtrVector<StyleValue> values;
  46. switch (display.outside()) {
  47. case CSS::Display::Outside::Inline:
  48. values.append(IdentifierStyleValue::create(CSS::ValueID::Inline));
  49. break;
  50. case CSS::Display::Outside::Block:
  51. values.append(IdentifierStyleValue::create(CSS::ValueID::Block));
  52. break;
  53. case CSS::Display::Outside::RunIn:
  54. values.append(IdentifierStyleValue::create(CSS::ValueID::RunIn));
  55. break;
  56. }
  57. switch (display.inside()) {
  58. case CSS::Display::Inside::Flow:
  59. values.append(IdentifierStyleValue::create(CSS::ValueID::Flow));
  60. break;
  61. case CSS::Display::Inside::FlowRoot:
  62. values.append(IdentifierStyleValue::create(CSS::ValueID::FlowRoot));
  63. break;
  64. case CSS::Display::Inside::Table:
  65. values.append(IdentifierStyleValue::create(CSS::ValueID::Table));
  66. break;
  67. case CSS::Display::Inside::Flex:
  68. values.append(IdentifierStyleValue::create(CSS::ValueID::Flex));
  69. break;
  70. case CSS::Display::Inside::Grid:
  71. values.append(IdentifierStyleValue::create(CSS::ValueID::Grid));
  72. break;
  73. case CSS::Display::Inside::Ruby:
  74. values.append(IdentifierStyleValue::create(CSS::ValueID::Ruby));
  75. break;
  76. }
  77. return StyleValueList::create(move(values), StyleValueList::Separator::Space);
  78. }
  79. if (display.is_internal()) {
  80. switch (display.internal()) {
  81. case CSS::Display::Internal::TableRowGroup:
  82. return IdentifierStyleValue::create(CSS::ValueID::TableRowGroup);
  83. case CSS::Display::Internal::TableHeaderGroup:
  84. return IdentifierStyleValue::create(CSS::ValueID::TableHeaderGroup);
  85. case CSS::Display::Internal::TableFooterGroup:
  86. return IdentifierStyleValue::create(CSS::ValueID::TableFooterGroup);
  87. case CSS::Display::Internal::TableRow:
  88. return IdentifierStyleValue::create(CSS::ValueID::TableRow);
  89. case CSS::Display::Internal::TableCell:
  90. return IdentifierStyleValue::create(CSS::ValueID::TableCell);
  91. case CSS::Display::Internal::TableColumnGroup:
  92. return IdentifierStyleValue::create(CSS::ValueID::TableColumnGroup);
  93. case CSS::Display::Internal::TableColumn:
  94. return IdentifierStyleValue::create(CSS::ValueID::TableColumn);
  95. case CSS::Display::Internal::TableCaption:
  96. return IdentifierStyleValue::create(CSS::ValueID::TableCaption);
  97. case CSS::Display::Internal::RubyBase:
  98. return IdentifierStyleValue::create(CSS::ValueID::RubyBase);
  99. case CSS::Display::Internal::RubyText:
  100. return IdentifierStyleValue::create(CSS::ValueID::RubyText);
  101. case CSS::Display::Internal::RubyBaseContainer:
  102. return IdentifierStyleValue::create(CSS::ValueID::RubyBaseContainer);
  103. case CSS::Display::Internal::RubyTextContainer:
  104. return IdentifierStyleValue::create(CSS::ValueID::RubyTextContainer);
  105. }
  106. }
  107. TODO();
  108. }
  109. static CSS::ValueID to_css_value_id(CSS::Float value)
  110. {
  111. switch (value) {
  112. case Float::None:
  113. return CSS::ValueID::None;
  114. case Float::Left:
  115. return CSS::ValueID::Left;
  116. case Float::Right:
  117. return CSS::ValueID::Right;
  118. }
  119. VERIFY_NOT_REACHED();
  120. }
  121. static CSS::ValueID to_css_value_id(CSS::Clear value)
  122. {
  123. switch (value) {
  124. case Clear::None:
  125. return CSS::ValueID::None;
  126. case Clear::Left:
  127. return CSS::ValueID::Left;
  128. case Clear::Right:
  129. return CSS::ValueID::Right;
  130. case Clear::Both:
  131. return CSS::ValueID::Both;
  132. }
  133. VERIFY_NOT_REACHED();
  134. }
  135. static CSS::ValueID to_css_value_id(CSS::TextDecorationLine value)
  136. {
  137. switch (value) {
  138. case TextDecorationLine::None:
  139. return CSS::ValueID::None;
  140. case TextDecorationLine::Underline:
  141. return CSS::ValueID::Underline;
  142. case TextDecorationLine::Overline:
  143. return CSS::ValueID::Overline;
  144. case TextDecorationLine::LineThrough:
  145. return CSS::ValueID::LineThrough;
  146. case TextDecorationLine::Blink:
  147. return CSS::ValueID::Blink;
  148. }
  149. VERIFY_NOT_REACHED();
  150. }
  151. static CSS::ValueID to_css_value_id(CSS::TextDecorationStyle value)
  152. {
  153. switch (value) {
  154. case TextDecorationStyle::Solid:
  155. return CSS::ValueID::Solid;
  156. case TextDecorationStyle::Double:
  157. return CSS::ValueID::Double;
  158. case TextDecorationStyle::Dotted:
  159. return CSS::ValueID::Dotted;
  160. case TextDecorationStyle::Dashed:
  161. return CSS::ValueID::Dashed;
  162. case TextDecorationStyle::Wavy:
  163. return CSS::ValueID::Wavy;
  164. }
  165. VERIFY_NOT_REACHED();
  166. }
  167. static CSS::ValueID to_css_value_id(CSS::Cursor value)
  168. {
  169. switch (value) {
  170. case Cursor::Auto:
  171. return CSS::ValueID::Auto;
  172. case Cursor::Default:
  173. return CSS::ValueID::Default;
  174. case Cursor::None:
  175. return CSS::ValueID::None;
  176. case Cursor::ContextMenu:
  177. return CSS::ValueID::ContextMenu;
  178. case Cursor::Help:
  179. return CSS::ValueID::Help;
  180. case Cursor::Pointer:
  181. return CSS::ValueID::Pointer;
  182. case Cursor::Progress:
  183. return CSS::ValueID::Progress;
  184. case Cursor::Wait:
  185. return CSS::ValueID::Wait;
  186. case Cursor::Cell:
  187. return CSS::ValueID::Cell;
  188. case Cursor::Crosshair:
  189. return CSS::ValueID::Crosshair;
  190. case Cursor::Text:
  191. return CSS::ValueID::Text;
  192. case Cursor::VerticalText:
  193. return CSS::ValueID::VerticalText;
  194. case Cursor::Alias:
  195. return CSS::ValueID::Alias;
  196. case Cursor::Copy:
  197. return CSS::ValueID::Copy;
  198. case Cursor::Move:
  199. return CSS::ValueID::Move;
  200. case Cursor::NoDrop:
  201. return CSS::ValueID::NoDrop;
  202. case Cursor::NotAllowed:
  203. return CSS::ValueID::NotAllowed;
  204. case Cursor::Grab:
  205. return CSS::ValueID::Grab;
  206. case Cursor::Grabbing:
  207. return CSS::ValueID::Grabbing;
  208. case Cursor::EResize:
  209. return CSS::ValueID::EResize;
  210. case Cursor::NResize:
  211. return CSS::ValueID::NResize;
  212. case Cursor::NeResize:
  213. return CSS::ValueID::NeResize;
  214. case Cursor::NwResize:
  215. return CSS::ValueID::NwResize;
  216. case Cursor::SResize:
  217. return CSS::ValueID::SResize;
  218. case Cursor::SeResize:
  219. return CSS::ValueID::SeResize;
  220. case Cursor::SwResize:
  221. return CSS::ValueID::SwResize;
  222. case Cursor::WResize:
  223. return CSS::ValueID::WResize;
  224. case Cursor::EwResize:
  225. return CSS::ValueID::EwResize;
  226. case Cursor::NsResize:
  227. return CSS::ValueID::NsResize;
  228. case Cursor::NeswResize:
  229. return CSS::ValueID::NeswResize;
  230. case Cursor::NwseResize:
  231. return CSS::ValueID::NwseResize;
  232. case Cursor::ColResize:
  233. return CSS::ValueID::ColResize;
  234. case Cursor::RowResize:
  235. return CSS::ValueID::RowResize;
  236. case Cursor::AllScroll:
  237. return CSS::ValueID::AllScroll;
  238. case Cursor::ZoomIn:
  239. return CSS::ValueID::ZoomIn;
  240. case Cursor::ZoomOut:
  241. return CSS::ValueID::ZoomOut;
  242. }
  243. VERIFY_NOT_REACHED();
  244. }
  245. static CSS::ValueID to_css_value_id(CSS::TextAlign value)
  246. {
  247. switch (value) {
  248. case TextAlign::Left:
  249. return CSS::ValueID::Left;
  250. case TextAlign::Center:
  251. return CSS::ValueID::Center;
  252. case TextAlign::Right:
  253. return CSS::ValueID::Right;
  254. case TextAlign::Justify:
  255. return CSS::ValueID::Justify;
  256. case TextAlign::LibwebCenter:
  257. return CSS::ValueID::LibwebCenter;
  258. }
  259. VERIFY_NOT_REACHED();
  260. }
  261. static CSS::ValueID to_css_value_id(CSS::TextTransform value)
  262. {
  263. switch (value) {
  264. case TextTransform::None:
  265. return CSS::ValueID::None;
  266. case TextTransform::Capitalize:
  267. return CSS::ValueID::Capitalize;
  268. case TextTransform::Uppercase:
  269. return CSS::ValueID::Uppercase;
  270. case TextTransform::Lowercase:
  271. return CSS::ValueID::Lowercase;
  272. case TextTransform::FullWidth:
  273. return CSS::ValueID::FullWidth;
  274. case TextTransform::FullSizeKana:
  275. return CSS::ValueID::FullSizeKana;
  276. }
  277. VERIFY_NOT_REACHED();
  278. }
  279. static CSS::ValueID to_css_value_id(CSS::Position value)
  280. {
  281. switch (value) {
  282. case Position::Static:
  283. return CSS::ValueID::Static;
  284. case Position::Relative:
  285. return CSS::ValueID::Relative;
  286. case Position::Absolute:
  287. return CSS::ValueID::Absolute;
  288. case Position::Fixed:
  289. return CSS::ValueID::Fixed;
  290. case Position::Sticky:
  291. return CSS::ValueID::Sticky;
  292. }
  293. VERIFY_NOT_REACHED();
  294. }
  295. static CSS::ValueID to_css_value_id(CSS::WhiteSpace value)
  296. {
  297. switch (value) {
  298. case WhiteSpace::Normal:
  299. return CSS::ValueID::Normal;
  300. case WhiteSpace::Pre:
  301. return CSS::ValueID::Pre;
  302. case WhiteSpace::Nowrap:
  303. return CSS::ValueID::Nowrap;
  304. case WhiteSpace::PreLine:
  305. return CSS::ValueID::PreLine;
  306. case WhiteSpace::PreWrap:
  307. return CSS::ValueID::PreWrap;
  308. }
  309. VERIFY_NOT_REACHED();
  310. }
  311. static CSS::ValueID to_css_value_id(CSS::FlexDirection value)
  312. {
  313. switch (value) {
  314. case FlexDirection::Row:
  315. return CSS::ValueID::Row;
  316. case FlexDirection::RowReverse:
  317. return CSS::ValueID::RowReverse;
  318. case FlexDirection::Column:
  319. return CSS::ValueID::Column;
  320. case FlexDirection::ColumnReverse:
  321. return CSS::ValueID::ColumnReverse;
  322. }
  323. VERIFY_NOT_REACHED();
  324. }
  325. static CSS::ValueID to_css_value_id(CSS::FlexWrap value)
  326. {
  327. switch (value) {
  328. case FlexWrap::Nowrap:
  329. return CSS::ValueID::Nowrap;
  330. case FlexWrap::Wrap:
  331. return CSS::ValueID::Wrap;
  332. case FlexWrap::WrapReverse:
  333. return CSS::ValueID::WrapReverse;
  334. }
  335. VERIFY_NOT_REACHED();
  336. }
  337. static CSS::ValueID to_css_value_id(CSS::ImageRendering value)
  338. {
  339. switch (value) {
  340. case ImageRendering::Auto:
  341. return CSS::ValueID::Auto;
  342. case ImageRendering::CrispEdges:
  343. return CSS::ValueID::CrispEdges;
  344. case ImageRendering::HighQuality:
  345. return CSS::ValueID::HighQuality;
  346. case ImageRendering::Pixelated:
  347. return CSS::ValueID::Pixelated;
  348. case ImageRendering::Smooth:
  349. return CSS::ValueID::Smooth;
  350. }
  351. VERIFY_NOT_REACHED();
  352. }
  353. static CSS::ValueID to_css_value_id(CSS::JustifyContent value)
  354. {
  355. switch (value) {
  356. case JustifyContent::FlexStart:
  357. return CSS::ValueID::FlexStart;
  358. case JustifyContent::FlexEnd:
  359. return CSS::ValueID::FlexEnd;
  360. case JustifyContent::Center:
  361. return CSS::ValueID::Center;
  362. case JustifyContent::SpaceBetween:
  363. return CSS::ValueID::SpaceBetween;
  364. case JustifyContent::SpaceAround:
  365. return CSS::ValueID::SpaceAround;
  366. }
  367. VERIFY_NOT_REACHED();
  368. }
  369. static CSS::ValueID to_css_value_id(CSS::Overflow value)
  370. {
  371. switch (value) {
  372. case Overflow::Auto:
  373. return CSS::ValueID::Auto;
  374. case Overflow::Clip:
  375. return CSS::ValueID::Clip;
  376. case Overflow::Hidden:
  377. return CSS::ValueID::Hidden;
  378. case Overflow::Scroll:
  379. return CSS::ValueID::Scroll;
  380. case Overflow::Visible:
  381. return CSS::ValueID::Visible;
  382. }
  383. VERIFY_NOT_REACHED();
  384. }
  385. static CSS::ValueID to_css_value_id(CSS::VerticalAlign value)
  386. {
  387. switch (value) {
  388. case CSS::VerticalAlign::Baseline:
  389. return CSS::ValueID::Baseline;
  390. case CSS::VerticalAlign::Bottom:
  391. return CSS::ValueID::Bottom;
  392. case CSS::VerticalAlign::Middle:
  393. return CSS::ValueID::Middle;
  394. case CSS::VerticalAlign::Sub:
  395. return CSS::ValueID::Sub;
  396. case CSS::VerticalAlign::Super:
  397. return CSS::ValueID::Super;
  398. case CSS::VerticalAlign::TextBottom:
  399. return CSS::ValueID::TextBottom;
  400. case CSS::VerticalAlign::TextTop:
  401. return CSS::ValueID::TextTop;
  402. case CSS::VerticalAlign::Top:
  403. return CSS::ValueID::Top;
  404. }
  405. VERIFY_NOT_REACHED();
  406. }
  407. static CSS::ValueID to_css_value_id(CSS::ListStyleType value)
  408. {
  409. switch (value) {
  410. case ListStyleType::None:
  411. return CSS::ValueID::None;
  412. case ListStyleType::Disc:
  413. return CSS::ValueID::Disc;
  414. case ListStyleType::Circle:
  415. return CSS::ValueID::Circle;
  416. case ListStyleType::Square:
  417. return CSS::ValueID::Square;
  418. case ListStyleType::Decimal:
  419. return CSS::ValueID::Decimal;
  420. case ListStyleType::DecimalLeadingZero:
  421. return CSS::ValueID::DecimalLeadingZero;
  422. case ListStyleType::LowerAlpha:
  423. return CSS::ValueID::LowerAlpha;
  424. case ListStyleType::LowerLatin:
  425. return CSS::ValueID::LowerLatin;
  426. case ListStyleType::LowerRoman:
  427. return CSS::ValueID::LowerRoman;
  428. case ListStyleType::UpperAlpha:
  429. return CSS::ValueID::UpperAlpha;
  430. case ListStyleType::UpperLatin:
  431. return CSS::ValueID::UpperLatin;
  432. case ListStyleType::UpperRoman:
  433. return CSS::ValueID::UpperRoman;
  434. }
  435. VERIFY_NOT_REACHED();
  436. }
  437. static CSS::ValueID to_css_value_id(CSS::LineStyle value)
  438. {
  439. switch (value) {
  440. case CSS::LineStyle::None:
  441. return CSS::ValueID::None;
  442. case CSS::LineStyle::Hidden:
  443. return CSS::ValueID::Hidden;
  444. case CSS::LineStyle::Dotted:
  445. return CSS::ValueID::Dotted;
  446. case CSS::LineStyle::Dashed:
  447. return CSS::ValueID::Dashed;
  448. case CSS::LineStyle::Solid:
  449. return CSS::ValueID::Solid;
  450. case CSS::LineStyle::Double:
  451. return CSS::ValueID::Double;
  452. case CSS::LineStyle::Groove:
  453. return CSS::ValueID::Groove;
  454. case CSS::LineStyle::Ridge:
  455. return CSS::ValueID::Ridge;
  456. case CSS::LineStyle::Inset:
  457. return CSS::ValueID::Inset;
  458. case CSS::LineStyle::Outset:
  459. return CSS::ValueID::Outset;
  460. }
  461. VERIFY_NOT_REACHED();
  462. }
  463. static NonnullRefPtr<StyleValue> value_or_default(Optional<StyleProperty> property, NonnullRefPtr<StyleValue> default_style)
  464. {
  465. if (property.has_value())
  466. return property.value().value;
  467. return default_style;
  468. }
  469. static NonnullRefPtr<StyleValue> style_value_for_length_percentage(LengthPercentage const& length_percentage)
  470. {
  471. if (length_percentage.is_percentage())
  472. return PercentageStyleValue::create(length_percentage.percentage());
  473. return LengthStyleValue::create(length_percentage.length());
  474. }
  475. RefPtr<StyleValue> ResolvedCSSStyleDeclaration::style_value_for_property(Layout::NodeWithStyle const& layout_node, PropertyID property_id) const
  476. {
  477. switch (property_id) {
  478. case CSS::PropertyID::Float:
  479. return IdentifierStyleValue::create(to_css_value_id(layout_node.computed_values().float_()));
  480. case CSS::PropertyID::Clear:
  481. return IdentifierStyleValue::create(to_css_value_id(layout_node.computed_values().clear()));
  482. case CSS::PropertyID::Cursor:
  483. return IdentifierStyleValue::create(to_css_value_id(layout_node.computed_values().cursor()));
  484. case CSS::PropertyID::Display:
  485. return style_value_for_display(layout_node.computed_values().display());
  486. case CSS::PropertyID::ZIndex: {
  487. auto maybe_z_index = layout_node.computed_values().z_index();
  488. if (!maybe_z_index.has_value())
  489. return {};
  490. return NumericStyleValue::create_integer(maybe_z_index.release_value());
  491. }
  492. case CSS::PropertyID::TextAlign:
  493. return IdentifierStyleValue::create(to_css_value_id(layout_node.computed_values().text_align()));
  494. case CSS::PropertyID::TextDecorationLine:
  495. return IdentifierStyleValue::create(to_css_value_id(layout_node.computed_values().text_decoration_line()));
  496. case CSS::PropertyID::TextDecorationStyle:
  497. return IdentifierStyleValue::create(to_css_value_id(layout_node.computed_values().text_decoration_style()));
  498. case CSS::PropertyID::TextTransform:
  499. return IdentifierStyleValue::create(to_css_value_id(layout_node.computed_values().text_transform()));
  500. case CSS::PropertyID::Position:
  501. return IdentifierStyleValue::create(to_css_value_id(layout_node.computed_values().position()));
  502. case CSS::PropertyID::WhiteSpace:
  503. return IdentifierStyleValue::create(to_css_value_id(layout_node.computed_values().white_space()));
  504. case CSS::PropertyID::FlexDirection:
  505. return IdentifierStyleValue::create(to_css_value_id(layout_node.computed_values().flex_direction()));
  506. case CSS::PropertyID::FlexWrap:
  507. return IdentifierStyleValue::create(to_css_value_id(layout_node.computed_values().flex_wrap()));
  508. case CSS::PropertyID::FlexBasis: {
  509. switch (layout_node.computed_values().flex_basis().type) {
  510. case FlexBasis::Content:
  511. return IdentifierStyleValue::create(CSS::ValueID::Content);
  512. case FlexBasis::LengthPercentage:
  513. return style_value_for_length_percentage(*layout_node.computed_values().flex_basis().length_percentage);
  514. case FlexBasis::Auto:
  515. return IdentifierStyleValue::create(CSS::ValueID::Auto);
  516. default:
  517. VERIFY_NOT_REACHED();
  518. }
  519. break;
  520. case CSS::PropertyID::FlexGrow:
  521. return NumericStyleValue::create_float(layout_node.computed_values().flex_grow());
  522. case CSS::PropertyID::FlexShrink:
  523. return NumericStyleValue::create_float(layout_node.computed_values().flex_shrink());
  524. case CSS::PropertyID::Opacity:
  525. return NumericStyleValue::create_float(layout_node.computed_values().opacity());
  526. case CSS::PropertyID::ImageRendering:
  527. return IdentifierStyleValue::create(to_css_value_id(layout_node.computed_values().image_rendering()));
  528. case CSS::PropertyID::JustifyContent:
  529. return IdentifierStyleValue::create(to_css_value_id(layout_node.computed_values().justify_content()));
  530. case CSS::PropertyID::BoxShadow: {
  531. auto box_shadow_layers = layout_node.computed_values().box_shadow();
  532. if (box_shadow_layers.is_empty())
  533. return {};
  534. auto make_box_shadow_style_value = [](BoxShadowData const& data) {
  535. return BoxShadowStyleValue::create(data.color, data.offset_x, data.offset_y, data.blur_radius, data.spread_distance, data.placement);
  536. };
  537. if (box_shadow_layers.size() == 1)
  538. return make_box_shadow_style_value(box_shadow_layers.first());
  539. NonnullRefPtrVector<StyleValue> box_shadow;
  540. box_shadow.ensure_capacity(box_shadow_layers.size());
  541. for (auto const& layer : box_shadow_layers)
  542. box_shadow.append(make_box_shadow_style_value(layer));
  543. return StyleValueList::create(move(box_shadow), StyleValueList::Separator::Comma);
  544. }
  545. case CSS::PropertyID::Width:
  546. return style_value_for_length_percentage(layout_node.computed_values().width().value_or(Length::make_auto()));
  547. case CSS::PropertyID::MinWidth:
  548. if (!layout_node.computed_values().min_width().has_value())
  549. return IdentifierStyleValue::create(CSS::ValueID::Auto);
  550. return style_value_for_length_percentage(layout_node.computed_values().min_width().value());
  551. case CSS::PropertyID::MaxWidth:
  552. if (!layout_node.computed_values().max_width().has_value())
  553. return IdentifierStyleValue::create(CSS::ValueID::None);
  554. return style_value_for_length_percentage(layout_node.computed_values().max_width().value());
  555. case CSS::PropertyID::Height:
  556. return style_value_for_length_percentage(layout_node.computed_values().height().value_or(Length::make_auto()));
  557. case CSS::PropertyID::MinHeight:
  558. if (!layout_node.computed_values().min_height().has_value())
  559. return IdentifierStyleValue::create(CSS::ValueID::Auto);
  560. return style_value_for_length_percentage(layout_node.computed_values().min_height().value());
  561. case CSS::PropertyID::MaxHeight:
  562. if (!layout_node.computed_values().max_height().has_value())
  563. return IdentifierStyleValue::create(CSS::ValueID::None);
  564. return style_value_for_length_percentage(layout_node.computed_values().max_height().value());
  565. case CSS::PropertyID::Margin: {
  566. auto margin = layout_node.computed_values().margin();
  567. auto values = NonnullRefPtrVector<StyleValue> {};
  568. values.append(style_value_for_length_percentage(margin.top));
  569. values.append(style_value_for_length_percentage(margin.right));
  570. values.append(style_value_for_length_percentage(margin.bottom));
  571. values.append(style_value_for_length_percentage(margin.left));
  572. return StyleValueList::create(move(values), StyleValueList::Separator::Space);
  573. }
  574. case CSS::PropertyID::MarginTop:
  575. return style_value_for_length_percentage(layout_node.computed_values().margin().top);
  576. case CSS::PropertyID::MarginRight:
  577. return style_value_for_length_percentage(layout_node.computed_values().margin().right);
  578. case CSS::PropertyID::MarginBottom:
  579. return style_value_for_length_percentage(layout_node.computed_values().margin().bottom);
  580. case CSS::PropertyID::MarginLeft:
  581. return style_value_for_length_percentage(layout_node.computed_values().margin().left);
  582. case CSS::PropertyID::Padding: {
  583. auto padding = layout_node.computed_values().padding();
  584. auto values = NonnullRefPtrVector<StyleValue> {};
  585. values.append(style_value_for_length_percentage(padding.top));
  586. values.append(style_value_for_length_percentage(padding.right));
  587. values.append(style_value_for_length_percentage(padding.bottom));
  588. values.append(style_value_for_length_percentage(padding.left));
  589. return StyleValueList::create(move(values), StyleValueList::Separator::Space);
  590. }
  591. case CSS::PropertyID::PaddingTop:
  592. return style_value_for_length_percentage(layout_node.computed_values().padding().top);
  593. case CSS::PropertyID::PaddingRight:
  594. return style_value_for_length_percentage(layout_node.computed_values().padding().right);
  595. case CSS::PropertyID::PaddingBottom:
  596. return style_value_for_length_percentage(layout_node.computed_values().padding().bottom);
  597. case CSS::PropertyID::PaddingLeft:
  598. return style_value_for_length_percentage(layout_node.computed_values().padding().left);
  599. case CSS::PropertyID::BorderRadius: {
  600. auto maybe_top_left_radius = property(CSS::PropertyID::BorderTopLeftRadius);
  601. auto maybe_top_right_radius = property(CSS::PropertyID::BorderTopRightRadius);
  602. auto maybe_bottom_left_radius = property(CSS::PropertyID::BorderBottomLeftRadius);
  603. auto maybe_bottom_right_radius = property(CSS::PropertyID::BorderBottomRightRadius);
  604. RefPtr<BorderRadiusStyleValue> top_left_radius, top_right_radius, bottom_left_radius, bottom_right_radius;
  605. if (maybe_top_left_radius.has_value()) {
  606. VERIFY(maybe_top_left_radius.value().value->is_border_radius());
  607. top_left_radius = maybe_top_left_radius.value().value->as_border_radius();
  608. }
  609. if (maybe_top_right_radius.has_value()) {
  610. VERIFY(maybe_top_right_radius.value().value->is_border_radius());
  611. top_right_radius = maybe_top_right_radius.value().value->as_border_radius();
  612. }
  613. if (maybe_bottom_left_radius.has_value()) {
  614. VERIFY(maybe_bottom_left_radius.value().value->is_border_radius());
  615. bottom_left_radius = maybe_bottom_left_radius.value().value->as_border_radius();
  616. }
  617. if (maybe_bottom_right_radius.has_value()) {
  618. VERIFY(maybe_bottom_right_radius.value().value->is_border_radius());
  619. bottom_right_radius = maybe_bottom_right_radius.value().value->as_border_radius();
  620. }
  621. return CombinedBorderRadiusStyleValue::create(top_left_radius.release_nonnull(), top_right_radius.release_nonnull(), bottom_right_radius.release_nonnull(), bottom_left_radius.release_nonnull());
  622. }
  623. // FIXME: The two radius components are not yet stored, as we currently don't actually render them.
  624. case CSS::PropertyID::BorderBottomLeftRadius:
  625. return BorderRadiusStyleValue::create(layout_node.computed_values().border_bottom_left_radius(), layout_node.computed_values().border_bottom_left_radius());
  626. case CSS::PropertyID::BorderBottomRightRadius:
  627. return BorderRadiusStyleValue::create(layout_node.computed_values().border_bottom_right_radius(), layout_node.computed_values().border_bottom_right_radius());
  628. case CSS::PropertyID::BorderTopLeftRadius:
  629. return BorderRadiusStyleValue::create(layout_node.computed_values().border_top_left_radius(), layout_node.computed_values().border_top_left_radius());
  630. case CSS::PropertyID::BorderTopRightRadius:
  631. return BorderRadiusStyleValue::create(layout_node.computed_values().border_top_right_radius(), layout_node.computed_values().border_top_right_radius());
  632. case CSS::PropertyID::BorderTopWidth:
  633. return LengthStyleValue::create(Length::make_px(layout_node.computed_values().border_top().width));
  634. case CSS::PropertyID::BorderRightWidth:
  635. return LengthStyleValue::create(Length::make_px(layout_node.computed_values().border_right().width));
  636. case CSS::PropertyID::BorderBottomWidth:
  637. return LengthStyleValue::create(Length::make_px(layout_node.computed_values().border_bottom().width));
  638. case CSS::PropertyID::BorderLeftWidth:
  639. return LengthStyleValue::create(Length::make_px(layout_node.computed_values().border_left().width));
  640. case CSS::PropertyID::BorderTopColor:
  641. return ColorStyleValue::create(layout_node.computed_values().border_top().color);
  642. case CSS::PropertyID::BorderRightColor:
  643. return ColorStyleValue::create(layout_node.computed_values().border_right().color);
  644. case CSS::PropertyID::BorderBottomColor:
  645. return ColorStyleValue::create(layout_node.computed_values().border_bottom().color);
  646. case CSS::PropertyID::BorderLeftColor:
  647. return ColorStyleValue::create(layout_node.computed_values().border_left().color);
  648. case CSS::PropertyID::BorderTopStyle:
  649. return IdentifierStyleValue::create(to_css_value_id(layout_node.computed_values().border_top().line_style));
  650. case CSS::PropertyID::BorderRightStyle:
  651. return IdentifierStyleValue::create(to_css_value_id(layout_node.computed_values().border_right().line_style));
  652. case CSS::PropertyID::BorderBottomStyle:
  653. return IdentifierStyleValue::create(to_css_value_id(layout_node.computed_values().border_bottom().line_style));
  654. case CSS::PropertyID::BorderLeftStyle:
  655. return IdentifierStyleValue::create(to_css_value_id(layout_node.computed_values().border_left().line_style));
  656. case CSS::PropertyID::BorderTop: {
  657. auto border = layout_node.computed_values().border_top();
  658. auto width = LengthStyleValue::create(Length::make_px(border.width));
  659. auto style = IdentifierStyleValue::create(to_css_value_id(border.line_style));
  660. auto color = ColorStyleValue::create(border.color);
  661. return BorderStyleValue::create(width, style, color);
  662. }
  663. case CSS::PropertyID::BorderRight: {
  664. auto border = layout_node.computed_values().border_right();
  665. auto width = LengthStyleValue::create(Length::make_px(border.width));
  666. auto style = IdentifierStyleValue::create(to_css_value_id(border.line_style));
  667. auto color = ColorStyleValue::create(border.color);
  668. return BorderStyleValue::create(width, style, color);
  669. }
  670. case CSS::PropertyID::BorderBottom: {
  671. auto border = layout_node.computed_values().border_bottom();
  672. auto width = LengthStyleValue::create(Length::make_px(border.width));
  673. auto style = IdentifierStyleValue::create(to_css_value_id(border.line_style));
  674. auto color = ColorStyleValue::create(border.color);
  675. return BorderStyleValue::create(width, style, color);
  676. }
  677. case CSS::PropertyID::BorderLeft: {
  678. auto border = layout_node.computed_values().border_left();
  679. auto width = LengthStyleValue::create(Length::make_px(border.width));
  680. auto style = IdentifierStyleValue::create(to_css_value_id(border.line_style));
  681. auto color = ColorStyleValue::create(border.color);
  682. return BorderStyleValue::create(width, style, color);
  683. }
  684. case CSS::PropertyID::OverflowX:
  685. return IdentifierStyleValue::create(to_css_value_id(layout_node.computed_values().overflow_x()));
  686. case CSS::PropertyID::OverflowY:
  687. return IdentifierStyleValue::create(to_css_value_id(layout_node.computed_values().overflow_y()));
  688. case CSS::PropertyID::Color:
  689. return ColorStyleValue::create(layout_node.computed_values().color());
  690. case PropertyID::BackgroundColor:
  691. return ColorStyleValue::create(layout_node.computed_values().background_color());
  692. case CSS::PropertyID::Background: {
  693. auto maybe_background_color = property(CSS::PropertyID::BackgroundColor);
  694. auto maybe_background_image = property(CSS::PropertyID::BackgroundImage);
  695. auto maybe_background_position = property(CSS::PropertyID::BackgroundPosition);
  696. auto maybe_background_size = property(CSS::PropertyID::BackgroundSize);
  697. auto maybe_background_repeat = property(CSS::PropertyID::BackgroundRepeat);
  698. auto maybe_background_attachment = property(CSS::PropertyID::BackgroundAttachment);
  699. auto maybe_background_origin = property(CSS::PropertyID::BackgroundOrigin);
  700. auto maybe_background_clip = property(CSS::PropertyID::BackgroundClip);
  701. return BackgroundStyleValue::create(
  702. value_or_default(maybe_background_color, InitialStyleValue::the()),
  703. value_or_default(maybe_background_image, IdentifierStyleValue::create(CSS::ValueID::None)),
  704. value_or_default(maybe_background_position, PositionStyleValue::create(PositionEdge::Left, Length::make_px(0), PositionEdge::Top, Length::make_px(0))),
  705. value_or_default(maybe_background_size, IdentifierStyleValue::create(CSS::ValueID::Auto)),
  706. value_or_default(maybe_background_repeat, BackgroundRepeatStyleValue::create(CSS::Repeat::Repeat, CSS::Repeat::Repeat)),
  707. value_or_default(maybe_background_attachment, IdentifierStyleValue::create(CSS::ValueID::Scroll)),
  708. value_or_default(maybe_background_origin, IdentifierStyleValue::create(CSS::ValueID::PaddingBox)),
  709. value_or_default(maybe_background_clip, IdentifierStyleValue::create(CSS::ValueID::BorderBox)));
  710. }
  711. case CSS::PropertyID::VerticalAlign:
  712. if (auto const* length_percentage = layout_node.computed_values().vertical_align().get_pointer<CSS::LengthPercentage>()) {
  713. if (length_percentage->is_length())
  714. return LengthStyleValue::create(length_percentage->length());
  715. if (length_percentage->is_percentage())
  716. return PercentageStyleValue::create(length_percentage->percentage());
  717. VERIFY_NOT_REACHED();
  718. }
  719. return IdentifierStyleValue::create(to_css_value_id(layout_node.computed_values().vertical_align().get<CSS::VerticalAlign>()));
  720. case CSS::PropertyID::ListStyleType:
  721. return IdentifierStyleValue::create(to_css_value_id(layout_node.computed_values().list_style_type()));
  722. case CSS::PropertyID::BoxSizing:
  723. return IdentifierStyleValue::create(to_css_value_id(layout_node.computed_values().box_sizing()));
  724. case CSS::PropertyID::Invalid:
  725. return IdentifierStyleValue::create(CSS::ValueID::Invalid);
  726. case CSS::PropertyID::Custom:
  727. dbgln_if(LIBWEB_CSS_DEBUG, "Computed style for custom properties was requested (?)");
  728. return {};
  729. default:
  730. dbgln_if(LIBWEB_CSS_DEBUG, "FIXME: Computed style for the '{}' property was requested", string_from_property_id(property_id));
  731. return {};
  732. }
  733. }
  734. }
  735. Optional<StyleProperty> ResolvedCSSStyleDeclaration::property(PropertyID property_id) const
  736. {
  737. // FIXME: Only update layout if required to resolve the property we're accessing.
  738. const_cast<DOM::Document&>(m_element->document()).update_layout();
  739. if (!m_element->layout_node()) {
  740. auto style = m_element->document().style_computer().compute_style(const_cast<DOM::Element&>(*m_element));
  741. if (auto maybe_property = style->property(property_id); maybe_property.has_value()) {
  742. return StyleProperty {
  743. .property_id = property_id,
  744. .value = maybe_property.release_value(),
  745. };
  746. }
  747. return {};
  748. }
  749. auto& layout_node = *m_element->layout_node();
  750. auto value = style_value_for_property(layout_node, property_id);
  751. if (!value)
  752. return {};
  753. return StyleProperty {
  754. .property_id = property_id,
  755. .value = value.release_nonnull(),
  756. };
  757. }
  758. bool ResolvedCSSStyleDeclaration::set_property(PropertyID, StringView)
  759. {
  760. return false;
  761. }
  762. String ResolvedCSSStyleDeclaration::serialized() const
  763. {
  764. // https://www.w3.org/TR/cssom/#dom-cssstyledeclaration-csstext
  765. // If the computed flag is set, then return the empty string.
  766. // NOTE: ResolvedCSSStyleDeclaration is something you would only get from window.getComputedStyle(),
  767. // which returns what the spec calls "resolved style". The "computed flag" is always set here.
  768. return String::empty();
  769. }
  770. }