DeprecatedCSSParser.cpp 33 KB

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