Parser.cpp 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306
  1. /*
  2. * Copyright (c) 2020-2021, the SerenityOS developers.
  3. * Copyright (c) 2021, Sam Atkins <atkinssj@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/NonnullRefPtrVector.h>
  8. #include <AK/SourceLocation.h>
  9. #include <LibWeb/CSS/CSSStyleDeclaration.h>
  10. #include <LibWeb/CSS/CSSStyleRule.h>
  11. #include <LibWeb/CSS/CSSStyleSheet.h>
  12. #include <LibWeb/CSS/Parser/DeclarationOrAtRule.h>
  13. #include <LibWeb/CSS/Parser/Parser.h>
  14. #include <LibWeb/CSS/Parser/StyleBlockRule.h>
  15. #include <LibWeb/CSS/Parser/StyleComponentValueRule.h>
  16. #include <LibWeb/CSS/Parser/StyleFunctionRule.h>
  17. #include <LibWeb/CSS/Parser/StyleRule.h>
  18. #include <LibWeb/CSS/Selector.h>
  19. #include <LibWeb/DOM/Document.h>
  20. #include <LibWeb/Dump.h>
  21. #define CSS_PARSER_TRACE 1
  22. static void log_parse_error(const SourceLocation& location = SourceLocation::current())
  23. {
  24. dbgln_if(CSS_PARSER_TRACE, "Parse error (CSS) {}", location);
  25. }
  26. namespace Web::CSS {
  27. ParsingContext::ParsingContext()
  28. {
  29. }
  30. ParsingContext::ParsingContext(DOM::Document const& document)
  31. : m_document(&document)
  32. {
  33. }
  34. ParsingContext::ParsingContext(DOM::ParentNode const& parent_node)
  35. : m_document(&parent_node.document())
  36. {
  37. }
  38. bool ParsingContext::in_quirks_mode() const
  39. {
  40. return m_document ? m_document->in_quirks_mode() : false;
  41. }
  42. URL ParsingContext::complete_url(String const& addr) const
  43. {
  44. return m_document ? m_document->url().complete_url(addr) : URL::create_with_url_or_path(addr);
  45. }
  46. template<typename T>
  47. TokenStream<T>::TokenStream(Vector<T> const& tokens)
  48. : m_tokens(tokens)
  49. , m_eof(make_eof())
  50. {
  51. }
  52. template<typename T>
  53. TokenStream<T>::~TokenStream()
  54. {
  55. }
  56. template<typename T>
  57. bool TokenStream<T>::has_next_token()
  58. {
  59. return (size_t)(m_iterator_offset + 1) < m_tokens.size();
  60. }
  61. template<typename T>
  62. T const& TokenStream<T>::peek_token()
  63. {
  64. if (!has_next_token())
  65. return m_eof;
  66. return m_tokens.at(m_iterator_offset + 1);
  67. }
  68. template<typename T>
  69. T const& TokenStream<T>::next_token()
  70. {
  71. if (!has_next_token())
  72. return m_eof;
  73. ++m_iterator_offset;
  74. return m_tokens.at(m_iterator_offset);
  75. }
  76. template<typename T>
  77. T const& TokenStream<T>::current_token()
  78. {
  79. if ((size_t)m_iterator_offset >= m_tokens.size())
  80. return m_eof;
  81. return m_tokens.at(m_iterator_offset);
  82. }
  83. template<typename T>
  84. void TokenStream<T>::reconsume_current_input_token()
  85. {
  86. VERIFY(m_iterator_offset >= 0);
  87. --m_iterator_offset;
  88. }
  89. template<typename T>
  90. void TokenStream<T>::skip_whitespace()
  91. {
  92. while (peek_token().is(Token::Type::Whitespace))
  93. next_token();
  94. }
  95. template<>
  96. Token TokenStream<Token>::make_eof()
  97. {
  98. return Tokenizer::create_eof_token();
  99. }
  100. template<>
  101. StyleComponentValueRule TokenStream<StyleComponentValueRule>::make_eof()
  102. {
  103. return StyleComponentValueRule(Tokenizer::create_eof_token());
  104. }
  105. template<typename T>
  106. void TokenStream<T>::dump_all_tokens()
  107. {
  108. dbgln("Dumping all tokens:");
  109. for (auto& token : m_tokens)
  110. dbgln("{}", token.to_string());
  111. }
  112. Parser::Parser(ParsingContext const& context, StringView const& input, String const& encoding)
  113. : m_context(context)
  114. , m_tokenizer(input, encoding)
  115. , m_tokens(m_tokenizer.parse())
  116. , m_token_stream(TokenStream(m_tokens))
  117. {
  118. }
  119. Parser::~Parser()
  120. {
  121. }
  122. NonnullRefPtr<CSSStyleSheet> Parser::parse_as_stylesheet()
  123. {
  124. return parse_as_stylesheet(m_token_stream);
  125. }
  126. template<typename T>
  127. NonnullRefPtr<CSSStyleSheet> Parser::parse_as_stylesheet(TokenStream<T>& tokens)
  128. {
  129. auto parser_rules = consume_a_list_of_rules(tokens, true);
  130. NonnullRefPtrVector<CSSRule> rules;
  131. for (auto& raw_rule : parser_rules) {
  132. auto rule = convert_to_rule(raw_rule);
  133. if (rule)
  134. rules.append(*rule);
  135. }
  136. auto stylesheet = CSSStyleSheet::create(rules);
  137. dump_sheet(stylesheet);
  138. return stylesheet;
  139. }
  140. Vector<Selector> Parser::parse_a_selector()
  141. {
  142. return parse_a_selector(m_token_stream);
  143. }
  144. template<typename T>
  145. Vector<Selector> Parser::parse_a_selector(TokenStream<T>& tokens)
  146. {
  147. auto comma_separated_lists = parse_as_comma_separated_list_of_component_values(tokens);
  148. Vector<Selector> selectors;
  149. for (auto& selector_parts : comma_separated_lists) {
  150. auto stream = TokenStream(selector_parts);
  151. auto selector = parse_single_selector(stream);
  152. if (selector.has_value())
  153. selectors.append(selector.value());
  154. }
  155. return selectors;
  156. }
  157. Vector<Selector> Parser::parse_a_relative_selector()
  158. {
  159. return parse_a_relative_selector(m_token_stream);
  160. }
  161. template<typename T>
  162. Vector<Selector> Parser::parse_a_relative_selector(TokenStream<T>& tokens)
  163. {
  164. auto comma_separated_lists = parse_as_comma_separated_list_of_component_values(tokens);
  165. Vector<Selector> selectors;
  166. for (auto& selector_parts : comma_separated_lists) {
  167. auto stream = TokenStream(selector_parts);
  168. auto selector = parse_single_selector(stream, true);
  169. if (selector.has_value())
  170. selectors.append(selector.value());
  171. }
  172. return selectors;
  173. }
  174. template<typename T>
  175. Optional<Selector> Parser::parse_single_selector(TokenStream<T>& tokens, bool is_relative)
  176. {
  177. // FIXME: Bring this all in line with the spec. https://www.w3.org/TR/selectors-4/
  178. Vector<Selector::ComplexSelector> selectors;
  179. auto parse_simple_selector = [&]() -> Optional<Selector::SimpleSelector> {
  180. auto current_value = tokens.next_token();
  181. if (current_value.is(Token::Type::EndOfFile))
  182. return {};
  183. Selector::SimpleSelector::Type type;
  184. String value;
  185. // FIXME: Handle namespace prefixes.
  186. if (current_value.is(Token::Type::Delim) && ((Token)current_value).delim() == "*") {
  187. // FIXME: Handle selectors like `*.foo`.
  188. type = Selector::SimpleSelector::Type::Universal;
  189. Selector::SimpleSelector result;
  190. result.type = type;
  191. return result;
  192. }
  193. if (current_value.is(Token::Type::Hash)) {
  194. if (((Token)current_value).m_hash_type != Token::HashType::Id) {
  195. dbgln("Selector contains hash token that is not an id: {}", current_value.to_string());
  196. return {};
  197. }
  198. type = Selector::SimpleSelector::Type::Id;
  199. value = ((Token)current_value).m_value.to_string();
  200. } else if (current_value.is(Token::Type::Delim) && ((Token)current_value).delim() == ".") {
  201. current_value = tokens.next_token();
  202. if (current_value.is(Token::Type::EndOfFile))
  203. return {};
  204. if (!current_value.is(Token::Type::Ident)) {
  205. dbgln("Expected an ident after '.', got: {}", current_value.to_string());
  206. return {};
  207. }
  208. type = Selector::SimpleSelector::Type::Class;
  209. value = current_value.to_string();
  210. } else if (current_value.is(Token::Type::Delim) && ((Token)current_value).delim() == "*") {
  211. type = Selector::SimpleSelector::Type::Universal;
  212. } else {
  213. type = Selector::SimpleSelector::Type::TagName;
  214. value = current_value.to_string().to_lowercase();
  215. }
  216. Selector::SimpleSelector simple_selector;
  217. simple_selector.type = type;
  218. simple_selector.value = value;
  219. current_value = tokens.next_token();
  220. if (current_value.is(Token::Type::EndOfFile))
  221. return simple_selector;
  222. // FIXME: Attribute selectors want to be their own Selector::SimpleSelector::Type according to the spec.
  223. if (current_value.is_block() && current_value.block().is_square()) {
  224. Vector<StyleComponentValueRule> const& attribute_parts = current_value.block().values();
  225. if (attribute_parts.is_empty()) {
  226. dbgln("CSS attribute selector is empty!");
  227. return {};
  228. }
  229. // FIXME: Handle namespace prefix for attribute name.
  230. auto& attribute_part = attribute_parts.first();
  231. if (!attribute_part.is(Token::Type::Ident)) {
  232. dbgln("Expected ident for attribute name, got: '{}'", attribute_part.to_string());
  233. return {};
  234. }
  235. simple_selector.attribute_match_type = Selector::SimpleSelector::AttributeMatchType::HasAttribute;
  236. simple_selector.attribute_name = attribute_part.token().ident();
  237. size_t attribute_index = 0;
  238. while (attribute_parts.at(attribute_index).is(Token::Type::Whitespace)) {
  239. attribute_index++;
  240. if (attribute_index >= attribute_parts.size())
  241. return simple_selector;
  242. }
  243. auto& delim_part = attribute_parts.at(attribute_index);
  244. if (!delim_part.is(Token::Type::Delim)) {
  245. dbgln("Expected a delim for attribute comparison, got: '{}'", delim_part.to_string());
  246. return {};
  247. }
  248. if (delim_part.token().delim() == "=") {
  249. simple_selector.attribute_match_type = Selector::SimpleSelector::AttributeMatchType::ExactValueMatch;
  250. attribute_index++;
  251. } else {
  252. attribute_index++;
  253. if (attribute_index >= attribute_parts.size()) {
  254. dbgln("Attribute selector ended part way through a match type.");
  255. return {};
  256. }
  257. auto& delim_second_part = attribute_parts.at(attribute_index);
  258. if (!(delim_part.is(Token::Type::Delim) && delim_part.token().delim() == "=")) {
  259. dbgln("Expected a double delim for attribute comparison, got: '{}{}'", delim_part.to_string(), delim_second_part.to_string());
  260. return {};
  261. }
  262. if (delim_part.token().delim() == "~") {
  263. simple_selector.attribute_match_type = Selector::SimpleSelector::AttributeMatchType::ContainsWord;
  264. attribute_index++;
  265. } else if (delim_part.token().delim() == "*") {
  266. simple_selector.attribute_match_type = Selector::SimpleSelector::AttributeMatchType::ContainsString;
  267. attribute_index++;
  268. } else if (delim_part.token().delim() == "|") {
  269. simple_selector.attribute_match_type = Selector::SimpleSelector::AttributeMatchType::StartsWithSegment;
  270. attribute_index++;
  271. } else if (delim_part.token().delim() == "^") {
  272. simple_selector.attribute_match_type = Selector::SimpleSelector::AttributeMatchType::StartsWithString;
  273. attribute_index++;
  274. } else if (delim_part.token().delim() == "$") {
  275. simple_selector.attribute_match_type = Selector::SimpleSelector::AttributeMatchType::EndsWithString;
  276. attribute_index++;
  277. }
  278. }
  279. while (attribute_parts.at(attribute_index).is(Token::Type::Whitespace)) {
  280. attribute_index++;
  281. if (attribute_index >= attribute_parts.size()) {
  282. dbgln("Attribute selector ended without a value to match.");
  283. return {};
  284. }
  285. }
  286. if (attribute_index >= attribute_parts.size()) {
  287. dbgln("Attribute selector ended without a value to match.");
  288. return {};
  289. }
  290. auto& value_part = attribute_parts.at(attribute_index);
  291. if (!value_part.is(Token::Type::Ident) && !value_part.is(Token::Type::String)) {
  292. dbgln("Expected a string or ident for the value to match attribute against, got: '{}'", value_part.to_string());
  293. return {};
  294. }
  295. simple_selector.attribute_value = value_part.token().is(Token::Type::Ident) ? value_part.token().ident() : value_part.token().string();
  296. // FIXME: Handle case-sensitivity suffixes. https://www.w3.org/TR/selectors-4/#attribute-case
  297. return simple_selector;
  298. }
  299. // FIXME: Pseudo-class selectors want to be their own Selector::SimpleSelector::Type according to the spec.
  300. if (current_value.is(Token::Type::Colon)) {
  301. bool is_pseudo = false;
  302. current_value = tokens.next_token();
  303. if (current_value.is(Token::Type::EndOfFile))
  304. return {};
  305. if (current_value.is(Token::Type::Colon)) {
  306. is_pseudo = true;
  307. current_value = tokens.next_token();
  308. if (current_value.is(Token::Type::EndOfFile))
  309. return {};
  310. }
  311. // Ignore for now, otherwise we produce a "false positive" selector
  312. // and apply styles to the element itself, not its pseudo element
  313. if (is_pseudo)
  314. return {};
  315. current_value = tokens.next_token();
  316. if (current_value.is(Token::Type::EndOfFile))
  317. return simple_selector;
  318. if (current_value.is(Token::Type::Ident)) {
  319. auto pseudo_name = ((Token)current_value).ident();
  320. if (pseudo_name.equals_ignoring_case("link")) {
  321. simple_selector.pseudo_class = Selector::SimpleSelector::PseudoClass::Link;
  322. } else if (pseudo_name.equals_ignoring_case("visited")) {
  323. simple_selector.pseudo_class = Selector::SimpleSelector::PseudoClass::Visited;
  324. } else if (pseudo_name.equals_ignoring_case("active")) {
  325. simple_selector.pseudo_class = Selector::SimpleSelector::PseudoClass::Active;
  326. } else if (pseudo_name.equals_ignoring_case("hover")) {
  327. simple_selector.pseudo_class = Selector::SimpleSelector::PseudoClass::Hover;
  328. } else if (pseudo_name.equals_ignoring_case("focus")) {
  329. simple_selector.pseudo_class = Selector::SimpleSelector::PseudoClass::Focus;
  330. } else if (pseudo_name.equals_ignoring_case("first-child")) {
  331. simple_selector.pseudo_class = Selector::SimpleSelector::PseudoClass::FirstChild;
  332. } else if (pseudo_name.equals_ignoring_case("last-child")) {
  333. simple_selector.pseudo_class = Selector::SimpleSelector::PseudoClass::LastChild;
  334. } else if (pseudo_name.equals_ignoring_case("only-child")) {
  335. simple_selector.pseudo_class = Selector::SimpleSelector::PseudoClass::OnlyChild;
  336. } else if (pseudo_name.equals_ignoring_case("empty")) {
  337. simple_selector.pseudo_class = Selector::SimpleSelector::PseudoClass::Empty;
  338. } else if (pseudo_name.equals_ignoring_case("root")) {
  339. simple_selector.pseudo_class = Selector::SimpleSelector::PseudoClass::Root;
  340. } else if (pseudo_name.equals_ignoring_case("first-of-type")) {
  341. simple_selector.pseudo_class = Selector::SimpleSelector::PseudoClass::FirstOfType;
  342. } else if (pseudo_name.equals_ignoring_case("last-of-type")) {
  343. simple_selector.pseudo_class = Selector::SimpleSelector::PseudoClass::LastOfType;
  344. } else if (pseudo_name.equals_ignoring_case("before")) {
  345. simple_selector.pseudo_element = Selector::SimpleSelector::PseudoElement::Before;
  346. } else if (pseudo_name.equals_ignoring_case("after")) {
  347. simple_selector.pseudo_element = Selector::SimpleSelector::PseudoElement::After;
  348. } else if (pseudo_name.equals_ignoring_case("disabled")) {
  349. simple_selector.pseudo_class = Selector::SimpleSelector::PseudoClass::Disabled;
  350. } else if (pseudo_name.equals_ignoring_case("enabled")) {
  351. simple_selector.pseudo_class = Selector::SimpleSelector::PseudoClass::Enabled;
  352. } else if (pseudo_name.equals_ignoring_case("checked")) {
  353. simple_selector.pseudo_class = Selector::SimpleSelector::PseudoClass::Checked;
  354. } else {
  355. dbgln("Unknown pseudo class: '{}'", pseudo_name);
  356. return simple_selector;
  357. }
  358. } else if (current_value.is(Token::Type::Function)) {
  359. auto& pseudo_function = current_value.function();
  360. if (pseudo_function.name().equals_ignoring_case("nth-child")) {
  361. simple_selector.pseudo_class = Selector::SimpleSelector::PseudoClass::NthChild;
  362. simple_selector.nth_child_pattern = Selector::SimpleSelector::NthChildPattern::parse(pseudo_function.values_as_string());
  363. } else if (pseudo_function.name().equals_ignoring_case("nth-last-child")) {
  364. simple_selector.pseudo_class = Selector::SimpleSelector::PseudoClass::NthLastChild;
  365. simple_selector.nth_child_pattern = Selector::SimpleSelector::NthChildPattern::parse(pseudo_function.values_as_string());
  366. } else if (pseudo_function.name().equals_ignoring_case("not")) {
  367. simple_selector.pseudo_class = Selector::SimpleSelector::PseudoClass::Not;
  368. simple_selector.not_selector = pseudo_function.values_as_string();
  369. } else {
  370. dbgln("Unknown pseudo class: '{}'()", pseudo_function.name());
  371. return simple_selector;
  372. }
  373. } else {
  374. dbgln("Unexpected Block in pseudo-class name, expected a function or identifier. '{}'", current_value.to_string());
  375. return simple_selector;
  376. }
  377. }
  378. return simple_selector;
  379. };
  380. auto parse_complex_selector = [&]() -> Optional<Selector::ComplexSelector> {
  381. auto relation = Selector::ComplexSelector::Relation::Descendant;
  382. auto current_value = tokens.peek_token();
  383. if (current_value.is(Token::Type::Delim)) {
  384. auto delim = ((Token)current_value).delim();
  385. if (delim == ">") {
  386. relation = Selector::ComplexSelector::Relation::ImmediateChild;
  387. tokens.next_token();
  388. } else if (delim == "+") {
  389. relation = Selector::ComplexSelector::Relation::AdjacentSibling;
  390. tokens.next_token();
  391. } else if (delim == "~") {
  392. relation = Selector::ComplexSelector::Relation::GeneralSibling;
  393. tokens.next_token();
  394. } else if (delim == "|") {
  395. tokens.next_token();
  396. auto next = tokens.peek_token();
  397. if (next.is(Token::Type::EndOfFile))
  398. return {};
  399. if (next.is(Token::Type::Delim) && next.token().delim() == "|") {
  400. relation = Selector::ComplexSelector::Relation::Column;
  401. tokens.next_token();
  402. }
  403. }
  404. }
  405. Vector<Selector::SimpleSelector> simple_selectors;
  406. for (;;) {
  407. auto component = parse_simple_selector();
  408. if (!component.has_value())
  409. break;
  410. simple_selectors.append(component.value());
  411. }
  412. if (simple_selectors.is_empty())
  413. return {};
  414. return Selector::ComplexSelector { relation, move(simple_selectors) };
  415. };
  416. for (;;) {
  417. auto complex = parse_complex_selector();
  418. if (complex.has_value())
  419. selectors.append(complex.value());
  420. auto current_value = tokens.peek_token();
  421. if (current_value.is(Token::Type::EndOfFile))
  422. break;
  423. if (current_value.is(Token::Type::Comma))
  424. break;
  425. tokens.next_token();
  426. }
  427. if (selectors.is_empty())
  428. return {};
  429. if (!is_relative)
  430. selectors.first().relation = Selector::ComplexSelector::Relation::None;
  431. return Selector(move(selectors));
  432. }
  433. NonnullRefPtrVector<StyleRule> Parser::consume_a_list_of_rules(bool top_level)
  434. {
  435. return consume_a_list_of_rules(m_token_stream, top_level);
  436. }
  437. template<typename T>
  438. NonnullRefPtrVector<StyleRule> Parser::consume_a_list_of_rules(TokenStream<T>& tokens, bool top_level)
  439. {
  440. NonnullRefPtrVector<StyleRule> rules;
  441. for (;;) {
  442. auto token = tokens.next_token();
  443. if (token.is(Token::Type::Whitespace)) {
  444. continue;
  445. }
  446. if (token.is(Token::Type::EndOfFile)) {
  447. break;
  448. }
  449. if (token.is(Token::Type::CDO) || token.is(Token::Type::CDC)) {
  450. if (top_level) {
  451. continue;
  452. }
  453. tokens.reconsume_current_input_token();
  454. auto maybe_qualified = consume_a_qualified_rule(tokens);
  455. if (maybe_qualified) {
  456. rules.append(maybe_qualified.release_nonnull());
  457. }
  458. continue;
  459. }
  460. if (token.is(Token::Type::AtKeyword)) {
  461. tokens.reconsume_current_input_token();
  462. rules.append(consume_an_at_rule(tokens));
  463. continue;
  464. }
  465. tokens.reconsume_current_input_token();
  466. auto maybe_qualified = consume_a_qualified_rule(tokens);
  467. if (maybe_qualified) {
  468. rules.append(maybe_qualified.release_nonnull());
  469. }
  470. }
  471. return rules;
  472. }
  473. NonnullRefPtr<StyleRule> Parser::consume_an_at_rule()
  474. {
  475. return consume_an_at_rule(m_token_stream);
  476. }
  477. template<typename T>
  478. NonnullRefPtr<StyleRule> Parser::consume_an_at_rule(TokenStream<T>& tokens)
  479. {
  480. auto name_ident = tokens.next_token();
  481. VERIFY(name_ident.is(Token::Type::Ident));
  482. NonnullRefPtr<StyleRule> rule = create<StyleRule>(StyleRule::Type::At);
  483. rule->m_name = ((Token)name_ident).ident();
  484. for (;;) {
  485. auto token = tokens.next_token();
  486. if (token.is(Token::Type::Semicolon)) {
  487. return rule;
  488. }
  489. if (token.is(Token::Type::EndOfFile)) {
  490. log_parse_error();
  491. return rule;
  492. }
  493. if (token.is(Token::Type::OpenCurly)) {
  494. rule->m_block = consume_a_simple_block(tokens);
  495. return rule;
  496. }
  497. // how is "simple block with an associated token of <{-token>" a valid token?
  498. tokens.reconsume_current_input_token();
  499. auto value = consume_a_component_value(tokens);
  500. rule->m_prelude.append(value);
  501. }
  502. }
  503. RefPtr<StyleRule> Parser::consume_a_qualified_rule()
  504. {
  505. return consume_a_qualified_rule(m_token_stream);
  506. }
  507. template<typename T>
  508. RefPtr<StyleRule> Parser::consume_a_qualified_rule(TokenStream<T>& tokens)
  509. {
  510. NonnullRefPtr<StyleRule> rule = create<StyleRule>(StyleRule::Type::Qualified);
  511. for (;;) {
  512. auto token = tokens.next_token();
  513. if (token.is(Token::Type::EndOfFile)) {
  514. log_parse_error();
  515. return {};
  516. }
  517. if (token.is(Token::Type::OpenCurly)) {
  518. rule->m_block = consume_a_simple_block(tokens);
  519. return rule;
  520. }
  521. // how is "simple block with an associated token of <{-token>" a valid token?
  522. tokens.reconsume_current_input_token();
  523. auto value = consume_a_component_value(tokens);
  524. rule->m_prelude.append(value);
  525. }
  526. return rule;
  527. }
  528. template<>
  529. StyleComponentValueRule Parser::consume_a_component_value(TokenStream<StyleComponentValueRule>& tokens)
  530. {
  531. return tokens.next_token();
  532. }
  533. template<typename T>
  534. StyleComponentValueRule Parser::consume_a_component_value(TokenStream<T>& tokens)
  535. {
  536. auto token = tokens.next_token();
  537. if (token.is(Token::Type::OpenCurly) || token.is(Token::Type::OpenSquare) || token.is(Token::Type::OpenParen))
  538. return StyleComponentValueRule(consume_a_simple_block(tokens));
  539. if (token.is(Token::Type::Function))
  540. return StyleComponentValueRule(consume_a_function(tokens));
  541. return StyleComponentValueRule(token);
  542. }
  543. StyleComponentValueRule Parser::consume_a_component_value()
  544. {
  545. return consume_a_component_value(m_token_stream);
  546. }
  547. NonnullRefPtr<StyleBlockRule> Parser::consume_a_simple_block()
  548. {
  549. return consume_a_simple_block(m_token_stream);
  550. }
  551. template<typename T>
  552. NonnullRefPtr<StyleBlockRule> Parser::consume_a_simple_block(TokenStream<T>& tokens)
  553. {
  554. auto ending_token = ((Token)tokens.current_token()).mirror_variant();
  555. NonnullRefPtr<StyleBlockRule> block = create<StyleBlockRule>();
  556. block->m_token = tokens.current_token();
  557. for (;;) {
  558. auto token = tokens.next_token();
  559. if (token.is(ending_token)) {
  560. return block;
  561. }
  562. if (token.is(Token::Type::EndOfFile)) {
  563. log_parse_error();
  564. return block;
  565. }
  566. tokens.reconsume_current_input_token();
  567. auto value = consume_a_component_value(tokens);
  568. if (value.is(Token::Type::Whitespace))
  569. continue;
  570. block->m_values.append(value);
  571. }
  572. }
  573. NonnullRefPtr<StyleFunctionRule> Parser::consume_a_function()
  574. {
  575. return consume_a_function(m_token_stream);
  576. }
  577. template<typename T>
  578. NonnullRefPtr<StyleFunctionRule> Parser::consume_a_function(TokenStream<T>& tokens)
  579. {
  580. auto name_ident = tokens.current_token();
  581. VERIFY(name_ident.is(Token::Type::Function));
  582. NonnullRefPtr<StyleFunctionRule> function = create<StyleFunctionRule>(((Token)name_ident).m_value.to_string());
  583. for (;;) {
  584. auto token = tokens.next_token();
  585. if (token.is(Token::Type::CloseParen)) {
  586. return function;
  587. }
  588. if (token.is(Token::Type::EndOfFile)) {
  589. log_parse_error();
  590. return function;
  591. }
  592. tokens.reconsume_current_input_token();
  593. auto value = consume_a_component_value(tokens);
  594. if (value.is(Token::Type::Whitespace))
  595. continue;
  596. function->m_values.append(value.to_string());
  597. }
  598. return function;
  599. }
  600. Optional<StyleDeclarationRule> Parser::consume_a_declaration()
  601. {
  602. return consume_a_declaration(m_token_stream);
  603. }
  604. template<typename T>
  605. Optional<StyleDeclarationRule> Parser::consume_a_declaration(TokenStream<T>& tokens)
  606. {
  607. auto token = tokens.next_token();
  608. StyleDeclarationRule declaration;
  609. VERIFY(token.is(Token::Type::Ident));
  610. declaration.m_name = ((Token)token).ident();
  611. tokens.skip_whitespace();
  612. auto colon = tokens.next_token();
  613. if (!colon.is(Token::Type::Colon)) {
  614. log_parse_error();
  615. return {};
  616. }
  617. tokens.skip_whitespace();
  618. for (;;) {
  619. if (tokens.peek_token().is(Token::Type::EndOfFile)) {
  620. break;
  621. }
  622. declaration.m_values.append(consume_a_component_value(tokens));
  623. }
  624. if (declaration.m_values.size() >= 2) {
  625. auto second_last = declaration.m_values.at(declaration.m_values.size() - 2);
  626. auto last = declaration.m_values.at(declaration.m_values.size() - 1);
  627. if (second_last.m_type == StyleComponentValueRule::ComponentType::Token && last.m_type == StyleComponentValueRule::ComponentType::Token) {
  628. auto last_token = last.m_token;
  629. auto second_last_token = second_last.m_token;
  630. if (second_last_token.is(Token::Type::Delim) && second_last_token.m_value.to_string().equals_ignoring_case("!")) {
  631. if (last_token.is(Token::Type::Ident) && last_token.m_value.to_string().equals_ignoring_case("important")) {
  632. declaration.m_values.remove(declaration.m_values.size() - 2);
  633. declaration.m_values.remove(declaration.m_values.size() - 1);
  634. declaration.m_important = true;
  635. }
  636. }
  637. }
  638. }
  639. for (;;) {
  640. auto maybe_whitespace = declaration.m_values.at(declaration.m_values.size() - 1);
  641. if (!(maybe_whitespace.is(Token::Type::Whitespace))) {
  642. break;
  643. }
  644. declaration.m_values.remove(declaration.m_values.size() - 1);
  645. }
  646. return declaration;
  647. }
  648. Vector<DeclarationOrAtRule> Parser::consume_a_list_of_declarations()
  649. {
  650. return consume_a_list_of_declarations(m_token_stream);
  651. }
  652. template<typename T>
  653. Vector<DeclarationOrAtRule> Parser::consume_a_list_of_declarations(TokenStream<T>& tokens)
  654. {
  655. Vector<DeclarationOrAtRule> list;
  656. for (;;) {
  657. auto token = tokens.next_token();
  658. if (token.is(Token::Type::Whitespace) || token.is(Token::Type::Semicolon)) {
  659. continue;
  660. }
  661. if (token.is(Token::Type::EndOfFile)) {
  662. return list;
  663. }
  664. if (token.is(Token::Type::AtKeyword)) {
  665. tokens.reconsume_current_input_token();
  666. list.append(DeclarationOrAtRule(consume_an_at_rule(tokens)));
  667. continue;
  668. }
  669. if (token.is(Token::Type::Ident)) {
  670. Vector<StyleComponentValueRule> temp;
  671. temp.append(token);
  672. for (;;) {
  673. auto peek = tokens.peek_token();
  674. if (peek.is(Token::Type::Semicolon) || peek.is(Token::Type::EndOfFile)) {
  675. break;
  676. }
  677. temp.append(consume_a_component_value(tokens));
  678. }
  679. auto token_stream = TokenStream(temp);
  680. auto maybe_declaration = consume_a_declaration(token_stream);
  681. if (maybe_declaration.has_value()) {
  682. list.append(DeclarationOrAtRule(maybe_declaration.value()));
  683. }
  684. continue;
  685. }
  686. log_parse_error();
  687. tokens.reconsume_current_input_token();
  688. auto peek = tokens.peek_token();
  689. if (!(peek.is(Token::Type::Semicolon) || peek.is(Token::Type::EndOfFile))) {
  690. (void)consume_a_component_value(tokens);
  691. }
  692. }
  693. return list;
  694. }
  695. RefPtr<CSSRule> Parser::parse_as_rule()
  696. {
  697. return parse_as_rule(m_token_stream);
  698. }
  699. template<typename T>
  700. RefPtr<CSSRule> Parser::parse_as_rule(TokenStream<T>& tokens)
  701. {
  702. RefPtr<CSSRule> rule;
  703. tokens.skip_whitespace();
  704. auto token = tokens.peek_token();
  705. if (token.is(Token::Type::EndOfFile)) {
  706. return {};
  707. } else if (token.is(Token::Type::AtKeyword)) {
  708. auto at_rule = consume_an_at_rule();
  709. rule = convert_to_rule(at_rule);
  710. } else {
  711. auto qualified_rule = consume_a_qualified_rule(tokens);
  712. if (!qualified_rule)
  713. return {};
  714. rule = convert_to_rule(*qualified_rule);
  715. }
  716. tokens.skip_whitespace();
  717. auto maybe_eof = tokens.peek_token();
  718. if (maybe_eof.is(Token::Type::EndOfFile)) {
  719. return rule;
  720. }
  721. return {};
  722. }
  723. NonnullRefPtrVector<CSSRule> Parser::parse_as_list_of_rules()
  724. {
  725. return parse_as_list_of_rules(m_token_stream);
  726. }
  727. template<typename T>
  728. NonnullRefPtrVector<CSSRule> Parser::parse_as_list_of_rules(TokenStream<T>& tokens)
  729. {
  730. auto parsed_rules = consume_a_list_of_rules(tokens, false);
  731. NonnullRefPtrVector<CSSRule> rules;
  732. for (auto& rule : parsed_rules) {
  733. auto converted_rule = convert_to_rule(rule);
  734. if (converted_rule)
  735. rules.append(*converted_rule);
  736. }
  737. return rules;
  738. }
  739. Optional<StyleProperty> Parser::parse_as_declaration()
  740. {
  741. return parse_as_declaration(m_token_stream);
  742. }
  743. template<typename T>
  744. Optional<StyleProperty> Parser::parse_as_declaration(TokenStream<T>& tokens)
  745. {
  746. tokens.skip_whitespace();
  747. auto token = tokens.peek_token();
  748. if (!token.is(Token::Type::Ident)) {
  749. return {};
  750. }
  751. auto declaration = consume_a_declaration(tokens);
  752. // FIXME: Return declaration
  753. return {};
  754. }
  755. RefPtr<CSSStyleDeclaration> Parser::parse_as_list_of_declarations()
  756. {
  757. return parse_as_list_of_declarations(m_token_stream);
  758. }
  759. template<typename T>
  760. RefPtr<CSSStyleDeclaration> Parser::parse_as_list_of_declarations(TokenStream<T>&)
  761. {
  762. // FIXME: Return the declarations.
  763. return {};
  764. }
  765. Optional<StyleComponentValueRule> Parser::parse_as_component_value()
  766. {
  767. return parse_as_component_value(m_token_stream);
  768. }
  769. template<typename T>
  770. Optional<StyleComponentValueRule> Parser::parse_as_component_value(TokenStream<T>& tokens)
  771. {
  772. tokens.skip_whitespace();
  773. auto token = tokens.peek_token();
  774. if (token.is(Token::Type::EndOfFile)) {
  775. return {};
  776. }
  777. auto value = consume_a_component_value(tokens);
  778. tokens.skip_whitespace();
  779. auto maybe_eof = tokens.peek_token();
  780. if (maybe_eof.is(Token::Type::EndOfFile)) {
  781. return value;
  782. }
  783. return {};
  784. }
  785. Vector<StyleComponentValueRule> Parser::parse_as_list_of_component_values()
  786. {
  787. return parse_as_list_of_component_values(m_token_stream);
  788. }
  789. template<typename T>
  790. Vector<StyleComponentValueRule> Parser::parse_as_list_of_component_values(TokenStream<T>& tokens)
  791. {
  792. Vector<StyleComponentValueRule> rules;
  793. for (;;) {
  794. if (tokens.peek_token().is(Token::Type::EndOfFile)) {
  795. break;
  796. }
  797. rules.append(consume_a_component_value(tokens));
  798. }
  799. return rules;
  800. }
  801. Vector<Vector<StyleComponentValueRule>> Parser::parse_as_comma_separated_list_of_component_values()
  802. {
  803. return parse_as_comma_separated_list_of_component_values(m_token_stream);
  804. }
  805. template<typename T>
  806. Vector<Vector<StyleComponentValueRule>> Parser::parse_as_comma_separated_list_of_component_values(TokenStream<T>& tokens)
  807. {
  808. Vector<Vector<StyleComponentValueRule>> lists;
  809. lists.append({});
  810. for (;;) {
  811. auto next = tokens.next_token();
  812. if (next.is(Token::Type::Comma)) {
  813. lists.append({});
  814. continue;
  815. } else if (next.is(Token::Type::EndOfFile)) {
  816. break;
  817. }
  818. tokens.reconsume_current_input_token();
  819. auto component_value = consume_a_component_value(tokens);
  820. lists.last().append(component_value);
  821. }
  822. return lists;
  823. }
  824. RefPtr<CSSRule> Parser::convert_to_rule(NonnullRefPtr<StyleRule> rule)
  825. {
  826. dbgln("Converting a rule: {}", rule->to_string());
  827. if (rule->m_type == StyleRule::Type::At) {
  828. dbgln("... It's an at rule");
  829. } else {
  830. dbgln("... It's a style rule");
  831. auto prelude_stream = TokenStream(rule->m_prelude);
  832. Vector<Selector> selectors = parse_a_selector(prelude_stream);
  833. auto declaration = convert_to_declaration(*rule->m_block);
  834. if (declaration && !selectors.is_empty())
  835. return CSSStyleRule::create(move(selectors), move(*declaration));
  836. }
  837. dbgln("... discarding because it's invalid or unsupported.");
  838. return {};
  839. }
  840. RefPtr<CSSStyleDeclaration> Parser::convert_to_declaration(NonnullRefPtr<StyleBlockRule> block)
  841. {
  842. if (!block->is_curly())
  843. return {};
  844. Vector<StyleProperty> properties;
  845. HashMap<String, StyleProperty> custom_properties;
  846. auto stream = TokenStream(block->m_values);
  847. auto declarations_and_at_rules = consume_a_list_of_declarations(stream);
  848. for (auto& declaration_or_at_rule : declarations_and_at_rules) {
  849. if (declaration_or_at_rule.is_at_rule()) {
  850. dbgln("CSS::Parser::convert_to_declaration(): Skipping @ rule.");
  851. continue;
  852. }
  853. auto& declaration = declaration_or_at_rule.m_declaration;
  854. auto& property_name = declaration.m_name;
  855. auto property_id = property_id_from_string(property_name);
  856. if (property_id == PropertyID::Invalid && property_name.starts_with("--"))
  857. property_id = PropertyID::Custom;
  858. if (property_id == PropertyID::Invalid && !property_name.starts_with("-")) {
  859. dbgln("CSS::Parser::convert_to_declaration(): Unrecognized property '{}'", property_name);
  860. continue;
  861. }
  862. auto value_token_stream = TokenStream(declaration.m_values);
  863. auto value = parse_css_value(property_id, value_token_stream);
  864. if (!value) {
  865. dbgln("CSS::Parser::convert_to_declaration(): Property '{}' has no value.", property_name);
  866. continue;
  867. }
  868. if (property_id == PropertyID::Custom) {
  869. custom_properties.set(property_name, StyleProperty { property_id, value.release_nonnull(), declaration.m_name, declaration.m_important });
  870. } else {
  871. properties.append(StyleProperty { property_id, value.release_nonnull(), {}, declaration.m_important });
  872. }
  873. }
  874. return CSSStyleDeclaration::create(move(properties), move(custom_properties));
  875. }
  876. Optional<float> Parser::try_parse_float(StringView string)
  877. {
  878. // FIXME: This is copied from DeprecatedCSSParser, so may not be to spec.
  879. const char* str = string.characters_without_null_termination();
  880. size_t len = string.length();
  881. size_t weight = 1;
  882. int exp_val = 0;
  883. float value = 0.0f;
  884. float fraction = 0.0f;
  885. bool has_sign = false;
  886. bool is_negative = false;
  887. bool is_fractional = false;
  888. bool is_scientific = false;
  889. if (str[0] == '-') {
  890. is_negative = true;
  891. has_sign = true;
  892. }
  893. if (str[0] == '+') {
  894. has_sign = true;
  895. }
  896. for (size_t i = has_sign; i < len; i++) {
  897. // Looks like we're about to start working on the fractional part
  898. if (str[i] == '.') {
  899. is_fractional = true;
  900. continue;
  901. }
  902. if (str[i] == 'e' || str[i] == 'E') {
  903. if (str[i + 1] == '-' || str[i + 1] == '+')
  904. exp_val = atoi(str + i + 2);
  905. else
  906. exp_val = atoi(str + i + 1);
  907. is_scientific = true;
  908. continue;
  909. }
  910. if (str[i] < '0' || str[i] > '9' || exp_val != 0) {
  911. return {};
  912. continue;
  913. }
  914. if (is_fractional) {
  915. fraction *= 10;
  916. fraction += str[i] - '0';
  917. weight *= 10;
  918. } else {
  919. value = value * 10;
  920. value += str[i] - '0';
  921. }
  922. }
  923. fraction /= weight;
  924. value += fraction;
  925. if (is_scientific) {
  926. bool divide = exp_val < 0;
  927. if (divide)
  928. exp_val *= -1;
  929. for (int i = 0; i < exp_val; i++) {
  930. if (divide)
  931. value /= 10;
  932. else
  933. value *= 10;
  934. }
  935. }
  936. return is_negative ? -value : value;
  937. }
  938. RefPtr<StyleValue> Parser::parse_css_value(PropertyID property_id, TokenStream<StyleComponentValueRule>& tokens)
  939. {
  940. // FIXME: This is mostly copied from the old, deprecated parser. It is probably not to spec.
  941. auto takes_integer_value = [](PropertyID property_id) -> bool {
  942. return property_id == PropertyID::ZIndex
  943. || property_id == PropertyID::FontWeight
  944. || property_id == PropertyID::Custom;
  945. };
  946. auto parse_length = [&]() -> Optional<Length> {
  947. Length::Type type = Length::Type::Undefined;
  948. Optional<float> numeric_value;
  949. auto token = tokens.next_token();
  950. if (token.is(Token::Type::Dimension)) {
  951. auto length_string = token.token().m_value.string_view();
  952. auto unit_string = token.token().m_unit.string_view();
  953. if (unit_string.equals_ignoring_case("%")) {
  954. type = Length::Type::Percentage;
  955. } else if (unit_string.equals_ignoring_case("px")) {
  956. type = Length::Type::Px;
  957. } else if (unit_string.equals_ignoring_case("pt")) {
  958. type = Length::Type::Pt;
  959. } else if (unit_string.equals_ignoring_case("pc")) {
  960. type = Length::Type::Pc;
  961. } else if (unit_string.equals_ignoring_case("mm")) {
  962. type = Length::Type::Mm;
  963. } else if (unit_string.equals_ignoring_case("rem")) {
  964. type = Length::Type::Rem;
  965. } else if (unit_string.equals_ignoring_case("em")) {
  966. type = Length::Type::Em;
  967. } else if (unit_string.equals_ignoring_case("ex")) {
  968. type = Length::Type::Ex;
  969. } else if (unit_string.equals_ignoring_case("vw")) {
  970. type = Length::Type::Vw;
  971. } else if (unit_string.equals_ignoring_case("vh")) {
  972. type = Length::Type::Vh;
  973. } else if (unit_string.equals_ignoring_case("vmax")) {
  974. type = Length::Type::Vmax;
  975. } else if (unit_string.equals_ignoring_case("vmin")) {
  976. type = Length::Type::Vmin;
  977. } else if (unit_string.equals_ignoring_case("cm")) {
  978. type = Length::Type::Cm;
  979. } else if (unit_string.equals_ignoring_case("in")) {
  980. type = Length::Type::In;
  981. } else if (unit_string.equals_ignoring_case("Q")) {
  982. type = Length::Type::Q;
  983. } else if (m_context.in_quirks_mode()) {
  984. type = Length::Type::Px;
  985. }
  986. numeric_value = try_parse_float(length_string);
  987. } else if (token.is(Token::Type::Number)) {
  988. auto value_string = token.token().m_value.string_view();
  989. if (value_string == "0") {
  990. type = Length::Type::Px;
  991. numeric_value = 0;
  992. } else if (m_context.in_quirks_mode()) {
  993. type = Length::Type::Px;
  994. numeric_value = try_parse_float(value_string);
  995. }
  996. }
  997. if (!numeric_value.has_value())
  998. return {};
  999. return Length(numeric_value.value(), type);
  1000. };
  1001. auto token = tokens.next_token();
  1002. if (takes_integer_value(property_id) && token.is(Token::Type::Number)) {
  1003. auto number = token.token();
  1004. if (number.m_number_type == Token::NumberType::Integer) {
  1005. return LengthStyleValue::create(Length::make_px(number.integer()));
  1006. }
  1007. }
  1008. if (token.is(Token::Type::Dimension) || token.is(Token::Type::Number)) {
  1009. tokens.reconsume_current_input_token();
  1010. auto length = parse_length();
  1011. if (length.has_value())
  1012. return LengthStyleValue::create(length.value());
  1013. auto value_string = token.token().m_value.string_view();
  1014. auto float_number = try_parse_float(value_string);
  1015. if (float_number.has_value())
  1016. return NumericStyleValue::create(float_number.value());
  1017. return nullptr;
  1018. }
  1019. if (token.is(Token::Type::Ident)) {
  1020. auto ident = token.token().ident();
  1021. if (ident.equals_ignoring_case("inherit"))
  1022. return InheritStyleValue::create();
  1023. if (ident.equals_ignoring_case("initial"))
  1024. return InitialStyleValue::create();
  1025. if (ident.equals_ignoring_case("auto"))
  1026. return LengthStyleValue::create(Length::make_auto());
  1027. }
  1028. if (token.is_function() && token.function().name().equals_ignoring_case("var")) {
  1029. // FIXME: Handle fallback value as second parameter
  1030. // https://www.w3.org/TR/css-variables-1/#using-variables
  1031. if (!token.function().values().is_empty()) {
  1032. auto& property_name = token.function().values().first();
  1033. return CustomStyleValue::create(property_name);
  1034. }
  1035. }
  1036. if (token.is(Token::Type::Ident)) {
  1037. auto value_id = value_id_from_string(token.token().ident());
  1038. if (value_id != ValueID::Invalid)
  1039. return IdentifierStyleValue::create(value_id);
  1040. }
  1041. auto parse_css_color = [&]() -> Optional<Color> {
  1042. if (token.is(Token::Type::Ident) && token.token().ident().equals_ignoring_case("transparent"))
  1043. return Color::from_rgba(0x00000000);
  1044. // FIXME: Handle all the different color notations.
  1045. // https://www.w3.org/TR/css-color-3/
  1046. // Right now, this uses non-CSS-specific parsing, and assumes the whole color value is one token,
  1047. // which is isn't if it's a function-style syntax.
  1048. auto color = Color::from_string(token.to_string().to_lowercase());
  1049. if (color.has_value())
  1050. return color;
  1051. return {};
  1052. };
  1053. auto color = parse_css_color();
  1054. if (color.has_value())
  1055. return ColorStyleValue::create(color.value());
  1056. if (token.is(Token::Type::String))
  1057. return StringStyleValue::create(token.token().string());
  1058. return {};
  1059. }
  1060. }