File indexing completed on 2024-04-28 15:22:41

0001 /*
0002  * This file is part of the DOM implementation for KDE.
0003  *
0004  * Copyright 1999-2003 Lars Knoll (knoll@kde.org)
0005  * Copyright 2002-2003 Dirk Mueller (mueller@kde.org)
0006  * Copyright 2002 Apple Computer, Inc.
0007  *
0008  * This library is free software; you can redistribute it and/or
0009  * modify it under the terms of the GNU Library General Public
0010  * License as published by the Free Software Foundation; either
0011  * version 2 of the License, or (at your option) any later version.
0012  *
0013  * This library is distributed in the hope that it will be useful,
0014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0016  * Library General Public License for more details.
0017  *
0018  * You should have received a copy of the GNU Library General Public License
0019  * along with this library; see the file COPYING.LIB.  If not, write to
0020  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0021  * Boston, MA 02110-1301, USA.
0022  *
0023  */
0024 #ifndef _CSS_css_ruleimpl_h_
0025 #define _CSS_css_ruleimpl_h_
0026 
0027 #include "dom/dom_string.h"
0028 #include "dom/css_rule.h"
0029 #include "css/css_base.h"
0030 #include "misc/loader_client.h"
0031 #include "misc/shared.h"
0032 
0033 namespace khtml
0034 {
0035 class CachedCSSStyleSheet;
0036 }
0037 
0038 namespace DOM
0039 {
0040 
0041 class CSSRule;
0042 class CSSStyleSheet;
0043 class CSSStyleSheetImpl;
0044 class CSSStyleDeclarationImpl;
0045 class CSSStyleListImpl;
0046 class MediaListImpl;
0047 
0048 class CSSRuleImpl : public StyleBaseImpl
0049 {
0050 public:
0051     CSSRuleImpl(StyleBaseImpl *parent)
0052         : StyleBaseImpl(parent), m_type(CSSRule::UNKNOWN_RULE) {}
0053 
0054     bool isRule() const override
0055     {
0056         return true;
0057     }
0058     unsigned short type() const
0059     {
0060         return m_type;
0061     }
0062 
0063     CSSStyleSheetImpl *parentStyleSheet() const;
0064     CSSRuleImpl *parentRule() const;
0065 
0066     virtual DOM::DOMString cssText() const;
0067     void setCssText(DOM::DOMString str);
0068     virtual void init() {}
0069 
0070 protected:
0071     CSSRule::RuleType m_type;
0072 };
0073 
0074 class CSSCharsetRuleImpl : public CSSRuleImpl
0075 {
0076 public:
0077     CSSCharsetRuleImpl(StyleBaseImpl *parent)
0078         : CSSRuleImpl(parent)
0079     {
0080         m_type = CSSRule::CHARSET_RULE;
0081     }
0082     CSSCharsetRuleImpl(StyleBaseImpl *parent, const DOM::DOMString &encoding)
0083         : CSSRuleImpl(parent), m_encoding(encoding)
0084     {
0085         m_type = CSSRule::CHARSET_RULE;
0086     }
0087 
0088     bool isCharsetRule() const override
0089     {
0090         return true;
0091     }
0092 
0093     DOMString encoding() const
0094     {
0095         return m_encoding;
0096     }
0097     void setEncoding(DOMString _encoding)
0098     {
0099         m_encoding = _encoding;
0100     }
0101     DOM::DOMString cssText() const override
0102     {
0103         return DOMString("@charset \"") + m_encoding + DOMString("\";");
0104     }
0105 protected:
0106     DOMString m_encoding;
0107 };
0108 
0109 class CSSFontFaceRuleImpl : public CSSRuleImpl
0110 {
0111 public:
0112     CSSFontFaceRuleImpl(StyleBaseImpl *parent);
0113 
0114     virtual ~CSSFontFaceRuleImpl();
0115 
0116     bool isFontFaceRule() const override
0117     {
0118         return true;
0119     }
0120 
0121     CSSStyleDeclarationImpl *style() const
0122     {
0123         return m_style;
0124     }
0125     void setDeclaration(CSSStyleDeclarationImpl *decl);
0126     DOMString cssText() const override;
0127 protected:
0128     CSSStyleDeclarationImpl *m_style;
0129 };
0130 
0131 class CSSImportRuleImpl : public khtml::CachedObjectClient, public CSSRuleImpl
0132 {
0133 public:
0134     CSSImportRuleImpl(StyleBaseImpl *parent, const DOM::DOMString &href,
0135                       const DOM::DOMString &media);
0136     CSSImportRuleImpl(StyleBaseImpl *parent, const DOM::DOMString &href,
0137                       MediaListImpl *media);
0138 
0139     virtual ~CSSImportRuleImpl();
0140 
0141     DOM::DOMString href() const
0142     {
0143         return m_strHref;
0144     }
0145     MediaListImpl *media() const
0146     {
0147         return m_lstMedia;
0148     }
0149     CSSStyleSheetImpl *styleSheet() const
0150     {
0151         return m_styleSheet;
0152     }
0153 
0154     bool isImportRule() const override
0155     {
0156         return true;
0157     }
0158     DOM::DOMString cssText() const override;
0159     void checkLoaded() const override;
0160 
0161     // from CachedObjectClient
0162     void setStyleSheet(const DOM::DOMString &url, const DOM::DOMString &sheet, const DOM::DOMString &charset, const DOM::DOMString &mimetype) override;
0163     void error(int err, const QString &text) override;
0164 
0165     bool isLoading() const;
0166     void init() override;
0167 
0168 protected:
0169     DOMString m_strHref;
0170     MediaListImpl *m_lstMedia;
0171     CSSStyleSheetImpl *m_styleSheet;
0172     khtml::CachedCSSStyleSheet *m_cachedSheet;
0173     bool m_loading;
0174     bool m_done;
0175 };
0176 
0177 class MediaList;
0178 
0179 class CSSRuleListImpl : public khtml::Shared<CSSRuleListImpl>
0180 {
0181 public:
0182     CSSRuleListImpl() : m_list(nullptr) {}
0183     CSSRuleListImpl(StyleListImpl *const lst, bool omitCharsetRules = false);
0184 
0185     ~CSSRuleListImpl();
0186 
0187     unsigned long length() const;
0188     CSSRuleImpl *item(unsigned long index);
0189 
0190     // FIXME: Not part of the DOM.  Only used by CSSMediaRuleImpl.  We should be able to remove them if we changed media rules to work
0191     // as StyleLists instead.
0192     unsigned long insertRule(CSSRuleImpl *rule, unsigned long index);
0193     void deleteRule(unsigned long index);
0194 
0195     void append(CSSRuleImpl *rule);
0196 protected:
0197     StyleListImpl *m_list;
0198     QList<CSSRuleImpl *> m_lstCSSRules;
0199 };
0200 
0201 class CSSMediaRuleImpl : public CSSRuleImpl
0202 {
0203 public:
0204     CSSMediaRuleImpl(StyleBaseImpl *parent);
0205     CSSMediaRuleImpl(StyleBaseImpl *parent, const DOM::DOMString &media);
0206     CSSMediaRuleImpl(StyleBaseImpl *parent, MediaListImpl *mediaList, CSSRuleListImpl *ruleList);
0207 
0208     virtual ~CSSMediaRuleImpl();
0209 
0210     MediaListImpl *media() const
0211     {
0212         return m_lstMedia;
0213     }
0214     CSSRuleListImpl *cssRules()
0215     {
0216         return m_lstCSSRules;
0217     }
0218 
0219     unsigned long insertRule(const DOM::DOMString &rule, unsigned long index);
0220     void deleteRule(unsigned long index)
0221     {
0222         m_lstCSSRules->deleteRule(index);
0223     }
0224 
0225     bool isMediaRule() const override
0226     {
0227         return true;
0228     }
0229     DOM::DOMString cssText() const override;
0230 
0231     /* Not part of the DOM */
0232     unsigned long append(CSSRuleImpl *rule);
0233 protected:
0234     MediaListImpl *m_lstMedia;
0235     CSSRuleListImpl *m_lstCSSRules;
0236 };
0237 
0238 class CSSPageRuleImpl : public CSSRuleImpl
0239 {
0240 public:
0241     CSSPageRuleImpl(StyleBaseImpl *parent);
0242 
0243     virtual ~CSSPageRuleImpl();
0244 
0245     CSSStyleDeclarationImpl *style() const
0246     {
0247         return m_style;
0248     }
0249 
0250     bool isPageRule() const override
0251     {
0252         return true;
0253     }
0254 
0255     DOM::DOMString selectorText() const;
0256     void setSelectorText(DOM::DOMString str);
0257 
0258 protected:
0259     CSSStyleDeclarationImpl *m_style;
0260 };
0261 
0262 class CSSStyleRuleImpl : public CSSRuleImpl
0263 {
0264 public:
0265     CSSStyleRuleImpl(StyleBaseImpl *parent);
0266 
0267     virtual ~CSSStyleRuleImpl();
0268 
0269     CSSStyleDeclarationImpl *style() const
0270     {
0271         return m_style;
0272     }
0273 
0274     bool isStyleRule() const override
0275     {
0276         return true;
0277     }
0278     DOM::DOMString cssText() const override;
0279 
0280     DOM::DOMString selectorText() const;
0281     void setSelectorText(DOM::DOMString str);
0282 
0283     bool parseString(const DOMString &string, bool = false) override;
0284 
0285     void setSelector(QList<CSSSelector *> *selector)
0286     {
0287         m_selector = selector;
0288     }
0289     void setDeclaration(CSSStyleDeclarationImpl *style);
0290 
0291     QList<CSSSelector *> *selector()
0292     {
0293         return m_selector;
0294     }
0295     CSSStyleDeclarationImpl *declaration()
0296     {
0297         return m_style;
0298     }
0299 
0300 protected:
0301     CSSStyleDeclarationImpl *m_style;
0302     QList<CSSSelector *> *m_selector;
0303 };
0304 
0305 class CSSNamespaceRuleImpl : public CSSRuleImpl
0306 {
0307 public:
0308     CSSNamespaceRuleImpl(StyleBaseImpl *parent, const DOMString &prefix, const DOMString &ns);
0309     DOMString namespaceURI() const
0310     {
0311         return m_namespace;
0312     }
0313     DOMString prefix() const
0314     {
0315         return m_prefix;
0316     }
0317 
0318     bool isDefault() const
0319     {
0320         return m_prefix.isEmpty();
0321     }
0322 private:
0323     DOMString m_prefix;
0324     DOMString m_namespace;
0325 };
0326 
0327 class CSSUnknownRuleImpl : public CSSRuleImpl
0328 {
0329 public:
0330     CSSUnknownRuleImpl(StyleBaseImpl *parent) : CSSRuleImpl(parent) {}
0331 
0332     bool isUnknownRule() const override
0333     {
0334         return true;
0335     }
0336 };
0337 
0338 } // namespace
0339 
0340 #endif