Bug 518104 - Implement HTML5 changes to <script defer>. r=hsivonen, sr=mrbkap. (Tests fixed and code merged to tip by hsivonen.)

This commit is contained in:
Jonas Sicking 2009-10-02 14:13:59 +03:00
parent 9d74609659
commit 0d2f92b52e
29 changed files with 95 additions and 136 deletions

View File

@ -357,11 +357,6 @@ nsContentSink::ScriptAvailable(nsresult aResult,
// Check if this is the element we were waiting for
if (count == 0 || aElement != mScriptElements[count - 1]) {
if (mDidGetReadyToCallDidBuildModelCall &&
!mScriptLoader->HasPendingOrCurrentScripts() &&
mParser && mParser->IsParserEnabled()) {
ContinueInterruptedParsingAsync();
}
return NS_OK;
}
@ -404,11 +399,6 @@ nsContentSink::ScriptEvaluated(nsresult aResult,
// Check if this is the element we were waiting for
PRInt32 count = mScriptElements.Count();
if (count == 0 || aElement != mScriptElements[count - 1]) {
if (mDidGetReadyToCallDidBuildModelCall &&
!mScriptLoader->HasPendingOrCurrentScripts() &&
mParser && mParser->IsParserEnabled()) {
ContinueInterruptedParsingAsync();
}
return NS_OK;
}
@ -1642,8 +1632,16 @@ nsContentSink::EndUpdate(nsIDocument *aDocument, nsUpdateType aUpdateType)
}
void
nsContentSink::DidBuildModelImpl(void)
nsContentSink::DidBuildModelImpl(PRBool aTerminated)
{
if (mDocument && !aTerminated) {
mDocument->SetReadyStateInternal(nsIDocument::READYSTATE_INTERACTIVE);
}
if (mScriptLoader) {
mScriptLoader->ParsingComplete(aTerminated);
}
if (!mDocument->HaveFiredDOMTitleChange()) {
mDocument->NotifyPossibleTitleChange(PR_FALSE);
}
@ -1768,26 +1766,6 @@ nsContentSink::ContinueInterruptedParsingAsync()
NS_DispatchToCurrentThread(ev);
}
PRBool
nsContentSink::ReadyToCallDidBuildModelImpl(PRBool aTerminated)
{
if (!mDidGetReadyToCallDidBuildModelCall) {
if (mDocument && !aTerminated) {
mDocument->SetReadyStateInternal(nsIDocument::READYSTATE_INTERACTIVE);
}
if (mScriptLoader) {
mScriptLoader->ParsingComplete(aTerminated);
}
}
mDidGetReadyToCallDidBuildModelCall = PR_TRUE;
// If we're terminated we always want to call DidBuildModel.
return aTerminated || !mScriptLoader ||
!mScriptLoader->HasPendingOrCurrentScripts();
}
// URIs: action, href, src, longdesc, usemap, cite
PRBool
IsAttrURI(nsIAtom *aName)

View File

@ -133,8 +133,7 @@ class nsContentSink : public nsICSSLoaderObserver,
NS_HIDDEN_(nsresult) WillResumeImpl(void);
NS_HIDDEN_(nsresult) DidProcessATokenImpl(void);
NS_HIDDEN_(void) WillBuildModelImpl(void);
NS_HIDDEN_(void) DidBuildModelImpl(void);
NS_HIDDEN_(PRBool) ReadyToCallDidBuildModelImpl(PRBool aTerminated);
NS_HIDDEN_(void) DidBuildModelImpl(PRBool aTerminated);
NS_HIDDEN_(void) DropParserAndPerfHint(void);
PRBool IsScriptExecutingImpl();
@ -338,8 +337,6 @@ protected:
PRUint8 mDeferredLayoutStart : 1;
// If true, we deferred notifications until sheets load
PRUint8 mDeferredFlushTags : 1;
// If true, we did get a ReadyToCallDidBuildModel call
PRUint8 mDidGetReadyToCallDidBuildModelCall : 1;
// If false, we're not ourselves a document observer; that means we
// shouldn't be performing any more content model notifications,
// since we're not longer updating our child counts.

View File

@ -238,6 +238,7 @@ _TEST_FILES = test_bug5141.html \
test_bug454326.html \
test_bug505783.html \
test_bug457746.html \
test_bug518104.html \
bug457746.sjs \
test_CrossSiteXHR.html \
file_CrossSiteXHR_inner.html \

View File

@ -37,7 +37,7 @@ onload = function () {
}
function done() {
is(res, "ABCDEFGHIJabcdef1M2g34hi", "scripts executed in the wrong order");
is(res, "AacBCDEFGeHIJfbd1M2g34hi", "scripts executed in the wrong order");
ok(!fHadExecuted, "Dynamic script executed too late");
SimpleTest.finish();
}

View File

@ -36,7 +36,7 @@ onload = function () {
}
function done() {
is(res, "ABCDEFGHIJabcdef1M2g34hi", "scripts executed in the wrong order");
is(res, "AacBCDEFGeHIJfbd1M2g34hi", "scripts executed in the wrong order");
ok(!fHadExecuted, "Dynamic script executed too late");
SimpleTest.finish();
}

View File

@ -23,24 +23,9 @@ function writeIt(n) {
document.write("<span>" + n + "</span>");
}
var recur = 0;
function externalScript() {
if (++recur == 3) {
return;
}
base = (recur-1) * 4
writeIt(6 + base);
s = document.createElement("script");
s.src = "data:text/plain,writeIt(" + (8+base) + ");writeIt(" + (9+base) + ");externalScript();";
document.body.appendChild(s);
writeIt(7 + base);
}
function done() {
nodes = document.getElementsByTagName('span');
is(nodes.length, 13, "wrong length");
is(nodes.length, 3, "wrong length");
for (i = 0; i < nodes.length; ++i) {
is(nodes[i].textContent, i+1, "wrong order");
}
@ -55,10 +40,8 @@ writeIt(1);
</script>
<script defer>
writeIt(3);
</script>
<script>
writeIt(2);
</script>
<script defer src="data:text/plain,writeIt(4);writeIt(5);"></script>
<script defer src="data:text/plain,externalScript();"></script>
<script>
writeIt(3);
</script>

View File

@ -0,0 +1,37 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=518104
-->
<head>
<title>Test for Bug 518104</title>
<script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=518104">Mozilla Bug 518104</a>
<p id="display"></p>
<div id="content" style="display: none">
<iframe id='iframe' src="data:text/html,
<div></div><div></div>
<script defer src='data:application/javascript,document.write(%2522<p></p>%2522);parent.done();document.close();'></script>">
</iframe>
</div>
<pre id="test">
<script class="testbody" type="text/javascript">
/** Test for Bug 518104 **/
SimpleTest.waitForExplicitFinish();
function done() {
var iframe = document.getElementById("iframe");
var divs = iframe.contentWindow.document.getElementsByTagName("div").length;
is(divs, 0, "Div wasn't blown away.")
SimpleTest.finish();
}
</script>
</pre>
</body>
</html>

View File

@ -521,8 +521,9 @@ nsHTMLScriptElement::GetScriptDeferred()
{
PRBool defer;
GetDefer(&defer);
nsCOMPtr<nsIURI> uri = GetScriptURI();
return defer;
return defer && uri;
}
PRBool

View File

@ -183,8 +183,7 @@ public:
// nsIContentSink
NS_IMETHOD WillParse(void);
NS_IMETHOD WillBuildModel(nsDTDMode aDTDMode);
NS_IMETHOD DidBuildModel(void);
virtual PRBool ReadyToCallDidBuildModel(PRBool aTerminated);
NS_IMETHOD DidBuildModel(PRBool aTerminated);
NS_IMETHOD WillInterrupt(void);
NS_IMETHOD WillResume(void);
NS_IMETHOD SetParser(nsIParser* aParser);
@ -1755,9 +1754,9 @@ HTMLContentSink::WillBuildModel(nsDTDMode aDTDMode)
}
NS_IMETHODIMP
HTMLContentSink::DidBuildModel(void)
HTMLContentSink::DidBuildModel(PRBool aTerminated)
{
DidBuildModelImpl();
DidBuildModelImpl(aTerminated);
// Reflow the last batch of content
if (mBody || mFrameset) {
@ -1805,12 +1804,6 @@ HTMLContentSink::DidBuildModel(void)
return NS_OK;
}
PRBool
HTMLContentSink::ReadyToCallDidBuildModel(PRBool aTerminated)
{
return ReadyToCallDidBuildModelImpl(aTerminated);
}
NS_IMETHODIMP
HTMLContentSink::SetParser(nsIParser* aParser)
{

View File

@ -90,7 +90,7 @@ public:
// nsIContentSink
NS_IMETHOD WillParse(void) { return NS_OK; }
NS_IMETHOD WillBuildModel(nsDTDMode aDTDMode);
NS_IMETHOD DidBuildModel(void);
NS_IMETHOD DidBuildModel(PRBool aTerminated);
NS_IMETHOD WillInterrupt(void);
NS_IMETHOD WillResume(void);
NS_IMETHOD SetParser(nsIParser* aParser);
@ -279,7 +279,7 @@ nsHTMLFragmentContentSink::WillBuildModel(nsDTDMode)
}
NS_IMETHODIMP
nsHTMLFragmentContentSink::DidBuildModel(void)
nsHTMLFragmentContentSink::DidBuildModel(PRBool aTerminated)
{
FlushText();
@ -667,7 +667,7 @@ nsHTMLFragmentContentSink::DidBuildContent()
{
if (!mAllContent) {
FlushText();
DidBuildModel(); // Release our ref to the parser now.
DidBuildModel(PR_FALSE); // Release our ref to the parser now.
mProcessing = PR_FALSE;
}

View File

@ -99,9 +99,9 @@ nsLoadSaveContentSink::WillBuildModel(nsDTDMode aDTDMode)
}
NS_IMETHODIMP
nsLoadSaveContentSink::DidBuildModel(void)
nsLoadSaveContentSink::DidBuildModel(PRBool aTerminated)
{
return mBaseSink->DidBuildModel();
return mBaseSink->DidBuildModel(aTerminated);
}
NS_IMETHODIMP

View File

@ -71,7 +71,7 @@ public:
// nsILoadSaveContentSink
NS_IMETHOD WillBuildModel(nsDTDMode aDTDMode);
NS_IMETHOD DidBuildModel(void);
NS_IMETHOD DidBuildModel(PRBool aTerminated);
NS_IMETHOD WillInterrupt(void);
NS_IMETHOD WillResume(void);
NS_IMETHOD SetParser(nsIParser* aParser);

View File

@ -303,9 +303,9 @@ CheckXSLTParamPI(nsIDOMProcessingInstruction* aPi,
}
NS_IMETHODIMP
nsXMLContentSink::DidBuildModel()
nsXMLContentSink::DidBuildModel(PRBool aTerminated)
{
DidBuildModelImpl();
DidBuildModelImpl(aTerminated);
if (mXSLTProcessor) {
// stop observing in order to avoid crashing when replacing content
@ -379,12 +379,6 @@ nsXMLContentSink::DidBuildModel()
return NS_OK;
}
PRBool
nsXMLContentSink::ReadyToCallDidBuildModel(PRBool aTerminated)
{
return ReadyToCallDidBuildModelImpl(aTerminated);
}
NS_IMETHODIMP
nsXMLContentSink::OnDocumentCreated(nsIDocument* aResultDocument)
{

View File

@ -93,8 +93,7 @@ public:
// nsIContentSink
NS_IMETHOD WillParse(void);
NS_IMETHOD WillBuildModel(nsDTDMode aDTDMode);
NS_IMETHOD DidBuildModel(void);
virtual PRBool ReadyToCallDidBuildModel(PRBool aTerminated);
NS_IMETHOD DidBuildModel(PRBool aTerminated);
NS_IMETHOD WillInterrupt(void);
NS_IMETHOD WillResume(void);
NS_IMETHOD SetParser(nsIParser* aParser);

View File

@ -95,7 +95,7 @@ public:
// nsIContentSink
NS_IMETHOD WillBuildModel(nsDTDMode aDTDMode);
NS_IMETHOD DidBuildModel();
NS_IMETHOD DidBuildModel(PRBool aTerminated);
NS_IMETHOD SetDocumentCharset(nsACString& aCharset);
virtual nsISupports *GetTarget();
NS_IMETHOD DidProcessATokenImpl();
@ -216,7 +216,7 @@ nsXMLFragmentContentSink::WillBuildModel(nsDTDMode aDTDMode)
}
NS_IMETHODIMP
nsXMLFragmentContentSink::DidBuildModel()
nsXMLFragmentContentSink::DidBuildModel(PRBool aTerminated)
{
if (mAllContent) {
PopContent(); // remove mRoot pushed above

View File

@ -109,7 +109,7 @@ public:
// nsIContentSink
NS_IMETHOD WillParse(void) { return NS_OK; }
NS_IMETHOD DidBuildModel();
NS_IMETHOD DidBuildModel(PRBool aTerminated);
NS_IMETHOD WillInterrupt(void) { return NS_OK; }
NS_IMETHOD WillResume(void) { return NS_OK; }
NS_IMETHOD SetParser(nsIParser* aParser) { return NS_OK; }
@ -244,7 +244,7 @@ txStylesheetSink::ReportError(const PRUnichar *aErrorText,
}
NS_IMETHODIMP
txStylesheetSink::DidBuildModel()
txStylesheetSink::DidBuildModel(PRBool aTerminated)
{
return mCompiler->doneLoading();
}

View File

@ -271,7 +271,7 @@ XULContentSinkImpl::WillBuildModel(nsDTDMode aDTDMode)
}
NS_IMETHODIMP
XULContentSinkImpl::DidBuildModel(void)
XULContentSinkImpl::DidBuildModel(PRBool aTerminated)
{
nsCOMPtr<nsIDocument> doc = do_QueryReferent(mDocument);
if (doc) {

View File

@ -71,7 +71,7 @@ public:
// nsIContentSink
NS_IMETHOD WillParse(void) { return NS_OK; }
NS_IMETHOD WillBuildModel(nsDTDMode aDTDMode);
NS_IMETHOD DidBuildModel(void);
NS_IMETHOD DidBuildModel(PRBool aTerminated);
NS_IMETHOD WillInterrupt(void);
NS_IMETHOD WillResume(void);
NS_IMETHOD SetParser(nsIParser* aParser);

View File

@ -425,12 +425,7 @@ nsHtml5Parser::Terminate(void)
// @see bug 108049
CancelParsingEvents();
#ifdef DEBUG
PRBool ready =
#endif
mExecutor->ReadyToCallDidBuildModel(PR_TRUE);
NS_ASSERTION(ready, "Should always be ready to call DidBuildModel here.");
return mExecutor->DidBuildModel();
return mExecutor->DidBuildModel(PR_TRUE);
}
NS_IMETHODIMP
@ -593,9 +588,9 @@ nsHtml5Parser::ParseUntilSuspend()
case STREAM_ENDING:
if (mStreamParser && !mStreamParser->IsDone()) { // may still have stream data left
mStreamParser->ParseUntilSuspend();
} else if (mExecutor->ReadyToCallDidBuildModel(PR_FALSE)) {
} else {
// no more data and not expecting more
mExecutor->DidBuildModel();
mExecutor->DidBuildModel(PR_FALSE);
}
return;
default:

View File

@ -581,9 +581,7 @@ nsHtml5StreamParser::ParseUntilSuspend()
return; // no more data for now but expecting more
case STREAM_ENDING:
mDone = PR_TRUE;
if (mExecutor->ReadyToCallDidBuildModel(PR_FALSE)) {
mExecutor->DidBuildModel();
}
mExecutor->DidBuildModel(PR_FALSE);
return; // no more data and not expecting more
default:
NS_NOTREACHED("It should be impossible to reach this.");

View File

@ -121,7 +121,7 @@ nsHtml5TreeOpExecutor::WillParse()
// This is called when the tree construction has ended
NS_IMETHODIMP
nsHtml5TreeOpExecutor::DidBuildModel()
nsHtml5TreeOpExecutor::DidBuildModel(PRBool aTerminated)
{
NS_ASSERTION(mLifeCycle == STREAM_ENDING, "Bad life cycle.");
mLifeCycle = TERMINATED;
@ -131,7 +131,7 @@ nsHtml5TreeOpExecutor::DidBuildModel()
}
GetTokenizer()->end();
// This is comes from nsXMLContentSink
DidBuildModelImpl();
DidBuildModelImpl(aTerminated);
mDocument->ScriptLoader()->RemoveObserver(this);
nsContentSink::StartLayout(PR_FALSE);
ScrollToRef();

View File

@ -142,11 +142,6 @@ class nsHtml5TreeOpExecutor : public nsIContentSink,
// nsIContentSink
virtual PRBool ReadyToCallDidBuildModel(PRBool aTerminated)
{
return ReadyToCallDidBuildModelImpl(aTerminated);
};
/**
* Unimplemented. For interface compat only.
*/
@ -166,7 +161,7 @@ class nsHtml5TreeOpExecutor : public nsIContentSink,
/**
* Emits EOF.
*/
NS_IMETHOD DidBuildModel();
NS_IMETHOD DidBuildModel(PRBool aTerminated);
/**
* Forwards to nsContentSink

View File

@ -56,10 +56,10 @@
class nsIParser;
// 5530ebaf-f9fd-44bf-b6b5-e46f3b67eb3d
// 57b395ad-4276-408c-9f98-7044b5025c3d
#define NS_ICONTENT_SINK_IID \
{ 0x5530ebaf, 0xf9fd, 0x44bf, \
{ 0xb6, 0xb5, 0xe4, 0x6f, 0x3b, 0x67, 0xeb, 0x3d } }
{ 0x57b395ad, 0x4276, 0x408c, \
{ 0x9f, 0x98, 0x70, 0x44, 0xb5, 0x02, 0x5c, 0x3d } }
class nsIContentSink : public nsISupports {
public:
@ -96,21 +96,10 @@ public:
*
* @update 5/7/98 gess
*/
NS_IMETHOD DidBuildModel() {
NS_IMETHOD DidBuildModel(PRBool aTerminated) {
return NS_OK;
}
/**
* Thie method gets caller right before DidBuildModel is called.
* If false, the parser won't call DidBuildModel yet.
*
* If aTerminated is true, the parser has been terminated.
*/
virtual PRBool ReadyToCallDidBuildModel(PRBool aTerminated)
{
return PR_TRUE;
};
/**
* This method gets called when the parser gets i/o blocked,
* and wants to notify the sink that it may be a while before

View File

@ -113,7 +113,7 @@ nsLoggingSink::WillBuildModel(nsDTDMode aDTDMode) {
}
NS_IMETHODIMP
nsLoggingSink::DidBuildModel() {
nsLoggingSink::DidBuildModel(PRBool aTerminated) {
WriteTabs(mOutput,--mLevel);
PR_fprintf(mOutput, "</begin>\n");
@ -121,7 +121,7 @@ nsLoggingSink::DidBuildModel() {
//proxy the call to the real sink if you have one.
nsresult theResult=NS_OK;
if(mSink) {
theResult=mSink->DidBuildModel();
theResult=mSink->DidBuildModel(aTerminated);
}
return theResult;

View File

@ -62,7 +62,7 @@ public:
// nsIContentSink
NS_IMETHOD WillParse();
NS_IMETHOD WillBuildModel(nsDTDMode aDTDMode);
NS_IMETHOD DidBuildModel();
NS_IMETHOD DidBuildModel(PRBool aTerminated);
NS_IMETHOD WillInterrupt();
NS_IMETHOD WillResume();
NS_IMETHOD SetParser(nsIParser* aParser);

View File

@ -1602,10 +1602,9 @@ nsParser::DidBuildModel(nsresult anErrorCode)
// Let sink know if we're about to end load because we've been terminated.
// In that case we don't want it to run deferred scripts.
PRBool terminated = mInternalState == NS_ERROR_HTMLPARSER_STOPPARSING;
if (mDTD && mSink &&
mSink->ReadyToCallDidBuildModel(terminated)) {
if (mDTD && mSink) {
nsresult dtdResult = mDTD->DidBuildModel(anErrorCode),
sinkResult = mSink->DidBuildModel();
sinkResult = mSink->DidBuildModel(terminated);
// nsIDTD::DidBuildModel used to be responsible for calling
// nsIContentSink::DidBuildModel, but that obligation isn't expressible
// in the nsIDTD interface itself, so it's sounder and simpler to give
@ -1768,7 +1767,7 @@ nsParser::Terminate(void)
} else if (mSink) {
// We have no parser context or no DTD yet (so we got terminated before we
// got any data). Manually break the reference cycle with the sink.
result = mSink->DidBuildModel();
result = mSink->DidBuildModel(PR_TRUE);
NS_ENSURE_SUCCESS(result, result);
}

View File

@ -98,7 +98,7 @@ nsSAXXMLReader::WillBuildModel(nsDTDMode)
}
NS_IMETHODIMP
nsSAXXMLReader::DidBuildModel()
nsSAXXMLReader::DidBuildModel(PRBool aTerminated)
{
if (mContentHandler)
return mContentHandler->EndDocument();

View File

@ -78,7 +78,7 @@ public:
}
NS_IMETHOD WillBuildModel(nsDTDMode aDTDMode);
NS_IMETHOD DidBuildModel();
NS_IMETHOD DidBuildModel(PRBool aTerminated);
NS_IMETHOD SetParser(nsIParser* aParser);
NS_IMETHOD WillInterrupt()

View File

@ -162,7 +162,7 @@ public:
// nsIContentSink
NS_IMETHOD WillParse(void);
NS_IMETHOD WillBuildModel(nsDTDMode aDTDMode);
NS_IMETHOD DidBuildModel(void);
NS_IMETHOD DidBuildModel(PRBool aTerminated);
NS_IMETHOD WillInterrupt(void);
NS_IMETHOD WillResume(void);
NS_IMETHOD SetParser(nsIParser* aParser);
@ -632,7 +632,7 @@ RDFContentSinkImpl::WillBuildModel(nsDTDMode)
}
NS_IMETHODIMP
RDFContentSinkImpl::DidBuildModel(void)
RDFContentSinkImpl::DidBuildModel(PRBool aTerminated)
{
if (mDataSource) {
nsCOMPtr<nsIRDFXMLSink> sink = do_QueryInterface(mDataSource);