/* * Copyright (C) 1999 Lars Knoll (knoll@kde.org) * (C) 1999 Antti Koivisto (koivisto@kde.org) * (C) 2001 Dirk Mueller (mueller@kde.org) * (C) 2006 Alexey Proskuryakov (ap@webkit.org) * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2011, 2012 Apple Inc. All rights reserved. * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/) * Copyright (C) 2008, 2009, 2011, 2012 Google Inc. All rights reserved. * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies) * Copyright (C) Research In Motion Limited 2010-2011. All rights reserved. * * 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 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #include "sky/engine/core/dom/Document.h" #include "gen/sky/core/EventNames.h" #include "gen/sky/platform/RuntimeEnabledFeatures.h" #include "sky/engine/bindings/exception_messages.h" #include "sky/engine/bindings/exception_state.h" #include "sky/engine/bindings/exception_state_placeholder.h" #include "sky/engine/core/css/CSSFontSelector.h" #include "sky/engine/core/css/CSSStyleDeclaration.h" #include "sky/engine/core/css/CSSStyleSheet.h" #include "sky/engine/core/css/MediaQueryMatcher.h" #include "sky/engine/core/css/StylePropertySet.h" #include "sky/engine/core/css/StyleSheetContents.h" #include "sky/engine/core/css/parser/BisonCSSParser.h" #include "sky/engine/core/css/resolver/FontBuilder.h" #include "sky/engine/core/css/resolver/StyleResolver.h" #include "sky/engine/core/css/resolver/StyleResolverStats.h" #include "sky/engine/core/dom/Attr.h" #include "sky/engine/core/dom/DocumentFragment.h" #include "sky/engine/core/dom/DocumentMarkerController.h" #include "sky/engine/core/dom/Element.h" #include "sky/engine/core/dom/ElementDataCache.h" #include "sky/engine/core/dom/ElementTraversal.h" #include "sky/engine/core/dom/ExceptionCode.h" #include "sky/engine/core/dom/MutationObserver.h" #include "sky/engine/core/dom/NodeRareData.h" #include "sky/engine/core/dom/NodeRenderStyle.h" #include "sky/engine/core/dom/NodeTraversal.h" #include "sky/engine/core/dom/NodeWithIndex.h" #include "sky/engine/core/dom/Range.h" #include "sky/engine/core/dom/RequestAnimationFrameCallback.h" #include "sky/engine/core/dom/ScriptedAnimationController.h" #include "sky/engine/core/dom/SelectorQuery.h" #include "sky/engine/core/dom/StaticNodeList.h" #include "sky/engine/core/dom/StyleEngine.h" #include "sky/engine/core/dom/Text.h" #include "sky/engine/core/editing/PositionWithAffinity.h" #include "sky/engine/core/events/Event.h" #include "sky/engine/core/frame/FrameHost.h" #include "sky/engine/core/frame/FrameView.h" #include "sky/engine/core/frame/LocalDOMWindow.h" #include "sky/engine/core/frame/LocalFrame.h" #include "sky/engine/core/frame/Settings.h" #include "sky/engine/core/inspector/ConsoleMessage.h" #include "sky/engine/core/inspector/InspectorCounters.h" #include "sky/engine/core/loader/FrameLoaderClient.h" #include "sky/engine/core/page/ChromeClient.h" #include "sky/engine/core/page/Page.h" #include "sky/engine/core/painting/Picture.h" #include "sky/engine/core/rendering/HitTestResult.h" #include "sky/engine/core/rendering/RenderView.h" #include "sky/engine/platform/DateComponents.h" #include "sky/engine/platform/EventDispatchForbiddenScope.h" #include "sky/engine/platform/Language.h" #include "sky/engine/platform/Logging.h" #include "sky/engine/platform/ScriptForbiddenScope.h" #include "sky/engine/platform/TraceEvent.h" #include "sky/engine/platform/text/SegmentedString.h" #include "sky/engine/public/platform/Platform.h" #include "sky/engine/wtf/CurrentTime.h" #include "sky/engine/wtf/DateMath.h" #include "sky/engine/wtf/HashFunctions.h" #include "sky/engine/wtf/MainThread.h" #include "sky/engine/wtf/StdLibExtras.h" #include "sky/engine/wtf/TemporaryChange.h" #include "sky/engine/wtf/text/StringBuffer.h" using namespace WTF; using namespace Unicode; namespace blink { // 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 const uint32_t nameStartMask = Letter_Lowercase | Letter_Uppercase | Letter_Other | Letter_Titlecase | Number_Letter; if (!(Unicode::category(c) & nameStartMask)) return false; // rule (c) above if (c >= 0xF900 && c < 0xFFFE) return false; // rule (d) above DecompositionType decompType = decompositionType(c); if (decompType == DecompositionFont || decompType == DecompositionCompat) return false; return true; } static inline bool isValidNamePart(UChar32 c) { // 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 const uint32_t otherNamePartMask = Mark_NonSpacing | Mark_Enclosing | Mark_SpacingCombining | Letter_Modifier | Number_DecimalDigit; if (!(Unicode::category(c) & otherNamePartMask)) return false; // rule (c) above if (c >= 0xF900 && c < 0xFFFE) return false; // rule (d) above DecompositionType decompType = decompositionType(c); if (decompType == DecompositionFont || decompType == DecompositionCompat) return false; return true; } #ifndef NDEBUG typedef HashSet > WeakDocumentSet; static WeakDocumentSet& liveDocumentSet() { DEFINE_STATIC_LOCAL(OwnPtr, set, (adoptPtr(new WeakDocumentSet()))); return *set; } #endif Document::Document(const DocumentInit& initializer) : ContainerNode(0, CreateDocument) , TreeScope(*this) , m_active(false) , m_visualUpdatePending(true) , m_inStyleRecalc(false) , m_stopped(false) , m_module(nullptr) , m_evaluateMediaQueriesOnStyleRecalc(false) , m_frame(initializer.frame()) , m_domWindow(m_frame ? m_frame->domWindow() : 0) , m_activeParserCount(0) , m_listenerTypes(0) , m_mutationObserverTypes(0) , m_readyState(Complete) , m_isParsing(false) , m_containsValidityStyleRules(false) , m_markers(adoptPtr(new DocumentMarkerController)) , m_loadEventProgress(LoadEventNotRun) , m_startTime(currentTime()) , m_renderView(0) #if !ENABLE(OILPAN) , m_weakFactory(this) #endif , m_didSetReferrerPolicy(false) , m_referrerPolicy(ReferrerPolicyDefault) , m_hasViewportUnits(false) , m_styleRecalcElementCounter(0) , m_frameView(nullptr) { // We depend on the url getting immediately set in subframes, but we // also depend on the url NOT getting immediately set in opened windows. // See fast/dom/early-frame-url.html // and fast/dom/location-new-window-no-crash.html, respectively. // FIXME: Can/should we unify this behavior? if (initializer.shouldSetURL()) setURL(initializer.url()); InspectorCounters::incrementCounter(InspectorCounters::DocumentCounter); #ifndef NDEBUG liveDocumentSet().add(this); #endif } Document::~Document() { ASSERT(!renderView()); ASSERT(!parentTreeScope()); ASSERT(m_ranges.isEmpty()); ASSERT(!hasGuardRefCount()); if (m_elemSheet) m_elemSheet->clearOwnerNode(); // We must call clearRareData() here since a Document class inherits TreeScope // as well as Node. See a comment on TreeScope.h for the reason. if (hasRareData()) clearRareData(); #ifndef NDEBUG liveDocumentSet().remove(this); #endif InspectorCounters::decrementCounter(InspectorCounters::DocumentCounter); } #if !ENABLE(OILPAN) void Document::dispose() { ASSERT_WITH_SECURITY_IMPLICATION(!m_deletionHasBegun); // We must make sure not to be retaining any of our children through // these extra pointers or we will create a reference cycle. m_hoverNode = nullptr; m_activeHoverElement = nullptr; m_userActionElements.documentDidRemoveLastRef(); detachParser(); // removeDetachedChildren() doesn't always unregister IDs, // so tear down scope information upfront to avoid having stale references in the map. destroyTreeScopeData(); removeDetachedChildren(); m_markers->clear(); if (m_scriptedAnimationController) m_scriptedAnimationController->clearDocumentPointer(); m_scriptedAnimationController.clear(); } #endif SelectorQueryCache& Document::selectorQueryCache() { if (!m_selectorQueryCache) m_selectorQueryCache = adoptPtr(new SelectorQueryCache()); return *m_selectorQueryCache; } MediaQueryMatcher& Document::mediaQueryMatcher() { if (!m_mediaQueryMatcher) m_mediaQueryMatcher = MediaQueryMatcher::create(*this); return *m_mediaQueryMatcher; } void Document::mediaQueryAffectingValueChanged() { m_evaluateMediaQueriesOnStyleRecalc = true; // FIXME(sky): Actually update media queries from