DeprecatedCSSParser.cpp 34 KB

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