CSSParser.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. #include <AK/HashMap.h>
  2. #include <LibHTML/CSS/PropertyID.h>
  3. #include <LibHTML/CSS/StyleSheet.h>
  4. #include <LibHTML/Parser/CSSParser.h>
  5. #include <ctype.h>
  6. #include <stdio.h>
  7. #define PARSE_ASSERT(x) \
  8. if (!(x)) { \
  9. dbg() << "CSS PARSER ASSERTION FAILED: " << #x; \
  10. dbg() << "At character# " << index << " in CSS: _" << css << "_"; \
  11. ASSERT_NOT_REACHED(); \
  12. }
  13. static Optional<Color> parse_css_color(const StringView& view)
  14. {
  15. auto color = Color::from_string(view);
  16. if (color.has_value())
  17. return color;
  18. // FIXME: Parse all valid color strings :^)
  19. return {};
  20. }
  21. NonnullRefPtr<StyleValue> parse_css_value(const StringView& view)
  22. {
  23. String string(view);
  24. char* endptr = nullptr;
  25. long value = strtol(String(view).characters(), &endptr, 10);
  26. if (endptr && ((!*endptr) || (endptr[0] == 'p' && endptr[1] == 'x' && endptr[2] == '\0')))
  27. return LengthStyleValue::create(Length(value, Length::Type::Absolute));
  28. if (string == "inherit")
  29. return InheritStyleValue::create();
  30. if (string == "initial")
  31. return InitialStyleValue::create();
  32. if (string == "auto")
  33. return LengthStyleValue::create(Length());
  34. auto color = parse_css_color(view);
  35. if (color.has_value())
  36. return ColorStyleValue::create(color.value());
  37. if (string == "-libhtml-link")
  38. return IdentifierStyleValue::create(CSS::ValueID::VendorSpecificLink);
  39. return StringStyleValue::create(string);
  40. }
  41. class CSSParser {
  42. public:
  43. CSSParser(const StringView& input)
  44. : css(input)
  45. {
  46. }
  47. bool next_is(const char* str) const
  48. {
  49. int len = strlen(str);
  50. for (int i = 0; i < len; ++i) {
  51. if (peek(i) != str[i])
  52. return false;
  53. }
  54. return true;
  55. }
  56. char peek(int offset = 0) const
  57. {
  58. if ((index + offset) < css.length())
  59. return css[index + offset];
  60. return 0;
  61. }
  62. char consume_specific(char ch)
  63. {
  64. if (peek() != ch) {
  65. dbg() << "peek() != '" << ch << "'";
  66. }
  67. PARSE_ASSERT(peek() == ch);
  68. PARSE_ASSERT(index < css.length());
  69. ++index;
  70. return ch;
  71. }
  72. char consume_one()
  73. {
  74. PARSE_ASSERT(index < css.length());
  75. return css[index++];
  76. };
  77. void consume_whitespace_or_comments()
  78. {
  79. bool in_comment = false;
  80. for (; index < css.length(); ++index) {
  81. char ch = peek();
  82. if (isspace(ch))
  83. continue;
  84. if (!in_comment && ch == '/' && peek(1) == '*') {
  85. in_comment = true;
  86. ++index;
  87. continue;
  88. }
  89. if (in_comment && ch == '*' && peek(1) == '/') {
  90. in_comment = false;
  91. ++index;
  92. continue;
  93. }
  94. if (in_comment)
  95. continue;
  96. break;
  97. }
  98. }
  99. bool is_valid_selector_char(char ch) const
  100. {
  101. return isalnum(ch) || ch == '-' || ch == '_' || ch == '(' || ch == ')' || ch == '@';
  102. }
  103. bool is_combinator(char ch) const
  104. {
  105. return ch == '~' || ch == '>' || ch == '+';
  106. }
  107. Optional<Selector::Component> parse_selector_component()
  108. {
  109. consume_whitespace_or_comments();
  110. Selector::Component::Type type;
  111. Selector::Component::Relation relation = Selector::Component::Relation::Descendant;
  112. if (peek() == '{')
  113. return {};
  114. if (is_combinator(peek())) {
  115. switch (peek()) {
  116. case '>':
  117. relation = Selector::Component::Relation::ImmediateChild;
  118. break;
  119. case '+':
  120. relation = Selector::Component::Relation::AdjacentSibling;
  121. break;
  122. case '~':
  123. relation = Selector::Component::Relation::GeneralSibling;
  124. break;
  125. }
  126. consume_one();
  127. consume_whitespace_or_comments();
  128. }
  129. if (peek() == '.') {
  130. type = Selector::Component::Type::Class;
  131. consume_one();
  132. } else if (peek() == '#') {
  133. type = Selector::Component::Type::Id;
  134. consume_one();
  135. } else {
  136. type = Selector::Component::Type::TagName;
  137. }
  138. while (is_valid_selector_char(peek()))
  139. buffer.append(consume_one());
  140. PARSE_ASSERT(!buffer.is_null());
  141. Selector::Component component { type, Selector::Component::PseudoClass::None, relation, String::copy(buffer) };
  142. buffer.clear();
  143. if (peek() == '[') {
  144. // FIXME: Implement attribute selectors.
  145. while (peek() != ']') {
  146. consume_one();
  147. }
  148. consume_one();
  149. }
  150. if (peek() == ':') {
  151. // FIXME: Implement pseudo elements.
  152. [[maybe_unused]] bool is_pseudo_element = false;
  153. consume_one();
  154. if (peek() == ':') {
  155. is_pseudo_element = true;
  156. consume_one();
  157. }
  158. while (is_valid_selector_char(peek()))
  159. buffer.append(consume_one());
  160. auto pseudo_name = String::copy(buffer);
  161. buffer.clear();
  162. if (pseudo_name == "link")
  163. component.pseudo_class = Selector::Component::PseudoClass::Link;
  164. else if (pseudo_name == "hover")
  165. component.pseudo_class = Selector::Component::PseudoClass::Hover;
  166. }
  167. return component;
  168. }
  169. void parse_selector()
  170. {
  171. Vector<Selector::Component> components;
  172. for (;;) {
  173. auto component = parse_selector_component();
  174. if (component.has_value())
  175. components.append(component.value());
  176. consume_whitespace_or_comments();
  177. if (peek() == ',' || peek() == '{')
  178. break;
  179. }
  180. if (components.is_empty())
  181. return;
  182. components.first().relation = Selector::Component::Relation::None;
  183. current_rule.selectors.append(Selector(move(components)));
  184. };
  185. void parse_selector_list()
  186. {
  187. for (;;) {
  188. parse_selector();
  189. consume_whitespace_or_comments();
  190. if (peek() == ',') {
  191. consume_one();
  192. continue;
  193. }
  194. if (peek() == '{')
  195. break;
  196. }
  197. }
  198. bool is_valid_property_name_char(char ch) const
  199. {
  200. return ch && !isspace(ch) && ch != ':';
  201. }
  202. bool is_valid_property_value_char(char ch) const
  203. {
  204. return ch && ch != '!' && ch != ';' && ch != '}';
  205. }
  206. Optional<StyleProperty> parse_property()
  207. {
  208. consume_whitespace_or_comments();
  209. if (peek() == ';') {
  210. consume_one();
  211. return {};
  212. }
  213. buffer.clear();
  214. while (is_valid_property_name_char(peek()))
  215. buffer.append(consume_one());
  216. auto property_name = String::copy(buffer);
  217. buffer.clear();
  218. consume_whitespace_or_comments();
  219. consume_specific(':');
  220. consume_whitespace_or_comments();
  221. while (is_valid_property_value_char(peek()))
  222. buffer.append(consume_one());
  223. // Remove trailing whitespace.
  224. while (!buffer.is_empty() && isspace(buffer.last()))
  225. buffer.take_last();
  226. auto property_value = String::copy(buffer);
  227. buffer.clear();
  228. consume_whitespace_or_comments();
  229. bool is_important = false;
  230. if (peek() == '!') {
  231. consume_specific('!');
  232. consume_specific('i');
  233. consume_specific('m');
  234. consume_specific('p');
  235. consume_specific('o');
  236. consume_specific('r');
  237. consume_specific('t');
  238. consume_specific('a');
  239. consume_specific('n');
  240. consume_specific('t');
  241. consume_whitespace_or_comments();
  242. is_important = true;
  243. }
  244. if (peek() && peek() != '}')
  245. consume_specific(';');
  246. auto property_id = CSS::property_id_from_string(property_name);
  247. return StyleProperty { property_id, parse_css_value(property_value), is_important };
  248. }
  249. void parse_declaration()
  250. {
  251. for (;;) {
  252. auto property = parse_property();
  253. if (property.has_value())
  254. current_rule.properties.append(property.value());
  255. consume_whitespace_or_comments();
  256. if (peek() == '}')
  257. break;
  258. }
  259. }
  260. void parse_rule()
  261. {
  262. consume_whitespace_or_comments();
  263. if (index >= css.length())
  264. return;
  265. // FIXME: We ignore @media rules for now.
  266. if (next_is("@media")) {
  267. while (peek() != '{')
  268. consume_one();
  269. int level = 0;
  270. for (;;) {
  271. auto ch = consume_one();
  272. if (ch == '{') {
  273. ++level;
  274. } else if (ch == '}') {
  275. --level;
  276. if (level == 0)
  277. break;
  278. }
  279. }
  280. consume_whitespace_or_comments();
  281. return;
  282. }
  283. parse_selector_list();
  284. consume_specific('{');
  285. parse_declaration();
  286. consume_specific('}');
  287. rules.append(StyleRule::create(move(current_rule.selectors), StyleDeclaration::create(move(current_rule.properties))));
  288. consume_whitespace_or_comments();
  289. }
  290. RefPtr<StyleSheet> parse_sheet()
  291. {
  292. while (index < css.length()) {
  293. parse_rule();
  294. }
  295. return StyleSheet::create(move(rules));
  296. }
  297. RefPtr<StyleDeclaration> parse_standalone_declaration()
  298. {
  299. consume_whitespace_or_comments();
  300. for (;;) {
  301. auto property = parse_property();
  302. if (property.has_value())
  303. current_rule.properties.append(property.value());
  304. consume_whitespace_or_comments();
  305. if (!peek())
  306. break;
  307. }
  308. return StyleDeclaration::create(move(current_rule.properties));
  309. }
  310. private:
  311. NonnullRefPtrVector<StyleRule> rules;
  312. struct CurrentRule {
  313. Vector<Selector> selectors;
  314. Vector<StyleProperty> properties;
  315. };
  316. CurrentRule current_rule;
  317. Vector<char> buffer;
  318. int index = 0;
  319. StringView css;
  320. };
  321. RefPtr<StyleSheet> parse_css(const StringView& css)
  322. {
  323. CSSParser parser(css);
  324. return parser.parse_sheet();
  325. }
  326. RefPtr<StyleDeclaration> parse_css_declaration(const StringView& css)
  327. {
  328. CSSParser parser(css);
  329. return parser.parse_standalone_declaration();
  330. }