⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cssstyleselector.cpp

📁 linux下开源浏览器WebKit的源码,市面上的很多商用浏览器都是移植自WebKit
💻 CPP
📖 第 1 页 / 共 5 页
字号:
        while (n) {            if (canShareStyleWithElement(n))                return n->renderStyle();            if (count++ == cStyleSearchThreshold)                return 0;            for (n = n->previousSibling(); n && !n->isElementNode(); n = n->previousSibling()) { }        }        if (!n)             n = locateCousinList(m_element->parentElement());        while (n) {            if (canShareStyleWithElement(n))                return n->renderStyle();            if (count++ == cStyleSearchThreshold)                return 0;            for (n = n->previousSibling(); n && !n->isElementNode(); n = n->previousSibling()) { }        }            }    return 0;}void CSSStyleSelector::matchUARules(int& firstUARule, int& lastUARule){    // First we match rules from the user agent sheet.    CSSRuleSet* userAgentStyleSheet = m_medium->mediaTypeMatchSpecific("print")        ? defaultPrintStyle : defaultStyle;    matchRules(userAgentStyleSheet, firstUARule, lastUARule);    // In quirks mode, we match rules from the quirks user agent sheet.    if (!m_checker.m_strictParsing)        matchRules(defaultQuirksStyle, firstUARule, lastUARule);            // If we're in view source mode, then we match rules from the view source style sheet.    if (m_checker.m_document->frame() && m_checker.m_document->frame()->inViewSourceMode()) {        if (!defaultViewSourceStyle)            loadViewSourceStyle();        matchRules(defaultViewSourceStyle, firstUARule, lastUARule);    }}// If resolveForRootDefault is true, style based on user agent style sheet only. This is used in media queries, where// relative units are interpreted according to document root element style, styled only with UA stylesheetPassRefPtr<RenderStyle> CSSStyleSelector::styleForElement(Element* e, RenderStyle* defaultParent, bool allowSharing, bool resolveForRootDefault){    // Once an element has a renderer, we don't try to destroy it, since otherwise the renderer    // will vanish if a style recalc happens during loading.    if (allowSharing && !e->document()->haveStylesheetsLoaded() && !e->renderer()) {        if (!s_styleNotYetAvailable) {            s_styleNotYetAvailable = ::new RenderStyle;            s_styleNotYetAvailable->ref();            s_styleNotYetAvailable->setDisplay(NONE);            s_styleNotYetAvailable->font().update(m_fontSelector);        }        s_styleNotYetAvailable->ref();        e->document()->setHasNodesWithPlaceholderStyle();        return s_styleNotYetAvailable;    }    initElementAndPseudoState(e);    if (allowSharing) {        RenderStyle* sharedStyle = locateSharedStyle();        if (sharedStyle)            return sharedStyle;    }    initForStyleResolve(e, defaultParent);    m_style = RenderStyle::create();    if (m_parentStyle)        m_style->inheritFrom(m_parentStyle);    else        m_parentStyle = style();    if (simpleDefaultStyleSheet && !elementCanUseSimpleDefaultStyle(e))        loadFullDefaultStyle();#if ENABLE(SVG)    static bool loadedSVGUserAgentSheet;    if (e->isSVGElement() && !loadedSVGUserAgentSheet) {        // SVG rules.        loadedSVGUserAgentSheet = true;        CSSStyleSheet* svgSheet = parseUASheet(svgUserAgentStyleSheet, sizeof(svgUserAgentStyleSheet));        defaultStyle->addRulesFromSheet(svgSheet, screenEval());        defaultPrintStyle->addRulesFromSheet(svgSheet, printEval());    }#endif#if ENABLE(WML)    static bool loadedWMLUserAgentSheet;    if (e->isWMLElement() && !loadedWMLUserAgentSheet) {        // WML rules.        loadedWMLUserAgentSheet = true;        CSSStyleSheet* wmlSheet = parseUASheet(wmlUserAgentStyleSheet, sizeof(wmlUserAgentStyleSheet));        defaultStyle->addRulesFromSheet(wmlSheet, screenEval());        defaultPrintStyle->addRulesFromSheet(wmlSheet, printEval());    }#endif#if ENABLE(VIDEO)    static bool loadedMediaStyleSheet;    if (!loadedMediaStyleSheet && (e->hasTagName(videoTag) || e->hasTagName(audioTag))) {        loadedMediaStyleSheet = true;        String mediaRules = String(mediaControlsUserAgentStyleSheet, sizeof(mediaControlsUserAgentStyleSheet)) + theme()->extraMediaControlsStyleSheet();        CSSStyleSheet* mediaControlsSheet = parseUASheet(mediaRules);        defaultStyle->addRulesFromSheet(mediaControlsSheet, screenEval());        defaultPrintStyle->addRulesFromSheet(mediaControlsSheet, printEval());    }#endif    int firstUARule = -1, lastUARule = -1;    int firstUserRule = -1, lastUserRule = -1;    int firstAuthorRule = -1, lastAuthorRule = -1;    matchUARules(firstUARule, lastUARule);    if (!resolveForRootDefault) {        // 4. Now we check user sheet rules.        if (m_matchAuthorAndUserStyles)            matchRules(m_userStyle, firstUserRule, lastUserRule);        // 5. Now check author rules, beginning first with presentational attributes        // mapped from HTML.        if (m_styledElement) {            // Ask if the HTML element has mapped attributes.            if (m_styledElement->hasMappedAttributes()) {                // Walk our attribute list and add in each decl.                const NamedMappedAttrMap* map = m_styledElement->mappedAttributes();                for (unsigned i = 0; i < map->length(); i++) {                    Attribute* attr = map->attributeItem(i);                    if (attr->isMappedAttribute()) {                        MappedAttribute* mappedAttr = static_cast<MappedAttribute*>(attr);                        if (mappedAttr->decl()) {                            lastAuthorRule = m_matchedDecls.size();                            if (firstAuthorRule == -1)                                firstAuthorRule = lastAuthorRule;                            addMatchedDeclaration(mappedAttr->decl());                        }                    }                }            }            // Now we check additional mapped declarations.            // Tables and table cells share an additional mapped rule that must be applied            // after all attributes, since their mapped style depends on the values of multiple attributes.            if (m_styledElement->canHaveAdditionalAttributeStyleDecls()) {                m_additionalAttributeStyleDecls.clear();                m_styledElement->additionalAttributeStyleDecls(m_additionalAttributeStyleDecls);                if (!m_additionalAttributeStyleDecls.isEmpty()) {                    unsigned additionalDeclsSize = m_additionalAttributeStyleDecls.size();                    if (firstAuthorRule == -1)                        firstAuthorRule = m_matchedDecls.size();                    lastAuthorRule = m_matchedDecls.size() + additionalDeclsSize - 1;                    for (unsigned i = 0; i < additionalDeclsSize; i++)                        addMatchedDeclaration(m_additionalAttributeStyleDecls[i]);                }            }        }            // 6. Check the rules in author sheets next.        if (m_matchAuthorAndUserStyles)            matchRules(m_authorStyle, firstAuthorRule, lastAuthorRule);        // 7. Now check our inline style attribute.        if (m_matchAuthorAndUserStyles && m_styledElement) {            CSSMutableStyleDeclaration* inlineDecl = m_styledElement->inlineStyleDecl();            if (inlineDecl) {                lastAuthorRule = m_matchedDecls.size();                if (firstAuthorRule == -1)                    firstAuthorRule = lastAuthorRule;                addMatchedDeclaration(inlineDecl);            }        }    }    // Now we have all of the matched rules in the appropriate order.  Walk the rules and apply    // high-priority properties first, i.e., those properties that other properties depend on.    // The order is (1) high-priority not important, (2) high-priority important, (3) normal not important    // and (4) normal important.    m_lineHeightValue = 0;    applyDeclarations(true, false, 0, m_matchedDecls.size() - 1);    if (!resolveForRootDefault) {        applyDeclarations(true, true, firstAuthorRule, lastAuthorRule);        applyDeclarations(true, true, firstUserRule, lastUserRule);    }    applyDeclarations(true, true, firstUARule, lastUARule);        // If our font got dirtied, go ahead and update it now.    if (m_fontDirty)        updateFont();    // Line-height is set when we are sure we decided on the font-size    if (m_lineHeightValue)        applyProperty(CSSPropertyLineHeight, m_lineHeightValue);    // Now do the normal priority UA properties.    applyDeclarations(false, false, firstUARule, lastUARule);        // Cache our border and background so that we can examine them later.    cacheBorderAndBackground();        // Now do the author and user normal priority properties and all the !important properties.    if (!resolveForRootDefault) {        applyDeclarations(false, false, lastUARule + 1, m_matchedDecls.size() - 1);        applyDeclarations(false, true, firstAuthorRule, lastAuthorRule);        applyDeclarations(false, true, firstUserRule, lastUserRule);    }    applyDeclarations(false, true, firstUARule, lastUARule);        // If our font got dirtied by one of the non-essential font props,     // go ahead and update it a second time.    if (m_fontDirty)        updateFont();        // Clean up our style object's display and text decorations (among other fixups).    adjustRenderStyle(style(), e);    // If we are a link, cache the determined pseudo-state.    if (e->isLink())        m_style->setPseudoState(pseudoState);    // If we have first-letter pseudo style, do not share this style    if (m_style->hasPseudoStyle(FIRST_LETTER))        m_style->setUnique();    // Now return the style.    return m_style.release();}void CSSStyleSelector::keyframeStylesForAnimation(Element* e, const RenderStyle* elementStyle, KeyframeList& list){    list.clear();        // Get the keyframesRule for this name    if (!e || list.animationName().isEmpty())        return;                if (!m_keyframesRuleMap.contains(list.animationName().impl()))        return;            const WebKitCSSKeyframesRule* rule = m_keyframesRuleMap.find(list.animationName().impl()).get()->second.get();        // Construct and populate the style for each keyframe    for (unsigned i = 0; i < rule->length(); ++i) {        // Apply the declaration to the style. This is a simplified version of the logic in styleForElement        initElementAndPseudoState(e);        initForStyleResolve(e);                const WebKitCSSKeyframeRule* kf = rule->item(i);        addMatchedDeclaration(kf->style());        ASSERT(!m_style);        // Create the style        m_style = RenderStyle::clone(elementStyle);                m_lineHeightValue = 0;                // We don't need to bother with !important. Since there is only ever one        // decl, there's nothing to override. So just add the first properties.        applyDeclarations(true, false, 0, m_matchedDecls.size() - 1);                // If our font got dirtied, go ahead and update it now.        if (m_fontDirty)            updateFont();        // Line-height is set when we are sure we decided on the font-size        if (m_lineHeightValue)            applyProperty(CSSPropertyLineHeight, m_lineHeightValue);                // Now do rest of the properties.        applyDeclarations(false, false, 0, m_matchedDecls.size() - 1);                // If our font got dirtied by one of the non-essential font props,         // go ahead and update it a second time.        if (m_fontDirty)            updateFont();        // Add all the animating properties to the list        CSSMutableStyleDeclaration::const_iterator end = kf->style()->end();        for (CSSMutableStyleDeclaration::const_iterator it = kf->style()->begin(); it != end; ++it)            list.addProperty((*it).id());                // Add this keyframe style to all the indicated key times        Vector<float> keys;        kf->getKeys(keys);        for (size_t keyIndex = 0; keyIndex < keys.size(); ++keyIndex) {            float key = keys[keyIndex];            list.insert(key, m_style);        }        m_style = 0;    }        // Make sure there is a 0% and a 100% keyframe    float first = -1;    float last = -1;    if (list.size() >= 2) {        first = list.beginKeyframes()->key();        last = (list.endKeyframes()-1)->key();    }    if (first != 0 || last != 1)        list.clear();}PassRefPtr<RenderStyle> CSSStyleSelector::pseudoStyleForElement(PseudoId pseudo, Element* e, RenderStyle* parentStyle){    if (!e)        return 0;    initElementAndPseudoState(e);    initForStyleResolve(e, parentStyle, pseudo);    m_style = parentStyle;        // Since we don't use pseudo-elements in any of our quirk/print user agent rules, don't waste time walking    // those rules.        // Check UA, user and author rules.    int firstUARule = -1, lastUARule = -1, firstUserRule = -1, lastUserRule = -1, firstAuthorRule = -1, lastAuthorRule = -1;    matchUARules(firstUARule, lastUARule);    if (m_matchAuthorAndUserStyles) {        matchRules(m_userStyle, firstUserRule, lastUserRule);        matchRules(m_authorStyle, firstAuthorRule, lastAuthorRule);    }    if (m_matchedDecls.isEmpty())        return 0;        m_style = RenderStyle::create();    if (parentStyle)        m_style->inheritFrom(parentStyle);    m_style->noninherited_flags._styleType = pseudo;        m_lineHeightValue = 0;    // High-priority properties.    applyDeclarations(true, false, 0, m_matchedDecls.size() - 1);    applyDeclarations(true, true, firstAuthorRule, lastAuthorRule);    applyDeclarations(true, true, firstUserRule, lastUserRule);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -