DeprecatedCSSParser.cpp 33 KB

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