2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
21 * Portions are Copyright (C) 2002 Netscape Communications Corporation.
22 * Other contributors: David Baron <dbaron@fas.harvard.edu>
24 * This library is free software; you can redistribute it and/or
25 * modify it under the terms of the GNU Lesser General Public
26 * License as published by the Free Software Foundation; either
27 * version 2.1 of the License, or (at your option) any later version.
29 * This library is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
32 * Lesser General Public License for more details.
34 * You should have received a copy of the GNU Lesser General Public
35 * License along with this library; if not, write to the Free Software
36 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
38 * Alternatively, the document type parsing portions of this file may be used
39 * under the terms of either the Mozilla Public License Version 1.1, found at
40 * http://www.mozilla.org/MPL/ (the "MPL") or the GNU General Public
41 * License Version 2.0, found at http://www.fsf.org/copyleft/gpl.html
42 * (the "GPL"), in which case the provisions of the MPL or the GPL are
43 * applicable instead of those above. If you wish to allow use of your
44 * version of this file only under the terms of one of those two
45 * licenses (the MPL or the GPL) and not to allow others to use your
46 * version of this file under the LGPL, indicate your decision by
47 * deleting the provisions above and replace them with the notice and
48 * other provisions required by the MPL or the GPL, as the case may be.
49 * If you do not delete the provisions above, a recipient may use your
50 * version of this file under any of the LGPL, the MPL or the GPL.
54 #include "HTMLDocument.h"
56 #include "CSSPropertyNames.h"
57 #include "CookieJar.h"
58 #include "DocumentLoader.h"
59 #include "DocumentType.h"
60 #include "ElementChildIterator.h"
61 #include "ExceptionCode.h"
62 #include "FocusController.h"
64 #include "FrameLoader.h"
65 #include "FrameTree.h"
66 #include "FrameView.h"
67 #include "HashTools.h"
68 #include "HTMLDocumentParser.h"
69 #include "HTMLBodyElement.h"
70 #include "HTMLElementFactory.h"
71 #include "HTMLFrameOwnerElement.h"
72 #include "HTMLFrameSetElement.h"
73 #include "HTMLHtmlElement.h"
74 #include "HTMLNames.h"
75 #include "JSDOMBinding.h"
77 #include "ScriptController.h"
79 #include "StyleResolver.h"
80 #include <wtf/text/CString.h>
84 using namespace HTMLNames;
86 HTMLDocument::HTMLDocument(Frame* frame, const URL& url, DocumentClassFlags documentClasses, unsigned constructionFlags)
87 : Document(frame, url, documentClasses | HTMLDocumentClass, constructionFlags)
92 HTMLDocument::~HTMLDocument()
96 int HTMLDocument::width()
98 updateLayoutIgnorePendingStylesheets();
99 FrameView* frameView = view();
100 return frameView ? frameView->contentsWidth() : 0;
103 int HTMLDocument::height()
105 updateLayoutIgnorePendingStylesheets();
106 FrameView* frameView = view();
107 return frameView ? frameView->contentsHeight() : 0;
110 const AtomicString& HTMLDocument::dir() const
112 auto* documentElement = this->documentElement();
113 if (!is<HTMLHtmlElement>(documentElement))
115 return downcast<HTMLHtmlElement>(*documentElement).dir();
118 void HTMLDocument::setDir(const AtomicString& value)
120 auto* documentElement = this->documentElement();
121 if (is<HTMLHtmlElement>(documentElement))
122 downcast<HTMLHtmlElement>(*documentElement).setDir(value);
125 String HTMLDocument::designMode() const
127 return inDesignMode() ? "on" : "off";
130 void HTMLDocument::setDesignMode(const String& value)
133 if (equalIgnoringCase(value, "on"))
135 else if (equalIgnoringCase(value, "off"))
139 Document::setDesignMode(mode);
142 const AtomicString& HTMLDocument::bgColor() const
144 auto* bodyElement = body();
147 return bodyElement->fastGetAttribute(bgcolorAttr);
150 void HTMLDocument::setBgColor(const String& value)
152 if (auto* bodyElement = body())
153 bodyElement->setAttribute(bgcolorAttr, value);
156 const AtomicString& HTMLDocument::fgColor() const
158 auto* bodyElement = body();
161 return bodyElement->fastGetAttribute(textAttr);
164 void HTMLDocument::setFgColor(const String& value)
166 if (auto* bodyElement = body())
167 bodyElement->setAttribute(textAttr, value);
170 const AtomicString& HTMLDocument::alinkColor() const
172 auto* bodyElement = body();
175 return bodyElement->fastGetAttribute(alinkAttr);
178 void HTMLDocument::setAlinkColor(const String& value)
180 if (auto* bodyElement = body())
181 bodyElement->setAttribute(alinkAttr, value);
184 const AtomicString& HTMLDocument::linkColor() const
186 auto* bodyElement = body();
189 return bodyElement->fastGetAttribute(linkAttr);
192 void HTMLDocument::setLinkColor(const String& value)
194 if (auto* bodyElement = body())
195 bodyElement->setAttribute(linkAttr, value);
198 const AtomicString& HTMLDocument::vlinkColor() const
200 auto* bodyElement = body();
203 return bodyElement->fastGetAttribute(vlinkAttr);
206 void HTMLDocument::setVlinkColor(const String& value)
208 if (auto* bodyElement = body())
209 bodyElement->setAttribute(vlinkAttr, value);
212 void HTMLDocument::captureEvents()
216 void HTMLDocument::releaseEvents()
220 Ref<DocumentParser> HTMLDocument::createParser()
222 return HTMLDocumentParser::create(*this);
225 // --------------------------------------------------------------------------
226 // not part of the DOM
227 // --------------------------------------------------------------------------
229 static void addLocalNameToSet(HashSet<AtomicStringImpl*>* set, const QualifiedName& qName)
231 set->add(qName.localName().impl());
234 static HashSet<AtomicStringImpl*>* createHtmlCaseInsensitiveAttributesSet()
236 // This is the list of attributes in HTML 4.01 with values marked as "[CI]" or case-insensitive
237 // Mozilla treats all other values as case-sensitive, thus so do we.
238 HashSet<AtomicStringImpl*>* attrSet = new HashSet<AtomicStringImpl*>;
240 addLocalNameToSet(attrSet, accept_charsetAttr);
241 addLocalNameToSet(attrSet, acceptAttr);
242 addLocalNameToSet(attrSet, alignAttr);
243 addLocalNameToSet(attrSet, alinkAttr);
244 addLocalNameToSet(attrSet, axisAttr);
245 addLocalNameToSet(attrSet, bgcolorAttr);
246 addLocalNameToSet(attrSet, charsetAttr);
247 addLocalNameToSet(attrSet, checkedAttr);
248 addLocalNameToSet(attrSet, clearAttr);
249 addLocalNameToSet(attrSet, codetypeAttr);
250 addLocalNameToSet(attrSet, colorAttr);
251 addLocalNameToSet(attrSet, compactAttr);
252 addLocalNameToSet(attrSet, declareAttr);
253 addLocalNameToSet(attrSet, deferAttr);
254 addLocalNameToSet(attrSet, dirAttr);
255 addLocalNameToSet(attrSet, disabledAttr);
256 addLocalNameToSet(attrSet, enctypeAttr);
257 addLocalNameToSet(attrSet, faceAttr);
258 addLocalNameToSet(attrSet, frameAttr);
259 addLocalNameToSet(attrSet, hreflangAttr);
260 addLocalNameToSet(attrSet, http_equivAttr);
261 addLocalNameToSet(attrSet, langAttr);
262 addLocalNameToSet(attrSet, languageAttr);
263 addLocalNameToSet(attrSet, linkAttr);
264 addLocalNameToSet(attrSet, mediaAttr);
265 addLocalNameToSet(attrSet, methodAttr);
266 addLocalNameToSet(attrSet, multipleAttr);
267 addLocalNameToSet(attrSet, nohrefAttr);
268 addLocalNameToSet(attrSet, noresizeAttr);
269 addLocalNameToSet(attrSet, noshadeAttr);
270 addLocalNameToSet(attrSet, nowrapAttr);
271 addLocalNameToSet(attrSet, readonlyAttr);
272 addLocalNameToSet(attrSet, relAttr);
273 addLocalNameToSet(attrSet, revAttr);
274 addLocalNameToSet(attrSet, rulesAttr);
275 addLocalNameToSet(attrSet, scopeAttr);
276 addLocalNameToSet(attrSet, scrollingAttr);
277 addLocalNameToSet(attrSet, selectedAttr);
278 addLocalNameToSet(attrSet, shapeAttr);
279 addLocalNameToSet(attrSet, targetAttr);
280 addLocalNameToSet(attrSet, textAttr);
281 addLocalNameToSet(attrSet, typeAttr);
282 addLocalNameToSet(attrSet, valignAttr);
283 addLocalNameToSet(attrSet, valuetypeAttr);
284 addLocalNameToSet(attrSet, vlinkAttr);
289 void HTMLDocument::addDocumentNamedItem(const AtomicStringImpl& name, Element& item)
291 m_documentNamedItem.add(name, item, *this);
292 addImpureProperty(AtomicString(const_cast<AtomicStringImpl*>(&name)));
295 void HTMLDocument::removeDocumentNamedItem(const AtomicStringImpl& name, Element& item)
297 m_documentNamedItem.remove(name, item);
300 void HTMLDocument::addWindowNamedItem(const AtomicStringImpl& name, Element& item)
302 m_windowNamedItem.add(name, item, *this);
305 void HTMLDocument::removeWindowNamedItem(const AtomicStringImpl& name, Element& item)
307 m_windowNamedItem.remove(name, item);
310 bool HTMLDocument::isCaseSensitiveAttribute(const QualifiedName& attributeName)
312 static HashSet<AtomicStringImpl*>* htmlCaseInsensitiveAttributesSet = createHtmlCaseInsensitiveAttributesSet();
313 bool isPossibleHTMLAttr = !attributeName.hasPrefix() && (attributeName.namespaceURI() == nullAtom);
314 return !isPossibleHTMLAttr || !htmlCaseInsensitiveAttributesSet->contains(attributeName.localName().impl());
317 void HTMLDocument::clear()
319 // FIXME: This does nothing, and that seems unlikely to be correct.
320 // We've long had a comment saying that IE doesn't support this.
321 // But I do see it in the documentation for Mozilla.
324 bool HTMLDocument::isFrameSet() const
326 if (!documentElement())
328 return !!childrenOfType<HTMLFrameSetElement>(*documentElement()).first();
331 Ref<Document> HTMLDocument::cloneDocumentWithoutChildren() const
333 return create(nullptr, url());