CSSParser.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. Optional<Selector::Component> parse_selector_component()
  74. {
  75. consume_whitespace();
  76. Selector::Component::Type type;
  77. Selector::Component::Relation relation = Selector::Component::Relation::None;
  78. if (peek() == '{')
  79. return {};
  80. if (peek() == '>') {
  81. relation = Selector::Component::Relation::ImmediateChild;
  82. consume_one();
  83. consume_whitespace();
  84. }
  85. if (peek() == '.') {
  86. type = Selector::Component::Type::Class;
  87. consume_one();
  88. } else if (peek() == '#') {
  89. type = Selector::Component::Type::Id;
  90. consume_one();
  91. } else {
  92. type = Selector::Component::Type::TagName;
  93. }
  94. while (is_valid_selector_char(peek()))
  95. buffer.append(consume_one());
  96. PARSE_ASSERT(!buffer.is_null());
  97. Selector::Component component { type, relation, String::copy(buffer) };
  98. buffer.clear();
  99. if (peek() == '[') {
  100. // FIXME: Implement attribute selectors.
  101. while (peek() != ']') {
  102. consume_one();
  103. }
  104. consume_one();
  105. }
  106. if (peek() == ':') {
  107. // FIXME: Implement pseudo stuff.
  108. consume_one();
  109. if (peek() == ':')
  110. consume_one();
  111. while (is_valid_selector_char(peek()))
  112. consume_one();
  113. }
  114. return component;
  115. }
  116. void parse_selector()
  117. {
  118. Vector<Selector::Component> components;
  119. for (;;) {
  120. auto component = parse_selector_component();
  121. if (component.has_value())
  122. components.append(component.value());
  123. consume_whitespace();
  124. if (peek() == ',' || peek() == '{')
  125. break;
  126. }
  127. current_rule.selectors.append(Selector(move(components)));
  128. };
  129. void parse_selector_list()
  130. {
  131. for (;;) {
  132. parse_selector();
  133. consume_whitespace();
  134. if (peek() == ',') {
  135. consume_one();
  136. continue;
  137. }
  138. if (peek() == '{')
  139. break;
  140. }
  141. }
  142. bool is_valid_property_name_char(char ch) const
  143. {
  144. return !isspace(ch) && ch != ':';
  145. }
  146. bool is_valid_property_value_char(char ch) const
  147. {
  148. return ch != '!' && ch != ';';
  149. }
  150. Optional<StyleProperty> parse_property()
  151. {
  152. consume_whitespace();
  153. if (peek() == ';') {
  154. consume_one();
  155. return {};
  156. }
  157. buffer.clear();
  158. while (is_valid_property_name_char(peek()))
  159. buffer.append(consume_one());
  160. auto property_name = String::copy(buffer);
  161. buffer.clear();
  162. consume_whitespace();
  163. consume_specific(':');
  164. consume_whitespace();
  165. while (is_valid_property_value_char(peek()))
  166. buffer.append(consume_one());
  167. auto property_value = String::copy(buffer);
  168. buffer.clear();
  169. consume_whitespace();
  170. bool is_important = false;
  171. if (peek() == '!') {
  172. consume_specific('!');
  173. consume_specific('i');
  174. consume_specific('m');
  175. consume_specific('p');
  176. consume_specific('o');
  177. consume_specific('r');
  178. consume_specific('t');
  179. consume_specific('a');
  180. consume_specific('n');
  181. consume_specific('t');
  182. consume_whitespace();
  183. is_important = true;
  184. }
  185. consume_specific(';');
  186. return StyleProperty { property_name, parse_css_value(property_value), is_important };
  187. }
  188. void parse_declaration()
  189. {
  190. for (;;) {
  191. auto property = parse_property();
  192. if (property.has_value())
  193. current_rule.properties.append(property.value());
  194. consume_whitespace();
  195. if (peek() == '}')
  196. break;
  197. }
  198. }
  199. void parse_rule()
  200. {
  201. parse_selector_list();
  202. consume_specific('{');
  203. parse_declaration();
  204. consume_specific('}');
  205. rules.append(StyleRule::create(move(current_rule.selectors), StyleDeclaration::create(move(current_rule.properties))));
  206. consume_whitespace();
  207. }
  208. NonnullRefPtr<StyleSheet> parse_sheet()
  209. {
  210. while (index < css.length()) {
  211. parse_rule();
  212. }
  213. return StyleSheet::create(move(rules));
  214. }
  215. NonnullRefPtr<StyleDeclaration> parse_standalone_declaration()
  216. {
  217. consume_whitespace();
  218. for (;;) {
  219. parse_property();
  220. consume_whitespace();
  221. if (!peek())
  222. break;
  223. }
  224. return StyleDeclaration::create(move(current_rule.properties));
  225. }
  226. private:
  227. NonnullRefPtrVector<StyleRule> rules;
  228. enum class State {
  229. Free,
  230. InSelectorComponent,
  231. InPropertyName,
  232. InPropertyValue,
  233. };
  234. struct CurrentRule {
  235. Vector<Selector> selectors;
  236. Vector<StyleProperty> properties;
  237. };
  238. CurrentRule current_rule;
  239. Vector<char> buffer;
  240. int index = 0;
  241. String css;
  242. };
  243. NonnullRefPtr<StyleSheet> parse_css(const String& css)
  244. {
  245. CSSParser parser(css);
  246. return parser.parse_sheet();
  247. }
  248. NonnullRefPtr<StyleDeclaration> parse_css_declaration(const String& css)
  249. {
  250. CSSParser parser(css);
  251. return parser.parse_standalone_declaration();
  252. }