2 * This file is part of the DOM implementation for KDE.
4 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
5 * (C) 1999 Antti Koivisto (koivisto@kde.org)
6 * (C) 2001 Dirk Mueller (mueller@kde.org)
7 * Copyright (C) 2004, 2005, 2006 Apple Computer, Inc.
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public License
20 * along with this library; see the file COPYING.LIB. If not, write to
21 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
28 #include "AXObjectCache.h"
29 #include "CDATASection.h"
30 #include "CSSStyleSheet.h"
31 #include "CSSValueKeywords.h"
33 #include "DOMImplementation.h"
34 #include "DocLoader.h"
35 #include "DocumentFragment.h"
36 #include "DocumentLoader.h"
37 #include "DocumentType.h"
38 #include "EditingText.h"
40 #include "EditorClient.h"
41 #include "EntityReference.h"
43 #include "EventListener.h"
44 #include "EventNames.h"
45 #include "ExceptionCode.h"
47 #include "FrameLoader.h"
48 #include "FrameTree.h"
49 #include "FrameView.h"
50 #include "HTMLBodyElement.h"
51 #include "HTMLDocument.h"
52 #include "HTMLElementFactory.h"
53 #include "HTMLImageLoader.h"
54 #include "HTMLInputElement.h"
55 #include "HTMLLinkElement.h"
56 #include "HTMLMapElement.h"
57 #include "HTMLNameCollection.h"
58 #include "HTMLNames.h"
59 #include "HTMLStyleElement.h"
60 #include "HitTestRequest.h"
61 #include "HitTestResult.h"
63 #include "KeyboardEvent.h"
65 #include "MouseEvent.h"
66 #include "MouseEventWithHitTestResults.h"
67 #include "MutationEvent.h"
68 #include "NameNodeList.h"
69 #include "NodeFilter.h"
70 #include "NodeIterator.h"
71 #include "PlatformKeyboardEvent.h"
72 #include "ProcessingInstruction.h"
73 #include "RegisteredEventListener.h"
74 #include "RegularExpression.h"
75 #include "RenderArena.h"
76 #include "RenderView.h"
77 #include "RenderWidget.h"
78 #include "SegmentedString.h"
79 #include "SelectionController.h"
80 #include "StringHash.h"
81 #include "StyleSheetList.h"
82 #include "SystemTime.h"
83 #include "TextIterator.h"
84 #include "TextResourceDecoder.h"
85 #include "TreeWalker.h"
87 #include "XMLTokenizer.h"
88 #include "csshelper.h"
89 #include "cssstyleselector.h"
90 #include "kjs_binding.h"
91 #include "kjs_proxy.h"
92 #include "xmlhttprequest.h"
95 #include "XPathEvaluator.h"
96 #include "XPathExpression.h"
97 #include "XPathNSResolver.h"
98 #include "XPathResult.h"
102 #include "XSLTProcessor.h"
106 #include "XBLBindingManager.h"
110 #include "SVGDocumentExtensions.h"
111 #include "SVGElementFactory.h"
112 #include "SVGZoomEvent.h"
113 #include "SVGStyleElement.h"
114 #include "KSVGTimeScheduler.h"
121 using namespace EventNames;
122 using namespace HTMLNames;
124 // #define INSTRUMENT_LAYOUT_SCHEDULING 1
126 // This amount of time must have elapsed before we will even consider scheduling a layout without a delay.
127 // FIXME: For faster machines this value can really be lowered to 200. 250 is adequate, but a little high
129 const int cLayoutScheduleThreshold = 250;
131 // Use 1 to represent the document's default form.
132 HTMLFormElement* const defaultForm = (HTMLFormElement*) 1;
134 // Golden ratio - arbitrary start value to avoid mapping all 0's to all 0's
135 static const unsigned PHI = 0x9e3779b9U;
137 // DOM Level 2 says (letters added):
139 // a) Name start characters must have one of the categories Ll, Lu, Lo, Lt, Nl.
140 // b) Name characters other than Name-start characters must have one of the categories Mc, Me, Mn, Lm, or Nd.
141 // c) Characters in the compatibility area (i.e. with character code greater than #xF900 and less than #xFFFE) are not allowed in XML names.
142 // d) Characters which have a font or compatibility decomposition (i.e. those with a "compatibility formatting tag" in field 5 of the database -- marked by field 5 beginning with a "<") are not allowed.
143 // e) The following characters are treated as name-start characters rather than name characters, because the property file classifies them as Alphabetic: [#x02BB-#x02C1], #x0559, #x06E5, #x06E6.
144 // f) Characters #x20DD-#x20E0 are excluded (in accordance with Unicode, section 5.14).
145 // g) Character #x00B7 is classified as an extender, because the property list so identifies it.
146 // h) Character #x0387 is added as a name character, because #x00B7 is its canonical equivalent.
147 // i) Characters ':' and '_' are allowed as name-start characters.
148 // j) Characters '-' and '.' are allowed as name characters.
150 // It also contains complete tables. If we decide it's better, we could include those instead of the following code.
152 static inline bool isValidNameStart(UChar32 c)
155 if ((c >= 0x02BB && c <= 0x02C1) || c == 0x559 || c == 0x6E5 || c == 0x6E6)
159 if (c == ':' || c == '_')
162 // rules (a) and (f) above
163 const uint32_t nameStartMask = U_GC_LL_MASK | U_GC_LU_MASK | U_GC_LO_MASK | U_GC_LT_MASK | U_GC_NL_MASK;
164 if (!(U_GET_GC_MASK(c) & nameStartMask))
168 if (c >= 0xF900 && c < 0xFFFE)
172 UDecompositionType decompType = static_cast<UDecompositionType>(u_getIntPropertyValue(c, UCHAR_DECOMPOSITION_TYPE));
173 if (decompType == U_DT_FONT || decompType == U_DT_COMPAT)
179 static inline bool isValidNamePart(UChar32 c)
181 // rules (a), (e), and (i) above
182 if (isValidNameStart(c))
185 // rules (g) and (h) above
186 if (c == 0x00B7 || c == 0x0387)
190 if (c == '-' || c == '.')
193 // rules (b) and (f) above
194 const uint32_t otherNamePartMask = U_GC_MC_MASK | U_GC_ME_MASK | U_GC_MN_MASK | U_GC_LM_MASK | U_GC_ND_MASK;
195 if (!(U_GET_GC_MASK(c) & otherNamePartMask))
199 if (c >= 0xF900 && c < 0xFFFE)
203 UDecompositionType decompType = static_cast<UDecompositionType>(u_getIntPropertyValue(c, UCHAR_DECOMPOSITION_TYPE));
204 if (decompType == U_DT_FONT || decompType == U_DT_COMPAT)
210 DeprecatedPtrList<Document> * Document::changedDocuments = 0;
212 // FrameView might be 0
213 Document::Document(DOMImplementation* impl, FrameView *v)
215 , m_implementation(impl)
216 , m_domtree_version(0)
217 , m_styleSheets(new StyleSheetList)
219 , m_titleSetExplicitly(false)
220 , m_imageLoadEventTimer(this, &Document::imageLoadEventTimerFired)
222 , m_bindingManager(new XBLBindingManager(this))
225 , m_transformSource(0)
228 , m_passwordFields(0)
230 , m_designMode(inherit)
231 , m_selfOnlyRefCount(0)
236 , m_hasDashboardRegions(false)
237 , m_dashboardRegionsDirty(false)
239 , m_accessKeyMapValid(false)
240 , m_createRenderers(true)
241 , m_inPageCache(false)
243 m_document.resetSkippingRef(this);
252 m_docLoader = new DocLoader(v ? v->frame() : 0, this);
254 visuallyOrdered = false;
255 m_loadingSheet = false;
257 m_docChanged = false;
262 m_textColor = Color::black;
265 m_styleSelectorDirty = false;
266 m_inStyleRecalc = false;
267 m_closeAfterStyleRecalc = false;
268 m_usesDescendantRules = false;
269 m_usesSiblingRules = false;
271 m_styleSelector = new CSSStyleSelector(this, m_usersheet, m_styleSheets.get(), !inCompatMode());
272 m_pendingStylesheets = 0;
273 m_ignorePendingStylesheets = false;
274 m_pendingSheetLayout = NoLayoutWithPendingSheets;
279 resetVisitedLinkColor();
280 resetActiveLinkColor();
282 m_processingLoadEvent = false;
283 m_startTime = currentTime();
284 m_overMinimumLayoutThreshold = false;
288 static int docID = 0;
292 void Document::removedLastRef()
294 if (m_selfOnlyRefCount) {
295 // if removing a child removes the last self-only ref, we don't
296 // want the document to be destructed until after
297 // removeAllChildren returns, so we guard ourselves with an
298 // extra self-only ref
300 DocPtr<Document> guard(this);
302 // we must make sure not to be retaining any of our children through
303 // these extra pointers or we will create a reference cycle
309 m_documentElement = 0;
313 deleteAllValues(m_markers);
322 Document::~Document()
325 assert(!m_inPageCache);
326 assert(m_savedRenderer == 0);
329 delete m_svgExtensions;
332 XMLHttpRequest::detachRequests(this);
333 KJS::ScriptInterpreter::forgetAllDOMNodesForDocument(this);
335 if (m_docChanged && changedDocuments)
336 changedDocuments->remove(this);
338 m_document.resetSkippingRef(0);
339 delete m_styleSelector;
343 delete m_renderArena;
348 xmlFreeDoc((xmlDocPtr)m_transformSource);
352 delete m_bindingManager;
355 deleteAllValues(m_markers);
357 if (m_axObjectCache) {
358 delete m_axObjectCache;
368 deleteAllValues(m_selectedRadioButtons);
371 void Document::resetLinkColor()
373 m_linkColor = Color(0, 0, 238);
376 void Document::resetVisitedLinkColor()
378 m_visitedLinkColor = Color(85, 26, 139);
381 void Document::resetActiveLinkColor()
383 m_activeLinkColor.setNamedColor("red");
386 void Document::setDocType(PassRefPtr<DocumentType> docType)
391 DocumentType *Document::doctype() const
393 return m_docType.get();
396 DOMImplementation* Document::implementation() const
398 return m_implementation.get();
401 void Document::childrenChanged()
403 // invalidate the document element we have cached in case it was replaced
404 m_documentElement = 0;
407 Element* Document::documentElement() const
409 if (!m_documentElement) {
410 Node* n = firstChild();
411 while (n && !n->isElementNode())
412 n = n->nextSibling();
413 m_documentElement = static_cast<Element*>(n);
416 return m_documentElement.get();
419 PassRefPtr<Element> Document::createElement(const String &name, ExceptionCode& ec)
421 return createElementNS(nullAtom, name, ec);
424 PassRefPtr<DocumentFragment> Document::createDocumentFragment()
426 return new DocumentFragment(document());
429 PassRefPtr<Text> Document::createTextNode(const String &data)
431 return new Text(this, data);
434 PassRefPtr<Comment> Document::createComment (const String &data)
436 return new Comment(this, data);
439 PassRefPtr<CDATASection> Document::createCDATASection(const String &data, ExceptionCode& ec)
441 if (isHTMLDocument()) {
442 ec = NOT_SUPPORTED_ERR;
445 return new CDATASection(this, data);
448 PassRefPtr<ProcessingInstruction> Document::createProcessingInstruction(const String &target, const String &data, ExceptionCode& ec)
450 if (!isValidName(target)) {
451 ec = INVALID_CHARACTER_ERR;
454 if (isHTMLDocument()) {
455 ec = NOT_SUPPORTED_ERR;
458 return new ProcessingInstruction(this, target, data);
461 PassRefPtr<EntityReference> Document::createEntityReference(const String &name, ExceptionCode& ec)
463 if (!isValidName(name)) {
464 ec = INVALID_CHARACTER_ERR;
467 if (isHTMLDocument()) {
468 ec = NOT_SUPPORTED_ERR;
471 return new EntityReference(this, name);
474 PassRefPtr<EditingText> Document::createEditingTextNode(const String &text)
476 return new EditingText(this, text);
479 PassRefPtr<CSSStyleDeclaration> Document::createCSSStyleDeclaration()
481 return new CSSMutableStyleDeclaration;
484 PassRefPtr<Node> Document::importNode(Node* importedNode, bool deep, ExceptionCode& ec)
489 ec = NOT_SUPPORTED_ERR;
493 switch (importedNode->nodeType()) {
495 return createTextNode(importedNode->nodeValue());
496 case CDATA_SECTION_NODE:
497 return createCDATASection(importedNode->nodeValue(), ec);
498 case ENTITY_REFERENCE_NODE:
499 return createEntityReference(importedNode->nodeName(), ec);
500 case PROCESSING_INSTRUCTION_NODE:
501 return createProcessingInstruction(importedNode->nodeName(), importedNode->nodeValue(), ec);
503 return createComment(importedNode->nodeValue());
505 Element *oldElement = static_cast<Element *>(importedNode);
506 RefPtr<Element> newElement = createElementNS(oldElement->namespaceURI(), oldElement->tagQName().toString(), ec);
511 NamedAttrMap* attrs = oldElement->attributes(true);
513 unsigned length = attrs->length();
514 for (unsigned i = 0; i < length; i++) {
515 Attribute* attr = attrs->attributeItem(i);
516 newElement->setAttribute(attr->name(), attr->value().impl(), ec);
522 newElement->copyNonAttributeProperties(oldElement);
525 for (Node* oldChild = oldElement->firstChild(); oldChild; oldChild = oldChild->nextSibling()) {
526 RefPtr<Node> newChild = importNode(oldChild, true, ec);
529 newElement->appendChild(newChild.release(), ec);
535 return newElement.release();
540 case DOCUMENT_TYPE_NODE:
541 case DOCUMENT_FRAGMENT_NODE:
543 case XPATH_NAMESPACE_NODE:
547 ec = NOT_SUPPORTED_ERR;
552 PassRefPtr<Node> Document::adoptNode(PassRefPtr<Node> source, ExceptionCode& ec)
555 ec = NOT_SUPPORTED_ERR;
559 switch (source->nodeType()) {
563 case DOCUMENT_TYPE_NODE:
564 case XPATH_NAMESPACE_NODE:
565 ec = NOT_SUPPORTED_ERR;
567 case ATTRIBUTE_NODE: {
568 Attr* attr = static_cast<Attr*>(source.get());
569 if (attr->ownerElement())
570 attr->ownerElement()->removeAttributeNode(attr, ec);
571 attr->m_specified = true;
575 if (source->parentNode())
576 source->parentNode()->removeChild(source.get(), ec);
579 for (Node* node = source.get(); node; node = node->traverseNextNode(source.get())) {
580 KJS::ScriptInterpreter::updateDOMNodeDocument(node, node->document(), this);
581 node->setDocument(this);
587 PassRefPtr<Element> Document::createElementNS(const String &_namespaceURI, const String &qualifiedName, ExceptionCode& ec)
589 // FIXME: We'd like a faster code path that skips this check for calls from inside the engine where the name is known to be valid.
590 String prefix, localName;
591 if (!parseQualifiedName(qualifiedName, prefix, localName)) {
592 ec = INVALID_CHARACTER_ERR;
597 QualifiedName qName = QualifiedName(AtomicString(prefix), AtomicString(localName), AtomicString(_namespaceURI));
599 // FIXME: Use registered namespaces and look up in a hash to find the right factory.
600 if (_namespaceURI == xhtmlNamespaceURI) {
601 e = HTMLElementFactory::createHTMLElement(qName.localName(), this, 0, false);
602 if (e && !prefix.isNull()) {
603 e->setPrefix(qName.prefix(), ec);
609 else if (_namespaceURI == SVGNames::svgNamespaceURI)
610 e = SVGElementFactory::createSVGElement(qName, this, false);
614 e = new Element(qName, document());
619 Element *Document::getElementById(const AtomicString& elementId) const
621 if (elementId.length() == 0)
624 Element *element = m_elementsById.get(elementId.impl());
628 if (m_duplicateIds.contains(elementId.impl())) {
629 for (Node *n = traverseNextNode(); n != 0; n = n->traverseNextNode()) {
630 if (n->isElementNode()) {
631 element = static_cast<Element*>(n);
632 if (element->hasID() && element->getAttribute(idAttr) == elementId) {
633 m_duplicateIds.remove(elementId.impl());
634 m_elementsById.set(elementId.impl(), element);
643 String Document::readyState() const
645 if (Frame* f = frame()) {
646 if (f->loader()->isComplete())
651 // FIXME: What does "interactive" mean?
652 // FIXME: Missing support for "uninitialized".
657 String Document::inputEncoding() const
659 if (TextResourceDecoder* d = decoder())
660 return d->encoding().name();
664 String Document::defaultCharset() const
666 if (Frame* f = frame())
667 return f->settings()->encoding();
671 void Document::setCharset(const String& charset)
675 decoder()->setEncoding(charset, TextResourceDecoder::UserChosenEncoding);
678 Element* Document::elementFromPoint(int x, int y) const
683 HitTestRequest request(true, true);
684 HitTestResult result(IntPoint(x, y));
685 renderer()->layer()->hitTest(request, result);
687 Node* n = result.innerNode();
688 while (n && !n->isElementNode())
691 n = n->shadowAncestorNode();
692 return static_cast<Element*>(n);
695 void Document::addElementById(const AtomicString& elementId, Element* element)
697 if (!m_elementsById.contains(elementId.impl()))
698 m_elementsById.set(elementId.impl(), element);
700 m_duplicateIds.add(elementId.impl());
703 void Document::removeElementById(const AtomicString& elementId, Element* element)
705 if (m_elementsById.get(elementId.impl()) == element)
706 m_elementsById.remove(elementId.impl());
708 m_duplicateIds.remove(elementId.impl());
711 Element* Document::getElementByAccessKey(const String& key) const
715 if (!m_accessKeyMapValid) {
716 for (Node* n = firstChild(); n; n = n->traverseNextNode()) {
717 if (!n->isElementNode())
719 Element* element = static_cast<Element*>(n);
720 const AtomicString& accessKey = element->getAttribute(accesskeyAttr);
721 if (!accessKey.isEmpty())
722 m_elementsByAccessKey.set(accessKey.impl(), element);
724 m_accessKeyMapValid = true;
726 return m_elementsByAccessKey.get(key.impl());
729 void Document::updateTitle()
731 if (Frame* f = frame())
732 f->loader()->setTitle(m_title);
735 void Document::setTitle(const String& title, Node* titleElement)
738 // Title set by JavaScript -- overrides any title elements.
739 m_titleSetExplicitly = true;
741 } else if (titleElement != m_titleElement) {
743 // Only allow the first title element to change the title -- others have no effect.
745 m_titleElement = titleElement;
748 if (m_title == title)
755 void Document::removeTitle(Node* titleElement)
757 if (m_titleElement != titleElement)
760 // FIXME: Ideally we might want this to search for the first remaining title element, and use it.
763 if (!m_title.isEmpty()) {
769 String Document::nodeName() const
774 Node::NodeType Document::nodeType() const
776 return DOCUMENT_NODE;
779 Frame* Document::frame() const
781 return m_view ? m_view->frame() : 0;
784 PassRefPtr<Range> Document::createRange()
786 return new Range(this);
789 PassRefPtr<NodeIterator> Document::createNodeIterator(Node* root, unsigned whatToShow,
790 PassRefPtr<NodeFilter> filter, bool expandEntityReferences, ExceptionCode& ec)
793 ec = NOT_SUPPORTED_ERR;
796 return new NodeIterator(root, whatToShow, filter, expandEntityReferences);
799 PassRefPtr<TreeWalker> Document::createTreeWalker(Node *root, unsigned whatToShow,
800 PassRefPtr<NodeFilter> filter, bool expandEntityReferences, ExceptionCode& ec)
803 ec = NOT_SUPPORTED_ERR;
806 return new TreeWalker(root, whatToShow, filter, expandEntityReferences);
809 void Document::setDocumentChanged(bool b)
813 if (!changedDocuments)
814 changedDocuments = new DeprecatedPtrList<Document>;
815 changedDocuments->append(this);
817 if (m_accessKeyMapValid) {
818 m_accessKeyMapValid = false;
819 m_elementsByAccessKey.clear();
822 if (m_docChanged && changedDocuments)
823 changedDocuments->remove(this);
829 void Document::recalcStyle(StyleChange change)
832 return; // Guard against re-entrancy. -dwh
834 m_inStyleRecalc = true;
836 ASSERT(!renderer() || renderArena());
837 if (!renderer() || !renderArena())
840 if (change == Force) {
841 RenderStyle* oldStyle = renderer()->style();
844 RenderStyle* _style = new (m_renderArena) RenderStyle();
846 _style->setDisplay(BLOCK);
847 _style->setVisuallyOrdered(visuallyOrdered);
848 // ### make the font stuff _really_ work!!!!
850 FontDescription fontDescription;
851 fontDescription.setUsePrinterFont(printing());
853 const Settings *settings = m_view->frame()->settings();
854 if (printing() && !settings->shouldPrintBackgrounds())
855 _style->setForceBackgroundsToWhite(true);
856 const AtomicString& stdfont = settings->stdFontName();
857 if (!stdfont.isEmpty()) {
858 fontDescription.firstFamily().setFamily(stdfont);
859 fontDescription.firstFamily().appendFamily(0);
861 fontDescription.setKeywordSize(CSS_VAL_MEDIUM - CSS_VAL_XX_SMALL + 1);
862 m_styleSelector->setFontSize(fontDescription, m_styleSelector->fontSizeForKeyword(CSS_VAL_MEDIUM, inCompatMode(), false));
865 _style->setFontDescription(fontDescription);
866 _style->font().update();
868 _style->setHtmlHacks(true); // enable html specific rendering tricks
870 StyleChange ch = diff(_style, oldStyle);
871 if (renderer() && ch != NoChange)
872 renderer()->setStyle(_style);
876 _style->deref(m_renderArena);
878 oldStyle->deref(m_renderArena);
881 for (Node* n = fastFirstChild(); n; n = n->nextSibling())
882 if (change >= Inherit || n->hasChangedChild() || n->changed())
883 n->recalcStyle(change);
885 if (changed() && m_view)
890 setHasChangedChild(false);
891 setDocumentChanged(false);
893 m_inStyleRecalc = false;
895 // If we wanted to emit the implicitClose() during recalcStyle, do so now that we're finished.
896 if (m_closeAfterStyleRecalc) {
897 m_closeAfterStyleRecalc = false;
902 void Document::updateRendering()
904 if (hasChangedChild())
905 recalcStyle(NoChange);
908 void Document::updateDocumentsRendering()
910 if (!changedDocuments)
913 while (Document* doc = changedDocuments->take()) {
914 doc->m_docChanged = false;
915 doc->updateRendering();
919 void Document::updateLayout()
921 // FIXME: Dave Hyatt's pretty sure we can remove this because layout calls recalcStyle as needed.
924 // Only do a layout if changes have occurred that make it necessary.
925 if (m_view && renderer() && (m_view->layoutPending() || renderer()->needsLayout()))
929 // FIXME: This is a bad idea and needs to be removed eventually.
930 // Other browsers load stylesheets before they continue parsing the web page.
931 // Since we don't, we can run JavaScript code that needs answers before the
932 // stylesheets are loaded. Doing a layout ignoring the pending stylesheets
933 // lets us get reasonable answers. The long term solution to this problem is
934 // to instead suspend JavaScript execution.
935 void Document::updateLayoutIgnorePendingStylesheets()
937 bool oldIgnore = m_ignorePendingStylesheets;
939 if (!haveStylesheetsLoaded()) {
940 m_ignorePendingStylesheets = true;
941 // FIXME: We are willing to attempt to suppress painting with outdated style info only once. Our assumption is that it would be
942 // dangerous to try to stop it a second time, after page content has already been loaded and displayed
943 // with accurate style information. (Our suppression involves blanking the whole page at the
944 // moment. If it were more refined, we might be able to do something better.)
945 // It's worth noting though that this entire method is a hack, since what we really want to do is
946 // suspend JS instead of doing a layout with inaccurate information.
947 if (body() && !body()->renderer() && m_pendingSheetLayout == NoLayoutWithPendingSheets)
948 m_pendingSheetLayout = DidLayoutWithPendingSheets;
949 updateStyleSelector();
954 m_ignorePendingStylesheets = oldIgnore;
957 void Document::attach()
960 assert(!m_inPageCache);
963 m_renderArena = new RenderArena();
965 // Create the rendering tree
966 setRenderer(new (m_renderArena) RenderView(this, m_view));
970 RenderObject* render = renderer();
973 ContainerNode::attach();
978 void Document::detach()
980 RenderObject* render = renderer();
982 // indicate destruction mode, i.e. attached() but renderer == 0
987 axObjectCache()->remove(render);
991 // Empty out these lists as a performance optimization, since detaching
992 // all the individual render objects will cause all the RenderImage
993 // objects to remove themselves from the lists.
994 m_imageLoadEventDispatchSoonList.clear();
995 m_imageLoadEventDispatchingList.clear();
1001 ContainerNode::detach();
1008 if (m_renderArena) {
1009 delete m_renderArena;
1014 void Document::removeAllEventListenersFromAllNodes()
1016 m_windowEventListeners.clear();
1017 removeAllDisconnectedNodeEventListeners();
1018 for (Node *n = this; n; n = n->traverseNextNode()) {
1019 if (!n->isEventTargetNode())
1021 EventTargetNodeCast(n)->removeAllEventListeners();
1025 void Document::registerDisconnectedNodeWithEventListeners(Node* node)
1027 m_disconnectedNodesWithEventListeners.add(node);
1030 void Document::unregisterDisconnectedNodeWithEventListeners(Node* node)
1032 m_disconnectedNodesWithEventListeners.remove(node);
1035 void Document::removeAllDisconnectedNodeEventListeners()
1037 HashSet<Node*>::iterator end = m_disconnectedNodesWithEventListeners.end();
1038 for (HashSet<Node*>::iterator i = m_disconnectedNodesWithEventListeners.begin(); i != end; ++i)
1039 EventTargetNodeCast(*i)->removeAllEventListeners();
1040 m_disconnectedNodesWithEventListeners.clear();
1043 AXObjectCache* Document::axObjectCache() const
1045 // The only document that actually has a AXObjectCache is the top-level
1046 // document. This is because we need to be able to get from any WebCoreAXObject
1047 // to any other WebCoreAXObject on the same page. Using a single cache allows
1048 // lookups across nested webareas (i.e. multiple documents).
1050 if (m_axObjectCache) {
1051 // return already known top-level cache
1052 if (!ownerElement())
1053 return m_axObjectCache;
1055 // In some pages with frames, the cache is created before the sub-webarea is
1056 // inserted into the tree. Here, we catch that case and just toss the old
1057 // cache and start over.
1058 delete m_axObjectCache;
1059 m_axObjectCache = 0;
1062 // ask the top-level document for its cache
1063 Document* doc = topDocument();
1065 return doc->axObjectCache();
1067 // this is the top-level document, so install a new cache
1068 m_axObjectCache = new AXObjectCache;
1069 return m_axObjectCache;
1072 void Document::setVisuallyOrdered()
1074 visuallyOrdered = true;
1076 renderer()->style()->setVisuallyOrdered(true);
1079 void Document::updateSelection()
1084 RenderView *canvas = static_cast<RenderView*>(renderer());
1085 Selection selection = frame()->selectionController()->selection();
1087 if (!selection.isRange())
1088 canvas->clearSelection();
1090 // Use the rightmost candidate for the start of the selection, and the leftmost candidate for the end of the selection.
1091 // Example: foo <a>bar</a>. Imagine that a line wrap occurs after 'foo', and that 'bar' is selected. If we pass [foo, 3]
1092 // as the start of the selection, the selection painting code will think that content on the line containing 'foo' is selected
1093 // and will fill the gap before 'bar'.
1094 Position startPos = selection.visibleStart().deepEquivalent();
1095 if (startPos.downstream().inRenderedContent())
1096 startPos = startPos.downstream();
1097 Position endPos = selection.visibleEnd().deepEquivalent();
1098 if (endPos.upstream().inRenderedContent())
1099 endPos = endPos.upstream();
1101 // We can get into a state where the selection endpoints map to the same VisiblePosition when a selection is deleted
1102 // because we don't yet notify the SelectionController of text removal.
1103 if (startPos.isNotNull() && endPos.isNotNull() && selection.visibleStart() != selection.visibleEnd()) {
1104 RenderObject *startRenderer = startPos.node()->renderer();
1105 RenderObject *endRenderer = endPos.node()->renderer();
1106 static_cast<RenderView*>(renderer())->setSelection(startRenderer, startPos.offset(), endRenderer, endPos.offset());
1111 // FIXME: We shouldn't post this AX notification here since updateSelection() is called far too often: every time Safari gains
1112 // or loses focus, and once for every low level change to the selection during an editing operation.
1113 // FIXME: We no longer blow away the selection before starting an editing operation, so the isNotNull checks below are no
1114 // longer a correct way to check for user-level selection changes.
1115 if (AXObjectCache::accessibilityEnabled() && selection.start().isNotNull() && selection.end().isNotNull())
1116 axObjectCache()->postNotification(selection.start().node()->renderer(), "AXSelectedTextChanged");
1120 Tokenizer *Document::createTokenizer()
1122 return newXMLTokenizer(this, m_view);
1125 void Document::open()
1127 // This is work that we should probably do in clear(), but we can't have it
1128 // happen when implicitOpen() is called unless we reorganize Frame code.
1129 if (Document *parent = parentDocument()) {
1130 setURL(parent->baseURL());
1131 setBaseURL(parent->baseURL());
1134 setURL(DeprecatedString());
1137 if ((frame() && frame()->loader()->isLoadingMainResource()) || (tokenizer() && tokenizer()->executingScript()))
1143 frame()->loader()->didExplicitOpen();
1146 void Document::cancelParsing()
1149 // We have to clear the tokenizer to avoid possibly triggering
1150 // the onload handler when closing as a side effect of a cancel-style
1151 // change, such as opening a new document or closing the window while
1159 void Document::implicitOpen()
1164 m_tokenizer = createTokenizer();
1168 HTMLElement* Document::body()
1170 Node *de = documentElement();
1174 // try to prefer a FRAMESET element over BODY
1176 for (Node* i = de->firstChild(); i; i = i->nextSibling()) {
1177 if (i->hasTagName(framesetTag))
1178 return static_cast<HTMLElement*>(i);
1180 if (i->hasTagName(bodyTag))
1183 return static_cast<HTMLElement *>(body);
1186 void Document::close()
1189 frame()->loader()->endIfNotLoading();
1193 void Document::implicitClose()
1195 // If we're in the middle of recalcStyle, we need to defer the close until the style information is accurate and all elements are re-attached.
1196 if (m_inStyleRecalc) {
1197 m_closeAfterStyleRecalc = true;
1201 bool wasLocationChangePending = frame() && frame()->loader()->isScheduledLocationChangePending();
1202 bool doload = !parsing() && m_tokenizer && !m_processingLoadEvent && !wasLocationChangePending;
1207 m_processingLoadEvent = true;
1209 // We have to clear the tokenizer, in case someone document.write()s from the
1210 // onLoad event handler, as in Radar 3206524.
1214 // Create a body element if we don't already have one.
1215 // In the case of Radar 3758785, the window.onload was set in some javascript, but never fired because there was no body.
1216 // This behavior now matches Firefox and IE.
1217 HTMLElement *body = this->body();
1218 if (!body && isHTMLDocument()) {
1219 Node *de = documentElement();
1221 body = new HTMLBodyElement(this);
1222 ExceptionCode ec = 0;
1223 de->appendChild(body, ec);
1229 dispatchImageLoadEventsNow();
1230 this->dispatchWindowEvent(loadEvent, false, false);
1231 if (Frame* f = frame())
1232 f->loader()->handledOnloadEvents();
1233 #ifdef INSTRUMENT_LAYOUT_SCHEDULING
1234 if (!ownerElement())
1235 printf("onload fired at %d\n", elapsedTime());
1238 m_processingLoadEvent = false;
1240 // Make sure both the initial layout and reflow happen after the onload
1241 // fires. This will improve onload scores, and other browsers do it.
1242 // If they wanna cheat, we can too. -dwh
1244 if (frame() && frame()->loader()->isScheduledLocationChangePending() && elapsedTime() < cLayoutScheduleThreshold) {
1245 // Just bail out. Before or during the onload we were shifted to another page.
1246 // The old i-Bench suite does this. When this happens don't bother painting or laying out.
1247 view()->unscheduleRelayout();
1252 frame()->loader()->checkEmitLoadEvent();
1254 // Now do our painting/layout, but only if we aren't in a subframe or if we're in a subframe
1255 // that has been sized already. Otherwise, our view size would be incorrect, so doing any
1256 // layout/painting now would be pointless.
1257 if (!ownerElement() || (ownerElement()->renderer() && !ownerElement()->renderer()->needsLayout())) {
1260 // Always do a layout after loading if needed.
1261 if (view() && renderer() && (!renderer()->firstChild() || renderer()->needsLayout()))
1266 if (renderer() && AXObjectCache::accessibilityEnabled())
1267 axObjectCache()->postNotificationToElement(renderer(), "AXLoadComplete");
1271 // FIXME: Officially, time 0 is when the outermost <svg> receives its
1272 // SVGLoad event, but we don't implement those yet. This is close enough
1273 // for now. In some cases we should have fired earlier.
1274 if (svgExtensions())
1275 accessSVGExtensions()->startAnimations();
1279 void Document::setParsing(bool b)
1282 if (!m_bParsing && view())
1283 view()->scheduleRelayout();
1285 #ifdef INSTRUMENT_LAYOUT_SCHEDULING
1286 if (!ownerElement() && !m_bParsing)
1287 printf("Parsing finished at %d\n", elapsedTime());
1291 bool Document::shouldScheduleLayout()
1293 // We can update layout if:
1294 // (a) we actually need a layout
1295 // (b) our stylesheets are all loaded
1296 // (c) we have a <body>
1297 return (renderer() && renderer()->needsLayout() && haveStylesheetsLoaded() &&
1298 documentElement() && documentElement()->renderer() &&
1299 (!documentElement()->hasTagName(htmlTag) || body()));
1302 int Document::minimumLayoutDelay()
1304 if (m_overMinimumLayoutThreshold)
1307 int elapsed = elapsedTime();
1308 m_overMinimumLayoutThreshold = elapsed > cLayoutScheduleThreshold;
1310 // We'll want to schedule the timer to fire at the minimum layout threshold.
1311 return max(0, cLayoutScheduleThreshold - elapsed);
1314 int Document::elapsedTime() const
1316 return static_cast<int>((currentTime() - m_startTime) * 1000);
1319 void Document::write(const DeprecatedString& text)
1321 write(String(text));
1324 void Document::write(const String& text)
1326 #ifdef INSTRUMENT_LAYOUT_SCHEDULING
1327 if (!ownerElement())
1328 printf("Beginning a document.write at %d\n", elapsedTime());
1333 assert(m_tokenizer);
1336 write(DeprecatedString("<html>"));
1338 m_tokenizer->write(text, false);
1340 #ifdef INSTRUMENT_LAYOUT_SCHEDULING
1341 if (!ownerElement())
1342 printf("Ending a document.write at %d\n", elapsedTime());
1346 void Document::writeln(const String &text)
1349 write(String("\n"));
1352 void Document::finishParsing()
1354 #ifdef INSTRUMENT_LAYOUT_SCHEDULING
1355 if (!ownerElement())
1356 printf("Received all data at %d\n", elapsedTime());
1359 // Let the tokenizer go through as much data as it can. There will be three possible outcomes after
1360 // finish() is called:
1361 // (1) All remaining data is parsed, document isn't loaded yet
1362 // (2) All remaining data is parsed, document is loaded, tokenizer gets deleted
1363 // (3) Data is still remaining to be parsed.
1365 m_tokenizer->finish();
1368 void Document::clear()
1375 m_windowEventListeners.clear();
1378 void Document::setURL(const DeprecatedString& url)
1381 if (m_styleSelector)
1382 m_styleSelector->setEncodedURL(m_url);
1385 void Document::setCSSStyleSheet(const String &url, const String& charset, const String &sheet)
1387 m_sheet = new CSSStyleSheet(this, url, charset);
1388 m_sheet->parseString(sheet);
1389 m_loadingSheet = false;
1391 updateStyleSelector();
1394 void Document::setUserStyleSheet(const String& sheet)
1396 if (m_usersheet != sheet) {
1397 m_usersheet = sheet;
1398 updateStyleSelector();
1402 CSSStyleSheet* Document::elementSheet()
1405 m_elemSheet = new CSSStyleSheet(this, baseURL());
1406 return m_elemSheet.get();
1409 void Document::determineParseMode(const String&)
1411 // For XML documents use strict parse mode.
1412 // HTML overrides this method to determine the parse mode.
1417 Node* Document::nextFocusNode(Node* fromNode, KeyboardEvent* event)
1419 unsigned short fromTabIndex;
1422 // No starting node supplied; begin with the top of the document
1424 int lowestTabIndex = 65535;
1425 for (Node* n = this; n; n = n->traverseNextNode())
1426 if (n->isKeyboardFocusable(event) && n->tabIndex() > 0 && n->tabIndex() < lowestTabIndex)
1427 lowestTabIndex = n->tabIndex();
1429 if (lowestTabIndex == 65535)
1432 // Go to the first node in the document that has the desired tab index
1433 for (Node* n = this; n; n = n->traverseNextNode())
1434 if (n->isKeyboardFocusable(event) && n->tabIndex() == lowestTabIndex)
1440 fromTabIndex = fromNode->tabIndex();
1442 if (fromTabIndex == 0) {
1443 // Just need to find the next selectable node after fromNode (in document order) that has a tab index of 0
1445 for (n = fromNode->traverseNextNode(); n; n = n->traverseNextNode())
1446 if (n->isKeyboardFocusable(event) && n->tabIndex() == 0)
1452 // Find the lowest tab index out of all the nodes except fromNode, that is greater than or equal to fromNode's
1453 // tab index. For nodes with the same tab index as fromNode, we are only interested in those that come after
1454 // fromNode in document order.
1455 // If we don't find a suitable tab index, the next focus node will be one with a tab index of 0.
1456 unsigned short lowestSuitableTabIndex = 65535;
1458 bool reachedFromNode = false;
1459 for (Node* n = this; n; n = n->traverseNextNode()) {
1461 reachedFromNode = true;
1462 else if (n->isKeyboardFocusable(event)
1463 && ((reachedFromNode && n->tabIndex() >= fromTabIndex)
1464 || (!reachedFromNode && n->tabIndex() > fromTabIndex)))
1465 // We found a selectable node with a tab index at least as high as fromNode's. Keep searching though,
1466 // as there may be another node which has a lower tab index but is still suitable for use.
1467 lowestSuitableTabIndex = min(lowestSuitableTabIndex, n->tabIndex());
1470 if (lowestSuitableTabIndex == 65535) {
1471 // No next node with a tab index -> just take first node with tab index of 0
1473 for (n = this; n; n = n->traverseNextNode())
1474 if (n->isKeyboardFocusable(event) && n->tabIndex() == 0)
1480 // Search forwards from fromNode
1481 for (Node* n = fromNode->traverseNextNode(); n; n = n->traverseNextNode())
1482 if (n->isKeyboardFocusable(event) && n->tabIndex() == lowestSuitableTabIndex)
1485 // The next node isn't after fromNode, start from the beginning of the document
1486 for (Node* n = this; n != fromNode; n = n->traverseNextNode())
1487 if (n->isKeyboardFocusable(event) && n->tabIndex() == lowestSuitableTabIndex)
1490 ASSERT_NOT_REACHED();
1494 Node* Document::previousFocusNode(Node* fromNode, KeyboardEvent* event)
1497 for (lastNode = this; lastNode->lastChild(); lastNode = lastNode->lastChild());
1500 // No starting node supplied; begin with the very last node in the document
1502 int highestTabIndex = 0;
1503 for (Node* n = lastNode; n; n = n->traversePreviousNode()) {
1504 if (n->isKeyboardFocusable(event)) {
1505 if (n->tabIndex() == 0)
1507 else if (n->tabIndex() > highestTabIndex)
1508 highestTabIndex = n->tabIndex();
1512 // No node with a tab index of 0; just go to the last node with the highest tab index
1513 for (Node* n = lastNode; n; n = n->traversePreviousNode())
1514 if (n->isKeyboardFocusable(event) && n->tabIndex() == highestTabIndex)
1520 unsigned short fromTabIndex = fromNode->tabIndex();
1522 if (fromTabIndex == 0) {
1523 // Find the previous selectable node before fromNode (in document order) that has a tab index of 0
1525 for (n = fromNode->traversePreviousNode(); n; n = n->traversePreviousNode())
1526 if (n->isKeyboardFocusable(event) && n->tabIndex() == 0)
1532 // No previous nodes with a 0 tab index, go to the last node in the document that has the highest tab index
1533 int highestTabIndex = 0;
1534 for (n = this; n; n = n->traverseNextNode())
1535 if (n->isKeyboardFocusable(event) && n->tabIndex() > highestTabIndex)
1536 highestTabIndex = n->tabIndex();
1538 if (!highestTabIndex)
1541 for (n = lastNode; n; n = n->traversePreviousNode())
1542 if (n->isKeyboardFocusable(event) && n->tabIndex() == highestTabIndex)
1545 ASSERT_NOT_REACHED();
1549 // Find the lowest tab index out of all the nodes except fromNode, that is less than or equal to fromNode's
1550 // tab index. For nodes with the same tab index as fromNode, we are only interested in those before
1552 // If we don't find a suitable tab index, then there will be no previous focus node.
1553 unsigned short highestSuitableTabIndex = 0;
1555 bool reachedFromNode = false;
1556 for (Node* n = this; n; n = n->traverseNextNode()) {
1557 if (n->isKeyboardFocusable(event) &&
1558 ((!reachedFromNode && (n->tabIndex() <= fromTabIndex)) ||
1559 (reachedFromNode && (n->tabIndex() < fromTabIndex))) &&
1560 (n->tabIndex() > highestSuitableTabIndex) &&
1563 // We found a selectable node with a tab index no higher than fromNode's. Keep searching though, as
1564 // there may be another node which has a higher tab index but is still suitable for use.
1565 highestSuitableTabIndex = n->tabIndex();
1569 reachedFromNode = true;
1572 if (highestSuitableTabIndex == 0)
1573 // No previous node with a tab index. Since the order specified by HTML is nodes with tab index > 0
1574 // first, this means that there is no previous node.
1577 // Search backwards from fromNode
1578 for (Node* n = fromNode->traversePreviousNode(); n; n = n->traversePreviousNode())
1579 if (n->isKeyboardFocusable(event) && (n->tabIndex() == highestSuitableTabIndex))
1582 // The previous node isn't before fromNode, start from the end of the document
1583 for (Node* n = lastNode; n != fromNode; n = n->traversePreviousNode())
1584 if (n->isKeyboardFocusable(event) && (n->tabIndex() == highestSuitableTabIndex))
1587 ASSERT_NOT_REACHED();
1591 int Document::nodeAbsIndex(Node *node)
1593 assert(node->document() == this);
1596 for (Node *n = node; n && n != this; n = n->traversePreviousNode())
1601 Node *Document::nodeWithAbsIndex(int absIndex)
1604 for (int i = 0; n && (i < absIndex); i++) {
1605 n = n->traverseNextNode();
1610 void Document::processHttpEquiv(const String &equiv, const String &content)
1612 assert(!equiv.isNull() && !content.isNull());
1614 Frame *frame = this->frame();
1616 if (equalIgnoringCase(equiv, "default-style")) {
1617 // The preferred style set has been overridden as per section
1618 // 14.3.2 of the HTML4.0 specification. We need to update the
1619 // sheet used variable and then update our style selector.
1620 // For more info, see the test at:
1621 // http://www.hixie.ch/tests/evil/css/import/main/preferred.html
1623 m_selectedStylesheetSet = content;
1624 m_preferredStylesheetSet = content;
1625 updateStyleSelector();
1626 } else if (equalIgnoringCase(equiv, "refresh")) {
1627 // get delay and url
1628 DeprecatedString str = content.stripWhiteSpace().deprecatedString();
1629 int pos = str.find(RegularExpression("[;,]"));
1631 pos = str.find(RegularExpression("[ \t]"));
1633 if (pos == -1) // There can be no url (David)
1637 delay = str.toInt(&ok);
1638 // We want a new history item if the refresh timeout > 1 second
1640 frame->loader()->scheduleRedirection(delay, frame->loader()->url().url(), delay <= 1);
1644 delay = str.left(pos).stripWhiteSpace().toDouble(&ok);
1647 while(pos < (int)str.length() && str[pos].isSpace()) pos++;
1649 if (str.find("url", 0, false) == 0)
1651 str = str.stripWhiteSpace();
1652 if (str.length() && str[0] == '=')
1653 str = str.mid(1).stripWhiteSpace();
1654 str = parseURL(String(str)).deprecatedString();
1656 // We want a new history item if the refresh timeout > 1 second
1657 frame->loader()->scheduleRedirection(delay, completeURL(str), delay <= 1);
1659 } else if (equalIgnoringCase(equiv, "expires")) {
1660 String str = content.stripWhiteSpace();
1661 time_t expire_date = str.toInt();
1662 m_docLoader->setExpireDate(expire_date);
1663 } else if (equalIgnoringCase(equiv, "set-cookie")) {
1664 // FIXME: make setCookie work on XML documents too; e.g. in case of <html:meta .....>
1665 if (isHTMLDocument())
1666 static_cast<HTMLDocument*>(this)->setCookie(content);
1670 MouseEventWithHitTestResults Document::prepareMouseEvent(bool readonly, bool active, bool mouseMove,
1671 const IntPoint& point, const PlatformMouseEvent& event)
1674 return MouseEventWithHitTestResults(event, 0, 0, false);
1676 assert(renderer()->isRenderView());
1677 HitTestRequest request(readonly, active, mouseMove);
1678 HitTestResult result(point);
1679 renderer()->layer()->hitTest(request, result);
1684 bool isOverLink = result.URLElement() && !result.URLElement()->getAttribute(hrefAttr).isNull();
1685 return MouseEventWithHitTestResults(event, result.innerNode(), result.scrollbar(), isOverLink);
1688 // DOM Section 1.1.1
1689 bool Document::childTypeAllowed(NodeType type)
1692 case ATTRIBUTE_NODE:
1693 case CDATA_SECTION_NODE:
1694 case DOCUMENT_FRAGMENT_NODE:
1697 case ENTITY_REFERENCE_NODE:
1700 case XPATH_NAMESPACE_NODE:
1703 case PROCESSING_INSTRUCTION_NODE:
1705 case DOCUMENT_TYPE_NODE:
1707 // Documents may contain no more than one of each of these.
1708 // (One Element and one DocumentType.)
1709 for (Node* c = firstChild(); c; c = c->nextSibling())
1710 if (c->nodeType() == type)
1717 PassRefPtr<Node> Document::cloneNode(bool /*deep*/)
1719 // Spec says cloning Document nodes is "implementation dependent"
1720 // so we do not support it...
1724 StyleSheetList* Document::styleSheets()
1726 return m_styleSheets.get();
1729 String Document::preferredStylesheetSet() const
1731 return m_preferredStylesheetSet;
1734 String Document::selectedStylesheetSet() const
1736 return m_selectedStylesheetSet;
1739 void Document::setSelectedStylesheetSet(const String& aString)
1741 m_selectedStylesheetSet = aString;
1742 updateStyleSelector();
1744 renderer()->repaint();
1747 // This method is called whenever a top-level stylesheet has finished loading.
1748 void Document::stylesheetLoaded()
1750 // Make sure we knew this sheet was pending, and that our count isn't out of sync.
1751 assert(m_pendingStylesheets > 0);
1753 m_pendingStylesheets--;
1755 #ifdef INSTRUMENT_LAYOUT_SCHEDULING
1756 if (!ownerElement())
1757 printf("Stylesheet loaded at time %d. %d stylesheets still remain.\n", elapsedTime(), m_pendingStylesheets);
1760 updateStyleSelector();
1763 void Document::updateStyleSelector()
1765 // Don't bother updating, since we haven't loaded all our style info yet.
1766 if (!haveStylesheetsLoaded())
1769 if (didLayoutWithPendingStylesheets() && m_pendingStylesheets <= 0) {
1770 m_pendingSheetLayout = IgnoreLayoutWithPendingSheets;
1772 renderer()->repaint();
1775 #ifdef INSTRUMENT_LAYOUT_SCHEDULING
1776 if (!ownerElement())
1777 printf("Beginning update of style selector at time %d.\n", elapsedTime());
1780 recalcStyleSelector();
1783 #ifdef INSTRUMENT_LAYOUT_SCHEDULING
1784 if (!ownerElement())
1785 printf("Finished update of style selector at time %d\n", elapsedTime());
1789 renderer()->setNeedsLayoutAndMinMaxRecalc();
1791 view()->scheduleRelayout();
1795 void Document::recalcStyleSelector()
1797 if (!renderer() || !attached())
1800 DeprecatedPtrList<StyleSheet> oldStyleSheets = m_styleSheets->styleSheets;
1801 m_styleSheets->styleSheets.clear();
1803 for (n = this; n; n = n->traverseNextNode()) {
1804 StyleSheet *sheet = 0;
1806 if (n->nodeType() == PROCESSING_INSTRUCTION_NODE)
1808 // Processing instruction (XML documents only)
1809 ProcessingInstruction* pi = static_cast<ProcessingInstruction*>(n);
1810 sheet = pi->sheet();
1812 // Don't apply XSL transforms to already transformed documents -- <rdar://problem/4132806>
1813 if (pi->isXSL() && !transformSourceDocument()) {
1814 // Don't apply XSL transforms until loading is finished.
1816 applyXSLTransform(pi);
1820 if (!sheet && !pi->localHref().isEmpty())
1822 // Processing instruction with reference to an element in this document - e.g.
1823 // <?xml-stylesheet href="#mystyle">, with the element
1824 // <foo id="mystyle">heading { color: red; }</foo> at some location in
1826 Element* elem = getElementById(pi->localHref().impl());
1828 String sheetText("");
1830 for (c = elem->firstChild(); c; c = c->nextSibling()) {
1831 if (c->nodeType() == TEXT_NODE || c->nodeType() == CDATA_SECTION_NODE)
1832 sheetText += c->nodeValue();
1835 CSSStyleSheet *cssSheet = new CSSStyleSheet(this);
1836 cssSheet->parseString(sheetText);
1837 pi->setCSSStyleSheet(cssSheet);
1843 else if (n->isHTMLElement() && (n->hasTagName(linkTag) || n->hasTagName(styleTag))) {
1844 HTMLElement *e = static_cast<HTMLElement *>(n);
1845 DeprecatedString title = e->getAttribute(titleAttr).deprecatedString();
1846 bool enabledViaScript = false;
1847 if (e->hasLocalName(linkTag)) {
1849 HTMLLinkElement* l = static_cast<HTMLLinkElement*>(n);
1850 if (l->isLoading() || l->isDisabled())
1853 title = DeprecatedString::null;
1854 enabledViaScript = l->isEnabledViaScript();
1857 // Get the current preferred styleset. This is the
1858 // set of sheets that will be enabled.
1859 if (e->hasLocalName(linkTag))
1860 sheet = static_cast<HTMLLinkElement*>(n)->sheet();
1863 sheet = static_cast<HTMLStyleElement*>(n)->sheet();
1865 // Check to see if this sheet belongs to a styleset
1866 // (thus making it PREFERRED or ALTERNATE rather than
1868 if (!enabledViaScript && !title.isEmpty()) {
1869 // Yes, we have a title.
1870 if (m_preferredStylesheetSet.isEmpty()) {
1871 // No preferred set has been established. If
1872 // we are NOT an alternate sheet, then establish
1873 // us as the preferred set. Otherwise, just ignore
1875 DeprecatedString rel = e->getAttribute(relAttr).deprecatedString();
1876 if (e->hasLocalName(styleTag) || !rel.contains("alternate"))
1877 m_preferredStylesheetSet = m_selectedStylesheetSet = title;
1880 if (title != m_preferredStylesheetSet)
1885 else if (n->isSVGElement() && n->hasTagName(SVGNames::styleTag)) {
1886 DeprecatedString title;
1888 SVGStyleElement *s = static_cast<SVGStyleElement*>(n);
1889 if (!s->isLoading()) {
1892 title = s->getAttribute(SVGNames::titleAttr).deprecatedString();
1895 if (!title.isEmpty() && m_preferredStylesheetSet.isEmpty())
1896 m_preferredStylesheetSet = m_selectedStylesheetSet = title;
1898 if (!title.isEmpty()) {
1899 if (title != m_preferredStylesheetSet)
1900 sheet = 0; // don't use it
1902 title = title.replace('&', "&&");
1909 m_styleSheets->styleSheets.append(sheet);
1912 // For HTML documents, stylesheets are not allowed within/after the <BODY> tag. So we
1913 // can stop searching here.
1914 if (isHTMLDocument() && n->hasTagName(bodyTag))
1918 // De-reference all the stylesheets in the old list
1919 DeprecatedPtrListIterator<StyleSheet> it(oldStyleSheets);
1920 for (; it.current(); ++it)
1921 it.current()->deref();
1923 // Create a new style selector
1924 delete m_styleSelector;
1925 String usersheet = m_usersheet;
1926 if (m_view && m_view->mediaType() == "print")
1927 usersheet += m_printSheet;
1928 m_styleSelector = new CSSStyleSelector(this, usersheet, m_styleSheets.get(), !inCompatMode());
1929 m_styleSelector->setEncodedURL(m_url);
1930 m_styleSelectorDirty = false;
1933 void Document::setHoverNode(PassRefPtr<Node> newHoverNode)
1935 m_hoverNode = newHoverNode;
1938 void Document::setActiveNode(PassRefPtr<Node> newActiveNode)
1940 m_activeNode = newActiveNode;
1943 void Document::hoveredNodeDetached(Node* node)
1945 if (!m_hoverNode || (node != m_hoverNode && (!m_hoverNode->isTextNode() || node != m_hoverNode->parent())))
1948 m_hoverNode = node->parent();
1949 while (m_hoverNode && !m_hoverNode->renderer())
1950 m_hoverNode = m_hoverNode->parent();
1952 view()->scheduleHoverStateUpdate();
1955 void Document::activeChainNodeDetached(Node* node)
1957 if (!m_activeNode || (node != m_activeNode && (!m_activeNode->isTextNode() || node != m_activeNode->parent())))
1960 m_activeNode = node->parent();
1961 while (m_activeNode && !m_activeNode->renderer())
1962 m_activeNode = m_activeNode->parent();
1965 bool Document::relinquishesEditingFocus(Node *node)
1968 assert(node->isContentEditable());
1970 Node *root = node->rootEditableElement();
1971 if (!frame() || !root)
1974 return frame()->editor()->client()->shouldEndEditing(rangeOfContents(root).get());
1977 bool Document::acceptsEditingFocus(Node *node)
1980 assert(node->isContentEditable());
1982 Node *root = node->rootEditableElement();
1983 if (!frame() || !root)
1986 return frame()->editor()->client()->shouldBeginEditing(rangeOfContents(root).get());
1989 void Document::didBeginEditing()
1994 frame()->editor()->client()->didBeginEditing();
1997 void Document::didEndEditing()
2002 frame()->editor()->client()->didEndEditing();
2006 const Vector<DashboardRegionValue>& Document::dashboardRegions() const
2008 return m_dashboardRegions;
2011 void Document::setDashboardRegions(const Vector<DashboardRegionValue>& regions)
2013 m_dashboardRegions = regions;
2014 setDashboardRegionsDirty(false);
2018 static Widget *widgetForNode(Node *focusNode)
2022 RenderObject *renderer = focusNode->renderer();
2023 if (!renderer || !renderer->isWidget())
2025 return static_cast<RenderWidget*>(renderer)->widget();
2028 bool Document::setFocusNode(PassRefPtr<Node> newFocusNode)
2030 // Make sure newFocusNode is actually in this document
2031 if (newFocusNode && (newFocusNode->document() != this))
2034 if (m_focusNode == newFocusNode)
2037 if (m_focusNode && m_focusNode.get() == m_focusNode->rootEditableElement() && !relinquishesEditingFocus(m_focusNode.get()))
2040 bool focusChangeBlocked = false;
2041 RefPtr<Node> oldFocusNode = m_focusNode;
2043 clearSelectionIfNeeded(newFocusNode.get());
2045 // Remove focus from the existing focus node (if any)
2046 if (oldFocusNode && !oldFocusNode->m_inDetach) {
2047 if (oldFocusNode->active())
2048 oldFocusNode->setActive(false);
2050 oldFocusNode->setFocus(false);
2052 // Dispatch a change event for text fields or textareas that have been edited
2053 RenderObject *r = static_cast<RenderObject*>(oldFocusNode.get()->renderer());
2054 if (r && (r->isTextArea() || r->isTextField()) && r->isEdited()) {
2055 EventTargetNodeCast(oldFocusNode.get())->dispatchHTMLEvent(changeEvent, true, false);
2056 if ((r = static_cast<RenderObject*>(oldFocusNode.get()->renderer())))
2057 r->setEdited(false);
2060 // Dispatch the blur event and let the node do any other blur related activities (important for text fields)
2061 EventTargetNodeCast(oldFocusNode.get())->dispatchBlurEvent();
2064 // handler shifted focus
2065 focusChangeBlocked = true;
2068 clearSelectionIfNeeded(newFocusNode.get());
2069 EventTargetNodeCast(oldFocusNode.get())->dispatchUIEvent(DOMFocusOutEvent);
2071 // handler shifted focus
2072 focusChangeBlocked = true;
2075 clearSelectionIfNeeded(newFocusNode.get());
2076 if ((oldFocusNode.get() == this) && oldFocusNode->hasOneRef())
2079 if (oldFocusNode.get() == oldFocusNode->rootEditableElement())
2084 if (newFocusNode == newFocusNode->rootEditableElement() && !acceptsEditingFocus(newFocusNode.get())) {
2085 // delegate blocks focus change
2086 focusChangeBlocked = true;
2087 goto SetFocusNodeDone;
2089 // Set focus on the new node
2090 m_focusNode = newFocusNode.get();
2092 // Dispatch the focus event and let the node do any other focus related activities (important for text fields)
2093 EventTargetNodeCast(m_focusNode.get())->dispatchFocusEvent();
2095 if (m_focusNode != newFocusNode) {
2096 // handler shifted focus
2097 focusChangeBlocked = true;
2098 goto SetFocusNodeDone;
2100 EventTargetNodeCast(m_focusNode.get())->dispatchUIEvent(DOMFocusInEvent);
2101 if (m_focusNode != newFocusNode) {
2102 // handler shifted focus
2103 focusChangeBlocked = true;
2104 goto SetFocusNodeDone;
2106 m_focusNode->setFocus();
2108 if (m_focusNode.get() == m_focusNode->rootEditableElement())
2111 // eww, I suck. set the qt focus correctly
2112 // ### find a better place in the code for this
2114 Widget *focusWidget = widgetForNode(m_focusNode.get());
2116 // Make sure a widget has the right size before giving it focus.
2117 // Otherwise, we are testing edge cases of the Widget code.
2118 // Specifically, in WebCore this does not work well for text fields.
2120 // Re-get the widget in case updating the layout changed things.
2121 focusWidget = widgetForNode(m_focusNode.get());
2124 focusWidget->setFocus();
2131 if (!focusChangeBlocked && m_focusNode && AXObjectCache::accessibilityEnabled())
2132 axObjectCache()->handleFocusedUIElementChanged();
2137 return !focusChangeBlocked;
2140 void Document::clearSelectionIfNeeded(Node *newFocusNode)
2145 // Clear the selection when changing the focus node to null or to a node that is not
2146 // contained by the current selection.
2147 Node *startContainer = frame()->selectionController()->start().node();
2148 if (!newFocusNode || (startContainer && startContainer != newFocusNode && !(startContainer->isDescendantOf(newFocusNode)) && startContainer->shadowAncestorNode() != newFocusNode))
2149 frame()->selectionController()->clear();
2152 void Document::setCSSTarget(Node* n)
2155 m_cssTarget->setChanged();
2161 Node* Document::getCSSTarget() const
2166 void Document::attachNodeIterator(NodeIterator *ni)
2168 m_nodeIterators.append(ni);
2171 void Document::detachNodeIterator(NodeIterator *ni)
2173 m_nodeIterators.remove(ni);
2176 void Document::notifyBeforeNodeRemoval(Node *n)
2178 if (Frame* f = frame()) {
2179 f->selectionController()->nodeWillBeRemoved(n);
2180 f->dragCaretController()->nodeWillBeRemoved(n);
2182 DeprecatedPtrListIterator<NodeIterator> it(m_nodeIterators);
2183 for (; it.current(); ++it)
2184 it.current()->notifyBeforeNodeRemoval(n);
2187 DOMWindow* Document::defaultView() const
2192 return frame()->domWindow();
2195 PassRefPtr<Event> Document::createEvent(const String &eventType, ExceptionCode& ec)
2197 if (eventType == "UIEvents" || eventType == "UIEvent")
2198 return new UIEvent();
2199 if (eventType == "MouseEvents" || eventType == "MouseEvent")
2200 return new MouseEvent();
2201 if (eventType == "MutationEvents" || eventType == "MutationEvent")
2202 return new MutationEvent();
2203 if (eventType == "KeyboardEvents" || eventType == "KeyboardEvent")
2204 return new KeyboardEvent();
2205 if (eventType == "HTMLEvents" || eventType == "Event" || eventType == "Events")
2208 if (eventType == "SVGEvents")
2210 if (eventType == "SVGZoomEvents")
2211 return new SVGZoomEvent();
2213 ec = NOT_SUPPORTED_ERR;
2217 CSSStyleDeclaration *Document::getOverrideStyle(Element */*elt*/, const String &/*pseudoElt*/)
2222 void Document::handleWindowEvent(Event *evt, bool useCapture)
2224 if (m_windowEventListeners.isEmpty())
2227 // if any html event listeners are registered on the window, then dispatch them here
2228 RegisteredEventListenerList listenersCopy = m_windowEventListeners;
2229 RegisteredEventListenerList::iterator it = listenersCopy.begin();
2231 for (; it != listenersCopy.end(); ++it)
2232 if ((*it)->eventType() == evt->type() && (*it)->useCapture() == useCapture && !(*it)->removed())
2233 (*it)->listener()->handleEvent(evt, true);
2237 void Document::defaultEventHandler(Event *evt)
2240 if (evt->type() == keydownEvent) {
2241 KeyboardEvent* kevt = static_cast<KeyboardEvent *>(evt);
2242 if (kevt->ctrlKey()) {
2243 const PlatformKeyboardEvent* ev = kevt->keyEvent();
2244 String key = (ev ? ev->unmodifiedText() : kevt->keyIdentifier()).lower();
2245 Element* elem = getElementByAccessKey(key);
2247 elem->accessKeyAction(false);
2248 evt->setDefaultHandled();
2254 void Document::setHTMLWindowEventListener(const AtomicString &eventType, PassRefPtr<EventListener> listener)
2256 // If we already have it we don't want removeWindowEventListener to delete it
2257 removeHTMLWindowEventListener(eventType);
2259 addWindowEventListener(eventType, listener, false);
2262 EventListener *Document::getHTMLWindowEventListener(const AtomicString &eventType)
2264 RegisteredEventListenerList::iterator it = m_windowEventListeners.begin();
2265 for (; it != m_windowEventListeners.end(); ++it)
2266 if ( (*it)->eventType() == eventType && (*it)->listener()->isHTMLEventListener())
2267 return (*it)->listener();
2271 void Document::removeHTMLWindowEventListener(const AtomicString &eventType)
2273 RegisteredEventListenerList::iterator it = m_windowEventListeners.begin();
2274 for (; it != m_windowEventListeners.end(); ++it)
2275 if ( (*it)->eventType() == eventType && (*it)->listener()->isHTMLEventListener()) {
2276 m_windowEventListeners.remove(it);
2281 void Document::addWindowEventListener(const AtomicString &eventType, PassRefPtr<EventListener> listener, bool useCapture)
2283 // Remove existing identical listener set with identical arguments.
2284 // The DOM 2 spec says that "duplicate instances are discarded" in this case.
2285 removeWindowEventListener(eventType, listener.get(), useCapture);
2286 m_windowEventListeners.append(new RegisteredEventListener(eventType, listener, useCapture));
2289 void Document::removeWindowEventListener(const AtomicString &eventType, EventListener *listener, bool useCapture)
2291 RegisteredEventListener rl(eventType, listener, useCapture);
2292 RegisteredEventListenerList::iterator it = m_windowEventListeners.begin();
2293 for (; it != m_windowEventListeners.end(); ++it)
2295 m_windowEventListeners.remove(it);
2300 bool Document::hasWindowEventListener(const AtomicString &eventType)
2302 RegisteredEventListenerList::iterator it = m_windowEventListeners.begin();
2303 for (; it != m_windowEventListeners.end(); ++it)
2304 if ((*it)->eventType() == eventType) {
2310 PassRefPtr<EventListener> Document::createHTMLEventListener(const String& functionName, const String& code, Node *node)
2312 if (Frame* frm = frame())
2313 if (KJSProxy* proxy = frm->scriptProxy())
2314 return proxy->createHTMLEventHandler(functionName, code, node);
2318 void Document::setHTMLWindowEventListener(const AtomicString& eventType, Attribute* attr)
2320 setHTMLWindowEventListener(eventType,
2321 createHTMLEventListener(attr->localName().domString(), attr->value(), 0));
2324 void Document::dispatchImageLoadEventSoon(HTMLImageLoader *image)
2326 m_imageLoadEventDispatchSoonList.append(image);
2327 if (!m_imageLoadEventTimer.isActive())
2328 m_imageLoadEventTimer.startOneShot(0);
2331 void Document::removeImage(HTMLImageLoader* image)
2333 // Remove instances of this image from both lists.
2334 // Use loops because we allow multiple instances to get into the lists.
2335 while (m_imageLoadEventDispatchSoonList.removeRef(image)) { }
2336 while (m_imageLoadEventDispatchingList.removeRef(image)) { }
2337 if (m_imageLoadEventDispatchSoonList.isEmpty())
2338 m_imageLoadEventTimer.stop();
2341 void Document::dispatchImageLoadEventsNow()
2343 // need to avoid re-entering this function; if new dispatches are
2344 // scheduled before the parent finishes processing the list, they
2345 // will set a timer and eventually be processed
2346 if (!m_imageLoadEventDispatchingList.isEmpty()) {
2350 m_imageLoadEventTimer.stop();
2352 m_imageLoadEventDispatchingList = m_imageLoadEventDispatchSoonList;
2353 m_imageLoadEventDispatchSoonList.clear();
2354 for (DeprecatedPtrListIterator<HTMLImageLoader> it(m_imageLoadEventDispatchingList); it.current();) {
2355 HTMLImageLoader* image = it.current();
2356 // Must advance iterator *before* dispatching call.
2357 // Otherwise, it might be advanced automatically if dispatching the call had a side effect
2358 // of destroying the current HTMLImageLoader, and then we would advance past the *next* item,
2359 // missing one altogether.
2361 image->dispatchLoadEvent();
2363 m_imageLoadEventDispatchingList.clear();
2366 void Document::imageLoadEventTimerFired(Timer<Document>*)
2368 dispatchImageLoadEventsNow();
2371 Element *Document::ownerElement() const
2375 return frame()->ownerElement();
2378 String Document::referrer() const
2381 return frame()->loader()->referrer();
2385 String Document::domain() const
2387 if (m_domain.isEmpty()) // not set yet (we set it on demand to save time and space)
2388 m_domain = KURL(URL()).host(); // Initially set to the host
2392 void Document::setDomain(const String &newDomain, bool force /*=false*/)
2395 m_domain = newDomain;
2398 if (m_domain.isEmpty()) // not set yet (we set it on demand to save time and space)
2399 m_domain = KURL(URL()).host(); // Initially set to the host
2401 // Both NS and IE specify that changing the domain is only allowed when
2402 // the new domain is a suffix of the old domain.
2403 int oldLength = m_domain.length();
2404 int newLength = newDomain.length();
2405 if (newLength < oldLength) // e.g. newDomain=kde.org (7) and m_domain=www.kde.org (11)
2407 String test = m_domain.copy();
2408 if (test[oldLength - newLength - 1] == '.') // Check that it's a subdomain, not e.g. "de.org"
2410 test.remove(0, oldLength - newLength); // now test is "kde.org" from m_domain
2411 if (test == newDomain) // and we check that it's the same thing as newDomain
2412 m_domain = newDomain;
2417 bool Document::isValidName(const String &name)
2419 const UChar* s = reinterpret_cast<const UChar*>(name.characters());
2420 unsigned length = name.length();
2428 U16_NEXT(s, i, length, c)
2429 if (!isValidNameStart(c))
2432 while (i < length) {
2433 U16_NEXT(s, i, length, c)
2434 if (!isValidNamePart(c))
2441 bool Document::parseQualifiedName(const String &qualifiedName, String &prefix, String &localName)
2443 unsigned length = qualifiedName.length();
2448 bool nameStart = true;
2449 bool sawColon = false;
2452 const UChar* s = reinterpret_cast<const UChar*>(qualifiedName.characters());
2453 for (unsigned i = 0; i < length;) {
2455 U16_NEXT(s, i, length, c)
2458 return false; // multiple colons: not allowed
2462 } else if (nameStart) {
2463 if (!isValidNameStart(c))
2467 if (!isValidNamePart(c))
2474 localName = qualifiedName.copy();
2476 prefix = qualifiedName.substring(0, colonPos);
2477 localName = qualifiedName.substring(colonPos + 1, length - (colonPos + 1));
2483 void Document::addImageMap(HTMLMapElement* imageMap)
2485 // Add the image map, unless there's already another with that name.
2486 // "First map wins" is the rule other browsers seem to implement.
2487 m_imageMapsByName.add(imageMap->getName().impl(), imageMap);
2490 void Document::removeImageMap(HTMLMapElement* imageMap)
2492 // Remove the image map by name.
2493 // But don't remove some other image map that just happens to have the same name.
2494 // FIXME: Use a HashCountedSet as we do for IDs to find the first remaining map
2495 // once a map has been removed.
2496 const AtomicString& name = imageMap->getName();
2497 ImageMapsByName::iterator it = m_imageMapsByName.find(name.impl());
2498 if (it != m_imageMapsByName.end() && it->second == imageMap)
2499 m_imageMapsByName.remove(it);
2502 HTMLMapElement *Document::getImageMap(const String& URL) const
2506 int hashPos = URL.find('#');
2507 AtomicString name = (hashPos < 0 ? URL : URL.substring(hashPos + 1)).impl();
2508 return m_imageMapsByName.get(name.impl());
2511 void Document::setDecoder(TextResourceDecoder *decoder)
2513 m_decoder = decoder;
2516 UChar Document::backslashAsCurrencySymbol() const
2520 return m_decoder->encoding().backslashAsCurrencySymbol();
2523 DeprecatedString Document::completeURL(const DeprecatedString& URL)
2525 // FIXME: This treats null URLs the same as empty URLs, unlike the String function below.
2527 // If both the URL and base URL are empty, like they are for documents
2528 // created using DOMImplementation::createDocument, just return the passed in URL.
2529 // (We do this because URL() returns "about:blank" for empty URLs.
2530 if (m_url.isEmpty() && m_baseURL.isEmpty())
2533 return KURL(baseURL(), URL).url();
2534 return KURL(baseURL(), URL, m_decoder->encoding()).url();
2537 String Document::completeURL(const String &URL)
2539 // FIXME: This always returns null when passed a null URL, unlike the String function above.
2542 return completeURL(URL.deprecatedString());
2545 bool Document::inPageCache()
2547 return m_inPageCache;
2550 void Document::setInPageCache(bool flag)
2552 if (m_inPageCache == flag)
2555 m_inPageCache = flag;
2557 ASSERT(m_savedRenderer == 0);
2558 m_savedRenderer = renderer();
2560 m_view->resetScrollbars();
2562 ASSERT(renderer() == 0 || renderer() == m_savedRenderer);
2563 ASSERT(m_renderArena);
2564 setRenderer(m_savedRenderer);
2565 m_savedRenderer = 0;
2569 void Document::passwordFieldAdded()
2574 void Document::passwordFieldRemoved()
2576 assert(m_passwordFields > 0);
2580 bool Document::hasPasswordField() const
2582 return m_passwordFields > 0;
2585 void Document::secureFormAdded()
2590 void Document::secureFormRemoved()
2592 assert(m_secureForms > 0);
2596 bool Document::hasSecureForm() const
2598 return m_secureForms > 0;
2601 void Document::setShouldCreateRenderers(bool f)
2603 m_createRenderers = f;
2606 bool Document::shouldCreateRenderers()
2608 return m_createRenderers;
2611 String Document::toString() const
2615 for (Node *child = firstChild(); child != NULL; child = child->nextSibling()) {
2616 result += child->toString();
2622 // Support for Javascript execCommand, and related methods
2624 JSEditor *Document::jsEditor()
2627 m_jsEditor = new JSEditor(this);
2631 bool Document::execCommand(const String &command, bool userInterface, const String &value)
2633 return jsEditor()->execCommand(command, userInterface, value);
2636 bool Document::queryCommandEnabled(const String &command)
2638 return jsEditor()->queryCommandEnabled(command);
2641 bool Document::queryCommandIndeterm(const String &command)
2643 return jsEditor()->queryCommandIndeterm(command);
2646 bool Document::queryCommandState(const String &command)
2648 return jsEditor()->queryCommandState(command);
2651 bool Document::queryCommandSupported(const String &command)
2653 return jsEditor()->queryCommandSupported(command);
2656 String Document::queryCommandValue(const String &command)
2658 return jsEditor()->queryCommandValue(command);
2661 static IntRect placeholderRectForMarker(void)
2663 return IntRect(-1,-1,-1,-1);
2666 void Document::addMarker(Range *range, DocumentMarker::MarkerType type, String description)
2668 // Use a TextIterator to visit the potentially multiple nodes the range covers.
2669 for (TextIterator markedText(range); !markedText.atEnd(); markedText.advance()) {
2670 RefPtr<Range> textPiece = markedText.range();
2672 DocumentMarker marker = {type, textPiece->startOffset(exception), textPiece->endOffset(exception), description};
2673 addMarker(textPiece->startContainer(exception), marker);
2677 void Document::removeMarkers(Range *range, DocumentMarker::MarkerType markerType)
2679 // Use a TextIterator to visit the potentially multiple nodes the range covers.
2680 for (TextIterator markedText(range); !markedText.atEnd(); markedText.advance()) {
2681 RefPtr<Range> textPiece = markedText.range();
2683 unsigned startOffset = textPiece->startOffset(exception);
2684 unsigned length = textPiece->endOffset(exception) - startOffset + 1;
2685 removeMarkers(textPiece->startContainer(exception), startOffset, length, markerType);
2689 // Markers are stored in order sorted by their location. They do not overlap each other, as currently
2690 // required by the drawing code in RenderText.cpp.
2692 void Document::addMarker(Node *node, DocumentMarker newMarker)
2694 assert(newMarker.endOffset >= newMarker.startOffset);
2695 if (newMarker.endOffset == newMarker.startOffset)
2698 MarkerMapVectorPair* vectorPair = m_markers.get(node);
2701 vectorPair = new MarkerMapVectorPair;
2702 vectorPair->first.append(newMarker);
2703 vectorPair->second.append(placeholderRectForMarker());
2704 m_markers.set(node, vectorPair);
2706 Vector<DocumentMarker>& markers = vectorPair->first;
2707 Vector<IntRect>& rects = vectorPair->second;
2708 ASSERT(markers.size() == rects.size());
2709 Vector<DocumentMarker>::iterator it;
2710 for (it = markers.begin(); it != markers.end();) {
2711 DocumentMarker marker = *it;
2713 if (newMarker.endOffset < marker.startOffset+1) {
2714 // This is the first marker that is completely after newMarker, and disjoint from it.
2715 // We found our insertion point.
\10
2717 } else if (newMarker.startOffset > marker.endOffset) {
2718 // maker is before newMarker, and disjoint from it. Keep scanning.
2720 } else if (newMarker == marker) {
2721 // already have this one, NOP
2724 // marker and newMarker intersect or touch - merge them into newMarker
2725 newMarker.startOffset = min(newMarker.startOffset, marker.startOffset);
2726 newMarker.endOffset = max(newMarker.endOffset, marker.endOffset);
2727 // remove old one, we'll add newMarker later
2728 unsigned removalIndex = it - markers.begin();
2729 markers.remove(removalIndex);
2730 rects.remove(removalIndex);
2731 // it points to the next marker to consider
2734 // at this point it points to the node before which we want to insert
2735 unsigned insertionIndex = it - markers.begin();
2736 markers.insert(insertionIndex, newMarker);
2737 rects.insert(insertionIndex, placeholderRectForMarker());
2740 // repaint the affected node
2741 if (node->renderer())
2742 node->renderer()->repaint();
2745 // copies markers from srcNode to dstNode, applying the specified shift delta to the copies. The shift is
2746 // useful if, e.g., the caller has created the dstNode from a non-prefix substring of the srcNode.
2747 void Document::copyMarkers(Node *srcNode, unsigned startOffset, int length, Node *dstNode, int delta, DocumentMarker::MarkerType markerType)
2752 MarkerMapVectorPair* vectorPair = m_markers.get(srcNode);
2756 ASSERT(vectorPair->first.size() == vectorPair->second.size());
2758 bool docDirty = false;
2759 unsigned endOffset = startOffset + length - 1;
2760 Vector<DocumentMarker>::iterator it;
2761 Vector<DocumentMarker>& markers = vectorPair->first;
2762 for (it = markers.begin(); it != markers.end(); ++it) {
2763 DocumentMarker marker = *it;
2765 // stop if we are now past the specified range
2766 if (marker.startOffset > endOffset)
2769 // skip marker that is before the specified range or is the wrong type
2770 if (marker.endOffset < startOffset || (marker.type != markerType && markerType != DocumentMarker::AllMarkers))
2773 // pin the marker to the specified range and apply the shift delta
2775 if (marker.startOffset < startOffset)
2776 marker.startOffset = startOffset;
2777 if (marker.endOffset > endOffset)
2778 marker.endOffset = endOffset;
2779 marker.startOffset += delta;
2780 marker.endOffset += delta;
2782 addMarker(dstNode, marker);
2785 // repaint the affected node
2786 if (docDirty && dstNode->renderer())
2787 dstNode->renderer()->repaint();
2790 void Document::removeMarkers(Node *node, unsigned startOffset, int length, DocumentMarker::MarkerType markerType)
2795 MarkerMapVectorPair* vectorPair = m_markers.get(node);
2799 Vector<DocumentMarker>& markers = vectorPair->first;
2800 Vector<IntRect>& rects = vectorPair->second;
2801 ASSERT(markers.size() == rects.size());
2802 bool docDirty = false;
2803 unsigned endOffset = startOffset + length - 1;
2804 Vector<DocumentMarker>::iterator it;
2805 for (it = markers.begin(); it < markers.end();) {
2806 DocumentMarker marker = *it;
2808 // markers are returned in order, so stop if we are now past the specified range
2809 if (marker.startOffset > endOffset)
2812 // skip marker that is wrong type or before target
2813 if (marker.endOffset < startOffset || (marker.type != markerType && markerType != DocumentMarker::AllMarkers)) {
2818 // at this point we know that marker and target intersect in some way
2821 // pitch the old marker and any associated rect
2822 unsigned removalIndex = it - markers.begin();
2823 markers.remove(removalIndex);
2824 rects.remove(removalIndex);
2825 // it now points to the next node
2827 // add either of the resulting slices that are left after removing target
2828 if (startOffset > marker.startOffset) {
2829 DocumentMarker newLeft = marker;
2830 newLeft.endOffset = startOffset;
2831 unsigned insertionIndex = it - markers.begin();
2832 markers.insert(insertionIndex, newLeft);
2833 rects.insert(insertionIndex, placeholderRectForMarker());
2834 // it now points to the newly-inserted node, but we want to skip that one
2837 if (marker.endOffset > endOffset) {
2838 DocumentMarker newRight = marker;
2839 newRight.startOffset = endOffset;
2840 unsigned insertionIndex = it - markers.begin();
2841 markers.insert(insertionIndex, newRight);
2842 rects.insert(insertionIndex, placeholderRectForMarker());
2843 // it now points to the newly-inserted node, but we want to skip that one
2848 if (markers.isEmpty()) {
2849 ASSERT(rects.isEmpty());
2850 m_markers.remove(node);
2854 // repaint the affected node
2855 if (docDirty && node->renderer())
2856 node->renderer()->repaint();
2859 DocumentMarker* Document::markerContainingPoint(const IntPoint& point, DocumentMarker::MarkerType markerType)
2861 // outer loop: process each node that contains any markers
2862 MarkerMap::iterator end = m_markers.end();
2863 for (MarkerMap::iterator nodeIterator = m_markers.begin(); nodeIterator != end; ++nodeIterator) {
2864 // inner loop; process each marker in this node
2865 MarkerMapVectorPair* vectorPair = nodeIterator->second;
2866 Vector<DocumentMarker>& markers = vectorPair->first;
2867 Vector<IntRect>& rects = vectorPair->second;
2868 ASSERT(markers.size() == rects.size());
2869 unsigned markerCount = markers.size();
2870 for (unsigned markerIndex = 0; markerIndex < markerCount; ++markerIndex) {
2871 DocumentMarker& marker = markers[markerIndex];
2873 // skip marker that is wrong type
2874 if (marker.type != markerType && markerType != DocumentMarker::AllMarkers)
2877 IntRect& r = rects[markerIndex];
2879 // skip placeholder rects
2880 if (r == placeholderRectForMarker())
2883 if (r.contains(point))
2891 Vector<DocumentMarker> Document::markersForNode(Node* node)
2893 MarkerMapVectorPair* vectorPair = m_markers.get(node);
2895 return vectorPair->first;
2896 return Vector<DocumentMarker>();
2899 Vector<IntRect> Document::renderedRectsForMarkers(DocumentMarker::MarkerType markerType)
2901 Vector<IntRect> result;
2903 // outer loop: process each node
2904 MarkerMap::iterator end = m_markers.end();
2905 for (MarkerMap::iterator nodeIterator = m_markers.begin(); nodeIterator != end; ++nodeIterator) {
2906 // inner loop; process each marker in this node
2907 MarkerMapVectorPair* vectorPair = nodeIterator->second;
2908 Vector<DocumentMarker>& markers = vectorPair->first;
2909 Vector<IntRect>& rects = vectorPair->second;
2910 ASSERT(markers.size() == rects.size());
2911 unsigned markerCount = markers.size();
2912 for (unsigned markerIndex = 0; markerIndex < markerCount; ++markerIndex) {
2913 DocumentMarker marker = markers[markerIndex];
2915 // skip marker that is wrong type
2916 if (marker.type != markerType && markerType != DocumentMarker::AllMarkers)
2919 IntRect r = rects[markerIndex];
2920 // skip placeholder rects
2921 if (r == placeholderRectForMarker())
2932 void Document::removeMarkers(Node* node)
2934 MarkerMap::iterator i = m_markers.find(node);
2935 if (i != m_markers.end()) {
2937 m_markers.remove(i);
2938 if (RenderObject* renderer = node->renderer())
2939 renderer->repaint();
2943 void Document::removeMarkers(DocumentMarker::MarkerType markerType)
2945 // outer loop: process each markered node in the document
2946 MarkerMap markerMapCopy = m_markers;
2947 MarkerMap::iterator end = markerMapCopy.end();
2948 for (MarkerMap::iterator i = markerMapCopy.begin(); i != end; ++i) {
2949 Node* node = i->first.get();
2950 bool nodeNeedsRepaint = false;
2952 // inner loop: process each marker in the current node
2953 MarkerMapVectorPair* vectorPair = i->second;
2954 Vector<DocumentMarker>& markers = vectorPair->first;
2955 Vector<IntRect>& rects = vectorPair->second;
2956 ASSERT(markers.size() == rects.size());
2957 Vector<DocumentMarker>::iterator markerIterator;
2958 for (markerIterator = markers.begin(); markerIterator != markers.end();) {
2959 DocumentMarker marker = *markerIterator;
2961 // skip nodes that are not of the specified type
2962 if (marker.type != markerType && markerType != DocumentMarker::AllMarkers) {
2967 // pitch the old marker
2968 markers.remove(markerIterator - markers.begin());
2969 rects.remove(markerIterator - markers.begin());
2970 nodeNeedsRepaint = true;
2971 // markerIterator now points to the next node
2974 // Redraw the node if it changed. Do this before the node is removed from m_markers, since
2975 // m_markers might contain the last reference to the node.
2976 if (nodeNeedsRepaint) {
2977 RenderObject* renderer = node->renderer();
2979 renderer->repaint();
2982 // delete the node's list if it is now empty
2983 if (markers.isEmpty()) {
2984 ASSERT(rects.isEmpty());
2985 m_markers.remove(node);
2991 void Document::repaintMarkers(DocumentMarker::MarkerType markerType)
2993 // outer loop: process each markered node in the document
2994 MarkerMap::iterator end = m_markers.end();
2995 for (MarkerMap::iterator i = m_markers.begin(); i != end; ++i) {
2996 Node* node = i->first.get();
2998 // inner loop: process each marker in the current node
2999 MarkerMapVectorPair* vectorPair = i->second;
3000 Vector<DocumentMarker>& markers = vectorPair->first;
3001 Vector<DocumentMarker>::iterator markerIterator;
3002 bool nodeNeedsRepaint = false;
3003 for (markerIterator = markers.begin(); markerIterator != markers.end(); ++markerIterator) {
3004 DocumentMarker marker = *markerIterator;
3006 // skip nodes that are not of the specified type
3007 if (marker.type == markerType || markerType == DocumentMarker::AllMarkers) {
3008 nodeNeedsRepaint = true;
3013 if (!nodeNeedsRepaint)
3016 // cause the node to be redrawn
3017 if (RenderObject* renderer = node->renderer())
3018 renderer->repaint();
3022 void Document::setRenderedRectForMarker(Node* node, DocumentMarker marker, const IntRect& r)
3024 MarkerMapVectorPair* vectorPair = m_markers.get(node);
3026 ASSERT_NOT_REACHED(); // shouldn't be trying to set the rect for a marker we don't already know about
3030 Vector<DocumentMarker>& markers = vectorPair->first;
3031 ASSERT(markers.size() == vectorPair->second.size());
3032 unsigned markerCount = markers.size();
3033 for (unsigned markerIndex = 0; markerIndex < markerCount; ++markerIndex) {
3034 DocumentMarker m = markers[markerIndex];
3036 vectorPair->second[markerIndex] = r;
3041 ASSERT_NOT_REACHED(); // shouldn't be trying to set the rect for a marker we don't already know about
3044 void Document::invalidateRenderedRectsForMarkersInRect(const IntRect& r)
3046 // outer loop: process each markered node in the document
3047 MarkerMap::iterator end = m_markers.end();
3048 for (MarkerMap::iterator i = m_markers.begin(); i != end; ++i) {
3050 // inner loop: process each rect in the current node
3051 MarkerMapVectorPair* vectorPair = i->second;
3052 Vector<IntRect>& rects = vectorPair->second;
3054 unsigned rectCount = rects.size();
3055 for (unsigned rectIndex = 0; rectIndex < rectCount; ++rectIndex)
3056 if (rects[rectIndex].intersects(r))
3057 rects[rectIndex] = placeholderRectForMarker();
3061 void Document::shiftMarkers(Node *node, unsigned startOffset, int delta, DocumentMarker::MarkerType markerType)
3063 MarkerMapVectorPair* vectorPair = m_markers.get(node);
3067 Vector<DocumentMarker>& markers = vectorPair->first;
3068 Vector<IntRect>& rects = vectorPair->second;
3069 ASSERT(markers.size() == rects.size());
3071 bool docDirty = false;
3072 Vector<DocumentMarker>::iterator it;
3073 for (it = markers.begin(); it != markers.end(); ++it) {
3074 DocumentMarker &marker = *it;
3075 if (marker.startOffset >= startOffset && (markerType == DocumentMarker::AllMarkers || marker.type == markerType)) {
3076 assert((int)marker.startOffset + delta >= 0);
3077 marker.startOffset += delta;
3078 marker.endOffset += delta;
3081 // Marker moved, so previously-computed rendered rectangle is now invalid
3082 rects[it - markers.begin()] = placeholderRectForMarker();
3086 // repaint the affected node
3087 if (docDirty && node->renderer())
3088 node->renderer()->repaint();
3093 void Document::applyXSLTransform(ProcessingInstruction* pi)
3095 RefPtr<XSLTProcessor> processor = new XSLTProcessor;
3096 processor->setXSLStylesheet(static_cast<XSLStyleSheet*>(pi->sheet()));
3098 DeprecatedString resultMIMEType;
3099 DeprecatedString newSource;
3100 DeprecatedString resultEncoding;
3101 if (!processor->transformToString(this, resultMIMEType, newSource, resultEncoding))
3103 // FIXME: If the transform failed we should probably report an error (like Mozilla does).
3104 processor->createDocumentFromSource(newSource, resultEncoding, resultMIMEType, this, view());
3109 void Document::setDesignMode(InheritedBool value)
3111 m_designMode = value;
3114 Document::InheritedBool Document::getDesignMode() const
3116 return m_designMode;
3119 bool Document::inDesignMode() const
3121 for (const Document* d = this; d; d = d->parentDocument()) {
3122 if (d->m_designMode != inherit)
3123 return d->m_designMode;
3128 Document *Document::parentDocument() const
3130 Frame *childPart = frame();
3133 Frame *parent = childPart->tree()->parent();
3136 return parent->document();
3139 Document *Document::topDocument() const
3141 Document *doc = const_cast<Document *>(this);
3143 while ((element = doc->ownerElement()) != 0)
3144 doc = element->document();
3149 PassRefPtr<Attr> Document::createAttributeNS(const String &namespaceURI, const String &qualifiedName, ExceptionCode& ec)
3151 if (qualifiedName.isNull()) {
3156 String localName = qualifiedName;
3159 if ((colonpos = qualifiedName.find(':')) >= 0) {
3160 prefix = qualifiedName.copy();
3161 localName = qualifiedName.copy();
3162 prefix.truncate(colonpos);
3163 localName.remove(0, colonpos+1);
3166 if (!isValidName(localName)) {
3167 ec = INVALID_CHARACTER_ERR;
3171 // FIXME: Assume this is a mapped attribute, since createAttribute isn't namespace-aware. There's no harm to XML
3172 // documents if we're wrong.
3173 return new Attr(0, this, new MappedAttribute(QualifiedName(prefix.impl(),
3175 namespaceURI.impl()), String("").impl()));
3179 const SVGDocumentExtensions* Document::svgExtensions()
3181 return m_svgExtensions;
3184 SVGDocumentExtensions* Document::accessSVGExtensions()
3186 if (!m_svgExtensions)
3187 m_svgExtensions = new SVGDocumentExtensions(this);
3188 return m_svgExtensions;
3192 void Document::radioButtonChecked(HTMLInputElement *caller, HTMLFormElement *form)
3194 // Without a name, there is no group.
3195 if (caller->name().isEmpty())
3199 // Uncheck the currently selected item
3200 NameToInputMap* formRadioButtons = m_selectedRadioButtons.get(form);
3201 if (!formRadioButtons) {
3202 formRadioButtons = new NameToInputMap;
3203 m_selectedRadioButtons.set(form, formRadioButtons);
3206 HTMLInputElement* currentCheckedRadio = formRadioButtons->get(caller->name().impl());
3207 if (currentCheckedRadio && currentCheckedRadio != caller)
3208 currentCheckedRadio->setChecked(false);
3210 formRadioButtons->set(caller->name().impl(), caller);
3213 HTMLInputElement* Document::checkedRadioButtonForGroup(AtomicStringImpl* name, HTMLFormElement *form)
3217 NameToInputMap* formRadioButtons = m_selectedRadioButtons.get(form);
3218 if (!formRadioButtons)
3221 return formRadioButtons->get(name);
3224 void Document::removeRadioButtonGroup(AtomicStringImpl* name, HTMLFormElement *form)
3228 NameToInputMap* formRadioButtons = m_selectedRadioButtons.get(form);
3229 if (formRadioButtons) {
3230 formRadioButtons->remove(name);
3231 if (formRadioButtons->isEmpty()) {
3232 m_selectedRadioButtons.remove(form);
3233 delete formRadioButtons;
3238 PassRefPtr<HTMLCollection> Document::images()
3240 return new HTMLCollection(this, HTMLCollection::DocImages);
3243 PassRefPtr<HTMLCollection> Document::applets()
3245 return new HTMLCollection(this, HTMLCollection::DocApplets);
3248 PassRefPtr<HTMLCollection> Document::embeds()
3250 return new HTMLCollection(this, HTMLCollection::DocEmbeds);
3253 PassRefPtr<HTMLCollection> Document::objects()
3255 return new HTMLCollection(this, HTMLCollection::DocObjects);
3258 PassRefPtr<HTMLCollection> Document::scripts()
3260 return new HTMLCollection(this, HTMLCollection::DocScripts);
3263 PassRefPtr<HTMLCollection> Document::links()
3265 return new HTMLCollection(this, HTMLCollection::DocLinks);
3268 PassRefPtr<HTMLCollection> Document::forms()
3270 return new HTMLCollection(this, HTMLCollection::DocForms);
3273 PassRefPtr<HTMLCollection> Document::anchors()
3275 return new HTMLCollection(this, HTMLCollection::DocAnchors);
3278 PassRefPtr<HTMLCollection> Document::all()
3280 return new HTMLCollection(this, HTMLCollection::DocAll);
3283 PassRefPtr<HTMLCollection> Document::windowNamedItems(const String &name)
3285 return new HTMLNameCollection(this, HTMLCollection::WindowNamedItems, name);
3288 PassRefPtr<HTMLCollection> Document::documentNamedItems(const String &name)
3290 return new HTMLNameCollection(this, HTMLCollection::DocumentNamedItems, name);
3293 HTMLCollection::CollectionInfo* Document::nameCollectionInfo(HTMLCollection::Type type, const String& name)
3295 HashMap<AtomicStringImpl*, HTMLCollection::CollectionInfo>& map = m_nameCollectionInfo[type - HTMLCollection::UnnamedCollectionTypes];
3297 AtomicString atomicName(name);
3299 HashMap<AtomicStringImpl*, HTMLCollection::CollectionInfo>::iterator iter = map.find(atomicName.impl());
3300 if (iter == map.end())
3301 iter = map.add(atomicName.impl(), HTMLCollection::CollectionInfo()).first;
3303 return &iter->second;
3306 PassRefPtr<NameNodeList> Document::getElementsByName(const String &elementName)
3308 return new NameNodeList(this, elementName);
3311 void Document::finishedParsing()
3314 if (Frame* f = frame())
3315 f->loader()->finishedParsing();
3318 Vector<String> Document::formElementsState() const
3320 Vector<String> stateVector;
3321 stateVector.reserveCapacity(m_formElementsWithState.size() * 3);
3322 typedef HashSet<HTMLGenericFormElement*>::const_iterator Iterator;
3323 Iterator end = m_formElementsWithState.end();
3324 for (Iterator it = m_formElementsWithState.begin(); it != end; ++it) {
3325 HTMLGenericFormElement* e = *it;
3326 stateVector.append(e->name().domString());
3327 stateVector.append(e->type().domString());
3328 stateVector.append(e->stateValue());
3333 #ifdef XPATH_SUPPORT
3335 PassRefPtr<XPathExpression> Document::createExpression(const String& expression,
3336 XPathNSResolver* resolver,
3339 if (!m_xpathEvaluator)
3340 m_xpathEvaluator = new XPathEvaluator;
3341 return m_xpathEvaluator->createExpression(expression, resolver, ec);
3344 PassRefPtr<XPathNSResolver> Document::createNSResolver(Node* nodeResolver)
3346 if (!m_xpathEvaluator)
3347 m_xpathEvaluator = new XPathEvaluator;
3348 return m_xpathEvaluator->createNSResolver(nodeResolver);
3351 PassRefPtr<XPathResult> Document::evaluate(const String& expression,
3353 XPathNSResolver* resolver,
3354 unsigned short type,
3355 XPathResult* result,
3358 if (!m_xpathEvaluator)
3359 m_xpathEvaluator = new XPathEvaluator;
3360 return m_xpathEvaluator->evaluate(expression, contextNode, resolver, type, result, ec);
3363 #endif // XPATH_SUPPORT
3365 void Document::setStateForNewFormElements(const Vector<String>& stateVector)
3367 // Walk the state vector backwards so that the value to use for each
3368 // name/type pair first is the one at the end of each individual vector
3369 // in the FormElementStateMap. We're using them like stacks.
3370 typedef FormElementStateMap::iterator Iterator;
3371 m_formElementsWithState.clear();
3372 for (size_t i = stateVector.size() / 3 * 3; i; i -= 3) {
3373 AtomicString a = stateVector[i - 3];
3374 AtomicString b = stateVector[i - 2];
3375 const String& c = stateVector[i - 1];
3376 FormElementKey key(a.impl(), b.impl());
3377 Iterator it = m_stateForNewFormElements.find(key);
3378 if (it != m_stateForNewFormElements.end())
3379 it->second.append(c);
3381 Vector<String> v(1);
3383 m_stateForNewFormElements.set(key, v);
3388 bool Document::hasStateForNewFormElements() const
3390 return !m_stateForNewFormElements.isEmpty();
3393 bool Document::takeStateForFormElement(AtomicStringImpl* name, AtomicStringImpl* type, String& state)
3395 typedef FormElementStateMap::iterator Iterator;
3396 Iterator it = m_stateForNewFormElements.find(FormElementKey(name, type));
3397 if (it == m_stateForNewFormElements.end())
3399 ASSERT(it->second.size());
3400 state = it->second.last();
3401 if (it->second.size() > 1)
3402 it->second.removeLast();
3404 m_stateForNewFormElements.remove(it);
3408 FormElementKey::FormElementKey(AtomicStringImpl* name, AtomicStringImpl* type)
3409 : m_name(name), m_type(type)
3414 FormElementKey::~FormElementKey()
3419 FormElementKey::FormElementKey(const FormElementKey& other)
3420 : m_name(other.name()), m_type(other.type())
3425 FormElementKey& FormElementKey::operator=(const FormElementKey& other)
3429 m_name = other.name();
3430 m_type = other.type();
3434 void FormElementKey::ref() const
3436 if (name() && name() != HashTraits<AtomicStringImpl*>::deletedValue())
3442 void FormElementKey::deref() const
3444 if (name() && name() != HashTraits<AtomicStringImpl*>::deletedValue())
3450 unsigned FormElementKeyHash::hash(const FormElementKey& k)
3452 ASSERT(sizeof(k) % (sizeof(uint16_t) * 2) == 0);
3454 unsigned l = sizeof(k) / (sizeof(uint16_t) * 2);
3455 const uint16_t* s = reinterpret_cast<const uint16_t*>(&k);
3456 uint32_t hash = PHI;
3459 for (; l > 0; l--) {
3461 uint32_t tmp = (s[1] << 11) ^ hash;
3462 hash = (hash << 16) ^ tmp;
3467 // Force "avalanching" of final 127 bits
3474 // this avoids ever returning a hash code of 0, since that is used to
3475 // signal "hash not computed yet", using a value that is likely to be
3476 // effectively the same as 0 when the low bits are masked
3483 FormElementKey FormElementKeyHashTraits::deletedValue()
3485 return HashTraits<AtomicStringImpl*>::deletedValue();
3489 String Document::iconURL()
3494 void Document::setIconURL(const String& iconURL, const String& type)
3496 // FIXME - <rdar://problem/4727645> - At some point in the future, we might actually honor the "type"
3497 if (m_iconURL.isEmpty())
3498 m_iconURL = iconURL;
3499 else if (!type.isEmpty())
3500 m_iconURL = iconURL;