Document.cpp 145 KB
Newer Older
1
/*
kocienda's avatar
kocienda committed
2 3
 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
 *           (C) 1999 Antti Koivisto (koivisto@kde.org)
4
 *           (C) 2001 Dirk Mueller (mueller@kde.org)
ap's avatar
ap committed
5
 *           (C) 2006 Alexey Proskuryakov (ap@webkit.org)
6
 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
7
 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
levin@chromium.org's avatar
levin@chromium.org committed
8
 * Copyright (C) 2008, 2009 Google Inc. All rights reserved.
kocienda's avatar
kocienda committed
9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
ddkilzer's avatar
ddkilzer committed
22 23
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
kocienda's avatar
kocienda committed
24 25
 */

mjs's avatar
mjs committed
26
#include "config.h"
darin's avatar
darin committed
27
#include "Document.h"
darin's avatar
darin committed
28

29
#include "AXObjectCache.h"
weinig@apple.com's avatar
weinig@apple.com committed
30
#include "AnimationController.h"
darin@apple.com's avatar
darin@apple.com committed
31
#include "Attr.h"
darin's avatar
darin committed
32
#include "CDATASection.h"
weinig's avatar
weinig committed
33 34
#include "CSSHelper.h"
#include "CSSStyleSelector.h"
35
#include "CSSStyleSheet.h"
darin's avatar
darin committed
36
#include "CSSValueKeywords.h"
weinig@apple.com's avatar
weinig@apple.com committed
37
#include "CString.h"
weinig@apple.com's avatar
weinig@apple.com committed
38
#include "CachedCSSStyleSheet.h"
darin's avatar
darin committed
39
#include "Comment.h"
ap@webkit.org's avatar
ap@webkit.org committed
40
#include "Console.h"
oliver's avatar
oliver committed
41
#include "CookieJar.h"
darin's avatar
darin committed
42
#include "DOMImplementation.h"
weinig@apple.com's avatar
weinig@apple.com committed
43
#include "DOMWindow.h"
darin's avatar
darin committed
44
#include "DocLoader.h"
darin's avatar
darin committed
45
#include "DocumentFragment.h"
darin's avatar
darin committed
46
#include "DocumentLoader.h"
darin's avatar
darin committed
47 48
#include "DocumentType.h"
#include "EditingText.h"
andersca's avatar
andersca committed
49
#include "Editor.h"
50
#include "EntityReference.h"
weinig's avatar
 
weinig committed
51
#include "Event.h"
darin's avatar
darin committed
52
#include "EventHandler.h"
darin's avatar
darin committed
53
#include "EventListener.h"
darin's avatar
darin committed
54
#include "EventNames.h"
darin's avatar
darin committed
55
#include "ExceptionCode.h"
56
#include "FocusController.h"
mjs's avatar
mjs committed
57
#include "Frame.h"
ggaren's avatar
ggaren committed
58
#include "FrameLoader.h"
mjs's avatar
mjs committed
59
#include "FrameTree.h"
darin's avatar
darin committed
60
#include "FrameView.h"
61
#include "HTMLAnchorElement.h"
62
#include "HTMLBodyElement.h"
63
#include "HTMLCanvasElement.h"
darin@apple.com's avatar
darin@apple.com committed
64
#include "HTMLCollection.h"
darin's avatar
darin committed
65 66
#include "HTMLDocument.h"
#include "HTMLElementFactory.h"
darin's avatar
darin committed
67
#include "HTMLFrameOwnerElement.h"
darin's avatar
darin committed
68
#include "HTMLHeadElement.h"
darin's avatar
darin committed
69
#include "HTMLInputElement.h"
70
#include "HTMLLinkElement.h"
71
#include "HTMLMapElement.h"
darin's avatar
darin committed
72
#include "HTMLNameCollection.h"
darin's avatar
darin committed
73
#include "HTMLNames.h"
74
#include "HTMLParser.h"
75
#include "HTMLStyleElement.h"
bdash's avatar
bdash committed
76
#include "HTMLTitleElement.h"
ap's avatar
ap committed
77
#include "HTTPParsers.h"
weinig@apple.com's avatar
weinig@apple.com committed
78
#include "HistoryItem.h"
weinig's avatar
weinig committed
79 80
#include "HitTestRequest.h"
#include "HitTestResult.h"
zimmermann@webkit.org's avatar
zimmermann@webkit.org committed
81
#include "ImageLoader.h"
82
#include "InspectorController.h"
83
#include "InspectorTimelineAgent.h"
weinig's avatar
 
weinig committed
84
#include "KeyboardEvent.h"
darin's avatar
darin committed
85
#include "Logging.h"
darin@apple.com's avatar
darin@apple.com committed
86
#include "MappedAttribute.h"
weinig@apple.com's avatar
weinig@apple.com committed
87
#include "MessageEvent.h"
weinig's avatar
 
