Selector.cpp 3.6 KB

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