Qt 4.8
Classes | Public Functions | Public Variables | Private Functions | List of all members
QCss::StyleSelector Class Referenceabstract

#include <qcssparser_p.h>

Inheritance diagram for QCss::StyleSelector:
QStyleSheetStyleSelector QSvgStyleSelector QTextHtmlStyleSelector

Classes

union  NodePtr
 

Public Functions

virtual QString attribute (NodePtr node, const QString &name) const =0
 
QVector< DeclarationdeclarationsForNode (NodePtr node, const char *extraPseudo=0)
 
virtual NodePtr duplicateNode (NodePtr node) const =0
 
virtual void freeNode (NodePtr node) const =0
 
virtual bool hasAttributes (NodePtr node) const =0
 
virtual bool isNullNode (NodePtr node) const =0
 
virtual QStringList nodeIds (NodePtr node) const
 
virtual bool nodeNameEquals (NodePtr node, const QString &nodeName) const
 
virtual QStringList nodeNames (NodePtr node) const =0
 
virtual NodePtr parentNode (NodePtr node) const =0
 
virtual NodePtr previousSiblingNode (NodePtr node) const =0
 
QVector< StyleRulestyleRulesForNode (NodePtr node)
 
 StyleSelector ()
 
virtual ~StyleSelector ()
 

Public Variables

QString medium
 
Qt::CaseSensitivity nameCaseSensitivity
 
QVector< StyleSheetstyleSheets
 

Private Functions

bool basicSelectorMatches (const BasicSelector &rule, NodePtr node)
 
void matchRule (NodePtr node, const StyleRule &rules, StyleSheetOrigin origin, int depth, QMap< uint, StyleRule > *weightedRules)
 
bool selectorMatches (const Selector &rule, NodePtr node)
 

Detailed Description

Definition at line 650 of file qcssparser_p.h.

Constructors and Destructors

◆ StyleSelector()

QCss::StyleSelector::StyleSelector ( )
inline

Definition at line 653 of file qcssparser_p.h.

◆ ~StyleSelector()

StyleSelector::~StyleSelector ( )
virtual

Definition at line 1790 of file qcssparser.cpp.

1791 {
1792 }

Functions

◆ attribute()

virtual QString QCss::StyleSelector::attribute ( NodePtr  node,
const QString name 
) const
pure virtual

◆ basicSelectorMatches()

bool StyleSelector::basicSelectorMatches ( const BasicSelector rule,
NodePtr  node 
)
private

Definition at line 1859 of file qcssparser.cpp.

