khtml Library API Documentation

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     // ### pass correct charset here!!
00200     m_cachedSheet = docLoader->requestStyleSheet(absHref, QString::null);
00201 
00202     if (m_cachedSheet)
00203     {
00204       m_cachedSheet->ref(this);
00205       m_loading = true;
00206     }
00207 }
00208 
00209 // --------------------------------------------------------------------------
00210 
00211 
00212 CSSMediaRuleImpl::CSSMediaRuleImpl(StyleBaseImpl *parent)
00213     :   CSSRuleImpl( parent )
00214 {
00215     m_type = CSSRule::MEDIA_RULE;
00216     m_lstMedia = 0;
00217     m_lstCSSRules = new CSSRuleListImpl();
00218     m_lstCSSRules->ref();
00219 }
00220 
00221 CSSMediaRuleImpl::CSSMediaRuleImpl( StyleBaseImpl *parent, const QChar *&curP,
00222                               const QChar *endP, const DOM::DOMString &media )
00223 :   CSSRuleImpl( parent )
00224 {
00225     m_type = CSSRule::MEDIA_RULE;
00226     m_lstMedia = new MediaListImpl( this, media );
00227     m_lstMedia->ref();
00228     m_lstCSSRules = new CSSRuleListImpl();
00229     m_lstCSSRules->ref();
00230 
00231     // Parse CSS data
00232     while( curP < endP )
00233     {
00234 //         kdDebug( 6080 ) << "Style rule: '" << QString( curP, endP - curP )
00235 //                         << "'" << endl;
00236         CSSRuleImpl *rule = parseStyleRule( curP, endP );
00237         if ( rule ) {
00238             rule->ref();
00239             appendRule( rule );
00240         }
00241         if (!curP) break;
00242         while( curP < endP && *curP == QChar( ' ' ) )
00243             curP++;
00244     }
00245 }
00246 
00247 CSSMediaRuleImpl::~CSSMediaRuleImpl()
00248 {
00249     if( m_lstMedia ) {
00250     m_lstMedia->setParent( 0 );
00251         m_lstMedia->deref();
00252     }
00253     m_lstCSSRules->deref();
00254 }
00255 
00256 MediaListImpl *CSSMediaRuleImpl::media() const
00257 {
00258     return m_lstMedia;
00259 }
00260 
00261 CSSRuleListImpl *CSSMediaRuleImpl::cssRules()
00262 {
00263     return m_lstCSSRules;
00264 }
00265 
00266 unsigned long CSSMediaRuleImpl::appendRule( CSSRuleImpl *rule )
00267 {
00268     return rule ? m_lstCSSRules->insertRule( rule, m_lstCSSRules->length() ) : 0;
00269 }
00270 
00271 unsigned long CSSMediaRuleImpl::insertRule( const DOMString &rule,
00272                                             unsigned long index )
00273 {
00274     const QChar *curP = rule.unicode();
00275     CSSRuleImpl *newRule = parseRule( curP, curP + rule.length() );
00276 
00277     return newRule ? m_lstCSSRules->insertRule( newRule, index ) : 0;
00278 }
00279 
00280 void CSSMediaRuleImpl::deleteRule( unsigned long index )
00281 {
00282     m_lstCSSRules->deleteRule( index );
00283 }
00284 
00285 CSSRuleListImpl::~CSSRuleListImpl()
00286 {
00287     CSSRuleImpl* rule;
00288     while ( !m_lstCSSRules.isEmpty() && ( rule = m_lstCSSRules.take( 0 ) ) )
00289         rule->deref();
00290 }
00291 
00292 // ---------------------------------------------------------------------------
00293 
00294 CSSPageRuleImpl::CSSPageRuleImpl(StyleBaseImpl *parent)
00295     : CSSRuleImpl(parent)
00296 {
00297     m_type = CSSRule::PAGE_RULE;
00298     m_style = 0;
00299 }
00300 
00301 CSSPageRuleImpl::~CSSPageRuleImpl()
00302 {
00303     if(m_style) m_style->deref();
00304 }
00305 
00306 CSSStyleDeclarationImpl *CSSPageRuleImpl::style() const
00307 {
00308     return m_style;
00309 }
00310 
00311 DOM::DOMString CSSPageRuleImpl::selectorText() const
00312 {
00313     // ###
00314     return DOMString();
00315 }
00316 
00317 void CSSPageRuleImpl::setSelectorText(DOM::DOMString /*str*/)
00318 {
00319     // ###
00320 }
00321 
00322 // --------------------------------------------------------------------------
00323 
00324 CSSStyleRuleImpl::CSSStyleRuleImpl(StyleBaseImpl *parent)
00325     : CSSRuleImpl(parent)
00326 {
00327     m_type = CSSRule::STYLE_RULE;
00328     m_style = 0;
00329     m_selector = 0;
00330 }
00331 
00332 CSSStyleRuleImpl::~CSSStyleRuleImpl()
00333 {
00334     if(m_style) {
00335     m_style->setParent( 0 );
00336     m_style->deref();
00337     }
00338     delete m_selector;
00339 }
00340 
00341 CSSStyleDeclarationImpl *CSSStyleRuleImpl::style() const
00342 {
00343     return m_style;
00344 }
00345 
00346 DOM::DOMString CSSStyleRuleImpl::selectorText() const
00347 {
00348     if ( m_selector && m_selector->first() ) {
00349         // ### m_selector will be a single selector hopefully. so ->first() will disappear
00350         CSSSelector* cs = m_selector->first();
00351         //cs->print(); // debug
00352         return cs->selectorText();
00353     }
00354     return DOMString();
00355 }
00356 
00357 void CSSStyleRuleImpl::setSelectorText(DOM::DOMString /*str*/)
00358 {
00359     // ###
00360 }
00361 
00362 bool CSSStyleRuleImpl::parseString( const DOMString &/*string*/, bool )
00363 {
00364     // ###
00365     return false;
00366 }
00367 
00368 void CSSStyleRuleImpl::setSelector( QPtrList<CSSSelector> *selector)
00369 {
00370     m_selector = selector;
00371 }
00372 
00373 void CSSStyleRuleImpl::setDeclaration( CSSStyleDeclarationImpl *style)
00374 {
00375     if ( m_style != style ) {
00376     if(m_style) m_style->deref();
00377     m_style = style;
00378     if(m_style) m_style->ref();
00379     }
00380 }
00381 
00382 void CSSStyleRuleImpl::setNonCSSHints()
00383 {
00384     CSSSelector *s = m_selector->first();
00385     while ( s ) {
00386     s->nonCSSHint = true;
00387     s = m_selector->next();
00388     }
00389 }
00390 
00391 
00392 // --------------------------------------------------------------------------
00393 
00394 CSSUnknownRuleImpl::CSSUnknownRuleImpl(StyleBaseImpl *parent)
00395     : CSSRuleImpl(parent)
00396 {
00397 }
00398 
00399 CSSUnknownRuleImpl::~CSSUnknownRuleImpl()
00400 {
00401 }
00402 
00403 // --------------------------------------------------------------------------
00404 
00405 CSSRuleListImpl::CSSRuleListImpl()
00406 {
00407 }
00408 
00409 unsigned long CSSRuleListImpl::length() const
00410 {
00411     return m_lstCSSRules.count();
00412 }
00413 
00414 CSSRuleImpl *CSSRuleListImpl::item ( unsigned long index )
00415 {
00416     return m_lstCSSRules.at( index );
00417 }
00418 
00419 void CSSRuleListImpl::deleteRule ( unsigned long index )
00420 {
00421     CSSRuleImpl *rule = m_lstCSSRules.take( index );
00422     if( rule )
00423         rule->deref();
00424     else
00425         ; // ### Throw INDEX_SIZE_ERR exception here (TODO)
00426 }
00427 
00428 unsigned long CSSRuleListImpl::insertRule( CSSRuleImpl *rule,
00429                                            unsigned long index )
00430 {
00431     if( rule && m_lstCSSRules.insert( index, rule ) )
00432     {
00433         rule->ref();
00434         return index;
00435     }
00436 
00437     // ### Should throw INDEX_SIZE_ERR exception instead! (TODO)
00438     return 0;
00439 }
00440 
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.0.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Oct 8 12:22:34 2003 by doxygen 1.2.18 written by Dimitri van Heesch, © 1997-2001