CSSParser.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #include <LibHTML/CSS/StyleSheet.h>
  2. #include <LibHTML/Parser/CSSParser.h>
  3. #include <ctype.h>
  4. #include <stdio.h>
  5. static Optional<Color> parse_css_color(const StringView& view)
  6. {
  7. auto color = Color::from_string(view);
  8. if (color.has_value())
  9. return color;
  10. // FIXME: Parse all valid color strings :^)
  11. return {};
  12. }
  13. NonnullRefPtr<StyleValue> parse_css_value(const StringView& view)
  14. {
  15. String string(view);
  16. bool ok;
  17. int as_int = string.to_int(ok);
  18. if (ok)
  19. return LengthStyleValue::create(Length(as_int, Length::Type::Absolute));
  20. unsigned as_uint = string.to_uint(ok);
  21. if (ok)
  22. return LengthStyleValue::create(Length(as_uint, Length::Type::Absolute));
  23. if (string == "inherit")
  24. return InheritStyleValue::create();
  25. if (string == "initial")
  26. return InitialStyleValue::create();
  27. if (string == "auto")
  28. return LengthStyleValue::create(Length());
  29. auto color = parse_css_color(view);
  30. if (color.has_value())
  31. return ColorStyleValue::create(color.value());
  32. return StringStyleValue::create(string);
  33. }
  34. NonnullRefPtr<StyleSheet> parse_css(const String& css)
  35. {
  36. NonnullRefPtrVector<StyleRule> rules;
  37. enum class State {
  38. Free,
  39. InSelectorComponent,
  40. InPropertyName,
  41. InPropertyValue,
  42. };
  43. struct CurrentRule {
  44. Vector<Selector> selectors;
  45. NonnullRefPtrVector<StyleDeclaration> declarations;
  46. };
  47. CurrentRule current_rule;
  48. Vector<char> buffer;
  49. int index = 0;
  50. auto peek = [&]() -> char {
  51. if (index < css.length())
  52. return css[index];
  53. return 0;
  54. };
  55. auto consume_specific = [&](char ch) {
  56. ASSERT(peek() == ch);
  57. ++index;
  58. return ch;
  59. };
  60. auto consume_one = [&]() -> char {
  61. return css[index++];
  62. };
  63. auto consume_whitespace = [&] {
  64. while (isspace(peek()))
  65. ++index;
  66. };
  67. auto is_valid_selector_char = [](char ch) {
  68. return isalnum(ch) || ch == '-' || ch == '_';
  69. };
  70. auto parse_selector = [&] {
  71. consume_whitespace();
  72. Selector::Component::Type type;
  73. if (peek() == '.') {
  74. type = Selector::Component::Type::Class;
  75. consume_one();
  76. } else if (peek() == '#') {
  77. type = Selector::Component::Type::Id;
  78. consume_one();
  79. } else {
  80. type = Selector::Component::Type::TagName;
  81. }
  82. while (is_valid_selector_char(peek()))
  83. buffer.append(consume_one());
  84. ASSERT(!buffer.is_null());
  85. auto component_string = String::copy(buffer);
  86. Vector<Selector::Component> components;
  87. components.append({ type, component_string });
  88. buffer.clear();
  89. current_rule.selectors.append(Selector(move(components)));
  90. };
  91. auto parse_selector_list = [&] {
  92. for (;;) {
  93. parse_selector();
  94. consume_whitespace();
  95. if (peek() == ',') {
  96. consume_one();
  97. continue;
  98. }
  99. if (peek() == '{')
  100. break;
  101. }
  102. };
  103. auto is_valid_property_name_char = [](char ch) {
  104. return !isspace(ch) && ch != ':';
  105. };
  106. auto is_valid_property_value_char = [](char ch) {
  107. return !isspace(ch) && ch != ';';
  108. };
  109. auto parse_declaration = [&] {
  110. consume_whitespace();
  111. buffer.clear();
  112. while (is_valid_property_name_char(peek()))
  113. buffer.append(consume_one());
  114. auto property_name = String::copy(buffer);
  115. buffer.clear();
  116. consume_whitespace();
  117. consume_specific(':');
  118. consume_whitespace();
  119. while (is_valid_property_value_char(peek()))
  120. buffer.append(consume_one());
  121. auto property_value = String::copy(buffer);
  122. buffer.clear();
  123. consume_specific(';');
  124. current_rule.declarations.append(StyleDeclaration::create(property_name, parse_css_value(property_value)));
  125. };
  126. auto parse_declarations = [&] {
  127. for (;;) {
  128. parse_declaration();
  129. consume_whitespace();
  130. if (peek() == '}')
  131. break;
  132. }
  133. };
  134. auto parse_rule = [&] {
  135. parse_selector_list();
  136. consume_specific('{');
  137. parse_declarations();
  138. consume_specific('}');
  139. rules.append(StyleRule::create(move(current_rule.selectors), move(current_rule.declarations)));
  140. consume_whitespace();
  141. };
  142. while (index < css.length()) {
  143. parse_rule();
  144. }
  145. return StyleSheet::create(move(rules));
  146. }