aboutsummaryrefslogtreecommitdiff
path: root/core/fxcrt/css/cfx_cssselector.cpp
blob: f10bf5f743f23900140c2c17ba63b3bb90e59df4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright 2017 The PDFium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com

#include "core/fxcrt/css/cfx_cssselector.h"

#include <utility>

#include "core/fxcrt/fx_extension.h"
#include "third_party/base/check.h"

namespace {

size_t GetCSSNameLen(WideStringView str) {
  for (size_t i = 0; i < str.GetLength(); ++i) {
    wchar_t wch = str[i];
    if (!isascii(wch) || (!isalnum(wch) && wch != '_' && wch != '-'))
      return i;
  }
  return str.GetLength();
}

}  // namespace

CFX_CSSSelector::CFX_CSSSelector(WideStringView str,
                                 std::unique_ptr<CFX_CSSSelector> next)
    : name_hash_(FX_HashCode_GetLoweredW(str)), next_(std::move(next)) {}

CFX_CSSSelector::~CFX_CSSSelector() = default;

// static.
std::unique_ptr<CFX_CSSSelector> CFX_CSSSelector::FromString(
    WideStringView str) {
  DCHECK(!str.IsEmpty());

  for (wchar_t wch : str) {
    switch (wch) {
      case '>':
      case '[':
      case '+':
        return nullptr;
    }
  }

  std::unique_ptr<CFX_CSSSelector> head;
  for (size_t i = 0; i < str.GetLength();) {
    wchar_t wch = str[i];
    if (wch == ' ') {
      ++i;
      continue;
    }

    const bool is_star = wch == '*';
    const bool is_valid_char = is_star || (isascii(wch) && isalpha(wch));
    if (!is_valid_char)
      return nullptr;

    if (head)
      head->set_is_descendant();
    size_t len = is_star ? 1 : GetCSSNameLen(str.Last(str.GetLength() - i));
    auto new_head =
        std::make_unique<CFX_CSSSelector>(str.Substr(i, len), std::move(head));
    head = std::move(new_head);
    i += len;
  }
  return head;
}