khtml Library API Documentation

css_stylesheetimpl.cpp

00001 
00023 //#define CSS_STYLESHEET_DEBUG
00024 
00025 #include "dom/dom_string.h"
00026 #include "dom/dom_exception.h"
00027 #include "dom/css_stylesheet.h"
00028 #include "dom/css_rule.h"
00029 
00030 #include "css/css_ruleimpl.h"
00031 #include "css/css_valueimpl.h"
00032 #include "css/cssparser.h"
00033 #include "css/css_stylesheetimpl.h"
00034 
00035 #include "xml/dom_nodeimpl.h"
00036 #include "html/html_documentimpl.h"
00037 #include "misc/loader.h"
00038 
00039 #include <kdebug.h>
00040 
00041 using namespace DOM;
00042 using namespace khtml;
00043 // --------------------------------------------------------------------------------
00044 
00045 StyleSheetImpl::StyleSheetImpl(StyleSheetImpl *parentSheet, DOMString href)
00046     : StyleListImpl(parentSheet)
00047 {
00048     m_disabled = false;
00049     m_media = 0;
00050     m_parentNode = 0;
00051     m_strHref = href;
00052 }
00053 
00054 
00055 StyleSheetImpl::StyleSheetImpl(DOM::NodeImpl *parentNode, DOMString href)
00056     : StyleListImpl()
00057 {
00058     m_parentNode = parentNode;
00059     m_disabled = false;
00060     m_media = 0;
00061     m_strHref = href;
00062 }
00063 
00064 StyleSheetImpl::StyleSheetImpl(StyleBaseImpl *owner, DOMString href)
00065     : StyleListImpl(owner)
00066 {
00067     m_disabled = false;
00068     m_media = 0;
00069     m_parentNode = 0;
00070     m_strHref = href;
00071 }
00072 
00073 StyleSheetImpl::~StyleSheetImpl()
00074 {
00075     if(m_media) {
00076     m_media->setParent( 0 );
00077     m_media->deref();
00078     }
00079 }
00080 
00081 StyleSheetImpl *StyleSheetImpl::parentStyleSheet() const
00082 {
00083     if( !m_parent ) return 0;
00084     if( m_parent->isStyleSheet() ) return static_cast<StyleSheetImpl *>(m_parent);
00085     return 0;
00086 }
00087 
00088 void StyleSheetImpl::setMedia( MediaListImpl *media )
00089 {
00090     if( media )
00091     media->ref();
00092     if( m_media )
00093     m_media->deref();
00094     m_media = media;
00095 }
00096 
00097 // -----------------------------------------------------------------------
00098 
00099 
00100 CSSStyleSheetImpl::CSSStyleSheetImpl(CSSStyleSheetImpl *parentSheet, DOMString href)
00101     : StyleSheetImpl(parentSheet, href)
00102 {
00103     m_lstChildren = new QPtrList<StyleBaseImpl>;
00104     m_doc = 0;
00105     m_implicit = false;
00106 }
00107 
00108 CSSStyleSheetImpl::CSSStyleSheetImpl(DOM::NodeImpl *parentNode, DOMString href, bool _implicit)
00109     : StyleSheetImpl(parentNode, href)
00110 {
00111     m_lstChildren = new QPtrList<StyleBaseImpl>;
00112     m_doc = parentNode->getDocument();
00113     m_implicit = _implicit;
00114 }
00115 
00116 CSSStyleSheetImpl::CSSStyleSheetImpl(CSSRuleImpl *ownerRule, DOMString href)
00117     : StyleSheetImpl(ownerRule, href)
00118 {
00119     m_lstChildren = new QPtrList<StyleBaseImpl>;
00120     m_doc = 0;
00121     m_implicit = false;
00122 }
00123 
00124 CSSStyleSheetImpl::CSSStyleSheetImpl(DOM::NodeImpl *parentNode, CSSStyleSheetImpl *orig)
00125     : StyleSheetImpl(parentNode, orig->m_strHref)
00126 {
00127     m_lstChildren = new QPtrList<StyleBaseImpl>;
00128     StyleBaseImpl *rule;
00129     for ( rule = orig->m_lstChildren->first(); rule != 0; rule = orig->m_lstChildren->next() )
00130     {
00131         m_lstChildren->append(rule);
00132         rule->setParent(this);
00133     }
00134     m_doc = parentNode->getDocument();
00135     m_implicit = false;
00136 }
00137 
00138 CSSStyleSheetImpl::CSSStyleSheetImpl(CSSRuleImpl *ownerRule, CSSStyleSheetImpl *orig)
00139     : StyleSheetImpl(ownerRule, orig->m_strHref)
00140 {
00141     // m_lstChildren is deleted in StyleListImpl
00142     m_lstChildren = new QPtrList<StyleBaseImpl>;
00143     StyleBaseImpl *rule;
00144     for ( rule = orig->m_lstChildren->first(); rule != 0; rule = orig->m_lstChildren->next() )
00145     {
00146         m_lstChildren->append(rule);
00147         rule->setParent(this);
00148     }
00149     m_doc  = 0;
00150     m_implicit = false;
00151 }
00152 
00153 CSSRuleImpl *CSSStyleSheetImpl::ownerRule() const
00154 {
00155     if( !m_parent ) return 0;
00156     if( m_parent->isRule() ) return static_cast<CSSRuleImpl *>(m_parent);
00157     return 0;
00158 }
00159 
00160 unsigned long CSSStyleSheetImpl::insertRule( const DOMString &rule, unsigned long index, int &exceptioncode )
00161 {
00162     exceptioncode = 0;
00163     if(index > m_lstChildren->count()) {
00164         exceptioncode = DOMException::INDEX_SIZE_ERR;
00165         return 0;
00166     }
00167     CSSParser p( strictParsing );
00168     CSSRuleImpl *r = p.parseRule( this, rule );
00169 
00170     if(!r) {
00171         exceptioncode = CSSException::SYNTAX_ERR + CSSException::_EXCEPTION_OFFSET;
00172         return 0;
00173     }
00174     // ###
00175     // HIERARCHY_REQUEST_ERR: Raised if the rule cannot be inserted at the specified index e.g. if an
00176     //@import rule is inserted after a standard rule set or other at-rule.
00177     m_lstChildren->insert(index, r);
00178     return index;
00179 }
00180 
00181 CSSRuleList CSSStyleSheetImpl::cssRules()
00182 {
00183     return this;
00184 }
00185 
00186 void CSSStyleSheetImpl::deleteRule( unsigned long index, int &exceptioncode )
00187 {
00188     exceptioncode = 0;
00189     StyleBaseImpl *b = m_lstChildren->take(index);
00190     if(!b) {
00191         exceptioncode = DOMException::INDEX_SIZE_ERR;
00192         return;
00193     }
00194     b->deref();
00195 }
00196 
00197 bool CSSStyleSheetImpl::parseString(const DOMString &string, bool strict)
00198 {
00199 #ifdef CSS_STYLESHEET_DEBUG
00200     kdDebug( 6080 ) << "parsing sheet, len=" << string.length() << ", sheet is " << string.string() << endl;
00201 #endif
00202 
00203     strictParsing = strict;
00204     CSSParser p( strict );
00205     p.parseSheet( this, string );
00206     return true;
00207 }
00208 
00209 bool CSSStyleSheetImpl::isLoading() const
00210 {
00211     StyleBaseImpl *rule;
00212     for ( rule = m_lstChildren->first(); rule != 0; rule = m_lstChildren->next() )
00213     {
00214         if(rule->isImportRule())
00215         {
00216             CSSImportRuleImpl *import = static_cast<CSSImportRuleImpl *>(rule);
00217 #ifdef CSS_STYLESHEET_DEBUG
00218             kdDebug( 6080 ) << "found import" << endl;
00219 #endif
00220             if(import->isLoading())
00221             {
00222 #ifdef CSS_STYLESHEET_DEBUG
00223                 kdDebug( 6080 ) << "--> not loaded" << endl;
00224 #endif
00225                 return true;
00226             }
00227         }
00228     }
00229     return false;
00230 }
00231 
00232 void CSSStyleSheetImpl::checkLoaded() const
00233 {
00234     if(isLoading()) return;
00235     if(m_parent) m_parent->checkLoaded();
00236     if(m_parentNode) m_parentNode->sheetLoaded();
00237 }
00238 
00239 void CSSStyleSheetImpl::setNonCSSHints()
00240 {
00241     StyleBaseImpl *rule = m_lstChildren->first();
00242     while(rule) {
00243         if(rule->isStyleRule()) {
00244             static_cast<CSSStyleRuleImpl *>(rule)->setNonCSSHints();
00245         }
00246         rule = m_lstChildren->next();
00247     }
00248 }
00249 
00250 
00251 // ---------------------------------------------------------------------------
00252 
00253 
00254 StyleSheetListImpl::~StyleSheetListImpl()
00255 {
00256     for ( QPtrListIterator<StyleSheetImpl> it ( styleSheets ); it.current(); ++it )
00257         it.current()->deref();
00258 }
00259 
00260 void StyleSheetListImpl::add( StyleSheetImpl* s )
00261 {
00262     if ( !styleSheets.containsRef( s ) ) {
00263         s->ref();
00264         styleSheets.append( s );
00265     }
00266 }
00267 
00268 void StyleSheetListImpl::remove( StyleSheetImpl* s )
00269 {
00270     if ( styleSheets.removeRef( s ) )
00271         s->deref();
00272 }
00273 
00274 unsigned long StyleSheetListImpl::length() const
00275 {
00276     // hack so implicit BODY stylesheets don't get counted here
00277     unsigned long l = 0;
00278     QPtrListIterator<StyleSheetImpl> it(styleSheets);
00279     for (; it.current(); ++it) {
00280         if (!it.current()->isCSSStyleSheet() || !static_cast<CSSStyleSheetImpl*>(it.current())->implicit())
00281             ++l;
00282     }
00283     return l;
00284 }
00285 
00286 StyleSheetImpl *StyleSheetListImpl::item ( unsigned long index )
00287 {
00288     unsigned long l = 0;
00289     QPtrListIterator<StyleSheetImpl> it(styleSheets);
00290     for (; it.current(); ++it) {
00291         if (!it.current()->isCSSStyleSheet() || !static_cast<CSSStyleSheetImpl*>(it.current())->implicit()) {
00292             if (l == index)
00293                 return it.current();
00294             ++l;
00295         }
00296     }
00297     return 0;
00298 }
00299 
00300 // --------------------------------------------------------------------------------------------
00301 
00302 MediaListImpl::MediaListImpl( CSSStyleSheetImpl *parentSheet,
00303                               const DOMString &media )
00304     : StyleBaseImpl( parentSheet )
00305 {
00306     setMediaText( media );
00307 }
00308 
00309 MediaListImpl::MediaListImpl( CSSRuleImpl *parentRule, const DOMString &media )
00310     : StyleBaseImpl(parentRule)
00311 {
00312     setMediaText( media );
00313 }
00314 
00315 bool MediaListImpl::contains( const DOMString &medium ) const
00316 {
00317     return m_lstMedia.empty() || m_lstMedia.contains( medium ) ||
00318             m_lstMedia.contains( "all" );
00319 }
00320 
00321 CSSStyleSheetImpl *MediaListImpl::parentStyleSheet() const
00322 {
00323     if( m_parent->isCSSStyleSheet() ) return static_cast<CSSStyleSheetImpl *>(m_parent);
00324     return 0;
00325 }
00326 
00327 CSSRuleImpl *MediaListImpl::parentRule() const
00328 {
00329     if( m_parent->isRule() ) return static_cast<CSSRuleImpl *>(m_parent);
00330     return 0;
00331 }
00332 
00333 void MediaListImpl::deleteMedium( const DOMString &oldMedium )
00334 {
00335     const QValueList<DOMString>::Iterator itEnd = m_lstMedia.end();
00336 
00337     for ( QValueList<DOMString>::Iterator it = m_lstMedia.begin(); it != itEnd; ++it ) {
00338         if( (*it) == oldMedium ) {
00339             m_lstMedia.remove( it );
00340             return;
00341         }
00342     }
00343 }
00344 
00345 DOM::DOMString MediaListImpl::mediaText() const
00346 {
00347     DOMString text;
00348     const QValueList<DOMString>::ConstIterator itEnd = m_lstMedia.end();
00349 
00350     for ( QValueList<DOMString>::ConstIterator it = m_lstMedia.begin(); it != itEnd; ++it ) {
00351         text += *it;
00352         text += ", ";
00353     }
00354     return text;
00355 }
00356 
00357 void MediaListImpl::setMediaText(const DOM::DOMString &value)
00358 {
00359     m_lstMedia.clear();
00360     const QString val = value.string();
00361     const QStringList list = QStringList::split( ',', val );
00362 
00363     const QStringList::ConstIterator itEnd = list.end();
00364 
00365     for ( QStringList::ConstIterator it = list.begin(); it != itEnd; ++it )
00366     {
00367         const DOMString medium = (*it).stripWhiteSpace();
00368         if( !medium.isEmpty() )
00369             m_lstMedia.append( medium );
00370     }
00371 }
KDE Logo
This file is part of the documentation for khtml Library Version 3.4.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Feb 8 08:04:43 2006 by doxygen 1.4.4 written by Dimitri van Heesch, © 1997-2003