cssstyleselector.cpp
来自「konqueror3 embedded版本, KDE环境下的当家浏览器的嵌入式版」· C++ 代码 · 共 1,879 行 · 第 1/5 页
CPP
1,879 行
case CSS_PROP_DIRECTION: case CSS_PROP_BACKGROUND_IMAGE: case CSS_PROP_DISPLAY: // these have to be applied first, because other properties use the computed // values of these properties. first = true; break; default: first = false; break; } array->prop = prop; array->pseudoId = RenderStyle::NOPSEUDO; array->selector = 0; array->position = i; array->priority = (!first << 30) | (source << 24); propsToApply[numProps++] = array++; } return numProps;}static bool subject;// modified version of the one in kurl.cppstatic void cleanpath(QString &path){ int pos; while ( (pos = path.find( "/../" )) != -1 ) { int prev = 0; if ( pos > 0 ) prev = path.findRev( "/", pos -1 ); // don't remove the host, i.e. http://foo.org/../foo.html if (prev < 0 || (prev > 3 && path.findRev("://", prev-1) == prev-2)) path.remove( pos, 3); else // matching directory found ? path.remove( prev, pos- prev + 3 ); } pos = 0; // Don't remove "//" from an anchor identifier. -rjw // Set refPos to -2 to mean "I haven't looked for the anchor yet". // We don't want to waste a function call on the search for the anchor // in the vast majority of cases where there is no "//" in the path. int refPos = -2; while ( (pos = path.find( "//", pos )) != -1) { if (refPos == -2) refPos = path.find("#", 0); if (refPos > 0 && pos >= refPos) break; if ( pos == 0 || path[pos-1] != ':' ) path.remove( pos, 1 ); else pos += 2; } while ( (pos = path.find( "/./" )) != -1) path.remove( pos, 2 ); //kdDebug() << "checkPseudoState " << path << endl;}static void checkPseudoState( const CSSStyleSelector::Encodedurl& encodedurl, DOM::ElementImpl *e ){ if( e->id() != ID_A ) { pseudoState = PseudoNone; return; } DOMString attr = e->getAttribute(ATTR_HREF); if( attr.isNull() ) { pseudoState = PseudoNone; return; } QConstString cu(attr.unicode(), attr.length()); QString u = cu.string(); if ( !u.contains("://") ) { if ( u[0] == '/' ) u = encodedurl.host + u; else if ( u[0] == '#' ) u = encodedurl.file + u; else u = encodedurl.path + u; cleanpath( u ); } //completeURL( attr.string() ); bool contains = KHTMLFactory::vLinks()->contains( u ); if ( !contains && u.contains('/')==2 ) contains = KHTMLFactory::vLinks()->contains( u+'/' ); pseudoState = contains ? PseudoVisited : PseudoLink;}// a helper function for parsing nth-argumentsstatic inline bool matchNth(int count, const QString& nth){ if (nth.isEmpty()) return false; int a = 0; int b = 0; if (nth == "odd") { a = 2; b = 1; } else if (nth == "even") { a = 2; b = 0; } else { int n = nth.find('n'); if (n != -1) { if (nth[0] == '-') if (n==1) a = -1; else a = nth.mid(1,n-1).toInt(); else if (n==0) a = 1; else a = nth.left(n).toInt(); int p = nth.find('+'); if (p != -1) b = nth.mid(p+1).toInt(); else { p = nth.find('-'); b = -nth.mid(p+1).toInt(); } } else { b = nth.toInt(); } } if (a == 0) return count == b; else if (a > 0) if (count < b) return false; else return (count - b) % a == 0; else if (a < 0) { if (count > b) return false; else return (b - count) % (-a) == 0; } return false;}void CSSStyleSelector::checkSelector(int selIndex, DOM::ElementImpl *e){ dynamicPseudo = RenderStyle::NOPSEUDO; NodeImpl *n = e; selectorCache[ selIndex ].state = Invalid; CSSSelector *sel = selectors[ selIndex ]; // we have the subject part of the selector subject = true; // We track whether or not the rule contains only :hover and :active in a simple selector. If // so, we can't allow that to apply to every element on the page. We assume the author intended // to apply the rules only to links. bool onlyHoverActive = (sel->tag == anyQName && (sel->match == CSSSelector::PseudoClass && (sel->pseudoType() == CSSSelector::PseudoHover || sel->pseudoType() == CSSSelector::PseudoActive))); bool affectedByHover = style->affectedByHoverRules(); bool affectedByActive = style->affectedByActiveRules(); // first selector has to match if(!checkOneSelector(sel, e)) return; // check the subselectors CSSSelector::Relation relation = sel->relation; while((sel = sel->tagHistory)) { if(!n->isElementNode()) return; switch(relation) { case CSSSelector::Descendant: { bool found = false; while(!found) { subject = false; n = n->parentNode(); if(!n || !n->isElementNode()) return; ElementImpl *elem = static_cast<ElementImpl *>(n); if(checkOneSelector(sel, elem)) found = true; } break; } case CSSSelector::Child: { subject = false; n = n->parentNode(); if (!strictParsing) while (n && n->implicitNode()) n = n->parentNode(); if(!n || !n->isElementNode()) return; ElementImpl *elem = static_cast<ElementImpl *>(n); if(!checkOneSelector(sel, elem)) return; break; } case CSSSelector::DirectAdjacent: { subject = false; n = n->previousSibling(); while( n && !n->isElementNode() ) n = n->previousSibling(); if( !n ) return; ElementImpl *elem = static_cast<ElementImpl *>(n); if(!checkOneSelector(sel, elem)) return; break; } case CSSSelector::IndirectAdjacent: { subject = false; ElementImpl *elem = 0; do { n = n->previousSibling(); while( n && !n->isElementNode() ) n = n->previousSibling(); if( !n ) return; elem = static_cast<ElementImpl *>(n); } while (!checkOneSelector(sel, elem)); break; } case CSSSelector::SubSelector: { if (onlyHoverActive) onlyHoverActive = (sel->match == CSSSelector::PseudoClass && (sel->pseudoType() == CSSSelector::PseudoHover || sel->pseudoType() == CSSSelector::PseudoActive)); //kdDebug() << "CSSOrderedRule::checkSelector" << endl; ElementImpl *elem = static_cast<ElementImpl *>(n); // a selector is invalid if something follows :first-xxx if ( dynamicPseudo != RenderStyle::NOPSEUDO ) { return; } if(!checkOneSelector(sel, elem)) return; //kdDebug() << "CSSOrderedRule::checkSelector: passed" << endl; break; } } relation = sel->relation; } // disallow *:hover, *:active, and *:hover:active except for links if (onlyHoverActive && subject) { if (pseudoState == PseudoUnknown) checkPseudoState( encodedurl, e ); if (pseudoState == PseudoNone) { if (!affectedByHover && style->affectedByHoverRules()) style->setAffectedByHoverRules(false); if (!affectedByActive && style->affectedByActiveRules()) style->setAffectedByActiveRules(false); return; } } if ( dynamicPseudo != RenderStyle::NOPSEUDO ) { selectorCache[selIndex].state = AppliesPseudo; selectors[ selIndex ]->pseudoId = dynamicPseudo; } else selectorCache[ selIndex ].state = Applies; //qDebug( "selector %d applies", selIndex ); //selectors[ selIndex ]->print(); return;}bool CSSStyleSelector::checkOneSelector(DOM::CSSSelector *sel, DOM::ElementImpl *e){ if(!e) return false; if (sel->tag != anyQName) { int eltID = e->id(); Q_UINT16 localName = localNamePart(eltID); Q_UINT16 ns = namespacePart(eltID); Q_UINT16 selLocalName = localNamePart(sel->tag); Q_UINT16 selNS = namespacePart(sel->tag); if (localName <= ID_LAST_TAG && e->isHTMLElement()) ns = xhtmlNamespace; // FIXME: Really want to move away from this complicated hackery and just // switch tags and attr names over to AtomicStrings. if ((selLocalName != anyLocalName && localName != selLocalName) || (selNS != anyNamespace && ns != selNS)) return false; } if(sel->attr) { DOMString value = e->getAttribute(sel->attr); if(value.isNull()) return false; // attribute is not set switch(sel->match) { case CSSSelector::Exact: /* attribut values are case insensitive in all HTML modes, even in the strict ones */ if ( e->getDocument()->htmlMode() != DocumentImpl::XHtml ) { if ( strcasecmp(sel->value, value) ) return false; } else { if ( strcmp(sel->value, value) ) return false; } break; case CSSSelector::Id: if( (strictParsing && strcmp(sel->value, value) ) || (!strictParsing && strcasecmp(sel->value, value))) return false; break; case CSSSelector::Set: break; case CSSSelector::List: { const QChar* s = value.unicode(); int l = value.length(); while( l && !s->isSpace() ) l--,s++; if (!l) { // There is no list, just a single item. We can avoid // allocing QStrings and just treat this as an exact // match check. if( (strictParsing && strcmp(sel->value, value) ) || (!strictParsing && strcasecmp(sel->value, value))) return false; break; } // The selector's value can't contain a space, or it's totally bogus. l = sel->value.find(' '); if (l != -1) return false; QString str = value.string(); QString selStr = sel->value.string(); const int selStrlen = selStr.length(); int pos = 0; for ( ;; ) { pos = str.find(selStr, pos, strictParsing); if ( pos == -1 ) return false; if ( pos == 0 || str[pos-1].isSpace() ) { uint endpos = pos + selStrlen; if ( endpos >= str.length() || str[endpos].isSpace() ) break; // We have a match. } ++pos; } break; } case CSSSelector::Contain: { //kdDebug( 6080 ) << "checking for contains match" << endl; QString str = value.string(); QString selStr = sel->value.string(); int pos = str.find(selStr, 0, strictParsing); if(pos == -1) return false; break; } case CSSSelector::Begin: { //kdDebug( 6080 ) << "checking for beginswith match" << endl; QString str = value.string(); QString selStr = sel->value.string(); int pos = str.find(selStr, 0, strictParsing); if(pos != 0) return false; break; } case CSSSelector::End: { //kdDebug( 6080 ) << "checking for endswith match" << endl;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?