- add third_party src.
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / dom / NamedFlow.cpp
1 /*
2  * Copyright (C) 2011 Adobe Systems Incorporated. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above
9  *    copyright notice, this list of conditions and the following
10  *    disclaimer.
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.
15  *
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
27  * SUCH DAMAGE.
28  */
29
30 #include "config.h"
31 #include "core/dom/NamedFlow.h"
32
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"
40
41 namespace WebCore {
42
43 NamedFlow::NamedFlow(PassRefPtr<NamedFlowCollection> manager, const AtomicString& flowThreadName)
44     : m_flowThreadName(flowThreadName)
45     , m_flowManager(manager)
46     , m_parentFlowThread(0)
47 {
48     ASSERT(RuntimeEnabledFeatures::cssRegionsEnabled());
49     ScriptWrappable::init(this);
50 }
51
52 NamedFlow::~NamedFlow()
53 {
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);
56 }
57
58 PassRefPtr<NamedFlow> NamedFlow::create(PassRefPtr<NamedFlowCollection> manager, const AtomicString& flowThreadName)
59 {
60     return adoptRef(new NamedFlow(manager, flowThreadName));
61 }
62
63 const AtomicString& NamedFlow::name() const
64 {
65     return m_flowThreadName;
66 }
67
68 bool NamedFlow::overset() const
69 {
70     if (m_flowManager->document())
71         m_flowManager->document()->updateLayoutIgnorePendingStylesheets();
72
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;
76 }
77
78 static inline bool inFlowThread(RenderObject* renderer, RenderNamedFlowThread* flowThread)
79 {
80     if (!renderer)
81         return false;
82     RenderFlowThread* currentFlowThread = renderer->flowThreadContainingBlock();
83     if (flowThread == currentFlowThread)
84         return true;
85     if (renderer->flowThreadState() != RenderObject::InsideInFlowThread)
86         return false;
87
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);
90 }
91
92 int NamedFlow::firstEmptyRegionIndex() const
93 {
94     if (m_flowManager->document())
95         m_flowManager->document()->updateLayoutIgnorePendingStylesheets();
96
97     if (!m_parentFlowThread)
98         return -1;
99
100     const RenderRegionList& regionList = m_parentFlowThread->renderRegionList();
101     if (regionList.isEmpty())
102         return -1;
103
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())
112             continue;
113         countNonPseudoRegions++;
114         if (renderRegion->regionOversetState() == RegionEmpty)
115             return countNonPseudoRegions;
116     }
117     return -1;
118 }
119
120 PassRefPtr<NodeList> NamedFlow::getRegionsByContent(Node* contentNode)
121 {
122     Vector<RefPtr<Node> > regionNodes;
123
124     if (!contentNode)
125         return StaticNodeList::adopt(regionNodes);
126
127     if (m_flowManager->document())
128         m_flowManager->document()->updateLayoutIgnorePendingStylesheets();
129
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);
134
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())
142                 continue;
143             if (m_parentFlowThread->objectInFlowRegion(contentNode->renderer(), renderRegion))
144                 regionNodes.append(renderRegion->node());
145         }
146     }
147
148     return StaticNodeList::adopt(regionNodes);
149 }
150
151 PassRefPtr<NodeList> NamedFlow::getRegions()
152 {
153     Vector<RefPtr<Node> > regionNodes;
154
155     if (m_flowManager->document())
156         m_flowManager->document()->updateLayoutIgnorePendingStylesheets();
157
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);
162
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())
169             continue;
170         regionNodes.append(renderRegion->node());
171     }
172
173     return StaticNodeList::adopt(regionNodes);
174 }
175
176 PassRefPtr<NodeList> NamedFlow::getContent()
177 {
178     Vector<RefPtr<Node> > contentNodes;
179
180     if (m_flowManager->document())
181         m_flowManager->document()->updateLayoutIgnorePendingStylesheets();
182
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);
187
188     const NamedFlowContentNodes& contentNodesList = m_parentFlowThread->contentNodes();
189     for (NamedFlowContentNodes::const_iterator it = contentNodesList.begin(); it != contentNodesList.end(); ++it) {
190         Node* node = *it;
191         ASSERT(node->computedStyle()->flowThread() == m_parentFlowThread->flowThreadName());
192         contentNodes.append(node);
193     }
194
195     return StaticNodeList::adopt(contentNodes);
196 }
197
198 void NamedFlow::setRenderer(RenderNamedFlowThread* parentFlowThread)
199 {
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));
202
203     // If parentFlowThread is 0, the flow thread will move in the "NULL" state.
204     m_parentFlowThread = parentFlowThread;
205 }
206
207 void NamedFlow::dispatchRegionLayoutUpdateEvent()
208 {
209     ASSERT(!NoEventDispatchAssertion::isEventDispatchForbidden());
210
211     // If the flow is in the "NULL" state the event should not be dispatched any more.
212     if (flowState() == FlowStateNull)
213         return;
214
215     RefPtr<Event> event = UIEvent::create(EventTypeNames::webkitregionlayoutupdate, false, false, m_flowManager->document()->domWindow(), 0);
216
217     dispatchEvent(event);
218 }
219
220 void NamedFlow::dispatchRegionOversetChangeEvent()
221 {
222     ASSERT(!NoEventDispatchAssertion::isEventDispatchForbidden());
223
224     // If the flow is in the "NULL" state the event should not be dispatched any more.
225     if (flowState() == FlowStateNull)
226         return;
227
228     RefPtr<Event> event = UIEvent::create(EventTypeNames::webkitregionoversetchange, false, false, m_flowManager->document()->domWindow(), 0);
229
230     dispatchEvent(event);
231 }
232
233 const AtomicString& NamedFlow::interfaceName() const
234 {
235     return EventTargetNames::NamedFlow;
236 }
237
238 ExecutionContext* NamedFlow::executionContext() const
239 {
240     return m_flowManager->document();
241 }
242
243 Node* NamedFlow::ownerNode() const
244 {
245     return m_flowManager->document();
246 }
247
248 } // namespace WebCore
249