Source Code (Use browser search to find items of interest.)

Class Index

kdelibs'CSSStyleSelector (./kdelibs/khtml/css/cssstyleselector.h:68)

    class CSSStyleSelector : public StyleSelector
    {
    public:
	/**
	 * creates a new StyleSelector for a XML Document.
	 * goes through all StyleSheets defined in the document and
	 * creates a list of rules it needs to apply to objects
	 *
	 * Basically the same as the constructor for a HTMLDocument,
	 * but the defaultStyle (which is html only) will not be used.
	 */
	CSSStyleSelector(DOM::DocumentImpl *doc);
	/**
	 * does the same for an HTML document. Initializes the default style
	 * for html.
	 */
	CSSStyleSelector(DOM::HTMLDocumentImpl *doc);
	/**
	 * same as above but for a single stylesheet.
	 */
	CSSStyleSelector(DOM::StyleSheetImpl *sheet);

	virtual ~CSSStyleSelector();
	
	void addSheet(DOM::StyleSheetImpl *sheet);
	
	static void loadDefaultStyle();
	static void setUserStyle(DOM::StyleSheetImpl *sheet);

	virtual RenderStyle *styleForElement(DOM::ElementImpl *e);
	
    protected:
	static CSSStyleSelectorList *defaultStyle;
	static CSSStyleSelectorList *userStyle;
	CSSStyleSelectorList *authorStyle;
    };

    /*
     * List of properties that get applied to the Element. We need to collect them first
     * and then apply them one by one, because we have to change the apply order.
     * Some properties depend on other one already being applied (for example all properties spezifying
     * some length need to have already the correct font size. Same applies to color
     *
     * While sorting them, we have to take care not to mix up the original order.
     */

kdelibs'CSSStyleSelector::CSSStyleSelector() (./kdelibs/khtml/css/cssstyleselector.cpp:64)

CSSStyleSelector::CSSStyleSelector(DocumentImpl * /*doc*/)
{
    // ### parse the xml for processing instructions containing style sheet info
    authorStyle = new CSSStyleSelectorList();
}


kdelibs'CSSStyleSelector::CSSStyleSelector() (./kdelibs/khtml/css/cssstyleselector.cpp:70)

CSSStyleSelector::CSSStyleSelector(HTMLDocumentImpl *doc)
{
    if(!defaultStyle) loadDefaultStyle();

    authorStyle = new CSSStyleSelectorList();
    // ### go through DOM tree for style elements

    NodeImpl *test = doc->firstChild(); // should be html element
    if(!test) return;
    test = test->firstChild();
    if(!test) return;
    while(test && (test->id() != ID_HEAD))
	test = test->nextSibling();
    if(!test) return;
    HTMLHeadElementImpl *head = static_cast<HTMLHeadElementImpl *>(test);

    // all LINK and STYLE elements have to be direct children of the HEAD element
    test = head->firstChild();
    while(test)
    {
	if(test->id() == ID_LINK)
	{
	    HTMLLinkElementImpl *link = static_cast<HTMLLinkElementImpl *>(test);
	    authorStyle->append(link->sheet());
	}
	else if(test->id() == ID_STYLE)
	{
	    HTMLStyleElementImpl *style = static_cast<HTMLStyleElementImpl *>(test);
	    authorStyle->append(style->sheet());
	}
	test = test->nextSibling();
    }

    HTMLElementImpl *e = doc->body();
    if(e && e->id() == ID_BODY)
    {
	kdDebug( 6080 ) << "found body element" << endl;
	HTMLBodyElementImpl *body = static_cast<HTMLBodyElementImpl *>(e);
	if(body->sheet())
	{
	    kdDebug( 6080 ) << "body has style sheet " << body->sheet() << endl;
	    authorStyle->append(body->sheet());
	}
    }
}


kdelibs'CSSStyleSelector::CSSStyleSelector() (./kdelibs/khtml/css/cssstyleselector.cpp:116)

CSSStyleSelector::CSSStyleSelector(StyleSheetImpl *sheet)
{
    if(!defaultStyle) loadDefaultStyle();

    authorStyle = new CSSStyleSelectorList();
    authorStyle->append(sheet);
}


kdelibs'CSSStyleSelector::~CSSStyleSelector() (./kdelibs/khtml/css/cssstyleselector.cpp:124)

CSSStyleSelector::~CSSStyleSelector()
{
    delete authorStyle;
}


kdelibs'CSSStyleSelector::addSheet() (./kdelibs/khtml/css/cssstyleselector.cpp:129)

void CSSStyleSelector::addSheet(StyleSheetImpl *sheet)
{
    authorStyle->append(sheet);
}


kdelibs'CSSStyleSelector::setUserStyle() (./kdelibs/khtml/css/cssstyleselector.cpp:134)

void CSSStyleSelector::setUserStyle(StyleSheetImpl *sheet)
{
    if(userStyle) delete userStyle;
    userStyle = new CSSStyleSelectorList();
    userStyle->append(sheet);
}


kdelibs'CSSStyleSelector::loadDefaultStyle() (./kdelibs/khtml/css/cssstyleselector.cpp:141)

void CSSStyleSelector::loadDefaultStyle()
{
    if(defaultStyle) return;

    QFile f(locate( "data", "khtml/css/html4.css" ) );
    f.open(IO_ReadOnly);

    QTextStream t( &f );
    QString style = t.read();
    DOMString str(style);

    // ### memory leak!
    DOM::CSSStyleSheetImpl *sheet = new DOM::CSSStyleSheetImpl((DOM::CSSStyleSheetImpl *)0);
    sheet->parseString( str );

    defaultStyle = new CSSStyleSelectorList();
    defaultStyle->append(sheet);
}

kdelibs'CSSStyleSelector::styleForElement() (./kdelibs/khtml/css/cssstyleselector.cpp:164)

RenderStyle *CSSStyleSelector::styleForElement(ElementImpl *e)
{
    CSSOrderedPropertyList *propsToApply = new CSSOrderedPropertyList();

    // the higher the offset or important number, the later the rules get applied.

    if(defaultStyle) defaultStyle->collect(propsToApply, e, 0x00100000); // no important rules here
    // important rules from user styles are higher than important rules from author styles.
    // for non important rules the order is reversed
    if(userStyle) userStyle->collect(propsToApply, e, 0x00200000, 0x04000000);
    if(authorStyle) authorStyle->collect(propsToApply, e, 0x00400000, 0x01000000);
    // these count as author rules, and come after all other style sheets
    if(e->styleRules()) propsToApply->append(e->styleRules(), 0x00800000, 0x02000000);

    propsToApply->sort();

    RenderStyle *style;
    if(e->parentNode())
    {
	assert(e->parentNode()->style() != 0);
	style = new RenderStyle(e->parentNode()->style());
    }
    else
	style = new RenderStyle();	

    for(int i = 0; i < (int)propsToApply->count(); i++)
	applyRule(style, propsToApply->at(i)->prop, e);
	
	
//    kdDebug( 6080 ) << "STYLE count=" << RenderStyle::counter << ", DATA count=" << SharedData::counter << endl;

    // experimental -antti
#if 0
    for ( int n=0; n<(int)lastStyles.count(); n++)
    {
    	style->mergeData(lastStyles.at(n));
    }

    lastStyles.append(style);

    if (lastStyles.count()>5)
    	lastStyles.removeFirst();
#endif

    delete propsToApply;

    return style;
}

// ----------------------------------------------------------------------