#include "config.h"
#include "CSSStyleSheet.h"
#include "CSSImportRule.h"
#include "CSSNamespace.h"
#include "cssparser.h"
#include "CSSRuleList.h"
#include "Document.h"
#include "ExceptionCode.h"
#include "Node.h"
namespace WebCore {
CSSStyleSheet::CSSStyleSheet(CSSStyleSheet* parentSheet, String href)
: StyleSheet(parentSheet, href)
, m_doc(parentSheet ? parentSheet->doc() : 0)
, m_implicit(false)
, m_namespaces(0)
{
}
CSSStyleSheet::CSSStyleSheet(Node *parentNode, String href, bool _implicit)
: StyleSheet(parentNode, href)
, m_doc(parentNode->document())
, m_implicit(_implicit)
, m_namespaces(0)
{
}
CSSStyleSheet::CSSStyleSheet(CSSRule *ownerRule, String href)
: StyleSheet(ownerRule, href)
, m_doc(0)
, m_implicit(false)
, m_namespaces(0)
{
}
CSSStyleSheet::~CSSStyleSheet()
{
delete m_namespaces;
}
CSSRule *CSSStyleSheet::ownerRule() const
{
return (parent() && parent()->isRule()) ? static_cast<CSSRule*>(parent()) : 0;
}
unsigned CSSStyleSheet::insertRule(const String& rule, unsigned index, ExceptionCode& ec)
{
ec = 0;
if (index > length()) {
ec = INDEX_SIZE_ERR;
return 0;
}
CSSParser p(useStrictParsing());
RefPtr<CSSRule> r = p.parseRule(this, rule);
if (!r) {
ec = SYNTAX_ERR;
return 0;
}
insert(index, r.release());
styleSheetChanged();
return index;
}
unsigned CSSStyleSheet::addRule(const String &selector, const String &style, int index, ExceptionCode& ec)
{
if (index == -1)
index = length();
return insertRule(selector + " { " + style + " }", index, ec);
}
CSSRuleList *CSSStyleSheet::cssRules()
{
return new CSSRuleList(this);
}
void CSSStyleSheet::deleteRule(unsigned index, ExceptionCode& ec)
{
if (index >= length()) {
ec = INDEX_SIZE_ERR;
return;
}
ec = 0;
remove(index);
styleSheetChanged();
}
void CSSStyleSheet::addNamespace(CSSParser* p, const AtomicString& prefix, const AtomicString& uri)
{
if (uri.isEmpty())
return;
m_namespaces = new CSSNamespace(prefix, uri, m_namespaces);
if (prefix.isEmpty())
p->defaultNamespace = uri;
}
const AtomicString& CSSStyleSheet::determineNamespace(const AtomicString& prefix)
{
if (prefix.isEmpty())
return nullAtom; else if (prefix == starAtom)
return starAtom; else if (m_namespaces) {
CSSNamespace* ns = m_namespaces->namespaceForPrefix(prefix);
if (ns)
return ns->uri();
}
return nullAtom; }
bool CSSStyleSheet::parseString(const String &string, bool strict)
{
setStrictParsing(strict);
CSSParser p(strict);
p.parseSheet(this, string);
return true;
}
bool CSSStyleSheet::isLoading()
{
unsigned len = length();
for (unsigned i = 0; i < len; ++i) {
StyleBase* rule = item(i);
if (rule->isImportRule() && static_cast<CSSImportRule*>(rule)->isLoading())
return true;
}
return false;
}
void CSSStyleSheet::checkLoaded()
{
if (isLoading())
return;
if (parent())
parent()->checkLoaded();
if (m_parentNode)
m_parentNode->sheetLoaded();
}
DocLoader *CSSStyleSheet::docLoader()
{
if (!m_doc) return 0;
return m_doc->docLoader();
}
void CSSStyleSheet::styleSheetChanged()
{
if (m_doc)
m_doc->updateStyleSelector();
}
}