cssstyleselector.cpp

来自「konqueror3 embedded版本, KDE环境下的当家浏览器的嵌入式版」· C++ 代码 · 共 1,879 行 · 第 1/5 页

CPP
1,879 行
字号
    if ( properties ) {	CSSOrderedProperty **prop = properties;	while ( *prop ) {	    delete (*prop);	    prop++;	}        delete [] properties;    }    selectors = 0;    properties = 0;    selectorCache = 0;}void CSSStyleSelector::buildLists(){    clearLists();    // collect all selectors and Properties in lists. Then transfer them to the array for faster lookup.    QPtrList<CSSSelector> selectorList;    CSSOrderedPropertyList propertyList;    if(m_medium == "print" && defaultPrintStyle)      defaultPrintStyle->collect( &selectorList, &propertyList, Default,        Default );    else if(defaultStyle) defaultStyle->collect( &selectorList, &propertyList,      Default, Default );    if (!strictParsing && defaultQuirksStyle)        defaultQuirksStyle->collect( &selectorList, &propertyList, Default, Default );    if(userStyle) userStyle->collect(&selectorList, &propertyList, User, UserImportant );    if(authorStyle) authorStyle->collect(&selectorList, &propertyList, Author, AuthorImportant );    selectors_size = selectorList.count();    selectors = new CSSSelector *[selectors_size];    CSSSelector *s = selectorList.first();    CSSSelector **sel = selectors;    while ( s ) {	*sel = s;	s = selectorList.next();	++sel;    }    selectorCache = new SelectorCache[selectors_size];    for ( unsigned int i = 0; i < selectors_size; i++ ) {        selectorCache[i].state = Unknown;        selectorCache[i].props_size = 0;        selectorCache[i].props = 0;    }    // presort properties. Should make the sort() calls in styleForElement faster.    propertyList.sort();    properties_size = propertyList.count() + 1;    properties = new CSSOrderedProperty *[ properties_size ];    CSSOrderedProperty *p = propertyList.first();    CSSOrderedProperty **prop = properties;    while ( p ) {	*prop = p;	p = propertyList.next();	++prop;    }    *prop = 0;    unsigned int* offsets = new unsigned int[selectors_size];    if(properties[0])	offsets[properties[0]->selector] = 0;    for(unsigned int p = 1; p < properties_size; ++p) {	if(!properties[p] || (properties[p]->selector != properties[p - 1]->selector)) {	    unsigned int sel = properties[p - 1]->selector;            int* newprops = new int[selectorCache[sel].props_size+2];            for ( unsigned int i=0; i < selectorCache[sel].props_size; i++ )                newprops[i] = selectorCache[sel].props[i];	    newprops[selectorCache[sel].props_size] = offsets[sel];	    newprops[selectorCache[sel].props_size+1] = p - offsets[sel];            delete [] selectorCache[sel].props;            selectorCache[sel].props = newprops;            selectorCache[sel].props_size += 2;	    if(properties[p]) {		sel = properties[p]->selector;		offsets[sel] = p;            }        }    }    delete [] offsets;}// ----------------------------------------------------------------------CSSOrderedRule::CSSOrderedRule(DOM::CSSStyleRuleImpl *r, DOM::CSSSelector *s, int _index){    rule = r;    if(rule) r->ref();    index = _index;    selector = s;}CSSOrderedRule::~CSSOrderedRule(){    if(rule) rule->deref();}// -----------------------------------------------------------------CSSStyleSelectorList::CSSStyleSelectorList()    : QPtrList<CSSOrderedRule>(){    setAutoDelete(true);}CSSStyleSelectorList::~CSSStyleSelectorList(){}void CSSStyleSelectorList::append( CSSStyleSheetImpl *sheet,                                   const DOMString &medium ){    if(!sheet || !sheet->isCSSStyleSheet()) return;    // No media implies "all", but if a medialist exists it must    // contain our current medium    if( sheet->media() && !sheet->media()->contains( medium ) )        return; // style sheet not applicable for this medium    int len = sheet->length();    for(int i = 0; i< len; i++)    {        StyleBaseImpl *item = sheet->item(i);        if(item->isStyleRule())        {            CSSStyleRuleImpl *r = static_cast<CSSStyleRuleImpl *>(item);            QPtrList<CSSSelector> *s = r->selector();            for(int j = 0; j < (int)s->count(); j++)            {                CSSOrderedRule *rule = new CSSOrderedRule(r, s->at(j), count());		QPtrList<CSSOrderedRule>::append(rule);                //kdDebug( 6080 ) << "appending StyleRule!" << endl;            }        }        else if(item->isImportRule())        {            CSSImportRuleImpl *import = static_cast<CSSImportRuleImpl *>(item);            //kdDebug( 6080 ) << "@import: Media: "            //                << import->media()->mediaText().string() << endl;            if( !import->media() || import->media()->contains( medium ) )            {                CSSStyleSheetImpl *importedSheet = import->styleSheet();                append( importedSheet, medium );            }        }        else if( item->isMediaRule() )        {            CSSMediaRuleImpl *r = static_cast<CSSMediaRuleImpl *>( item );            CSSRuleListImpl *rules = r->cssRules();            //DOMString mediaText = media->mediaText();            //kdDebug( 6080 ) << "@media: Media: "            //                << r->media()->mediaText().string() << endl;            if( ( !r->media() || r->media()->contains( medium ) ) && rules)            {                // Traverse child elements of the @import rule. Since                // many elements are not allowed as child we do not use                // a recursive call to append() here                for( unsigned j = 0; j < rules->length(); j++ )                {                    //kdDebug( 6080 ) << "*** Rule #" << j << endl;                    CSSRuleImpl *childItem = rules->item( j );                    if( childItem->isStyleRule() )                    {                        // It is a StyleRule, so append it to our list                        CSSStyleRuleImpl *styleRule =                                static_cast<CSSStyleRuleImpl *>( childItem );                        QPtrList<CSSSelector> *s = styleRule->selector();                        for( int j = 0; j < ( int ) s->count(); j++ )                        {                            CSSOrderedRule *orderedRule = new CSSOrderedRule(                                            styleRule, s->at( j ), count() );                	    QPtrList<CSSOrderedRule>::append( orderedRule );                        }                    }                    else                    {                        //kdDebug( 6080 ) << "Ignoring child rule of "                        //    "ImportRule: rule is not a StyleRule!" << endl;                    }                }   // for rules            }   // if rules            else            {                //kdDebug( 6080 ) << "CSSMediaRule not rendered: "                //                << "rule empty or wrong medium!" << endl;            }        }        // ### include other rules    }}void CSSStyleSelectorList::collect( QPtrList<CSSSelector> *selectorList, CSSOrderedPropertyList *propList,				    Source regular, Source important ){    CSSOrderedRule *r = first();    while( r ) {	CSSSelector *sel = selectorList->first();	int selectorNum = 0;	while( sel ) {	    if ( *sel == *(r->selector) )		break;	    sel = selectorList->next();	    selectorNum++;	}	if ( !sel )	    selectorList->append( r->selector );//	else//	    qDebug("merged one selector");	propList->append(r->rule->declaration(), selectorNum, r->selector->specificity(), regular, important );	r = next();    }}// -------------------------------------------------------------------------int CSSOrderedPropertyList::compareItems(QPtrCollection::Item i1, QPtrCollection::Item i2){    int diff =  static_cast<CSSOrderedProperty *>(i1)->priority        - static_cast<CSSOrderedProperty *>(i2)->priority;    return diff ? diff : static_cast<CSSOrderedProperty *>(i1)->position        - static_cast<CSSOrderedProperty *>(i2)->position;}void CSSOrderedPropertyList::append(DOM::CSSStyleDeclarationImpl *decl, uint selector, uint specificity,				    Source regular, Source important ){    QPtrList<CSSProperty> *values = decl->values();    if(!values) return;    int len = values->count();    for(int i = 0; i < len; i++)    {        CSSProperty *prop = values->at(i);	Source source = regular;	if( prop->m_bImportant ) source = important;	if( prop->nonCSSHint ) source = NonCSSHint;	bool first = false;        // give special priority to font-xxx, color properties        switch(prop->m_id)        {        case CSS_PROP_FONT_STYLE:	case CSS_PROP_FONT_SIZE:	case CSS_PROP_FONT_WEIGHT:        case CSS_PROP_FONT_FAMILY:        case CSS_PROP_FONT:        case CSS_PROP_COLOR:        case CSS_PROP_BACKGROUND_IMAGE:        case CSS_PROP_DISPLAY:            // these have to be applied first, because other properties use the computed            // values of these porperties.	    first = true;            break;        default:            break;        }	QPtrList<CSSOrderedProperty>::append(new CSSOrderedProperty(prop, selector,								 first, source, specificity,								 count() ));    }}// -------------------------------------------------------------------------------------// this is mostly boring stuff on how to apply a certain rule to the renderstyle...static Length convertToLength( CSSPrimitiveValueImpl *primitiveValue, RenderStyle *style, QPaintDeviceMetrics *paintDeviceMetrics, bool *ok = 0 ){    Length l;    if ( !primitiveValue ) {	if ( ok )            *ok = false;    } else {	int type = primitiveValue->primitiveType();	if(type > CSSPrimitiveValue::CSS_PERCENTAGE && type < CSSPrimitiveValue::CSS_DEG)	    l = Length(primitiveValue->computeLength(style, paintDeviceMetrics), Fixed);	else if(type == CSSPrimitiveValue::CSS_PERCENTAGE)	    l = Length(int(primitiveValue->floatValue(CSSPrimitiveValue::CSS_PERCENTAGE)), Percent);	else if(type == CSSPrimitiveValue::CSS_NUMBER)	    l = Length(int(primitiveValue->floatValue(CSSPrimitiveValue::CSS_NUMBER)*100), Percent);	else if (type == CSSPrimitiveValue::CSS_HTML_RELATIVE)	    l = Length(int(primitiveValue->floatValue(CSSPrimitiveValue::CSS_HTML_RELATIVE)), Relative);	else if ( ok )	    *ok = false;    }    return l;}// color mapping codestruct colorMap {    int css_value;    QRgb color;};static const colorMap cmap[] = {    { CSS_VAL_AQUA, 0xFF00FFFF },    { CSS_VAL_BLACK, 0xFF000000 },    { CSS_VAL_BLUE, 0xFF0000FF },    { CSS_VAL_CRIMSON, 0xFFDC143C },    { CSS_VAL_FUCHSIA, 0xFFFF00FF },    { CSS_VAL_GRAY, 0xFF808080 },    { CSS_VAL_GREEN, 0xFF008000  },    { CSS_VAL_INDIGO, 0xFF4B0082 },    { CSS_VAL_LIME, 0xFF00FF00 },    { CSS_VAL_MAROON, 0xFF800000 },    { CSS_VAL_NAVY, 0xFF000080 },    { CSS_VAL_OLIVE, 0xFF808000  },    { CSS_VAL_ORANGE, 0xFFFFA500 },    { CSS_VAL_PURPLE, 0xFF800080 },    { CSS_VAL_RED, 0xFFFF0000 },    { CSS_VAL_SILVER, 0xFFC0C0C0 },    { CSS_VAL_TEAL, 0xFF008080  },    { CSS_VAL_WHITE, 0xFFFFFFFF },    { CSS_VAL_YELLOW, 0xFFFFFF00 },    { CSS_VAL_INVERT, invertedColor },    { CSS_VAL_TRANSPARENT, transparentColor },    { CSS_VAL_GREY, 0xff808080 },    { 0, 0 }};struct uiColors {    int css_value;    const char * configGroup;    const char * configEntry;QPalette::ColorGroup group;QColorGroup::ColorRole role;};const char * const wmgroup = "WM";const char * const generalgroup = "General";/* Mapping system settings to CSS 2* Tried hard to get an appropriate mapping - schlpbch*/static const uiColors uimap[] = {	// Active window border.    { CSS_VAL_ACTIVEBORDER, wmgroup, "background", QPalette::Active, QColorGroup::Light },	// Active window caption.    { CSS_VAL_ACTIVECAPTION, wmgroup, "background", QPalette::Active, QColorGroup::Text },        // Text in caption, size box, and scrollbar arrow box.    { CSS_VAL_CAPTIONTEXT, wmgroup, "activeForeground", QPalette::Active, QColorGroup::Text },	// Face color for three-dimensional display elements.    { CSS_VAL_BUTTONFACE, wmgroup, 0, QPalette::Inactive, QColorGroup::Button },	// Dark shadow for three-dimensional display elements (for edges facing away from the light source).    { CSS_VAL_BUTTONHIGHLIGHT, wmgroup, 0, QPalette::Inactive, QColorGroup::Light },	// Shadow color for three-dimensional display elements.    { CSS_VAL_BUTTONSHADOW, wmgroup, 0, QPalette::Inactive, QColorGroup::Shadow },	// Text on push buttons.    { CSS_VAL_BUTTONTEXT, wmgroup, "buttonForeground", QPalette::Inactive, QColorGroup::ButtonText },	// Dark shadow for three-dimensional display elements.    { CSS_VAL_THREEDDARKSHADOW, wmgroup, 0, QPalette::Inactive, QColorGroup::Dark },	// Face color for three-dimensional display elements.    { CSS_VAL_THREEDFACE, wmgroup, 0, QPalette::Inactive, QColorGroup::Button },	// Highlight color for three-dimensional display elements.    { CSS_VAL_THREEDHIGHLIGHT, wmgroup, 0, QPalette::Inactive, QColorGroup::Light },	// Light color for three-dimensional display elements (for edges facing the light source).    { CSS_V

⌨️ 快捷键说明

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