Diff
Modified: trunk/Source/WebCore/ChangeLog (212462 => 212463)
--- trunk/Source/WebCore/ChangeLog 2017-02-16 21:31:17 UTC (rev 212462)
+++ trunk/Source/WebCore/ChangeLog 2017-02-16 22:04:32 UTC (rev 212463)
@@ -1,3 +1,62 @@
+2017-02-16 Antti Koivisto <[email protected]>
+
+ Execute pending scripts asynchronously after stylesheet loads complete
+ https://bugs.webkit.org/show_bug.cgi?id=168367
+ rdar://problem/30561379
+
+ Reviewed by Andreas Kling.
+
+ The current synchronous execution is fragile and creates various problems.
+
+ * css/StyleSheetContents.cpp:
+ (WebCore::StyleSheetContents::checkLoaded):
+ * dom/ContainerNode.cpp:
+ (WebCore::ContainerNode::takeAllChildrenFrom):
+ (WebCore::ContainerNode::notifyChildInserted):
+ (WebCore::ContainerNode::removeChild):
+ (WebCore::ContainerNode::parserRemoveChild):
+ (WebCore::ContainerNode::removeChildren):
+
+ Remove various places where we would trigger delayed synchronous execution.
+
+ * dom/Document.cpp:
+ (WebCore::Document::Document):
+ (WebCore::Document::recalcStyle):
+
+ Trigger scroll to anchor at the end of style resolution instead of when style sheet load completes.
+
+ (WebCore::Document::didRemoveAllPendingStylesheet):
+
+ Call asynchronous script execution function.
+
+ * dom/Document.h:
+ (WebCore::Document::setNeedsNotifyRemoveAllPendingStylesheet): Deleted.
+ (WebCore::Document::notifyRemovePendingSheetIfNeeded): Deleted.
+ * dom/ScriptableDocumentParser.cpp:
+ (WebCore::ScriptableDocumentParser::ScriptableDocumentParser):
+ (WebCore::ScriptableDocumentParser::executeScriptsWaitingForStylesheetsSoon):
+ (WebCore::ScriptableDocumentParser::scriptsWaitingForStylesheetsExecutionTimerFired):
+
+ Add a timer for executing pending scripts.
+
+ (WebCore::ScriptableDocumentParser::detach):
+ * dom/ScriptableDocumentParser.h:
+ (WebCore::ScriptableDocumentParser::executeScriptsWaitingForStylesheets):
+ * html/HTMLLinkElement.cpp:
+ (WebCore::HTMLLinkElement::removedFrom):
+ (WebCore::HTMLLinkElement::removePendingSheet):
+ * html/HTMLLinkElement.h:
+ * html/parser/HTMLDocumentParser.cpp:
+ (WebCore::HTMLDocumentParser::detach):
+ * loader/DocumentLoader.cpp:
+ (WebCore::DocumentLoader::isLoadingInAPISense):
+
+ Stay in loading state if we have a pending script. This matches existing behavior.
+
+ * style/StyleScope.cpp:
+ (WebCore::Style::Scope::removePendingSheet):
+ * style/StyleScope.h:
+
2017-02-16 Anders Carlsson <[email protected]>
Set the DHTML drag image in the DHTML type check
Modified: trunk/Source/WebCore/css/StyleSheetContents.cpp (212462 => 212463)
--- trunk/Source/WebCore/css/StyleSheetContents.cpp 2017-02-16 21:31:17 UTC (rev 212462)
+++ trunk/Source/WebCore/css/StyleSheetContents.cpp 2017-02-16 22:04:32 UTC (rev 212463)
@@ -364,9 +364,6 @@
if (isLoading())
return;
- // Avoid |this| being deleted by scripts that run via
- // ScriptableDocumentParser::executeScriptsWaitingForStylesheets().
- // See <rdar://problem/6622300>.
Ref<StyleSheetContents> protectedThis(*this);
StyleSheetContents* parentSheet = parentStyleSheet();
if (parentSheet) {
Modified: trunk/Source/WebCore/dom/ContainerNode.cpp (212462 => 212463)
--- trunk/Source/WebCore/dom/ContainerNode.cpp 2017-02-16 21:31:17 UTC (rev 212462)
+++ trunk/Source/WebCore/dom/ContainerNode.cpp 2017-02-16 22:04:32 UTC (rev 212463)
@@ -145,8 +145,6 @@
childrenChanged(change);
}
- document().notifyRemovePendingSheetIfNeeded();
-
// FIXME: assert that we don't dispatch events here since this container node is still disconnected.
for (auto& child : children) {
RELEASE_ASSERT(!child->parentNode() && &child->treeScope() == &treeScope());
@@ -364,9 +362,6 @@
childrenChanged(change);
- // Some call sites may have deleted child nodes. Calling it earlier results in bugs like webkit.org/b/168069.
- document().notifyRemovePendingSheetIfNeeded();
-
for (auto& target : postInsertionNotificationTargets)
target->finishedInsertingSubtree();
}
@@ -554,7 +549,6 @@
notifyChildRemoved(child, prev, next, ChildChangeSourceAPI);
}
- document().notifyRemovePendingSheetIfNeeded();
rebuildSVGExtensionsElementsIfNecessary();
dispatchSubtreeModifiedEvent();
@@ -615,7 +609,6 @@
removeBetween(prev, next, oldChild);
notifyChildRemoved(oldChild, prev, next, ChildChangeSourceParser);
- document().notifyRemovePendingSheetIfNeeded();
}
// https://dom.spec.whatwg.org/#concept-node-replace-all
@@ -709,7 +702,6 @@
childrenChanged(change);
}
- document().notifyRemovePendingSheetIfNeeded();
rebuildSVGExtensionsElementsIfNecessary();
dispatchSubtreeModifiedEvent();
}
Modified: trunk/Source/WebCore/dom/Document.cpp (212462 => 212463)
--- trunk/Source/WebCore/dom/Document.cpp 2017-02-16 21:31:17 UTC (rev 212462)
+++ trunk/Source/WebCore/dom/Document.cpp 2017-02-16 22:04:32 UTC (rev 212463)
@@ -443,7 +443,6 @@
, m_referencingNodeCount(0)
, m_settings(frame ? Ref<Settings>(frame->settings()) : Settings::create(nullptr))
, m_hasNodesWithPlaceholderStyle(false)
- , m_needsNotifyRemoveAllPendingStylesheet(false)
, m_ignorePendingStylesheets(false)
, m_pendingSheetLayout(NoLayoutWithPendingSheets)
, m_cachedResourceLoader(m_frame ? Ref<CachedResourceLoader>(m_frame->loader().activeDocumentLoader()->cachedResourceLoader()) : CachedResourceLoader::create(nullptr))
@@ -1846,6 +1845,9 @@
// to check if any other elements ended up under the mouse pointer due to re-layout.
if (m_hoveredElement && !m_hoveredElement->renderer())
frameView.frame().mainFrame().eventHandler().dispatchFakeMouseMoveEventSoon();
+
+ if (m_gotoAnchorNeededAfterStylesheetsLoad && !styleScope().hasPendingSheets())
+ frameView.scrollToFragment(m_url);
}
bool Document::needsStyleRecalc() const
@@ -2617,7 +2619,7 @@
return;
}
- m_frame->loader().checkCompleted();
+ checkCompleted();
}
void Document::implicitClose()
@@ -3067,19 +3069,17 @@
void Document::didRemoveAllPendingStylesheet()
{
- m_needsNotifyRemoveAllPendingStylesheet = false;
-
if (m_pendingSheetLayout == DidLayoutWithPendingSheets) {
+ // Painting is disabled when doing layouts with pending sheets to avoid FOUC.
+ // We need to force paint when coming out from this state.
+ // FIXME: This is not very elegant.
m_pendingSheetLayout = IgnoreLayoutWithPendingSheets;
if (renderView())
renderView()->repaintViewAndCompositedLayers();
}
- if (ScriptableDocumentParser* parser = scriptableDocumentParser())
- parser->executeScriptsWaitingForStylesheets();
-
- if (m_gotoAnchorNeededAfterStylesheetsLoad && view())
- view()->scrollToFragment(m_url);
+ if (auto* parser = scriptableDocumentParser())
+ parser->executeScriptsWaitingForStylesheetsSoon();
}
bool Document::usesStyleBasedEditability() const
@@ -6032,10 +6032,15 @@
void Document::loadEventDelayTimerFired()
{
- if (frame())
- frame()->loader().checkCompleted();
+ checkCompleted();
}
+void Document::checkCompleted()
+{
+ if (auto* frame = this->frame())
+ frame->loader().checkCompleted();
+}
+
double Document::monotonicTimestamp() const
{
auto* loader = this->loader();
Modified: trunk/Source/WebCore/dom/Document.h (212462 => 212463)
--- trunk/Source/WebCore/dom/Document.h 2017-02-16 21:31:17 UTC (rev 212462)
+++ trunk/Source/WebCore/dom/Document.h 2017-02-16 22:04:32 UTC (rev 212463)
@@ -486,8 +486,6 @@
CSSFontSelector& fontSelector() { return m_fontSelector; }
- void notifyRemovePendingSheetIfNeeded();
-
WEBCORE_EXPORT bool haveStylesheetsLoaded() const;
WEBCORE_EXPORT StyleSheetList& styleSheets();
@@ -1115,6 +1113,7 @@
void incrementLoadEventDelayCount() { ++m_loadEventDelayCount; }
void decrementLoadEventDelayCount();
bool isDelayingLoadEvent() const { return m_loadEventDelayCount; }
+ void checkCompleted();
#if ENABLE(IOS_TOUCH_EVENTS)
#include <WebKitAdditions/DocumentIOS.h>
@@ -1205,7 +1204,6 @@
DocumentSharedObjectPool* sharedObjectPool() { return m_sharedObjectPool.get(); }
void didRemoveAllPendingStylesheet();
- void setNeedsNotifyRemoveAllPendingStylesheet() { m_needsNotifyRemoveAllPendingStylesheet = true; }
void didClearStyleResolver();
bool inStyleRecalc() const { return m_inStyleRecalc; }
@@ -1390,7 +1388,6 @@
std::unique_ptr<StyleResolver> m_userAgentShadowTreeStyleResolver;
bool m_hasNodesWithPlaceholderStyle;
- bool m_needsNotifyRemoveAllPendingStylesheet;
// But sometimes you need to ignore pending stylesheet count to
// force an immediate layout when requested by JS.
bool m_ignorePendingStylesheets;
@@ -1756,12 +1753,6 @@
Element* eventTargetElementForDocument(Document*);
-inline void Document::notifyRemovePendingSheetIfNeeded()
-{
- if (m_needsNotifyRemoveAllPendingStylesheet)
- didRemoveAllPendingStylesheet();
-}
-
inline TextEncoding Document::textEncoding() const
{
if (auto* decoder = this->decoder())
Modified: trunk/Source/WebCore/dom/ScriptableDocumentParser.cpp (212462 => 212463)
--- trunk/Source/WebCore/dom/ScriptableDocumentParser.cpp 2017-02-16 21:31:17 UTC (rev 212462)
+++ trunk/Source/WebCore/dom/ScriptableDocumentParser.cpp 2017-02-16 22:04:32 UTC (rev 212463)
@@ -28,6 +28,7 @@
#include "Document.h"
#include "Settings.h"
+#include "StyleScope.h"
namespace WebCore {
@@ -35,6 +36,7 @@
: DecodedDataDocumentParser(document)
, m_wasCreatedByScript(false)
, m_parserContentPolicy(parserContentPolicy)
+ , m_scriptsWaitingForStylesheetsExecutionTimer(*this, &ScriptableDocumentParser::scriptsWaitingForStylesheetsExecutionTimerFired)
{
if (!pluginContentIsAllowed(m_parserContentPolicy))
m_parserContentPolicy = allowPluginContent(m_parserContentPolicy);
@@ -43,4 +45,32 @@
m_parserContentPolicy = disallowScriptingContent(m_parserContentPolicy);
}
+void ScriptableDocumentParser::executeScriptsWaitingForStylesheetsSoon()
+{
+ ASSERT(!document()->styleScope().hasPendingSheets());
+
+ if (m_scriptsWaitingForStylesheetsExecutionTimer.isActive())
+ return;
+ if (!hasScriptsWaitingForStylesheets())
+ return;
+
+ m_scriptsWaitingForStylesheetsExecutionTimer.startOneShot(0);
+}
+
+void ScriptableDocumentParser::scriptsWaitingForStylesheetsExecutionTimerFired()
+{
+ if (!document()->styleScope().hasPendingSheets())
+ executeScriptsWaitingForStylesheets();
+
+ if (!isDetached())
+ document()->checkCompleted();
+}
+
+void ScriptableDocumentParser::detach()
+{
+ m_scriptsWaitingForStylesheetsExecutionTimer.stop();
+
+ DecodedDataDocumentParser::detach();
+}
+
};
Modified: trunk/Source/WebCore/dom/ScriptableDocumentParser.h (212462 => 212463)
--- trunk/Source/WebCore/dom/ScriptableDocumentParser.h 2017-02-16 21:31:17 UTC (rev 212462)
+++ trunk/Source/WebCore/dom/ScriptableDocumentParser.h 2017-02-16 22:04:32 UTC (rev 212463)
@@ -27,6 +27,7 @@
#include "DecodedDataDocumentParser.h"
#include "FragmentScriptingPermission.h"
+#include "Timer.h"
#include <wtf/text/TextPosition.h>
namespace WebCore {
@@ -37,14 +38,14 @@
// _javascript_ document.open() call right now, or it should be ignored.
virtual bool isExecutingScript() const { return false; }
- // FIXME: Only the HTMLDocumentParser ever blocks script execution on
- // stylesheet load, which is likely a bug in the XMLDocumentParser.
- virtual void executeScriptsWaitingForStylesheets() { }
-
virtual bool isWaitingForScripts() const = 0;
virtual TextPosition textPosition() const = 0;
+ virtual bool hasScriptsWaitingForStylesheets() const { return false; }
+
+ void executeScriptsWaitingForStylesheetsSoon();
+
// Returns true if the parser didn't yield or pause or synchronously execute a script,
// so calls to PageConsoleClient should be associated with the parser's text position.
virtual bool shouldAssociateConsoleMessagesWithTextPosition() const = 0;
@@ -57,12 +58,19 @@
protected:
explicit ScriptableDocumentParser(Document&, ParserContentPolicy = AllowScriptingContent);
+ virtual void executeScriptsWaitingForStylesheets() { }
+
+ void detach() override;
+
private:
ScriptableDocumentParser* asScriptableDocumentParser() final { return this; }
+ void scriptsWaitingForStylesheetsExecutionTimerFired();
+
// http://www.whatwg.org/specs/web-apps/current-work/#script-created-parser
bool m_wasCreatedByScript;
ParserContentPolicy m_parserContentPolicy;
+ Timer m_scriptsWaitingForStylesheetsExecutionTimer;
};
} // namespace WebCore
Modified: trunk/Source/WebCore/html/HTMLLinkElement.cpp (212462 => 212463)
--- trunk/Source/WebCore/html/HTMLLinkElement.cpp 2017-02-16 21:31:17 UTC (rev 212462)
+++ trunk/Source/WebCore/html/HTMLLinkElement.cpp 2017-02-16 22:04:32 UTC (rev 212463)
@@ -327,7 +327,7 @@
clearSheet();
if (styleSheetIsLoading())
- removePendingSheet(RemovePendingSheetNotifyLater);
+ removePendingSheet();
if (m_styleScope) {
m_styleScope->removeStyleSheetCandidateNode(*this);
@@ -554,7 +554,7 @@
m_styleScope->addPendingSheet();
}
-void HTMLLinkElement::removePendingSheet(RemovePendingSheetNotificationType notification)
+void HTMLLinkElement::removePendingSheet()
{
PendingSheetType type = m_pendingSheetType;
m_pendingSheetType = Unknown;
@@ -569,10 +569,7 @@
return;
}
- m_styleScope->removePendingSheet(
- notification == RemovePendingSheetNotifyImmediately
- ? Style::Scope::RemovePendingSheetNotifyImmediately
- : Style::Scope::RemovePendingSheetNotifyLater);
+ m_styleScope->removePendingSheet();
}
} // namespace WebCore
Modified: trunk/Source/WebCore/html/HTMLLinkElement.h (212462 => 212463)
--- trunk/Source/WebCore/html/HTMLLinkElement.h 2017-02-16 21:31:17 UTC (rev 212462)
+++ trunk/Source/WebCore/html/HTMLLinkElement.h 2017-02-16 22:04:32 UTC (rev 212463)
@@ -111,13 +111,8 @@
enum PendingSheetType { Unknown, ActiveSheet, InactiveSheet };
void addPendingSheet(PendingSheetType);
- enum RemovePendingSheetNotificationType {
- RemovePendingSheetNotifyImmediately,
- RemovePendingSheetNotifyLater
- };
+ void removePendingSheet();
- void removePendingSheet(RemovePendingSheetNotificationType = RemovePendingSheetNotifyImmediately);
-
LinkLoader m_linkLoader;
Style::Scope* m_styleScope { nullptr };
CachedResourceHandle<CachedCSSStyleSheet> m_cachedSheet;
Modified: trunk/Source/WebCore/html/parser/HTMLDocumentParser.cpp (212462 => 212463)
--- trunk/Source/WebCore/html/parser/HTMLDocumentParser.cpp 2017-02-16 21:31:17 UTC (rev 212462)
+++ trunk/Source/WebCore/html/parser/HTMLDocumentParser.cpp 2017-02-16 22:04:32 UTC (rev 212463)
@@ -87,7 +87,7 @@
void HTMLDocumentParser::detach()
{
- DocumentParser::detach();
+ ScriptableDocumentParser::detach();
if (m_scriptRunner)
m_scriptRunner->detach();
@@ -538,6 +538,11 @@
resumeParsingAfterScriptExecution();
}
+bool HTMLDocumentParser::hasScriptsWaitingForStylesheets() const
+{
+ return m_scriptRunner && m_scriptRunner->hasScriptsWaitingForStylesheets();
+}
+
void HTMLDocumentParser::executeScriptsWaitingForStylesheets()
{
// Document only calls this when the Document owns the DocumentParser
Modified: trunk/Source/WebCore/html/parser/HTMLDocumentParser.h (212462 => 212463)
--- trunk/Source/WebCore/html/parser/HTMLDocumentParser.h 2017-02-16 21:31:17 UTC (rev 212462)
+++ trunk/Source/WebCore/html/parser/HTMLDocumentParser.h 2017-02-16 22:04:32 UTC (rev 212463)
@@ -83,6 +83,7 @@
void stopParsing() final;
bool isWaitingForScripts() const override;
bool isExecutingScript() const final;
+ bool hasScriptsWaitingForStylesheets() const final;
void executeScriptsWaitingForStylesheets() final;
void suspendScheduledTasks() final;
void resumeScheduledTasks() final;
Modified: trunk/Source/WebCore/loader/DocumentLoader.cpp (212462 => 212463)
--- trunk/Source/WebCore/loader/DocumentLoader.cpp 2017-02-16 21:31:17 UTC (rev 212462)
+++ trunk/Source/WebCore/loader/DocumentLoader.cpp 2017-02-16 22:04:32 UTC (rev 212463)
@@ -68,6 +68,7 @@
#include "ResourceHandle.h"
#include "ResourceLoadObserver.h"
#include "SchemeRegistry.h"
+#include "ScriptableDocumentParser.h"
#include "SecurityPolicy.h"
#include "Settings.h"
#include "SubresourceLoader.h"
@@ -1057,6 +1058,9 @@
return true;
if (document.hasActiveParser())
return true;
+ auto* scriptableParser = document.scriptableDocumentParser();
+ if (scriptableParser && scriptableParser->hasScriptsWaitingForStylesheets())
+ return true;
}
return frameLoader()->subframeIsLoading();
}
Modified: trunk/Source/WebCore/loader/FrameLoader.cpp (212462 => 212463)
--- trunk/Source/WebCore/loader/FrameLoader.cpp 2017-02-16 21:31:17 UTC (rev 212462)
+++ trunk/Source/WebCore/loader/FrameLoader.cpp 2017-02-16 22:04:32 UTC (rev 212463)
@@ -787,6 +787,10 @@
if (m_frame.document()->isDelayingLoadEvent())
return;
+ auto* scriptableParser = m_frame.document()->scriptableDocumentParser();
+ if (scriptableParser && scriptableParser->hasScriptsWaitingForStylesheets())
+ return;
+
// Any frame that hasn't completed yet?
if (!allChildrenAreComplete())
return;
Modified: trunk/Source/WebCore/style/StyleScope.cpp (212462 => 212463)
--- trunk/Source/WebCore/style/StyleScope.cpp 2017-02-16 21:31:17 UTC (rev 212462)
+++ trunk/Source/WebCore/style/StyleScope.cpp 2017-02-16 22:04:32 UTC (rev 212463)
@@ -170,7 +170,7 @@
}
// This method is called whenever a top-level stylesheet has finished loading.
-void Scope::removePendingSheet(RemovePendingSheetNotificationType notification)
+void Scope::removePendingSheet()
{
// Make sure we knew this sheet was pending, and that our count isn't out of sync.
ASSERT(m_pendingStyleSheetCount > 0);
@@ -179,11 +179,6 @@
if (m_pendingStyleSheetCount)
return;
- if (notification == RemovePendingSheetNotifyLater) {
- m_document.setNeedsNotifyRemoveAllPendingStylesheet();
- return;
- }
-
didChangeActiveStyleSheetCandidates();
if (!m_shadowRoot)
Modified: trunk/Source/WebCore/style/StyleScope.h (212462 => 212463)
--- trunk/Source/WebCore/style/StyleScope.h 2017-02-16 21:31:17 UTC (rev 212462)
+++ trunk/Source/WebCore/style/StyleScope.h 2017-02-16 22:04:32 UTC (rev 212463)
@@ -82,11 +82,7 @@
void setSelectedStylesheetSetName(const String&);
void addPendingSheet() { m_pendingStyleSheetCount++; }
- enum RemovePendingSheetNotificationType {
- RemovePendingSheetNotifyImmediately,
- RemovePendingSheetNotifyLater
- };
- void removePendingSheet(RemovePendingSheetNotificationType = RemovePendingSheetNotifyImmediately);
+ void removePendingSheet();
bool hasPendingSheets() const { return m_pendingStyleSheetCount > 0; }