DeprecatedCSSParser.cpp 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082
  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. DeprecatedParsingContext::DeprecatedParsingContext()
  31. {
  32. }
  33. DeprecatedParsingContext::DeprecatedParsingContext(const DOM::Document& document)
  34. : m_document(&document)
  35. {
  36. }
  37. DeprecatedParsingContext::DeprecatedParsingContext(const DOM::ParentNode& parent_node)
  38. : m_document(&parent_node.document())
  39. {
  40. }
  41. bool DeprecatedParsingContext::in_quirks_mode() const
  42. {
  43. return m_document ? m_document->in_quirks_mode() : false;
  44. }
  45. URL DeprecatedParsingContext::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::DeprecatedParsingContext&, 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::DeprecatedParsingContext& 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(',');
  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::DeprecatedParsingContext& 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. auto float_number = try_parse_float(string);
  209. if (float_number.has_value())
  210. return CSS::NumericStyleValue::create(float_number.value());
  211. return nullptr;
  212. }
  213. if (!length.is_undefined())
  214. return CSS::LengthStyleValue::create(length);
  215. if (string.equals_ignoring_case("inherit"))
  216. return CSS::InheritStyleValue::create();
  217. if (string.equals_ignoring_case("initial"))
  218. return CSS::InitialStyleValue::create();
  219. if (string.equals_ignoring_case("auto"))
  220. return CSS::LengthStyleValue::create(CSS::Length::make_auto());
  221. if (string.starts_with("var("))
  222. return CSS::CustomStyleValue::create(parse_custom_property_name(string));
  223. auto value_id = CSS::value_id_from_string(string);
  224. if (value_id != CSS::ValueID::Invalid)
  225. return CSS::IdentifierStyleValue::create(value_id);
  226. auto color = parse_css_color(context, string);
  227. if (color.has_value())
  228. return CSS::ColorStyleValue::create(color.value());
  229. return CSS::StringStyleValue::create(string);
  230. }
  231. RefPtr<CSS::LengthStyleValue> parse_line_width(const CSS::DeprecatedParsingContext& context, const StringView& part)
  232. {
  233. auto value = parse_css_value(context, part);
  234. if (value && value->is_length())
  235. return static_ptr_cast<CSS::LengthStyleValue>(value);
  236. return nullptr;
  237. }
  238. RefPtr<CSS::ColorStyleValue> parse_color(const CSS::DeprecatedParsingContext& context, const StringView& part)
  239. {
  240. auto value = parse_css_value(context, part);
  241. if (value && value->is_color())
  242. return static_ptr_cast<CSS::ColorStyleValue>(value);
  243. return nullptr;
  244. }
  245. RefPtr<CSS::IdentifierStyleValue> parse_line_style(const CSS::DeprecatedParsingContext& context, const StringView& part)
  246. {
  247. auto parsed_value = parse_css_value(context, part);
  248. if (!parsed_value || parsed_value->type() != CSS::StyleValue::Type::Identifier)
  249. return nullptr;
  250. auto value = static_ptr_cast<CSS::IdentifierStyleValue>(parsed_value);
  251. if (value->id() == CSS::ValueID::Dotted)
  252. return value;
  253. if (value->id() == CSS::ValueID::Dashed)
  254. return value;
  255. if (value->id() == CSS::ValueID::Solid)
  256. return value;
  257. if (value->id() == CSS::ValueID::Double)
  258. return value;
  259. if (value->id() == CSS::ValueID::Groove)
  260. return value;
  261. if (value->id() == CSS::ValueID::Ridge)
  262. return value;
  263. if (value->id() == CSS::ValueID::None)
  264. return value;
  265. if (value->id() == CSS::ValueID::Hidden)
  266. return value;
  267. if (value->id() == CSS::ValueID::Inset)
  268. return value;
  269. if (value->id() == CSS::ValueID::Outset)
  270. return value;
  271. return nullptr;
  272. }
  273. class CSSParser {
  274. public:
  275. CSSParser(const CSS::DeprecatedParsingContext& context, const StringView& input)
  276. : m_context(context)
  277. , css(input)
  278. {
  279. }
  280. bool next_is(const char* str) const
  281. {
  282. size_t len = strlen(str);
  283. for (size_t i = 0; i < len; ++i) {
  284. if (peek(i) != str[i])
  285. return false;
  286. }
  287. return true;
  288. }
  289. char peek(size_t offset = 0) const
  290. {
  291. if ((index + offset) < css.length())
  292. return css[index + offset];
  293. return 0;
  294. }
  295. bool consume_specific(char ch)
  296. {
  297. if (peek() != ch) {
  298. dbgln("CSSParser: Peeked '{:c}' wanted specific '{:c}'", peek(), ch);
  299. }
  300. if (!peek()) {
  301. log_parse_error();
  302. return false;
  303. }
  304. if (peek() != ch) {
  305. log_parse_error();
  306. ++index;
  307. return false;
  308. }
  309. ++index;
  310. return true;
  311. }
  312. char consume_one()
  313. {
  314. PARSE_VERIFY(index < css.length());
  315. return css[index++];
  316. };
  317. bool consume_whitespace_or_comments()
  318. {
  319. size_t original_index = index;
  320. bool in_comment = false;
  321. for (; index < css.length(); ++index) {
  322. char ch = peek();
  323. if (isspace(ch))
  324. continue;
  325. if (!in_comment && ch == '/' && peek(1) == '*') {
  326. in_comment = true;
  327. ++index;
  328. continue;
  329. }
  330. if (in_comment && ch == '*' && peek(1) == '/') {
  331. in_comment = false;
  332. ++index;
  333. continue;
  334. }
  335. if (in_comment)
  336. continue;
  337. break;
  338. }
  339. return original_index != index;
  340. }
  341. static bool is_valid_selector_char(char ch)
  342. {
  343. return isalnum(ch) || ch == '-' || ch == '+' || ch == '_' || ch == '(' || ch == ')' || ch == '@';
  344. }
  345. static bool is_valid_selector_args_char(char ch)
  346. {
  347. return is_valid_selector_char(ch) || ch == ' ' || ch == '\t';
  348. }
  349. bool is_combinator(char ch) const
  350. {
  351. return ch == '~' || ch == '>' || ch == '+';
  352. }
  353. static StringView capture_selector_args(const String& pseudo_name)
  354. {
  355. if (const auto start_pos = pseudo_name.find('('); start_pos.has_value()) {
  356. const auto start = start_pos.value() + 1;
  357. if (const auto end_pos = pseudo_name.find(')', start); end_pos.has_value()) {
  358. return pseudo_name.substring_view(start, end_pos.value() - start).trim_whitespace();
  359. }
  360. }
  361. return {};
  362. }
  363. Optional<CSS::Selector::SimpleSelector> parse_simple_selector()
  364. {
  365. auto index_at_start = index;
  366. if (consume_whitespace_or_comments())
  367. return {};
  368. if (!peek() || peek() == '{' || peek() == ',' || is_combinator(peek()))
  369. return {};
  370. CSS::Selector::SimpleSelector simple_selector;
  371. if (peek() == '*') {
  372. simple_selector.type = CSS::Selector::SimpleSelector::Type::Universal;
  373. consume_one();
  374. return simple_selector;
  375. }
  376. if (peek() == '.') {
  377. simple_selector.type = CSS::Selector::SimpleSelector::Type::Class;
  378. consume_one();
  379. } else if (peek() == '#') {
  380. simple_selector.type = CSS::Selector::SimpleSelector::Type::Id;
  381. consume_one();
  382. } else if (isalpha(peek())) {
  383. simple_selector.type = CSS::Selector::SimpleSelector::Type::TagName;
  384. } else if (peek() == '[') {
  385. simple_selector.type = CSS::Selector::SimpleSelector::Type::Attribute;
  386. } else if (peek() == ':') {
  387. simple_selector.type = CSS::Selector::SimpleSelector::Type::PseudoClass;
  388. } else {
  389. simple_selector.type = CSS::Selector::SimpleSelector::Type::Universal;
  390. }
  391. if ((simple_selector.type != CSS::Selector::SimpleSelector::Type::Universal)
  392. && (simple_selector.type != CSS::Selector::SimpleSelector::Type::Attribute)
  393. && (simple_selector.type != CSS::Selector::SimpleSelector::Type::PseudoClass)) {
  394. while (is_valid_selector_char(peek()))
  395. buffer.append(consume_one());
  396. PARSE_VERIFY(!buffer.is_empty());
  397. }
  398. auto value = String::copy(buffer);
  399. if (simple_selector.type == CSS::Selector::SimpleSelector::Type::TagName) {
  400. // Some stylesheets use uppercase tag names, so here's a hack to just lowercase them internally.
  401. value = value.to_lowercase();
  402. }
  403. simple_selector.value = value;
  404. buffer.clear();
  405. if (simple_selector.type == CSS::Selector::SimpleSelector::Type::Attribute) {
  406. CSS::Selector::SimpleSelector::Attribute::MatchType attribute_match_type = CSS::Selector::SimpleSelector::Attribute::MatchType::HasAttribute;
  407. String attribute_name;
  408. String attribute_value;
  409. bool in_value = false;
  410. consume_specific('[');
  411. char expected_end_of_attribute_selector = ']';
  412. while (peek() != expected_end_of_attribute_selector) {
  413. char ch = consume_one();
  414. if (ch == '=' || (ch == '~' && peek() == '=')) {
  415. if (ch == '=') {
  416. attribute_match_type = CSS::Selector::SimpleSelector::Attribute::MatchType::ExactValueMatch;
  417. } else if (ch == '~') {
  418. consume_one();
  419. attribute_match_type = CSS::Selector::SimpleSelector::Attribute::MatchType::ContainsWord;
  420. }
  421. attribute_name = String::copy(buffer);
  422. buffer.clear();
  423. in_value = true;
  424. consume_whitespace_or_comments();
  425. if (peek() == '\'') {
  426. expected_end_of_attribute_selector = '\'';
  427. consume_one();
  428. } else if (peek() == '"') {
  429. expected_end_of_attribute_selector = '"';
  430. consume_one();
  431. }
  432. continue;
  433. }
  434. // FIXME: This is a hack that will go away when we replace this with a big boy CSS parser.
  435. if (ch == '\\')
  436. ch = consume_one();
  437. buffer.append(ch);
  438. }
  439. if (in_value)
  440. attribute_value = String::copy(buffer);
  441. else
  442. attribute_name = String::copy(buffer);
  443. buffer.clear();
  444. simple_selector.attribute.match_type = attribute_match_type;
  445. simple_selector.attribute.name = attribute_name;
  446. simple_selector.attribute.value = attribute_value;
  447. if (expected_end_of_attribute_selector != ']') {
  448. if (!consume_specific(expected_end_of_attribute_selector))
  449. return {};
  450. }
  451. consume_whitespace_or_comments();
  452. if (!consume_specific(']'))
  453. return {};
  454. }
  455. if (simple_selector.type == CSS::Selector::SimpleSelector::Type::PseudoClass) {
  456. // FIXME: Implement pseudo elements.
  457. [[maybe_unused]] bool is_pseudo_element = false;
  458. consume_one();
  459. if (peek() == ':') {
  460. is_pseudo_element = true;
  461. consume_one();
  462. }
  463. if (next_is("not")) {
  464. buffer.append(consume_one());
  465. buffer.append(consume_one());
  466. buffer.append(consume_one());
  467. if (!consume_specific('('))
  468. return {};
  469. buffer.append('(');
  470. while (peek() != ')')
  471. buffer.append(consume_one());
  472. if (!consume_specific(')'))
  473. return {};
  474. buffer.append(')');
  475. } else {
  476. int nesting_level = 0;
  477. while (true) {
  478. const auto ch = peek();
  479. if (ch == '(')
  480. ++nesting_level;
  481. else if (ch == ')' && nesting_level > 0)
  482. --nesting_level;
  483. if (nesting_level > 0 ? is_valid_selector_args_char(ch) : is_valid_selector_char(ch))
  484. buffer.append(consume_one());
  485. else
  486. break;
  487. };
  488. }
  489. auto pseudo_name = String::copy(buffer);
  490. buffer.clear();
  491. // Ignore for now, otherwise we produce a "false positive" selector
  492. // and apply styles to the element itself, not its pseudo element
  493. if (is_pseudo_element)
  494. return {};
  495. auto& pseudo_class = simple_selector.pseudo_class;
  496. if (pseudo_name.equals_ignoring_case("link")) {
  497. pseudo_class.type = CSS::Selector::SimpleSelector::PseudoClass::Type::Link;
  498. } else if (pseudo_name.equals_ignoring_case("visited")) {
  499. pseudo_class.type = CSS::Selector::SimpleSelector::PseudoClass::Type::Visited;
  500. } else if (pseudo_name.equals_ignoring_case("active")) {
  501. pseudo_class.type = CSS::Selector::SimpleSelector::PseudoClass::Type::Active;
  502. } else if (pseudo_name.equals_ignoring_case("hover")) {
  503. pseudo_class.type = CSS::Selector::SimpleSelector::PseudoClass::Type::Hover;
  504. } else if (pseudo_name.equals_ignoring_case("focus")) {
  505. pseudo_class.type = CSS::Selector::SimpleSelector::PseudoClass::Type::Focus;
  506. } else if (pseudo_name.equals_ignoring_case("first-child")) {
  507. pseudo_class.type = CSS::Selector::SimpleSelector::PseudoClass::Type::FirstChild;
  508. } else if (pseudo_name.equals_ignoring_case("last-child")) {
  509. pseudo_class.type = CSS::Selector::SimpleSelector::PseudoClass::Type::LastChild;
  510. } else if (pseudo_name.equals_ignoring_case("only-child")) {
  511. pseudo_class.type = CSS::Selector::SimpleSelector::PseudoClass::Type::OnlyChild;
  512. } else if (pseudo_name.equals_ignoring_case("empty")) {
  513. pseudo_class.type = CSS::Selector::SimpleSelector::PseudoClass::Type::Empty;
  514. } else if (pseudo_name.equals_ignoring_case("root")) {
  515. pseudo_class.type = CSS::Selector::SimpleSelector::PseudoClass::Type::Root;
  516. } else if (pseudo_name.equals_ignoring_case("first-of-type")) {
  517. pseudo_class.type = CSS::Selector::SimpleSelector::PseudoClass::Type::FirstOfType;
  518. } else if (pseudo_name.equals_ignoring_case("last-of-type")) {
  519. pseudo_class.type = CSS::Selector::SimpleSelector::PseudoClass::Type::LastOfType;
  520. } else if (pseudo_name.starts_with("nth-child", CaseSensitivity::CaseInsensitive)) {
  521. pseudo_class.type = CSS::Selector::SimpleSelector::PseudoClass::Type::NthChild;
  522. pseudo_class.nth_child_pattern = CSS::Selector::SimpleSelector::NthChildPattern::parse(capture_selector_args(pseudo_name));
  523. } else if (pseudo_name.starts_with("nth-last-child", CaseSensitivity::CaseInsensitive)) {
  524. pseudo_class.type = CSS::Selector::SimpleSelector::PseudoClass::Type::NthLastChild;
  525. pseudo_class.nth_child_pattern = CSS::Selector::SimpleSelector::NthChildPattern::parse(capture_selector_args(pseudo_name));
  526. } else if (pseudo_name.equals_ignoring_case("before")) {
  527. simple_selector.pseudo_element = CSS::Selector::SimpleSelector::PseudoElement::Before;
  528. } else if (pseudo_name.equals_ignoring_case("after")) {
  529. simple_selector.pseudo_element = CSS::Selector::SimpleSelector::PseudoElement::After;
  530. } else if (pseudo_name.equals_ignoring_case("disabled")) {
  531. pseudo_class.type = CSS::Selector::SimpleSelector::PseudoClass::Type::Disabled;
  532. } else if (pseudo_name.equals_ignoring_case("enabled")) {
  533. pseudo_class.type = CSS::Selector::SimpleSelector::PseudoClass::Type::Enabled;
  534. } else if (pseudo_name.equals_ignoring_case("checked")) {
  535. pseudo_class.type = CSS::Selector::SimpleSelector::PseudoClass::Type::Checked;
  536. } else if (pseudo_name.starts_with("not", CaseSensitivity::CaseInsensitive)) {
  537. pseudo_class.type = CSS::Selector::SimpleSelector::PseudoClass::Type::Not;
  538. pseudo_class.not_selector = capture_selector_args(pseudo_name);
  539. } else {
  540. dbgln("Unknown pseudo class: '{}'", pseudo_name);
  541. return {};
  542. }
  543. }
  544. if (index == index_at_start) {
  545. // We consumed nothing.
  546. return {};
  547. }
  548. return simple_selector;
  549. }
  550. Optional<CSS::Selector::ComplexSelector> parse_complex_selector()
  551. {
  552. auto relation = CSS::Selector::ComplexSelector::Relation::Descendant;
  553. if (peek() == '{' || peek() == ',')
  554. return {};
  555. if (is_combinator(peek())) {
  556. switch (peek()) {
  557. case '>':
  558. relation = CSS::Selector::ComplexSelector::Relation::ImmediateChild;
  559. break;
  560. case '+':
  561. relation = CSS::Selector::ComplexSelector::Relation::AdjacentSibling;
  562. break;
  563. case '~':
  564. relation = CSS::Selector::ComplexSelector::Relation::GeneralSibling;
  565. break;
  566. }
  567. consume_one();
  568. consume_whitespace_or_comments();
  569. }
  570. consume_whitespace_or_comments();
  571. Vector<CSS::Selector::SimpleSelector> simple_selectors;
  572. for (;;) {
  573. auto component = parse_simple_selector();
  574. if (!component.has_value())
  575. break;
  576. simple_selectors.append(component.value());
  577. // If this assert triggers, we're most likely up to no good.
  578. PARSE_VERIFY(simple_selectors.size() < 100);
  579. }
  580. if (simple_selectors.is_empty())
  581. return {};
  582. return CSS::Selector::ComplexSelector { relation, move(simple_selectors) };
  583. }
  584. void parse_selector()
  585. {
  586. Vector<CSS::Selector::ComplexSelector> complex_selectors;
  587. for (;;) {
  588. auto index_before = index;
  589. auto complex_selector = parse_complex_selector();
  590. if (complex_selector.has_value())
  591. complex_selectors.append(complex_selector.value());
  592. consume_whitespace_or_comments();
  593. if (!peek() || peek() == ',' || peek() == '{')
  594. break;
  595. // HACK: If we didn't move forward, just let go.
  596. if (index == index_before)
  597. break;
  598. }
  599. if (complex_selectors.is_empty())
  600. return;
  601. complex_selectors.first().relation = CSS::Selector::ComplexSelector::Relation::None;
  602. current_rule.selectors.append(CSS::Selector::create(move(complex_selectors)));
  603. }
  604. RefPtr<CSS::Selector> parse_individual_selector()
  605. {
  606. parse_selector();
  607. if (current_rule.selectors.is_empty())
  608. return {};
  609. return current_rule.selectors.last();
  610. }
  611. void parse_selector_list()
  612. {
  613. for (;;) {
  614. auto index_before = index;
  615. parse_selector();
  616. consume_whitespace_or_comments();
  617. if (peek() == ',') {
  618. consume_one();
  619. continue;
  620. }
  621. if (peek() == '{')
  622. break;
  623. // HACK: If we didn't move forward, just let go.
  624. if (index_before == index)
  625. break;
  626. }
  627. }
  628. bool is_valid_property_name_char(char ch) const
  629. {
  630. return ch && !isspace(ch) && ch != ':';
  631. }
  632. bool is_valid_property_value_char(char ch) const
  633. {
  634. return ch && ch != '!' && ch != ';' && ch != '}';
  635. }
  636. bool is_valid_string_quotes_char(char ch) const
  637. {
  638. return ch == '\'' || ch == '\"';
  639. }
  640. struct ValueAndImportant {
  641. String value;
  642. bool important { false };
  643. };
  644. ValueAndImportant consume_css_value()
  645. {
  646. buffer.clear();
  647. int paren_nesting_level = 0;
  648. bool important = false;
  649. for (;;) {
  650. char ch = peek();
  651. if (ch == '(') {
  652. ++paren_nesting_level;
  653. buffer.append(consume_one());
  654. continue;
  655. }
  656. if (ch == ')') {
  657. PARSE_VERIFY(paren_nesting_level > 0);
  658. --paren_nesting_level;
  659. buffer.append(consume_one());
  660. continue;
  661. }
  662. if (paren_nesting_level > 0) {
  663. buffer.append(consume_one());
  664. continue;
  665. }
  666. if (next_is("!important")) {
  667. consume_specific('!');
  668. consume_specific('i');
  669. consume_specific('m');
  670. consume_specific('p');
  671. consume_specific('o');
  672. consume_specific('r');
  673. consume_specific('t');
  674. consume_specific('a');
  675. consume_specific('n');
  676. consume_specific('t');
  677. important = true;
  678. continue;
  679. }
  680. if (next_is("/*")) {
  681. consume_whitespace_or_comments();
  682. continue;
  683. }
  684. if (!ch)
  685. break;
  686. if (ch == '\\') {
  687. consume_one();
  688. buffer.append(consume_one());
  689. continue;
  690. }
  691. if (ch == '}')
  692. break;
  693. if (ch == ';')
  694. break;
  695. buffer.append(consume_one());
  696. }
  697. // Remove trailing whitespace.
  698. while (!buffer.is_empty() && isspace(buffer.last()))
  699. buffer.take_last();
  700. auto string = String::copy(buffer);
  701. buffer.clear();
  702. return { string, important };
  703. }
  704. Optional<CSS::StyleProperty> parse_property()
  705. {
  706. consume_whitespace_or_comments();
  707. if (peek() == ';') {
  708. consume_one();
  709. return {};
  710. }
  711. if (peek() == '}')
  712. return {};
  713. buffer.clear();
  714. while (is_valid_property_name_char(peek()))
  715. buffer.append(consume_one());
  716. auto property_name = String::copy(buffer);
  717. buffer.clear();
  718. consume_whitespace_or_comments();
  719. if (!consume_specific(':'))
  720. return {};
  721. consume_whitespace_or_comments();
  722. auto [property_value, important] = consume_css_value();
  723. consume_whitespace_or_comments();
  724. if (peek() && peek() != '}') {
  725. if (!consume_specific(';'))
  726. return {};
  727. }
  728. auto property_id = CSS::property_id_from_string(property_name);
  729. if (property_id == CSS::PropertyID::Invalid && property_name.starts_with("--"))
  730. property_id = CSS::PropertyID::Custom;
  731. if (property_id == CSS::PropertyID::Invalid && !property_name.starts_with("-")) {
  732. dbgln("CSSParser: Unrecognized property '{}'", property_name);
  733. }
  734. auto value = parse_css_value(m_context, property_value, property_id);
  735. if (!value)
  736. return {};
  737. if (property_id == CSS::PropertyID::Custom) {
  738. return CSS::StyleProperty { property_id, value.release_nonnull(), property_name, important };
  739. }
  740. return CSS::StyleProperty { property_id, value.release_nonnull(), {}, important };
  741. }
  742. void parse_declaration()
  743. {
  744. for (;;) {
  745. auto property = parse_property();
  746. if (property.has_value()) {
  747. auto property_value = property.value();
  748. if (property_value.property_id == CSS::PropertyID::Custom)
  749. current_rule.custom_properties.set(property_value.custom_name, property_value);
  750. else
  751. current_rule.properties.append(property_value);
  752. }
  753. consume_whitespace_or_comments();
  754. if (!peek() || peek() == '}')
  755. break;
  756. }
  757. }
  758. void parse_style_rule()
  759. {
  760. parse_selector_list();
  761. if (!consume_specific('{')) {
  762. log_parse_error();
  763. return;
  764. }
  765. parse_declaration();
  766. if (!consume_specific('}')) {
  767. log_parse_error();
  768. return;
  769. }
  770. rules.append(CSS::CSSStyleRule::create(move(current_rule.selectors), CSS::CSSStyleDeclaration::create(move(current_rule.properties), move(current_rule.custom_properties))));
  771. }
  772. Optional<String> parse_string()
  773. {
  774. if (!is_valid_string_quotes_char(peek())) {
  775. log_parse_error();
  776. return {};
  777. }
  778. char end_char = consume_one();
  779. buffer.clear();
  780. while (peek() && peek() != end_char) {
  781. if (peek() == '\\') {
  782. consume_specific('\\');
  783. if (peek() == 0)
  784. break;
  785. }
  786. buffer.append(consume_one());
  787. }
  788. String string_value(String::copy(buffer));
  789. buffer.clear();
  790. if (consume_specific(end_char)) {
  791. return { string_value };
  792. }
  793. return {};
  794. }
  795. Optional<String> parse_url()
  796. {
  797. if (is_valid_string_quotes_char(peek()))
  798. return parse_string();
  799. buffer.clear();
  800. while (peek() && peek() != ')')
  801. buffer.append(consume_one());
  802. String url_value(String::copy(buffer));
  803. buffer.clear();
  804. if (peek() == ')')
  805. return { url_value };
  806. return {};
  807. }
  808. void parse_at_import_rule()
  809. {
  810. consume_whitespace_or_comments();
  811. Optional<String> imported_address;
  812. if (is_valid_string_quotes_char(peek())) {
  813. imported_address = parse_string();
  814. } else if (next_is("url")) {
  815. consume_specific('u');
  816. consume_specific('r');
  817. consume_specific('l');
  818. consume_whitespace_or_comments();
  819. if (!consume_specific('('))
  820. return;
  821. imported_address = parse_url();
  822. if (!consume_specific(')'))
  823. return;
  824. } else {
  825. log_parse_error();
  826. return;
  827. }
  828. if (imported_address.has_value())
  829. rules.append(CSS::CSSImportRule::create(m_context.complete_url(imported_address.value())));
  830. // FIXME: We ignore possible media query list
  831. while (peek() && peek() != ';')
  832. consume_one();
  833. consume_specific(';');
  834. }
  835. void parse_at_rule()
  836. {
  837. HashMap<String, void (CSSParser::*)()> at_rules_parsers({ { "@import", &CSSParser::parse_at_import_rule } });
  838. for (const auto& rule_parser_pair : at_rules_parsers) {
  839. if (next_is(rule_parser_pair.key.characters())) {
  840. for (char c : rule_parser_pair.key) {
  841. consume_specific(c);
  842. }
  843. (this->*(rule_parser_pair.value))();
  844. return;
  845. }
  846. }
  847. // FIXME: We ignore other @-rules completely for now.
  848. while (peek() != 0 && peek() != '{')
  849. consume_one();
  850. int level = 0;
  851. for (;;) {
  852. auto ch = consume_one();
  853. if (ch == '{') {
  854. ++level;
  855. } else if (ch == '}') {
  856. --level;
  857. if (level == 0)
  858. break;
  859. }
  860. }
  861. }
  862. void parse_rule()
  863. {
  864. consume_whitespace_or_comments();
  865. if (!peek())
  866. return;
  867. if (peek() == '@') {
  868. parse_at_rule();
  869. } else {
  870. parse_style_rule();
  871. }
  872. consume_whitespace_or_comments();
  873. }
  874. RefPtr<CSS::CSSStyleSheet> parse_sheet()
  875. {
  876. if (peek(0) == (char)0xef && peek(1) == (char)0xbb && peek(2) == (char)0xbf) {
  877. // HACK: Skip UTF-8 BOM.
  878. index += 3;
  879. }
  880. while (peek()) {
  881. parse_rule();
  882. }
  883. return CSS::CSSStyleSheet::create(move(rules));
  884. }
  885. RefPtr<CSS::CSSStyleDeclaration> parse_standalone_declaration()
  886. {
  887. consume_whitespace_or_comments();
  888. for (;;) {
  889. auto property = parse_property();
  890. if (property.has_value()) {
  891. auto property_value = property.value();
  892. if (property_value.property_id == CSS::PropertyID::Custom)
  893. current_rule.custom_properties.set(property_value.custom_name, property_value);
  894. else
  895. current_rule.properties.append(property_value);
  896. }
  897. consume_whitespace_or_comments();
  898. if (!peek())
  899. break;
  900. }
  901. return CSS::CSSStyleDeclaration::create(move(current_rule.properties), move(current_rule.custom_properties));
  902. }
  903. private:
  904. CSS::DeprecatedParsingContext m_context;
  905. NonnullRefPtrVector<CSS::CSSRule> rules;
  906. struct CurrentRule {
  907. NonnullRefPtrVector<CSS::Selector> selectors;
  908. Vector<CSS::StyleProperty> properties;
  909. HashMap<String, CSS::StyleProperty> custom_properties;
  910. };
  911. CurrentRule current_rule;
  912. Vector<char> buffer;
  913. size_t index = 0;
  914. StringView css;
  915. };
  916. RefPtr<CSS::Selector> parse_selector(const CSS::DeprecatedParsingContext& context, const StringView& selector_text)
  917. {
  918. CSSParser parser(context, selector_text);
  919. return parser.parse_individual_selector();
  920. }
  921. RefPtr<CSS::CSSStyleSheet> parse_css(const CSS::DeprecatedParsingContext& context, const StringView& css)
  922. {
  923. if (css.is_empty())
  924. return CSS::CSSStyleSheet::create({});
  925. CSSParser parser(context, css);
  926. return parser.parse_sheet();
  927. }
  928. RefPtr<CSS::CSSStyleDeclaration> parse_css_declaration(const CSS::DeprecatedParsingContext& context, const StringView& css)
  929. {
  930. if (css.is_empty())
  931. return CSS::CSSStyleDeclaration::create({}, {});
  932. CSSParser parser(context, css);
  933. return parser.parse_standalone_declaration();
  934. }
  935. RefPtr<CSS::StyleValue> parse_html_length(const DOM::Document& document, const StringView& string)
  936. {
  937. auto integer = string.to_int();
  938. if (integer.has_value())
  939. return CSS::LengthStyleValue::create(CSS::Length::make_px(integer.value()));
  940. return parse_css_value(CSS::DeprecatedParsingContext(document), string);
  941. }
  942. }