| # khtml Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Source Files

# css_ruleimpl.cpp

    00001
    00021 #include "dom/css_rule.h"
    00022 #include "dom/css_stylesheet.h"
    00023 #include "dom/dom_exception.h"
    00024 #include "dom/dom_string.h"
    00025
    00026 #include "css/css_stylesheetimpl.h"
    00027 #include "css/css_valueimpl.h"
    00028 #include "css/cssparser.h"
    00029 #include "css/css_ruleimpl.h"
    00030
    00031 #include "misc/loader.h"
    00032 #include "misc/htmltags.h"
    00033 #include "misc/htmlattrs.h"
    00034 #include "xml/dom_docimpl.h"
    00035
    00036 using namespace DOM;
    00037
    00038 #include <kdebug.h>
    00039
    00040 CSSRuleImpl::CSSRuleImpl(StyleBaseImpl *parent)
    00041     : StyleBaseImpl(parent)
    00042 {
    00043     m_type = CSSRule::UNKNOWN_RULE;
    00044 }
    00045
    00046 CSSRuleImpl::~CSSRuleImpl()
    00047 {
    00048 }
    00049
    00050 unsigned short CSSRuleImpl::type() const
    00051 {
    00052     return m_type;
    00053 }
    00054
    00055 CSSStyleSheetImpl *CSSRuleImpl::parentStyleSheet() const
    00056 {
    00057     return ( m_parent && m_parent->isCSSStyleSheet() )  ?
    00058         static_cast<CSSStyleSheetImpl *>(m_parent) : 0;
    00059 }
    00060
    00061 CSSRuleImpl *CSSRuleImpl::parentRule() const
    00062 {
    00063     return ( m_parent && m_parent->isRule() )  ?
    00064         static_cast<CSSRuleImpl *>(m_parent) : 0;
    00065 }
    00066
    00067 DOM::DOMString CSSRuleImpl::cssText() const
    00068 {
    00069     // ###
    00070     return DOMString();
    00071 }
    00072
    00073 void CSSRuleImpl::setCssText(DOM::DOMString /*str*/)
    00074 {
    00075     // ###
    00076 }
    00077
    00078 // ---------------------------------------------------------------------------
    00079
    00080 CSSCharsetRuleImpl::CSSCharsetRuleImpl(StyleBaseImpl *parent)
    00081     : CSSRuleImpl(parent), m_encoding()
    00082 {
    00083     m_type = CSSRule::CHARSET_RULE;
    00084 }
    00085
    00086 CSSCharsetRuleImpl::~CSSCharsetRuleImpl()
    00087 {
    00088 }
    00089
    00090 // ---------------------------------------------------------------------------
    00091
    00092 CSSFontFaceRuleImpl::CSSFontFaceRuleImpl(StyleBaseImpl *parent)
    00093     : CSSRuleImpl(parent)
    00094 {
    00095     m_type = CSSRule::FONT_FACE_RULE;
    00096     m_style = 0;
    00097 }
    00098
    00099 CSSFontFaceRuleImpl::~CSSFontFaceRuleImpl()
    00100 {
    00101     if(m_style) m_style->deref();
    00102 }
    00103
    00104 CSSStyleDeclarationImpl *CSSFontFaceRuleImpl::style() const
    00105 {
    00106     return m_style;
    00107 }
    00108
    00109 // --------------------------------------------------------------------------
    00110
    00111 CSSImportRuleImpl::CSSImportRuleImpl( StyleBaseImpl *parent,
    00112                                       const DOM::DOMString &href,
    00113                                       const DOM::DOMString &media )
    00114     : CSSRuleImpl(parent)
    00115 {
    00116     m_type = CSSRule::IMPORT_RULE;
    00117
    00118     m_lstMedia = new MediaListImpl( this, media );
    00119     m_lstMedia->ref();
    00120
    00121     m_strHref = href;
    00122     m_styleSheet = 0;
    00123
    00124     m_cachedSheet = 0;
    00125
    00126     init();
    00127 }
    00128
    00129 CSSImportRuleImpl::~CSSImportRuleImpl()
    00130 {
    00131     if(m_lstMedia) m_lstMedia->deref();
    00132     if(m_styleSheet) {
    00133         m_styleSheet->setParent(0);
    00134         m_styleSheet->deref();
    00135     }
    00136
    00137     if(m_cachedSheet) m_cachedSheet->deref(this);
    00138 }
    00139
    00140 DOMString CSSImportRuleImpl::href() const
    00141 {
    00142     return m_strHref;
    00143 }
    00144
    00145 MediaListImpl *CSSImportRuleImpl::media() const
    00146 {
    00147     return m_lstMedia;
    00148 }
    00149
    00150 CSSStyleSheetImpl *CSSImportRuleImpl::styleSheet() const
    00151 {
    00152     return m_styleSheet;
    00153 }
    00154
    00155 void CSSImportRuleImpl::setStyleSheet(const DOM::DOMString &url, const DOM::DOMString &sheet)
    00156 {
    00157     if ( m_styleSheet ) {
    00158         m_styleSheet->setParent(0);
    00159         m_styleSheet->deref();
    00160     }
    00161     m_styleSheet = new CSSStyleSheetImpl(this, url);
    00162     m_styleSheet->ref();
    00163
    00164     CSSStyleSheetImpl *parent = parentStyleSheet();
    00165     m_styleSheet->parseString( sheet, parent ? parent->useStrictParsing() : true );
    00166     m_loading = false;
    00167
    00168     checkLoaded();
    00169 }
    00170
    00171 bool CSSImportRuleImpl::isLoading()
    00172 {
    00173     return ( m_loading || (m_styleSheet && m_styleSheet->isLoading()) );
    00174 }
    00175
    00176 void CSSImportRuleImpl::init()
    00177 {
    00178     khtml::DocLoader *docLoader = 0;
    00179     StyleBaseImpl *root = this;
    00180     StyleBaseImpl *parent;
    00181     while ( ( parent = root->parent()) )
    00182         root = parent;
    00183     if (root->isCSSStyleSheet())
    00184         docLoader = static_cast<CSSStyleSheetImpl*>(root)->docLoader();
    00185
    00186     DOMString absHref = m_strHref;
    00187     CSSStyleSheetImpl *parentSheet = parentStyleSheet();
    00188     if (!parentSheet->href().isNull()) {
    00189       // use parent styleheet's URL as the base URL
    00190       absHref = KURL(parentSheet->href().string(),m_strHref.string()).url();
    00191     }
    00192 /*
    00193     else {
    00194       // use documents's URL as the base URL
    00195       DocumentImpl *doc = static_cast<CSSStyleSheetImpl*>(root)->doc();
    00196       absHref = KURL(doc->URL(),m_strHref.string()).url();
    00197     }
    00198 */
    00199     // Check for a cycle in our import chain.  If we encounter a stylesheet
    00200     // in our parent chain with the same URL, then just bail.
    00201     for ( parent = static_cast<StyleBaseImpl*>( this )->parent();
    00202          parent;
    00203          parent = parent->parent() )
    00204         if ( absHref == parent->baseURL() )
    00205             return;
    00206
    00207     // ### pass correct charset here!!
    00208     m_cachedSheet = docLoader->requestStyleSheet(absHref, QString::null);
    00209
    00210     if (m_cachedSheet)
    00211     {
    00212       m_cachedSheet->ref(this);
    00213       m_loading = true;
    00214     }
    00215 }
    00216
    00217 // --------------------------------------------------------------------------
    00218
    00219
    00220 CSSMediaRuleImpl::CSSMediaRuleImpl(StyleBaseImpl *parent)
    00221     :   CSSRuleImpl( parent )
    00222 {
    00223     m_type = CSSRule::MEDIA_RULE;
    00224     m_lstMedia = 0;
    00225     m_lstCSSRules = new CSSRuleListImpl();
    00226     m_lstCSSRules->ref();
    00227 }
    00228
    00229 CSSMediaRuleImpl::CSSMediaRuleImpl( StyleBaseImpl *parent, const QChar *&curP,
    00230                               const QChar *endP, const DOM::DOMString &media )
    00231 :   CSSRuleImpl( parent )
    00232 {
    00233     m_type = CSSRule::MEDIA_RULE;
    00234     m_lstMedia = new MediaListImpl( this, media );
    00235     m_lstMedia->ref();
    00236     m_lstCSSRules = new CSSRuleListImpl();
    00237     m_lstCSSRules->ref();
    00238
    00239     // Parse CSS data
    00240     while( curP < endP )
    00241     {
    00242 //         kdDebug( 6080 ) << "Style rule: '" << QString( curP, endP - curP )
    00243 //                         << "'" << endl;
    00244         CSSRuleImpl *rule = parseStyleRule( curP, endP );
    00245         if ( rule ) {
    00246             rule->ref();
    00247             appendRule( rule );
    00248         }
    00249         if (!curP) break;
    00250         while( curP < endP && *curP == QChar( ' ' ) )
    00251             curP++;
    00252     }
    00253 }
    00254
    00255 CSSMediaRuleImpl::~CSSMediaRuleImpl()
    00256 {
    00257     if( m_lstMedia ) {
    00258         m_lstMedia->setParent( 0 );
    00259         m_lstMedia->deref();
    00260     }
    00261     m_lstCSSRules->deref();
    00262 }
    00263
    00264 MediaListImpl *CSSMediaRuleImpl::media() const
    00265 {
    00266     return m_lstMedia;
    00267 }
    00268
    00269 CSSRuleListImpl *CSSMediaRuleImpl::cssRules()
    00270 {
    00271     return m_lstCSSRules;
    00272 }
    00273
    00274 unsigned long CSSMediaRuleImpl::appendRule( CSSRuleImpl *rule )
    00275 {
    00276     return rule ? m_lstCSSRules->insertRule( rule, m_lstCSSRules->length() ) : 0;
    00277 }
    00278
    00279 unsigned long CSSMediaRuleImpl::insertRule( const DOMString &rule,
    00280                                             unsigned long index )
    00281 {
    00282     const QChar *curP = rule.unicode();
    00283     CSSRuleImpl *newRule = parseRule( curP, curP + rule.length() );
    00284
    00285     return newRule ? m_lstCSSRules->insertRule( newRule, index ) : 0;
    00286 }
    00287
    00288 void CSSMediaRuleImpl::deleteRule( unsigned long index )
    00289 {
    00290     m_lstCSSRules->deleteRule( index );
    00291 }
    00292
    00293 CSSRuleListImpl::~CSSRuleListImpl()
    00294 {
    00295     CSSRuleImpl* rule;
    00296     while ( !m_lstCSSRules.isEmpty() && ( rule = m_lstCSSRules.take( 0 ) ) )
    00297         rule->deref();
    00298 }
    00299
    00300 // ---------------------------------------------------------------------------
    00301
    00302 CSSPageRuleImpl::CSSPageRuleImpl(StyleBaseImpl *parent)
    00303     : CSSRuleImpl(parent)
    00304 {
    00305     m_type = CSSRule::PAGE_RULE;
    00306     m_style = 0;
    00307 }
    00308
    00309 CSSPageRuleImpl::~CSSPageRuleImpl()
    00310 {
    00311     if(m_style) m_style->deref();
    00312 }
    00313
    00314 CSSStyleDeclarationImpl *CSSPageRuleImpl::style() const
    00315 {
    00316     return m_style;
    00317 }
    00318
    00319 DOM::DOMString CSSPageRuleImpl::selectorText() const
    00320 {
    00321     // ###
    00322     return DOMString();
    00323 }
    00324
    00325 void CSSPageRuleImpl::setSelectorText(DOM::DOMString /*str*/)
    00326 {
    00327     // ###
    00328 }
    00329
    00330 // --------------------------------------------------------------------------
    00331
    00332 CSSStyleRuleImpl::CSSStyleRuleImpl(StyleBaseImpl *parent)
    00333     : CSSRuleImpl(parent)
    00334 {
    00335     m_type = CSSRule::STYLE_RULE;
    00336     m_style = 0;
    00337     m_selector = 0;
    00338 }
    00339
    00340 CSSStyleRuleImpl::~CSSStyleRuleImpl()
    00341 {
    00342     if(m_style) {
    00343         m_style->setParent( 0 );
    00344         m_style->deref();
    00345     }
    00346     delete m_selector;
    00347 }
    00348
    00349 CSSStyleDeclarationImpl *CSSStyleRuleImpl::style() const
    00350 {
    00351     return m_style;
    00352 }
    00353
    00354 DOM::DOMString CSSStyleRuleImpl::selectorText() const
    00355 {
    00356     if ( m_selector && m_selector->first() ) {
    00357         // ### m_selector will be a single selector hopefully. so ->first() will disappear
    00358         CSSSelector* cs = m_selector->first();
    00359         //cs->print(); // debug
    00360         return cs->selectorText();
    00361     }
    00362     return DOMString();
    00363 }
    00364
    00365 void CSSStyleRuleImpl::setSelectorText(DOM::DOMString /*str*/)
    00366 {
    00367     // ###
    00368 }
    00369
    00370 bool CSSStyleRuleImpl::parseString( const DOMString &/*string*/, bool )
    00371 {
    00372     // ###
    00373     return false;
    00374 }
    00375
    00376 void CSSStyleRuleImpl::setSelector( QPtrList<CSSSelector> *selector)
    00377 {
    00378     m_selector = selector;
    00379 }
    00380
    00381 void CSSStyleRuleImpl::setDeclaration( CSSStyleDeclarationImpl *style)
    00382 {
    00383     if ( m_style != style ) {
    00384     if(m_style) m_style->deref();
    00385     m_style = style;
    00386     if(m_style) m_style->ref();
    00387     }
    00388 }
    00389
    00390 void CSSStyleRuleImpl::setNonCSSHints()
    00391 {
    00392     CSSSelector *s = m_selector->first();
    00393     while ( s ) {
    00394         s->nonCSSHint = true;
    00395         s = m_selector->next();
    00396     }
    00397 }
    00398
    00399
    00400 // --------------------------------------------------------------------------
    00401
    00402 CSSUnknownRuleImpl::CSSUnknownRuleImpl(StyleBaseImpl *parent)
    00403     : CSSRuleImpl(parent)
    00404 {
    00405 }
    00406
    00407 CSSUnknownRuleImpl::~CSSUnknownRuleImpl()
    00408 {
    00409 }
    00410
    00411 // --------------------------------------------------------------------------
    00412
    00413 CSSRuleListImpl::CSSRuleListImpl()
    00414 {
    00415 }
    00416
    00417 unsigned long CSSRuleListImpl::length() const
    00418 {
    00419     return m_lstCSSRules.count();
    00420 }
    00421
    00422 CSSRuleImpl *CSSRuleListImpl::item ( unsigned long index )
    00423 {
    00424     return m_lstCSSRules.at( index );
    00425 }
    00426
    00427 void CSSRuleListImpl::deleteRule ( unsigned long index )
    00428 {
    00429     CSSRuleImpl *rule = m_lstCSSRules.take( index );
    00430     if( rule )
    00431         rule->deref();
    00432     else
    00433         ; // ### Throw INDEX_SIZE_ERR exception here (TODO)
    00434 }
    00435
    00436 unsigned long CSSRuleListImpl::insertRule( CSSRuleImpl *rule,
    00437                                            unsigned long index )
    00438 {
    00439     if( rule && m_lstCSSRules.insert( index, rule ) )
    00440     {
    00441         rule->ref();
    00442         return index;
    00443     }
    00444
    00445     // ### Should throw INDEX_SIZE_ERR exception instead! (TODO)
    00446     return 0;
    00447 }
    00448

This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 13:33:32 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001