HTMLStyleElement.cpp [plain text]
#include "config.h"
#include "HTMLStyleElement.h"
#include "Attribute.h"
#include "Document.h"
#include "Event.h"
#include "EventSender.h"
#include "HTMLNames.h"
#include "RuntimeEnabledFeatures.h"
#include "ScriptEventListener.h"
#include "ScriptableDocumentParser.h"
namespace WebCore {
using namespace HTMLNames;
static StyleEventSender& styleLoadEventSender()
{
DEFINE_STATIC_LOCAL(StyleEventSender, sharedLoadEventSender, (eventNames().loadEvent));
return sharedLoadEventSender;
}
inline HTMLStyleElement::HTMLStyleElement(const QualifiedName& tagName, Document* document, bool createdByParser)
: HTMLElement(tagName, document)
, StyleElement(document, createdByParser)
, m_firedLoad(false)
, m_loadedSheet(false)
#if ENABLE(STYLE_SCOPED)
, m_isRegisteredWithScopingNode(false)
#endif
{
ASSERT(hasTagName(styleTag));
}
HTMLStyleElement::~HTMLStyleElement()
{
StyleElement::clearDocumentData(document(), this);
styleLoadEventSender().cancelEvent(this);
}
PassRefPtr<HTMLStyleElement> HTMLStyleElement::create(const QualifiedName& tagName, Document* document, bool createdByParser)
{
return adoptRef(new HTMLStyleElement(tagName, document, createdByParser));
}
void HTMLStyleElement::parseAttribute(Attribute* attr)
{
if (attr->name() == titleAttr && m_sheet)
m_sheet->setTitle(attr->value());
else if (attr->name() == onloadAttr)
setAttributeEventListener(eventNames().loadEvent, createAttributeEventListener(this, attr));
else if (attr->name() == onerrorAttr)
setAttributeEventListener(eventNames().errorEvent, createAttributeEventListener(this, attr));
#if ENABLE(STYLE_SCOPED)
else if (attr->name() == scopedAttr) {
if (!attr->isNull() && !m_isRegisteredWithScopingNode && inDocument())
registerWithScopingNode();
else if (attr->isNull() && m_isRegisteredWithScopingNode)
unregisterWithScopingNode();
}
#endif
else
HTMLElement::parseAttribute(attr);
}
void HTMLStyleElement::finishParsingChildren()
{
StyleElement::finishParsingChildren(this);
HTMLElement::finishParsingChildren();
}
#if ENABLE(STYLE_SCOPED)
void HTMLStyleElement::registerWithScopingNode()
{
ASSERT(!m_isRegisteredWithScopingNode);
ASSERT(inDocument());
if (m_isRegisteredWithScopingNode)
return;
if (!RuntimeEnabledFeatures::styleScopedEnabled())
return;
ContainerNode* scope = parentNode();
if (!scope)
return;
if (!scope->isElementNode() && !scope->isShadowRoot()) {
ASSERT_NOT_REACHED();
return;
}
scope->registerScopedHTMLStyleChild();
scope->setNeedsStyleRecalc();
if (inDocument() && !document()->parsing() && document()->renderer())
document()->styleResolverChanged(DeferRecalcStyle);
m_isRegisteredWithScopingNode = true;
}
void HTMLStyleElement::unregisterWithScopingNode()
{
ASSERT(m_isRegisteredWithScopingNode || !RuntimeEnabledFeatures::styleScopedEnabled());
if (!m_isRegisteredWithScopingNode)
return;
if (!RuntimeEnabledFeatures::styleScopedEnabled())
return;
ContainerNode* scope = parentNode();
ASSERT(scope);
if (scope) {
ASSERT(scope->hasScopedHTMLStyleChild());
scope->unregisterScopedHTMLStyleChild();
scope->setNeedsStyleRecalc();
}
if (inDocument() && !document()->parsing() && document()->renderer())
document()->styleResolverChanged(DeferRecalcStyle);
m_isRegisteredWithScopingNode = false;
}
#endif
Node::InsertionNotificationRequest HTMLStyleElement::insertedInto(Node* insertionPoint)
{
HTMLElement::insertedInto(insertionPoint);
if (insertionPoint->inDocument())
StyleElement::insertedIntoDocument(document(), this);
#if ENABLE(STYLE_SCOPED)
if (scoped() && !m_isRegisteredWithScopingNode)
registerWithScopingNode();
#endif
return InsertionDone;
}
void HTMLStyleElement::removedFrom(Node* insertionPoint)
{
HTMLElement::removedFrom(insertionPoint);
if (insertionPoint->inDocument()) {
#if ENABLE(STYLE_SCOPED)
if (m_isRegisteredWithScopingNode)
unregisterWithScopingNode();
#endif
StyleElement::removedFromDocument(document(), this);
}
}
#if ENABLE(STYLE_SCOPED)
void HTMLStyleElement::willRemove()
{
ASSERT(!scoped() || !inDocument() || m_isRegisteredWithScopingNode || !RuntimeEnabledFeatures::styleScopedEnabled());
if (m_isRegisteredWithScopingNode)
unregisterWithScopingNode();
HTMLElement::willRemove();
}
#endif
void HTMLStyleElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
{
HTMLElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
StyleElement::childrenChanged(this);
}
const AtomicString& HTMLStyleElement::media() const
{
return getAttribute(mediaAttr);
}
const AtomicString& HTMLStyleElement::type() const
{
return getAttribute(typeAttr);
}
#if ENABLE(STYLE_SCOPED)
bool HTMLStyleElement::scoped() const
{
return fastHasAttribute(scopedAttr);
}
void HTMLStyleElement::setScoped(bool scopedValue)
{
setBooleanAttribute(scopedAttr, scopedValue);
}
Element* HTMLStyleElement::scopingElement() const
{
if (!scoped())
return 0;
ContainerNode* parentOrHost = parentOrHostNode();
if (!parentOrHost || !parentOrHost->isElementNode())
return 0;
return toElement(parentOrHost);
}
#endif // ENABLE(STYLE_SCOPED)
void HTMLStyleElement::dispatchPendingLoadEvents()
{
styleLoadEventSender().dispatchPendingEvents();
}
void HTMLStyleElement::dispatchPendingEvent(StyleEventSender* eventSender)
{
ASSERT_UNUSED(eventSender, eventSender == &styleLoadEventSender());
if (m_loadedSheet)
dispatchEvent(Event::create(eventNames().loadEvent, false, false));
else
dispatchEvent(Event::create(eventNames().errorEvent, false, false));
}
void HTMLStyleElement::notifyLoadedSheetAndAllCriticalSubresources(bool errorOccurred)
{
if (m_firedLoad)
return;
m_loadedSheet = !errorOccurred;
styleLoadEventSender().dispatchEventSoon(this);
m_firedLoad = true;
}
void HTMLStyleElement::addSubresourceAttributeURLs(ListHashSet<KURL>& urls) const
{
HTMLElement::addSubresourceAttributeURLs(urls);
if (CSSStyleSheet* styleSheet = const_cast<HTMLStyleElement*>(this)->sheet())
styleSheet->internal()->addSubresourceStyleURLs(urls);
}
bool HTMLStyleElement::disabled() const
{
if (!m_sheet)
return false;
return m_sheet->disabled();
}
void HTMLStyleElement::setDisabled(bool setDisabled)
{
if (CSSStyleSheet* styleSheet = sheet())
styleSheet->setDisabled(setDisabled);
}
}