1860 {
1861  if (!sel.attributeSelectors.isEmpty()) {
1862  if (!hasAttributes(node))
1863  return false;
1864 
1865  for (int i = 0; i < sel.attributeSelectors.count(); ++i) {
1866  const QCss::AttributeSelector &a = sel.attributeSelectors.at(i);
1867 
1868  const QString attrValue = attribute(node, a.name);
1869  if (attrValue.isNull())
1870  return false;
1871 
1873 
1874  QStringList lst = attrValue.split(QLatin1Char(' '));
1875  if (!lst.contains(a.value))
1876  return false;
1877  } else if (
1879  && attrValue != a.value)
1880  ||
1882  && !attrValue.startsWith(a.value))
1883  )
1884  return false;
1885  }
1886  }
1887 
1888  if (!sel.elementName.isEmpty()
1889  && !nodeNameEquals(node, sel.elementName))
1890  return false;
1891 
1892  if (!sel.ids.isEmpty()
1893  && sel.ids != nodeIds(node))
1894  return false;
1895 
1896  return true;
1897 }
virtual QStringList nodeIds(NodePtr node) const
bool startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string starts with s; otherwise returns false.
Definition: qstring.cpp:3734
long ASN1_INTEGER_get ASN1_INTEGER * a
The QString class provides a Unicode character string.
Definition: qstring.h:83
virtual QString attribute(NodePtr node, const QString &name) const =0
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
QBool contains(const QString &str, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the list contains the string str; otherwise returns false.
Definition: qstringlist.h:172
bool isNull() const
Returns true if this string is null; otherwise returns false.
Definition: qstring.h:505
ValueMatchType valueMatchCriterium
Definition: qcssparser_p.h:532
virtual bool nodeNameEquals(NodePtr node, const QString &nodeName) const
virtual bool hasAttributes(NodePtr node) const =0
QStringList split(const QString &sep, SplitBehavior behavior=KeepEmptyParts, Qt::CaseSensitivity cs=Qt::CaseSensitive) const Q_REQUIRED_RESULT
Splits the string into substrings wherever sep occurs, and returns the list of those strings...
Definition: qstring.cpp:6526
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ declarationsForNode()

QVector< Declaration > StyleSelector::declarationsForNode ( NodePtr  node,
const char *  extraPseudo = 0 
)

Definition at line 1982 of file qcssparser.cpp.

Referenced by cssStyleLookup(), QTextHtmlParser::declarationsForNode(), and parseStopNode().

1983 {
1984  QVector<Declaration> decls;
1985  QVector<StyleRule> rules = styleRulesForNode(node);
1986  for (int i = 0; i < rules.count(); i++) {
1987  const Selector& selector = rules.at(i).selectors.at(0);
1988  const QString pseudoElement = selector.pseudoElement();
1989 
1990  if (extraPseudo && pseudoElement == QLatin1String(extraPseudo)) {
1991  decls += rules.at(i).declarations;
1992  continue;
1993  }
1994 
1995  if (!pseudoElement.isEmpty()) // skip rules with pseudo elements
1996  continue;
1997  quint64 pseudoClass = selector.pseudoClass();
1998  if (pseudoClass == PseudoClass_Enabled || pseudoClass == PseudoClass_Unspecified)
1999  decls += rules.at(i).declarations;
2000  }
2001  return decls;
2002 }
const quint64 PseudoClass_Enabled
Definition: qcssparser_p.h:462
QString pseudoElement() const
int count(const T &t) const
Returns the number of occurrences of value in the vector.
Definition: qvector.h:742
quint64 pseudoClass(quint64 *negated=0) const
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QVector class is a template class that provides a dynamic array.
Definition: qdatastream.h:64
unsigned __int64 quint64
Definition: qglobal.h:943
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
QVector< Selector > selectors
Definition: qcssparser_p.h:605
QVector< Declaration > declarations
Definition: qcssparser_p.h:606
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
const quint64 PseudoClass_Unspecified
Definition: qcssparser_p.h:470
static quint64 pseudoClass(QStyle::State state)
QVector< StyleRule > styleRulesForNode(NodePtr node)

◆ duplicateNode()

virtual NodePtr QCss::StyleSelector::duplicateNode ( NodePtr  node) const
pure virtual

◆ freeNode()

virtual void QCss::StyleSelector::freeNode ( NodePtr  node) const
pure virtual

◆ hasAttributes()

virtual bool QCss::StyleSelector::hasAttributes ( NodePtr  node) const
pure virtual

◆ isNullNode()

virtual bool QCss::StyleSelector::isNullNode ( NodePtr  node) const
pure virtual

◆ matchRule()

void StyleSelector::matchRule ( NodePtr  node,
const StyleRule rules,
StyleSheetOrigin  origin,
int  depth,
QMap< uint, StyleRule > *  weightedRules 
)
private

Definition at line 1899 of file qcssparser.cpp.

1901 {
1902  for (int j = 0; j < rule.selectors.count(); ++j) {
1903  const Selector& selector = rule.selectors.at(j);
1904  if (selectorMatches(selector, node)) {
1905  uint weight = rule.order
1906  + selector.specificity() *0x100
1907  + (uint(origin) + depth)*0x100000;
1908  StyleRule newRule = rule;
1909  if(rule.selectors.count() > 1) {
1910  newRule.selectors.resize(1);
1911  newRule.selectors[0] = selector;
1912  }
1913  //We might have rules with the same weight if they came from a rule with several selectors
1914  weightedRules->insertMulti(weight, newRule);
1915  }
1916  }
1917 }
bool selectorMatches(const Selector &rule, NodePtr node)
iterator insertMulti(const Key &key, const T &value)
Inserts a new item with the key key and a value of value.
Definition: qmap.h:595
QVector< Selector > selectors
Definition: qcssparser_p.h:605
unsigned int uint
Definition: qglobal.h:996
int specificity() const

◆ nodeIds()

QStringList StyleSelector::nodeIds ( NodePtr  node) const
virtual

Reimplemented in QStyleSheetStyleSelector, and QSvgStyleSelector.

Definition at line 1799 of file qcssparser.cpp.

1800 {
1801  return QStringList(attribute(node, QLatin1String("id")));
1802 }
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
virtual QString attribute(NodePtr node, const QString &name) const =0
The QStringList class provides a list of strings.
Definition: qstringlist.h:66

◆ nodeNameEquals()

bool StyleSelector::nodeNameEquals ( NodePtr  node,
const QString nodeName 
) const
virtual

Reimplemented in QStyleSheetStyleSelector, and QSvgStyleSelector.

Definition at line 1794 of file qcssparser.cpp.

1795 {
1796  return nodeNames(node).contains(nodeName, nameCaseSensitivity);
1797 }
virtual QStringList nodeNames(NodePtr node) const =0
QBool contains(const QString &str, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the list contains the string str; otherwise returns false.
Definition: qstringlist.h:172
Qt::CaseSensitivity nameCaseSensitivity
Definition: qcssparser_p.h:677

◆ nodeNames()

virtual QStringList QCss::StyleSelector::nodeNames ( NodePtr  node) const
pure virtual

◆ parentNode()

virtual NodePtr QCss::StyleSelector::parentNode ( NodePtr  node) const
pure virtual

◆ previousSiblingNode()

virtual NodePtr QCss::StyleSelector::previousSiblingNode ( NodePtr  node) const
pure virtual

◆ selectorMatches()

bool StyleSelector::selectorMatches ( const Selector rule,
NodePtr  node 
)
private

Definition at line 1804 of file qcssparser.cpp.

1805 {
1806  if (selector.basicSelectors.isEmpty())
1807  return false;
1808 
1809  if (selector.basicSelectors.at(0).relationToNext == BasicSelector::NoRelation) {
1810  if (selector.basicSelectors.count() != 1)
1811  return false;
1812  return basicSelectorMatches(selector.basicSelectors.at(0), node);
1813  }
1814  if (selector.basicSelectors.count() <= 1)
1815  return false;
1816 
1817  int i = selector.basicSelectors.count() - 1;
1818  node = duplicateNode(node);
1819  bool match = true;
1820 
1821  BasicSelector sel = selector.basicSelectors.at(i);
1822  do {
1823  match = basicSelectorMatches(sel, node);
1824  if (!match) {
1826  || i == selector.basicSelectors.count() - 1) // first element must always match!
1827  break;
1828  }
1829 
1831  --i;
1832 
1833  if (i < 0)
1834  break;
1835 
1836  sel = selector.basicSelectors.at(i);
1839 
1840  NodePtr nextParent = parentNode(node);
1841  freeNode(node);
1842  node = nextParent;
1844  NodePtr previousSibling = previousSiblingNode(node);
1845  freeNode(node);
1846  node = previousSibling;
1847  }
1848  if (isNullNode(node)) {
1849  match = false;
1850  break;
1851  }
1852  } while (i >= 0 && (match || sel.relationToNext == BasicSelector::MatchNextSelectorIfAncestor));
1853 
1854  freeNode(node);
1855 
1856  return match;
1857 }
static bool match(const uchar *found, const char *target, uint len)
bool basicSelectorMatches(const BasicSelector &rule, NodePtr node)
virtual void freeNode(NodePtr node) const =0
virtual bool isNullNode(NodePtr node) const =0
virtual NodePtr duplicateNode(NodePtr node) const =0
virtual NodePtr parentNode(NodePtr node) const =0
virtual NodePtr previousSiblingNode(NodePtr node) const =0

