/src/mozilla-central/editor/libeditor/HTMLURIRefObject.cpp
Line | Count | Source (jump to first uncovered line) |
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/. */ |
5 | | |
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. |
20 | | |
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 |
40 | | */ |
41 | | |
42 | | #include "HTMLURIRefObject.h" |
43 | | |
44 | | #include "mozilla/mozalloc.h" |
45 | | #include "mozilla/dom/Attr.h" |
46 | | #include "mozilla/dom/Element.h" |
47 | | #include "nsAString.h" |
48 | | #include "nsDebug.h" |
49 | | #include "nsDOMAttributeMap.h" |
50 | | #include "nsError.h" |
51 | | #include "nsID.h" |
52 | | #include "nsINode.h" |
53 | | #include "nsISupportsUtils.h" |
54 | | #include "nsString.h" |
55 | | #include "nsGkAtoms.h" |
56 | | |
57 | | namespace mozilla { |
58 | | |
59 | | // String classes change too often and I can't keep up. |
60 | | // Set this macro to this week's approved case-insensitive compare routine. |
61 | | #define MATCHES(tagName, str) tagName.EqualsIgnoreCase(str) |
62 | | |
63 | | HTMLURIRefObject::HTMLURIRefObject() |
64 | | : mCurAttrIndex(0) |
65 | | , mAttributeCnt(0) |
66 | | , mAttrsInited(false) |
67 | 0 | { |
68 | 0 | } |
69 | | |
70 | | HTMLURIRefObject::~HTMLURIRefObject() |
71 | 0 | { |
72 | 0 | } |
73 | | |
74 | | //Interfaces for addref and release and queryinterface |
75 | | NS_IMPL_ISUPPORTS(HTMLURIRefObject, nsIURIRefObject) |
76 | | |
77 | | NS_IMETHODIMP |
78 | | HTMLURIRefObject::Reset() |
79 | 0 | { |
80 | 0 | mCurAttrIndex = 0; |
81 | 0 | return NS_OK; |
82 | 0 | } |
83 | | |
84 | | NS_IMETHODIMP |
85 | | HTMLURIRefObject::GetNextURI(nsAString& aURI) |
86 | 0 | { |
87 | 0 | NS_ENSURE_TRUE(mNode, NS_ERROR_NOT_INITIALIZED); |
88 | 0 |
|
89 | 0 | if (NS_WARN_IF(!mNode->IsElement())) { |
90 | 0 | return NS_ERROR_INVALID_ARG; |
91 | 0 | } |
92 | 0 | |
93 | 0 | RefPtr<dom::Element> element = mNode->AsElement(); |
94 | 0 |
|
95 | 0 | // Loop over attribute list: |
96 | 0 | if (!mAttrsInited) { |
97 | 0 | mAttrsInited = true; |
98 | 0 | mAttributeCnt = element->GetAttrCount(); |
99 | 0 | NS_ENSURE_TRUE(mAttributeCnt, NS_ERROR_FAILURE); |
100 | 0 | mCurAttrIndex = 0; |
101 | 0 | } |
102 | 0 |
|
103 | 0 | while (mCurAttrIndex < mAttributeCnt) { |
104 | 0 | BorrowedAttrInfo attrInfo = element->GetAttrInfoAt(mCurAttrIndex++); |
105 | 0 | NS_ENSURE_ARG_POINTER(attrInfo.mName); |
106 | 0 |
|
107 | 0 | // href >> A, AREA, BASE, LINK |
108 | 0 | if (attrInfo.mName->Equals(nsGkAtoms::href)) { |
109 | 0 | if (!element->IsAnyOfHTMLElements(nsGkAtoms::a, |
110 | 0 | nsGkAtoms::area, |
111 | 0 | nsGkAtoms::base, |
112 | 0 | nsGkAtoms::link)) { |
113 | 0 | continue; |
114 | 0 | } |
115 | 0 | |
116 | 0 | attrInfo.mValue->ToString(aURI); |
117 | 0 | // href pointing to a named anchor doesn't count |
118 | 0 | if (StringBeginsWith(aURI, NS_LITERAL_STRING("#"))) { |
119 | 0 | aURI.Truncate(); |
120 | 0 | return NS_ERROR_INVALID_ARG; |
121 | 0 | } |
122 | 0 | |
123 | 0 | return NS_OK; |
124 | 0 | } |
125 | 0 | // src >> FRAME, IFRAME, IMG, INPUT, SCRIPT |
126 | 0 | else if (attrInfo.mName->Equals(nsGkAtoms::src)) { |
127 | 0 | if (!element->IsAnyOfHTMLElements(nsGkAtoms::img, |
128 | 0 | nsGkAtoms::frame, |
129 | 0 | nsGkAtoms::iframe, |
130 | 0 | nsGkAtoms::input, |
131 | 0 | nsGkAtoms::script)) { |
132 | 0 | continue; |
133 | 0 | } |
134 | 0 | attrInfo.mValue->ToString(aURI); |
135 | 0 | return NS_OK; |
136 | 0 | } |
137 | 0 | //<META http-equiv="refresh" content="3,http://www.acme.com/intro.html"> |
138 | 0 | else if (attrInfo.mName->Equals(nsGkAtoms::content)) { |
139 | 0 | if (!element->IsHTMLElement(nsGkAtoms::meta)) { |
140 | 0 | continue; |
141 | 0 | } |
142 | 0 | |
143 | 0 | // XXXbz And if it is? |
144 | 0 | } |
145 | 0 | // longdesc >> FRAME, IFRAME, IMG |
146 | 0 | else if (attrInfo.mName->Equals(nsGkAtoms::longdesc)) { |
147 | 0 | if (!element->IsAnyOfHTMLElements(nsGkAtoms::img, |
148 | 0 | nsGkAtoms::frame, |
149 | 0 | nsGkAtoms::iframe)) { |
150 | 0 | continue; |
151 | 0 | } |
152 | 0 | |
153 | 0 | // XXXbz And if it is? |
154 | 0 | } |
155 | 0 | // usemap >> IMG, INPUT, OBJECT |
156 | 0 | else if (attrInfo.mName->Equals(nsGkAtoms::usemap)) { |
157 | 0 | if (!element->IsAnyOfHTMLElements(nsGkAtoms::img, |
158 | 0 | nsGkAtoms::input, |
159 | 0 | nsGkAtoms::object)) { |
160 | 0 | continue; |
161 | 0 | } |
162 | 0 | } |
163 | 0 | // action >> FORM |
164 | 0 | else if (attrInfo.mName->Equals(nsGkAtoms::action)) { |
165 | 0 | if (!element->IsHTMLElement(nsGkAtoms::form)) { |
166 | 0 | continue; |
167 | 0 | } |
168 | 0 | |
169 | 0 | // XXXbz And if it is? |
170 | 0 | } |
171 | 0 | // background >> BODY |
172 | 0 | else if (attrInfo.mName->Equals(nsGkAtoms::background)) { |
173 | 0 | if (!element->IsHTMLElement(nsGkAtoms::body)) { |
174 | 0 | continue; |
175 | 0 | } |
176 | 0 | |
177 | 0 | // XXXbz And if it is? |
178 | 0 | } |
179 | 0 | // codebase >> OBJECT |
180 | 0 | else if (attrInfo.mName->Equals(nsGkAtoms::codebase)) { |
181 | 0 | if (!element->IsHTMLElement(nsGkAtoms::object)) { |
182 | 0 | continue; |
183 | 0 | } |
184 | 0 | |
185 | 0 | // XXXbz And if it is? |
186 | 0 | } |
187 | 0 | // classid >> OBJECT |
188 | 0 | else if (attrInfo.mName->Equals(nsGkAtoms::classid)) { |
189 | 0 | if (!element->IsHTMLElement(nsGkAtoms::object)) { |
190 | 0 | continue; |
191 | 0 | } |
192 | 0 | |
193 | 0 | // XXXbz And if it is? |
194 | 0 | } |
195 | 0 | // data >> OBJECT |
196 | 0 | else if (attrInfo.mName->Equals(nsGkAtoms::data)) { |
197 | 0 | if (!element->IsHTMLElement(nsGkAtoms::object)) { |
198 | 0 | continue; |
199 | 0 | } |
200 | 0 | |
201 | 0 | // XXXbz And if it is? |
202 | 0 | } |
203 | 0 | // cite >> BLOCKQUOTE, DEL, INS, Q |
204 | 0 | else if (attrInfo.mName->Equals(nsGkAtoms::cite)) { |
205 | 0 | if (!element->IsAnyOfHTMLElements(nsGkAtoms::blockquote, |
206 | 0 | nsGkAtoms::q, |
207 | 0 | nsGkAtoms::del, |
208 | 0 | nsGkAtoms::ins)) { |
209 | 0 | continue; |
210 | 0 | } |
211 | 0 | |
212 | 0 | // XXXbz And if it is? |
213 | 0 | } |
214 | 0 | // profile >> HEAD |
215 | 0 | else if (attrInfo.mName->Equals(nsGkAtoms::profile)) { |
216 | 0 | if (!element->IsHTMLElement(nsGkAtoms::head)) { |
217 | 0 | continue; |
218 | 0 | } |
219 | 0 |
|
220 | 0 | // XXXbz And if it is? |
221 | 0 | } |
222 | 0 | } |
223 | 0 | // Return a code to indicate that there are no more, |
224 | 0 | // to distinguish that case from real errors. |
225 | 0 | return NS_ERROR_NOT_AVAILABLE; |
226 | 0 | } |
227 | | |
228 | | NS_IMETHODIMP |
229 | | HTMLURIRefObject::RewriteAllURIs(const nsAString& aOldPat, |
230 | | const nsAString& aNewPat, |
231 | | bool aMakeRel) |
232 | 0 | { |
233 | 0 | return NS_ERROR_NOT_IMPLEMENTED; |
234 | 0 | } |
235 | | |
236 | | NS_IMETHODIMP |
237 | | HTMLURIRefObject::GetNode(nsINode** aNode) |
238 | 0 | { |
239 | 0 | NS_ENSURE_TRUE(mNode, NS_ERROR_NOT_INITIALIZED); |
240 | 0 | NS_ENSURE_TRUE(aNode, NS_ERROR_NULL_POINTER); |
241 | 0 | *aNode = do_AddRef(mNode).take(); |
242 | 0 | return NS_OK; |
243 | 0 | } |
244 | | |
245 | | NS_IMETHODIMP |
246 | | HTMLURIRefObject::SetNode(nsINode* aNode) |
247 | 0 | { |
248 | 0 | mNode = aNode; |
249 | 0 | nsAutoString dummyURI; |
250 | 0 | if (NS_SUCCEEDED(GetNextURI(dummyURI))) { |
251 | 0 | mCurAttrIndex = 0; // Reset so we'll get the first node next time |
252 | 0 | return NS_OK; |
253 | 0 | } |
254 | 0 | |
255 | 0 | // If there weren't any URIs in the attributes, |
256 | 0 | // then don't accept this node. |
257 | 0 | mNode = nullptr; |
258 | 0 | return NS_ERROR_INVALID_ARG; |
259 | 0 | } |
260 | | |
261 | | } // namespace mozilla |
262 | | |
263 | | nsresult NS_NewHTMLURIRefObject(nsIURIRefObject** aResult, nsINode* aNode) |
264 | 0 | { |
265 | 0 | RefPtr<mozilla::HTMLURIRefObject> refObject = new mozilla::HTMLURIRefObject(); |
266 | 0 | nsresult rv = refObject->SetNode(aNode); |
267 | 0 | if (NS_FAILED(rv)) { |
268 | 0 | *aResult = 0; |
269 | 0 | return rv; |
270 | 0 | } |
271 | 0 | refObject.forget(aResult); |
272 | 0 | return NS_OK; |
273 | 0 | } |