1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 /* Here is the list, from beppe and glazman:
39 href >> A, AREA, BASE, LINK
40 src >> FRAME, IFRAME, IMG, INPUT, SCRIPT
41 <META http-equiv="refresh" content="3,http://www.acme.com/intro.html">
42 longdesc >> FRAME, IFRAME, IMG
43 usemap >> IMG, INPUT, OBJECT
46 codebase >> OBJECT, APPLET
49 cite >> BLOCKQUOTE, DEL, INS, Q
51 ARCHIVE attribute on APPLET ; warning, it contains a list of URIs.
53 Easier way of organizing the list:
58 blockquote: cite (not normally rewritable)
64 img: src, longdesc, usemap
66 applet: codebase, archive <list>
67 object: codebase, data, classid, usemap
74 /* Here is how to open a channel for testing
75 (from embed/qa/testembed/Tests.cpp):
77 nsCOMPtr<nsIChannel> theChannel;
79 nsCOMPtr<nsIURI> theURI;
80 rv = NS_NewURI(getter_AddRefs(theURI), theSpec);
83 rv = NS_OpenURI(getter_AddRefs(theChannel), theURI, nsnull, theLoadGroup);
86 nsCOMPtr<nsILoadGroup> theLoadGroup(do_CreateInstance(NS_LOADGROUP_CONTRACTID));
89 nsCOMPtr<nsIStreamListener> listener(static_cast<nsIStreamListener*>(qaBrowserImpl));
90 //nsCOMPtr<nsIWeakReference> thisListener(do_GetWeakReference(listener));
91 //qaWebBrowser->AddWebBrowserListener(thisListener, NS_GET_IID(nsIStreamListener));
93 // this calls nsIStreamListener::OnDataAvailable()
94 rv = theChannel->AsyncOpen(listener, nsnull);
96 nsCOMPtr<nsIRequest> theRequest = do_QueryInterface(theChannel);
97 // Now we can do things on nsIRequest (like what?)
100 #include "nsHTMLURIRefObject.h"
102 #include "nsAString.h"
103 #include "nsString.h"
104 #include "nsIDOMAttr.h"
105 #include "nsIDOMElement.h"
107 // String classes change too often and I can't keep up.
108 // Set this macro to this week's approved case-insensitive compare routine.
109 #define MATCHES(tagName, str) tagName.EqualsIgnoreCase(str)
111 nsHTMLURIRefObject::nsHTMLURIRefObject()
113 mCurAttrIndex
= mAttributeCnt
= 0;
116 nsHTMLURIRefObject::~nsHTMLURIRefObject()
120 //Interfaces for addref and release and queryinterface
121 NS_IMPL_ISUPPORTS1(nsHTMLURIRefObject
, nsIURIRefObject
)
124 nsHTMLURIRefObject::Reset()
131 nsHTMLURIRefObject::GetNextURI(nsAString
& aURI
)
134 return NS_ERROR_NOT_INITIALIZED
;
136 nsAutoString tagName
;
137 nsresult rv
= mNode
->GetNodeName(tagName
);
141 // Loop over attribute list:
144 nsCOMPtr
<nsIDOMElement
> element (do_QueryInterface(mNode
));
146 return NS_ERROR_INVALID_ARG
;
149 mNode
->GetAttributes(getter_AddRefs(mAttributes
));
151 return NS_ERROR_NOT_INITIALIZED
;
153 rv
= mAttributes
->GetLength(&mAttributeCnt
);
154 NS_ENSURE_SUCCESS(rv
, rv
);
155 if (!mAttributeCnt
) return NS_ERROR_FAILURE
;
159 printf("Looking at tag '%s'\n",
160 NS_LossyConvertUTF16toASCII(tagName
).get());
162 while (mCurAttrIndex
< mAttributeCnt
)
164 nsCOMPtr
<nsIDOMNode
> attrNode
;
165 rv
= mAttributes
->Item(mCurAttrIndex
++, getter_AddRefs(attrNode
));
166 // XXX Does Item() addref, or not?
167 // The comparable code in nsEditor assumes it doesn't.
168 NS_ENSURE_SUCCESS(rv
, rv
);
169 NS_ENSURE_ARG_POINTER(attrNode
);
170 nsCOMPtr
<nsIDOMAttr
> curAttrNode (do_QueryInterface(attrNode
));
171 NS_ENSURE_ARG_POINTER(curAttrNode
);
173 rv
= curAttrNode
->GetName(curAttr
);
174 NS_ENSURE_SUCCESS(rv
, rv
);
176 // href >> A, AREA, BASE, LINK
178 printf("Trying to match attribute '%s'\n",
179 NS_LossyConvertUTF16toASCII(curAttr
).get());
181 if (MATCHES(curAttr
, "href"))
183 if (!MATCHES(tagName
, "a") && !MATCHES(tagName
, "area")
184 && !MATCHES(tagName
, "base") && !MATCHES(tagName
, "link"))
186 rv
= curAttrNode
->GetValue(aURI
);
187 NS_ENSURE_SUCCESS(rv
, rv
);
189 // href pointing to a named anchor doesn't count
190 if (aURI
.First() != PRUnichar('#'))
193 return NS_ERROR_INVALID_ARG
;
195 // src >> FRAME, IFRAME, IMG, INPUT, SCRIPT
196 else if (MATCHES(curAttr
, "src"))
198 if (!MATCHES(tagName
, "img")
199 && !MATCHES(tagName
, "frame") && !MATCHES(tagName
, "iframe")
200 && !MATCHES(tagName
, "input") && !MATCHES(tagName
, "script"))
202 return curAttrNode
->GetValue(aURI
);
204 //<META http-equiv="refresh" content="3,http://www.acme.com/intro.html">
205 else if (MATCHES(curAttr
, "content"))
207 if (!MATCHES(tagName
, "meta"))
210 // longdesc >> FRAME, IFRAME, IMG
211 else if (MATCHES(curAttr
, "longdesc"))
213 if (!MATCHES(tagName
, "img")
214 && !MATCHES(tagName
, "frame") && !MATCHES(tagName
, "iframe"))
217 // usemap >> IMG, INPUT, OBJECT
218 else if (MATCHES(curAttr
, "usemap"))
220 if (!MATCHES(tagName
, "img")
221 && !MATCHES(tagName
, "input") && !MATCHES(tagName
, "object"))
225 else if (MATCHES(curAttr
, "action"))
227 if (!MATCHES(tagName
, "form"))
230 // background >> BODY
231 else if (MATCHES(curAttr
, "background"))
233 if (!MATCHES(tagName
, "body"))
236 // codebase >> OBJECT, APPLET
237 else if (MATCHES(curAttr
, "codebase"))
239 if (!MATCHES(tagName
, "meta"))
243 else if (MATCHES(curAttr
, "classid"))
245 if (!MATCHES(tagName
, "object"))
249 else if (MATCHES(curAttr
, "data"))
251 if (!MATCHES(tagName
, "object"))
254 // cite >> BLOCKQUOTE, DEL, INS, Q
255 else if (MATCHES(curAttr
, "cite"))
257 if (!MATCHES(tagName
, "blockquote") && !MATCHES(tagName
, "q")
258 && !MATCHES(tagName
, "del") && !MATCHES(tagName
, "ins"))
262 else if (MATCHES(curAttr
, "profile"))
264 if (!MATCHES(tagName
, "head"))
267 // archive attribute on APPLET; warning, it contains a list of URIs.
268 else if (MATCHES(curAttr
, "archive"))
270 if (!MATCHES(tagName
, "applet"))
274 // Return a code to indicate that there are no more,
275 // to distinguish that case from real errors.
276 return NS_ERROR_NOT_AVAILABLE
;
280 nsHTMLURIRefObject::RewriteAllURIs(const nsAString
& aOldPat
,
281 const nsAString
& aNewPat
,
285 printf("Can't rewrite URIs yet\n");
287 return NS_ERROR_NOT_IMPLEMENTED
;
291 nsHTMLURIRefObject::GetNode(nsIDOMNode
** aNode
)
294 return NS_ERROR_NOT_INITIALIZED
;
296 return NS_ERROR_NULL_POINTER
;
297 *aNode
= mNode
.get();
303 nsHTMLURIRefObject::SetNode(nsIDOMNode
*aNode
)
306 nsAutoString dummyURI
;
307 if (NS_SUCCEEDED(GetNextURI(dummyURI
)))
309 mCurAttrIndex
= 0; // Reset so we'll get the first node next time
313 // If there weren't any URIs in the attributes,
314 // then don't accept this node.
316 return NS_ERROR_INVALID_ARG
;
319 nsresult
NS_NewHTMLURIRefObject(nsIURIRefObject
** aResult
, nsIDOMNode
* aNode
)
321 nsHTMLURIRefObject
* refObject
= new nsHTMLURIRefObject();
322 if (!refObject
) return NS_ERROR_OUT_OF_MEMORY
;
323 nsresult rv
= refObject
->SetNode(aNode
);
329 return refObject
->QueryInterface(NS_GET_IID(nsIURIRefObject
),