#include "config.h"
#include "ShadowRoot.h"
#include "ContentDistributor.h"
#include "ElementShadow.h"
#include "HistogramSupport.h"
#include "InsertionPoint.h"
#include "RuntimeEnabledFeatures.h"
#include "StyleResolver.h"
#include "Text.h"
#include "markup.h"
namespace WebCore {
struct SameSizeAsShadowRoot : public DocumentFragment, public TreeScope {
unsigned countersAndFlags[1];
};
COMPILE_ASSERT(sizeof(ShadowRoot) == sizeof(SameSizeAsShadowRoot), shadowroot_should_stay_small);
enum ShadowRootUsageOriginType {
ShadowRootUsageOriginWeb = 0,
ShadowRootUsageOriginNotWeb,
ShadowRootUsageOriginMax
};
ShadowRoot::ShadowRoot(Document* document, ShadowRootType type)
: DocumentFragment(0, CreateShadowRoot)
, TreeScope(this, document)
, m_numberOfStyles(0)
, m_applyAuthorStyles(false)
, m_resetStyleInheritance(false)
, m_type(type)
{
ASSERT(document);
}
ShadowRoot::~ShadowRoot()
{
willBeDeletedFrom(documentInternal());
removeDetachedChildren();
if (hasRareData())
clearRareData();
}
void ShadowRoot::dispose()
{
removeDetachedChildren();
}
PassRefPtr<Node> ShadowRoot::cloneNode(bool, ExceptionCode& ec)
{
ec = DATA_CLONE_ERR;
return 0;
}
String ShadowRoot::innerHTML() const
{
return createMarkup(this, ChildrenOnly);
}
void ShadowRoot::setInnerHTML(const String& markup, ExceptionCode& ec)
{
if (isOrphan()) {
ec = INVALID_ACCESS_ERR;
return;
}
if (RefPtr<DocumentFragment> fragment = createFragmentForInnerOuterHTML(markup, host(), AllowScriptingContent, ec))
replaceChildrenWithFragment(this, fragment.release(), ec);
}
bool ShadowRoot::childTypeAllowed(NodeType type) const
{
switch (type) {
case ELEMENT_NODE:
case PROCESSING_INSTRUCTION_NODE:
case COMMENT_NODE:
case TEXT_NODE:
case CDATA_SECTION_NODE:
case ENTITY_REFERENCE_NODE:
return true;
default:
return false;
}
}
void ShadowRoot::recalcStyle(StyleChange change)
{
ASSERT(!hasCustomStyleCallbacks());
StyleResolver* styleResolver = document()->ensureStyleResolver();
styleResolver->pushParentShadowRoot(this);
for (Node* child = firstChild(); child; child = child->nextSibling()) {
if (child->isElementNode())
toElement(child)->recalcStyle(change);
else if (child->isTextNode())
toText(child)->recalcTextStyle(change);
}
styleResolver->popParentShadowRoot(this);
clearNeedsStyleRecalc();
clearChildNeedsStyleRecalc();
}
void ShadowRoot::setApplyAuthorStyles(bool value)
{
if (isOrphan())
return;
if (m_applyAuthorStyles != value) {
m_applyAuthorStyles = value;
host()->setNeedsStyleRecalc();
}
}
void ShadowRoot::setResetStyleInheritance(bool value)
{
if (isOrphan())
return;
if (value != m_resetStyleInheritance) {
m_resetStyleInheritance = value;
if (attached() && owner())
owner()->recalcStyle(Force);
}
}
void ShadowRoot::attach(const AttachContext& context)
{
StyleResolver* styleResolver = document()->ensureStyleResolver();
styleResolver->pushParentShadowRoot(this);
DocumentFragment::attach(context);
styleResolver->popParentShadowRoot(this);
}
void ShadowRoot::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
{
if (isOrphan())
return;
ContainerNode::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
owner()->invalidateDistribution();
}
void ShadowRoot::registerScopedHTMLStyleChild()
{
++m_numberOfStyles;
setHasScopedHTMLStyleChild(true);
}
void ShadowRoot::unregisterScopedHTMLStyleChild()
{
ASSERT(hasScopedHTMLStyleChild() && m_numberOfStyles > 0);
--m_numberOfStyles;
setHasScopedHTMLStyleChild(m_numberOfStyles > 0);
}
}