#include "config.h"
#include "ShadowRoot.h"
#include "AuthorStyleSheets.h"
#include "CSSStyleSheet.h"
#include "ElementTraversal.h"
#include "RenderElement.h"
#include "RuntimeEnabledFeatures.h"
#include "SlotAssignment.h"
#include "StyleResolver.h"
#include "markup.h"
namespace WebCore {
struct SameSizeAsShadowRoot : public DocumentFragment, public TreeScope {
unsigned countersAndFlags[1];
void* styleResolver;
void* authorStyleSheets;
void* host;
void* slotAssignment;
};
COMPILE_ASSERT(sizeof(ShadowRoot) == sizeof(SameSizeAsShadowRoot), shadowroot_should_stay_small);
ShadowRoot::ShadowRoot(Document& document, Type type)
: DocumentFragment(document, CreateShadowRoot)
, TreeScope(*this, document)
, m_type(type)
{
}
ShadowRoot::ShadowRoot(Document& document, std::unique_ptr<SlotAssignment>&& slotAssignment)
: DocumentFragment(document, CreateShadowRoot)
, TreeScope(*this, document)
, m_type(Type::UserAgent)
, m_slotAssignment(WTFMove(slotAssignment))
{
}
ShadowRoot::~ShadowRoot()
{
willBeDeletedFrom(document());
removeDetachedChildren();
}
StyleResolver& ShadowRoot::styleResolver()
{
if (m_type == Type::UserAgent)
return document().userAgentShadowTreeStyleResolver();
if (!m_styleResolver) {
m_styleResolver = std::make_unique<StyleResolver>(document());
if (m_authorStyleSheets)
m_styleResolver->appendAuthorStyleSheets(m_authorStyleSheets->activeStyleSheets());
}
return *m_styleResolver;
}
void ShadowRoot::resetStyleResolver()
{
m_styleResolver = nullptr;
}
AuthorStyleSheets& ShadowRoot::authorStyleSheets()
{
if (!m_authorStyleSheets)
m_authorStyleSheets = std::make_unique<AuthorStyleSheets>(*this);
return *m_authorStyleSheets;
}
void ShadowRoot::updateStyle()
{
bool shouldRecalcStyle = false;
if (m_authorStyleSheets) {
shouldRecalcStyle = m_authorStyleSheets->updateActiveStyleSheets(AuthorStyleSheets::FullUpdate);
}
if (shouldRecalcStyle)
setNeedsStyleRecalc();
}
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(*host(), markup, AllowScriptingContent, ec))
replaceChildrenWithFragment(*this, fragment.releaseNonNull(), 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:
return true;
default:
return false;
}
}
void ShadowRoot::setResetStyleInheritance(bool value)
{
if (isOrphan())
return;
if (value != m_resetStyleInheritance) {
m_resetStyleInheritance = value;
if (host())
setNeedsStyleRecalc();
}
}
Ref<Node> ShadowRoot::cloneNodeInternal(Document&, CloningOperation)
{
RELEASE_ASSERT_NOT_REACHED();
return *static_cast<Node*>(nullptr); }
void ShadowRoot::removeAllEventListeners()
{
DocumentFragment::removeAllEventListeners();
for (Node* node = firstChild(); node; node = NodeTraversal::next(*node))
node->removeAllEventListeners();
}
HTMLSlotElement* ShadowRoot::findAssignedSlot(const Node& node)
{
ASSERT(node.parentNode() == host());
if (!m_slotAssignment)
return nullptr;
return m_slotAssignment->findAssignedSlot(node, *this);
}
void ShadowRoot::addSlotElementByName(const AtomicString& name, HTMLSlotElement& slot)
{
if (!m_slotAssignment)
m_slotAssignment = std::make_unique<SlotAssignment>();
return m_slotAssignment->addSlotElementByName(name, slot, *this);
}
void ShadowRoot::removeSlotElementByName(const AtomicString& name, HTMLSlotElement& slot)
{
return m_slotAssignment->removeSlotElementByName(name, slot, *this);
}
const Vector<Node*>* ShadowRoot::assignedNodesForSlot(const HTMLSlotElement& slot)
{
if (!m_slotAssignment)
return nullptr;
return m_slotAssignment->assignedNodesForSlot(slot, *this);
}
}