2 * Copyright (C) 2011 Adobe Systems Incorporated. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
11 * 2. Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials
14 * provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER "AS IS" AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
21 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
25 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
26 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include "core/dom/NamedFlow.h"
33 #include "RuntimeEnabledFeatures.h"
34 #include "core/dom/NamedFlowCollection.h"
35 #include "core/dom/StaticNodeList.h"
36 #include "core/events/ThreadLocalEventNames.h"
37 #include "core/events/UIEvent.h"
38 #include "core/rendering/RenderNamedFlowThread.h"
39 #include "core/rendering/RenderRegion.h"
43 NamedFlow::NamedFlow(PassRefPtr<NamedFlowCollection> manager, const AtomicString& flowThreadName)
44 : m_flowThreadName(flowThreadName)
45 , m_flowManager(manager)
46 , m_parentFlowThread(0)
48 ASSERT(RuntimeEnabledFeatures::cssRegionsEnabled());
49 ScriptWrappable::init(this);
52 NamedFlow::~NamedFlow()
54 // The named flow is not "strong" referenced from anywhere at this time so it shouldn't be reused if the named flow is recreated.
55 m_flowManager->discardNamedFlow(this);
58 PassRefPtr<NamedFlow> NamedFlow::create(PassRefPtr<NamedFlowCollection> manager, const AtomicString& flowThreadName)
60 return adoptRef(new NamedFlow(manager, flowThreadName));
63 const AtomicString& NamedFlow::name() const
65 return m_flowThreadName;
68 bool NamedFlow::overset() const
70 if (m_flowManager->document())
71 m_flowManager->document()->updateLayoutIgnorePendingStylesheets();
73 // The renderer may be destroyed or created after the style update.
74 // Because this is called from JS, where the wrapper keeps a reference to the NamedFlow, no guard is necessary.
75 return m_parentFlowThread ? m_parentFlowThread->overset() : true;
78 static inline bool inFlowThread(RenderObject* renderer, RenderNamedFlowThread* flowThread)
82 RenderFlowThread* currentFlowThread = renderer->flowThreadContainingBlock();
83 if (flowThread == currentFlowThread)
85 if (renderer->flowThreadState() != RenderObject::InsideInFlowThread)
88 // An in-flow flow thread can be nested inside an out-of-flow one, so we have to recur up to check.
89 return inFlowThread(currentFlowThread->containingBlock(), flowThread);
92 int NamedFlow::firstEmptyRegionIndex() const
94 if (m_flowManager->document())
95 m_flowManager->document()->updateLayoutIgnorePendingStylesheets();
97 if (!m_parentFlowThread)
100 const RenderRegionList& regionList = m_parentFlowThread->renderRegionList();
101 if (regionList.isEmpty())
104 int countNonPseudoRegions = -1;
105 RenderRegionList::const_iterator iter = regionList.begin();
106 for (int index = 0; iter != regionList.end(); ++index, ++iter) {
107 const RenderRegion* renderRegion = *iter;
108 // FIXME: Pseudo-elements are not included in the list.
109 // They will be included when we will properly support the Region interface
110 // http://dev.w3.org/csswg/css-regions/#the-region-interface
111 if (renderRegion->isPseudoElement())
113 countNonPseudoRegions++;
114 if (renderRegion->regionOversetState() == RegionEmpty)
115 return countNonPseudoRegions;
120 PassRefPtr<NodeList> NamedFlow::getRegionsByContent(Node* contentNode)
122 Vector<RefPtr<Node> > regionNodes;
125 return StaticNodeList::adopt(regionNodes);
127 if (m_flowManager->document())
128 m_flowManager->document()->updateLayoutIgnorePendingStylesheets();
130 // The renderer may be destroyed or created after the style update.
131 // Because this is called from JS, where the wrapper keeps a reference to the NamedFlow, no guard is necessary.
132 if (!m_parentFlowThread)
133 return StaticNodeList::adopt(regionNodes);
135 if (inFlowThread(contentNode->renderer(), m_parentFlowThread)) {
136 const RenderRegionList& regionList = m_parentFlowThread->renderRegionList();
137 for (RenderRegionList::const_iterator iter = regionList.begin(); iter != regionList.end(); ++iter) {
138 const RenderRegion* renderRegion = *iter;
139 // They will be included when we will properly support the Region interface
140 // http://dev.w3.org/csswg/css-regions/#the-region-interface
141 if (renderRegion->isPseudoElement())
143 if (m_parentFlowThread->objectInFlowRegion(contentNode->renderer(), renderRegion))
144 regionNodes.append(renderRegion->node());
148 return StaticNodeList::adopt(regionNodes);
151 PassRefPtr<NodeList> NamedFlow::getRegions()
153 Vector<RefPtr<Node> > regionNodes;
155 if (m_flowManager->document())
156 m_flowManager->document()->updateLayoutIgnorePendingStylesheets();
158 // The renderer may be destroyed or created after the style update.
159 // Because this is called from JS, where the wrapper keeps a reference to the NamedFlow, no guard is necessary.
160 if (!m_parentFlowThread)
161 return StaticNodeList::adopt(regionNodes);
163 const RenderRegionList& regionList = m_parentFlowThread->renderRegionList();
164 for (RenderRegionList::const_iterator iter = regionList.begin(); iter != regionList.end(); ++iter) {
165 const RenderRegion* renderRegion = *iter;
166 // They will be included when we will properly support the Region interface
167 // http://dev.w3.org/csswg/css-regions/#the-region-interface
168 if (renderRegion->isPseudoElement())
170 regionNodes.append(renderRegion->node());
173 return StaticNodeList::adopt(regionNodes);
176 PassRefPtr<NodeList> NamedFlow::getContent()
178 Vector<RefPtr<Node> > contentNodes;
180 if (m_flowManager->document())
181 m_flowManager->document()->updateLayoutIgnorePendingStylesheets();
183 // The renderer may be destroyed or created after the style update.
184 // Because this is called from JS, where the wrapper keeps a reference to the NamedFlow, no guard is necessary.
185 if (!m_parentFlowThread)
186 return StaticNodeList::adopt(contentNodes);
188 const NamedFlowContentNodes& contentNodesList = m_parentFlowThread->contentNodes();
189 for (NamedFlowContentNodes::const_iterator it = contentNodesList.begin(); it != contentNodesList.end(); ++it) {
191 ASSERT(node->computedStyle()->flowThread() == m_parentFlowThread->flowThreadName());
192 contentNodes.append(node);
195 return StaticNodeList::adopt(contentNodes);
198 void NamedFlow::setRenderer(RenderNamedFlowThread* parentFlowThread)
200 // The named flow can either go from a no_renderer->renderer or renderer->no_renderer state; anything else could indicate a bug.
201 ASSERT((!m_parentFlowThread && parentFlowThread) || (m_parentFlowThread && !parentFlowThread));
203 // If parentFlowThread is 0, the flow thread will move in the "NULL" state.
204 m_parentFlowThread = parentFlowThread;
207 void NamedFlow::dispatchRegionLayoutUpdateEvent()
209 ASSERT(!NoEventDispatchAssertion::isEventDispatchForbidden());
211 // If the flow is in the "NULL" state the event should not be dispatched any more.
212 if (flowState() == FlowStateNull)
215 RefPtr<Event> event = UIEvent::create(EventTypeNames::webkitregionlayoutupdate, false, false, m_flowManager->document()->domWindow(), 0);
217 dispatchEvent(event);
220 void NamedFlow::dispatchRegionOversetChangeEvent()
222 ASSERT(!NoEventDispatchAssertion::isEventDispatchForbidden());
224 // If the flow is in the "NULL" state the event should not be dispatched any more.
225 if (flowState() == FlowStateNull)
228 RefPtr<Event> event = UIEvent::create(EventTypeNames::webkitregionoversetchange, false, false, m_flowManager->document()->domWindow(), 0);
230 dispatchEvent(event);
233 const AtomicString& NamedFlow::interfaceName() const
235 return EventTargetNames::NamedFlow;
238 ExecutionContext* NamedFlow::executionContext() const
240 return m_flowManager->document();
243 Node* NamedFlow::ownerNode() const
245 return m_flowManager->document();
248 } // namespace WebCore