Skip to content

Commit

Permalink
fix: fix pseudoType parsing with AtomicString.
Browse files Browse the repository at this point in the history
  • Loading branch information
andycall committed Nov 19, 2024
1 parent 40aac11 commit 4f34f8d
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 12 deletions.
4 changes: 2 additions & 2 deletions bridge/core/css/css_selector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ void CSSSelector::UpdatePseudoPage(const AtomicString& value,
const Document* document) {
DCHECK_EQ(Match(), kPagePseudoClass);
SetValue(value);
PseudoType type = CSSSelectorParser::ParsePseudoType(value.ToStdString(), false, document);
PseudoType type = CSSSelectorParser::ParsePseudoType(value, false, document);
if (type != kPseudoFirstPage && type != kPseudoLeftPage &&
type != kPseudoRightPage) {
type = kPseudoUnknown;
Expand All @@ -735,7 +735,7 @@ void CSSSelector::UpdatePseudoType(const AtomicString& value,
DCHECK(Match() == kPseudoClass || Match() == kPseudoElement);
AtomicString lower_value = value.LowerASCII();
PseudoType pseudo_type = CSSSelectorParser::ParsePseudoType(
lower_value.ToStdString(), has_arguments, context.GetDocument());
lower_value, has_arguments, context.GetDocument());
SetPseudoType(pseudo_type);
SetValue(pseudo_type == kPseudoStateDeprecatedSyntax ? value : lower_value);

Expand Down
4 changes: 3 additions & 1 deletion bridge/core/css/parser/css_lazy_parsing_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ class CSSLazyParsingTest : public testing::Test {
public:
bool HasParsedProperties(StyleRule* rule) { return rule->HasParsedProperties(); }

StyleRule* RuleAt(StyleSheetContents* sheet, size_t index) { return To<StyleRule>(sheet->ChildRules()[index].get()); }
StyleRule* RuleAt(StyleSheetContents* sheet, size_t index) {
return To<StyleRule>(sheet->ChildRules()[index].get());
}

protected:
std::shared_ptr<StyleSheetContents> cached_contents_;
Expand Down
1 change: 0 additions & 1 deletion bridge/core/css/parser/css_parser_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,6 @@ static CSSParserImpl::AllowedRulesType ComputeNewAllowedRules(CSSParserImpl::All
return CSSParserImpl::kRegularRules;
}

// TODO:当前进度[ConsumeQualifiedRule]
template <typename T>
bool CSSParserImpl::ConsumeRuleList(CSSParserTokenStream& stream,
RuleListType rule_list_type,
Expand Down
8 changes: 4 additions & 4 deletions bridge/core/css/parser/css_selector_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,7 @@ bool CSSSelectorParser::ConsumePartialComplexSelector(CSSParserTokenStream& stre
}

// static
CSSSelector::PseudoType CSSSelectorParser::ParsePseudoType(const std::string& name,
CSSSelector::PseudoType CSSSelectorParser::ParsePseudoType(const AtomicString& name,
bool has_arguments,
const Document* document) {
CSSSelector::PseudoType pseudo_type = CSSSelector::NameToPseudoType(name, has_arguments, document);
Expand All @@ -912,10 +912,10 @@ CSSSelector::PseudoType CSSSelectorParser::ParsePseudoType(const std::string& na
return pseudo_type;
}

if (name.compare(0, 8, "-webkit-") == 0) {
if (name.StartsWith("-webkit-")) {
return CSSSelector::PseudoType::kPseudoWebKitCustomElement;
}
if (name.compare(0, 10, "-internal-") == 0) {
if (name.StartsWith("-internal-")) {
return CSSSelector::PseudoType::kPseudoBlinkInternalElement;
}
return CSSSelector::PseudoType::kPseudoUnknown;
Expand Down Expand Up @@ -949,7 +949,7 @@ PseudoId CSSSelectorParser::ParsePseudoElement(const std::string& selector_strin
}

CSSSelector::PseudoType pseudo_type =
ParsePseudoType(std::string(selector_name_token.Value()),
ParsePseudoType(AtomicString(selector_name_token.Value()),
/*has_arguments=*/false, parent ? &parent->GetDocument() : nullptr);

PseudoId pseudo_id = CSSSelector::GetPseudoId(pseudo_type);
Expand Down
2 changes: 1 addition & 1 deletion bridge/core/css/parser/css_selector_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class CSSSelectorParser {
static bool SupportsComplexSelector(CSSParserTokenStream&,
std::shared_ptr<const CSSParserContext>);

static CSSSelector::PseudoType ParsePseudoType(const std::string& name,
static CSSSelector::PseudoType ParsePseudoType(const AtomicString& name,
bool has_arguments,
const Document*);

Expand Down
8 changes: 7 additions & 1 deletion bridge/foundation/atomic_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class AtomicString {

const char* Characters8() const;
const char16_t* Characters16() const;
std::string GetString() const { return string_->Characters8(); }
std::string GetString() const { return std::string(string_->Characters8(), string_->length()); }

AtomicString RemoveCharacters(CharacterMatchFunctionPtr);

Expand All @@ -85,6 +85,12 @@ class AtomicString {
return string_->Find(match_function, start);
}

bool StartsWith(
const std::string_view& prefix,
TextCaseSensitivity case_sensitivity = kTextCaseSensitive) const {
return string_->StartsWith(prefix);
}

inline bool ContainsOnlyLatin1OrEmpty() const {
if (empty())
return true;
Expand Down
32 changes: 32 additions & 0 deletions bridge/foundation/string_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,25 @@ namespace webf {
DEFINE_GLOBAL(StringImpl, g_global_empty);
DEFINE_GLOBAL(StringImpl, g_global_empty16_bit);

ALWAYS_INLINE bool Equal(const char* a, const char16_t* b, size_t length) {
for (size_t i = 0; i < length; ++i) {
if (a[i] != b[i])
return false;
}
return true;
}

ALWAYS_INLINE bool Equal(const char16_t* a, const char* b, size_t length) {
return Equal(b, a, length);
}

template <typename CharType>
ALWAYS_INLINE bool Equal(const CharType* a,
const CharType* b,
size_t length) {
return std::equal(a, a + length, b);
}

// Callers need the global empty strings to be non-const.
StringImpl* StringImpl::empty_ = const_cast<StringImpl*>(&g_global_empty);
StringImpl* StringImpl::empty16_bit_ =
Expand Down Expand Up @@ -154,6 +173,19 @@ size_t StringImpl::Find(CharacterMatchFunctionPtr match_function,
return internal::Find(Characters16(), length_, match_function, start);
}

bool StringImpl::StartsWith(char character) const {
return length_ && (*this)[0] == character;
}

bool StringImpl::StartsWith(const std::string_view& prefix) const {
if (prefix.length() > length())
return false;
if (Is8Bit()) {
return Equal(Characters8(), prefix.data(), prefix.length());
}
return Equal(Characters16(), prefix.data(), prefix.length());
}

std::shared_ptr<StringImpl> StringImpl::Substring(size_t start,
size_t length) const {
if (start >= length_)
Expand Down
7 changes: 5 additions & 2 deletions bridge/foundation/string_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,10 @@ class StringImpl : public std::enable_shared_from_this<StringImpl> {
static StringImpl* empty_;
static StringImpl* empty16_bit_;

FORCE_INLINE static std::shared_ptr<StringImpl> empty_shared() {
ALWAYS_INLINE static std::shared_ptr<StringImpl> empty_shared() {
return std::shared_ptr<StringImpl>(empty_, [](StringImpl*) {});
}
FORCE_INLINE static std::shared_ptr<StringImpl> empty16_shared() {
ALWAYS_INLINE static std::shared_ptr<StringImpl> empty16_shared() {
return std::shared_ptr<StringImpl>(empty16_bit_, [](StringImpl*) {});
}

Expand Down Expand Up @@ -209,6 +209,9 @@ class StringImpl : public std::enable_shared_from_this<StringImpl> {
// size_t Find(base::RepeatingCallback<bool(UChar)> match_callback,
// wtf_size_t index = 0) const;

bool StartsWith(char) const;
bool StartsWith(const std::string_view&) const;

std::shared_ptr<StringImpl> Substring(size_t pos, size_t len = UINT_MAX) const;

// The high bits of 'hash' are always empty, but we prefer to store our
Expand Down

0 comments on commit 4f34f8d

Please sign in to comment.