~ubuntu-branches/ubuntu/maverick/webkit/maverick

« back to all changes in this revision

Viewing changes to WebCore/css/CSSImportRule.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Mike Hommey
  • Date: 2007-08-19 15:54:12 UTC
  • Revision ID: james.westby@ubuntu.com-20070819155412-uxxg1h9plpghmtbi
Tags: upstream-0~svn25144
ImportĀ upstreamĀ versionĀ 0~svn25144

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * This file is part of the DOM implementation for KDE.
 
3
 *
 
4
 * (C) 1999-2003 Lars Knoll (knoll@kde.org)
 
5
 * (C) 2002-2003 Dirk Mueller (mueller@kde.org)
 
6
 * Copyright (C) 2002, 2005, 2006 Apple Computer, Inc.
 
7
 *
 
8
 * This library is free software; you can redistribute it and/or
 
9
 * modify it under the terms of the GNU Library General Public
 
10
 * License as published by the Free Software Foundation; either
 
11
 * version 2 of the License, or (at your option) any later version.
 
12
 *
 
13
 * This library is distributed in the hope that it will be useful,
 
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
16
 * Library General Public License for more details.
 
17
 *
 
18
 * You should have received a copy of the GNU Library General Public License
 
19
 * along with this library; see the file COPYING.LIB.  If not, write to
 
20
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
21
 * Boston, MA 02111-1307, USA.
 
22
 */
 
23
 
 
24
#include "config.h"
 
25
#include "CSSImportRule.h"
 
26
 
 
27
#include "CachedCSSStyleSheet.h"
 
28
#include "CSSStyleSheet.h"
 
29
#include "DocLoader.h"
 
30
#include "Document.h"
 
31
#include "KURL.h"
 
32
#include "MediaList.h"
 
33
 
 
34
namespace WebCore {
 
35
 
 
36
CSSImportRule::CSSImportRule(StyleBase* parent, const String& href, MediaList* media)
 
37
    : CSSRule(parent)
 
38
    , m_strHref(href)
 
39
    , m_lstMedia(media)
 
40
    , m_cachedSheet(0)
 
41
    , m_loading(false)
 
42
{
 
43
    if (m_lstMedia)
 
44
        m_lstMedia->setParent(this);
 
45
    else
 
46
        m_lstMedia = new MediaList(this, String());
 
47
}
 
48
 
 
49
CSSImportRule::~CSSImportRule()
 
50
{
 
51
    if (m_lstMedia)
 
52
        m_lstMedia->setParent(0);
 
53
    if (m_styleSheet)
 
54
        m_styleSheet->setParent(0);
 
55
    if (m_cachedSheet)
 
56
        m_cachedSheet->deref(this);
 
57
}
 
58
 
 
59
void CSSImportRule::setCSSStyleSheet(const String& url, const String& charset, const String& sheet)
 
60
{
 
61
    if (m_styleSheet)
 
62
        m_styleSheet->setParent(0);
 
63
    m_styleSheet = new CSSStyleSheet(this, url, charset);
 
64
 
 
65
    CSSStyleSheet* parent = parentStyleSheet();
 
66
    m_styleSheet->parseString(sheet, !parent || parent->useStrictParsing());
 
67
    m_loading = false;
 
68
 
 
69
    checkLoaded();
 
70
}
 
71
 
 
72
bool CSSImportRule::isLoading() const
 
73
{
 
74
    return m_loading || (m_styleSheet && m_styleSheet->isLoading());
 
75
}
 
76
 
 
77
void CSSImportRule::insertedIntoParent()
 
78
{
 
79
    StyleBase* root = this;
 
80
    StyleBase* parent;
 
81
    while ((parent = root->parent()))
 
82
        root = parent;
 
83
    if (!root->isCSSStyleSheet())
 
84
        return;
 
85
    DocLoader* docLoader = static_cast<CSSStyleSheet*>(root)->docLoader();
 
86
    if (!docLoader)
 
87
        return;
 
88
 
 
89
    String absHref = m_strHref;
 
90
    CSSStyleSheet* parentSheet = parentStyleSheet();
 
91
    if (!parentSheet->href().isNull())
 
92
        // use parent styleheet's URL as the base URL
 
93
        absHref = KURL(parentSheet->href().deprecatedString(), m_strHref.deprecatedString()).url();
 
94
 
 
95
    // Check for a cycle in our import chain.  If we encounter a stylesheet
 
96
    // in our parent chain with the same URL, then just bail.
 
97
    for (parent = static_cast<StyleBase*>(this)->parent(); parent; parent = parent->parent()) {
 
98
        if (absHref == parent->baseURL())
 
99
            return;
 
100
    }
 
101
 
 
102
    m_cachedSheet = docLoader->requestCSSStyleSheet(absHref, parentSheet->charset());
 
103
    if (m_cachedSheet) {
 
104
        // if the import rule is issued dynamically, the sheet may be
 
105
        // removed from the pending sheet count, so let the doc know
 
106
        // the sheet being imported is pending.
 
107
        if (parentSheet && parentSheet->loadCompleted() && parentSheet->doc())
 
108
            parentSheet->doc()->addPendingSheet();
 
109
        m_loading = true;
 
110
        m_cachedSheet->ref(this);
 
111
    }
 
112
}
 
113
 
 
114
String CSSImportRule::cssText() const
 
115
{
 
116
    String result = "@import url(\"";
 
117
    result += m_strHref;
 
118
    result += "\")";
 
119
 
 
120
    if (m_lstMedia) {
 
121
        result += " ";
 
122
        result += m_lstMedia->mediaText();
 
123
    }
 
124
    result += ";";
 
125
 
 
126
    return result;
 
127
}
 
128
 
 
129
} // namespace WebCore