1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* ***** BEGIN LICENSE BLOCK *****
3
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
5
* The contents of this file are subject to the Netscape Public License
6
* Version 1.1 (the "License"); you may not use this file except in
7
* compliance with the License. You may obtain a copy of the License at
8
* http://www.mozilla.org/NPL/
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.
25
* Alternatively, the contents of this file may be used under the terms of
26
* either the GNU General Public License Version 2 or later (the "GPL"), or
27
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28
* in which case the provisions of the GPL or the LGPL are applicable instead
29
* of those above. If you wish to allow use of your version of this file only
30
* under the terms of either the GPL or the LGPL, and not to allow others to
31
* use your version of this file under the terms of the NPL, indicate your
32
* decision by deleting the provisions above and replace them with the notice
33
* and other provisions required by the GPL or the LGPL. If you do not delete
34
* the provisions above, a recipient may use your version of this file under
35
* the terms of any one of the NPL, the GPL or the LGPL.
37
* ***** END LICENSE BLOCK ***** */
39
/* Here is the list, from beppe and glazman:
40
href >> A, AREA, BASE, LINK
41
src >> FRAME, IFRAME, IMG, INPUT, SCRIPT
42
<META http-equiv="refresh" content="3,http://www.acme.com/intro.html">
43
longdesc >> FRAME, IFRAME, IMG
44
usemap >> IMG, INPUT, OBJECT
47
codebase >> OBJECT, APPLET
50
cite >> BLOCKQUOTE, DEL, INS, Q
52
ARCHIVE attribute on APPLET ; warning, it contains a list of URIs.
54
Easier way of organizing the list:
59
blockquote: cite (not normally rewritable)
65
img: src, longdesc, usemap
67
applet: codebase, archive <list>
68
object: codebase, data, classid, usemap
75
/* Here is how to open a channel for testing
76
(from embed/qa/testembed/Tests.cpp):
78
nsCOMPtr<nsIChannel> theChannel;
80
nsCOMPtr<nsIURI> theURI;
81
rv = NS_NewURI(getter_AddRefs(theURI), theSpec);
84
rv = NS_OpenURI(getter_AddRefs(theChannel), theURI, nsnull, theLoadGroup);
87
nsCOMPtr<nsILoadGroup> theLoadGroup(do_CreateInstance(NS_LOADGROUP_CONTRACTID));
90
nsCOMPtr<nsIStreamListener> listener(NS_STATIC_CAST(nsIStreamListener*, qaBrowserImpl));
91
//nsCOMPtr<nsIWeakReference> thisListener(do_GetWeakReference(listener));
92
//qaWebBrowser->AddWebBrowserListener(thisListener, NS_GET_IID(nsIStreamListener));
94
// this calls nsIStreamListener::OnDataAvailable()
95
rv = theChannel->AsyncOpen(listener, nsnull);
97
nsCOMPtr<nsIRequest> theRequest = do_QueryInterface(theChannel);
98
// Now we can do things on nsIRequest (like what?)
101
#include "nsHTMLURIRefObject.h"
103
#include "nsAString.h"
104
#include "nsString.h"
105
#include "nsIDOMAttr.h"
106
#include "nsIDOMElement.h"
108
// String classes change too often and I can't keep up.
109
// Set this macro to this week's approved case-insensitive compare routine.
110
#define MATCHES(tagName, str) tagName.EqualsIgnoreCase(str)
112
nsHTMLURIRefObject::nsHTMLURIRefObject()
114
mCurAttrIndex = mAttributeCnt = 0;
117
nsHTMLURIRefObject::~nsHTMLURIRefObject()
121
//Interfaces for addref and release and queryinterface
122
NS_IMPL_ISUPPORTS1(nsHTMLURIRefObject, nsIURIRefObject)
125
nsHTMLURIRefObject::Reset()
132
nsHTMLURIRefObject::GetNextURI(nsAString & aURI)
135
return NS_ERROR_NOT_INITIALIZED;
137
nsAutoString tagName;
138
nsresult rv = mNode->GetNodeName(tagName);
142
// Loop over attribute list:
145
nsCOMPtr<nsIDOMElement> element (do_QueryInterface(mNode));
147
return NS_ERROR_INVALID_ARG;
150
mNode->GetAttributes(getter_AddRefs(mAttributes));
152
return NS_ERROR_NOT_INITIALIZED;
154
rv = mAttributes->GetLength(&mAttributeCnt);
155
NS_ENSURE_SUCCESS(rv, rv);
156
if (!mAttributeCnt) return NS_ERROR_FAILURE;
160
printf("Looking at tag '%s'\n",
161
NS_LossyConvertUCS2toASCII(tagName).get());
163
while (mCurAttrIndex < mAttributeCnt)
165
nsCOMPtr<nsIDOMNode> attrNode;
166
rv = mAttributes->Item(mCurAttrIndex++, getter_AddRefs(attrNode));
167
// XXX Does Item() addref, or not?
168
// The comparable code in nsEditor assumes it doesn't.
169
NS_ENSURE_SUCCESS(rv, rv);
170
NS_ENSURE_ARG_POINTER(attrNode);
171
nsCOMPtr<nsIDOMAttr> curAttrNode (do_QueryInterface(attrNode));
172
NS_ENSURE_ARG_POINTER(curAttrNode);
174
rv = curAttrNode->GetName(curAttr);
175
NS_ENSURE_SUCCESS(rv, rv);
177
// href >> A, AREA, BASE, LINK
179
printf("Trying to match attribute '%s'\n",
180
NS_LossyConvertUCS2toASCII(curAttr).get());
182
if (MATCHES(curAttr, "href"))
184
if (!MATCHES(tagName, "a") && !MATCHES(tagName, "area")
185
&& !MATCHES(tagName, "base") && !MATCHES(tagName, "link"))
187
rv = curAttrNode->GetValue(aURI);
188
NS_ENSURE_SUCCESS(rv, rv);
190
// href pointing to a named anchor doesn't count
191
if (aURI.First() != PRUnichar('#'))
194
return NS_ERROR_INVALID_ARG;
196
// src >> FRAME, IFRAME, IMG, INPUT, SCRIPT
197
else if (MATCHES(curAttr, "src"))
199
if (!MATCHES(tagName, "img")
200
&& !MATCHES(tagName, "frame") && !MATCHES(tagName, "iframe")
201
&& !MATCHES(tagName, "input") && !MATCHES(tagName, "script"))
203
return curAttrNode->GetValue(aURI);
205
//<META http-equiv="refresh" content="3,http://www.acme.com/intro.html">
206
else if (MATCHES(curAttr, "content"))
208
if (!MATCHES(tagName, "meta"))
211
// longdesc >> FRAME, IFRAME, IMG
212
else if (MATCHES(curAttr, "longdesc"))
214
if (!MATCHES(tagName, "img")
215
&& !MATCHES(tagName, "frame") && !MATCHES(tagName, "iframe"))
218
// usemap >> IMG, INPUT, OBJECT
219
else if (MATCHES(curAttr, "usemap"))
221
if (!MATCHES(tagName, "img")
222
&& !MATCHES(tagName, "input") && !MATCHES(tagName, "object"))
226
else if (MATCHES(curAttr, "action"))
228
if (!MATCHES(tagName, "form"))
231
// background >> BODY
232
else if (MATCHES(curAttr, "background"))
234
if (!MATCHES(tagName, "body"))
237
// codebase >> OBJECT, APPLET
238
else if (MATCHES(curAttr, "codebase"))
240
if (!MATCHES(tagName, "meta"))
244
else if (MATCHES(curAttr, "classid"))
246
if (!MATCHES(tagName, "object"))
250
else if (MATCHES(curAttr, "data"))
252
if (!MATCHES(tagName, "object"))
255
// cite >> BLOCKQUOTE, DEL, INS, Q
256
else if (MATCHES(curAttr, "cite"))
258
if (!MATCHES(tagName, "blockquote") && !MATCHES(tagName, "q")
259
&& !MATCHES(tagName, "del") && !MATCHES(tagName, "ins"))
263
else if (MATCHES(curAttr, "profile"))
265
if (!MATCHES(tagName, "head"))
268
// archive attribute on APPLET; warning, it contains a list of URIs.
269
else if (MATCHES(curAttr, "archive"))
271
if (!MATCHES(tagName, "applet"))
275
// Return a code to indicate that there are no more,
276
// to distinguish that case from real errors.
277
return NS_ERROR_NOT_AVAILABLE;
281
nsHTMLURIRefObject::RewriteAllURIs(const nsAString & aOldPat,
282
const nsAString & aNewPat,
286
printf("Can't rewrite URIs yet\n");
288
return NS_ERROR_NOT_IMPLEMENTED;
292
nsHTMLURIRefObject::GetNode(nsIDOMNode** aNode)
295
return NS_ERROR_NOT_INITIALIZED;
297
return NS_ERROR_NULL_POINTER;
298
*aNode = mNode.get();
304
nsHTMLURIRefObject::SetNode(nsIDOMNode *aNode)
307
nsAutoString dummyURI;
308
if (NS_SUCCEEDED(GetNextURI(dummyURI)))
310
mCurAttrIndex = 0; // Reset so we'll get the first node next time
314
// If there weren't any URIs in the attributes,
315
// then don't accept this node.
317
return NS_ERROR_INVALID_ARG;
320
nsresult NS_NewHTMLURIRefObject(nsIURIRefObject** aResult, nsIDOMNode* aNode)
322
nsHTMLURIRefObject* refObject = new nsHTMLURIRefObject();
323
if (!refObject) return NS_ERROR_OUT_OF_MEMORY;
324
nsresult rv = refObject->SetNode(aNode);
330
return refObject->QueryInterface(NS_GET_IID(nsIURIRefObject),