1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 /* Here is the list, from beppe and glazman:
7 href >> A, AREA, BASE, LINK
8 src >> FRAME, IFRAME, IMG, INPUT, SCRIPT
9 <META http-equiv="refresh" content="3,http://www.acme.com/intro.html">
10 longdesc >> FRAME, IFRAME, IMG
11 usemap >> IMG, INPUT, OBJECT
14 codebase >> OBJECT, APPLET
17 cite >> BLOCKQUOTE, DEL, INS, Q
19 ARCHIVE attribute on APPLET ; warning, it contains a list of URIs.
21 Easier way of organizing the list:
26 blockquote: cite (not normally rewritable)
32 img: src, longdesc, usemap
34 applet: codebase, archive <list>
35 object: codebase, data, classid, usemap
42 #include "nsHTMLURIRefObject.h"
44 #include "mozilla/mozalloc.h"
45 #include "nsAString.h"
49 #include "nsIDOMAttr.h"
50 #include "nsIDOMElement.h"
51 #include "nsIDOMMozNamedAttrMap.h"
52 #include "nsIDOMNode.h"
53 #include "nsISupportsUtils.h"
55 #include "nsAutoPtr.h"
57 // String classes change too often and I can't keep up.
58 // Set this macro to this week's approved case-insensitive compare routine.
59 #define MATCHES(tagName, str) tagName.EqualsIgnoreCase(str)
61 nsHTMLURIRefObject::nsHTMLURIRefObject()
62 : mCurAttrIndex(0), mAttributeCnt(0)
66 nsHTMLURIRefObject::~nsHTMLURIRefObject()
70 //Interfaces for addref and release and queryinterface
71 NS_IMPL_ISUPPORTS(nsHTMLURIRefObject
, nsIURIRefObject
)
74 nsHTMLURIRefObject::Reset()
81 nsHTMLURIRefObject::GetNextURI(nsAString
& aURI
)
83 NS_ENSURE_TRUE(mNode
, NS_ERROR_NOT_INITIALIZED
);
86 nsresult rv
= mNode
->GetNodeName(tagName
);
87 NS_ENSURE_SUCCESS(rv
, rv
);
89 // Loop over attribute list:
92 nsCOMPtr
<nsIDOMElement
> element (do_QueryInterface(mNode
));
93 NS_ENSURE_TRUE(element
, NS_ERROR_INVALID_ARG
);
96 element
->GetAttributes(getter_AddRefs(mAttributes
));
97 NS_ENSURE_TRUE(mAttributes
, NS_ERROR_NOT_INITIALIZED
);
99 rv
= mAttributes
->GetLength(&mAttributeCnt
);
100 NS_ENSURE_SUCCESS(rv
, rv
);
101 NS_ENSURE_TRUE(mAttributeCnt
, NS_ERROR_FAILURE
);
105 while (mCurAttrIndex
< mAttributeCnt
)
107 nsCOMPtr
<nsIDOMAttr
> attrNode
;
108 rv
= mAttributes
->Item(mCurAttrIndex
++, getter_AddRefs(attrNode
));
109 NS_ENSURE_SUCCESS(rv
, rv
);
110 NS_ENSURE_ARG_POINTER(attrNode
);
112 rv
= attrNode
->GetName(curAttr
);
113 NS_ENSURE_SUCCESS(rv
, rv
);
115 // href >> A, AREA, BASE, LINK
116 if (MATCHES(curAttr
, "href"))
118 if (!MATCHES(tagName
, "a") && !MATCHES(tagName
, "area")
119 && !MATCHES(tagName
, "base") && !MATCHES(tagName
, "link"))
121 rv
= attrNode
->GetValue(aURI
);
122 NS_ENSURE_SUCCESS(rv
, rv
);
124 // href pointing to a named anchor doesn't count
125 if (aURI
.First() != char16_t('#'))
128 return NS_ERROR_INVALID_ARG
;
130 // src >> FRAME, IFRAME, IMG, INPUT, SCRIPT
131 else if (MATCHES(curAttr
, "src"))
133 if (!MATCHES(tagName
, "img")
134 && !MATCHES(tagName
, "frame") && !MATCHES(tagName
, "iframe")
135 && !MATCHES(tagName
, "input") && !MATCHES(tagName
, "script"))
137 return attrNode
->GetValue(aURI
);
139 //<META http-equiv="refresh" content="3,http://www.acme.com/intro.html">
140 else if (MATCHES(curAttr
, "content"))
142 if (!MATCHES(tagName
, "meta"))
145 // longdesc >> FRAME, IFRAME, IMG
146 else if (MATCHES(curAttr
, "longdesc"))
148 if (!MATCHES(tagName
, "img")
149 && !MATCHES(tagName
, "frame") && !MATCHES(tagName
, "iframe"))
152 // usemap >> IMG, INPUT, OBJECT
153 else if (MATCHES(curAttr
, "usemap"))
155 if (!MATCHES(tagName
, "img")
156 && !MATCHES(tagName
, "input") && !MATCHES(tagName
, "object"))
160 else if (MATCHES(curAttr
, "action"))
162 if (!MATCHES(tagName
, "form"))
165 // background >> BODY
166 else if (MATCHES(curAttr
, "background"))
168 if (!MATCHES(tagName
, "body"))
171 // codebase >> OBJECT, APPLET
172 else if (MATCHES(curAttr
, "codebase"))
174 if (!MATCHES(tagName
, "meta"))
178 else if (MATCHES(curAttr
, "classid"))
180 if (!MATCHES(tagName
, "object"))
184 else if (MATCHES(curAttr
, "data"))
186 if (!MATCHES(tagName
, "object"))
189 // cite >> BLOCKQUOTE, DEL, INS, Q
190 else if (MATCHES(curAttr
, "cite"))
192 if (!MATCHES(tagName
, "blockquote") && !MATCHES(tagName
, "q")
193 && !MATCHES(tagName
, "del") && !MATCHES(tagName
, "ins"))
197 else if (MATCHES(curAttr
, "profile"))
199 if (!MATCHES(tagName
, "head"))
202 // archive attribute on APPLET; warning, it contains a list of URIs.
203 else if (MATCHES(curAttr
, "archive"))
205 if (!MATCHES(tagName
, "applet"))
209 // Return a code to indicate that there are no more,
210 // to distinguish that case from real errors.
211 return NS_ERROR_NOT_AVAILABLE
;
215 nsHTMLURIRefObject::RewriteAllURIs(const nsAString
& aOldPat
,
216 const nsAString
& aNewPat
,
219 return NS_ERROR_NOT_IMPLEMENTED
;
223 nsHTMLURIRefObject::GetNode(nsIDOMNode
** aNode
)
225 NS_ENSURE_TRUE(mNode
, NS_ERROR_NOT_INITIALIZED
);
226 NS_ENSURE_TRUE(aNode
, NS_ERROR_NULL_POINTER
);
227 *aNode
= mNode
.get();
233 nsHTMLURIRefObject::SetNode(nsIDOMNode
*aNode
)
236 nsAutoString dummyURI
;
237 if (NS_SUCCEEDED(GetNextURI(dummyURI
)))
239 mCurAttrIndex
= 0; // Reset so we'll get the first node next time
243 // If there weren't any URIs in the attributes,
244 // then don't accept this node.
246 return NS_ERROR_INVALID_ARG
;
249 nsresult
NS_NewHTMLURIRefObject(nsIURIRefObject
** aResult
, nsIDOMNode
* aNode
)
251 RefPtr
<nsHTMLURIRefObject
> refObject
= new nsHTMLURIRefObject();
252 nsresult rv
= refObject
->SetNode(aNode
);
257 refObject
.forget(aResult
);