/* * Copyright (C) 1999 Lars Knoll (knoll@kde.org) * (C) 1999 Antti Koivisto (koivisto@kde.org) * (C) 2001 Dirk Mueller (mueller@kde.org) * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All rights reserved. * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/) * Copyright (C) 2011 Google Inc. All rights reserved. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. * */ #include "config.h" #include "NodeRenderingContext.h" #include "ComposedShadowTreeWalker.h" #include "ContainerNode.h" #include "ContentDistributor.h" #include "ElementShadow.h" #include "FlowThreadController.h" #include "HTMLContentElement.h" #include "HTMLNames.h" #include "HTMLShadowElement.h" #include "Node.h" #include "RenderFullScreen.h" #include "RenderNamedFlowThread.h" #include "RenderObject.h" #include "RenderView.h" #include "ShadowRoot.h" #if ENABLE(SVG) #include "SVGNames.h" #endif namespace WebCore { using namespace HTMLNames; static RenderObject* firstRendererOf(Node*); static RenderObject* lastRendererOf(Node*); NodeRenderingContext::NodeRenderingContext(Node* node) : m_node(node) , m_style(0) , m_parentFlowRenderer(0) { ComposedShadowTreeWalker::findParent(m_node, &m_parentDetails); } NodeRenderingContext::NodeRenderingContext(Node* node, RenderStyle* style) : m_node(node) , m_style(style) , m_parentFlowRenderer(0) { } NodeRenderingContext::~NodeRenderingContext() { } void NodeRenderingContext::setStyle(PassRefPtr style) { m_style = style; moveToFlowThreadIfNeeded(); } PassRefPtr NodeRenderingContext::releaseStyle() { return m_style.release(); } static inline RenderObject* nextRendererOfInsertionPoint(InsertionPoint* parent, Node* current) { size_t start = parent->indexOf(current); if (notFound == start) return 0; for (size_t i = start + 1; i < parent->size(); ++i) { if (RenderObject* renderer = parent->at(i)->renderer()) return renderer; } return 0; } static inline RenderObject* previousRendererOfInsertionPoint(InsertionPoint* parent, Node* current) { RenderObject* lastRenderer = 0; for (size_t i = 0; i < parent->size(); ++i) { if (parent->at(i) == current) break; if (RenderObject* renderer = parent->at(i)->renderer()) lastRenderer = renderer; } return lastRenderer; } static inline RenderObject* firstRendererOfInsertionPoint(InsertionPoint* parent) { size_t size = parent->size(); for (size_t i = 0; i < size; ++i) { if (RenderObject* renderer = parent->at(i)->renderer()) return renderer; } return firstRendererOf(parent->firstChild()); } static inline RenderObject* lastRendererOfInsertionPoint(InsertionPoint* parent) { size_t size = parent->size(); for (size_t i = 0; i < size; ++i) { if (RenderObject* renderer = parent->at(size - 1 - i)->renderer()) return renderer; } return lastRendererOf(parent->lastChild()); } static inline RenderObject* firstRendererOf(Node* node) { for (; node; node = node->nextSibling()) { if (node->renderer()) { // Do not return elements that are attached to a different flow-thread. if (node->renderer()->style() && !node->renderer()->style()->flowThread().isEmpty()) continue; return node->renderer(); } if (isInsertionPoint(node) && toInsertionPoint(node)->isActive()) { if (RenderObject* first = firstRendererOfInsertionPoint(toInsertionPoint(node))) return first; } } return 0; } static inline RenderObject* lastRendererOf(Node* node) { for (; node; node = node->previousSibling()) { if (node->renderer()) { // Do not return elements that are attached to a different flow-thread. if (node->renderer()->style() && !node->renderer()->style()->flowThread().isEmpty()) continue; return node->renderer(); } if (isInsertionPoint(node) && toInsertionPoint(node)->isActive()) { if (RenderObject* last = lastRendererOfInsertionPoint(toInsertionPoint(node))) return last; } } return 0; } RenderObject* NodeRenderingContext::nextRenderer() const { if (RenderObject* renderer = m_node->renderer()) return renderer->nextSibling(); if (m_parentFlowRenderer) return m_parentFlowRenderer->nextRendererForNode(m_node); if (m_parentDetails.insertionPoint()) { if (RenderObject* found = nextRendererOfInsertionPoint(m_parentDetails.insertionPoint(), m_node)) return found; return NodeRenderingContext(m_parentDetails.insertionPoint()).nextRenderer(); } // Avoid an O(N^2) problem with this function by not checking for // nextRenderer() when the parent element hasn't attached yet. if (m_node->parentOrHostNode() && !m_node->parentOrHostNode()->attached()) return 0; return firstRendererOf(m_node->nextSibling()); } RenderObject* NodeRenderingContext::previousRenderer() const { if (RenderObject* renderer = m_node->renderer()) return renderer->previousSibling(); if (m_parentFlowRenderer) return m_parentFlowRenderer->previousRendererForNode(m_node); if (m_parentDetails.insertionPoint()) { if (RenderObject* found = previousRendererOfInsertionPoint(m_parentDetails.insertionPoint(), m_node)) return found; return NodeRenderingContext(m_parentDetails.insertionPoint()).previousRenderer(); } // FIXME: We should have the same O(N^2) avoidance as nextRenderer does // however, when I tried adding it, several tests failed. return lastRendererOf(m_node->previousSibling()); } RenderObject* NodeRenderingContext::parentRenderer() const { if (RenderObject* renderer = m_node->renderer()) return renderer->parent(); if (m_parentFlowRenderer) return m_parentFlowRenderer; return m_parentDetails.node() ? m_parentDetails.node()->renderer() : 0; } bool NodeRenderingContext::shouldCreateRenderer() const { if (!m_parentDetails.node()) return false; RenderObject* parentRenderer = this->parentRenderer(); if (!parentRenderer) return false; if (!parentRenderer->canHaveChildren()) return false; if (!m_parentDetails.node()->childShouldCreateRenderer(*this)) return false; return true; } void NodeRenderingContext::moveToFlowThreadIfNeeded() { if (!m_node->document()->cssRegionsEnabled()) return; if (!m_node->isElementNode() || !m_style || m_style->flowThread().isEmpty()) return; // FIXME: Do not collect elements if they are in shadow tree. if (m_node->isInShadowTree()) return; #if ENABLE(SVG) // Allow only svg root elements to be directly collected by a render flow thread. if (m_node->isSVGElement() && (!(m_node->hasTagName(SVGNames::svgTag) && m_node->parentNode() && !m_node->parentNode()->isSVGElement()))) return; #endif m_flowThread = m_style->flowThread(); ASSERT(m_node->document()->renderView()); FlowThreadController* flowThreadController = m_node->document()->renderView()->flowThreadController(); m_parentFlowRenderer = flowThreadController->ensureRenderFlowThreadWithName(m_flowThread); flowThreadController->registerNamedFlowContentNode(m_node, m_parentFlowRenderer); } bool NodeRenderingContext::isOnEncapsulationBoundary() const { return isOnUpperEncapsulationBoundary() || isLowerEncapsulationBoundary(m_parentDetails.insertionPoint()) || isLowerEncapsulationBoundary(m_node->parentNode()); } bool NodeRenderingContext::isOnUpperEncapsulationBoundary() const { return m_node->parentNode() && m_node->parentNode()->isShadowRoot(); } NodeRendererFactory::NodeRendererFactory(Node* node) : m_context(node) { } RenderObject* NodeRendererFactory::createRenderer() { Node* node = m_context.node(); RenderObject* newRenderer = node->createRenderer(node->document()->renderArena(), m_context.style()); if (!newRenderer) return 0; if (!m_context.parentRenderer()->isChildAllowed(newRenderer, m_context.style())) { newRenderer->destroy(); return 0; } node->setRenderer(newRenderer); newRenderer->setAnimatableStyle(m_context.releaseStyle()); // setAnimatableStyle() can depend on renderer() already being set. return newRenderer; } void NodeRendererFactory::createRendererIfNeeded() { Node* node = m_context.node(); Document* document = node->document(); if (!document->shouldCreateRenderers()) return; ASSERT(!node->renderer()); ASSERT(document->shouldCreateRenderers()); if (!m_context.shouldCreateRenderer()) return; Element* element = node->isElementNode() ? toElement(node) : 0; if (element) m_context.setStyle(element->styleForRenderer()); else if (RenderObject* parentRenderer = m_context.parentRenderer()) m_context.setStyle(parentRenderer->style()); if (!node->rendererIsNeeded(m_context)) { if (element && m_context.style()->affectedByEmpty()) element->setStyleAffectedByEmpty(); return; } RenderObject* parentRenderer = m_context.hasFlowThreadParent() ? m_context.parentFlowRenderer() : m_context.parentRenderer(); // Do not call m_context.nextRenderer() here in the first clause, because it expects to have // the renderer added to its parent already. RenderObject* nextRenderer = m_context.hasFlowThreadParent() ? m_context.parentFlowRenderer()->nextRendererForNode(node) : m_context.nextRenderer(); RenderObject* newRenderer = createRenderer(); #if ENABLE(FULLSCREEN_API) if (document->webkitIsFullScreen() && document->webkitCurrentFullScreenElement() == node) newRenderer = RenderFullScreen::wrapRenderer(newRenderer, document); #endif if (!newRenderer) return; // Note: Adding newRenderer instead of renderer(). renderer() may be a child of newRenderer. parentRenderer->addChild(newRenderer, nextRenderer); } }