KHtml

SVGElementInstance.cpp
1 /*
2  Copyright (C) 2007 Nikolas Zimmermann <[email protected]>
3 
4  This file is part of the KDE project
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License as published by the Free Software Foundation; either
9  version 2 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Library General Public License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to
18  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  Boston, MA 02110-1301, USA.
20 */
21 
22 #include "wtf/Platform.h"
23 
24 #if ENABLE(SVG)
25 #include "SVGElementInstance.h"
26 
27 /*#include "Event.h"
28 #include "EventListener.h"*/
29 #include "SVGElementInstanceList.h"
30 #include "SVGUseElement.h"
31 
32 #include <wtf/Assertions.h>
33 
34 namespace WebCore
35 {
36 
37 SVGElementInstance::SVGElementInstance(SVGUseElement *useElement, PassRefPtr<SVGElement> originalElement)
38  : m_refCount(0)
39  , m_parent(nullptr)
40  , m_useElement(useElement)
41  , m_element(originalElement)
42  , m_shadowTreeElement(nullptr)
43  , m_previousSibling(nullptr)
44  , m_nextSibling(nullptr)
45  , m_firstChild(nullptr)
46  , m_lastChild(nullptr)
47 {
48  ASSERT(m_useElement);
49  ASSERT(m_element);
50 
51  // Register as instance for passed element.
52  m_element->document()->accessSVGExtensions()->mapInstanceToElement(this, m_element.get());
53 }
54 
55 SVGElementInstance::~SVGElementInstance()
56 {
57  for (RefPtr<SVGElementInstance> child = m_firstChild; child; child = child->m_nextSibling) {
58  child->setParent(nullptr);
59  }
60 
61  // Deregister as instance for passed element.
62  m_element->document()->accessSVGExtensions()->removeInstanceMapping(this, m_element.get());
63 }
64 
65 SVGElement *SVGElementInstance::correspondingElement() const
66 {
67  return m_element.get();
68 }
69 
70 SVGUseElement *SVGElementInstance::correspondingUseElement() const
71 {
72  return m_useElement;
73 }
74 
75 SVGElementInstance *SVGElementInstance::parentNode() const
76 {
77  return parent();
78 }
79 
80 PassRefPtr<SVGElementInstanceList> SVGElementInstance::childNodes()
81 {
82  return SVGElementInstanceList::create(this);
83 }
84 
85 SVGElementInstance *SVGElementInstance::previousSibling() const
86 {
87  return m_previousSibling;
88 }
89 
90 SVGElementInstance *SVGElementInstance::nextSibling() const
91 {
92  return m_nextSibling;
93 }
94 
95 SVGElementInstance *SVGElementInstance::firstChild() const
96 {
97  return m_firstChild;
98 }
99 
100 SVGElementInstance *SVGElementInstance::lastChild() const
101 {
102  return m_lastChild;
103 }
104 
105 SVGElement *SVGElementInstance::shadowTreeElement() const
106 {
107  return m_shadowTreeElement;
108 }
109 
110 void SVGElementInstance::setShadowTreeElement(SVGElement *element)
111 {
112  ASSERT(element);
113  m_shadowTreeElement = element;
114 }
115 
116 void SVGElementInstance::appendChild(PassRefPtr<SVGElementInstance> child)
117 {
118  child->setParent(this);
119 
120  if (m_lastChild) {
121  child->m_previousSibling = m_lastChild;
122  m_lastChild->m_nextSibling = child.get();
123  } else {
124  m_firstChild = child.get();
125  }
126 
127  m_lastChild = child.get();
128 }
129 
130 // Helper function for updateInstance
131 static bool containsUseChildNode(Node *start)
132 {
133  if (start->hasTagName(SVGNames::useTag)) {
134  return true;
135  }
136 
137  for (Node *current = start->firstChild(); current; current = current->nextSibling()) {
138  if (containsUseChildNode(current)) {
139  return true;
140  }
141  }
142 
143  return false;
144 }
145 
146 void SVGElementInstance::updateInstance(SVGElement *element)
147 {
148  ASSERT(element == m_element);
149  ASSERT(m_shadowTreeElement);
150  Q_UNUSED(element);
151 
152  // TODO: Eventually come up with a more optimized updating logic for the cases below:
153  //
154  // <symbol>: We can't just clone the original element, we need to apply
155  // the same "replace by generated content" logic that SVGUseElement does.
156  //
157  // <svg>: <use> on <svg> is too rare to actually implement it faster.
158  // If someone still wants to do it: recloning, adjusting width/height attributes is enough.
159  //
160  // <use>: Too hard to get it right in a fast way. Recloning seems the only option.
161 
162  if (m_element->hasTagName(SVGNames::symbolTag) ||
163  m_element->hasTagName(SVGNames::svgTag) ||
164  containsUseChildNode(m_element.get())) {
165  m_useElement->buildPendingResource();
166  return;
167  }
168 
169  // For all other nodes this logic is sufficient.
170  WTF::PassRefPtr<Node> clone = m_element->cloneNode(true);
171  SVGUseElement::removeDisallowedElementsFromSubtree(clone.get());
172  SVGElement *svgClone = nullptr;
173  if (clone && clone->isSVGElement()) {
174  svgClone = static_cast<SVGElement *>(clone.get());
175  }
176  ASSERT(svgClone);
177 
178  // Replace node in the <use> shadow tree
179  /*ExceptionCode*//*khtml*/int ec = 0;
180  m_shadowTreeElement->parentNode()->replaceChild(clone.releaseRef(), m_shadowTreeElement, ec);
181  ASSERT(ec == 0);
182 
183  m_shadowTreeElement = svgClone;
184 }
185 
186 SVGElementInstance *SVGElementInstance::toSVGElementInstance()
187 {
188  return this;
189 }
190 
191 EventTargetNode *SVGElementInstance::toNode()
192 {
193  return m_element.get();
194 }
195 
196 void SVGElementInstance::addEventListener(const AtomicString &eventType, PassRefPtr<EventListener> eventListener, bool useCapture)
197 {
198  Q_UNUSED(eventType);
199  Q_UNUSED(eventListener);
200  Q_UNUSED(useCapture);
201  // FIXME!
202 }
203 
204 void SVGElementInstance::removeEventListener(const AtomicString &eventType, EventListener *eventListener, bool useCapture)
205 {
206  Q_UNUSED(eventType);
207  Q_UNUSED(eventListener);
208  Q_UNUSED(useCapture);
209  // FIXME!
210 }
211 
212 bool SVGElementInstance::dispatchEvent(PassRefPtr<Event>, ExceptionCode &ec, bool tempEvent)
213 {
214  Q_UNUSED(ec);
215  Q_UNUSED(tempEvent);
216  // FIXME!
217  return false;
218 }
219 
220 }
221 
222 #endif // ENABLE(SVG)
223 
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Oct 16 2021 22:48:01 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.