Selector.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "Selector.h"
  7. #include <AK/StringUtils.h>
  8. #include <ctype.h>
  9. namespace Web::CSS {
  10. Selector::Selector(Vector<CompoundSelector>&& compound_selectors)
  11. : m_compound_selectors(move(compound_selectors))
  12. {
  13. }
  14. Selector::~Selector()
  15. {
  16. }
  17. u32 Selector::specificity() const
  18. {
  19. unsigned ids = 0;
  20. unsigned tag_names = 0;
  21. unsigned classes = 0;
  22. for (auto& list : m_compound_selectors) {
  23. for (auto& simple_selector : list.simple_selectors) {
  24. switch (simple_selector.type) {
  25. case SimpleSelector::Type::Id:
  26. ++ids;
  27. break;
  28. case SimpleSelector::Type::Class:
  29. ++classes;
  30. break;
  31. case SimpleSelector::Type::TagName:
  32. ++tag_names;
  33. break;
  34. default:
  35. break;
  36. }
  37. }
  38. }
  39. return ids * 0x10000 + classes * 0x100 + tag_names;
  40. }
  41. Selector::SimpleSelector::ANPlusBPattern Selector::SimpleSelector::ANPlusBPattern::parse(StringView const& args)
  42. {
  43. // FIXME: Remove this when the DeprecatedCSSParser is gone.
  44. // The new Parser::parse_nth_child_pattern() does the same as this, using Tokens.
  45. CSS::Selector::SimpleSelector::ANPlusBPattern pattern;
  46. if (args.equals_ignoring_case("odd")) {
  47. pattern.step_size = 2;
  48. pattern.offset = 1;
  49. } else if (args.equals_ignoring_case("even")) {
  50. pattern.step_size = 2;
  51. } else {
  52. auto const consume_int = [](GenericLexer& lexer) -> Optional<int> {
  53. return AK::StringUtils::convert_to_int(lexer.consume_while([](char c) -> bool {
  54. return isdigit(c) || c == '+' || c == '-';
  55. }));
  56. };
  57. // Try to match any of following patterns:
  58. // 1. An+B
  59. // 2. An
  60. // 3. B
  61. // ...where "A" is "step_size", "B" is "offset" and rest are literals.
  62. // "A" can be omitted, in that case "A" = 1.
  63. // "A" may have "+" or "-" sign, "B" always must be predated by sign for pattern (1).
  64. int step_size_or_offset = 0;
  65. GenericLexer lexer { args };
  66. // "When a=1, or a=-1, the 1 may be omitted from the rule."
  67. if (lexer.consume_specific("n") || lexer.consume_specific("+n")) {
  68. step_size_or_offset = +1;
  69. lexer.retreat();
  70. } else if (lexer.consume_specific("-n")) {
  71. step_size_or_offset = -1;
  72. lexer.retreat();
  73. } else {
  74. auto const value = consume_int(lexer);
  75. if (!value.has_value())
  76. return {};
  77. step_size_or_offset = value.value();
  78. }
  79. if (lexer.consume_specific("n")) {
  80. lexer.ignore_while(isspace);
  81. if (lexer.next_is('+') || lexer.next_is('-')) {
  82. auto const sign = lexer.next_is('+') ? 1 : -1;
  83. lexer.ignore();
  84. lexer.ignore_while(isspace);
  85. // "An+B" pattern
  86. auto const offset = consume_int(lexer);
  87. if (!offset.has_value())
  88. return {};
  89. pattern.step_size = step_size_or_offset;
  90. pattern.offset = sign * offset.value();
  91. } else {
  92. // "An" pattern
  93. pattern.step_size = step_size_or_offset;
  94. }
  95. } else {
  96. // "B" pattern
  97. pattern.offset = step_size_or_offset;
  98. }
  99. if (lexer.remaining().length() > 0)
  100. return {};
  101. }
  102. return pattern;
  103. }
  104. }