weinig committed
88
#include "MouseEvent.h"
darin's avatar
darin committed
89
#include "MouseEventWithHitTestResults.h"
weinig's avatar
 
weinig committed
90
#include "MutationEvent.h"
darin's avatar
darin committed
91
#include "NameNodeList.h"
92 93
#include "NodeFilter.h"
#include "NodeIterator.h"
darin@apple.com's avatar
darin@apple.com committed
94
#include "NodeWithIndex.h"
darin's avatar
darin committed
95
#include "OverflowEvent.h"
96
#include "Page.h"
97
#include "PageGroup.h"
ap@apple.com's avatar
ap@apple.com committed
98
#include "PageTransitionEvent.h"
darin's avatar
darin committed
99
#include "PlatformKeyboardEvent.h"
100
#include "ProcessingInstruction.h"
antti's avatar
antti committed
101
#include "ProgressEvent.h"
weinig's avatar
 
weinig committed
102
#include "RegisteredEventListener.h"
darin's avatar
darin committed
103
#include "RenderArena.h"
104
#include "RenderTextControl.h"
105
#include "RenderView.h"
106
#include "RenderWidget.h"
107
#include "ScriptController.h"
mitz@apple.com's avatar
mitz@apple.com committed
108
#include "ScriptElement.h"
109
#include "ScriptEventListener.h"
weinig's avatar
weinig committed
110
#include "SecurityOrigin.h"
darin's avatar
darin committed
111
#include "SegmentedString.h"
darin's avatar
darin committed
112
#include "SelectionController.h"
ggaren's avatar
ggaren committed
113
#include "Settings.h"
114
#include "StyleSheetList.h"
darin's avatar
darin committed
115
#include "TextEvent.h"
darin's avatar
darin committed
116
#include "TextIterator.h"
darin's avatar
darin committed
117
#include "TextResourceDecoder.h"
118
#include "Timer.h"
119
#include "TransformSource.h"
weinig@apple.com's avatar
weinig@apple.com committed
120
#include "TreeWalker.h"
weinig's avatar
 
weinig committed
121
#include "UIEvent.h"
122
#include "UserContentURLPattern.h"
123 124
#include "WebKitAnimationEvent.h"
#include "WebKitTransitionEvent.h"
darin's avatar
darin committed
125
#include "WheelEvent.h"
weinig's avatar
weinig committed
126
#include "XMLHttpRequest.h"
eric@webkit.org's avatar
eric@webkit.org committed
127
#include "XMLNames.h"
darin's avatar
darin committed
128
#include "XMLTokenizer.h"
129
#include "htmlediting.h"
ap@webkit.org's avatar
ap@webkit.org committed
130
#include <wtf/CurrentTime.h>
dsmith@webkit.org's avatar
dsmith@webkit.org committed
131
#include <wtf/HashFunctions.h>
132 133
#include <wtf/MainThread.h>
#include <wtf/PassRefPtr.h>
weinig@apple.com's avatar
weinig@apple.com committed
134
#include <wtf/StdLibExtras.h>
135

136
#if ENABLE(DATABASE)
137
#include "Database.h"
138 139 140
#include "DatabaseThread.h"
#endif

141 142 143 144
#if ENABLE(SHARED_WORKERS)
#include "SharedWorkerRepository.h"
#endif

145 146 147 148
#if ENABLE(DOM_STORAGE)
#include "StorageEvent.h"
#endif

mjs's avatar
mjs committed
149
#if ENABLE(XPATH)
150 151 152 153
#include "XPathEvaluator.h"
#include "XPathExpression.h"
#include "XPathNSResolver.h"
#include "XPathResult.h"
154
#endif
kocienda's avatar
kocienda committed
155

mjs's avatar
mjs committed
156
#if ENABLE(XSLT)
darin's avatar
darin committed
157
#include "XSLTProcessor.h"
158 159
#endif

mjs's avatar
mjs committed
160
#if ENABLE(XBL)
161
#include "XBLBindingManager.h"
162 163
#endif

mjs's avatar
mjs committed
164
#if ENABLE(SVG)
165
#include "SVGDocumentExtensions.h"
eseidel's avatar
eseidel committed
166
#include "SVGElementFactory.h"
167
#include "SVGNames.h"
darin's avatar
darin committed
168 169
#include "SVGZoomEvent.h"
#include "SVGStyleElement.h"
eseidel's avatar
eseidel committed
170 171
#endif

172
#if ENABLE(WML)
173
#include "WMLDocument.h"
174 175 176 177 178
#include "WMLElement.h"
#include "WMLElementFactory.h"
#include "WMLNames.h"
#endif

