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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
|
/*
* Copyright (C) 1999-2003 Lars Knoll (knoll@kde.org)
* 1999 Waldo Bastian (bastian@kde.org)
* Copyright (C) 2004-2022 Apple Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#pragma once
#include "CSSSelectorEnums.h"
#include "QualifiedName.h"
#include "RenderStyleConstants.h"
#include <wtf/EnumTraits.h>
#include <wtf/FixedVector.h>
#include <wtf/TZoneMalloc.h>
namespace WebCore {
class CSSSelectorList;
struct CSSSelectorParserContext;
struct PossiblyQuotedIdentifier {
AtomString identifier;
bool wasQuoted { false };
bool isNull() const { return identifier.isNull(); }
};
WTF::TextStream& operator<<(WTF::TextStream&, PossiblyQuotedIdentifier);
enum class SelectorSpecificityIncrement {
ClassA = 0x10000,
ClassB = 0x100,
ClassC = 1
};
// Selector for a StyleRule.
DECLARE_ALLOCATOR_WITH_HEAP_IDENTIFIER(CSSSelectorRareData);
class CSSSelector {
WTF_MAKE_TZONE_ALLOCATED(CSSSelector);
public:
CSSSelector() = default;
CSSSelector(const CSSSelector&);
explicit CSSSelector(const QualifiedName&, bool tagIsForNamespaceRule = false);
~CSSSelector();
// Re-create selector text from selector's data.
String selectorText(StringView separator = { }, StringView rightSide = { }) const;
unsigned computeSpecificity() const;
std::array<uint8_t, 3> computeSpecificityTuple() const;
unsigned specificityForPage() const;
bool visitAllSimpleSelectors(auto& apply) const;
bool hasExplicitNestingParent() const;
bool hasExplicitPseudoClassScope() const;
void resolveNestingParentSelectors(const CSSSelectorList& parent);
void replaceNestingParentByPseudoClassScope();
using PseudoClass = CSSSelectorPseudoClass;
using PseudoElement = CSSSelectorPseudoElement;
// How the attribute value has to match. Default is Exact.
enum class Match : uint8_t {
Unknown = 0,
Tag,
Id,
Class,
Exact,
Set,
List,
Hyphen,
PseudoClass,
PseudoElement,
Contain, // css3: E[foo*="bar"]
Begin, // css3: E[foo^="bar"]
End, // css3: E[foo$="bar"]
PagePseudoClass,
NestingParent, // &
HasScope, // matches the :has() scope
ForgivingUnknown,
ForgivingUnknownNestContaining
};
enum class Relation : uint8_t {
Subselector,
DescendantSpace,
Child,
DirectAdjacent,
IndirectAdjacent,
ShadowDescendant,
ShadowPartDescendant,
ShadowSlotted
};
enum class PagePseudoClass : uint8_t {
First,
Left,
Right,
};
enum AttributeMatchType { CaseSensitive, CaseInsensitive };
static PseudoId pseudoId(PseudoElement);
static bool isPseudoClassEnabled(PseudoClass, const CSSSelectorParserContext&);
static bool isPseudoElementEnabled(PseudoElement, StringView, const CSSSelectorParserContext&);
static std::optional<PseudoElement> parsePseudoElementName(StringView, const CSSSelectorParserContext&);
static bool pseudoClassRequiresArgument(PseudoClass);
static bool pseudoElementRequiresArgument(PseudoElement);
static bool pseudoClassMayHaveArgument(PseudoClass);
static bool pseudoElementMayHaveArgument(PseudoElement);
static const ASCIILiteral selectorTextForPseudoClass(PseudoClass);
static const ASCIILiteral nameForUserAgentPartLegacyAlias(StringView);
// Selectors are kept in an array by CSSSelectorList.
// The next component of the selector is the next item in the array.
WTF_ALLOW_UNSAFE_BUFFER_USAGE_BEGIN
const CSSSelector* tagHistory() const { return m_isLastInTagHistory ? nullptr : this + 1; }
WTF_ALLOW_UNSAFE_BUFFER_USAGE_END
const CSSSelector* firstInCompound() const;
const QualifiedName& tagQName() const;
const AtomString& tagLowercaseLocalName() const;
const AtomString& value() const;
const AtomString& serializingValue() const;
const QualifiedName& attribute() const;
const AtomString& argument() const { return m_hasRareData ? m_data.rareData->argument : nullAtom(); }
bool attributeValueMatchingIsCaseInsensitive() const;
const FixedVector<AtomString>* argumentList() const { return m_hasRareData ? &m_data.rareData->argumentList : nullptr; }
const FixedVector<PossiblyQuotedIdentifier>* langList() const { return m_hasRareData ? &m_data.rareData->langList : nullptr; }
const CSSSelectorList* selectorList() const { return m_hasRareData ? m_data.rareData->selectorList.get() : nullptr; }
CSSSelectorList* selectorList() { return m_hasRareData ? m_data.rareData->selectorList.get() : nullptr; }
bool matchNth(int count) const;
int nthA() const;
int nthB() const;
bool hasDescendantRelation() const { return relation() == Relation::DescendantSpace; }
bool hasDescendantOrChildRelation() const { return relation() == Relation::Child || hasDescendantRelation(); }
PseudoClass pseudoClass() const;
PseudoElement pseudoElement() const;
PagePseudoClass pagePseudoClass() const;
bool matchesPseudoElement() const;
bool isSiblingSelector() const;
bool isAttributeSelector() const;
bool isHostPseudoClass() const;
Relation relation() const { return static_cast<Relation>(m_relation); }
Match match() const { return static_cast<Match>(m_match); }
bool isLastInSelectorList() const { return m_isLastInSelectorList; }
bool isFirstInTagHistory() const { return m_isFirstInTagHistory; }
bool isLastInTagHistory() const { return m_isLastInTagHistory; }
// FIXME: These should ideally be private, but CSSSelectorList and StyleRule use them.
void setLastInSelectorList() { m_isLastInSelectorList = true; }
void setNotFirstInTagHistory() { m_isFirstInTagHistory = false; }
void setNotLastInTagHistory() { m_isLastInTagHistory = false; }
bool isForPage() const { return m_isForPage; }
// Implicit means that this selector is not author/UA written.
bool isImplicit() const { return m_isImplicit; }
#if !ASSERT_WITH_SECURITY_IMPLICATION_DISABLED
bool destructorHasBeenCalled() const { return m_destructorHasBeenCalled; }
#endif
private:
friend class MutableCSSSelector;
void setValue(const AtomString&, bool matchLowerCase = false);
void setAttribute(const QualifiedName&, AttributeMatchType);
void setNth(int a, int b);
void setArgument(const AtomString&);
void setArgumentList(FixedVector<AtomString>);
void setLangList(FixedVector<PossiblyQuotedIdentifier>);
void setSelectorList(std::unique_ptr<CSSSelectorList>);
void setPseudoClass(PseudoClass);
void setPseudoElement(PseudoElement);
void setPagePseudoClass(PagePseudoClass);
void setRelation(Relation);
void setMatch(Match);
void setForPage() { m_isForPage = true; }
void setImplicit() { m_isImplicit = true; }
unsigned simpleSelectorSpecificityForPage() const;
WTF_ALLOW_UNSAFE_BUFFER_USAGE_BEGIN
CSSSelector* tagHistory() { return m_isLastInTagHistory ? nullptr : this + 1; }
WTF_ALLOW_UNSAFE_BUFFER_USAGE_END
unsigned m_relation : 4 { enumToUnderlyingType(Relation::DescendantSpace) };
mutable unsigned m_match : 5 { enumToUnderlyingType(Match::Unknown) };
mutable unsigned m_pseudoType : 8 { 0 }; // PseudoType.
// 17 bits
unsigned m_isLastInSelectorList : 1 { false };
unsigned m_isFirstInTagHistory : 1 { true };
unsigned m_isLastInTagHistory : 1 { true };
unsigned m_hasRareData : 1 { false };
unsigned m_isForPage : 1 { false };
unsigned m_tagIsForNamespaceRule : 1 { false };
unsigned m_caseInsensitiveAttributeValueMatching : 1 { false };
unsigned m_isImplicit : 1 { false };
// 25 bits
#if !ASSERT_WITH_SECURITY_IMPLICATION_DISABLED
unsigned m_destructorHasBeenCalled : 1 { false };
#endif
CSSSelector& operator=(const CSSSelector&) = delete;
CSSSelector(CSSSelector&&) = delete;
struct RareData : public RefCounted<RareData> {
WTF_MAKE_STRUCT_FAST_ALLOCATED_WITH_HEAP_IDENTIFIER(CSSSelectorRareData);
static Ref<RareData> create(AtomString);
WEBCORE_EXPORT ~RareData();
bool matchNth(int count);
// For quirks mode, class and id are case-insensitive. In the case where uppercase
// letters are used in quirks mode, |m_matchingValue| holds the lowercase class/id
// and |m_serializingValue| holds the original string.
AtomString matchingValue;
AtomString serializingValue;
int a { 0 }; // Used for :nth-*
int b { 0 }; // Used for :nth-*
QualifiedName attribute; // used for attribute selector
AtomString argument; // Used for :contains and :nth-*
FixedVector<AtomString> argumentList; // Used for :active-view-transition-type, ::highlight, ::view-transition-{group, image-pair, new, old}, ::part arguments.
FixedVector<PossiblyQuotedIdentifier> langList; // Used for :lang arguments.
std::unique_ptr<CSSSelectorList> selectorList; // Used for :is(), :matches(), and :not().
Ref<RareData> deepCopy() const;
private:
RareData(AtomString&& value);
RareData(const RareData& other);
};
void createRareData();
union DataUnion {
AtomStringImpl* value { nullptr };
QualifiedName::QualifiedNameImpl* tagQName;
RareData* rareData;
} m_data;
};
inline bool operator==(const AtomString& a, const PossiblyQuotedIdentifier& b) { return a == b.identifier; }
inline bool operator==(const PossiblyQuotedIdentifier& a, const AtomString& b) { return a.identifier == b; }
inline const QualifiedName& CSSSelector::attribute() const
{
ASSERT(isAttributeSelector());
ASSERT(m_hasRareData);
return m_data.rareData->attribute;
}
inline bool CSSSelector::matchesPseudoElement() const
{
return match() == Match::PseudoElement;
}
static inline bool pseudoClassIsRelativeToSiblings(CSSSelector::PseudoClass type)
{
return type == CSSSelector::PseudoClass::Empty
|| type == CSSSelector::PseudoClass::FirstChild
|| type == CSSSelector::PseudoClass::FirstOfType
|| type == CSSSelector::PseudoClass::LastChild
|| type == CSSSelector::PseudoClass::LastOfType
|| type == CSSSelector::PseudoClass::OnlyChild
|| type == CSSSelector::PseudoClass::OnlyOfType
|| type == CSSSelector::PseudoClass::NthChild
|| type == CSSSelector::PseudoClass::NthOfType
|| type == CSSSelector::PseudoClass::NthLastChild
|| type == CSSSelector::PseudoClass::NthLastOfType;
}
static inline bool isTreeStructuralPseudoClass(CSSSelector::PseudoClass type)
{
return pseudoClassIsRelativeToSiblings(type) || type == CSSSelector::PseudoClass::Root;
}
inline bool isLogicalCombinationPseudoClass(CSSSelector::PseudoClass pseudoClass)
{
switch (pseudoClass) {
case CSSSelector::PseudoClass::Is:
case CSSSelector::PseudoClass::Where:
case CSSSelector::PseudoClass::WebKitAny:
case CSSSelector::PseudoClass::Not:
case CSSSelector::PseudoClass::Has:
return true;
default:
return false;
}
}
inline bool CSSSelector::isSiblingSelector() const
{
return relation() == Relation::DirectAdjacent
|| relation() == Relation::IndirectAdjacent
|| (match() == CSSSelector::Match::PseudoClass && pseudoClassIsRelativeToSiblings(pseudoClass()));
}
inline bool CSSSelector::isAttributeSelector() const
{
return match() == CSSSelector::Match::Exact
|| match() == CSSSelector::Match::Set
|| match() == CSSSelector::Match::List
|| match() == CSSSelector::Match::Hyphen
|| match() == CSSSelector::Match::Contain
|| match() == CSSSelector::Match::Begin
|| match() == CSSSelector::Match::End;
}
inline void CSSSelector::setValue(const AtomString& value, bool matchLowerCase)
{
ASSERT(match() != Match::Tag);
auto matchingValue = matchLowerCase ? value.convertToASCIILowercase() : value;
if (!m_hasRareData && matchingValue != value)
createRareData();
// Need to do ref counting manually for the union.
if (!m_hasRareData) {
if (m_data.value)
m_data.value->deref();
m_data.value = value.impl();
m_data.value->ref();
return;
}
m_data.rareData->matchingValue = WTFMove(matchingValue);
m_data.rareData->serializingValue = value;
}
inline CSSSelector::~CSSSelector()
{
ASSERT_WITH_SECURITY_IMPLICATION(!m_destructorHasBeenCalled);
#if !ASSERT_WITH_SECURITY_IMPLICATION_DISABLED
m_destructorHasBeenCalled = true;
#endif
if (m_hasRareData) {
m_data.rareData->deref();
m_data.rareData = nullptr;
m_hasRareData = false;
} else if (match() == Match::Tag) {
m_data.tagQName->deref();
m_data.tagQName = nullptr;
m_match = enumToUnderlyingType(Match::Unknown);
} else if (m_data.value) {
m_data.value->deref();
m_data.value = nullptr;
}
}
inline const QualifiedName& CSSSelector::tagQName() const
{
return *reinterpret_cast<const QualifiedName*>(&m_data.tagQName);
}
inline const AtomString& CSSSelector::tagLowercaseLocalName() const
{
return tagQName().localNameLowercase();
}
inline const AtomString& CSSSelector::value() const
{
ASSERT(match() != Match::Tag);
if (m_hasRareData)
return m_data.rareData->matchingValue;
// AtomString is really just an AtomStringImpl* so the cast below is safe.
return *reinterpret_cast<const AtomString*>(&m_data.value);
}
inline const AtomString& CSSSelector::serializingValue() const
{
ASSERT(match() != Match::Tag);
if (m_hasRareData)
return m_data.rareData->serializingValue;
// AtomString is really just an AtomStringImpl* so the cast below is safe.
return *reinterpret_cast<const AtomString*>(&m_data.value);
}
inline bool CSSSelector::attributeValueMatchingIsCaseInsensitive() const
{
return m_caseInsensitiveAttributeValueMatching;
}
inline auto CSSSelector::pseudoClass() const -> PseudoClass
{
ASSERT(match() == Match::PseudoClass);
return static_cast<PseudoClass>(m_pseudoType);
}
inline void CSSSelector::setPseudoClass(PseudoClass pseudoClass)
{
m_pseudoType = enumToUnderlyingType(pseudoClass);
ASSERT(static_cast<PseudoClass>(m_pseudoType) == pseudoClass);
}
inline auto CSSSelector::pseudoElement() const -> PseudoElement
{
ASSERT(match() == Match::PseudoElement);
return static_cast<PseudoElement>(m_pseudoType);
}
inline void CSSSelector::setPseudoElement(PseudoElement pseudoElement)
{
m_pseudoType = enumToUnderlyingType(pseudoElement);
ASSERT(static_cast<PseudoElement>(m_pseudoType) == pseudoElement);
}
inline auto CSSSelector::pagePseudoClass() const -> PagePseudoClass
{
ASSERT(match() == Match::PagePseudoClass);
return static_cast<PagePseudoClass>(m_pseudoType);
}
inline void CSSSelector::setPagePseudoClass(PagePseudoClass pagePseudoClass)
{
m_pseudoType = enumToUnderlyingType(pagePseudoClass);
ASSERT(static_cast<PagePseudoClass>(m_pseudoType) == pagePseudoClass);
}
inline void CSSSelector::setRelation(Relation relation)
{
m_relation = enumToUnderlyingType(relation);
}
inline void CSSSelector::setMatch(Match match)
{
m_match = enumToUnderlyingType(match);
}
} // namespace WebCore
|