CSSParser.cpp 8.1 KB

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