StyleResolver.cpp 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, the SerenityOS developers.
  4. * Copyright (c) 2021, Sam Atkins <atkinssj@gmail.com>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <AK/QuickSort.h>
  9. #include <LibWeb/CSS/CSSStyleRule.h>
  10. #include <LibWeb/CSS/Parser/Parser.h>
  11. #include <LibWeb/CSS/SelectorEngine.h>
  12. #include <LibWeb/CSS/StyleResolver.h>
  13. #include <LibWeb/CSS/StyleSheet.h>
  14. #include <LibWeb/DOM/Document.h>
  15. #include <LibWeb/DOM/Element.h>
  16. #include <LibWeb/Dump.h>
  17. #include <ctype.h>
  18. #include <stdio.h>
  19. namespace Web::CSS {
  20. StyleResolver::StyleResolver(DOM::Document& document)
  21. : m_document(document)
  22. {
  23. }
  24. StyleResolver::~StyleResolver()
  25. {
  26. }
  27. static StyleSheet& default_stylesheet()
  28. {
  29. static StyleSheet* sheet;
  30. if (!sheet) {
  31. extern char const default_stylesheet_source[];
  32. String css = default_stylesheet_source;
  33. sheet = parse_css(CSS::ParsingContext(), css).leak_ref();
  34. }
  35. return *sheet;
  36. }
  37. static StyleSheet& quirks_mode_stylesheet()
  38. {
  39. static StyleSheet* sheet;
  40. if (!sheet) {
  41. extern char const quirks_mode_stylesheet_source[];
  42. String css = quirks_mode_stylesheet_source;
  43. sheet = parse_css(CSS::ParsingContext(), css).leak_ref();
  44. }
  45. return *sheet;
  46. }
  47. template<typename Callback>
  48. void StyleResolver::for_each_stylesheet(Callback callback) const
  49. {
  50. callback(default_stylesheet());
  51. if (document().in_quirks_mode())
  52. callback(quirks_mode_stylesheet());
  53. for (auto& sheet : document().style_sheets().sheets()) {
  54. callback(sheet);
  55. }
  56. }
  57. Vector<MatchingRule> StyleResolver::collect_matching_rules(DOM::Element const& element) const
  58. {
  59. Vector<MatchingRule> matching_rules;
  60. size_t style_sheet_index = 0;
  61. for_each_stylesheet([&](auto& sheet) {
  62. if (!is<CSSStyleSheet>(sheet))
  63. return;
  64. size_t rule_index = 0;
  65. static_cast<CSSStyleSheet const&>(sheet).for_each_effective_style_rule([&](auto& rule) {
  66. size_t selector_index = 0;
  67. for (auto& selector : rule.selectors()) {
  68. if (SelectorEngine::matches(selector, element)) {
  69. matching_rules.append({ rule, style_sheet_index, rule_index, selector_index, selector.specificity() });
  70. break;
  71. }
  72. ++selector_index;
  73. }
  74. ++rule_index;
  75. });
  76. ++style_sheet_index;
  77. });
  78. return matching_rules;
  79. }
  80. void StyleResolver::sort_matching_rules(Vector<MatchingRule>& matching_rules) const
  81. {
  82. quick_sort(matching_rules, [&](MatchingRule& a, MatchingRule& b) {
  83. auto& a_selector = a.rule->selectors()[a.selector_index];
  84. auto& b_selector = b.rule->selectors()[b.selector_index];
  85. auto a_specificity = a_selector.specificity();
  86. auto b_specificity = b_selector.specificity();
  87. if (a_selector.specificity() == b_selector.specificity()) {
  88. if (a.style_sheet_index == b.style_sheet_index)
  89. return a.rule_index < b.rule_index;
  90. return a.style_sheet_index < b.style_sheet_index;
  91. }
  92. return a_specificity < b_specificity;
  93. });
  94. }
  95. bool StyleResolver::is_inherited_property(CSS::PropertyID property_id)
  96. {
  97. static HashTable<CSS::PropertyID> inherited_properties;
  98. if (inherited_properties.is_empty()) {
  99. inherited_properties.set(CSS::PropertyID::BorderCollapse);
  100. inherited_properties.set(CSS::PropertyID::BorderSpacing);
  101. inherited_properties.set(CSS::PropertyID::Color);
  102. inherited_properties.set(CSS::PropertyID::FontFamily);
  103. inherited_properties.set(CSS::PropertyID::FontSize);
  104. inherited_properties.set(CSS::PropertyID::FontStyle);
  105. inherited_properties.set(CSS::PropertyID::FontVariant);
  106. inherited_properties.set(CSS::PropertyID::FontWeight);
  107. inherited_properties.set(CSS::PropertyID::LetterSpacing);
  108. inherited_properties.set(CSS::PropertyID::LineHeight);
  109. inherited_properties.set(CSS::PropertyID::ListStyle);
  110. inherited_properties.set(CSS::PropertyID::ListStyleImage);
  111. inherited_properties.set(CSS::PropertyID::ListStylePosition);
  112. inherited_properties.set(CSS::PropertyID::ListStyleType);
  113. inherited_properties.set(CSS::PropertyID::TextAlign);
  114. inherited_properties.set(CSS::PropertyID::TextIndent);
  115. inherited_properties.set(CSS::PropertyID::TextTransform);
  116. inherited_properties.set(CSS::PropertyID::Visibility);
  117. inherited_properties.set(CSS::PropertyID::WhiteSpace);
  118. inherited_properties.set(CSS::PropertyID::WordSpacing);
  119. // FIXME: This property is not supposed to be inherited, but we currently
  120. // rely on inheritance to propagate decorations into line boxes.
  121. inherited_properties.set(CSS::PropertyID::TextDecorationLine);
  122. }
  123. return inherited_properties.contains(property_id);
  124. }
  125. static Vector<String> split_on_whitespace(StringView const& string)
  126. {
  127. if (string.is_empty())
  128. return {};
  129. Vector<String> v;
  130. size_t substart = 0;
  131. for (size_t i = 0; i < string.length(); ++i) {
  132. char ch = string.characters_without_null_termination()[i];
  133. if (isspace(ch)) {
  134. size_t sublen = i - substart;
  135. if (sublen != 0)
  136. v.append(string.substring_view(substart, sublen));
  137. substart = i + 1;
  138. }
  139. }
  140. size_t taillen = string.length() - substart;
  141. if (taillen != 0)
  142. v.append(string.substring_view(substart, taillen));
  143. return v;
  144. }
  145. enum class Edge {
  146. Top,
  147. Right,
  148. Bottom,
  149. Left,
  150. All,
  151. };
  152. static bool contains(Edge a, Edge b)
  153. {
  154. return a == b || b == Edge::All;
  155. }
  156. static inline void set_property_border_width(StyleProperties& style, StyleValue const& value, Edge edge)
  157. {
  158. VERIFY(value.is_length());
  159. if (contains(Edge::Top, edge))
  160. style.set_property(CSS::PropertyID::BorderTopWidth, value);
  161. if (contains(Edge::Right, edge))
  162. style.set_property(CSS::PropertyID::BorderRightWidth, value);
  163. if (contains(Edge::Bottom, edge))
  164. style.set_property(CSS::PropertyID::BorderBottomWidth, value);
  165. if (contains(Edge::Left, edge))
  166. style.set_property(CSS::PropertyID::BorderLeftWidth, value);
  167. }
  168. static inline void set_property_border_color(StyleProperties& style, StyleValue const& value, Edge edge)
  169. {
  170. VERIFY(value.is_color());
  171. if (contains(Edge::Top, edge))
  172. style.set_property(CSS::PropertyID::BorderTopColor, value);
  173. if (contains(Edge::Right, edge))
  174. style.set_property(CSS::PropertyID::BorderRightColor, value);
  175. if (contains(Edge::Bottom, edge))
  176. style.set_property(CSS::PropertyID::BorderBottomColor, value);
  177. if (contains(Edge::Left, edge))
  178. style.set_property(CSS::PropertyID::BorderLeftColor, value);
  179. }
  180. static inline void set_property_border_style(StyleProperties& style, StyleValue const& value, Edge edge)
  181. {
  182. VERIFY(value.type() == CSS::StyleValue::Type::Identifier);
  183. if (contains(Edge::Top, edge))
  184. style.set_property(CSS::PropertyID::BorderTopStyle, value);
  185. if (contains(Edge::Right, edge))
  186. style.set_property(CSS::PropertyID::BorderRightStyle, value);
  187. if (contains(Edge::Bottom, edge))
  188. style.set_property(CSS::PropertyID::BorderBottomStyle, value);
  189. if (contains(Edge::Left, edge))
  190. style.set_property(CSS::PropertyID::BorderLeftStyle, value);
  191. }
  192. static inline bool is_background_repeat(StyleValue const& value)
  193. {
  194. if (value.is_builtin_or_dynamic())
  195. return true;
  196. switch (value.to_identifier()) {
  197. case CSS::ValueID::NoRepeat:
  198. case CSS::ValueID::Repeat:
  199. case CSS::ValueID::RepeatX:
  200. case CSS::ValueID::RepeatY:
  201. case CSS::ValueID::Round:
  202. case CSS::ValueID::Space:
  203. return true;
  204. default:
  205. return false;
  206. }
  207. }
  208. static inline bool is_background_image(StyleValue const& value)
  209. {
  210. if (value.is_builtin_or_dynamic())
  211. return true;
  212. if (value.is_image())
  213. return true;
  214. if (value.to_identifier() == ValueID::None)
  215. return true;
  216. return false;
  217. }
  218. static inline bool is_color(StyleValue const& value)
  219. {
  220. if (value.is_builtin_or_dynamic())
  221. return true;
  222. if (value.is_color())
  223. return true;
  224. return false;
  225. }
  226. static inline bool is_flex_direction(StyleValue const& value)
  227. {
  228. if (value.is_builtin_or_dynamic())
  229. return true;
  230. switch (value.to_identifier()) {
  231. case ValueID::Row:
  232. case ValueID::RowReverse:
  233. case ValueID::Column:
  234. case ValueID::ColumnReverse:
  235. return true;
  236. default:
  237. return false;
  238. }
  239. }
  240. static inline bool is_flex_wrap(StyleValue const& value)
  241. {
  242. if (value.is_builtin_or_dynamic())
  243. return true;
  244. switch (value.to_identifier()) {
  245. case ValueID::Wrap:
  246. case ValueID::Nowrap:
  247. case ValueID::WrapReverse:
  248. return true;
  249. default:
  250. return false;
  251. }
  252. }
  253. static inline bool is_flex_grow_or_shrink(StyleValue const& value)
  254. {
  255. if (value.is_builtin_or_dynamic())
  256. return true;
  257. if (value.is_numeric())
  258. return true;
  259. return false;
  260. }
  261. static inline bool is_flex_basis(StyleValue const& value)
  262. {
  263. if (value.is_builtin_or_dynamic())
  264. return true;
  265. if (value.is_length())
  266. return true;
  267. if (value.is_identifier() && value.to_identifier() == ValueID::Content)
  268. return true;
  269. return false;
  270. }
  271. static inline bool is_font_family(StyleValue const& value)
  272. {
  273. if (value.is_builtin_or_dynamic())
  274. return true;
  275. if (value.is_string())
  276. return true;
  277. switch (value.to_identifier()) {
  278. case ValueID::Cursive:
  279. case ValueID::Fantasy:
  280. case ValueID::Monospace:
  281. case ValueID::Serif:
  282. case ValueID::SansSerif:
  283. case ValueID::UiMonospace:
  284. case ValueID::UiRounded:
  285. case ValueID::UiSerif:
  286. case ValueID::UiSansSerif:
  287. return true;
  288. default:
  289. return false;
  290. }
  291. }
  292. static inline bool is_font_size(StyleValue const& value)
  293. {
  294. if (value.is_builtin_or_dynamic())
  295. return true;
  296. if (value.is_length())
  297. return true;
  298. switch (value.to_identifier()) {
  299. case ValueID::XxSmall:
  300. case ValueID::XSmall:
  301. case ValueID::Small:
  302. case ValueID::Medium:
  303. case ValueID::Large:
  304. case ValueID::XLarge:
  305. case ValueID::XxLarge:
  306. case ValueID::XxxLarge:
  307. case ValueID::Smaller:
  308. case ValueID::Larger:
  309. return true;
  310. default:
  311. return false;
  312. }
  313. }
  314. static inline bool is_font_style(StyleValue const& value)
  315. {
  316. // FIXME: Handle angle parameter to `oblique`: https://www.w3.org/TR/css-fonts-4/#font-style-prop
  317. if (value.is_builtin_or_dynamic())
  318. return true;
  319. switch (value.to_identifier()) {
  320. case ValueID::Normal:
  321. case ValueID::Italic:
  322. case ValueID::Oblique:
  323. return true;
  324. default:
  325. return false;
  326. }
  327. }
  328. static inline bool is_font_weight(StyleValue const& value)
  329. {
  330. if (value.is_builtin_or_dynamic())
  331. return true;
  332. if (value.is_numeric()) {
  333. auto weight = static_cast<NumericStyleValue const&>(value).value();
  334. return (weight >= 1 && weight <= 1000);
  335. }
  336. switch (value.to_identifier()) {
  337. case ValueID::Normal:
  338. case ValueID::Bold:
  339. case ValueID::Bolder:
  340. case ValueID::Lighter:
  341. return true;
  342. default:
  343. return false;
  344. }
  345. }
  346. static inline bool is_line_height(StyleValue const& value)
  347. {
  348. if (value.is_builtin_or_dynamic())
  349. return true;
  350. if (value.is_numeric())
  351. return true;
  352. if (value.is_length())
  353. return true;
  354. if (value.to_identifier() == ValueID::Normal)
  355. return true;
  356. return false;
  357. }
  358. static inline bool is_line_style(StyleValue const& value)
  359. {
  360. if (value.is_builtin_or_dynamic())
  361. return true;
  362. switch (value.to_identifier()) {
  363. case ValueID::Dotted:
  364. case ValueID::Dashed:
  365. case ValueID::Solid:
  366. case ValueID::Double:
  367. case ValueID::Groove:
  368. case ValueID::Ridge:
  369. case ValueID::None:
  370. case ValueID::Hidden:
  371. case ValueID::Inset:
  372. case ValueID::Outset:
  373. return true;
  374. default:
  375. return false;
  376. }
  377. }
  378. static inline bool is_line_width(StyleValue const& value)
  379. {
  380. if (value.is_builtin_or_dynamic())
  381. return true;
  382. if (value.is_length())
  383. return true;
  384. // FIXME: Implement thin/medium/thick
  385. switch (value.to_identifier()) {
  386. case ValueID::None:
  387. return true;
  388. default:
  389. return false;
  390. }
  391. }
  392. static inline bool is_list_style_image(StyleValue const& value)
  393. {
  394. if (value.is_builtin_or_dynamic())
  395. return true;
  396. if (value.is_image())
  397. return true;
  398. if (value.is_identifier() && value.to_identifier() == ValueID::None)
  399. return true;
  400. return false;
  401. }
  402. static inline bool is_list_style_position(StyleValue const& value)
  403. {
  404. if (value.is_builtin_or_dynamic())
  405. return true;
  406. switch (value.to_identifier()) {
  407. case ValueID::Inside:
  408. case ValueID::Outside:
  409. return true;
  410. default:
  411. return false;
  412. }
  413. }
  414. static inline bool is_list_style_type(StyleValue const& value)
  415. {
  416. if (value.is_builtin_or_dynamic())
  417. return true;
  418. // FIXME: Handle strings and symbols("...") syntax
  419. switch (value.to_identifier()) {
  420. case CSS::ValueID::None:
  421. case CSS::ValueID::Disc:
  422. case CSS::ValueID::Circle:
  423. case CSS::ValueID::Square:
  424. case CSS::ValueID::Decimal:
  425. case CSS::ValueID::DecimalLeadingZero:
  426. case CSS::ValueID::LowerAlpha:
  427. case CSS::ValueID::LowerLatin:
  428. case CSS::ValueID::UpperAlpha:
  429. case CSS::ValueID::UpperLatin:
  430. case CSS::ValueID::UpperRoman:
  431. case CSS::ValueID::LowerRoman:
  432. return true;
  433. default:
  434. return true;
  435. }
  436. }
  437. static inline bool is_text_decoration_line(StyleValue const& value)
  438. {
  439. if (value.is_builtin_or_dynamic())
  440. return true;
  441. switch (value.to_identifier()) {
  442. case ValueID::None:
  443. case ValueID::Underline:
  444. case ValueID::Overline:
  445. case ValueID::LineThrough:
  446. case ValueID::Blink:
  447. return true;
  448. default:
  449. return false;
  450. }
  451. }
  452. static inline bool is_text_decoration_style(StyleValue const& value)
  453. {
  454. if (value.is_builtin_or_dynamic())
  455. return true;
  456. switch (value.to_identifier()) {
  457. case ValueID::Solid:
  458. case ValueID::Double:
  459. case ValueID::Dotted:
  460. case ValueID::Dashed:
  461. case ValueID::Wavy:
  462. return true;
  463. default:
  464. return false;
  465. }
  466. }
  467. static void set_property_expanding_shorthands(StyleProperties& style, CSS::PropertyID property_id, StyleValue const& value, DOM::Document& document, bool is_internally_generated_pseudo_property = false)
  468. {
  469. CSS::ParsingContext context(document);
  470. if (is_pseudo_property(property_id) && !is_internally_generated_pseudo_property) {
  471. dbgln("Ignoring non-internally-generated pseudo property: {}", string_from_property_id(property_id));
  472. return;
  473. }
  474. if (property_id == CSS::PropertyID::TextDecoration) {
  475. if (value.is_color()) {
  476. style.set_property(CSS::PropertyID::TextDecorationColor, value);
  477. return;
  478. }
  479. if (is_text_decoration_line(value)) {
  480. style.set_property(CSS::PropertyID::TextDecorationLine, value);
  481. return;
  482. }
  483. if (is_text_decoration_style(value)) {
  484. style.set_property(CSS::PropertyID::TextDecorationStyle, value);
  485. return;
  486. }
  487. if (value.is_value_list()) {
  488. auto& parts = static_cast<CSS::ValueListStyleValue const&>(value).values();
  489. if (!parts.is_empty() && parts.size() <= 3) {
  490. RefPtr<StyleValue> color_value;
  491. RefPtr<StyleValue> line_value;
  492. RefPtr<StyleValue> style_value;
  493. for (auto& part : parts) {
  494. auto value = Parser::parse_css_value(context, property_id, part);
  495. if (!value)
  496. return;
  497. if (value->is_color()) {
  498. if (color_value)
  499. return;
  500. color_value = move(value);
  501. continue;
  502. }
  503. if (is_text_decoration_line(*value)) {
  504. if (line_value)
  505. return;
  506. line_value = move(value);
  507. continue;
  508. }
  509. if (is_text_decoration_style(*value)) {
  510. if (style_value)
  511. return;
  512. style_value = move(value);
  513. continue;
  514. }
  515. return;
  516. }
  517. if (color_value)
  518. style.set_property(CSS::PropertyID::TextDecorationColor, *color_value);
  519. if (line_value)
  520. style.set_property(CSS::PropertyID::TextDecorationLine, *line_value);
  521. if (style_value)
  522. style.set_property(CSS::PropertyID::TextDecorationStyle, *style_value);
  523. }
  524. return;
  525. }
  526. return;
  527. }
  528. if (property_id == CSS::PropertyID::Overflow) {
  529. style.set_property(CSS::PropertyID::OverflowX, value);
  530. style.set_property(CSS::PropertyID::OverflowY, value);
  531. return;
  532. }
  533. if (property_id == CSS::PropertyID::Border) {
  534. set_property_expanding_shorthands(style, CSS::PropertyID::BorderTop, value, document);
  535. set_property_expanding_shorthands(style, CSS::PropertyID::BorderRight, value, document);
  536. set_property_expanding_shorthands(style, CSS::PropertyID::BorderBottom, value, document);
  537. set_property_expanding_shorthands(style, CSS::PropertyID::BorderLeft, value, document);
  538. return;
  539. }
  540. if (property_id == CSS::PropertyID::BorderRadius) {
  541. // FIXME: Allow for two values per corner to support elliptical radii.
  542. // FIXME: Add support the '/' to specify elliptical radii.
  543. if (value.is_length()) {
  544. style.set_property(CSS::PropertyID::BorderTopLeftRadius, value);
  545. style.set_property(CSS::PropertyID::BorderTopRightRadius, value);
  546. style.set_property(CSS::PropertyID::BorderBottomRightRadius, value);
  547. style.set_property(CSS::PropertyID::BorderBottomLeftRadius, value);
  548. return;
  549. }
  550. if (value.is_value_list()) {
  551. auto& parts = static_cast<CSS::ValueListStyleValue const&>(value).values();
  552. if (parts.size() == 2) {
  553. auto diagonal1 = Parser::parse_css_value(context, property_id, parts[0]);
  554. auto diagonal2 = Parser::parse_css_value(context, property_id, parts[1]);
  555. if (diagonal1 && diagonal2) {
  556. style.set_property(CSS::PropertyID::BorderTopLeftRadius, *diagonal1);
  557. style.set_property(CSS::PropertyID::BorderBottomRightRadius, *diagonal1);
  558. style.set_property(CSS::PropertyID::BorderTopRightRadius, *diagonal2);
  559. style.set_property(CSS::PropertyID::BorderBottomLeftRadius, *diagonal2);
  560. }
  561. return;
  562. }
  563. if (parts.size() == 3) {
  564. auto top_left = Parser::parse_css_value(context, property_id, parts[0]);
  565. auto diagonal = Parser::parse_css_value(context, property_id, parts[1]);
  566. auto bottom_right = Parser::parse_css_value(context, property_id, parts[2]);
  567. if (top_left && diagonal && bottom_right) {
  568. style.set_property(CSS::PropertyID::BorderTopLeftRadius, *top_left);
  569. style.set_property(CSS::PropertyID::BorderBottomRightRadius, *bottom_right);
  570. style.set_property(CSS::PropertyID::BorderTopRightRadius, *diagonal);
  571. style.set_property(CSS::PropertyID::BorderBottomLeftRadius, *diagonal);
  572. }
  573. return;
  574. }
  575. if (parts.size() == 4) {
  576. auto top_left = Parser::parse_css_value(context, property_id, parts[0]);
  577. auto top_right = Parser::parse_css_value(context, property_id, parts[1]);
  578. auto bottom_right = Parser::parse_css_value(context, property_id, parts[2]);
  579. auto bottom_left = Parser::parse_css_value(context, property_id, parts[3]);
  580. if (top_left && top_right && bottom_right && bottom_left) {
  581. style.set_property(CSS::PropertyID::BorderTopLeftRadius, *top_left);
  582. style.set_property(CSS::PropertyID::BorderBottomRightRadius, *bottom_right);
  583. style.set_property(CSS::PropertyID::BorderTopRightRadius, *top_right);
  584. style.set_property(CSS::PropertyID::BorderBottomLeftRadius, *bottom_left);
  585. }
  586. return;
  587. }
  588. dbgln("Unsure what to do with CSS border-radius value '{}'", value.to_string());
  589. return;
  590. }
  591. return;
  592. }
  593. if (property_id == CSS::PropertyID::BorderTop
  594. || property_id == CSS::PropertyID::BorderRight
  595. || property_id == CSS::PropertyID::BorderBottom
  596. || property_id == CSS::PropertyID::BorderLeft) {
  597. Edge edge = Edge::All;
  598. switch (property_id) {
  599. case CSS::PropertyID::BorderTop:
  600. edge = Edge::Top;
  601. break;
  602. case CSS::PropertyID::BorderRight:
  603. edge = Edge::Right;
  604. break;
  605. case CSS::PropertyID::BorderBottom:
  606. edge = Edge::Bottom;
  607. break;
  608. case CSS::PropertyID::BorderLeft:
  609. edge = Edge::Left;
  610. break;
  611. default:
  612. break;
  613. }
  614. auto parts = split_on_whitespace(value.to_string());
  615. if (value.is_length()) {
  616. set_property_border_width(style, value, edge);
  617. return;
  618. }
  619. if (value.is_color()) {
  620. set_property_border_color(style, value, edge);
  621. return;
  622. }
  623. if (value.is_value_list()) {
  624. auto& parts = static_cast<CSS::ValueListStyleValue const&>(value).values();
  625. if (parts.size() == 1) {
  626. auto value = Parser::parse_css_value(context, property_id, parts[0]);
  627. if (value && is_line_style(*value)) {
  628. set_property_border_style(style, value.release_nonnull(), edge);
  629. set_property_border_color(style, ColorStyleValue::create(Gfx::Color::Black), edge);
  630. set_property_border_width(style, LengthStyleValue::create(Length(3, Length::Type::Px)), edge);
  631. return;
  632. }
  633. }
  634. RefPtr<StyleValue> line_width_value;
  635. RefPtr<StyleValue> color_value;
  636. RefPtr<StyleValue> line_style_value;
  637. for (auto& part : parts) {
  638. auto value = Parser::parse_css_value(context, property_id, part);
  639. if (!value)
  640. return;
  641. if (is_line_width(*value)) {
  642. if (line_width_value)
  643. return;
  644. line_width_value = move(value);
  645. continue;
  646. }
  647. if (is_color(*value)) {
  648. if (color_value)
  649. return;
  650. color_value = move(value);
  651. continue;
  652. }
  653. if (is_line_style(*value)) {
  654. if (line_style_value)
  655. return;
  656. line_style_value = move(value);
  657. continue;
  658. }
  659. }
  660. if (line_width_value)
  661. set_property_border_width(style, line_width_value.release_nonnull(), edge);
  662. if (color_value)
  663. set_property_border_color(style, color_value.release_nonnull(), edge);
  664. if (line_style_value)
  665. set_property_border_style(style, line_style_value.release_nonnull(), edge);
  666. return;
  667. }
  668. return;
  669. }
  670. if (property_id == CSS::PropertyID::BorderStyle) {
  671. if (value.is_value_list()) {
  672. auto& parts = static_cast<CSS::ValueListStyleValue const&>(value).values();
  673. if (parts.size() == 4) {
  674. auto top = Parser::parse_css_value(context, property_id, parts[0]);
  675. auto right = Parser::parse_css_value(context, property_id, parts[1]);
  676. auto bottom = Parser::parse_css_value(context, property_id, parts[2]);
  677. auto left = Parser::parse_css_value(context, property_id, parts[3]);
  678. if (top && right && bottom && left) {
  679. style.set_property(CSS::PropertyID::BorderTopStyle, *top);
  680. style.set_property(CSS::PropertyID::BorderRightStyle, *right);
  681. style.set_property(CSS::PropertyID::BorderBottomStyle, *bottom);
  682. style.set_property(CSS::PropertyID::BorderLeftStyle, *left);
  683. }
  684. } else if (parts.size() == 3) {
  685. auto top = Parser::parse_css_value(context, property_id, parts[0]);
  686. auto right = Parser::parse_css_value(context, property_id, parts[1]);
  687. auto bottom = Parser::parse_css_value(context, property_id, parts[2]);
  688. auto left = Parser::parse_css_value(context, property_id, parts[1]);
  689. if (top && right && bottom && left) {
  690. style.set_property(CSS::PropertyID::BorderTopStyle, *top);
  691. style.set_property(CSS::PropertyID::BorderRightStyle, *right);
  692. style.set_property(CSS::PropertyID::BorderBottomStyle, *bottom);
  693. style.set_property(CSS::PropertyID::BorderLeftStyle, *left);
  694. }
  695. } else if (parts.size() == 2) {
  696. auto vertical = Parser::parse_css_value(context, property_id, parts[0]);
  697. auto horizontal = Parser::parse_css_value(context, property_id, parts[1]);
  698. if (vertical && horizontal) {
  699. style.set_property(CSS::PropertyID::BorderTopStyle, *vertical);
  700. style.set_property(CSS::PropertyID::BorderRightStyle, *horizontal);
  701. style.set_property(CSS::PropertyID::BorderBottomStyle, *vertical);
  702. style.set_property(CSS::PropertyID::BorderLeftStyle, *horizontal);
  703. }
  704. } else {
  705. auto line_style = Parser::parse_css_value(context, property_id, parts[0]);
  706. if (line_style) {
  707. style.set_property(CSS::PropertyID::BorderTopStyle, *line_style);
  708. style.set_property(CSS::PropertyID::BorderRightStyle, *line_style);
  709. style.set_property(CSS::PropertyID::BorderBottomStyle, *line_style);
  710. style.set_property(CSS::PropertyID::BorderLeftStyle, *line_style);
  711. }
  712. }
  713. return;
  714. }
  715. style.set_property(CSS::PropertyID::BorderTopStyle, value);
  716. style.set_property(CSS::PropertyID::BorderRightStyle, value);
  717. style.set_property(CSS::PropertyID::BorderBottomStyle, value);
  718. style.set_property(CSS::PropertyID::BorderLeftStyle, value);
  719. return;
  720. }
  721. if (property_id == CSS::PropertyID::BorderWidth) {
  722. if (value.is_value_list()) {
  723. auto& parts = static_cast<CSS::ValueListStyleValue const&>(value).values();
  724. if (parts.size() == 4) {
  725. auto top_border_width = Parser::parse_css_value(context, property_id, parts[0]);
  726. auto right_border_width = Parser::parse_css_value(context, property_id, parts[1]);
  727. auto bottom_border_width = Parser::parse_css_value(context, property_id, parts[2]);
  728. auto left_border_width = Parser::parse_css_value(context, property_id, parts[3]);
  729. if (top_border_width && right_border_width && bottom_border_width && left_border_width) {
  730. style.set_property(CSS::PropertyID::BorderTopWidth, *top_border_width);
  731. style.set_property(CSS::PropertyID::BorderRightWidth, *right_border_width);
  732. style.set_property(CSS::PropertyID::BorderBottomWidth, *bottom_border_width);
  733. style.set_property(CSS::PropertyID::BorderLeftWidth, *left_border_width);
  734. }
  735. } else if (parts.size() == 3) {
  736. auto top_border_width = Parser::parse_css_value(context, property_id, parts[0]);
  737. auto horizontal_border_width = Parser::parse_css_value(context, property_id, parts[1]);
  738. auto bottom_border_width = Parser::parse_css_value(context, property_id, parts[2]);
  739. if (top_border_width && horizontal_border_width && bottom_border_width) {
  740. style.set_property(CSS::PropertyID::BorderTopWidth, *top_border_width);
  741. style.set_property(CSS::PropertyID::BorderRightWidth, *horizontal_border_width);
  742. style.set_property(CSS::PropertyID::BorderBottomWidth, *bottom_border_width);
  743. style.set_property(CSS::PropertyID::BorderLeftWidth, *horizontal_border_width);
  744. }
  745. } else if (parts.size() == 2) {
  746. auto vertical_border_width = Parser::parse_css_value(context, property_id, parts[0]);
  747. auto horizontal_border_width = Parser::parse_css_value(context, property_id, parts[1]);
  748. if (vertical_border_width && horizontal_border_width) {
  749. style.set_property(CSS::PropertyID::BorderTopWidth, *vertical_border_width);
  750. style.set_property(CSS::PropertyID::BorderRightWidth, *horizontal_border_width);
  751. style.set_property(CSS::PropertyID::BorderBottomWidth, *vertical_border_width);
  752. style.set_property(CSS::PropertyID::BorderLeftWidth, *horizontal_border_width);
  753. }
  754. } else {
  755. auto border_width = Parser::parse_css_value(context, property_id, parts[0]);
  756. if (border_width) {
  757. style.set_property(CSS::PropertyID::BorderTopWidth, *border_width);
  758. style.set_property(CSS::PropertyID::BorderRightWidth, *border_width);
  759. style.set_property(CSS::PropertyID::BorderBottomWidth, *border_width);
  760. style.set_property(CSS::PropertyID::BorderLeftWidth, *border_width);
  761. }
  762. }
  763. return;
  764. }
  765. style.set_property(CSS::PropertyID::BorderTopStyle, value);
  766. style.set_property(CSS::PropertyID::BorderRightStyle, value);
  767. style.set_property(CSS::PropertyID::BorderBottomStyle, value);
  768. style.set_property(CSS::PropertyID::BorderLeftStyle, value);
  769. return;
  770. }
  771. if (property_id == CSS::PropertyID::BorderColor) {
  772. if (value.is_value_list()) {
  773. auto& parts = static_cast<CSS::ValueListStyleValue const&>(value).values();
  774. if (parts.size() == 4) {
  775. auto top = Parser::parse_css_value(context, property_id, parts[0]);
  776. auto right = Parser::parse_css_value(context, property_id, parts[1]);
  777. auto bottom = Parser::parse_css_value(context, property_id, parts[2]);
  778. auto left = Parser::parse_css_value(context, property_id, parts[3]);
  779. if (top && right && bottom && left) {
  780. style.set_property(CSS::PropertyID::BorderTopColor, *top);
  781. style.set_property(CSS::PropertyID::BorderRightColor, *right);
  782. style.set_property(CSS::PropertyID::BorderBottomColor, *bottom);
  783. style.set_property(CSS::PropertyID::BorderLeftColor, *left);
  784. }
  785. } else if (parts.size() == 3) {
  786. auto top = Parser::parse_css_value(context, property_id, parts[0]);
  787. auto horizontal = Parser::parse_css_value(context, property_id, parts[1]);
  788. auto bottom = Parser::parse_css_value(context, property_id, parts[2]);
  789. if (top && horizontal && bottom) {
  790. style.set_property(CSS::PropertyID::BorderTopColor, *top);
  791. style.set_property(CSS::PropertyID::BorderRightColor, *horizontal);
  792. style.set_property(CSS::PropertyID::BorderBottomColor, *bottom);
  793. style.set_property(CSS::PropertyID::BorderLeftColor, *horizontal);
  794. }
  795. } else if (parts.size() == 2) {
  796. auto vertical = Parser::parse_css_value(context, property_id, parts[0]);
  797. auto horizontal = Parser::parse_css_value(context, property_id, parts[1]);
  798. if (vertical && horizontal) {
  799. style.set_property(CSS::PropertyID::BorderTopColor, *vertical);
  800. style.set_property(CSS::PropertyID::BorderRightColor, *horizontal);
  801. style.set_property(CSS::PropertyID::BorderBottomColor, *vertical);
  802. style.set_property(CSS::PropertyID::BorderLeftColor, *horizontal);
  803. }
  804. } else {
  805. auto color = Parser::parse_css_value(context, property_id, parts[0]);
  806. if (color) {
  807. style.set_property(CSS::PropertyID::BorderTopColor, *color);
  808. style.set_property(CSS::PropertyID::BorderRightColor, *color);
  809. style.set_property(CSS::PropertyID::BorderBottomColor, *color);
  810. style.set_property(CSS::PropertyID::BorderLeftColor, *color);
  811. }
  812. }
  813. return;
  814. }
  815. style.set_property(CSS::PropertyID::BorderTopColor, value);
  816. style.set_property(CSS::PropertyID::BorderRightColor, value);
  817. style.set_property(CSS::PropertyID::BorderBottomColor, value);
  818. style.set_property(CSS::PropertyID::BorderLeftColor, value);
  819. return;
  820. }
  821. if (property_id == CSS::PropertyID::Background) {
  822. if (value.to_identifier() == ValueID::None) {
  823. style.set_property(CSS::PropertyID::BackgroundColor, ColorStyleValue::create(Color::Transparent));
  824. return;
  825. }
  826. if (is_background_image(value)) {
  827. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundImage, value, document);
  828. return;
  829. }
  830. if (is_color(value)) {
  831. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundColor, value, document);
  832. return;
  833. }
  834. if (value.is_value_list()) {
  835. auto parts = static_cast<CSS::ValueListStyleValue const&>(value).values();
  836. RefPtr<StyleValue> background_color_value;
  837. RefPtr<StyleValue> background_image_value;
  838. RefPtr<StyleValue> repeat_x_value;
  839. RefPtr<StyleValue> repeat_y_value;
  840. // FIXME: Implement background-position.
  841. // FIXME: Implement background-size.
  842. // FIXME: Implement background-attachment.
  843. // FIXME: Implement background-clip.
  844. // FIXME: Implement background-origin.
  845. for (size_t i = 0; i < parts.size(); ++i) {
  846. auto& part = parts[i];
  847. // FIXME: Handle multiple backgrounds.
  848. if (part.is(Token::Type::Comma))
  849. break;
  850. auto value = Parser::parse_css_value(context, property_id, part);
  851. if (!value) {
  852. dbgln("Unable to parse token in `background` as a CSS value: '{}'", part.to_debug_string());
  853. return;
  854. }
  855. if (value->is_color()) {
  856. if (background_color_value)
  857. return;
  858. background_color_value = move(value);
  859. continue;
  860. }
  861. if (is_background_image(*value)) {
  862. if (background_image_value)
  863. return;
  864. background_image_value = move(value);
  865. continue;
  866. }
  867. if (is_background_repeat(*value)) {
  868. if (repeat_x_value)
  869. return;
  870. auto value_id = value->to_identifier();
  871. if (value_id == ValueID::RepeatX || value_id == ValueID::RepeatY) {
  872. repeat_x_value = IdentifierStyleValue::create(value_id == ValueID::RepeatX ? ValueID::Repeat : ValueID::NoRepeat);
  873. repeat_y_value = IdentifierStyleValue::create(value_id == ValueID::RepeatX ? ValueID::NoRepeat : ValueID::Repeat);
  874. continue;
  875. }
  876. // Check following value, if it's also a repeat, set both.
  877. if (i + 1 < parts.size()) {
  878. auto next_value = Parser::parse_css_value(context, property_id, parts[i + 1]);
  879. if (next_value && is_background_repeat(*next_value)) {
  880. ++i;
  881. repeat_x_value = move(value);
  882. repeat_y_value = move(next_value);
  883. continue;
  884. }
  885. }
  886. repeat_x_value = value;
  887. repeat_y_value = value;
  888. continue;
  889. }
  890. dbgln("Unhandled token in `background` declaration: '{}'", part.to_debug_string());
  891. return;
  892. }
  893. if (background_color_value)
  894. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundColor, background_color_value.release_nonnull(), document);
  895. if (background_image_value)
  896. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundImage, background_image_value.release_nonnull(), document);
  897. if (repeat_x_value)
  898. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundRepeatX, repeat_x_value.release_nonnull(), document, true);
  899. if (repeat_y_value)
  900. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundRepeatY, repeat_y_value.release_nonnull(), document, true);
  901. return;
  902. }
  903. return;
  904. }
  905. if (property_id == CSS::PropertyID::BackgroundImage) {
  906. if (is_background_image(value)) {
  907. style.set_property(CSS::PropertyID::BackgroundImage, value);
  908. return;
  909. }
  910. // FIXME: Remove string parsing once DeprecatedCSSParser is gone.
  911. if (value.is_string()) {
  912. return;
  913. auto string = value.to_string();
  914. if (!string.starts_with("url("))
  915. return;
  916. if (!string.ends_with(')'))
  917. return;
  918. auto url = string.substring_view(4, string.length() - 5);
  919. if (url.length() >= 2 && url.starts_with('"') && url.ends_with('"'))
  920. url = url.substring_view(1, url.length() - 2);
  921. else if (url.length() >= 2 && url.starts_with('\'') && url.ends_with('\''))
  922. url = url.substring_view(1, url.length() - 2);
  923. auto background_image_value = ImageStyleValue::create(document.complete_url(url), document);
  924. style.set_property(CSS::PropertyID::BackgroundImage, move(background_image_value));
  925. return;
  926. }
  927. if (value.is_value_list()) {
  928. auto parts = static_cast<CSS::ValueListStyleValue const&>(value).values();
  929. // FIXME: Handle multiple backgrounds.
  930. if (!parts.is_empty()) {
  931. auto first_value = Parser::parse_css_value(context, property_id, parts[0]);
  932. if (first_value)
  933. style.set_property(CSS::PropertyID::BackgroundImage, *first_value);
  934. }
  935. return;
  936. }
  937. dbgln("Unsure what to do with CSS background-image value '{}'", value.to_string());
  938. return;
  939. }
  940. if (property_id == CSS::PropertyID::BackgroundRepeat) {
  941. auto assign_background_repeat_from_single_value = [&](StyleValue const& value) {
  942. auto value_id = value.to_identifier();
  943. if (value_id == ValueID::RepeatX || value_id == ValueID::RepeatY) {
  944. auto repeat_x = IdentifierStyleValue::create(value_id == ValueID::RepeatX ? ValueID::Repeat : ValueID::NoRepeat);
  945. auto repeat_y = IdentifierStyleValue::create(value_id == ValueID::RepeatX ? ValueID::NoRepeat : ValueID::Repeat);
  946. set_property_expanding_shorthands(style, PropertyID::BackgroundRepeatX, repeat_x, document, true);
  947. set_property_expanding_shorthands(style, PropertyID::BackgroundRepeatY, repeat_y, document, true);
  948. } else {
  949. set_property_expanding_shorthands(style, PropertyID::BackgroundRepeatX, value, document, true);
  950. set_property_expanding_shorthands(style, PropertyID::BackgroundRepeatY, value, document, true);
  951. }
  952. };
  953. if (is_background_repeat(value)) {
  954. assign_background_repeat_from_single_value(value);
  955. }
  956. if (value.is_value_list()) {
  957. auto parts = static_cast<CSS::ValueListStyleValue const&>(value).values();
  958. NonnullRefPtrVector<StyleValue> repeat_values;
  959. for (auto& part : parts) {
  960. if (part.is(Token::Type::Comma)) {
  961. // FIXME: Handle multiple backgrounds.
  962. break;
  963. }
  964. auto parsed_value = Parser::parse_css_value(context, property_id, part);
  965. if (parsed_value)
  966. repeat_values.append(parsed_value.release_nonnull());
  967. }
  968. if (repeat_values.size() == 1) {
  969. assign_background_repeat_from_single_value(repeat_values[0]);
  970. } else if (repeat_values.size() == 2) {
  971. set_property_expanding_shorthands(style, PropertyID::BackgroundRepeatX, repeat_values[0], document, true);
  972. set_property_expanding_shorthands(style, PropertyID::BackgroundRepeatY, repeat_values[1], document, true);
  973. }
  974. }
  975. return;
  976. }
  977. if (property_id == CSS::PropertyID::BackgroundRepeatX || property_id == CSS::PropertyID::BackgroundRepeatY) {
  978. auto value_id = value.to_identifier();
  979. if (value_id == CSS::ValueID::RepeatX || value_id == CSS::ValueID::RepeatY)
  980. return;
  981. style.set_property(property_id, value);
  982. return;
  983. }
  984. if (property_id == CSS::PropertyID::Margin) {
  985. if (value.is_length()) {
  986. style.set_property(CSS::PropertyID::MarginTop, value);
  987. style.set_property(CSS::PropertyID::MarginRight, value);
  988. style.set_property(CSS::PropertyID::MarginBottom, value);
  989. style.set_property(CSS::PropertyID::MarginLeft, value);
  990. return;
  991. }
  992. if (value.is_value_list()) {
  993. auto parts = static_cast<CSS::ValueListStyleValue const&>(value).values();
  994. if (parts.size() == 2) {
  995. auto vertical = Parser::parse_css_value(context, property_id, parts[0]);
  996. auto horizontal = Parser::parse_css_value(context, property_id, parts[1]);
  997. if (vertical && horizontal) {
  998. style.set_property(CSS::PropertyID::MarginTop, *vertical);
  999. style.set_property(CSS::PropertyID::MarginBottom, *vertical);
  1000. style.set_property(CSS::PropertyID::MarginLeft, *horizontal);
  1001. style.set_property(CSS::PropertyID::MarginRight, *horizontal);
  1002. }
  1003. return;
  1004. } else if (parts.size() == 3) {
  1005. auto top = Parser::parse_css_value(context, property_id, parts[0]);
  1006. auto horizontal = Parser::parse_css_value(context, property_id, parts[1]);
  1007. auto bottom = Parser::parse_css_value(context, property_id, parts[2]);
  1008. if (top && horizontal && bottom) {
  1009. style.set_property(CSS::PropertyID::MarginTop, *top);
  1010. style.set_property(CSS::PropertyID::MarginBottom, *bottom);
  1011. style.set_property(CSS::PropertyID::MarginLeft, *horizontal);
  1012. style.set_property(CSS::PropertyID::MarginRight, *horizontal);
  1013. }
  1014. return;
  1015. } else if (parts.size() == 4) {
  1016. auto top = Parser::parse_css_value(context, property_id, parts[0]);
  1017. auto right = Parser::parse_css_value(context, property_id, parts[1]);
  1018. auto bottom = Parser::parse_css_value(context, property_id, parts[2]);
  1019. auto left = Parser::parse_css_value(context, property_id, parts[3]);
  1020. if (top && right && bottom && left) {
  1021. style.set_property(CSS::PropertyID::MarginTop, *top);
  1022. style.set_property(CSS::PropertyID::MarginBottom, *bottom);
  1023. style.set_property(CSS::PropertyID::MarginLeft, *left);
  1024. style.set_property(CSS::PropertyID::MarginRight, *right);
  1025. }
  1026. return;
  1027. }
  1028. dbgln("Unsure what to do with CSS margin value '{}'", value.to_string());
  1029. return;
  1030. }
  1031. return;
  1032. }
  1033. if (property_id == CSS::PropertyID::Padding) {
  1034. if (value.is_length()) {
  1035. style.set_property(CSS::PropertyID::PaddingTop, value);
  1036. style.set_property(CSS::PropertyID::PaddingRight, value);
  1037. style.set_property(CSS::PropertyID::PaddingBottom, value);
  1038. style.set_property(CSS::PropertyID::PaddingLeft, value);
  1039. return;
  1040. }
  1041. if (value.is_value_list()) {
  1042. auto parts = static_cast<CSS::ValueListStyleValue const&>(value).values();
  1043. if (parts.size() == 2) {
  1044. auto vertical = Parser::parse_css_value(context, property_id, parts[0]);
  1045. auto horizontal = Parser::parse_css_value(context, property_id, parts[1]);
  1046. if (vertical && horizontal) {
  1047. style.set_property(CSS::PropertyID::PaddingTop, *vertical);
  1048. style.set_property(CSS::PropertyID::PaddingBottom, *vertical);
  1049. style.set_property(CSS::PropertyID::PaddingLeft, *horizontal);
  1050. style.set_property(CSS::PropertyID::PaddingRight, *horizontal);
  1051. }
  1052. return;
  1053. } else if (parts.size() == 3) {
  1054. auto top = Parser::parse_css_value(context, property_id, parts[0]);
  1055. auto horizontal = Parser::parse_css_value(context, property_id, parts[1]);
  1056. auto bottom = Parser::parse_css_value(context, property_id, parts[2]);
  1057. if (top && bottom && horizontal) {
  1058. style.set_property(CSS::PropertyID::PaddingTop, *top);
  1059. style.set_property(CSS::PropertyID::PaddingBottom, *bottom);
  1060. style.set_property(CSS::PropertyID::PaddingLeft, *horizontal);
  1061. style.set_property(CSS::PropertyID::PaddingRight, *horizontal);
  1062. }
  1063. return;
  1064. } else if (parts.size() == 4) {
  1065. auto top = Parser::parse_css_value(context, property_id, parts[0]);
  1066. auto right = Parser::parse_css_value(context, property_id, parts[1]);
  1067. auto bottom = Parser::parse_css_value(context, property_id, parts[2]);
  1068. auto left = Parser::parse_css_value(context, property_id, parts[3]);
  1069. if (top && bottom && left && right) {
  1070. style.set_property(CSS::PropertyID::PaddingTop, *top);
  1071. style.set_property(CSS::PropertyID::PaddingBottom, *bottom);
  1072. style.set_property(CSS::PropertyID::PaddingLeft, *left);
  1073. style.set_property(CSS::PropertyID::PaddingRight, *right);
  1074. }
  1075. return;
  1076. }
  1077. dbgln("Unsure what to do with CSS padding value '{}'", value.to_string());
  1078. return;
  1079. }
  1080. return;
  1081. }
  1082. if (property_id == CSS::PropertyID::ListStyle) {
  1083. if (value.is_value_list()) {
  1084. auto parts = static_cast<CSS::ValueListStyleValue const&>(value).values();
  1085. if (!parts.is_empty() && parts.size() <= 3) {
  1086. RefPtr<StyleValue> position_value;
  1087. RefPtr<StyleValue> image_value;
  1088. RefPtr<StyleValue> type_value;
  1089. // FIXME: `none` is ambiguous as it is a valid value for ListStyleImage and ListStyleType,
  1090. // so requires special handling. https://www.w3.org/TR/css-lists-3/#propdef-list-style
  1091. for (auto& part : parts) {
  1092. auto value = Parser::parse_css_value(context, property_id, part);
  1093. if (!value)
  1094. return;
  1095. if (is_list_style_position(*value)) {
  1096. if (position_value)
  1097. return;
  1098. position_value = move(value);
  1099. continue;
  1100. }
  1101. if (is_list_style_image(*value)) {
  1102. if (image_value)
  1103. return;
  1104. image_value = move(value);
  1105. continue;
  1106. }
  1107. if (is_list_style_type(*value)) {
  1108. if (type_value)
  1109. return;
  1110. type_value = move(value);
  1111. continue;
  1112. }
  1113. }
  1114. if (position_value)
  1115. style.set_property(CSS::PropertyID::ListStylePosition, *position_value);
  1116. if (image_value)
  1117. style.set_property(CSS::PropertyID::ListStyleImage, *image_value);
  1118. if (type_value)
  1119. style.set_property(CSS::PropertyID::ListStyleType, *type_value);
  1120. }
  1121. return;
  1122. }
  1123. if (is_list_style_position(value))
  1124. style.set_property(CSS::PropertyID::ListStylePosition, value);
  1125. else if (is_list_style_image(value))
  1126. style.set_property(CSS::PropertyID::ListStyleImage, value);
  1127. else if (is_list_style_type(value))
  1128. style.set_property(CSS::PropertyID::ListStyleType, value);
  1129. return;
  1130. }
  1131. if (property_id == CSS::PropertyID::Font) {
  1132. if (value.is_value_list()) {
  1133. auto parts = static_cast<CSS::ValueListStyleValue const&>(value).values();
  1134. RefPtr<StyleValue> font_style_value;
  1135. RefPtr<StyleValue> font_weight_value;
  1136. RefPtr<StyleValue> font_size_value;
  1137. RefPtr<StyleValue> line_height_value;
  1138. RefPtr<StyleValue> font_family_value;
  1139. // FIXME: Implement font-stretch and font-variant.
  1140. for (size_t i = 0; i < parts.size(); ++i) {
  1141. auto value = Parser::parse_css_value(context, property_id, parts[i]);
  1142. if (!value)
  1143. return;
  1144. if (is_font_style(*value)) {
  1145. if (font_style_value)
  1146. return;
  1147. font_style_value = move(value);
  1148. continue;
  1149. }
  1150. if (is_font_weight(*value)) {
  1151. if (font_weight_value)
  1152. return;
  1153. font_weight_value = move(value);
  1154. continue;
  1155. }
  1156. if (is_font_size(*value)) {
  1157. if (font_size_value)
  1158. return;
  1159. font_size_value = move(value);
  1160. // Consume `/ line-height` if present
  1161. if (i + 2 < parts.size()) {
  1162. auto solidus_part = parts[i + 1];
  1163. if (!(solidus_part.is(Token::Type::Delim) && solidus_part.token().delim() == "/"sv))
  1164. break;
  1165. auto line_height = Parser::parse_css_value(context, property_id, parts[i + 2]);
  1166. if (!(line_height && is_line_height(*line_height)))
  1167. return;
  1168. line_height_value = move(line_height);
  1169. i += 2;
  1170. }
  1171. // Consume font-family
  1172. // FIXME: Handle multiple font-families separated by commas, for fallback purposes.
  1173. if (i + 1 < parts.size()) {
  1174. auto& font_family_part = parts[i + 1];
  1175. auto font_family = Parser::parse_css_value(context, property_id, font_family_part);
  1176. if (!font_family) {
  1177. // Single-word font-families may not be quoted. We convert it to a String for convenience.
  1178. if (font_family_part.is(Token::Type::Ident))
  1179. font_family = StringStyleValue::create(font_family_part.token().ident());
  1180. else
  1181. return;
  1182. } else if (!is_font_family(*font_family)) {
  1183. dbgln("*** Unable to parse '{}' as a font-family.", font_family_part.to_debug_string());
  1184. return;
  1185. }
  1186. font_family_value = move(font_family);
  1187. }
  1188. break;
  1189. }
  1190. return;
  1191. }
  1192. if (!font_size_value || !font_family_value)
  1193. return;
  1194. style.set_property(CSS::PropertyID::FontSize, *font_size_value);
  1195. style.set_property(CSS::PropertyID::FontFamily, *font_family_value);
  1196. if (font_style_value)
  1197. style.set_property(CSS::PropertyID::FontStyle, *font_style_value);
  1198. if (font_weight_value)
  1199. style.set_property(CSS::PropertyID::FontWeight, *font_weight_value);
  1200. if (line_height_value)
  1201. style.set_property(CSS::PropertyID::LineHeight, *line_height_value);
  1202. return;
  1203. }
  1204. if (value.is_inherit()) {
  1205. style.set_property(CSS::PropertyID::FontSize, value);
  1206. style.set_property(CSS::PropertyID::FontFamily, value);
  1207. style.set_property(CSS::PropertyID::FontStyle, value);
  1208. style.set_property(CSS::PropertyID::FontVariant, value);
  1209. style.set_property(CSS::PropertyID::FontWeight, value);
  1210. style.set_property(CSS::PropertyID::LineHeight, value);
  1211. // FIXME: Implement font-stretch
  1212. return;
  1213. }
  1214. // FIXME: Handle system fonts. (caption, icon, menu, message-box, small-caption, status-bar)
  1215. return;
  1216. }
  1217. if (property_id == CSS::PropertyID::FontFamily) {
  1218. if (value.is_value_list()) {
  1219. auto parts = static_cast<CSS::ValueListStyleValue const&>(value).values();
  1220. // FIXME: Handle multiple font-families separated by commas, for fallback purposes.
  1221. for (auto& part : parts) {
  1222. auto value = Parser::parse_css_value(context, property_id, part);
  1223. if (!value)
  1224. return;
  1225. if (is_font_family(*value))
  1226. style.set_property(CSS::PropertyID::FontFamily, *value);
  1227. break;
  1228. }
  1229. return;
  1230. }
  1231. if (is_font_family(value)) {
  1232. style.set_property(CSS::PropertyID::FontFamily, value);
  1233. return;
  1234. }
  1235. return;
  1236. }
  1237. if (property_id == CSS::PropertyID::Flex) {
  1238. if (value.is_length() || (value.is_identifier() && value.to_identifier() == CSS::ValueID::Content)) {
  1239. style.set_property(CSS::PropertyID::FlexBasis, value);
  1240. return;
  1241. }
  1242. if (value.is_value_list()) {
  1243. auto parts = static_cast<CSS::ValueListStyleValue const&>(value).values();
  1244. if (parts.size() == 1) {
  1245. auto value = Parser::parse_css_value(context, property_id, parts[0]);
  1246. if (!value)
  1247. return;
  1248. if (is_flex_basis(*value)) {
  1249. style.set_property(CSS::PropertyID::FlexBasis, *value);
  1250. } else if (is_flex_grow_or_shrink(*value)) {
  1251. style.set_property(CSS::PropertyID::FlexGrow, *value);
  1252. }
  1253. return;
  1254. }
  1255. if (parts.size() == 2) {
  1256. auto flex_grow = Parser::parse_css_value(context, property_id, parts[0]);
  1257. auto second_value = Parser::parse_css_value(context, property_id, parts[1]);
  1258. if (!flex_grow || !second_value)
  1259. return;
  1260. style.set_property(CSS::PropertyID::FlexGrow, *flex_grow);
  1261. if (is_flex_basis(*second_value)) {
  1262. style.set_property(CSS::PropertyID::FlexBasis, *second_value);
  1263. } else if (is_flex_grow_or_shrink(*second_value)) {
  1264. style.set_property(CSS::PropertyID::FlexShrink, *second_value);
  1265. }
  1266. return;
  1267. }
  1268. if (parts.size() == 3) {
  1269. auto flex_grow = Parser::parse_css_value(context, property_id, parts[0]);
  1270. auto flex_shrink = Parser::parse_css_value(context, property_id, parts[1]);
  1271. auto flex_basis = Parser::parse_css_value(context, property_id, parts[2]);
  1272. if (!flex_grow || !flex_shrink || !flex_basis)
  1273. return;
  1274. style.set_property(CSS::PropertyID::FlexGrow, *flex_grow);
  1275. style.set_property(CSS::PropertyID::FlexShrink, *flex_shrink);
  1276. if (is_flex_basis(*flex_basis))
  1277. style.set_property(CSS::PropertyID::FlexBasis, *flex_basis);
  1278. return;
  1279. }
  1280. return;
  1281. }
  1282. dbgln("Unsure what to do with CSS flex value '{}'", value.to_string());
  1283. return;
  1284. }
  1285. if (property_id == CSS::PropertyID::FlexFlow) {
  1286. if (value.is_value_list()) {
  1287. auto parts = static_cast<CSS::ValueListStyleValue const&>(value).values();
  1288. if (parts.is_empty() || parts.size() > 2)
  1289. return;
  1290. RefPtr<StyleValue> flex_direction_value;
  1291. RefPtr<StyleValue> flex_wrap_value;
  1292. for (auto& part : parts) {
  1293. auto value = Parser::parse_css_value(context, property_id, part);
  1294. if (!value)
  1295. return;
  1296. if (is_flex_direction(*value)) {
  1297. if (flex_direction_value)
  1298. return;
  1299. flex_direction_value = move(value);
  1300. continue;
  1301. }
  1302. if (is_flex_wrap(*value)) {
  1303. if (flex_wrap_value)
  1304. return;
  1305. flex_wrap_value = move(value);
  1306. continue;
  1307. }
  1308. }
  1309. if (flex_direction_value)
  1310. style.set_property(CSS::PropertyID::FlexDirection, *flex_direction_value);
  1311. if (flex_wrap_value)
  1312. style.set_property(CSS::PropertyID::FlexWrap, *flex_wrap_value);
  1313. return;
  1314. }
  1315. return;
  1316. }
  1317. if (value.is_value_list()) {
  1318. dbgln("Values list for CSS property '{}' went unhandled. List: '{}'", string_from_property_id(property_id), value.to_string());
  1319. return;
  1320. }
  1321. style.set_property(property_id, value);
  1322. }
  1323. StyleResolver::CustomPropertyResolutionTuple StyleResolver::resolve_custom_property_with_specificity(DOM::Element& element, String const& custom_property_name) const
  1324. {
  1325. if (auto maybe_property = element.resolve_custom_property(custom_property_name); maybe_property.has_value())
  1326. return maybe_property.value();
  1327. auto parent_element = element.parent_element();
  1328. CustomPropertyResolutionTuple parent_resolved {};
  1329. if (parent_element)
  1330. parent_resolved = resolve_custom_property_with_specificity(*parent_element, custom_property_name);
  1331. auto matching_rules = collect_matching_rules(element);
  1332. sort_matching_rules(matching_rules);
  1333. for (int i = matching_rules.size() - 1; i >= 0; --i) {
  1334. auto& match = matching_rules[i];
  1335. if (match.specificity < parent_resolved.specificity)
  1336. continue;
  1337. auto custom_property_style = match.rule->declaration().custom_property(custom_property_name);
  1338. if (custom_property_style.has_value()) {
  1339. element.add_custom_property(custom_property_name, { custom_property_style.value(), match.specificity });
  1340. return { custom_property_style.value(), match.specificity };
  1341. }
  1342. }
  1343. return parent_resolved;
  1344. }
  1345. Optional<StyleProperty> StyleResolver::resolve_custom_property(DOM::Element& element, String const& custom_property_name) const
  1346. {
  1347. auto resolved_with_specificity = resolve_custom_property_with_specificity(element, custom_property_name);
  1348. return resolved_with_specificity.style;
  1349. }
  1350. NonnullRefPtr<StyleProperties> StyleResolver::resolve_style(DOM::Element& element) const
  1351. {
  1352. auto style = StyleProperties::create();
  1353. if (auto* parent_style = element.parent_element() ? element.parent_element()->specified_css_values() : nullptr) {
  1354. parent_style->for_each_property([&](auto property_id, auto& value) {
  1355. if (is_inherited_property(property_id))
  1356. set_property_expanding_shorthands(style, property_id, value, m_document);
  1357. });
  1358. }
  1359. element.apply_presentational_hints(*style);
  1360. auto matching_rules = collect_matching_rules(element);
  1361. sort_matching_rules(matching_rules);
  1362. for (auto& match : matching_rules) {
  1363. for (auto& property : match.rule->declaration().properties()) {
  1364. auto property_value = property.value;
  1365. if (property.value->is_custom_property()) {
  1366. auto prop = reinterpret_cast<CSS::CustomStyleValue const*>(property.value.ptr());
  1367. auto custom_prop_name = prop->custom_property_name();
  1368. auto resolved = resolve_custom_property(element, custom_prop_name);
  1369. if (resolved.has_value()) {
  1370. property_value = resolved.value().value;
  1371. }
  1372. }
  1373. set_property_expanding_shorthands(style, property.property_id, property_value, m_document);
  1374. }
  1375. }
  1376. if (auto* inline_style = element.inline_style()) {
  1377. for (auto& property : inline_style->properties()) {
  1378. set_property_expanding_shorthands(style, property.property_id, property.value, m_document);
  1379. }
  1380. }
  1381. return style;
  1382. }
  1383. }