◆ styleRulesForNode()

QVector< StyleRule > StyleSelector::styleRulesForNode ( NodePtr  node)

Definition at line 1921 of file qcssparser.cpp.

1922 {
1923  QVector<StyleRule> rules;
1924  if (styleSheets.isEmpty())
1925  return rules;
1926 
1927  QMap<uint, StyleRule> weightedRules; // (spec, rule) that will be sorted below
1928 
1929  //prune using indexed stylesheet
1930  for (int sheetIdx = 0; sheetIdx < styleSheets.count(); ++sheetIdx) {
1931  const StyleSheet &styleSheet = styleSheets.at(sheetIdx);
1932  for (int i = 0; i < styleSheet.styleRules.count(); ++i) {
1933  matchRule(node, styleSheet.styleRules.at(i), styleSheet.origin, styleSheet.depth, &weightedRules);
1934  }
1935 
1936  if (!styleSheet.idIndex.isEmpty()) {
1937  QStringList ids = nodeIds(node);
1938  for (int i = 0; i < ids.count(); i++) {
1939  const QString &key = ids.at(i);
1940  QMultiHash<QString, StyleRule>::const_iterator it = styleSheet.idIndex.constFind(key);
1941  while (it != styleSheet.idIndex.constEnd() && it.key() == key) {
1942  matchRule(node, it.value(), styleSheet.origin, styleSheet.depth, &weightedRules);
1943  ++it;
1944  }
1945  }
1946  }
1947  if (!styleSheet.nameIndex.isEmpty()) {
1948  QStringList names = nodeNames(node);
1949  for (int i = 0; i < names.count(); i++) {
1950  QString name = names.at(i);
1952  name = name.toLower();
1953  QMultiHash<QString, StyleRule>::const_iterator it = styleSheet.nameIndex.constFind(name);
1954  while (it != styleSheet.nameIndex.constEnd() && it.key() == name) {
1955  matchRule(node, it.value(), styleSheet.origin, styleSheet.depth, &weightedRules);
1956  ++it;
1957  }
1958  }
1959  }
1960  if (!medium.isEmpty()) {
1961  for (int i = 0; i < styleSheet.mediaRules.count(); ++i) {
1962  if (styleSheet.mediaRules.at(i).media.contains(medium, Qt::CaseInsensitive)) {
1963  for (int j = 0; j < styleSheet.mediaRules.at(i).styleRules.count(); ++j) {
1964  matchRule(node, styleSheet.mediaRules.at(i).styleRules.at(j), styleSheet.origin,
1965  styleSheet.depth, &weightedRules);
1966  }
1967  }
1968  }
1969  }
1970  }
1971 
1972  rules.reserve(weightedRules.count());
1974  for ( ; it != weightedRules.constEnd() ; ++it)
1975  rules += *it;
1976 
1977  return rules;
1978 }
The QHash::const_iterator class provides an STL-style const iterator for QHash and QMultiHash...
Definition: qhash.h:395
#define it(className, varName)
int count(const Key &key) const
Returns the number of items associated with key key.
Definition: qmap.h:539
virtual QStringList nodeIds(NodePtr node) const
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QVector class is a template class that provides a dynamic array.
Definition: qdatastream.h:64
StyleSheetOrigin origin
Definition: qcssparser_p.h:643
const Key & key() const
Returns the current item&#39;s key.
Definition: qhash.h:419
QMultiHash< QString, StyleRule > idIndex
Definition: qcssparser_p.h:646
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
const char * name
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
virtual QStringList nodeNames(NodePtr node) const =0
void matchRule(NodePtr node, const StyleRule &rules, StyleSheetOrigin origin, int depth, QMap< uint, StyleRule > *weightedRules)
QVector< StyleRule > styleRules
Definition: qcssparser_p.h:639
const T & value() const
Returns the current item&#39;s value.
Definition: qhash.h:420
QVector< MediaRule > mediaRules
Definition: qcssparser_p.h:640
QVector< StyleSheet > styleSheets
Definition: qcssparser_p.h:675
const_iterator constBegin() const
Returns a const STL-style iterator pointing to the first item in the map.
Definition: qmap.h:374
The QMap::const_iterator class provides an STL-style const iterator for QMap and QMultiMap.
Definition: qmap.h:301
const_iterator constEnd() const
Returns a const STL-style iterator pointing to the imaginary item after the last item in the map...
Definition: qmap.h:380
Qt::CaseSensitivity nameCaseSensitivity
Definition: qcssparser_p.h:677
QMultiHash< QString, StyleRule > nameIndex
Definition: qcssparser_p.h:645
int key
QString toLower() const Q_REQUIRED_RESULT
Returns a lowercase copy of the string.
Definition: qstring.cpp:5389
void reserve(int size)
Attempts to allocate memory for at least size elements.
Definition: qvector.h:339
The QMap class is a template class that provides a skip-list-based dictionary.
Definition: qdatastream.h:67

Properties

◆ medium

QString QCss::StyleSelector::medium

Definition at line 676 of file qcssparser_p.h.

Referenced by QTextHtmlParser::declarationsForNode().

◆ nameCaseSensitivity

Qt::CaseSensitivity QCss::StyleSelector::nameCaseSensitivity

Definition at line 677 of file qcssparser_p.h.

◆ styleSheets

QVector<StyleSheet> QCss::StyleSelector::styleSheets

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