DeprecatedCSSParser.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/HashMap.h>
  7. #include <AK/SourceLocation.h>
  8. #include <LibWeb/CSS/CSSImportRule.h>
  9. #include <LibWeb/CSS/CSSRule.h>
  10. #include <LibWeb/CSS/CSSStyleRule.h>
  11. #include <LibWeb/CSS/Parser/DeprecatedCSSParser.h>
  12. #include <LibWeb/CSS/PropertyID.h>
  13. #include <LibWeb/CSS/Selector.h>
  14. #include <LibWeb/DOM/Document.h>
  15. #include <ctype.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #define PARSE_VERIFY(x) \
  19. if (!(x)) { \
  20. dbgln("CSS PARSER ASSERTION FAILED: {}", #x); \
  21. dbgln("At character# {} in CSS: _{}_", index, css); \
  22. VERIFY_NOT_REACHED(); \
  23. }
  24. static inline void log_parse_error(const SourceLocation& location = SourceLocation::current())
  25. {
  26. dbgln("CSS Parse error! {}", location);
  27. }
  28. namespace Web {
  29. namespace CSS {
  30. ParsingContext::ParsingContext()
  31. {
  32. }
  33. ParsingContext::ParsingContext(const DOM::Document& document)
  34. : m_document(&document)
  35. {
  36. }
  37. ParsingContext::ParsingContext(const DOM::ParentNode& parent_node)
  38. : m_document(&parent_node.document())
  39. {
  40. }
  41. bool ParsingContext::in_quirks_mode() const
  42. {
  43. return m_document ? m_document->in_quirks_mode() : false;
  44. }
  45. URL ParsingContext::complete_url(const String& addr) const
  46. {
  47. return m_document ? m_document->url().complete_url(addr) : URL::create_with_url_or_path(addr);
  48. }
  49. }
  50. static Optional<Color> parse_css_color(const CSS::ParsingContext&, const StringView& view)
  51. {
  52. if (view.equals_ignoring_case("transparent"))
  53. return Color::from_rgba(0x00000000);
  54. auto color = Color::from_string(view.to_string().to_lowercase());
  55. if (color.has_value())
  56. return color;
  57. return {};
  58. }
  59. static Optional<float> try_parse_float(const StringView& string)
  60. {
  61. const char* str = string.characters_without_null_termination();
  62. size_t len = string.length();
  63. size_t weight = 1;
  64. int exp_val = 0;
  65. float value = 0.0f;
  66. float fraction = 0.0f;
  67. bool has_sign = false;
  68. bool is_negative = false;
  69. bool is_fractional = false;
  70. bool is_scientific = false;
  71. if (str[0] == '-') {
  72. is_negative = true;
  73. has_sign = true;
  74. }
  75. if (str[0] == '+') {
  76. has_sign = true;
  77. }
  78. for (size_t i = has_sign; i < len; i++) {
  79. // Looks like we're about to start working on the fractional part
  80. if (str[i] == '.') {
  81. is_fractional = true;
  82. continue;
  83. }
  84. if (str[i] == 'e' || str[i] == 'E') {
  85. if (str[i + 1] == '-' || str[i + 1] == '+')
  86. exp_val = atoi(str + i + 2);
  87. else
  88. exp_val = atoi(str + i + 1);
  89. is_scientific = true;
  90. continue;
  91. }
  92. if (str[i] < '0' || str[i] > '9' || exp_val != 0) {
  93. return {};
  94. continue;
  95. }
  96. if (is_fractional) {
  97. fraction *= 10;
  98. fraction += str[i] - '0';
  99. weight *= 10;
  100. } else {
  101. value = value * 10;
  102. value += str[i] - '0';
  103. }
  104. }
  105. fraction /= weight;
  106. value += fraction;
  107. if (is_scientific) {
  108. bool divide = exp_val < 0;
  109. if (divide)
  110. exp_val *= -1;
  111. for (int i = 0; i < exp_val; i++) {
  112. if (divide)
  113. value /= 10;
  114. else
  115. value *= 10;
  116. }
  117. }
  118. return is_negative ? -value : value;
  119. }
  120. static CSS::Length parse_length(const CSS::ParsingContext& context, const StringView& view, bool& is_bad_length)
  121. {
  122. CSS::Length::Type type = CSS::Length::Type::Undefined;
  123. Optional<float> value;
  124. if (view.ends_with('%')) {
  125. type = CSS::Length::Type::Percentage;
  126. value = try_parse_float(view.substring_view(0, view.length() - 1));
  127. } else if (view.ends_with("px", CaseSensitivity::CaseInsensitive)) {
  128. type = CSS::Length::Type::Px;
  129. value = try_parse_float(view.substring_view(0, view.length() - 2));
  130. } else if (view.ends_with("pt", CaseSensitivity::CaseInsensitive)) {
  131. type = CSS::Length::Type::Pt;
  132. value = try_parse_float(view.substring_view(0, view.length() - 2));
  133. } else if (view.ends_with("pc", CaseSensitivity::CaseInsensitive)) {
  134. type = CSS::Length::Type::Pc;
  135. value = try_parse_float(view.substring_view(0, view.length() - 2));
  136. } else if (view.ends_with("mm", CaseSensitivity::CaseInsensitive)) {
  137. type = CSS::Length::Type::Mm;
  138. value = try_parse_float(view.substring_view(0, view.length() - 2));
  139. } else if (view.ends_with("rem", CaseSensitivity::CaseInsensitive)) {
  140. type = CSS::Length::Type::Rem;
  141. value = try_parse_float(view.substring_view(0, view.length() - 3));
  142. } else if (view.ends_with("em", CaseSensitivity::CaseInsensitive)) {
  143. type = CSS::Length::Type::Em;
  144. value = try_parse_float(view.substring_view(0, view.length() - 2));
  145. } else if (view.ends_with("ex", CaseSensitivity::CaseInsensitive)) {
  146. type = CSS::Length::Type::Ex;
  147. value = try_parse_float(view.substring_view(0, view.length() - 2));
  148. } else if (view.ends_with("vw", CaseSensitivity::CaseInsensitive)) {
  149. type = CSS::Length::Type::Vw;
  150. value = try_parse_float(view.substring_view(0, view.length() - 2));
  151. } else if (view.ends_with("vh", CaseSensitivity::CaseInsensitive)) {
  152. type = CSS::Length::Type::Vh;
  153. value = try_parse_float(view.substring_view(0, view.length() - 2));
  154. } else if (view.ends_with("vmax", CaseSensitivity::CaseInsensitive)) {
  155. type = CSS::Length::Type::Vmax;
  156. value = try_parse_float(view.substring_view(0, view.length() - 4));
  157. } else if (view.ends_with("vmin", CaseSensitivity::CaseInsensitive)) {
  158. type = CSS::Length::Type::Vmin;
  159. value = try_parse_float(view.substring_view(0, view.length() - 4));
  160. } else if (view.ends_with("cm", CaseSensitivity::CaseInsensitive)) {
  161. type = CSS::Length::Type::Cm;
  162. value = try_parse_float(view.substring_view(0, view.length() - 2));
  163. } else if (view.ends_with("in", CaseSensitivity::CaseInsensitive)) {
  164. type = CSS::Length::Type::In;
  165. value = try_parse_float(view.substring_view(0, view.length() - 2));
  166. } else if (view.ends_with("Q", CaseSensitivity::CaseInsensitive)) {
  167. type = CSS::Length::Type::Q;
  168. value = try_parse_float(view.substring_view(0, view.length() - 1));
  169. } else if (view == "0") {
  170. type = CSS::Length::Type::Px;
  171. value = 0;
  172. } else if (context.in_quirks_mode()) {
  173. type = CSS::Length::Type::Px;
  174. value = try_parse_float(view);
  175. } else {
  176. value = try_parse_float(view);
  177. if (value.has_value())
  178. is_bad_length = true;
  179. }
  180. if (!value.has_value())
  181. return {};
  182. return CSS::Length(value.value(), type);
  183. }
  184. static bool takes_integer_value(CSS::PropertyID property_id)
  185. {
  186. return property_id == CSS::PropertyID::ZIndex || property_id == CSS::PropertyID::FontWeight || property_id == CSS::PropertyID::Custom;
  187. }
  188. static StringView parse_custom_property_name(const StringView& value)
  189. {
  190. if (!value.starts_with("var(") && !value.ends_with(")"))
  191. return {};
  192. // FIXME: Allow for fallback
  193. auto first_comma_index = value.find_first_of(",");
  194. auto length = value.length();
  195. auto substring_length = first_comma_index.has_value() ? first_comma_index.value() - 4 - 1 : length - 4 - 1;
  196. return value.substring_view(4, substring_length);
  197. }
  198. RefPtr<CSS::StyleValue> parse_css_value(const CSS::ParsingContext& context, const StringView& string, CSS::PropertyID property_id)
  199. {
  200. bool is_bad_length = false;
  201. if (takes_integer_value(property_id)) {
  202. auto integer = string.to_int();
  203. if (integer.has_value())
  204. return CSS::LengthStyleValue::create(CSS::Length::make_px(integer.value()));
  205. }
  206. auto length = parse_length(context, string, is_bad_length);
  207. if (is_bad_length)
  208. return nullptr;
  209. if (!length.is_undefined())
  210. return CSS::LengthStyleValue::create(length);
  211. if (string.equals_ignoring_case("inherit"))
  212. return CSS::InheritStyleValue::create();
  213. if (string.equals_ignoring_case("initial"))
  214. return CSS::InitialStyleValue::create();
  215. if (string.equals_ignoring_case("auto"))
  216. return CSS::LengthStyleValue::create(CSS::Length::make_auto());
  217. if (string.starts_with("var("))
  218. return CSS::CustomStyleValue::create(parse_custom_property_name(string));
  219. auto value_id = CSS::value_id_from_string(string);
  220. if (value_id != CSS::ValueID::Invalid)
  221. return CSS::IdentifierStyleValue::create(value_id);
  222. auto color = parse_css_color(context, string);
  223. if (color.has_value())
  224. return CSS::ColorStyleValue::create(color.value());
  225. return CSS::StringStyleValue::create(string);
  226. }
  227. RefPtr<CSS::LengthStyleValue> parse_line_width(const CSS::ParsingContext& context, const StringView& part)
  228. {
  229. auto value = parse_css_value(context, part);
  230. if (value && value->is_length())
  231. return static_ptr_cast<CSS::LengthStyleValue>(value);
  232. return nullptr;
  233. }
  234. RefPtr<CSS::ColorStyleValue> parse_color(const CSS::ParsingContext& context, const StringView& part)
  235. {
  236. auto value = parse_css_value(context, part);
  237. if (value && value->is_color())
  238. return static_ptr_cast<CSS::ColorStyleValue>(value);
  239. return nullptr;
  240. }
  241. RefPtr<CSS::IdentifierStyleValue> parse_line_style(const CSS::ParsingContext& context, const StringView& part)
  242. {
  243. auto parsed_value = parse_css_value(context, part);
  244. if (!parsed_value || parsed_value->type() != CSS::StyleValue::Type::Identifier)
  245. return nullptr;
  246. auto value = static_ptr_cast<CSS::IdentifierStyleValue>(parsed_value);
  247. if (value->id() == CSS::ValueID::Dotted)
  248. return value;
  249. if (value->id() == CSS::ValueID::Dashed)
  250. return value;
  251. if (value->id() == CSS::ValueID::Solid)
  252. return value;
  253. if (value->id() == CSS::ValueID::Double)
  254. return value;
  255. if (value->id() == CSS::ValueID::Groove)
  256. return value;
  257. if (value->id() == CSS::ValueID::Ridge)
  258. return value;
  259. if (value->id() == CSS::ValueID::None)
  260. return value;
  261. if (value->id() == CSS::ValueID::Hidden)
  262. return value;
  263. if (value->id() == CSS::ValueID::Inset)
  264. return value;
  265. if (value->id() == CSS::ValueID::Outset)
  266. return value;
  267. return nullptr;
  268. }
  269. class CSSParser {
  270. public:
  271. CSSParser(const CSS::ParsingContext& context, const StringView& input)
  272. : m_context(context)
  273. , css(input)
  274. {
  275. }
  276. bool next_is(const char* str) const
  277. {
  278. size_t len = strlen(str);
  279. for (size_t i = 0; i < len; ++i) {
  280. if (peek(i) != str[i])
  281. return false;
  282. }
  283. return true;
  284. }
  285. char peek(size_t offset = 0) const
  286. {
  287. if ((index + offset) < css.length())
  288. return css[index + offset];
  289. return 0;
  290. }
  291. bool consume_specific(char ch)
  292. {
  293. if (peek() != ch) {
  294. dbgln("CSSParser: Peeked '{:c}' wanted specific '{:c}'", peek(), ch);
  295. }
  296. if (!peek()) {
  297. log_parse_error();
  298. return false;
  299. }
  300. if (peek() != ch) {
  301. log_parse_error();
  302. ++index;
  303. return false;
  304. }
  305. ++index;
  306. return true;
  307. }
  308. char consume_one()
  309. {
  310. PARSE_VERIFY(index < css.length());
  311. return css[index++];
  312. };
  313. bool consume_whitespace_or_comments()
  314. {
  315. size_t original_index = index;
  316. bool in_comment = false;
  317. for (; index < css.length(); ++index) {
  318. char ch = peek();
  319. if (isspace(ch))
  320. continue;
  321. if (!in_comment && ch == '/' && peek(1) == '*') {
  322. in_comment = true;
  323. ++index;
  324. continue;
  325. }
  326. if (in_comment && ch == '*' && peek(1) == '/') {
  327. in_comment = false;
  328. ++index;
  329. continue;
  330. }
  331. if (in_comment)
  332. continue;
  333. break;
  334. }
  335. return original_index != index;
  336. }
  337. static bool is_valid_selector_char(char ch)
  338. {
  339. return isalnum(ch) || ch == '-' || ch == '+' || ch == '_' || ch == '(' || ch == ')' || ch == '@';
  340. }
  341. static bool is_valid_selector_args_char(char ch)
  342. {
  343. return is_valid_selector_char(ch) || ch == ' ' || ch == '\t';
  344. }
  345. bool is_combinator(char ch) const
  346. {
  347. return ch == '~' || ch == '>' || ch == '+';
  348. }
  349. static StringView capture_selector_args(const String& pseudo_name)
  350. {
  351. if (const auto start_pos = pseudo_name.find('('); start_pos.has_value()) {
  352. const auto start = start_pos.value() + 1;
  353. if (const auto end_pos = pseudo_name.find(')', start); end_pos.has_value()) {
  354. return pseudo_name.substring_view(start, end_pos.value() - start).trim_whitespace();
  355. }
  356. }
  357. return {};
  358. }
  359. Optional<CSS::Selector::SimpleSelector> parse_simple_selector()
  360. {
  361. auto index_at_start = index;
  362. if (consume_whitespace_or_comments())
  363. return {};
  364. if (!peek() || peek() == '{' || peek() == ',' || is_combinator(peek()))
  365. return {};
  366. CSS::Selector::SimpleSelector::Type type;
  367. if (peek() == '*') {
  368. type = CSS::Selector::SimpleSelector::Type::Universal;
  369. consume_one();
  370. CSS::Selector::SimpleSelector result;
  371. result.type = type;
  372. return result;
  373. }
  374. if (peek() == '.') {
  375. type = CSS::Selector::SimpleSelector::Type::Class;
  376. consume_one();
  377. } else if (peek() == '#') {
  378. type = CSS::Selector::SimpleSelector::Type::Id;
  379. consume_one();
  380. } else if (isalpha(peek())) {
  381. type = CSS::Selector::SimpleSelector::Type::TagName;
  382. } else {
  383. type = CSS::Selector::SimpleSelector::Type::Universal;
  384. }
  385. if (type != CSS::Selector::SimpleSelector::Type::Universal) {
  386. while (is_valid_selector_char(peek()))
  387. buffer.append(consume_one());
  388. PARSE_VERIFY(!buffer.is_empty());
  389. }
  390. auto value = String::copy(buffer);
  391. if (type == CSS::Selector::SimpleSelector::Type::TagName) {
  392. // Some stylesheets use uppercase tag names, so here's a hack to just lowercase them internally.
  393. value = value.to_lowercase();
  394. }
  395. CSS::Selector::SimpleSelector simple_selector;
  396. simple_selector.type = type;
  397. simple_selector.value = value;
  398. buffer.clear();
  399. if (peek() == '[') {
  400. CSS::Selector::SimpleSelector::AttributeMatchType attribute_match_type = CSS::Selector::SimpleSelector::AttributeMatchType::HasAttribute;
  401. String attribute_name;
  402. String attribute_value;
  403. bool in_value = false;
  404. consume_specific('[');
  405. char expected_end_of_attribute_selector = ']';
  406. while (peek() != expected_end_of_attribute_selector) {
  407. char ch = consume_one();
  408. if (ch == '=' || (ch == '~' && peek() == '=')) {
  409. if (ch == '=') {
  410. attribute_match_type = CSS::Selector::SimpleSelector::AttributeMatchType::ExactValueMatch;
  411. } else if (ch == '~') {
  412. consume_one();
  413. attribute_match_type = CSS::Selector::SimpleSelector::AttributeMatchType::Contains;
  414. }
  415. attribute_name = String::copy(buffer);
  416. buffer.clear();
  417. in_value = true;
  418. consume_whitespace_or_comments();
  419. if (peek() == '\'') {
  420. expected_end_of_attribute_selector = '\'';
  421. consume_one();
  422. } else if (peek() == '"') {
  423. expected_end_of_attribute_selector = '"';
  424. consume_one();
  425. }
  426. continue;
  427. }
  428. // FIXME: This is a hack that will go away when we replace this with a big boy CSS parser.
  429. if (ch == '\\')
  430. ch = consume_one();
  431. buffer.append(ch);
  432. }
  433. if (in_value)
  434. attribute_value = String::copy(buffer);
  435. else
  436. attribute_name = String::copy(buffer);
  437. buffer.clear();
  438. simple_selector.attribute_match_type = attribute_match_type;
  439. simple_selector.attribute_name = attribute_name;
  440. simple_selector.attribute_value = attribute_value;
  441. if (expected_end_of_attribute_selector != ']') {
  442. if (!consume_specific(expected_end_of_attribute_selector))
  443. return {};
  444. }
  445. consume_whitespace_or_comments();
  446. if (!consume_specific(']'))
  447. return {};
  448. }
  449. if (peek() == ':') {
  450. // FIXME: Implement pseudo elements.
  451. [[maybe_unused]] bool is_pseudo_element = false;
  452. consume_one();
  453. if (peek() == ':') {
  454. is_pseudo_element = true;
  455. consume_one();
  456. }
  457. if (next_is("not")) {
  458. buffer.append(consume_one());
  459. buffer.append(consume_one());
  460. buffer.append(consume_one());
  461. if (!consume_specific('('))
  462. return {};
  463. buffer.append('(');
  464. while (peek() != ')')
  465. buffer.append(consume_one());
  466. if (!consume_specific(')'))
  467. return {};
  468. buffer.append(')');
  469. } else {
  470. int nesting_level = 0;
  471. while (true) {
  472. const auto ch = peek();
  473. if (ch == '(')
  474. ++nesting_level;
  475. else if (ch == ')' && nesting_level > 0)
  476. --nesting_level;
  477. if (nesting_level > 0 ? is_valid_selector_args_char(ch) : is_valid_selector_char(ch))
  478. buffer.append(consume_one());
  479. else
  480. break;
  481. };
  482. }
  483. auto pseudo_name = String::copy(buffer);
  484. buffer.clear();
  485. // Ignore for now, otherwise we produce a "false positive" selector
  486. // and apply styles to the element itself, not its pseudo element
  487. if (is_pseudo_element)
  488. return {};
  489. if (pseudo_name.equals_ignoring_case("link")) {
  490. simple_selector.pseudo_class = CSS::Selector::SimpleSelector::PseudoClass::Link;
  491. } else if (pseudo_name.equals_ignoring_case("visited")) {
  492. simple_selector.pseudo_class = CSS::Selector::SimpleSelector::PseudoClass::Visited;
  493. } else if (pseudo_name.equals_ignoring_case("hover")) {
  494. simple_selector.pseudo_class = CSS::Selector::SimpleSelector::PseudoClass::Hover;
  495. } else if (pseudo_name.equals_ignoring_case("focus")) {
  496. simple_selector.pseudo_class = CSS::Selector::SimpleSelector::PseudoClass::Focus;
  497. } else if (pseudo_name.equals_ignoring_case("first-child")) {
  498. simple_selector.pseudo_class = CSS::Selector::SimpleSelector::PseudoClass::FirstChild;
  499. } else if (pseudo_name.equals_ignoring_case("last-child")) {
  500. simple_selector.pseudo_class = CSS::Selector::SimpleSelector::PseudoClass::LastChild;
  501. } else if (pseudo_name.equals_ignoring_case("only-child")) {
  502. simple_selector.pseudo_class = CSS::Selector::SimpleSelector::PseudoClass::OnlyChild;
  503. } else if (pseudo_name.equals_ignoring_case("empty")) {
  504. simple_selector.pseudo_class = CSS::Selector::SimpleSelector::PseudoClass::Empty;
  505. } else if (pseudo_name.equals_ignoring_case("root")) {
  506. simple_selector.pseudo_class = CSS::Selector::SimpleSelector::PseudoClass::Root;
  507. } else if (pseudo_name.equals_ignoring_case("first-of-type")) {
  508. simple_selector.pseudo_class = CSS::Selector::SimpleSelector::PseudoClass::FirstOfType;
  509. } else if (pseudo_name.equals_ignoring_case("last-of-type")) {
  510. simple_selector.pseudo_class = CSS::Selector::SimpleSelector::PseudoClass::LastOfType;
  511. } else if (pseudo_name.starts_with("nth-child", CaseSensitivity::CaseInsensitive)) {
  512. simple_selector.pseudo_class = CSS::Selector::SimpleSelector::PseudoClass::NthChild;
  513. simple_selector.nth_child_pattern = CSS::Selector::SimpleSelector::NthChildPattern::parse(capture_selector_args(pseudo_name));
  514. } else if (pseudo_name.starts_with("nth-last-child", CaseSensitivity::CaseInsensitive)) {
  515. simple_selector.pseudo_class = CSS::Selector::SimpleSelector::PseudoClass::NthLastChild;
  516. simple_selector.nth_child_pattern = CSS::Selector::SimpleSelector::NthChildPattern::parse(capture_selector_args(pseudo_name));
  517. } else if (pseudo_name.equals_ignoring_case("before")) {
  518. simple_selector.pseudo_element = CSS::Selector::SimpleSelector::PseudoElement::Before;
  519. } else if (pseudo_name.equals_ignoring_case("after")) {
  520. simple_selector.pseudo_element = CSS::Selector::SimpleSelector::PseudoElement::After;
  521. } else if (pseudo_name.equals_ignoring_case("disabled")) {
  522. simple_selector.pseudo_class = CSS::Selector::SimpleSelector::PseudoClass::Disabled;
  523. } else if (pseudo_name.equals_ignoring_case("enabled")) {
  524. simple_selector.pseudo_class = CSS::Selector::SimpleSelector::PseudoClass::Enabled;
  525. } else if (pseudo_name.equals_ignoring_case("checked")) {
  526. simple_selector.pseudo_class = CSS::Selector::SimpleSelector::PseudoClass::Checked;
  527. } else if (pseudo_name.starts_with("not", CaseSensitivity::CaseInsensitive)) {
  528. simple_selector.pseudo_class = CSS::Selector::SimpleSelector::PseudoClass::Not;
  529. simple_selector.not_selector = capture_selector_args(pseudo_name);
  530. } else {
  531. dbgln("Unknown pseudo class: '{}'", pseudo_name);
  532. return {};
  533. }
  534. }
  535. if (index == index_at_start) {
  536. // We consumed nothing.
  537. return {};
  538. }
  539. return simple_selector;
  540. }
  541. Optional<CSS::Selector::ComplexSelector> parse_complex_selector()
  542. {
  543. auto relation = CSS::Selector::ComplexSelector::Relation::Descendant;
  544. if (peek() == '{' || peek() == ',')
  545. return {};
  546. if (is_combinator(peek())) {
  547. switch (peek()) {
  548. case '>':
  549. relation = CSS::Selector::ComplexSelector::Relation::ImmediateChild;
  550. break;
  551. case '+':
  552. relation = CSS::Selector::ComplexSelector::Relation::AdjacentSibling;
  553. break;
  554. case '~':
  555. relation = CSS::Selector::ComplexSelector::Relation::GeneralSibling;
  556. break;
  557. }
  558. consume_one();
  559. consume_whitespace_or_comments();
  560. }
  561. consume_whitespace_or_comments();
  562. Vector<CSS::Selector::SimpleSelector> simple_selectors;
  563. for (;;) {
  564. auto component = parse_simple_selector();
  565. if (!component.has_value())
  566. break;
  567. simple_selectors.append(component.value());
  568. // If this assert triggers, we're most likely up to no good.
  569. PARSE_VERIFY(simple_selectors.size() < 100);
  570. }
  571. if (simple_selectors.is_empty())
  572. return {};
  573. return CSS::Selector::ComplexSelector { relation, move(simple_selectors) };
  574. }
  575. void parse_selector()
  576. {
  577. Vector<CSS::Selector::ComplexSelector> complex_selectors;
  578. for (;;) {
  579. auto index_before = index;
  580. auto complex_selector = parse_complex_selector();
  581. if (complex_selector.has_value())
  582. complex_selectors.append(complex_selector.value());
  583. consume_whitespace_or_comments();
  584. if (!peek() || peek() == ',' || peek() == '{')
  585. break;
  586. // HACK: If we didn't move forward, just let go.
  587. if (index == index_before)
  588. break;
  589. }
  590. if (complex_selectors.is_empty())
  591. return;
  592. complex_selectors.first().relation = CSS::Selector::ComplexSelector::Relation::None;
  593. current_rule.selectors.append(CSS::Selector(move(complex_selectors)));
  594. }
  595. Optional<CSS::Selector> parse_individual_selector()
  596. {
  597. parse_selector();
  598. if (current_rule.selectors.is_empty())
  599. return {};
  600. return current_rule.selectors.last();
  601. }
  602. void parse_selector_list()
  603. {
  604. for (;;) {
  605. auto index_before = index;
  606. parse_selector();
  607. consume_whitespace_or_comments();
  608. if (peek() == ',') {
  609. consume_one();
  610. continue;
  611. }
  612. if (peek() == '{')
  613. break;
  614. // HACK: If we didn't move forward, just let go.
  615. if (index_before == index)
  616. break;
  617. }
  618. }
  619. bool is_valid_property_name_char(char ch) const
  620. {
  621. return ch && !isspace(ch) && ch != ':';
  622. }
  623. bool is_valid_property_value_char(char ch) const
  624. {
  625. return ch && ch != '!' && ch != ';' && ch != '}';
  626. }
  627. bool is_valid_string_quotes_char(char ch) const
  628. {
  629. return ch == '\'' || ch == '\"';
  630. }
  631. struct ValueAndImportant {
  632. String value;
  633. bool important { false };
  634. };
  635. ValueAndImportant consume_css_value()
  636. {
  637. buffer.clear();
  638. int paren_nesting_level = 0;
  639. bool important = false;
  640. for (;;) {
  641. char ch = peek();
  642. if (ch == '(') {
  643. ++paren_nesting_level;
  644. buffer.append(consume_one());
  645. continue;
  646. }
  647. if (ch == ')') {
  648. PARSE_VERIFY(paren_nesting_level > 0);
  649. --paren_nesting_level;
  650. buffer.append(consume_one());
  651. continue;
  652. }
  653. if (paren_nesting_level > 0) {
  654. buffer.append(consume_one());
  655. continue;
  656. }
  657. if (next_is("!important")) {
  658. consume_specific('!');
  659. consume_specific('i');
  660. consume_specific('m');
  661. consume_specific('p');
  662. consume_specific('o');
  663. consume_specific('r');
  664. consume_specific('t');
  665. consume_specific('a');
  666. consume_specific('n');
  667. consume_specific('t');
  668. important = true;
  669. continue;
  670. }
  671. if (next_is("/*")) {
  672. consume_whitespace_or_comments();
  673. continue;
  674. }
  675. if (!ch)
  676. break;
  677. if (ch == '\\') {
  678. consume_one();
  679. buffer.append(consume_one());
  680. continue;
  681. }
  682. if (ch == '}')
  683. break;
  684. if (ch == ';')
  685. break;
  686. buffer.append(consume_one());
  687. }
  688. // Remove trailing whitespace.
  689. while (!buffer.is_empty() && isspace(buffer.last()))
  690. buffer.take_last();
  691. auto string = String::copy(buffer);
  692. buffer.clear();
  693. return { string, important };
  694. }
  695. Optional<CSS::StyleProperty> parse_property()
  696. {
  697. consume_whitespace_or_comments();
  698. if (peek() == ';') {
  699. consume_one();
  700. return {};
  701. }
  702. if (peek() == '}')
  703. return {};
  704. buffer.clear();
  705. while (is_valid_property_name_char(peek()))
  706. buffer.append(consume_one());
  707. auto property_name = String::copy(buffer);
  708. buffer.clear();
  709. consume_whitespace_or_comments();
  710. if (!consume_specific(':'))
  711. return {};
  712. consume_whitespace_or_comments();
  713. auto [property_value, important] = consume_css_value();
  714. consume_whitespace_or_comments();
  715. if (peek() && peek() != '}') {
  716. if (!consume_specific(';'))
  717. return {};
  718. }
  719. auto property_id = CSS::property_id_from_string(property_name);
  720. if (property_id == CSS::PropertyID::Invalid && property_name.starts_with("--"))
  721. property_id = CSS::PropertyID::Custom;
  722. if (property_id == CSS::PropertyID::Invalid && !property_name.starts_with("-")) {
  723. dbgln("CSSParser: Unrecognized property '{}'", property_name);
  724. }
  725. auto value = parse_css_value(m_context, property_value, property_id);
  726. if (!value)
  727. return {};
  728. if (property_id == CSS::PropertyID::Custom) {
  729. return CSS::StyleProperty { property_id, value.release_nonnull(), property_name, important };
  730. }
  731. return CSS::StyleProperty { property_id, value.release_nonnull(), {}, important };
  732. }
  733. void parse_declaration()
  734. {
  735. for (;;) {
  736. auto property = parse_property();
  737. if (property.has_value()) {
  738. auto property_value = property.value();
  739. if (property_value.property_id == CSS::PropertyID::Custom)
  740. current_rule.custom_properties.set(property_value.custom_name, property_value);
  741. else
  742. current_rule.properties.append(property_value);
  743. }
  744. consume_whitespace_or_comments();
  745. if (!peek() || peek() == '}')
  746. break;
  747. }
  748. }
  749. void parse_style_rule()
  750. {
  751. parse_selector_list();
  752. if (!consume_specific('{')) {
  753. log_parse_error();
  754. return;
  755. }
  756. parse_declaration();
  757. if (!consume_specific('}')) {
  758. log_parse_error();
  759. return;
  760. }
  761. rules.append(CSS::CSSStyleRule::create(move(current_rule.selectors), CSS::CSSStyleDeclaration::create(move(current_rule.properties), move(current_rule.custom_properties))));
  762. }
  763. Optional<String> parse_string()
  764. {
  765. if (!is_valid_string_quotes_char(peek())) {
  766. log_parse_error();
  767. return {};
  768. }
  769. char end_char = consume_one();
  770. buffer.clear();
  771. while (peek() && peek() != end_char) {
  772. if (peek() == '\\') {
  773. consume_specific('\\');
  774. if (peek() == 0)
  775. break;
  776. }
  777. buffer.append(consume_one());
  778. }
  779. String string_value(String::copy(buffer));
  780. buffer.clear();
  781. if (consume_specific(end_char)) {
  782. return { string_value };
  783. }
  784. return {};
  785. }
  786. Optional<String> parse_url()
  787. {
  788. if (is_valid_string_quotes_char(peek()))
  789. return parse_string();
  790. buffer.clear();
  791. while (peek() && peek() != ')')
  792. buffer.append(consume_one());
  793. String url_value(String::copy(buffer));
  794. buffer.clear();
  795. if (peek() == ')')
  796. return { url_value };
  797. return {};
  798. }
  799. void parse_at_import_rule()
  800. {
  801. consume_whitespace_or_comments();
  802. Optional<String> imported_address;
  803. if (is_valid_string_quotes_char(peek())) {
  804. imported_address = parse_string();
  805. } else if (next_is("url")) {
  806. consume_specific('u');
  807. consume_specific('r');
  808. consume_specific('l');
  809. consume_whitespace_or_comments();
  810. if (!consume_specific('('))
  811. return;
  812. imported_address = parse_url();
  813. if (!consume_specific(')'))
  814. return;
  815. } else {
  816. log_parse_error();
  817. return;
  818. }
  819. if (imported_address.has_value())
  820. rules.append(CSS::CSSImportRule::create(m_context.complete_url(imported_address.value())));
  821. // FIXME: We ignore possible media query list
  822. while (peek() && peek() != ';')
  823. consume_one();
  824. consume_specific(';');
  825. }
  826. void parse_at_rule()
  827. {
  828. HashMap<String, void (CSSParser::*)()> at_rules_parsers({ { "@import", &CSSParser::parse_at_import_rule } });
  829. for (const auto& rule_parser_pair : at_rules_parsers) {
  830. if (next_is(rule_parser_pair.key.characters())) {
  831. for (char c : rule_parser_pair.key) {
  832. consume_specific(c);
  833. }
  834. (this->*(rule_parser_pair.value))();
  835. return;
  836. }
  837. }
  838. // FIXME: We ignore other @-rules completely for now.
  839. while (peek() != 0 && peek() != '{')
  840. consume_one();
  841. int level = 0;
  842. for (;;) {
  843. auto ch = consume_one();
  844. if (ch == '{') {
  845. ++level;
  846. } else if (ch == '}') {
  847. --level;
  848. if (level == 0)
  849. break;
  850. }
  851. }
  852. }
  853. void parse_rule()
  854. {
  855. consume_whitespace_or_comments();
  856. if (!peek())
  857. return;
  858. if (peek() == '@') {
  859. parse_at_rule();
  860. } else {
  861. parse_style_rule();
  862. }
  863. consume_whitespace_or_comments();
  864. }
  865. RefPtr<CSS::CSSStyleSheet> parse_sheet()
  866. {
  867. if (peek(0) == (char)0xef && peek(1) == (char)0xbb && peek(2) == (char)0xbf) {
  868. // HACK: Skip UTF-8 BOM.
  869. index += 3;
  870. }
  871. while (peek()) {
  872. parse_rule();
  873. }
  874. return CSS::CSSStyleSheet::create(move(rules));
  875. }
  876. RefPtr<CSS::CSSStyleDeclaration> parse_standalone_declaration()
  877. {
  878. consume_whitespace_or_comments();
  879. for (;;) {
  880. auto property = parse_property();
  881. if (property.has_value()) {
  882. auto property_value = property.value();
  883. if (property_value.property_id == CSS::PropertyID::Custom)
  884. current_rule.custom_properties.set(property_value.custom_name, property_value);
  885. else
  886. current_rule.properties.append(property_value);
  887. }
  888. consume_whitespace_or_comments();
  889. if (!peek())
  890. break;
  891. }
  892. return CSS::CSSStyleDeclaration::create(move(current_rule.properties), move(current_rule.custom_properties));
  893. }
  894. private:
  895. CSS::ParsingContext m_context;
  896. NonnullRefPtrVector<CSS::CSSRule> rules;
  897. struct CurrentRule {
  898. Vector<CSS::Selector> selectors;
  899. Vector<CSS::StyleProperty> properties;
  900. HashMap<String, CSS::StyleProperty> custom_properties;
  901. };
  902. CurrentRule current_rule;
  903. Vector<char> buffer;
  904. size_t index = 0;
  905. StringView css;
  906. };
  907. Optional<CSS::Selector> parse_selector(const CSS::ParsingContext& context, const StringView& selector_text)
  908. {
  909. CSSParser parser(context, selector_text);
  910. return parser.parse_individual_selector();
  911. }
  912. RefPtr<CSS::CSSStyleSheet> parse_css(const CSS::ParsingContext& context, const StringView& css)
  913. {
  914. if (css.is_empty())
  915. return CSS::CSSStyleSheet::create({});
  916. CSSParser parser(context, css);
  917. return parser.parse_sheet();
  918. }
  919. RefPtr<CSS::CSSStyleDeclaration> parse_css_declaration(const CSS::ParsingContext& context, const StringView& css)
  920. {
  921. if (css.is_empty())
  922. return CSS::CSSStyleDeclaration::create({}, {});
  923. CSSParser parser(context, css);
  924. return parser.parse_standalone_declaration();
  925. }
  926. RefPtr<CSS::StyleValue> parse_html_length(const DOM::Document& document, const StringView& string)
  927. {
  928. auto integer = string.to_int();
  929. if (integer.has_value())
  930. return CSS::LengthStyleValue::create(CSS::Length::make_px(integer.value()));
  931. return parse_css_value(CSS::ParsingContext(document), string);
  932. }
  933. }