DeprecatedCSSParser.cpp 33 KB

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