Bug 1247796. Use keyboardFocusIndicatorColor for ActiveBorder system color keyword...
[gecko.git] / editor / libeditor / nsHTMLURIRefObject.cpp
blob35fbaa4271f692b85c1984bb3c37e08ce427d70d
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
12 action >> FORM
13 background >> BODY
14 codebase >> OBJECT, APPLET
15 classid >> OBJECT
16 data >> OBJECT
17 cite >> BLOCKQUOTE, DEL, INS, Q
18 profile >> HEAD
19 ARCHIVE attribute on APPLET ; warning, it contains a list of URIs.
21 Easier way of organizing the list:
22 a: href
23 area: href
24 base: href
25 body: background
26 blockquote: cite (not normally rewritable)
27 link: href
28 frame: src, longdesc
29 iframe: src, longdesc
30 input: src, usemap
31 form: action
32 img: src, longdesc, usemap
33 script: src
34 applet: codebase, archive <list>
35 object: codebase, data, classid, usemap
36 head: profile
37 del: cite
38 ins: cite
39 q: cite
42 #include "nsHTMLURIRefObject.h"
44 #include "mozilla/mozalloc.h"
45 #include "nsAString.h"
46 #include "nsDebug.h"
47 #include "nsError.h"
48 #include "nsID.h"
49 #include "nsIDOMAttr.h"
50 #include "nsIDOMElement.h"
51 #include "nsIDOMMozNamedAttrMap.h"
52 #include "nsIDOMNode.h"
53 #include "nsISupportsUtils.h"
54 #include "nsString.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)
73 NS_IMETHODIMP
74 nsHTMLURIRefObject::Reset()
76 mCurAttrIndex = 0;
77 return NS_OK;
80 NS_IMETHODIMP
81 nsHTMLURIRefObject::GetNextURI(nsAString & aURI)
83 NS_ENSURE_TRUE(mNode, NS_ERROR_NOT_INITIALIZED);
85 nsAutoString tagName;
86 nsresult rv = mNode->GetNodeName(tagName);
87 NS_ENSURE_SUCCESS(rv, rv);
89 // Loop over attribute list:
90 if (!mAttributes)
92 nsCOMPtr<nsIDOMElement> element (do_QueryInterface(mNode));
93 NS_ENSURE_TRUE(element, NS_ERROR_INVALID_ARG);
95 mCurAttrIndex = 0;
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);
102 mCurAttrIndex = 0;
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);
111 nsString curAttr;
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"))
120 continue;
121 rv = attrNode->GetValue(aURI);
122 NS_ENSURE_SUCCESS(rv, rv);
123 nsString uri (aURI);
124 // href pointing to a named anchor doesn't count
125 if (aURI.First() != char16_t('#'))
126 return NS_OK;
127 aURI.Truncate();
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"))
136 continue;
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"))
143 continue;
145 // longdesc >> FRAME, IFRAME, IMG
146 else if (MATCHES(curAttr, "longdesc"))
148 if (!MATCHES(tagName, "img")
149 && !MATCHES(tagName, "frame") && !MATCHES(tagName, "iframe"))
150 continue;
152 // usemap >> IMG, INPUT, OBJECT
153 else if (MATCHES(curAttr, "usemap"))
155 if (!MATCHES(tagName, "img")
156 && !MATCHES(tagName, "input") && !MATCHES(tagName, "object"))
157 continue;
159 // action >> FORM
160 else if (MATCHES(curAttr, "action"))
162 if (!MATCHES(tagName, "form"))
163 continue;
165 // background >> BODY
166 else if (MATCHES(curAttr, "background"))
168 if (!MATCHES(tagName, "body"))
169 continue;
171 // codebase >> OBJECT, APPLET
172 else if (MATCHES(curAttr, "codebase"))
174 if (!MATCHES(tagName, "meta"))
175 continue;
177 // classid >> OBJECT
178 else if (MATCHES(curAttr, "classid"))
180 if (!MATCHES(tagName, "object"))
181 continue;
183 // data >> OBJECT
184 else if (MATCHES(curAttr, "data"))
186 if (!MATCHES(tagName, "object"))
187 continue;
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"))
194 continue;
196 // profile >> HEAD
197 else if (MATCHES(curAttr, "profile"))
199 if (!MATCHES(tagName, "head"))
200 continue;
202 // archive attribute on APPLET; warning, it contains a list of URIs.
203 else if (MATCHES(curAttr, "archive"))
205 if (!MATCHES(tagName, "applet"))
206 continue;
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;
214 NS_IMETHODIMP
215 nsHTMLURIRefObject::RewriteAllURIs(const nsAString & aOldPat,
216 const nsAString & aNewPat,
217 bool aMakeRel)
219 return NS_ERROR_NOT_IMPLEMENTED;
222 NS_IMETHODIMP
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();
228 NS_ADDREF(*aNode);
229 return NS_OK;
232 NS_IMETHODIMP
233 nsHTMLURIRefObject::SetNode(nsIDOMNode *aNode)
235 mNode = aNode;
236 nsAutoString dummyURI;
237 if (NS_SUCCEEDED(GetNextURI(dummyURI)))
239 mCurAttrIndex = 0; // Reset so we'll get the first node next time
240 return NS_OK;
243 // If there weren't any URIs in the attributes,
244 // then don't accept this node.
245 mNode = 0;
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);
253 if (NS_FAILED(rv)) {
254 *aResult = 0;
255 return rv;
257 refObject.forget(aResult);
258 return NS_OK;