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

A simple selector, i.e. a type/universal selector followed by attribute, id or pseudo-class selectors. More...

+ Inheritance diagram for CssSimpleSelector:

Public Member Functions

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

Private Member Functions

void compile ()
 

Private Attributes

QList< CssSelectorBase * > m_selectors
 
QString m_token
 

Detailed Description

A simple selector, i.e. a type/universal selector followed by attribute, id or pseudo-class selectors.

Definition at line 232 of file SvgCssHelper.cpp.

Constructor & Destructor Documentation

◆ CssSimpleSelector()

CssSimpleSelector::CssSimpleSelector ( const QString & token)
inline

Definition at line 235 of file SvgCssHelper.cpp.

236 : m_token(token)
237 {
238 compile();
239 }

References compile().

◆ ~CssSimpleSelector()

CssSimpleSelector::~CssSimpleSelector ( )
inlineoverride

Definition at line 240 of file SvgCssHelper.cpp.

241 {
242 qDeleteAll(m_selectors);
243 }
QList< CssSelectorBase * > m_selectors

References m_selectors.

Member Function Documentation

◆ compile()

void CssSimpleSelector::compile ( )
inlineprivate

Definition at line 273 of file SvgCssHelper.cpp.

274 {
275 if (m_token == "*") {
276 m_selectors.append(new UniversalSelector());
277 return;
278 }
279
280 enum {
281 Start,
282 Finish,
283 Bad,
284 InType,
285 InId,
286 InAttribute,
287 InClassAttribute,
288 InPseudoClass
289 } state;
290
291 // add terminator to string
292 QString expr = m_token + QChar();
293 int i = 0;
294 state = Start;
295
296 QString token;
297 QString sep("#[:.");
298 // split into base selectors
299 while((state != Finish) && (state != Bad) && (i < expr.length())) {
300 QChar ch = expr[i];
301 switch(state) {
302 case Start:
303 token += ch;
304 if (ch == '#')
305 state = InId;
306 else if (ch == '[')
307 state = InAttribute;
308 else if (ch == ':')
309 state = InPseudoClass;
310 else if (ch == '.')
311 state = InClassAttribute;
312 else if (ch != '*')
313 state = InType;
314 break;
315 case InAttribute:
316 if (ch.isNull()) {
317 // reset state and token string
318 state = Finish;
319 token.clear();
320 continue;
321 } else {
322 token += ch;
323 if (ch == ']') {
324 m_selectors.append(new AttributeSelector(token));
325 state = Start;
326 token.clear();
327 }
328 }
329 break;
330 case InType:
331 case InId:
332 case InClassAttribute:
333 case InPseudoClass:
334 // are we at the start of the next selector or even finished?
335 if (sep.contains(ch) || ch.isNull()) {
336 if (state == InType)
337 m_selectors.append(new TypeSelector(token));
338 else if (state == InId)
339 m_selectors.append(new IdSelector(token));
340 else if ( state == InClassAttribute)
341 m_selectors.append(new AttributeSelector("[class~="+token.mid(1)+']'));
342 else if (state == InPseudoClass) {
343 m_selectors.append(new PseudoClassSelector(token));
344 }
345 // reset state and token string
346 state = ch.isNull() ? Finish : Start;
347 token.clear();
348 continue;
349 } else {
350 // append character to current token
351 if (!ch.isNull())
352 token += ch;
353 }
354 break;
355 default:
356 break;
357 }
358 i++;
359 }
360 }
Attribute selector, matching existence or content of attributes.
Id selector, matching the id attribute.
Pseudo-class selector.
Type selector, matching the type of an element.
Universal selector, matching anything.

References m_selectors, and m_token.

◆ match()

bool CssSimpleSelector::match ( const QDomElement & )
inlineoverridevirtual

Matches the given element.

Implements CssSelectorBase.

Definition at line 245 of file SvgCssHelper.cpp.

246 {
247 Q_FOREACH (CssSelectorBase *s, m_selectors) {
248 if (!s->match(e))
249 return false;
250 }
251
252 return true;
253 }
Selector base class, merely an interface.
virtual bool match(const QDomElement &)=0
Matches the given element.

References m_selectors, and CssSelectorBase::match().

◆ priority()

int CssSimpleSelector::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 263 of file SvgCssHelper.cpp.

264 {
265 int p = 0;
266 Q_FOREACH (CssSelectorBase *s, m_selectors) {
267 p += s->priority();
268 }
269 return p;
270 }
const Params2D p
virtual int priority()

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

◆ toString()

QString CssSimpleSelector::toString ( ) const
inlineoverridevirtual

Returns string representation of selector.

Reimplemented from CssSelectorBase.

Definition at line 255 of file SvgCssHelper.cpp.

256 {
257 QString str;
258 Q_FOREACH (CssSelectorBase *s, m_selectors) {
259 str += s->toString();
260 }
261 return str;
262 }
virtual QString toString() const
Returns string representation of selector.

References m_selectors, and CssSelectorBase::toString().

Member Data Documentation

◆ m_selectors

QList<CssSelectorBase*> CssSimpleSelector::m_selectors
private

Definition at line 362 of file SvgCssHelper.cpp.

◆ m_token

QString CssSimpleSelector::m_token
private

Definition at line 363 of file SvgCssHelper.cpp.


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