1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=2 sw=2 et tw=78: */
3 : /* ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
6 : * The contents of this file are subject to the Mozilla Public License Version
7 : * 1.1 (the "License"); you may not use this file except in compliance with
8 : * the License. You may obtain a copy of the License at
9 : * http://www.mozilla.org/MPL/
10 : *
11 : * Software distributed under the License is distributed on an "AS IS" basis,
12 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 : * for the specific language governing rights and limitations under the
14 : * License.
15 : *
16 : * The Original Code is the Mozilla SVG project.
17 : *
18 : * The Initial Developer of the Original Code is
19 : * Crocodile Clips Ltd..
20 : * Portions created by the Initial Developer are Copyright (C) 2002
21 : * the Initial Developer. All Rights Reserved.
22 : *
23 : * Contributor(s):
24 : * Alex Fritze <alex@croczilla.com> (original author)
25 : *
26 : * Alternatively, the contents of this file may be used under the terms of
27 : * either the GNU General Public License Version 2 or later (the "GPL"), or
28 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 : * in which case the provisions of the GPL or the LGPL are applicable instead
30 : * of those above. If you wish to allow use of your version of this file only
31 : * under the terms of either the GPL or the LGPL, and not to allow others to
32 : * use your version of this file under the terms of the MPL, indicate your
33 : * decision by deleting the provisions above and replace them with the notice
34 : * and other provisions required by the GPL or the LGPL. If you do not delete
35 : * the provisions above, a recipient may use your version of this file under
36 : * the terms of any one of the MPL, the GPL or the LGPL.
37 : *
38 : * ***** END LICENSE BLOCK ***** */
39 :
40 : #include "mozilla/Util.h"
41 :
42 : #include "nsSVGElement.h"
43 : #include "nsGkAtoms.h"
44 : #include "nsIDOMSVGScriptElement.h"
45 : #include "nsIDOMSVGURIReference.h"
46 : #include "nsCOMPtr.h"
47 : #include "nsSVGString.h"
48 : #include "nsIDocument.h"
49 : #include "nsIURI.h"
50 : #include "nsNetUtil.h"
51 : #include "nsScriptElement.h"
52 : #include "nsContentUtils.h"
53 :
54 : using namespace mozilla;
55 : using namespace mozilla::dom;
56 :
57 : typedef nsSVGElement nsSVGScriptElementBase;
58 :
59 : class nsSVGScriptElement : public nsSVGScriptElementBase,
60 : public nsIDOMSVGScriptElement,
61 : public nsIDOMSVGURIReference,
62 : public nsScriptElement
63 0 : {
64 : protected:
65 : friend nsresult NS_NewSVGScriptElement(nsIContent **aResult,
66 : already_AddRefed<nsINodeInfo> aNodeInfo,
67 : FromParser aFromParser);
68 : nsSVGScriptElement(already_AddRefed<nsINodeInfo> aNodeInfo,
69 : FromParser aFromParser);
70 :
71 : public:
72 : // interfaces:
73 :
74 : NS_DECL_ISUPPORTS_INHERITED
75 : NS_DECL_NSIDOMSVGSCRIPTELEMENT
76 : NS_DECL_NSIDOMSVGURIREFERENCE
77 :
78 : // xxx If xpcom allowed virtual inheritance we wouldn't need to
79 : // forward here :-(
80 0 : NS_FORWARD_NSIDOMNODE(nsSVGScriptElementBase::)
81 0 : NS_FORWARD_NSIDOMELEMENT(nsSVGScriptElementBase::)
82 0 : NS_FORWARD_NSIDOMSVGELEMENT(nsSVGScriptElementBase::)
83 :
84 : // nsIScriptElement
85 : virtual void GetScriptType(nsAString& type);
86 : virtual void GetScriptText(nsAString& text);
87 : virtual void GetScriptCharset(nsAString& charset);
88 : virtual void FreezeUriAsyncDefer();
89 : virtual CORSMode GetCORSMode() const;
90 :
91 : // nsScriptElement
92 : virtual bool HasScriptContent();
93 :
94 : // nsIContent specializations:
95 : virtual nsresult BindToTree(nsIDocument* aDocument, nsIContent* aParent,
96 : nsIContent* aBindingParent,
97 : bool aCompileEventHandlers);
98 : virtual nsresult AfterSetAttr(PRInt32 aNamespaceID, nsIAtom* aName,
99 : const nsAttrValue* aValue, bool aNotify);
100 : virtual bool ParseAttribute(PRInt32 aNamespaceID,
101 : nsIAtom* aAttribute,
102 : const nsAString& aValue,
103 : nsAttrValue& aResult);
104 :
105 : virtual nsresult Clone(nsINodeInfo *aNodeInfo, nsINode **aResult) const;
106 :
107 : virtual nsXPCClassInfo* GetClassInfo();
108 : protected:
109 : virtual StringAttributesInfo GetStringInfo();
110 :
111 : enum { HREF };
112 : nsSVGString mStringAttributes[1];
113 : static StringInfo sStringInfo[1];
114 : };
115 :
116 : nsSVGElement::StringInfo nsSVGScriptElement::sStringInfo[1] =
117 : {
118 : { &nsGkAtoms::href, kNameSpaceID_XLink, false }
119 : };
120 :
121 0 : NS_IMPL_NS_NEW_SVG_ELEMENT_CHECK_PARSER(Script)
122 :
123 : //----------------------------------------------------------------------
124 : // nsISupports methods
125 :
126 0 : NS_IMPL_ADDREF_INHERITED(nsSVGScriptElement,nsSVGScriptElementBase)
127 0 : NS_IMPL_RELEASE_INHERITED(nsSVGScriptElement,nsSVGScriptElementBase)
128 :
129 0 : DOMCI_NODE_DATA(SVGScriptElement, nsSVGScriptElement)
130 :
131 0 : NS_INTERFACE_TABLE_HEAD(nsSVGScriptElement)
132 0 : NS_NODE_INTERFACE_TABLE8(nsSVGScriptElement, nsIDOMNode, nsIDOMElement,
133 : nsIDOMSVGElement, nsIDOMSVGScriptElement,
134 : nsIDOMSVGURIReference, nsIScriptLoaderObserver,
135 : nsIScriptElement, nsIMutationObserver)
136 0 : NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(SVGScriptElement)
137 0 : NS_INTERFACE_MAP_END_INHERITING(nsSVGScriptElementBase)
138 :
139 : //----------------------------------------------------------------------
140 : // Implementation
141 :
142 0 : nsSVGScriptElement::nsSVGScriptElement(already_AddRefed<nsINodeInfo> aNodeInfo,
143 : FromParser aFromParser)
144 : : nsSVGScriptElementBase(aNodeInfo)
145 0 : , nsScriptElement(aFromParser)
146 : {
147 0 : AddMutationObserver(this);
148 0 : }
149 :
150 : //----------------------------------------------------------------------
151 : // nsIDOMNode methods
152 :
153 : nsresult
154 0 : nsSVGScriptElement::Clone(nsINodeInfo *aNodeInfo, nsINode **aResult) const
155 : {
156 0 : *aResult = nsnull;
157 :
158 0 : nsCOMPtr<nsINodeInfo> ni = aNodeInfo;
159 0 : nsSVGScriptElement* it = new nsSVGScriptElement(ni.forget(), NOT_FROM_PARSER);
160 :
161 0 : nsCOMPtr<nsINode> kungFuDeathGrip = it;
162 0 : nsresult rv = it->Init();
163 0 : rv |= CopyInnerTo(it);
164 0 : NS_ENSURE_SUCCESS(rv, rv);
165 :
166 : // The clone should be marked evaluated if we are.
167 0 : it->mAlreadyStarted = mAlreadyStarted;
168 0 : it->mLineNumber = mLineNumber;
169 0 : it->mMalformed = mMalformed;
170 :
171 0 : kungFuDeathGrip.swap(*aResult);
172 :
173 0 : return NS_OK;
174 : }
175 :
176 : //----------------------------------------------------------------------
177 : // nsIDOMSVGScriptElement methods
178 :
179 : /* attribute DOMString type; */
180 : NS_IMETHODIMP
181 0 : nsSVGScriptElement::GetType(nsAString & aType)
182 : {
183 0 : GetAttr(kNameSpaceID_None, nsGkAtoms::type, aType);
184 :
185 0 : return NS_OK;
186 : }
187 : NS_IMETHODIMP
188 0 : nsSVGScriptElement::SetType(const nsAString & aType)
189 : {
190 0 : return SetAttr(kNameSpaceID_None, nsGkAtoms::type, aType, true);
191 : }
192 :
193 : //----------------------------------------------------------------------
194 : // nsIDOMSVGURIReference methods
195 :
196 : /* readonly attribute nsIDOMSVGAnimatedString href; */
197 : NS_IMETHODIMP
198 0 : nsSVGScriptElement::GetHref(nsIDOMSVGAnimatedString * *aHref)
199 : {
200 0 : return mStringAttributes[HREF].ToDOMAnimatedString(aHref, this);
201 : }
202 :
203 : //----------------------------------------------------------------------
204 : // nsIScriptElement methods
205 :
206 : void
207 0 : nsSVGScriptElement::GetScriptType(nsAString& type)
208 : {
209 0 : GetType(type);
210 0 : }
211 :
212 : void
213 0 : nsSVGScriptElement::GetScriptText(nsAString& text)
214 : {
215 0 : nsContentUtils::GetNodeTextContent(this, false, text);
216 0 : }
217 :
218 : void
219 0 : nsSVGScriptElement::GetScriptCharset(nsAString& charset)
220 : {
221 0 : charset.Truncate();
222 0 : }
223 :
224 : void
225 0 : nsSVGScriptElement::FreezeUriAsyncDefer()
226 : {
227 0 : if (mFrozen) {
228 0 : return;
229 : }
230 :
231 0 : if (mStringAttributes[HREF].IsExplicitlySet()) {
232 : // variation of this code in nsHTMLScriptElement - check if changes
233 : // need to be transfered when modifying
234 0 : nsAutoString src;
235 0 : mStringAttributes[HREF].GetAnimValue(src, this);
236 :
237 0 : nsCOMPtr<nsIURI> baseURI = GetBaseURI();
238 0 : NS_NewURI(getter_AddRefs(mUri), src, nsnull, baseURI);
239 : // At this point mUri will be null for invalid URLs.
240 0 : mExternal = true;
241 : }
242 :
243 0 : mFrozen = true;
244 : }
245 :
246 : //----------------------------------------------------------------------
247 : // nsScriptElement methods
248 :
249 : bool
250 0 : nsSVGScriptElement::HasScriptContent()
251 : {
252 0 : return (mFrozen ? mExternal : mStringAttributes[HREF].IsExplicitlySet()) ||
253 0 : nsContentUtils::HasNonEmptyTextContent(this);
254 : }
255 :
256 : //----------------------------------------------------------------------
257 : // nsSVGElement methods
258 :
259 : nsSVGElement::StringAttributesInfo
260 0 : nsSVGScriptElement::GetStringInfo()
261 : {
262 : return StringAttributesInfo(mStringAttributes, sStringInfo,
263 0 : ArrayLength(sStringInfo));
264 : }
265 :
266 : //----------------------------------------------------------------------
267 : // nsIContent methods
268 :
269 : nsresult
270 0 : nsSVGScriptElement::BindToTree(nsIDocument* aDocument, nsIContent* aParent,
271 : nsIContent* aBindingParent,
272 : bool aCompileEventHandlers)
273 : {
274 : nsresult rv = nsSVGScriptElementBase::BindToTree(aDocument, aParent,
275 : aBindingParent,
276 0 : aCompileEventHandlers);
277 0 : NS_ENSURE_SUCCESS(rv, rv);
278 :
279 0 : if (aDocument) {
280 0 : MaybeProcessScript();
281 : }
282 :
283 0 : return NS_OK;
284 : }
285 :
286 : nsresult
287 0 : nsSVGScriptElement::AfterSetAttr(PRInt32 aNamespaceID, nsIAtom* aName,
288 : const nsAttrValue* aValue, bool aNotify)
289 : {
290 0 : if (aNamespaceID == kNameSpaceID_XLink && aName == nsGkAtoms::href) {
291 0 : MaybeProcessScript();
292 : }
293 : return nsSVGScriptElementBase::AfterSetAttr(aNamespaceID, aName,
294 0 : aValue, aNotify);
295 : }
296 :
297 : bool
298 0 : nsSVGScriptElement::ParseAttribute(PRInt32 aNamespaceID,
299 : nsIAtom* aAttribute,
300 : const nsAString& aValue,
301 : nsAttrValue& aResult)
302 : {
303 0 : if (aNamespaceID == kNameSpaceID_None &&
304 : aAttribute == nsGkAtoms::crossorigin) {
305 0 : ParseCORSValue(aValue, aResult);
306 0 : return true;
307 : }
308 :
309 : return nsSVGScriptElementBase::ParseAttribute(aNamespaceID, aAttribute,
310 0 : aValue, aResult);
311 : }
312 :
313 : CORSMode
314 0 : nsSVGScriptElement::GetCORSMode() const
315 : {
316 0 : return AttrValueToCORSMode(GetParsedAttr(nsGkAtoms::crossorigin));
317 : }
318 :
|