Krita Source Code Documentation
Loading...
Searching...
No Matches
CssComplexSelector Class Reference

Complex selector, i.e. a combination of simple selectors. More...

+ Inheritance diagram for CssComplexSelector:

Public Member Functions

 CssComplexSelector (const QList< CssToken > &tokens)
 
bool match (const QDomElement &e) override
 Matches the given element.
 
int priority () override
 
QString toString () const override
 Returns string representation of selector.
 
 ~CssComplexSelector () override
 
- Public Member Functions inherited from CssSelectorBase
virtual ~CssSelectorBase ()
 

Private Member Functions

void compile (const QList< CssToken > &tokens)
 

Private Attributes

QString m_combinators
 
QList< CssSelectorBase * > m_selectors
 

Detailed Description

Complex selector, i.e. a combination of simple selectors.

Definition at line 367 of file SvgCssHelper.cpp.

Constructor & Destructor Documentation

◆ CssComplexSelector()

CssComplexSelector::CssComplexSelector ( const QList< CssToken > & tokens)
inline

Definition at line 370 of file SvgCssHelper.cpp.

371 {
372 compile(tokens);
373 }
void compile(const QList< CssToken > &tokens)

References compile().

◆ ~CssComplexSelector()

CssComplexSelector::~CssComplexSelector ( )
inlineoverride

Definition at line 374 of file SvgCssHelper.cpp.

375 {
376 qDeleteAll(m_selectors);
377 }
QList< CssSelectorBase * > m_selectors

References m_selectors.

Member Function Documentation

◆ compile()

void CssComplexSelector::compile ( const QList< CssToken > & tokens)
inlineprivate

Definition at line 467 of file SvgCssHelper.cpp.

468 {
469 Q_FOREACH (const CssToken &token, tokens) {
470 if(token.first == SelectorToken) {
471 m_selectors.append(new CssSimpleSelector(token.second));
472 } else {
473 m_combinators += token.second;
474 }
475 }
476 }
@ SelectorToken
a selector token
QPair< CssTokenType, QString > CssToken
A token used for tokenizing complex selectors.
A simple selector, i.e. a type/universal selector followed by attribute, id or pseudo-class selectors...

References m_combinators, m_selectors, and SelectorToken.

◆ match()

bool CssComplexSelector::match ( const QDomElement & )
inlineoverridevirtual

Matches the given element.

Implements CssSelectorBase.

Definition at line 392 of file SvgCssHelper.cpp.

393 {
394 int selectorCount = m_selectors.count();
395 int combinatorCount = m_combinators.length();
396 // check count of selectors and combinators
397 if (selectorCount-combinatorCount != 1)
398 return false;
399
400 QDomElement currentElement = e;
401
402 // match in reverse order
403 for(int i = 0; i < selectorCount; ++i) {
404 CssSelectorBase * curr = m_selectors[selectorCount-1-i];
405 if (!curr->match(currentElement)) {
406 return false;
407 }
408 // last selector and still there -> rule matched completely
409 if(i == selectorCount-1)
410 return true;
411
412 CssSelectorBase * next = m_selectors[selectorCount-1-i-1];
413 QChar combinator = m_combinators[combinatorCount-1-i];
414 if (combinator == ' ') {
415 bool matched = false;
416 // descendant combinator
417 QDomNode parent = currentElement.parentNode();
418 while(!parent.isNull()) {
419 currentElement = parent.toElement();
420 if (next->match(currentElement)) {
421 matched = true;
422 break;
423 }
424 parent = currentElement.parentNode();
425 }
426 if(!matched)
427 return false;
428 } else if (combinator == '>') {
429 // child selector
430 QDomNode parent = currentElement.parentNode();
431 if (parent.isNull())
432 return false;
433 QDomElement parentElement = parent.toElement();
434 if (next->match(parentElement)) {
435 currentElement = parentElement;
436 } else {
437 return false;
438 }
439 } else if (combinator == '+') {
440 QDomNode neighbor = currentElement.previousSibling();
441 while(!neighbor.isNull() && !neighbor.isElement())
442 neighbor = neighbor.previousSibling();
443 if (neighbor.isNull() || !neighbor.isElement())
444 return false;
445 QDomElement neighborElement = neighbor.toElement();
446 if (next->match(neighborElement)) {
447 currentElement = neighborElement;
448 } else {
449 return false;
450 }
451 } else {
452 return false;
453 }
454 }
455 return true;
456 }
Selector base class, merely an interface.
virtual bool match(const QDomElement &)=0
Matches the given element.
QAction * next(const QObject *recvr, const char *slot, QObject *parent)
ChildIterator< value_type, is_const > parent(const ChildIterator< value_type, is_const > &it)
Definition KisForest.h:327

References m_combinators, m_selectors, and CssSelectorBase::match().

◆ priority()

int CssComplexSelector::priority ( )
inlineoverridevirtual

Returns priority of selector see http://www.w3.org/TR/1998/REC-CSS2-19980512/cascade.html#specificity

Reimplemented from CssSelectorBase.

Definition at line 457 of file SvgCssHelper.cpp.

458 {
459 int p = 0;
460 Q_FOREACH (CssSelectorBase *s, m_selectors) {
461 p += s->priority();
462 }
463 return p;
464 }
const Params2D p
virtual int priority()

References m_selectors, p, and CssSelectorBase::priority().

◆ toString()

QString CssComplexSelector::toString ( ) const
inlineoverridevirtual

Returns string representation of selector.

Reimplemented from CssSelectorBase.

Definition at line 378 of file SvgCssHelper.cpp.

379 {
380 QString str;
381 int selectorCount = m_selectors.count();
382 if (selectorCount) {
383 for(int i = 0; i < selectorCount-1; ++i) {
384 str += m_selectors[i]->toString() +
385 m_combinators[i];
386 }
387 str += m_selectors.last()->toString();
388 }
389 return str;
390 }

References m_combinators, and m_selectors.

Member Data Documentation

◆ m_combinators

QString CssComplexSelector::m_combinators
private

Definition at line 478 of file SvgCssHelper.cpp.

◆ m_selectors

QList<CssSelectorBase*> CssComplexSelector::m_selectors
private

Definition at line 479 of file SvgCssHelper.cpp.


The documentation for this class was generated from the following file: