blob: fa4278952a6736afb10589c37e53c10c84e43678 [file] [log] [blame]
/*
* 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 "core/dom/Document.h"
#include "bindings/core/v8/CustomElementConstructorBuilder.h"
#include "bindings/core/v8/DOMDataStore.h"
#include "bindings/core/v8/ExceptionMessages.h"
#include "bindings/core/v8/ExceptionState.h"
#include "bindings/core/v8/ExceptionStatePlaceholder.h"
#include "bindings/core/v8/ScriptCallStack.h"
#include "bindings/core/v8/ScriptController.h"
#include "bindings/core/v8/UnionTypesCore.h"
#include "bindings/core/v8/V8DOMWrapper.h"
#include "bindings/core/v8/V8PerIsolateData.h"
#include "bindings/core/v8/WindowProxy.h"
#include "core/HTMLElementFactory.h"
#include "core/HTMLNames.h"
#include "core/SVGElementFactory.h"
#include "core/SVGNames.h"
#include "core/XMLNSNames.h"
#include "core/XMLNames.h"
#include "core/animation/AnimationTimeline.h"
#include "core/animation/DocumentAnimations.h"
#include "core/css/CSSFontSelector.h"
#include "core/css/CSSStyleDeclaration.h"
#include "core/css/CSSStyleSheet.h"
#include "core/css/MediaQueryMatcher.h"
#include "core/css/StylePropertySet.h"
#include "core/css/StyleSheetContents.h"
#include "core/css/StyleSheetList.h"
#include "core/css/invalidation/StyleInvalidator.h"
#include "core/css/parser/CSSParser.h"
#include "core/css/resolver/FontBuilder.h"
#include "core/css/resolver/StyleResolver.h"
#include "core/css/resolver/StyleResolverStats.h"
#include "core/dom/AXObjectCache.h"
#include "core/dom/AddConsoleMessageTask.h"
#include "core/dom/Attr.h"
#include "core/dom/CDATASection.h"
#include "core/dom/ClientRect.h"
#include "core/dom/Comment.h"
#include "core/dom/ContextFeatures.h"
#include "core/dom/DOMImplementation.h"
#include "core/dom/DocumentFragment.h"
#include "core/dom/DocumentLifecycleObserver.h"
#include "core/dom/DocumentType.h"
#include "core/dom/Element.h"
#include "core/dom/ElementDataCache.h"
#include "core/dom/ElementRegistrationOptions.h"
#include "core/dom/ElementTraversal.h"
#include "core/dom/ExceptionCode.h"
#include "core/dom/ExecutionContextTask.h"
#include "core/dom/FrameRequestCallback.h"
#include "core/dom/IntersectionObserverController.h"
#include "core/dom/LayoutTreeBuilderTraversal.h"
#include "core/dom/MainThreadTaskRunner.h"
#include "core/dom/Microtask.h"
#include "core/dom/MutationObserver.h"
#include "core/dom/NodeChildRemovalTracker.h"
#include "core/dom/NodeComputedStyle.h"
#include "core/dom/NodeFilter.h"
#include "core/dom/NodeIntersectionObserverData.h"
#include "core/dom/NodeIterator.h"
#include "core/dom/NodeRareData.h"
#include "core/dom/NodeTraversal.h"
#include "core/dom/NodeWithIndex.h"
#include "core/dom/NthIndexCache.h"
#include "core/dom/ProcessingInstruction.h"
#include "core/dom/ScriptRunner.h"
#include "core/dom/ScriptedAnimationController.h"
#include "core/dom/ScriptedIdleTaskController.h"
#include "core/dom/SelectorQuery.h"
#include "core/dom/StaticNodeList.h"
#include "core/dom/StyleEngine.h"
#include "core/dom/TouchList.h"
#include "core/dom/TransformSource.h"
#include "core/dom/TreeWalker.h"
#include "core/dom/VisitedLinkState.h"
#include "core/dom/XMLDocument.h"
#include "core/dom/custom/CustomElementMicrotaskRunQueue.h"
#include "core/dom/custom/CustomElementRegistrationContext.h"
#include "core/dom/shadow/ElementShadow.h"
#include "core/dom/shadow/FlatTreeTraversal.h"
#include "core/dom/shadow/ShadowRoot.h"
#include "core/editing/DragCaretController.h"
#include "core/editing/Editor.h"
#include "core/editing/FrameSelection.h"
#include "core/editing/InputMethodController.h"
#include "core/editing/markers/DocumentMarkerController.h"
#include "core/editing/serializers/Serialization.h"
#include "core/editing/spellcheck/SpellChecker.h"
#include "core/events/BeforeUnloadEvent.h"
#include "core/events/Event.h"
#include "core/events/EventFactory.h"
#include "core/events/EventListener.h"
#include "core/events/HashChangeEvent.h"
#include "core/events/PageTransitionEvent.h"
#include "core/events/ScopedEventQueue.h"
#include "core/fetch/ResourceFetcher.h"
#include "core/frame/DOMTimer.h"
#include "core/frame/EventHandlerRegistry.h"
#include "core/frame/FrameConsole.h"
#include "core/frame/FrameHost.h"
#include "core/frame/FrameView.h"
#include "core/frame/History.h"
#include "core/frame/LocalDOMWindow.h"
#include "core/frame/LocalFrame.h"
#include "core/frame/OriginsUsingFeatures.h"
#include "core/frame/Settings.h"
#include "core/frame/csp/ContentSecurityPolicy.h"
#include "core/html/DocumentNameCollection.h"
#include "core/html/HTMLAllCollection.h"
#include "core/html/HTMLAnchorElement.h"
#include "core/html/HTMLBaseElement.h"
#include "core/html/HTMLBodyElement.h"
#include "core/html/HTMLCanvasElement.h"
#include "core/html/HTMLCollection.h"
#include "core/html/HTMLDialogElement.h"
#include "core/html/HTMLDocument.h"
#include "core/html/HTMLFrameOwnerElement.h"
#include "core/html/HTMLHeadElement.h"
#include "core/html/HTMLHtmlElement.h"
#include "core/html/HTMLIFrameElement.h"
#include "core/html/HTMLInputElement.h"
#include "core/html/HTMLLinkElement.h"
#include "core/html/HTMLMetaElement.h"
#include "core/html/HTMLScriptElement.h"
#include "core/html/HTMLStyleElement.h"
#include "core/html/HTMLTemplateElement.h"
#include "core/html/HTMLTitleElement.h"
#include "core/html/PluginDocument.h"
#include "core/html/WindowNameCollection.h"
#include "core/html/canvas/CanvasContextCreationAttributes.h"
#include "core/html/canvas/CanvasFontCache.h"
#include "core/html/canvas/CanvasRenderingContext.h"
#include "core/html/forms/FormController.h"
#include "core/html/imports/HTMLImportLoader.h"
#include "core/html/imports/HTMLImportsController.h"
#include "core/html/parser/HTMLDocumentParser.h"
#include "core/html/parser/HTMLParserIdioms.h"
#include "core/html/parser/NestingLevelIncrementer.h"
#include "core/html/parser/TextResourceDecoder.h"
#include "core/input/EventHandler.h"
#include "core/inspector/ConsoleMessage.h"
#include "core/inspector/InspectorInstrumentation.h"
#include "core/inspector/InspectorTraceEvents.h"
#include "core/inspector/InstanceCounters.h"
#include "core/layout/HitTestResult.h"
#include "core/layout/LayoutPart.h"
#include "core/layout/LayoutView.h"
#include "core/layout/TextAutosizer.h"
#include "core/layout/compositing/PaintLayerCompositor.h"
#include "core/loader/CookieJar.h"
#include "core/loader/DocumentLoader.h"
#include "core/loader/FrameFetchContext.h"
#include "core/loader/FrameLoader.h"
#include "core/loader/FrameLoaderClient.h"
#include "core/loader/ImageLoader.h"
#include "core/loader/NavigationScheduler.h"
#include "core/loader/appcache/ApplicationCacheHost.h"
#include "core/page/ChromeClient.h"
#include "core/page/EventWithHitTestResults.h"
#include "core/page/FocusController.h"
#include "core/page/FrameTree.h"
#include "core/page/Page.h"
#include "core/page/PointerLockController.h"
#include "core/page/scrolling/ScrollingCoordinator.h"
#include "core/svg/SVGDocumentExtensions.h"
#include "core/svg/SVGTitleElement.h"
#include "core/svg/SVGUseElement.h"
#include "core/timing/DOMWindowPerformance.h"
#include "core/timing/Performance.h"
#include "core/workers/SharedWorkerRepositoryClient.h"
#include "core/xml/parser/XMLDocumentParser.h"
#include "platform/DateComponents.h"
#include "platform/EventDispatchForbiddenScope.h"
#include "platform/Histogram.h"
#include "platform/Language.h"
#include "platform/LengthFunctions.h"
#include "platform/Logging.h"
#include "platform/PluginScriptForbiddenScope.h"
#include "platform/RuntimeEnabledFeatures.h"
#include "platform/ScriptForbiddenScope.h"
#include "platform/TraceEvent.h"
#include "platform/network/ContentSecurityPolicyParsers.h"
#include "platform/network/HTTPParsers.h"
#include "platform/scheduler/CancellableTaskFactory.h"
#include "platform/scroll/ScrollbarTheme.h"
#include "platform/text/PlatformLocale.h"
#include "platform/text/SegmentedString.h"
#include "platform/weborigin/OriginAccessEntry.h"
#include "platform/weborigin/SchemeRegistry.h"
#include "platform/weborigin/SecurityOrigin.h"
#include "public/platform/Platform.h"
#include "public/platform/WebFrameScheduler.h"
#include "public/platform/WebScheduler.h"
#include "wtf/CurrentTime.h"
#include "wtf/DateMath.h"
#include "wtf/Functional.h"
#include "wtf/HashFunctions.h"
#include "wtf/MainThread.h"
#include "wtf/StdLibExtras.h"
#include "wtf/TemporaryChange.h"
#include "wtf/text/StringBuffer.h"
#include "wtf/text/TextEncodingRegistry.h"
using namespace WTF;
using namespace Unicode;
namespace blink {
using namespace HTMLNames;
static const unsigned cMaxWriteRecursionDepth = 21;
// 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. :)
static const int cLayoutScheduleThreshold = 250;
// 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
CharDecompositionType 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
CharDecompositionType decompType = decompositionType(c);
if (decompType == DecompositionFont || decompType == DecompositionCompat)
return false;
return true;
}
static bool shouldInheritSecurityOriginFromOwner(const KURL& url)
{
// http://www.whatwg.org/specs/web-apps/current-work/#origin-0
//
// If a Document has the address "about:blank"
// The origin of the Document is the origin it was assigned when its browsing context was created.
//
// Note: We generalize this to all "blank" URLs and invalid URLs because we
// treat all of these URLs as about:blank.
//
return url.isEmpty() || url.protocolIsAbout();
}
static Widget* widgetForElement(const Element& focusedElement)
{
LayoutObject* layoutObject = focusedElement.layoutObject();
if (!layoutObject || !layoutObject->isLayoutPart())
return 0;
return toLayoutPart(layoutObject)->widget();
}
static bool acceptsEditingFocus(const Element& element)
{
ASSERT(element.hasEditableStyle());
return element.document().frame() && element.rootEditableElement();
}
static bool isOriginPotentiallyTrustworthy(SecurityOrigin* origin, String* errorMessage)
{
if (errorMessage)
return origin->isPotentiallyTrustworthy(*errorMessage);
return origin->isPotentiallyTrustworthy();
}
uint64_t Document::s_globalTreeVersion = 0;
static bool s_threadedParsingEnabledForTesting = true;
Document::WeakDocumentSet& Document::liveDocumentSet()
{
DEFINE_STATIC_LOCAL(OwnPtrWillBePersistent<WeakDocumentSet>, set, (adoptPtrWillBeNoop(new WeakDocumentSet())));
return *set;
}
// This class doesn't work with non-Document ExecutionContext.
class AutofocusTask final : public ExecutionContextTask {
public:
static PassOwnPtr<AutofocusTask> create()
{
return adoptPtr(new AutofocusTask());
}
~AutofocusTask() override { }
private:
AutofocusTask() { }
void performTask(ExecutionContext* context) override
{
Document* document = toDocument(context);
if (RefPtrWillBeRawPtr<Element> element = document->autofocusElement()) {
document->setAutofocusElement(0);
element->focus();
}
}
};
Document::Document(const DocumentInit& initializer, DocumentClassFlags documentClasses)
: ContainerNode(0, CreateDocument)
, TreeScope(*this)
, m_hasNodesWithPlaceholderStyle(false)
, m_evaluateMediaQueriesOnStyleRecalc(false)
, m_pendingSheetLayout(NoLayoutWithPendingSheets)
, m_frame(initializer.frame())
, m_domWindow(m_frame ? m_frame->localDOMWindow() : 0)
, m_importsController(initializer.importsController())
, m_activeParserCount(0)
, m_contextFeatures(ContextFeatures::defaultSwitch())
, m_wellFormed(false)
, m_printing(false)
, m_wasPrinting(false)
, m_paginatedForScreen(false)
, m_compatibilityMode(NoQuirksMode)
, m_compatibilityModeLocked(false)
, m_executeScriptsWaitingForResourcesTask(CancellableTaskFactory::create(this, &Document::executeScriptsWaitingForResources))
, m_hasAutofocused(false)
, m_clearFocusedElementTimer(this, &Document::clearFocusedElementTimerFired)
, m_domTreeVersion(++s_globalTreeVersion)
, m_styleVersion(0)
, m_listenerTypes(0)
, m_mutationObserverTypes(0)
, m_visitedLinkState(VisitedLinkState::create(*this))
, m_visuallyOrdered(false)
, m_readyState(Complete)
, m_parsingState(FinishedParsing)
, m_gotoAnchorNeededAfterStylesheetsLoad(false)
, m_containsValidityStyleRules(false)
, m_containsPlugins(false)
, m_updateFocusAppearanceSelectionBahavior(SelectionBehaviorOnFocus::Reset)
, m_ignoreDestructiveWriteCount(0)
, m_markers(adoptPtrWillBeNoop(new DocumentMarkerController))
, m_updateFocusAppearanceTimer(this, &Document::updateFocusAppearanceTimerFired)
, m_cssTarget(nullptr)
, m_loadEventProgress(LoadEventNotRun)
, m_startTime(currentTime())
, m_scriptRunner(ScriptRunner::create(this))
, m_xmlVersion("1.0")
, m_xmlStandalone(StandaloneUnspecified)
, m_hasXMLDeclaration(0)
, m_designMode(false)
, m_isRunningExecCommand(false)
, m_hasAnnotatedRegions(false)
, m_annotatedRegionsDirty(false)
, m_useSecureKeyboardEntryWhenActive(false)
, m_documentClasses(documentClasses)
, m_isViewSource(false)
, m_sawElementsInKnownNamespaces(false)
, m_isSrcdocDocument(false)
, m_isMobileDocument(false)
, m_layoutView(0)
#if !ENABLE(OILPAN)
, m_weakFactory(this)
#endif
, m_contextDocument(initializer.contextDocument())
, m_hasFullscreenSupplement(false)
, m_loadEventDelayCount(0)
, m_loadEventDelayTimer(this, &Document::loadEventDelayTimerFired)
, m_pluginLoadingTimer(this, &Document::pluginLoadingTimerFired)
, m_documentTiming(*this)
, m_writeRecursionIsTooDeep(false)
, m_writeRecursionDepth(0)
, m_taskRunner(MainThreadTaskRunner::create(this))
, m_registrationContext(initializer.registrationContext(this))
, m_elementDataCacheClearTimer(this, &Document::elementDataCacheClearTimerFired)
, m_timeline(AnimationTimeline::create(this))
, m_templateDocumentHost(nullptr)
, m_didAssociateFormControlsTimer(this, &Document::didAssociateFormControlsTimerFired)
, m_timers(timerTaskRunner()->adoptClone())
, m_hasViewportUnits(false)
, m_parserSyncPolicy(AllowAsynchronousParsing)
, m_nodeCount(0)
{
if (m_frame) {
ASSERT(m_frame->page());
provideContextFeaturesToDocumentFrom(*this, *m_frame->page());
m_fetcher = m_frame->loader().documentLoader()->fetcher();
FrameFetchContext::provideDocumentToContext(m_fetcher->context(), this);
} else if (m_importsController) {
m_fetcher = FrameFetchContext::createContextAndFetcher(nullptr);
FrameFetchContext::provideDocumentToContext(m_fetcher->context(), this);
} else {
m_fetcher = ResourceFetcher::create(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());
initSecurityContext(initializer);
initDNSPrefetch();
#if !ENABLE(OILPAN)
for (unsigned i = 0; i < WTF_ARRAY_LENGTH(m_nodeListCounts); ++i)
m_nodeListCounts[i] = 0;
#endif
InstanceCounters::incrementCounter(InstanceCounters::DocumentCounter);
m_lifecycle.advanceTo(DocumentLifecycle::Inactive);
// Since CSSFontSelector requires Document::m_fetcher and StyleEngine owns
// CSSFontSelector, need to initialize m_styleEngine after initializing
// m_fetcher.
m_styleEngine = StyleEngine::create(*this);
// The parent's parser should be suspended together with all the other objects,
// else this new Document would have a new ExecutionContext which suspended state
// would not match the one from the parent, and could start loading resources
// ignoring the defersLoading flag.
ASSERT(!parentDocument() || !parentDocument()->activeDOMObjectsAreSuspended());
liveDocumentSet().add(this);
}
Document::~Document()
{
ASSERT(!layoutView());
ASSERT(!parentTreeScope());
// If a top document with a cache, verify that it was comprehensively
// cleared during detach.
ASSERT(!m_axObjectCache);
#if !ENABLE(OILPAN)
ASSERT(m_ranges.isEmpty());
ASSERT(!hasGuardRefCount());
ASSERT(!m_importsController);
// With Oilpan, either the document outlives the visibility observers
// or the visibility observers and the document die in the same GC round.
// When they die in the same GC round, the list of visibility observers
// will not be empty on Document destruction.
ASSERT(m_visibilityObservers.isEmpty());
if (m_templateDocument)
m_templateDocument->m_templateDocumentHost = nullptr; // balanced in ensureTemplateDocument().
m_scriptRunner.clear();
// FIXME: Oilpan: Not removing event listeners here also means that we do
// not notify the inspector instrumentation that the event listeners are
// gone. The Document and all the nodes in the document are gone, so maybe
// that is OK?
removeAllEventListenersRecursively();
// Currently we believe that Document can never outlive the parser.
// Although the Document may be replaced synchronously, DocumentParsers
// generally keep at least one reference to an Element which would in turn
// has a reference to the Document. If you hit this ASSERT, then that
// assumption is wrong. DocumentParser::detach() should ensure that even
// if the DocumentParser outlives the Document it won't cause badness.
ASSERT(!m_parser || m_parser->refCount() == 1);
detachParser();
if (m_styleSheetList)
m_styleSheetList->detachFromDocument();
m_timeline->detachFromDocument();
// We need to destroy CSSFontSelector before destroying m_fetcher.
m_styleEngine->detachFromDocument();
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();
ASSERT(m_listsInvalidatedAtDocument.isEmpty());
for (unsigned i = 0; i < WTF_ARRAY_LENGTH(m_nodeListCounts); ++i)
ASSERT(!m_nodeListCounts[i]);
liveDocumentSet().remove(this);
#endif
InstanceCounters::decrementCounter(InstanceCounters::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_docType = nullptr;
m_focusedElement = nullptr;
m_hoverNode = nullptr;
m_activeHoverElement = nullptr;
m_titleElement = nullptr;
m_documentElement = nullptr;
m_contextFeatures = ContextFeatures::defaultSwitch();
m_userActionElements.documentDidRemoveLastRef();
m_associatedFormControls.clear();
m_scriptRunner->dispose();
detachParser();
m_registrationContext.clear();
// removeDetachedChildren() doesn't always unregister IDs,
// so tear down scope information upfront to avoid having stale references in the map.
destroyTreeScopeData();
removeDetachedChildren();
// removeDetachedChildren() can access FormController.
m_formController.clear();
m_markers->clear();
// FIXME: consider using ActiveDOMObject.
if (m_scriptedAnimationController)
m_scriptedAnimationController->clearDocumentPointer();
m_scriptedAnimationController.clear();
m_scriptedIdleTaskController.clear();
if (svgExtensions())
accessSVGExtensions().pauseAnimations();
if (m_intersectionObserverData)
m_intersectionObserverData->dispose();
m_lifecycle.advanceTo(DocumentLifecycle::Disposed);
DocumentLifecycleNotifier::notifyDocumentWasDisposed();
m_canvasFontCache.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()
{
styleEngine().resolverChanged(FullStyleUpdate);
m_evaluateMediaQueriesOnStyleRecalc = true;
styleEngine().clearMediaQueryRuleSetStyleSheets();
InspectorInstrumentation::mediaQueryResultChanged(this);
}
void Document::setCompatibilityMode(CompatibilityMode mode)
{
if (m_compatibilityModeLocked || mode == m_compatibilityMode)
return;
m_compatibilityMode = mode;
selectorQueryCache().invalidate();
}
String Document::compatMode() const
{
return inQuirksMode() ? "BackCompat" : "CSS1Compat";
}
void Document::setDoctype(PassRefPtrWillBeRawPtr<DocumentType> docType)
{
// This should never be called more than once.
ASSERT(!m_docType || !docType);
m_docType = docType;
if (m_docType) {
this->adoptIfNeeded(*m_docType);
if (m_docType->publicId().startsWith("-//wapforum//dtd xhtml mobile 1.", TextCaseInsensitive))
m_isMobileDocument = true;
}
// Doctype affects the interpretation of the stylesheets.
styleEngine().clearResolver();
}
DOMImplementation& Document::implementation()
{
if (!m_implementation)
m_implementation = DOMImplementation::create(*this);
return *m_implementation;
}
bool Document::hasAppCacheManifest() const
{
return isHTMLHtmlElement(documentElement()) && documentElement()->hasAttribute(manifestAttr);
}
Location* Document::location() const
{
if (!frame())
return 0;
return domWindow()->location();
}
void Document::childrenChanged(const ChildrenChange& change)
{
ContainerNode::childrenChanged(change);
m_documentElement = ElementTraversal::firstWithin(*this);
}
AtomicString Document::convertLocalName(const AtomicString& name)
{
return isHTMLDocument() ? name.lower() : name;
}
PassRefPtrWillBeRawPtr<Element> Document::createElement(const AtomicString& name, ExceptionState& exceptionState)
{
if (!isValidName(name)) {
exceptionState.throwDOMException(InvalidCharacterError, "The tag name provided ('" + name + "') is not a valid name.");
return nullptr;
}
if (isXHTMLDocument() || isHTMLDocument())
return HTMLElementFactory::createHTMLElement(convertLocalName(name), *this, 0, false);
return Element::create(QualifiedName(nullAtom, name, nullAtom), this);
}
PassRefPtrWillBeRawPtr<Element> Document::createElement(const AtomicString& localName, const AtomicString& typeExtension, ExceptionState& exceptionState)
{
if (!isValidName(localName)) {
exceptionState.throwDOMException(InvalidCharacterError, "The tag name provided ('" + localName + "') is not a valid name.");
return nullptr;
}
RefPtrWillBeRawPtr<Element> element;
if (CustomElement::isValidName(localName) && registrationContext()) {
element = registrationContext()->createCustomTagElement(*this, QualifiedName(nullAtom, convertLocalName(localName), xhtmlNamespaceURI));
} else {
element = createElement(localName, exceptionState);
if (exceptionState.hadException())
return nullptr;
}
if (!typeExtension.isEmpty())
CustomElementRegistrationContext::setIsAttributeAndTypeExtension(element.get(), typeExtension);
return element.release();
}
static inline QualifiedName createQualifiedName(const AtomicString& namespaceURI, const AtomicString& qualifiedName, ExceptionState& exceptionState)
{
AtomicString prefix, localName;
if (!Document::parseQualifiedName(qualifiedName, prefix, localName, exceptionState))
return QualifiedName::null();
QualifiedName qName(prefix, localName, namespaceURI);
if (!Document::hasValidNamespaceForElements(qName)) {
exceptionState.throwDOMException(NamespaceError, "The namespace URI provided ('" + namespaceURI + "') is not valid for the qualified name provided ('" + qualifiedName + "').");
return QualifiedName::null();
}
return qName;
}
PassRefPtrWillBeRawPtr<Element> Document::createElementNS(const AtomicString& namespaceURI, const AtomicString& qualifiedName, ExceptionState& exceptionState)
{
QualifiedName qName(createQualifiedName(namespaceURI, qualifiedName, exceptionState));
if (qName == QualifiedName::null())
return nullptr;
return createElement(qName, false);
}
PassRefPtrWillBeRawPtr<Element> Document::createElementNS(const AtomicString& namespaceURI, const AtomicString& qualifiedName, const AtomicString& typeExtension, ExceptionState& exceptionState)
{
QualifiedName qName(createQualifiedName(namespaceURI, qualifiedName, exceptionState));
if (qName == QualifiedName::null())
return nullptr;
RefPtrWillBeRawPtr<Element> element;
if (CustomElement::isValidName(qName.localName()) && registrationContext())
element = registrationContext()->createCustomTagElement(*this, qName);
else
element = createElement(qName, false);
if (!typeExtension.isEmpty())
CustomElementRegistrationContext::setIsAttributeAndTypeExtension(element.get(), typeExtension);
return element.release();
}
ScriptValue Document::registerElement(ScriptState* scriptState, const AtomicString& name, const ElementRegistrationOptions& options, ExceptionState& exceptionState, CustomElement::NameSet validNames)
{
OriginsUsingFeatures::countMainWorldOnly(scriptState, *this, OriginsUsingFeatures::Feature::DocumentRegisterElement);
if (!registrationContext()) {
exceptionState.throwDOMException(NotSupportedError, "No element registration context is available.");
return ScriptValue();
}
CustomElementConstructorBuilder constructorBuilder(scriptState, options);
registrationContext()->registerElement(this, &constructorBuilder, name, validNames, exceptionState);
return constructorBuilder.bindingsReturnValue();
}
CustomElementMicrotaskRunQueue* Document::customElementMicrotaskRunQueue()
{
if (!m_customElementMicrotaskRunQueue)
m_customElementMicrotaskRunQueue = CustomElementMicrotaskRunQueue::create();
return m_customElementMicrotaskRunQueue.get();
}
void Document::setImportsController(HTMLImportsController* controller)
{
ASSERT(!m_importsController || !controller);
m_importsController = controller;
if (!m_importsController && !loader())
m_fetcher->clearContext();
}
HTMLImportLoader* Document::importLoader() const
{
if (!m_importsController)
return 0;
return m_importsController->loaderFor(*this);
}
bool Document::haveImportsLoaded() const
{
if (!m_importsController)
return true;
return !m_importsController->shouldBlockScriptExecution(*this);
}
LocalDOMWindow* Document::executingWindow()
{
if (LocalDOMWindow* owningWindow = domWindow())
return owningWindow;
if (HTMLImportsController* import = this->importsController())
return import->master()->domWindow();
return 0;
}
LocalFrame* Document::executingFrame()
{
LocalDOMWindow* window = executingWindow();
if (!window)
return 0;
return window->frame();
}
PassRefPtrWillBeRawPtr<DocumentFragment> Document::createDocumentFragment()
{
return DocumentFragment::create(*this);
}
PassRefPtrWillBeRawPtr<Text> Document::createTextNode(const String& data)
{
return Text::create(*this, data);
}
PassRefPtrWillBeRawPtr<Comment> Document::createComment(const String& data)
{
return Comment::create(*this, data);
}
PassRefPtrWillBeRawPtr<CDATASection> Document::createCDATASection(const String& data, ExceptionState& exceptionState)
{
if (isHTMLDocument()) {
exceptionState.throwDOMException(NotSupportedError, "This operation is not supported for HTML documents.");
return nullptr;
}
if (data.contains("]]>")) {
exceptionState.throwDOMException(InvalidCharacterError, "String cannot contain ']]>' since that is the end delimiter of a CData section.");
return nullptr;
}
return CDATASection::create(*this, data);
}
PassRefPtrWillBeRawPtr<ProcessingInstruction> Document::createProcessingInstruction(const String& target, const String& data, ExceptionState& exceptionState)
{
if (!isValidName(target)) {
exceptionState.throwDOMException(InvalidCharacterError, "The target provided ('" + target + "') is not a valid name.");
return nullptr;
}
if (data.contains("?>")) {
exceptionState.throwDOMException(InvalidCharacterError, "The data provided ('" + data + "') contains '?>'.");
return nullptr;
}
return ProcessingInstruction::create(*this, target, data);
}
PassRefPtrWillBeRawPtr<Text> Document::createEditingTextNode(const String& text)
{
return Text::createEditingText(*this, text);
}
bool Document::importContainerNodeChildren(ContainerNode* oldContainerNode, PassRefPtrWillBeRawPtr<ContainerNode> newContainerNode, ExceptionState& exceptionState)
{
for (Node& oldChild : NodeTraversal::childrenOf(*oldContainerNode)) {
RefPtrWillBeRawPtr<Node> newChild = importNode(&oldChild, true, exceptionState);
if (exceptionState.hadException())
return false;
newContainerNode->appendChild(newChild.release(), exceptionState);
if (exceptionState.hadException())
return false;
}
return true;
}
PassRefPtrWillBeRawPtr<Node> Document::importNode(Node* importedNode, bool deep, ExceptionState& exceptionState)
{
switch (importedNode->nodeType()) {
case TEXT_NODE:
return createTextNode(importedNode->nodeValue());
case CDATA_SECTION_NODE:
return CDATASection::create(*this, importedNode->nodeValue());
case PROCESSING_INSTRUCTION_NODE:
return createProcessingInstruction(importedNode->nodeName(), importedNode->nodeValue(), exceptionState);
case COMMENT_NODE:
return createComment(importedNode->nodeValue());
case DOCUMENT_TYPE_NODE: {
DocumentType* doctype = toDocumentType(importedNode);
return DocumentType::create(this, doctype->name(), doctype->publicId(), doctype->systemId());
}
case ELEMENT_NODE: {
Element* oldElement = toElement(importedNode);
// FIXME: The following check might be unnecessary. Is it possible that
// oldElement has mismatched prefix/namespace?
if (!hasValidNamespaceForElements(oldElement->tagQName())) {
exceptionState.throwDOMException(NamespaceError, "The imported node has an invalid namespace.");
return nullptr;
}
RefPtrWillBeRawPtr<Element> newElement = createElement(oldElement->tagQName(), false);
newElement->cloneDataFromElement(*oldElement);
if (deep) {
if (!importContainerNodeChildren(oldElement, newElement, exceptionState))
return nullptr;
if (isHTMLTemplateElement(*oldElement)
&& !ensureTemplateDocument().importContainerNodeChildren(
toHTMLTemplateElement(oldElement)->content(),
toHTMLTemplateElement(newElement)->content(), exceptionState))
return nullptr;
}
return newElement.release();
}
case ATTRIBUTE_NODE:
return Attr::create(*this, QualifiedName(nullAtom, AtomicString(toAttr(importedNode)->name()), nullAtom), toAttr(importedNode)->value());
case DOCUMENT_FRAGMENT_NODE: {
if (importedNode->isShadowRoot()) {
// ShadowRoot nodes should not be explicitly importable.
// Either they are imported along with their host node, or created implicitly.
exceptionState.throwDOMException(NotSupportedError, "The node provided is a shadow root, which may not be imported.");
return nullptr;
}
DocumentFragment* oldFragment = toDocumentFragment(importedNode);
RefPtrWillBeRawPtr<DocumentFragment> newFragment = createDocumentFragment();
if (deep && !importContainerNodeChildren(oldFragment, newFragment, exceptionState))
return nullptr;
return newFragment.release();
}
case DOCUMENT_NODE:
exceptionState.throwDOMException(NotSupportedError, "The node provided is a document, which may not be imported.");
return nullptr;
}
ASSERT_NOT_REACHED();
return nullptr;
}
PassRefPtrWillBeRawPtr<Node> Document::adoptNode(PassRefPtrWillBeRawPtr<Node> source, ExceptionState& exceptionState)
{
EventQueueScope scope;
switch (source->nodeType()) {
case DOCUMENT_NODE:
exceptionState.throwDOMException(NotSupportedError, "The node provided is of type '" + source->nodeName() + "', which may not be adopted.");
return nullptr;
case ATTRIBUTE_NODE: {
Attr* attr = toAttr(source.get());
if (RefPtrWillBeRawPtr<Element> ownerElement = attr->ownerElement())
ownerElement->removeAttributeNode(attr, exceptionState);
break;
}
default:
if (source->isShadowRoot()) {
// ShadowRoot cannot disconnect itself from the host node.
exceptionState.throwDOMException(HierarchyRequestError, "The node provided is a shadow root, which may not be adopted.");
return nullptr;
}
if (source->isFrameOwnerElement()) {
HTMLFrameOwnerElement* frameOwnerElement = toHTMLFrameOwnerElement(source.get());
if (frame() && frame()->tree().isDescendantOf(frameOwnerElement->contentFrame())) {
exceptionState.throwDOMException(HierarchyRequestError, "The node provided is a frame which contains this document.");
return nullptr;
}
}
if (source->parentNode()) {
source->parentNode()->removeChild(source.get(), exceptionState);
if (exceptionState.hadException())
return nullptr;
RELEASE_ASSERT(!source->parentNode());
}
}
this->adoptIfNeeded(*source);
return source;
}
bool Document::hasValidNamespaceForElements(const QualifiedName& qName)
{
// These checks are from DOM Core Level 2, createElementNS
// http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-DocCrElNS
if (!qName.prefix().isEmpty() && qName.namespaceURI().isNull()) // createElementNS(null, "html:div")
return false;
if (qName.prefix() == xmlAtom && qName.namespaceURI() != XMLNames::xmlNamespaceURI) // createElementNS("http://www.example.com", "xml:lang")
return false;
// Required by DOM Level 3 Core and unspecified by DOM Level 2 Core:
// http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#ID-DocCrElNS
// createElementNS("http://www.w3.org/2000/xmlns/", "foo:bar"), createElementNS(null, "xmlns:bar"), createElementNS(null, "xmlns")
if (qName.prefix() == xmlnsAtom || (qName.prefix().isEmpty() && qName.localName() == xmlnsAtom))
return qName.namespaceURI() == XMLNSNames::xmlnsNamespaceURI;
return qName.namespaceURI() != XMLNSNames::xmlnsNamespaceURI;
}
bool Document::hasValidNamespaceForAttributes(const QualifiedName& qName)
{
return hasValidNamespaceForElements(qName);
}
// FIXME: This should really be in a possible ElementFactory class
PassRefPtrWillBeRawPtr<Element> Document::createElement(const QualifiedName& qName, bool createdByParser)
{
RefPtrWillBeRawPtr<Element> e = nullptr;
// FIXME: Use registered namespaces and look up in a hash to find the right factory.
if (qName.namespaceURI() == xhtmlNamespaceURI)
e = HTMLElementFactory::createHTMLElement(qName.localName(), *this, 0, createdByParser);
else if (qName.namespaceURI() == SVGNames::svgNamespaceURI)
e = SVGElementFactory::createSVGElement(qName.localName(), *this, createdByParser);
if (e)
m_sawElementsInKnownNamespaces = true;
else
e = Element::create(qName, this);
if (e->prefix() != qName.prefix())
e->setTagNameForCreateElementNS(qName);
ASSERT(qName == e->tagQName());
return e.release();
}
String Document::readyState() const
{
DEFINE_STATIC_LOCAL(const String, loading, ("loading"));
DEFINE_STATIC_LOCAL(const String, interactive, ("interactive"));
DEFINE_STATIC_LOCAL(const String, complete, ("complete"));
switch (m_readyState) {
case Loading:
return loading;
case Interactive:
return interactive;
case Complete:
return complete;
}
ASSERT_NOT_REACHED();
return String();
}
void Document::setReadyState(ReadyState readyState)
{
if (readyState == m_readyState)
return;
switch (readyState) {
case Loading:
if (!m_documentTiming.domLoading()) {
m_documentTiming.markDomLoading();
}
break;
case Interactive:
if (!m_documentTiming.domInteractive())
m_documentTiming.markDomInteractive();
break;
case Complete:
if (!m_documentTiming.domComplete())
m_documentTiming.markDomComplete();
break;
}
m_readyState = readyState;
dispatchEvent(Event::create(EventTypeNames::readystatechange));
}
bool Document::isLoadCompleted()
{
return m_readyState == Complete;
}
AtomicString Document::encodingName() const
{
// TextEncoding::name() returns a char*, no need to allocate a new
// String for it each time.
// FIXME: We should fix TextEncoding to speak AtomicString anyway.
return AtomicString(encoding().name());
}
void Document::setContentLanguage(const AtomicString& language)
{
if (m_contentLanguage == language)
return;
m_contentLanguage = language;
// Document's style depends on the content language.
setNeedsStyleRecalc(SubtreeStyleChange, StyleChangeReasonForTracing::create(StyleChangeReason::Language));
}
void Document::setXMLVersion(const String& version, ExceptionState& exceptionState)
{
if (!XMLDocumentParser::supportsXMLVersion(version)) {
exceptionState.throwDOMException(NotSupportedError, "This document does not support the XML version '" + version + "'.");
return;
}
m_xmlVersion = version;
}
void Document::setXMLStandalone(bool standalone, ExceptionState& exceptionState)
{
m_xmlStandalone = standalone ? Standalone : NotStandalone;
}
void Document::setContent(const String& content)
{
open();
m_parser->append(content);
close();
}
String Document::suggestedMIMEType() const
{
if (isXMLDocument()) {
if (isXHTMLDocument())
return "application/xhtml+xml";
if (isSVGDocument())
return "image/svg+xml";
return "application/xml";
}
if (xmlStandalone())
return "text/xml";
if (isHTMLDocument())
return "text/html";
if (DocumentLoader* documentLoader = loader())
return documentLoader->responseMIMEType();
return String();
}
void Document::setMimeType(const AtomicString& mimeType)
{
m_mimeType = mimeType;
}
AtomicString Document::contentType() const
{
if (!m_mimeType.isEmpty())
return m_mimeType;
if (DocumentLoader* documentLoader = loader())
return documentLoader->mimeType();
String mimeType = suggestedMIMEType();
if (!mimeType.isEmpty())
return AtomicString(mimeType);
return AtomicString("application/xml");
}
Element* Document::elementFromPoint(int x, int y) const
{
if (!layoutView())
return 0;
return TreeScope::elementFromPoint(x, y);
}
WillBeHeapVector<RawPtrWillBeMember<Element>> Document::elementsFromPoint(int x, int y) const
{
if (!layoutView())
return WillBeHeapVector<RawPtrWillBeMember<Element>>();
return TreeScope::elementsFromPoint(x, y);
}
PassRefPtrWillBeRawPtr<Range> Document::caretRangeFromPoint(int x, int y)
{
if (!layoutView())
return nullptr;
HitTestResult result = hitTestInDocument(this, x, y);
PositionWithAffinity positionWithAffinity = result.position();
if (positionWithAffinity.position().isNull())
return nullptr;
Position rangeCompliantPosition = positionWithAffinity.position().parentAnchoredEquivalent();
return Range::createAdjustedToTreeScope(*this, rangeCompliantPosition);
}
Element* Document::scrollingElement()
{
if (RuntimeEnabledFeatures::scrollTopLeftInteropEnabled()) {
if (inQuirksMode()) {
updateLayoutTreeIfNeeded();
HTMLBodyElement* body = firstBodyElement();
if (body && body->layoutObject() && body->layoutObject()->hasOverflowClip())
return nullptr;
return body;
}
return documentElement();
}
return body();
}
/*
* Performs three operations:
* 1. Convert control characters to spaces
* 2. Trim leading and trailing spaces
* 3. Collapse internal whitespace.
*/
template <typename CharacterType>
static inline String canonicalizedTitle(Document* document, const String& title)
{
unsigned length = title.length();
unsigned builderIndex = 0;
const CharacterType* characters = title.getCharacters<CharacterType>();
StringBuffer<CharacterType> buffer(length);
// Replace control characters with spaces and collapse whitespace.
bool pendingWhitespace = false;
for (unsigned i = 0; i < length; ++i) {
UChar32 c = characters[i];
if (c <= 0x20 || c == 0x7F || (WTF::Unicode::category(c) & (WTF::Unicode::Separator_Line | WTF::Unicode::Separator_Paragraph))) {
if (builderIndex != 0)
pendingWhitespace = true;
} else {
if (pendingWhitespace) {
buffer[builderIndex++] = ' ';
pendingWhitespace = false;
}
buffer[builderIndex++] = c;
}
}
buffer.shrink(builderIndex);
return String::adopt(buffer);
}
void Document::updateTitle(const String& title)
{
if (m_rawTitle == title)
return;
m_rawTitle = title;
String oldTitle = m_title;
if (m_rawTitle.isEmpty())
m_title = String();
else if (m_rawTitle.is8Bit())
m_title = canonicalizedTitle<LChar>(this, m_rawTitle);
else
m_title = canonicalizedTitle<UChar>(this, m_rawTitle);
if (!m_frame || oldTitle == m_title)
return;
m_frame->loader().client()->dispatchDidReceiveTitle(m_title);
}
void Document::setTitle(const String& title)
{
// Title set by JavaScript -- overrides any title elements.
if (!isHTMLDocument() && !isXHTMLDocument()) {
m_titleElement = nullptr;
} else if (!m_titleElement) {
HTMLElement* headElement = head();
if (!headElement)
return;
m_titleElement = HTMLTitleElement::create(*this);
headElement->appendChild(m_titleElement.get());
}
if (isHTMLTitleElement(m_titleElement))
toHTMLTitleElement(m_titleElement)->setText(title);
else
updateTitle(title);
}
void Document::setTitleElement(Element* titleElement)
{
// Only allow the first title element to change the title -- others have no effect.
if (m_titleElement && m_titleElement != titleElement) {
if (isHTMLDocument() || isXHTMLDocument()) {
m_titleElement = Traversal<HTMLTitleElement>::firstWithin(*this);
} else if (isSVGDocument()) {
m_titleElement = Traversal<SVGTitleElement>::firstWithin(*this);
}
} else {
m_titleElement = titleElement;
}
if (isHTMLTitleElement(m_titleElement))
updateTitle(toHTMLTitleElement(m_titleElement)->text());
else if (isSVGTitleElement(m_titleElement))
updateTitle(toSVGTitleElement(m_titleElement)->textContent());
}
void Document::removeTitle(Element* titleElement)
{
if (m_titleElement != titleElement)
return;
m_titleElement = nullptr;
// Update title based on first title element in the document, if one exists.
if (isHTMLDocument() || isXHTMLDocument()) {
if (HTMLTitleElement* title = Traversal<HTMLTitleElement>::firstWithin(*this))
setTitleElement(title);
} else if (isSVGDocument()) {
if (SVGTitleElement* title = Traversal<SVGTitleElement>::firstWithin(*this))
setTitleElement(title);
}
if (!m_titleElement)
updateTitle(String());
}
const AtomicString& Document::dir()
{
Element* rootElement = documentElement();
if (isHTMLHtmlElement(rootElement))
return toHTMLHtmlElement(rootElement)->dir();
return nullAtom;
}
void Document::setDir(const AtomicString& value)
{
Element* rootElement = documentElement();
if (isHTMLHtmlElement(rootElement))
toHTMLHtmlElement(rootElement)->setDir(value);
}
PageVisibilityState Document::pageVisibilityState() const
{
// The visibility of the document is inherited from the visibility of the
// page. If there is no page associated with the document, we will assume
// that the page is hidden, as specified by the spec:
// http://dvcs.w3.org/hg/webperf/raw-file/tip/specs/PageVisibility/Overview.html#dom-document-hidden
if (!m_frame || !m_frame->page())
return PageVisibilityStateHidden;
return m_frame->page()->visibilityState();
}
String Document::visibilityState() const
{
return pageVisibilityStateString(pageVisibilityState());
}
bool Document::hidden() const
{
return pageVisibilityState() != PageVisibilityStateVisible;
}
void Document::didChangeVisibilityState()
{
dispatchEvent(Event::createBubble(EventTypeNames::visibilitychange));
// Also send out the deprecated version until it can be removed.
dispatchEvent(Event::createBubble(EventTypeNames::webkitvisibilitychange));
PageVisibilityState state = pageVisibilityState();
for (DocumentVisibilityObserver* observer : m_visibilityObservers)
observer->didChangeVisibilityState(state);
if (state == PageVisibilityStateVisible)
timeline().setAllCompositorPending();
if (hidden() && m_canvasFontCache)
m_canvasFontCache->pruneAll();
}
void Document::registerVisibilityObserver(DocumentVisibilityObserver* observer)
{
ASSERT(!m_visibilityObservers.contains(observer));
m_visibilityObservers.add(observer);
}
void Document::unregisterVisibilityObserver(DocumentVisibilityObserver* observer)
{
ASSERT(m_visibilityObservers.contains(observer));
m_visibilityObservers.remove(observer);
}
String Document::nodeName() const
{
return "#document";
}
Node::NodeType Document::nodeType() const
{
return DOCUMENT_NODE;
}
FormController& Document::formController()
{
if (!m_formController) {
m_formController = FormController::create();
if (m_frame && m_frame->loader().currentItem() && m_frame->loader().currentItem()->isCurrentDocument(this))
m_frame->loader().currentItem()->setDocumentState(m_formController->formElementsState());
}
return *m_formController;
}
DocumentState* Document::formElementsState() const
{
if (!m_formController)
return 0;
return m_formController->formElementsState();
}
void Document::setStateForNewFormElements(const Vector<String>& stateVector)
{
if (!stateVector.size() && !m_formController)
return;
formController().setStateForNewFormElements(stateVector);
}
FrameView* Document::view() const
{
return m_frame ? m_frame->view() : 0;
}
Page* Document::page() const
{
return m_frame ? m_frame->page() : 0;
}
FrameHost* Document::frameHost() const
{
return m_frame ? m_frame->host() : 0;
}
Settings* Document::settings() const
{
return m_frame ? m_frame->settings() : 0;
}
PassRefPtrWillBeRawPtr<Range> Document::createRange()
{
return Range::create(*this);
}
PassRefPtrWillBeRawPtr<NodeIterator> Document::createNodeIterator(Node* root, unsigned whatToShow, PassRefPtrWillBeRawPtr<NodeFilter> filter)
{
ASSERT(root);
return NodeIterator::create(root, whatToShow, filter);
}
PassRefPtrWillBeRawPtr<TreeWalker> Document::createTreeWalker(Node* root, unsigned whatToShow, PassRefPtrWillBeRawPtr<NodeFilter> filter)
{
ASSERT(root);
return TreeWalker::create(root, whatToShow, filter);
}
bool Document::needsLayoutTreeUpdate() const
{
if (!isActive() || !view())
return false;
if (needsFullLayoutTreeUpdate())
return true;
if (childNeedsStyleRecalc())
return true;
if (childNeedsStyleInvalidation())
return true;
if (layoutView()->wasNotifiedOfSubtreeChange())
return true;
return false;
}
bool Document::needsFullLayoutTreeUpdate() const
{
if (!isActive() || !view())
return false;
if (!m_useElementsNeedingUpdate.isEmpty())
return true;
if (!m_layerUpdateSVGFilterElements.isEmpty())
return true;
if (needsStyleRecalc())
return true;
if (needsStyleInvalidation())
return true;
// FIXME: The childNeedsDistributionRecalc bit means either self or children, we should fix that.
if (childNeedsDistributionRecalc())
return true;
if (DocumentAnimations::needsAnimationTimingUpdate(*this))
return true;
return false;
}
bool Document::shouldScheduleLayoutTreeUpdate() const
{
if (!isActive())
return false;
if (inStyleRecalc())
return false;
// InPreLayout will recalc style itself. There's no reason to schedule another recalc.
if (m_lifecycle.state() == DocumentLifecycle::InPreLayout)
return false;
if (!shouldScheduleLayout())
return false;
return true;
}
void Document::scheduleLayoutTreeUpdate()
{
ASSERT(!hasPendingStyleRecalc());
ASSERT(shouldScheduleLayoutTreeUpdate());
ASSERT(needsLayoutTreeUpdate());
if (!view()->canThrottleRendering())
page()->animator().scheduleVisualUpdate(frame());
m_lifecycle.ensureStateAtMost(DocumentLifecycle::VisualUpdatePending);
TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "ScheduleStyleRecalculation", TRACE_EVENT_SCOPE_THREAD, "data", InspectorRecalculateStylesEvent::data(frame()));
InspectorInstrumentation::didScheduleStyleRecalculation(this);
++m_styleVersion;
}
bool Document::hasPendingForcedStyleRecalc() const
{
return hasPendingStyleRecalc() && !inStyleRecalc() && styleChangeType() >= SubtreeStyleChange;
}
void Document::updateStyleInvalidationIfNeeded()
{
ScriptForbiddenScope forbidScript;
if (!isActive())
return;
if (!childNeedsStyleInvalidation())
return;
TRACE_EVENT0("blink", "Document::updateStyleInvalidationIfNeeded");
styleEngine().styleInvalidator().invalidate(*this);
}
void Document::setupFontBuilder(ComputedStyle& documentStyle)
{
FontBuilder fontBuilder(*this);
RefPtrWillBeRawPtr<CSSFontSelector> selector = styleEngine().fontSelector();
fontBuilder.createFontForDocument(selector, documentStyle);
}
void Document::inheritHtmlAndBodyElementStyles(StyleRecalcChange change)
{
ASSERT(inStyleRecalc());
ASSERT(documentElement());
bool didRecalcDocumentElement = false;
RefPtr<ComputedStyle> documentElementStyle = documentElement()->mutableComputedStyle();
if (change == Force)
documentElement()->clearAnimationStyleChange();
if (!documentElementStyle || documentElement()->needsStyleRecalc() || change == Force) {
documentElementStyle = ensureStyleResolver().styleForElement(documentElement());
didRecalcDocumentElement = true;
}
WritingMode rootWritingMode = documentElementStyle->writingMode();
TextDirection rootDirection = documentElementStyle->direction();
HTMLElement* body = this->body();
RefPtr<ComputedStyle> bodyStyle;
if (body) {
bodyStyle = body->mutableComputedStyle();
if (didRecalcDocumentElement)
body->clearAnimationStyleChange();
if (!bodyStyle || body->needsStyleRecalc() || didRecalcDocumentElement)
bodyStyle = ensureStyleResolver().styleForElement(body, documentElementStyle.get());
rootWritingMode = bodyStyle->writingMode();
rootDirection = bodyStyle->direction();
}
const ComputedStyle* backgroundStyle = documentElementStyle.get();
// http://www.w3.org/TR/css3-background/#body-background
// <html> root element with no background steals background from its first <body> child.
// Also see LayoutBoxModelObject::backgroundStolenForBeingBody()
if (isHTMLHtmlElement(documentElement()) && isHTMLBodyElement(body) && !backgroundStyle->hasBackground())
backgroundStyle = bodyStyle.get();
Color backgroundColor = backgroundStyle->visitedDependentColor(CSSPropertyBackgroundColor);
FillLayer backgroundLayers = backgroundStyle->backgroundLayers();
for (auto currentLayer = &backgroundLayers; currentLayer; currentLayer = currentLayer->next()) {
// http://www.w3.org/TR/css3-background/#root-background
// The root element background always have painting area of the whole canvas.
currentLayer->setClip(BorderFillBox);
// The root element doesn't scroll. It always propagates its layout overflow
// to the viewport. Positioning background against either box is equivalent to
// positioning against the scrolled box of the viewport.
if (currentLayer->attachment() == ScrollBackgroundAttachment)
currentLayer->setAttachment(LocalBackgroundAttachment);
}
EImageRendering imageRendering = backgroundStyle->imageRendering();
const ComputedStyle* overflowStyle = nullptr;
if (Element* element = viewportDefiningElement(documentElementStyle.get())) {
if (element == body) {
overflowStyle = bodyStyle.get();
} else {
ASSERT(element == documentElement());
overflowStyle = documentElementStyle.get();
// The body element has its own scrolling box, independent from the viewport.
// This is a bit of a weird edge case in the CSS spec that we might want to try to
// eliminate some day (eg. for ScrollTopLeftInterop - see http://crbug.com/157855).
if (bodyStyle && !bodyStyle->isOverflowVisible())
UseCounter::count(*this, UseCounter::BodyScrollsInAdditionToViewport);
}
}
// Resolved rem units are stored in the matched properties cache so we need to make sure to
// invalidate the cache if the documentElement needed to reattach or the font size changed
// and then trigger a full document recalc. We also need to clear it here since the
// call to styleForElement on the body above can cache bad values for rem units if the
// documentElement's style was dirty. We could keep track of which elements depend on
// rem units like we do for viewport styles, but we assume root font size changes are
// rare and just invalidate the cache for now.
if (styleEngine().usesRemUnits() && (documentElement()->needsAttach() || !documentElement()->computedStyle() || documentElement()->computedStyle()->fontSize() != documentElementStyle->fontSize())) {
ensureStyleResolver().invalidateMatchedPropertiesCache();
documentElement()->setNeedsStyleRecalc(SubtreeStyleChange, StyleChangeReasonForTracing::create(StyleChangeReason::FontSizeChange));
}
EOverflow overflowX = OAUTO;
EOverflow overflowY = OAUTO;
float columnGap = 0;
if (overflowStyle) {
overflowX = overflowStyle->overflowX();
overflowY = overflowStyle->overflowY();
// Visible overflow on the viewport is meaningless, and the spec says to treat it as 'auto':
if (overflowX == OVISIBLE)
overflowX = OAUTO;
if (overflowY == OVISIBLE)
overflowY = OAUTO;
// Column-gap is (ab)used by the current paged overflow implementation (in lack of other
// ways to specify gaps between pages), so we have to propagate it too.
columnGap = overflowStyle->columnGap();
}
RefPtr<ComputedStyle> documentStyle = layoutView()->mutableStyle();
if (documentStyle->writingMode() != rootWritingMode
|| documentStyle->direction() != rootDirection
|| documentStyle->visitedDependentColor(CSSPropertyBackgroundColor) != backgroundColor
|| documentStyle->backgroundLayers() != backgroundLayers
|| documentStyle->imageRendering() != imageRendering
|| documentStyle->overflowX() != overflowX
|| documentStyle->overflowY() != overflowY
|| documentStyle->columnGap() != columnGap) {
RefPtr<ComputedStyle> newStyle = ComputedStyle::clone(*documentStyle);
newStyle->setWritingMode(rootWritingMode);
newStyle->setDirection(rootDirection);
newStyle->setBackgroundColor(backgroundColor);
newStyle->accessBackgroundLayers() = backgroundLayers;
newStyle->setImageRendering(imageRendering);
newStyle->setOverflowX(overflowX);
newStyle->setOverflowY(overflowY);
newStyle->setColumnGap(columnGap);
layoutView()->setStyle(newStyle);
setupFontBuilder(*newStyle);
}
if (body) {
if (const ComputedStyle* style = body->computedStyle()) {
if (style->direction() != rootDirection || style->writingMode() != rootWritingMode)
body->setNeedsStyleRecalc(SubtreeStyleChange, StyleChangeReasonForTracing::create(StyleChangeReason::WritingModeChange));
}
}
if (const ComputedStyle* style = documentElement()->computedStyle()) {
if (style->direction() != rootDirection || style->writingMode() != rootWritingMode)
documentElement()->setNeedsStyleRecalc(SubtreeStyleChange, StyleChangeReasonForTracing::create(StyleChangeReason::WritingModeChange));
}
}
#if ENABLE(ASSERT)
static void assertLayoutTreeUpdated(Node& root)
{
for (Node& node : NodeTraversal::inclusiveDescendantsOf(root)) {
// We leave some nodes with dirty bits in the tree because they don't
// matter like Comment and ProcessingInstruction nodes.
// TODO(esprehn): Don't even mark those nodes as needing recalcs in the
// first place.
if (!node.isElementNode()
&& !node.isTextNode()
&& !node.isShadowRoot()
&& !node.isDocumentNode())
continue;
ASSERT(!node.needsStyleRecalc());
ASSERT(!node.childNeedsStyleRecalc());
ASSERT(!node.childNeedsDistributionRecalc());
ASSERT(!node.needsStyleInvalidation());
ASSERT(!node.childNeedsStyleInvalidation());
for (ShadowRoot* shadowRoot = node.youngestShadowRoot(); shadowRoot; shadowRoot = shadowRoot->olderShadowRoot())
assertLayoutTreeUpdated(*shadowRoot);
}
}
#endif
void Document::updateLayoutTreeIfNeeded()
{
ASSERT(isMainThread());
ScriptForbiddenScope forbidScript;
// We should forbid script execution for plugins here because update while layout is changing,
// HTMLPlugin element can be reattached and plugin can be destroyed. Plugin can execute scripts
// on destroy. It produces crash without PluginScriptForbiddenScope: crbug.com/550427.
PluginScriptForbiddenScope pluginForbidScript;
if (!view() || !isActive())
return;
if (view()->shouldThrottleRendering())
return;
if (!needsLayoutTreeUpdate()) {
if (lifecycle().state() < DocumentLifecycle::StyleClean) {
// needsLayoutTreeUpdate may change to false without any actual layout tree update.
// For example, needsAnimationTimingUpdate may change to false when time elapses.
// Advance lifecycle to StyleClean because style is actually clean now.
lifecycle().advanceTo(DocumentLifecycle::InStyleRecalc);
lifecycle().advanceTo(DocumentLifecycle::StyleClean);
}
return;
}
if (inStyleRecalc())
return;
// Entering here from inside layout or paint would be catastrophic since recalcStyle can
// tear down the layout tree or (unfortunately) run script. Kill the whole layoutObject if
// someone managed to get into here from inside layout or paint.
RELEASE_ASSERT(!view()->isInPerformLayout());
RELEASE_ASSERT(!view()->isPainting());
// Script can run below in WidgetUpdates, so protect the LocalFrame.
// TODO(esprehn): This should actually crash because of the ScriptForbiddenScope,
// for an example stack see crbug.com/536194, it seems like we should just use
// a PluginScriptForbiddenScope to block all script from running inside here
// to avoid the crash.
RefPtrWillBeRawPtr<LocalFrame> protect(m_frame.get());
TRACE_EVENT_BEGIN1("blink,devtools.timeline", "UpdateLayoutTree", "beginData", InspectorRecalculateStylesEvent::data(frame()));
TRACE_EVENT_SCOPED_SAMPLING_STATE("blink", "UpdateLayoutTree");
unsigned startElementCount = styleEngine().styleForElementCount();
InspectorInstrumentationCookie cookie = InspectorInstrumentation::willRecalculateStyle(this);
DocumentAnimations::updateAnimationTimingIfNeeded(*this);
evaluateMediaQueryListIfNeeded();
updateUseShadowTreesIfNeeded();
updateDistribution();
updateStyleInvalidationIfNeeded();
// FIXME: We should update style on our ancestor chain before proceeding
// however doing so currently causes several tests to crash, as LocalFrame::setDocument calls Document::attach
// before setting the LocalDOMWindow on the LocalFrame, or the SecurityOrigin on the document. The attach, in turn
// resolves style (here) and then when we resolve style on the parent chain, we may end up
// re-attaching our containing iframe, which when asked HTMLFrameElementBase::isURLAllowed
// hits a null-dereference due to security code always assuming the document has a SecurityOrigin.
updateStyle();
notifyLayoutTreeOfSubtreeChanges();
// As a result of the style recalculation, the currently hovered element might have been
// detached (for example, by setting display:none in the :hover style), schedule another mouseMove event
// to check if any other elements ended up under the mouse pointer due to re-layout.
if (hoverNode() && !hoverNode()->layoutObject() && frame())
frame()->eventHandler().dispatchFakeMouseMoveEventSoon();
if (m_focusedElement && !m_focusedElement->isFocusable())
clearFocusedElementSoon();
layoutView()->clearHitTestCache();
ASSERT(!DocumentAnimations::needsAnimationTimingUpdate(*this));
unsigned elementCount = styleEngine().styleForElementCount() - startElementCount;
TRACE_EVENT_END1("blink,devtools.timeline", "UpdateLayoutTree", "elementCount", elementCount);
InspectorInstrumentation::didRecalculateStyle(cookie, elementCount);
#if ENABLE(ASSERT)
assertLayoutTreeUpdated(*this);
#endif
}
void Document::updateStyle()
{
ASSERT(!view()->shouldThrottleRendering());
TRACE_EVENT_BEGIN0("blink,blink_style", "Document::updateStyle");
unsigned initialElementCount = styleEngine().styleForElementCount();
HTMLFrameOwnerElement::UpdateSuspendScope suspendWidgetHierarchyUpdates;
m_lifecycle.advanceTo(DocumentLifecycle::InStyleRecalc);
StyleRecalcChange change = NoChange;
if (styleChangeType() >= SubtreeStyleChange)
change = Force;
NthIndexCache nthIndexCache(*this);
// FIXME: Cannot access the ensureStyleResolver() before calling styleForDocument below because
// apparently the StyleResolver's constructor has side effects. We should fix it.
// See printing/setPrinting.html, printing/width-overflow.html though they only fail on
// mac when accessing the resolver by what appears to be a viewport size difference.
if (change == Force) {
m_hasNodesWithPlaceholderStyle = false;
RefPtr<ComputedStyle> documentStyle = StyleResolver::styleForDocument(*this);
StyleRecalcChange localChange = ComputedStyle::stylePropagationDiff(documentStyle.get(), layoutView()->style());
if (localChange != NoChange)
layoutView()->setStyle(documentStyle.release());
}
clearNeedsStyleRecalc();
StyleResolver& resolver = ensureStyleResolver();
bool shouldRecordStats;
TRACE_EVENT_CATEGORY_GROUP_ENABLED("blink,blink_style", &shouldRecordStats);
styleEngine().setStatsEnabled(shouldRecordStats);
if (Element* documentElement = this->documentElement()) {
inheritHtmlAndBodyElementStyles(change);
dirtyElementsForLayerUpdate();
if (documentElement->shouldCallRecalcStyle(change))
documentElement->recalcStyle(change);
while (dirtyElementsForLayerUpdate())
documentElement->recalcStyle(NoChange);
}
view()->recalcOverflowAfterStyleChange();
view()->setFrameTimingRequestsDirty(true);
clearChildNeedsStyleRecalc();
// Pseudo element removal and similar may only work with these flags still set. Reset them after the style recalc.
styleEngine().resetCSSFeatureFlags(resolver.ensureUpdatedRuleFeatureSet());
resolver.clearStyleSharingList();
m_wasPrinting = m_printing;
ASSERT(!needsStyleRecalc());
ASSERT(!childNeedsStyleRecalc());
ASSERT(inStyleRecalc());
ASSERT(styleResolver() == &resolver);
m_lifecycle.advanceTo(DocumentLifecycle::StyleClean);
if (shouldRecordStats) {
TRACE_EVENT_END2("blink,blink_style", "Document::updateStyle",
"resolverAccessCount", styleEngine().styleForElementCount() - initialElementCount,
"counters", styleEngine().stats()->toTracedValue());
} else {
TRACE_EVENT_END1("blink,blink_style", "Document::updateStyle",
"resolverAccessCount", styleEngine().styleForElementCount() - initialElementCount);
}
}
void Document::notifyLayoutTreeOfSubtreeChanges()
{
if (!layoutView()->wasNotifiedOfSubtreeChange())
return;
m_lifecycle.advanceTo(DocumentLifecycle::InLayoutSubtreeChange);
layoutView()->handleSubtreeModifications();
ASSERT(!layoutView()->wasNotifiedOfSubtreeChange());
m_lifecycle.advanceTo(DocumentLifecycle::LayoutSubtreeChangeClean);
}
bool Document::needsLayoutTreeUpdateForNode(const Node& node) const
{
if (!node.canParticipateInFlatTree())
return false;
if (!needsLayoutTreeUpdate())
return false;
if (!node.inDocument())
return false;
if (needsFullLayoutTreeUpdate() || node.needsStyleRecalc() || node.needsStyleInvalidation())
return true;
for (const ContainerNode* ancestor = LayoutTreeBuilderTraversal::parent(node); ancestor; ancestor = LayoutTreeBuilderTraversal::parent(*ancestor)) {
if (ancestor->needsStyleRecalc() || ancestor->needsStyleInvalidation() || ancestor->needsAdjacentStyleRecalc())
return true;
}
return false;
}
void Document::updateLayoutTreeForNodeIfNeeded(Node* node)
{
ASSERT(node);
if (!needsLayoutTreeUpdateForNode(*node))
return;
updateLayoutTreeIfNeeded();
}
void Document::updateLayout()
{
ASSERT(isMainThread());
ScriptForbiddenScope forbidScript;
RefPtrWillBeRawPtr<FrameView> frameView = view();
if (frameView && frameView->isInPerformLayout()) {
// View layout should not be re-entrant.
ASSERT_NOT_REACHED();
return;
}
if (HTMLFrameOwnerElement* owner = ownerElement())
owner->document().updateLayout();
updateLayoutTreeIfNeeded();
if (!isActive())
return;
if (frameView->needsLayout())
frameView->layout();
if (lifecycle().state() < DocumentLifecycle::LayoutClean)
lifecycle().advanceTo(DocumentLifecycle::LayoutClean);
}
void Document::layoutUpdated()
{
// Plugins can run script inside layout which can detach the page.
// TODO(esprehn): Can this still happen now that all plugins are out of
// process?
if (frame() && frame()->page())
frame()->page()->chromeClient().layoutUpdated(frame());
markers().updateRenderedRectsForMarkers();
// The layout system may perform layouts with pending stylesheets. When
// recording first layout time, we ignore these layouts, since painting is
// suppressed for them. We're interested in tracking the time of the
// first real or 'paintable' layout.
// TODO(esprehn): This doesn't really make sense, why not track the first
// beginFrame? This will catch the first layout in a page that does lots
// of layout thrashing even though that layout might not be followed by
// a paint for many seconds.
if (isRenderingReady() && body() && !styleEngine().hasPendingSheets()) {
if (!m_documentTiming.firstLayout())
m_documentTiming.markFirstLayout();
}
}
void Document::setNeedsFocusedElementCheck()
{
setNeedsStyleRecalc(LocalStyleChange, StyleChangeReasonForTracing::createWithExtraData(StyleChangeReason::PseudoClass, StyleChangeExtraData::Focus));
}
void Document::clearFocusedElementSoon()
{
if (!m_clearFocusedElementTimer.isActive())
m_clearFocusedElementTimer.startOneShot(0, BLINK_FROM_HERE);
}
void Document::clearFocusedElementTimerFired(Timer<Document>*)
{
updateLayoutTreeIfNeeded();
m_clearFocusedElementTimer.stop();
if (m_focusedElement && !m_focusedElement->isFocusable())
m_focusedElement->blur();
}
// FIXME: This is a bad idea and needs to be removed eventually.
// Other browsers load stylesheets before they continue parsing the web page.
// Since we don't, we can run JavaScript code that needs answers before the
// stylesheets are loaded. Doing a layout ignoring the pending stylesheets
// lets us get reasonable answers. The long term solution to this problem is
// to instead suspend JavaScript execution.
void Document::updateLayoutTreeIgnorePendingStylesheets()
{
StyleEngine::IgnoringPendingStylesheet ignoring(styleEngine());
if (styleEngine().hasPendingSheets()) {
// FIXME: We are willing to attempt to suppress painting with outdated style info only once.
// Our assumption is that it would be dangerous to try to stop it a second time, after page
// content has already been loaded and displayed with accurate style information. (Our
// suppression involves blanking the whole page at the moment. If it were more refined, we
// might be able to do something better.) It's worth noting though that this entire method
// is a hack, since what we really want to do is suspend JS instead of doing a layout with
// inaccurate information.
HTMLElement* bodyElement = body();
if (bodyElement && !bodyElement->layoutObject() && m_pendingSheetLayout == NoLayoutWithPendingSheets) {
m_pendingSheetLayout = DidLayoutWithPendingSheets;
styleEngine().resolverChanged(FullStyleUpdate);
} else if (m_hasNodesWithPlaceholderStyle) {
// If new nodes have been added or style recalc has been done with style sheets still
// pending, some nodes may not have had their real style calculated yet. Normally this
// gets cleaned when style sheets arrive but here we need up-to-date style immediately.
setNeedsStyleRecalc(SubtreeStyleChange, StyleChangeReasonForTracing::create(StyleChangeReason::CleanupPlaceholderStyles));
updateLayoutTreeIfNeeded();
}
}
updateLayoutTreeIfNeeded();
}
void Document::updateLayoutIgnorePendingStylesheets(Document::RunPostLayoutTasks runPostLayoutTasks)
{
updateLayoutTreeIgnorePendingStylesheets();
updateLayout();
if (runPostLayoutTasks == RunPostLayoutTasksSynchronously && view())
view()->flushAnyPendingPostLayoutTasks();
}
PassRefPtr<ComputedStyle> Document::styleForElementIgnoringPendingStylesheets(Element* element)
{
ASSERT_ARG(element, element->document() == this);
StyleEngine::IgnoringPendingStylesheet ignoring(styleEngine());
return ensureStyleResolver().styleForElement(element, element->parentNode() ? element->parentNode()->ensureComputedStyle() : 0);
}
PassRefPtr<ComputedStyle> Document::styleForPage(int pageIndex)
{
updateDistribution();
return ensureStyleResolver().styleForPage(pageIndex);
}
bool Document::isPageBoxVisible(int pageIndex)
{
return styleForPage(pageIndex)->visibility() != HIDDEN; // display property doesn't apply to @page.
}
void Document::pageSizeAndMarginsInPixels(int pageIndex, IntSize& pageSize, int& marginTop, int& marginRight, int& marginBottom, int& marginLeft)
{
RefPtr<ComputedStyle> style = styleForPage(pageIndex);
int width = pageSize.width();
int height = pageSize.height();
switch (style->pageSizeType()) {
case PAGE_SIZE_AUTO:
break;
case PAGE_SIZE_AUTO_LANDSCAPE:
if (width < height)
std::swap(width, height);
break;
case PAGE_SIZE_AUTO_PORTRAIT:
if (width > height)
std::swap(width, height);
break;
case PAGE_SIZE_RESOLVED: {
FloatSize size = style->pageSize();
width = size.width();
height = size.height();
break;
}
default:
ASSERT_NOT_REACHED();
}
pageSize = IntSize(width, height);
// The percentage is calculated with respect to the width even for margin top and bottom.
// http://www.w3.org/TR/CSS2/box.html#margin-properties
marginTop = style->marginTop().isAuto() ? marginTop : intValueForLength(style->marginTop(), width);
marginRight = style->marginRight().isAuto() ? marginRight : intValueForLength(style->marginRight(), width);
marginBottom = style->marginBottom().isAuto() ? marginBottom : intValueForLength(style->marginBottom(), width);
marginLeft = style->marginLeft().isAuto() ? marginLeft : intValueForLength(style->marginLeft(), width);
}
void Document::setIsViewSource(bool isViewSource)
{
m_isViewSource = isViewSource;
if (!m_isViewSource)
return;
setSecurityOrigin(SecurityOrigin::createUnique());
didUpdateSecurityOrigin();
}
bool Document::dirtyElementsForLayerUpdate()
{
if (m_layerUpdateSVGFilterElements.isEmpty())
return false;
for (Element* element : m_layerUpdateSVGFilterElements)
element->setNeedsStyleRecalc(LocalStyleChange, StyleChangeReasonForTracing::create(StyleChangeReason::SVGFilterLayerUpdate));
m_layerUpdateSVGFilterElements.clear();
return true;
}
void Document::scheduleSVGFilterLayerUpdateHack(Element& element)
{
if (element.styleChangeType() == NeedsReattachStyleChange)
return;
element.setSVGFilterNeedsLayerUpdate();
m_layerUpdateSVGFilterElements.add(&element);
scheduleLayoutTreeUpdateIfNeeded();
}
void Document::unscheduleSVGFilterLayerUpdateHack(Element& element)
{
element.clearSVGFilterNeedsLayerUpdate();
m_layerUpdateSVGFilterElements.remove(&element);
}
void Document::scheduleUseShadowTreeUpdate(SVGUseElement& element)
{
m_useElementsNeedingUpdate.add(&element);
scheduleLayoutTreeUpdateIfNeeded();
}
void Document::unscheduleUseShadowTreeUpdate(SVGUseElement& element)
{
m_useElementsNeedingUpdate.remove(&element);
}
void Document::updateUseShadowTreesIfNeeded()
{
ScriptForbiddenScope forbidScript;
if (m_useElementsNeedingUpdate.isEmpty())
return;
WillBeHeapVector<RawPtrWillBeMember<SVGUseElement>> elements;
copyToVector(m_useElementsNeedingUpdate, elements);
m_useElementsNeedingUpdate.clear();
for (SVGUseElement* element : elements)
element->buildPendingResource();
}
StyleResolver* Document::styleResolver() const
{
return m_styleEngine->resolver();
}
StyleResolver& Document::ensureStyleResolver() const
{
return m_styleEngine->ensureResolver();
}
void Document::attach(const AttachContext& context)
{
ASSERT(m_lifecycle.state() == DocumentLifecycle::Inactive);
ASSERT(!m_axObjectCache || this != &axObjectCacheOwner());
m_layoutView = new LayoutView(this);
setLayoutObject(m_layoutView);
m_layoutView->setIsInWindow(true);
m_layoutView->setStyle(StyleResolver::styleForDocument(*this));
m_layoutView->compositor()->setNeedsCompositingUpdate(CompositingUpdateAfterCompositingInputChange);
ContainerNode::attach(context);
// The TextAutosizer can't update layout view info while the Document is detached, so update now in case anything changed.
if (TextAutosizer* autosizer = textAutosizer())
autosizer->updatePageInfo();
m_lifecycle.advanceTo(DocumentLifecycle::StyleClean);
}
void Document::detach(const AttachContext& context)
{
TRACE_EVENT0("blink", "Document::detach");
RELEASE_ASSERT(!m_frame || m_frame->tree().childCount() == 0);
if (!isActive())
return;
// Frame navigation can cause a new Document to be attached. Don't allow that, since that will
// cause a situation where LocalFrame still has a Document attached after this finishes!
// Normally, it shouldn't actually be possible to trigger navigation here. However, plugins
// (see below) can cause lots of crazy things to happen, since plugin detach involves nested
// message loops.
FrameNavigationDisabler navigationDisabler(*m_frame);
// Defer widget updates to avoid plugins trying to run script inside ScriptForbiddenScope,
// which will crash the renderer after https://crrev.com/200984
HTMLFrameOwnerElement::UpdateSuspendScope suspendWidgetHierarchyUpdates;
// Don't allow script to run in the middle of detach() because a detaching Document is not in a
// consistent state.
ScriptForbiddenScope forbidScript;
view()->dispose();
m_markers->prepareForDestruction();
if (LocalDOMWindow* window = this->domWindow())
window->willDetachDocumentFromFrame();
m_lifecycle.advanceTo(DocumentLifecycle::Stopping);
if (page())
page()->documentDetached(this);
InspectorInstrumentation::documentDetached(this);
if (m_frame->loader().client()->sharedWorkerRepositoryClient())
m_frame->loader().client()->sharedWorkerRepositoryClient()->documentDetached(this);
for (DocumentVisibilityObserver* observer : m_visibilityObservers)
observer->willDetachDocument();
stopActiveDOMObjects();
// FIXME: consider using ActiveDOMObject.
if (m_scriptedAnimationController)
m_scriptedAnimationController->clearDocumentPointer();
m_scriptedAnimationController.clear();
m_scriptedIdleTaskController.clear();
if (svgExtensions())
accessSVGExtensions().pauseAnimations();
// FIXME: This shouldn't be needed once LocalDOMWindow becomes ExecutionContext.
if (m_domWindow)
m_domWindow->clearEventQueue();
if (m_layoutView)
m_layoutView->setIsInWindow(false);
if (registrationContext())
registrationContext()->documentWasDetached();
m_hoverNode = nullptr;
m_activeHoverElement = nullptr;
m_autofocusElement = nullptr;
if (m_focusedElement.get()) {
RefPtrWillBeRawPtr<Element> oldFocusedElement = m_focusedElement;
m_focusedElement = nullptr;
if (frameHost())
frameHost()->chromeClient().focusedNodeChanged(oldFocusedElement.get(), nullptr);
}
if (this == &axObjectCacheOwner())
clearAXObjectCache();
m_layoutView = nullptr;
ContainerNode::detach(context);
if (this != &axObjectCacheOwner()) {
if (AXObjectCache* cache = existingAXObjectCache()) {
// Documents that are not a root document use the AXObjectCache in
// their root document. Node::removedFrom is called after the
// document has been detached so it can't find the root document.
// We do the removals here instead.
for (Node& node : NodeTraversal::descendantsOf(*this)) {
cache->remove(&node);
}
}
}
styleEngine().didDetach();
frameHost()->eventHandlerRegistry().documentDetached(*this);
m_frame->inputMethodController().documentDetached();
// If this Document is associated with a live DocumentLoader, the
// DocumentLoader will take care of clearing the FetchContext. Deferring
// to the DocumentLoader when possible also prevents prematurely clearing
// the context in the case where multiple Documents end up associated with
// a single DocumentLoader (e.g., navigating to a javascript: url).
if (!loader())
m_fetcher->clearContext();
// If this document is the master for an HTMLImportsController, sever that
// relationship. This ensures that we don't leave import loads in flight,
// thinking they should have access to a valid frame when they don't.
if (m_importsController)
HTMLImportsController::removeFrom(*this);
m_timers.setTimerTaskRunner(
Platform::current()->currentThread()->scheduler()->timerTaskRunner()->adoptClone());
// This is required, as our LocalFrame might delete itself as soon as it detaches
// us. However, this violates Node::detach() semantics, as it's never
// possible to re-attach. Eventually Document::detach() should be renamed,
// or this setting of the frame to 0 could be made explicit in each of the
// callers of Document::detach().
m_frame = nullptr;
if (m_mediaQueryMatcher)
m_mediaQueryMatcher->documentDetached();
DocumentLifecycleNotifier::notifyDocumentWasDetached();
m_lifecycle.advanceTo(DocumentLifecycle::Stopped);
// FIXME: Currently we call notifyContextDestroyed() only in
// Document::detach(), which means that we don't call
// notifyContextDestroyed() for a document that doesn't get detached.
// If such a document has any observer, the observer won't get
// a contextDestroyed() notification. This can happen for a document
// created by DOMImplementation::createDocument().
DocumentLifecycleNotifier::notifyContextDestroyed();
ExecutionContext::notifyContextDestroyed();
}
void Document::removeAllEventListeners()
{
ContainerNode::removeAllEventListeners();
if (LocalDOMWindow* domWindow = this->domWindow())
domWindow->removeAllEventListeners();
}
Document& Document::axObjectCacheOwner() const
{
// FIXME(dmazzoni): Currently there's one AXObjectCache per page, owned
// by the top document, but with --site-isolation the top document may
// be a remote frame. As a quick fix we're making the local root the owner
// of the AXObjectCache (http://crbug.com/510410), but the proper fix
// will be for each Document to have its own AXObjectCache
// (http://crbug.com/532249).
Document* top = const_cast<Document*>(this);
LocalFrame* frame = this->frame();
if (!frame)
return *top;
// This loop is more efficient than calling localFrameRoot.
while (frame && frame->owner() && frame->owner()->isLocal()) {
HTMLFrameOwnerElement* owner = toHTMLFrameOwnerElement(frame->owner());
top = &owner->document();
frame = top->frame();
}
if (top->frame() && top->frame()->pagePopupOwner()) {
ASSERT(!top->m_axObjectCache);
return top->frame()->pagePopupOwner()->document().axObjectCacheOwner();
}
ASSERT(top);
return *top;
}
void Document::clearAXObjectCache()
{
ASSERT(&axObjectCacheOwner() == this);
// Clear the cache member variable before calling delete because attempts
// are made to access it during destruction.
if (m_axObjectCache)
m_axObjectCache->dispose();
m_axObjectCache.clear();
}
AXObjectCache* Document::existingAXObjectCache() const
{
// If the layoutObject is gone then we are in the process of destruction.
// This method will be called before m_frame = nullptr.
if (!axObjectCacheOwner().layoutView())
return 0;
return axObjectCacheOwner().m_axObjectCache.get();
}
AXObjectCache* Document::axObjectCache() const
{
Settings* settings = this->settings();
if (!settings || !settings->accessibilityEnabled())
return 0;
// The only document that actually has a AXObjectCache is the top-level
// document. This is because we need to be able to get from any WebCoreAXObject
// to any other WebCoreAXObject on the same page. Using a single cache allows
// lookups across nested webareas (i.e. multiple documents).
Document& cacheOwner = this->axObjectCacheOwner();
// If the document has already been detached, do not make a new axObjectCache.
if (!cacheOwner.layoutView())
return 0;
ASSERT(&cacheOwner == this || !m_axObjectCache);
if (!cacheOwner.m_axObjectCache)
cacheOwner.m_axObjectCache = AXObjectCache::create(cacheOwner);
return cacheOwner.m_axObjectCache.get();
}
CanvasFontCache* Document::canvasFontCache()
{
if (!m_canvasFontCache)
m_canvasFontCache = CanvasFontCache::create(*this);
return m_canvasFontCache.get();
}
PassRefPtrWillBeRawPtr<DocumentParser> Document::createParser()
{
if (isHTMLDocument()) {
bool reportErrors = InspectorInstrumentation::collectingHTMLParseErrors(this);
return HTMLDocumentParser::create(toHTMLDocument(*this), reportErrors, m_parserSyncPolicy);
}
// FIXME: this should probably pass the frame instead
return XMLDocumentParser::create(*this, view());
}
bool Document::isFrameSet() const
{
if (!isHTMLDocument())
return false;
return isHTMLFrameSetElement(body());
}
ScriptableDocumentParser* Document::scriptableDocumentParser() const
{
return parser() ? parser()->asScriptableDocumentParser() : 0;
}
void Document::open(Document* enteredDocument, ExceptionState& exceptionState)
{
if (importLoader()) {
exceptionState.throwDOMException(InvalidStateError, "Imported document doesn't support open().");
return;
}
if (!isHTMLDocument()) {
exceptionState.throwDOMException(InvalidStateError, "Only HTML documents support open().");
return;
}
if (enteredDocument) {
if (!securityOrigin()->canAccess(enteredDocument->securityOrigin())) {
exceptionState.throwSecurityError("Can only call open() on same-origin documents.");
return;
}
setSecurityOrigin(enteredDocument->securityOrigin());
setURL(enteredDocument->url());
m_cookieURL = enteredDocument->cookieURL();
}
open();
}
void Document::open()
{
ASSERT(!importLoader());
if (m_frame) {
if (ScriptableDocumentParser* parser = scriptableDocumentParser()) {
if (parser->isParsing()) {
// FIXME: HTML5 doesn't tell us to check this, it might not be correct.
if (parser->isExecutingScript())
return;
if (!parser->wasCreatedByScript() && parser->hasInsertionPoint())
return;
}
}
if (m_frame->loader().provisionalDocumentLoader())
m_frame->loader().stopAllLoaders();
}
removeAllEventListenersRecursively();
implicitOpen(ForceSynchronousParsing);
if (ScriptableDocumentParser* parser = scriptableDocumentParser())
parser->setWasCreatedByScript(true);
if (m_frame)
m_frame->loader().didExplicitOpen();
if (m_loadEventProgress != LoadEventInProgress && pageDismissalEventBeingDispatched() == NoDismissal)
m_loadEventProgress = LoadEventNotRun;
}
void Document::detachParser()
{
if (!m_parser)
return;
m_parser->detach();
m_parser.clear();
}
void Document::cancelParsing()
{
detachParser();
setParsingState(FinishedParsing);
setReadyState(Complete);
}
PassRefPtrWillBeRawPtr<DocumentParser> Document::implicitOpen(ParserSynchronizationPolicy parserSyncPolicy)
{
detachParser();
removeChildren();
ASSERT(!m_focusedElement);
setCompatibilityMode(NoQuirksMode);
if (!threadedParsingEnabledForTesting())
parserSyncPolicy = ForceSynchronousParsing;
m_parserSyncPolicy = parserSyncPolicy;
m_parser = createParser();
setParsingState(Parsing);
setReadyState(Loading);
return m_parser;
}
HTMLElement* Document::body() const
{
if (!documentElement() || !isHTMLHtmlElement(documentElement()))
return 0;
for (HTMLElement* child = Traversal<HTMLElement>::firstChild(*documentElement()); child; child = Traversal<HTMLElement>::nextSibling(*child)) {
if (isHTMLFrameSetElement(*child) || isHTMLBodyElement(*child))
return child;
}
return 0;
}
HTMLBodyElement* Document::firstBodyElement() const
{
if (!documentElement())
return 0;
for (HTMLElement* child = Traversal<HTMLElement>::firstChild(*documentElement()); child; child = Traversal<HTMLElement>::nextSibling(*child)) {
if (isHTMLBodyElement(*child))
return toHTMLBodyElement(child);
}
return 0;
}
void Document::setBody(PassRefPtrWillBeRawPtr<HTMLElement> prpNewBody, ExceptionState& exceptionState)
{
RefPtrWillBeRawPtr<HTMLElement> newBody = prpNewBody;
if (!newBody) {
exceptionState.throwDOMException(HierarchyRequestError, ExceptionMessages::argumentNullOrIncorrectType(1, "HTMLElement"));
return;
}
if (!documentElement()) {
exceptionState.throwDOMException(HierarchyRequestError, "No document element exists.");
return;
}
if (!isHTMLBodyElement(*newBody) && !isHTMLFrameSetElement(*newBody)) {
exceptionState.throwDOMException(HierarchyRequestError, "The new body element is of type '" + newBody->tagName() + "'. It must be either a 'BODY' or 'FRAMESET' element.");
return;
}
HTMLElement* oldBody = body();
if (oldBody == newBody)
return;
if (oldBody)
documentElement()->replaceChild(newBody.release(), oldBody, exceptionState);
else
documentElement()->appendChild(newBody.release(), exceptionState);
}
HTMLHeadElement* Document::head() const
{
Node* de = documentElement();
if (!de)
return 0;
return Traversal<HTMLHeadElement>::firstChild(*de);
}
Element* Document::viewportDefiningElement(const ComputedStyle* rootStyle) const
{
// If a BODY element sets non-visible overflow, it is to be propagated to the viewport, as long
// as the following conditions are all met:
// (1) The root element is HTML.
// (2) It is the primary BODY element (we only assert for this, expecting callers to behave).
// (3) The root element has visible overflow.
// Otherwise it's the root element's properties that are to be propagated.
Element* rootElement = documentElement();
Element* bodyElement = body();
if (!rootElement)
return 0;
if (!rootStyle) {
rootStyle = rootElement->computedStyle();
if (!rootStyle)
return 0;
}
if (bodyElement && rootStyle->isOverflowVisible() && isHTMLHtmlElement(*rootElement))
return bodyElement;
return rootElement;
}
void Document::close(ExceptionState& exceptionState)
{
// FIXME: We should follow the specification more closely:
// http://www.whatwg.org/specs/web-apps/current-work/#dom-document-close
if (importLoader()) {
exceptionState.throwDOMException(InvalidStateError, "Imported document doesn't support close().");
return;
}
if (!isHTMLDocument()) {
exceptionState.throwDOMException(InvalidStateError, "Only HTML documents support close().");
return;
}
close();
}
void Document::close()
{
if (!scriptableDocumentParser() || !scriptableDocumentParser()->wasCreatedByScript() || !scriptableDocumentParser()->isParsing())
return;
if (RefPtrWillBeRawPtr<DocumentParser> parser = m_parser)
parser->finish();
if (!m_frame) {
// Because we have no frame, we don't know if all loading has completed,
// so we just call implicitClose() immediately. FIXME: This might fire
// the load event prematurely <http://bugs.webkit.org/show_bug.cgi?id=14568>.
implicitClose();
return;
}
m_frame->loader().checkCompleted();
}
void Document::implicitClose()
{
ASSERT(!inStyleRecalc());
if (processingLoadEvent() || !m_parser)
return;
if (frame() && frame()->navigationScheduler().locationChangePending()) {
suppressLoadEvent();
return;
}
// The call to dispatchWindowLoadEvent (from documentWasClosed()) can detach
// the LocalDOMWindow and cause it (and its attached Document) to be
// destroyed.
RefPtrWillBeRawPtr<LocalDOMWindow> protectedWindow(this->domWindow());
m_loadEventProgress = LoadEventInProgress;
ScriptableDocumentParser* parser = scriptableDocumentParser();
m_wellFormed = parser && parser->wellFormed();
// We have to clear the parser, in case someone document.write()s from the
// onLoad event handler, as in Radar 3206524.
detachParser();
if (frame() && frame()->script().canExecuteScripts(NotAboutToExecuteScript)) {
ImageLoader::dispatchPendingLoadEvents();
ImageLoader::dispatchPendingErrorEvents();
HTMLLinkElement::dispatchPendingLoadEvents();
HTMLStyleElement::dispatchPendingLoadEvents();
}
// JS running below could remove the frame or destroy the LayoutView so we call
// those two functions repeatedly and don't save them on the stack.
// To align the HTML load event and the SVGLoad event for the outermost <svg> element, fire it from
// here, instead of doing it from SVGElement::finishedParsingChildren.
if (svgExtensions())
accessSVGExtensions().dispatchSVGLoadEventToOutermostSVGElements();
if (protectedWindow)
protectedWindow->documentWasClosed();
if (frame()) {
frame()->loader().client()->dispatchDidHandleOnloadEvents();
loader()->applicationCacheHost()->stopDeferringEvents();
}
if (!frame()) {
m_loadEventProgress = LoadEventCompleted;
return;
}
// Make sure both the initial layout and reflow happen after the onload
// fires. This will improve onload scores, and other browsers do it.
// If they wanna cheat, we can too. -dwh
if (frame()->navigationScheduler().locationChangePending() && elapsedTime() < cLayoutScheduleThreshold) {
// Just bail out. Before or during the onload we were shifted to another page.
// The old i-Bench suite does this. When this happens don't bother painting or laying out.
m_loadEventProgress = LoadEventCompleted;
return;
}
// We used to force a synchronous display and flush here. This really isn't
// necessary and can in fact be actively harmful if pages are loading at a rate of > 60fps
// (if your platform is syncing flushes and limiting them to 60fps).
if (!ownerElement() || (ownerElement()->layoutObject() && !ownerElement()->layoutObject()->needsLayout())) {
updateLayoutTreeIfNeeded();
// Always do a layout after loading if needed.
if (view() && layoutView() && (!layoutView()->firstChild() || layoutView()->needsLayout()))
view()->layout();
}
m_loadEventProgress = LoadEventCompleted;
if (frame() && layoutView() && settings()->accessibilityEnabled()) {
if (AXObjectCache* cache = axObjectCache()) {
if (this == &axObjectCacheOwner())
cache->handleLoadComplete(this);
else
cache->handleLayoutComplete(this);
}
}
if (svgExtensions