This source file includes following definitions.
- m_registeredWithShadowRoot
- setDistribution
- attach
- detach
- willRecalcStyle
- shouldUseFallbackElements
- canBeActive
- isActive
- isShadowInsertionPoint
- isContentInsertionPoint
- getDistributedNodes
- rendererIsNeeded
- childrenChanged
- insertedInto
- removedFrom
- resolveReprojection
- collectDestinationInsertionPoints
#include "config.h"
#include "core/dom/shadow/InsertionPoint.h"
#include "HTMLNames.h"
#include "core/dom/ElementTraversal.h"
#include "core/dom/QualifiedName.h"
#include "core/dom/StaticNodeList.h"
#include "core/dom/shadow/ElementShadow.h"
namespace WebCore {
using namespace HTMLNames;
InsertionPoint::InsertionPoint(const QualifiedName& tagName, Document& document)
: HTMLElement(tagName, document, CreateInsertionPoint)
, m_registeredWithShadowRoot(false)
{
setHasCustomStyleCallbacks();
}
InsertionPoint::~InsertionPoint()
{
}
void InsertionPoint::setDistribution(ContentDistribution& distribution)
{
if (shouldUseFallbackElements()) {
for (Node* child = firstChild(); child; child = child->nextSibling())
child->lazyReattachIfAttached();
}
size_t i = 0;
size_t j = 0;
for ( ; i < m_distribution.size() && j < distribution.size(); ++i, ++j) {
if (m_distribution.size() < distribution.size()) {
for ( ; j < distribution.size() && m_distribution.at(i) != distribution.at(j); ++j)
distribution.at(j)->lazyReattachIfAttached();
} else if (m_distribution.size() > distribution.size()) {
for ( ; i < m_distribution.size() && m_distribution.at(i) != distribution.at(j); ++i)
m_distribution.at(i)->lazyReattachIfAttached();
} else if (m_distribution.at(i) != distribution.at(j)) {
m_distribution.at(i)->lazyReattachIfAttached();
distribution.at(j)->lazyReattachIfAttached();
}
}
for ( ; i < m_distribution.size(); ++i)
m_distribution.at(i)->lazyReattachIfAttached();
for ( ; j < distribution.size(); ++j)
distribution.at(j)->lazyReattachIfAttached();
m_distribution.swap(distribution);
m_distribution.shrinkToFit();
}
void InsertionPoint::attach(const AttachContext& context)
{
for (size_t i = 0; i < m_distribution.size(); ++i) {
if (m_distribution.at(i)->needsAttach())
m_distribution.at(i)->attach(context);
}
HTMLElement::attach(context);
}
void InsertionPoint::detach(const AttachContext& context)
{
for (size_t i = 0; i < m_distribution.size(); ++i)
m_distribution.at(i)->lazyReattachIfAttached();
HTMLElement::detach(context);
}
void InsertionPoint::willRecalcStyle(StyleRecalcChange change)
{
if (change < Inherit)
return;
for (size_t i = 0; i < m_distribution.size(); ++i)
m_distribution.at(i)->setNeedsStyleRecalc(LocalStyleChange);
}
bool InsertionPoint::shouldUseFallbackElements() const
{
return isActive() && !hasDistribution();
}
bool InsertionPoint::canBeActive() const
{
if (!isInShadowTree())
return false;
for (Node* node = parentNode(); node; node = node->parentNode()) {
if (node->isInsertionPoint())
return false;
}
return true;
}
bool InsertionPoint::isActive() const
{
if (!canBeActive())
return false;
ShadowRoot* shadowRoot = containingShadowRoot();
if (!shadowRoot)
return false;
if (!isHTMLShadowElement(*this) || shadowRoot->descendantShadowElementCount() <= 1)
return true;
const Vector<RefPtr<InsertionPoint> >& insertionPoints = shadowRoot->descendantInsertionPoints();
for (size_t i = 0; i < insertionPoints.size(); ++i) {
InsertionPoint* point = insertionPoints[i].get();
if (isHTMLShadowElement(*point))
return point == this;
}
return true;
}
bool InsertionPoint::isShadowInsertionPoint() const
{
return isHTMLShadowElement(*this) && isActive();
}
bool InsertionPoint::isContentInsertionPoint() const
{
return isHTMLContentElement(*this) && isActive();
}
PassRefPtr<NodeList> InsertionPoint::getDistributedNodes()
{
document().updateDistributionForNodeIfNeeded(this);
Vector<RefPtr<Node> > nodes;
nodes.reserveInitialCapacity(m_distribution.size());
for (size_t i = 0; i < m_distribution.size(); ++i)
nodes.uncheckedAppend(m_distribution.at(i));
return StaticNodeList::adopt(nodes);
}
bool InsertionPoint::rendererIsNeeded(const RenderStyle& style)
{
return !isActive() && HTMLElement::rendererIsNeeded(style);
}
void InsertionPoint::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
{
HTMLElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
if (ShadowRoot* root = containingShadowRoot()) {
if (ElementShadow* rootOwner = root->owner())
rootOwner->setNeedsDistributionRecalc();
}
}
Node::InsertionNotificationRequest InsertionPoint::insertedInto(ContainerNode* insertionPoint)
{
HTMLElement::insertedInto(insertionPoint);
if (ShadowRoot* root = containingShadowRoot()) {
if (ElementShadow* rootOwner = root->owner()) {
rootOwner->setNeedsDistributionRecalc();
if (canBeActive() && !m_registeredWithShadowRoot && insertionPoint->treeScope().rootNode() == root) {
m_registeredWithShadowRoot = true;
root->didAddInsertionPoint(this);
rootOwner->didAffectApplyAuthorStyles();
if (canAffectSelector())
rootOwner->willAffectSelector();
}
}
}
return InsertionDone;
}
void InsertionPoint::removedFrom(ContainerNode* insertionPoint)
{
ShadowRoot* root = containingShadowRoot();
if (!root)
root = insertionPoint->containingShadowRoot();
if (root) {
if (ElementShadow* rootOwner = root->owner())
rootOwner->setNeedsDistributionRecalc();
}
ElementShadow* rootOwner = root ? root->owner() : 0;
clearDistribution();
if (m_registeredWithShadowRoot && insertionPoint->treeScope().rootNode() == root) {
ASSERT(root);
m_registeredWithShadowRoot = false;
root->didRemoveInsertionPoint(this);
if (rootOwner) {
rootOwner->didAffectApplyAuthorStyles();
if (canAffectSelector())
rootOwner->willAffectSelector();
}
}
HTMLElement::removedFrom(insertionPoint);
}
const InsertionPoint* resolveReprojection(const Node* projectedNode)
{
ASSERT(projectedNode);
const InsertionPoint* insertionPoint = 0;
const Node* current = projectedNode;
ElementShadow* lastElementShadow = 0;
while (true) {
ElementShadow* shadow = shadowWhereNodeCanBeDistributed(*current);
if (!shadow || shadow == lastElementShadow)
break;
lastElementShadow = shadow;
const InsertionPoint* insertedTo = shadow->finalDestinationInsertionPointFor(projectedNode);
if (!insertedTo)
break;
ASSERT(current != insertedTo);
current = insertedTo;
insertionPoint = insertedTo;
}
return insertionPoint;
}
void collectDestinationInsertionPoints(const Node& node, Vector<InsertionPoint*, 8>& results)
{
const Node* current = &node;
ElementShadow* lastElementShadow = 0;
while (true) {
ElementShadow* shadow = shadowWhereNodeCanBeDistributed(*current);
if (!shadow || shadow == lastElementShadow)
return;
lastElementShadow = shadow;
const DestinationInsertionPoints* insertionPoints = shadow->destinationInsertionPointsFor(&node);
if (!insertionPoints)
return;
for (size_t i = 0; i < insertionPoints->size(); ++i)
results.append(insertionPoints->at(i).get());
ASSERT(current != insertionPoints->last().get());
current = insertionPoints->last().get();
}
}
}