179 180 181 182 183 184
#if ENABLE(MATHML)
#include "MathMLElement.h"
#include "MathMLElementFactory.h"
#include "MathMLNames.h"
#endif

185 186 187 188
#if ENABLE(XHTMLMP)
#include "HTMLNoScriptElement.h"
#endif

darin's avatar
darin committed
189
using namespace std;
darin's avatar
darin committed
190 191
using namespace WTF;
using namespace Unicode;
darin's avatar
darin committed
192

darin's avatar
darin committed
193 194
namespace WebCore {

195
using namespace HTMLNames;
kocienda's avatar
kocienda committed
196

197
// #define INSTRUMENT_LAYOUT_SCHEDULING 1
hyatt's avatar
hyatt committed
198

199 200 201
// This amount of time must have elapsed before we will even consider scheduling a layout without a delay.
// FIXME: For faster machines this value can really be lowered to 200.  250 is adequate, but a little high
// for dual G5s. :)
darin's avatar
darin committed
202
static const int cLayoutScheduleThreshold = 250;
203

adele's avatar
adele committed
204
// Use 1 to represent the document's default form.
darin's avatar
darin committed
205
static HTMLFormElement* const defaultForm = reinterpret_cast<HTMLFormElement*>(1);
adele's avatar
adele committed
206

eseidel's avatar
eseidel committed
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
// DOM Level 2 says (letters added):
//
// a) Name start characters must have one of the categories Ll, Lu, Lo, Lt, Nl.
// b) Name characters other than Name-start characters must have one of the categories Mc, Me, Mn, Lm, or Nd.
// c) Characters in the compatibility area (i.e. with character code greater than #xF900 and less than #xFFFE) are not allowed in XML names.
// 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.
// 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.
// f) Characters #x20DD-#x20E0 are excluded (in accordance with Unicode, section 5.14).
// g) Character #x00B7 is classified as an extender, because the property list so identifies it.
// h) Character #x0387 is added as a name character, because #x00B7 is its canonical equivalent.
// i) Characters ':' and '_' are allowed as name-start characters.
// j) Characters '-' and '.' are allowed as name characters.
//
// It also contains complete tables. If we decide it's better, we could include those instead of the following code.

static inline bool isValidNameStart(UChar32 c)
{
    // rule (e) above
    if ((c >= 0x02BB && c <= 0x02C1) || c == 0x559 || c == 0x6E5 || c == 0x6E6)
        return true;

    // rule (i) above
    if (c == ':' || c == '_')
        return true;

    // rules (a) and (f) above
darin's avatar
darin committed
233 234
    const uint32_t nameStartMask = Letter_Lowercase | Letter_Uppercase | Letter_Other | Letter_Titlecase | Number_Letter;
    if (!(Unicode::category(c) & nameStartMask))
eseidel's avatar
eseidel committed
235 236 237 238 239 240 241
        return false;

    // rule (c) above
    if (c >= 0xF900 && c < 0xFFFE)
        return false;

    // rule (d) above
darin's avatar
darin committed
242 243
    DecompositionType decompType = decompositionType(c);
    if (decompType == DecompositionFont || decompType == DecompositionCompat)
eseidel's avatar
eseidel committed
244 245 246 247 248 249
        return false;

    return true;
}

static inline bool isValidNamePart(UChar32 c)
darin's avatar
darin committed
250
{
eseidel's avatar
eseidel committed
251 252 253 254 255 256 257 258 259 260 261 262 263
    // rules (a), (e), and (i) above
    if (isValidNameStart(c))
        return true;

    // rules (g) and (h) above
    if (c == 0x00B7 || c == 0x0387)
        return true;

    // rule (j) above
    if (c == '-' || c == '.')
        return true;

    // rules (b) and (f) above
darin's avatar
darin committed
264 265
    const uint32_t otherNamePartMask = Mark_NonSpacing | Mark_Enclosing | Mark_SpacingCombining | Letter_Modifier | Number_DecimalDigit;
    if (!(Unicode::category(c) & otherNamePartMask))
eseidel's avatar
eseidel committed
266 267 268 269
        return false;

    // rule (c) above
    if (c >= 0xF900 && c < 0xFFFE)
darin's avatar
darin committed
270
        return false;
eseidel's avatar
eseidel committed
271 272

    // rule (d) above
darin's avatar
darin committed
273 274
    DecompositionType decompType = decompositionType(c);
    if (decompType == DecompositionFont || decompType == DecompositionCompat)
eseidel's avatar
eseidel committed
275 276
        return false;

darin's avatar
darin committed
277 278 279
    return true;
}

ggaren's avatar
ggaren committed
280
static Widget* widgetForNode(Node* focusedNode)
darin@apple.com's avatar
darin@apple.com committed
281
{
ggaren's avatar
ggaren committed
282 283 284 285 286
    if (!focusedNode)
        return 0;
    RenderObject* renderer = focusedNode->renderer();
    if (!renderer || !renderer->isWidget())
        return 0;
287
    return toRenderWidget(renderer)->widget();
ggaren's avatar
ggaren committed
288 289 290 291
}

static bool acceptsEditingFocus(Node *node)
{
ggaren's avatar
ggaren committed
292 293
    ASSERT(node);
    ASSERT(node->isContentEditable());
ggaren's avatar
ggaren committed
294 295 296 297 298 299 300 301 302

    Node *root = node->rootEditableElement();
    Frame* frame = node->document()->frame();
    if (!frame || !root)
        return false;

    return frame->editor()->shouldBeginEditing(rangeOfContents(root).get());
}

adele@apple.com's avatar
adele@apple.com committed
303 304
static bool disableRangeMutation(Page* page)
{
305
#if PLATFORM(MAC)
adele@apple.com's avatar
adele@apple.com committed
306 307 308
    // Disable Range mutation on document modifications in Tiger and Leopard Mail
    // See <rdar://problem/5865171>
    return page && (page->settings()->needsLeopardMailQuirks() || page->settings()->needsTigerMailQuirks());
309 310 311
#else
    return false;
#endif
adele@apple.com's avatar
adele@apple.com committed
312 313
}

314
static HashSet<Document*>* documentsThatNeedStyleRecalc = 0;
315

weinig@apple.com's avatar
weinig@apple.com committed
316
Document::Document(Frame* frame, bool isXHTML)
darin's avatar
darin committed
317
    : ContainerNode(0)
darin's avatar
darin committed
318
    , m_domtree_version(0)
319
    , m_styleSheets(StyleSheetList::create(this))
320
    , m_styleRecalcTimer(this, &Document::styleRecalcTimerFired)
darin@apple.com's avatar
darin@apple.com committed
321
    , m_frameElementsShouldIgnoreScrolling(false)
darin's avatar
darin committed
322 323
    , m_title("")
    , m_titleSetExplicitly(false)
darin's avatar
darin committed
324
    , m_updateFocusAppearanceTimer(this, &Document::updateFocusAppearanceTimerFired)
mitz@apple.com's avatar
mitz@apple.com committed
325
    , m_executeScriptSoonTimer(this, &Document::executeScriptSoonTimerFired)
ap's avatar
ap committed
326 327
    , m_xmlVersion("1.0")
    , m_xmlStandalone(false)
mjs's avatar
mjs committed
328
#if ENABLE(XBL)
ap's avatar
ap committed
329
    , m_bindingManager(new XBLBindingManager(this))
330
#endif
adele's avatar
adele committed
331 332 333
    , m_savedRenderer(0)
    , m_secureForms(0)
    , m_designMode(inherit)
334
    , m_selfOnlyRefCount(0)
mjs's avatar
mjs committed
335
#if ENABLE(SVG)
darin's avatar
darin committed
336 337
    , m_svgExtensions(0)
#endif
ddkilzer@apple.com's avatar
ddkilzer@apple.com committed
338
#if ENABLE(DASHBOARD_SUPPORT)
rjw's avatar
rjw committed
339
    , m_hasDashboardRegions(false)
rjw's avatar
rjw committed
340
    , m_dashboardRegionsDirty(false)
ddkilzer@apple.com's avatar
ddkilzer@apple.com committed
341
#endif
342 343 344
    , m_accessKeyMapValid(false)
    , m_createRenderers(true)
    , m_inPageCache(false)
beidson's avatar
beidson committed
345
    , m_useSecureKeyboardEntryWhenActive(false)
346
    , m_isXHTML(isXHTML)
weinig@apple.com's avatar
weinig@apple.com committed
347
    , m_numNodeListCaches(0)
mjs@apple.com's avatar
mjs@apple.com committed
348 349 350
#if ENABLE(DATABASE)
    , m_hasOpenDatabases(false)
#endif
ddkilzer@apple.com's avatar
ddkilzer@apple.com committed
351
    , m_usingGeolocation(false)
352 353 354
#if ENABLE(WML)
    , m_containsWMLContent(false)
#endif
kocienda's avatar
kocienda committed
355
{
356
    m_document = this;
357

358 359
    m_pageGroupUserSheetCacheValid = false;

360
    m_printing = false;
adele@apple.com's avatar
adele@apple.com committed
361 362
    
    m_ignoreAutofocus = false;
kocienda's avatar
kocienda committed
363

mjs's avatar
mjs committed
364
    m_frame = frame;
365
    m_renderArena = 0;
366

367
    m_axObjectCache = 0;
368
    
eric@webkit.org's avatar
eric@webkit.org committed
369
    m_docLoader = new DocLoader(this);
kocienda's avatar
kocienda committed
370 371

    visuallyOrdered = false;
372
    m_bParsing = false;
kocienda's avatar
kocienda committed
373
    m_tokenizer = 0;
ap's avatar
ap committed
374
    m_wellFormed = false;
375

376 377
    setParseMode(Strict);

378
    m_textColor = Color::black;
kocienda's avatar
kocienda committed
379
    m_listenerTypes = 0;
380
    m_inDocument = true;
381
    m_inStyleRecalc = false;
382
    m_closeAfterStyleRecalc = false;
hyatt@apple.com's avatar
hyatt@apple.com committed
383

384
    m_usesDescendantRules = false;
385
    m_usesSiblingRules = false;
386
    m_usesFirstLineRules = false;
weinig's avatar
weinig committed
387
    m_usesFirstLetterRules = false;
388
    m_usesBeforeAfterRules = false;
hyatt@apple.com's avatar
hyatt@apple.com committed
389 390
    m_usesRemUnits = false;

thatcher's avatar
thatcher committed
391
    m_gotoAnchorNeededAfterStylesheetsLoad = false;
392 393
 
    m_styleSelector = 0;
ddkilzer's avatar
ddkilzer committed
394
    m_didCalculateStyleSelector = false;
hyatt's avatar
hyatt committed
395
    m_pendingStylesheets = 0;
mjs's avatar
mjs committed
396
    m_ignorePendingStylesheets = false;
antti's avatar
antti committed
397
    m_hasNodesWithPlaceholderStyle = false;
398
    m_pendingSheetLayout = NoLayoutWithPendingSheets;
399 400

    m_cssTarget = 0;
401

402 403 404 405
    resetLinkColor();
    resetVisitedLinkColor();
    resetActiveLinkColor();

406
    m_processingLoadEvent = false;
darin's avatar
darin committed
407
    m_startTime = currentTime();
408
    m_overMinimumLayoutThreshold = false;
kocienda's avatar
kocienda committed
409
    
abarth@webkit.org's avatar
abarth@webkit.org committed
410
    initSecurityContext();
collinj@webkit.org's avatar
 
collinj@webkit.org committed
411
    initDNSPrefetch();
weinig's avatar
weinig committed
412

413 414
    static int docID = 0;
    m_docID = docID++;
415 416 417
#if ENABLE(XHTMLMP)
    m_shouldProcessNoScriptElement = settings() && !settings()->isJavaScriptEnabled();
#endif
kocienda's avatar
kocienda committed
418 419
}

darin's avatar
darin committed
420
void Document::removedLastRef()
mjs's avatar
mjs committed
421
{
darin's avatar
darin committed
422
    ASSERT(!m_deletionHasBegun);
mjs's avatar
mjs committed
423
    if (m_selfOnlyRefCount) {
darin's avatar
darin committed
424
        // If removing a child removes the last self-only ref, we don't
mjs's avatar
mjs committed
425 426
        // want the document to be destructed until after
        // removeAllChildren returns, so we guard ourselves with an
darin's avatar
darin committed
427
        // extra self-only ref.
428
        selfOnlyRef();
mjs's avatar
mjs committed
429

darin's avatar
darin committed
430 431
        // We must make sure not to be retaining any of our children through
        // these extra pointers or we will create a reference cycle.
mjs's avatar
mjs committed
432
        m_docType = 0;
ggaren's avatar
ggaren committed
433
        m_focusedNode = 0;
mjs's avatar
mjs committed
434 435 436
        m_hoverNode = 0;
        m_activeNode = 0;
        m_titleElement = 0;
thatcher's avatar
thatcher committed
437
        m_documentElement = 0;
mjs's avatar
mjs committed
438 439

        removeAllChildren();
mjs's avatar
mjs committed
440

darin's avatar
darin committed
441
        deleteAllValues(m_markers);
mjs's avatar
mjs committed
442
        m_markers.clear();
mjs's avatar
mjs committed
443 444 445

        delete m_tokenizer;
        m_tokenizer = 0;
darin's avatar
darin committed
446

447 448
        m_cssCanvasElements.clear();

darin's avatar
darin committed
449 450 451
#ifndef NDEBUG
        m_inRemovedLastRefFunction = false;
#endif
452 453

        selfOnlyDeref();
harrison's avatar
harrison committed
454 455
    } else {
#ifndef NDEBUG
darin's avatar
darin committed
456
        m_deletionHasBegun = true;
harrison's avatar
harrison committed
457
#endif
mjs's avatar
mjs committed
458
        delete this;
harrison's avatar
harrison committed
459
    }
mjs's avatar
mjs committed
460 461
}

darin's avatar
darin committed
462
Document::~Document()
kocienda's avatar
kocienda committed
463
{
464 465 466
    ASSERT(!renderer());
    ASSERT(!m_inPageCache);
    ASSERT(!m_savedRenderer);
darin@apple.com's avatar
darin@apple.com committed
467
    ASSERT(m_ranges.isEmpty());
468
    ASSERT(!m_styleRecalcTimer.isActive());
469

mitz@apple.com's avatar
mitz@apple.com committed
470 471 472
    for (size_t i = 0; i < m_scriptsToExecuteSoon.size(); ++i)
        m_scriptsToExecuteSoon[i].first->element()->deref(); // Balances ref() in executeScriptSoon().

473
    removeAllEventListeners();
darin's avatar
darin committed
474

475
#if USE(JSC)
darin@apple.com's avatar
darin@apple.com committed
476
    forgetAllDOMNodesForDocument(this);
477
#endif
mjs's avatar
mjs committed
478

darin's avatar
darin committed
479
    delete m_tokenizer;
480
    m_document = 0;
kocienda's avatar
kocienda committed
481 482
    delete m_styleSelector;
    delete m_docLoader;
adele's avatar
adele committed
483
    
darin's avatar
darin committed
484
    if (m_renderArena) {
485 486 487
        delete m_renderArena;
        m_renderArena = 0;
    }
488

mjs's avatar
mjs committed
489
#if ENABLE(XBL)
490 491 492
    delete m_bindingManager;
#endif

darin's avatar
darin committed
493 494
    deleteAllValues(m_markers);

harrison@apple.com's avatar
harrison@apple.com committed
495 496
    clearAXObjectCache();

eseidel's avatar
eseidel committed
497
    m_decoder = 0;
kocienda's avatar
kocienda committed
498
    
andersca's avatar
andersca committed
499 500 501
    unsigned count = sizeof(m_nameCollectionInfo) / sizeof(m_nameCollectionInfo[0]);
    for (unsigned i = 0; i < count; i++)
        deleteAllValues(m_nameCollectionInfo[i]);
502

503
#if ENABLE(DATABASE)
beidson's avatar
beidson committed
504
    if (m_databaseThread) {
beidson@apple.com's avatar
beidson@apple.com committed
505
        ASSERT(m_databaseThread->terminationRequested());
beidson's avatar
beidson committed
506 507
        m_databaseThread = 0;
    }
508
#endif
beidson's avatar
beidson committed
509

510 511
    if (m_styleSheets)
        m_styleSheets->documentDestroyed();
kocienda's avatar
kocienda committed
512 513
}

darin's avatar
darin committed
514
void Document::resetLinkColor()
515
{
516
    m_linkColor = Color(0, 0, 238);
517 518
}

darin's avatar
darin committed
519
void Document::resetVisitedLinkColor()
520
{
521
    m_visitedLinkColor = Color(85, 26, 139);    
522 523
}

darin's avatar
darin committed
524
void Document::resetActiveLinkColor()
525
{
526
    m_activeLinkColor.setNamedColor("red");
527
}
528

darin's avatar
darin committed
529
void Document::setDocType(PassRefPtr<DocumentType> docType)
530
{
531 532 533 534 535
    // This should never be called more than once.
    // Note: This is not a public DOM method and can only be called by the parser.
    ASSERT(!m_docType || !docType);
    if (m_docType && docType)
        return;
536
    m_docType = docType;
537 538 539
    if (m_docType)
        m_docType->setDocument(this);
    determineParseMode();
kocienda's avatar
kocienda committed
540 541
}

darin's avatar
darin committed
542
DOMImplementation* Document::implementation() const
kocienda's avatar
kocienda committed
543
{
weinig@apple.com's avatar
weinig@apple.com committed
544 545
    if (!m_implementation)
        m_implementation = DOMImplementation::create();
darin's avatar
darin committed
546
    return m_implementation.get();
kocienda's avatar
kocienda committed
547 548
}

549
void Document::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
thatcher's avatar
thatcher committed
550
{
551 552 553
    ContainerNode::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
    
    // Invalidate the document element we have cached in case it was replaced.
thatcher's avatar
thatcher committed
554 555 556
    m_documentElement = 0;
}

557
void Document::cacheDocumentElement() const
kocienda's avatar
kocienda committed
558
{
559 560 561 562 563
    ASSERT(!m_documentElement);
    Node* n = firstChild();
    while (n && !n->isElementNode())
        n = n->nextSibling();
    m_documentElement = static_cast<Element*>(n);
kocienda's avatar
kocienda committed
564 565
}

566
PassRefPtr<Element> Document::createElement(const AtomicString& name, ExceptionCode& ec)
kocienda's avatar
kocienda committed
567
{
eric@webkit.org's avatar
eric@webkit.org committed
568 569 570 571 572 573
    if (!isValidName(name)) {
        ec = INVALID_CHARACTER_ERR;
        return 0;
    }

    if (m_isXHTML)
574
        return HTMLElementFactory::createHTMLElement(QualifiedName(nullAtom, name, xhtmlNamespaceURI), this, 0, false);
kmccullo's avatar
kmccullo committed
575

jchaffraix@webkit.org's avatar
jchaffraix@webkit.org committed
576
    return createElement(QualifiedName(nullAtom, name, nullAtom), false);
kocienda's avatar
kocienda committed
577 578
}

darin's avatar
darin committed
579
PassRefPtr<DocumentFragment> Document::createDocumentFragment()
580
{
581
    return DocumentFragment::create(document());
582 583
}

darin@apple.com's avatar
darin@apple.com committed
584
PassRefPtr<Text> Document::createTextNode(const String& data)
585
{
586
    return Text::create(this, data);
587 588
}

darin@apple.com's avatar
darin@apple.com committed
589
PassRefPtr<Comment> Document::createComment(const String& data)
590
{
591
    return Comment::create(this, data);
592 593
}

darin@apple.com's avatar
darin@apple.com committed
594
PassRefPtr<CDATASection> Document::createCDATASection(const String& data, ExceptionCode& ec)
595
{
darin's avatar
darin committed
596
    if (isHTMLDocument()) {
darin's avatar
darin committed
597
        ec = NOT_SUPPORTED_ERR;
darin's avatar
darin committed
598
        return 0;
darin's avatar
darin committed
599
    }
600
    return CDATASection::create(this, data);
601 602
}

darin@apple.com's avatar
darin@apple.com committed
603
PassRefPtr<ProcessingInstruction> Document::createProcessingInstruction(const String& target, const String& data, ExceptionCode& ec)
604
{
darin's avatar
darin committed
605
    if (!isValidName(target)) {
darin's avatar
darin committed
606
        ec = INVALID_CHARACTER_ERR;
607
        return 0;
darin's avatar
darin committed
608 609
    }
    if (isHTMLDocument()) {
darin's avatar
darin committed
610
        ec = NOT_SUPPORTED_ERR;
611
        return 0;
darin's avatar
darin committed
612
    }
613
    return ProcessingInstruction::create(this, target, data);
614 615
}

darin@apple.com's avatar
darin@apple.com committed
616
PassRefPtr<EntityReference> Document::createEntityReference(const String& name, ExceptionCode& ec)
617
{
darin's avatar
darin committed
618
    if (!isValidName(name)) {
darin's avatar
darin committed
619
        ec = INVALID_CHARACTER_ERR;
darin's avatar
darin committed
620
        return 0;
darin's avatar
darin committed
621 622
    }
    if (isHTMLDocument()) {
darin's avatar
darin committed
623
        ec = NOT_SUPPORTED_ERR;
darin's avatar
darin committed
624
        return 0;
darin's avatar
darin committed
625
    }
626
    return EntityReference::create(this, name);
627 628
}

darin@apple.com's avatar
darin@apple.com committed
629
PassRefPtr<EditingText> Document::createEditingTextNode(const String& text)
kocienda's avatar
kocienda committed
630
{
631
    return EditingText::create(this, text);
kocienda's avatar
kocienda committed
632 633
}

darin's avatar
darin committed
634
PassRefPtr<CSSStyleDeclaration> Document::createCSSStyleDeclaration()
kocienda's avatar
kocienda committed
635
{
darin@apple.com's avatar
darin@apple.com committed
636
    return CSSMutableStyleDeclaration::create();
kocienda's avatar
kocienda committed
637 638
}

darin's avatar
darin committed
639
PassRefPtr<Node> Document::importNode(Node* importedNode, bool deep, ExceptionCode& ec)
640
{
darin's avatar
darin committed
641
    ec = 0;
ggaren's avatar
ggaren committed
642
    
oliver's avatar
oliver committed
643
    if (!importedNode
ddkilzer@apple.com's avatar
ddkilzer@apple.com committed
644
#if ENABLE(SVG) && ENABLE(DASHBOARD_SUPPORT)
oliver's avatar
oliver committed
645 646 647
        || (importedNode->isSVGElement() && page() && page()->settings()->usesDashboardBackwardCompatibilityMode())
#endif
        ) {
ggaren's avatar
ggaren committed
648 649 650
        ec = NOT_SUPPORTED_ERR;
        return 0;
    }
darin's avatar
darin committed
651

darin's avatar
darin committed
652
    switch (importedNode->nodeType()) {
darin's avatar
darin committed
653
        case TEXT_NODE:
darin's avatar
darin committed
654
            return createTextNode(importedNode->nodeValue());
darin's avatar
darin committed
655 656 657 658 659 660 661
        case CDATA_SECTION_NODE:
            return createCDATASection(importedNode->nodeValue(), ec);
        case ENTITY_REFERENCE_NODE:
            return createEntityReference(importedNode->nodeName(), ec);
        case PROCESSING_INSTRUCTION_NODE:
            return createProcessingInstruction(importedNode->nodeName(), importedNode->nodeValue(), ec);
        case COMMENT_NODE:
darin's avatar
darin committed
662
            return createComment(importedNode->nodeValue());
darin's avatar
darin committed
663
        case ELEMENT_NODE: {
ap's avatar
ap committed
664
            Element* oldElement = static_cast<Element*>(importedNode);
mjs's avatar
mjs committed
665
            RefPtr<Element> newElement = createElementNS(oldElement->namespaceURI(), oldElement->tagQName().toString(), ec);
darin's avatar
darin committed
666
                        
ap's avatar
ap committed
667
            if (ec)
darin's avatar
darin committed
668
                return 0;
darin's avatar
darin committed
669

670
            NamedNodeMap* attrs = oldElement->attributes(true);
darin's avatar
darin committed
671 672 673
            if (attrs) {
                unsigned length = attrs->length();
                for (unsigned i = 0; i < length; i++) {
darin's avatar
darin committed
674
                    Attribute* attr = attrs->attributeItem(i);
darin's avatar
darin committed
675
                    newElement->setAttribute(attr->name(), attr->value().impl(), ec);
ap's avatar
ap committed
676
                    if (ec)
darin's avatar
darin committed
677 678 679 680
                        return 0;
                }
            }

681 682
            newElement->copyNonAttributeProperties(oldElement);

darin's avatar
darin committed
683
            if (deep) {
darin's avatar
darin committed
684 685
                for (Node* oldChild = oldElement->firstChild(); oldChild; oldChild = oldChild->nextSibling()) {
                    RefPtr<Node> newChild = importNode(oldChild, true, ec);
ap's avatar
ap committed
686
                    if (ec)
darin's avatar
darin committed
687
                        return 0;
darin's avatar
darin committed
688
                    newElement->appendChild(newChild.release(), ec);
ap's avatar
ap committed
689
                    if (ec)
darin's avatar
darin committed
690 691 692 693
                        return 0;
                }
            }

darin's avatar
darin committed
694
            return newElement.release();
darin's avatar
darin committed
695
        }
696 697
        case ATTRIBUTE_NODE:
            return Attr::create(0, this, static_cast<Attr*>(importedNode)->attr()->clone());
ap's avatar
ap committed
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
        case DOCUMENT_FRAGMENT_NODE: {
            DocumentFragment* oldFragment = static_cast<DocumentFragment*>(importedNode);
            RefPtr<DocumentFragment> newFragment = createDocumentFragment();
            if (deep) {
                for (Node* oldChild = oldFragment->firstChild(); oldChild; oldChild = oldChild->nextSibling()) {
                    RefPtr<Node> newChild = importNode(oldChild, true, ec);
                    if (ec)
                        return 0;
                    newFragment->appendChild(newChild.release(), ec);
                    if (ec)
                        return 0;
                }
            }
            
            return newFragment.release();
        }
darin's avatar
darin committed
714
        case ENTITY_NODE:
ap's avatar
ap committed
715 716 717
        case NOTATION_NODE:
            // FIXME: It should be possible to import these node types, however in DOM3 the DocumentType is readonly, so there isn't much sense in doing that.
            // Ability to add these imported nodes to a DocumentType will be considered for addition to a future release of the DOM.
darin's avatar
darin committed
718 719
        case DOCUMENT_NODE:
        case DOCUMENT_TYPE_NODE:
720
        case XPATH_NAMESPACE_NODE:
darin's avatar
darin committed
721
            break;
darin's avatar
darin committed
722 723
    }

darin's avatar
darin committed
724
    ec = NOT_SUPPORTED_ERR;
darin's avatar
darin committed
725
    return 0;
726 727
}

728

darin's avatar
darin committed
729
PassRefPtr<Node> Document::adoptNode(PassRefPtr<Node> source, ExceptionCode& ec)
730
{
ggaren's avatar
ggaren committed
731 732
    if (!source) {
        ec = NOT_SUPPORTED_ERR;
733
        return 0;
ggaren's avatar
ggaren committed
734
    }
eric@webkit.org's avatar
eric@webkit.org committed
735 736 737 738 739 740

    if (source->isReadOnlyNode()) {
        ec = NO_MODIFICATION_ALLOWED_ERR;
        return 0;
    }

741
    switch (source->nodeType()) {
darin's avatar
darin committed