Bug 604660 - Make XSLT-inserted script execute in the insertion order. r=jonas, a=blocking2.0-beta7.

--HG--
extra : rebase_source : fde633f62de8b4ec5af5c85f21c01ed4781e06e3
This commit is contained in:
Henri Sivonen 2010-10-27 10:18:24 +03:00
parent 024d857be8
commit 6b5b74d541
13 changed files with 211 additions and 15 deletions

View File

@ -47,7 +47,8 @@ enum FromParser {
NOT_FROM_PARSER = 0,
FROM_PARSER_NETWORK = 1,
FROM_PARSER_DOCUMENT_WRITE = 1 << 1,
FROM_PARSER_FRAGMENT = 1 << 2
FROM_PARSER_FRAGMENT = 1 << 2,
FROM_PARSER_XSLT = 1 << 3
};
} // namespace dom

View File

@ -66,7 +66,10 @@ public:
mFrozen(PR_FALSE),
mDefer(PR_FALSE),
mAsync(PR_FALSE),
mParserCreated(aFromParser),
mParserCreated(aFromParser == mozilla::dom::FROM_PARSER_FRAGMENT ?
mozilla::dom::NOT_FROM_PARSER : aFromParser),
// Fragment parser-created scripts (if executable)
// behave like script-created scripts.
mCreatorParser(nsnull)
{
}

View File

@ -157,6 +157,10 @@ nsScriptLoader::~nsScriptLoader()
mParserBlockingRequest->FireScriptAvailable(NS_ERROR_ABORT);
}
for (PRUint32 i = 0; i < mXSLTRequests.Length(); i++) {
mXSLTRequests[i]->FireScriptAvailable(NS_ERROR_ABORT);
}
for (PRUint32 i = 0; i < mDeferRequests.Length(); i++) {
mDeferRequests[i]->FireScriptAvailable(NS_ERROR_ABORT);
}
@ -577,8 +581,9 @@ nsScriptLoader::ProcessScriptElement(nsIScriptElement *aElement)
// a parser-inserted script is being run, we came here by the parser
// running the script, which means the parser is still alive and the
// parse is ongoing.
NS_ASSERTION(mDocument->GetCurrentContentSink(),
"Defer script on a document without an active parser; bug 592366.");
NS_ASSERTION(mDocument->GetCurrentContentSink() ||
aElement->GetParserCreated() == FROM_PARSER_XSLT,
"Non-XSLT Defer script on a document without an active parser; bug 592366.");
mDeferRequests.AppendElement(request);
return NS_OK;
}
@ -591,6 +596,19 @@ nsScriptLoader::ProcessScriptElement(nsIScriptElement *aElement)
}
return NS_OK;
}
if (aElement->GetParserCreated() == FROM_PARSER_XSLT) {
// Need to maintain order for XSLT-inserted scripts
NS_ASSERTION(!mParserBlockingRequest,
"Parser-blocking scripts and XSLT scripts in the same doc!");
mXSLTRequests.AppendElement(request);
if (!request->mLoading) {
// The script is available already. Run it ASAP when the event
// loop gets a chance to spin.
ProcessPendingRequestsAsync();
}
return NS_ERROR_HTMLPARSER_BLOCK;
}
if (!request->mLoading && ReadyToExecuteScripts()) {
// The request has already been loaded and there are no pending style
// sheets. If the script comes from the network stream, cheat for
@ -603,6 +621,8 @@ nsScriptLoader::ProcessScriptElement(nsIScriptElement *aElement)
// Web page.
NS_ASSERTION(!mParserBlockingRequest,
"There can be only one parser-blocking script at a time");
NS_ASSERTION(mXSLTRequests.IsEmpty(),
"Parser-blocking scripts and XSLT scripts in the same doc!");
mParserBlockingRequest = request;
ProcessPendingRequestsAsync();
return NS_ERROR_HTMLPARSER_BLOCK;
@ -611,6 +631,8 @@ nsScriptLoader::ProcessScriptElement(nsIScriptElement *aElement)
// The script will be run when it loads or the style sheet loads.
NS_ASSERTION(!mParserBlockingRequest,
"There can be only one parser-blocking script at a time");
NS_ASSERTION(mXSLTRequests.IsEmpty(),
"Parser-blocking scripts and XSLT scripts in the same doc!");
mParserBlockingRequest = request;
return NS_ERROR_HTMLPARSER_BLOCK;
}
@ -641,6 +663,14 @@ nsScriptLoader::ProcessScriptElement(nsIScriptElement *aElement)
request->mURI = mDocument->GetDocumentURI();
request->mLineNo = aElement->GetScriptLineNumber();
if (aElement->GetParserCreated() == FROM_PARSER_XSLT &&
(!ReadyToExecuteScripts() || !mXSLTRequests.IsEmpty())) {
// Need to maintain order for XSLT-inserted scripts
NS_ASSERTION(!mParserBlockingRequest,
"Parser-blocking scripts and XSLT scripts in the same doc!");
mXSLTRequests.AppendElement(request);
return NS_ERROR_HTMLPARSER_BLOCK;
}
if (aElement->GetParserCreated() == NOT_FROM_PARSER) {
NS_ASSERTION(!nsContentUtils::IsSafeToRunScript(),
"A script-inserted script is inserted without an update batch?");
@ -653,6 +683,8 @@ nsScriptLoader::ProcessScriptElement(nsIScriptElement *aElement)
NS_ASSERTION(!mParserBlockingRequest,
"There can be only one parser-blocking script at a time");
mParserBlockingRequest = request;
NS_ASSERTION(mXSLTRequests.IsEmpty(),
"Parser-blocking scripts and XSLT scripts in the same doc!");
return NS_ERROR_HTMLPARSER_BLOCK;
}
// We now have a document.written inline script or we have an inline script
@ -856,6 +888,14 @@ nsScriptLoader::ProcessPendingRequests()
ProcessRequest(request);
}
while (ReadyToExecuteScripts() &&
!mXSLTRequests.IsEmpty() &&
!mXSLTRequests[0]->mLoading) {
request.swap(mXSLTRequests[0]);
mXSLTRequests.RemoveElementAt(0);
ProcessRequest(request);
}
PRUint32 i = 0;
while (mEnabled && i < mAsyncRequests.Length()) {
if (!mAsyncRequests[i]->mLoading) {
@ -867,7 +907,7 @@ nsScriptLoader::ProcessPendingRequests()
++i;
}
if (mDocumentParsingDone) {
if (mDocumentParsingDone && mXSLTRequests.IsEmpty()) {
while (!mDeferRequests.IsEmpty() && !mDeferRequests[0]->mLoading) {
request.swap(mDeferRequests[0]);
mDeferRequests.RemoveElementAt(0);
@ -883,7 +923,7 @@ nsScriptLoader::ProcessPendingRequests()
if (mDocumentParsingDone && mDocument &&
!mParserBlockingRequest && mAsyncRequests.IsEmpty() &&
mDeferRequests.IsEmpty()) {
mXSLTRequests.IsEmpty() && mDeferRequests.IsEmpty()) {
// No more pending scripts; time to unblock onload.
// OK to unblock onload synchronously here, since callers must be
// prepared for the world changing anyway.
@ -1053,7 +1093,8 @@ nsScriptLoader::OnStreamComplete(nsIStreamLoader* aLoader,
aString);
if (NS_FAILED(rv)) {
if (mDeferRequests.RemoveElement(request) ||
mAsyncRequests.RemoveElement(request)) {
mAsyncRequests.RemoveElement(request) ||
mXSLTRequests.RemoveElement(request)) {
FireScriptAvailable(rv, request);
} else if (mParserBlockingRequest == request) {
mParserBlockingRequest = nsnull;
@ -1131,6 +1172,7 @@ nsScriptLoader::PrepareLoadedRequest(nsScriptLoadRequest* aRequest,
// wrong, especially if you see it more than once.
NS_ASSERTION(mDeferRequests.IndexOf(aRequest) >= 0 ||
mAsyncRequests.IndexOf(aRequest) >= 0 ||
mXSLTRequests.IndexOf(aRequest) >= 0 ||
mPreloads.Contains(aRequest, PreloadRequestComparator()) ||
mParserBlockingRequest,
"aRequest should be pending!");
@ -1183,6 +1225,7 @@ nsScriptLoader::ParsingComplete(PRBool aTerminated)
if (aTerminated) {
mDeferRequests.Clear();
mAsyncRequests.Clear();
mXSLTRequests.Clear();
mParserBlockingRequest = nsnull;
}

View File

@ -299,6 +299,7 @@ private:
nsCOMArray<nsIScriptLoaderObserver> mObservers;
nsTArray<nsRefPtr<nsScriptLoadRequest> > mAsyncRequests;
nsTArray<nsRefPtr<nsScriptLoadRequest> > mDeferRequests;
nsTArray<nsRefPtr<nsScriptLoadRequest> > mXSLTRequests;
nsRefPtr<nsScriptLoadRequest> mParserBlockingRequest;
// In mRequests, the additional information here is stored by the element.

View File

@ -431,6 +431,13 @@ _TEST_FILES2 = \
test_bug601803.html \
file_bug601803a.html \
file_bug601803b.html \
test_bug604660.html \
file_bug604660-1.xml \
file_bug604660-2.xsl \
file_bug604660-3.js \
file_bug604660-4.js \
file_bug604660-5.xml \
file_bug604660-6.xsl \
test_bug605982.html \
$(NULL)

View File

@ -0,0 +1,3 @@
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="file_bug604660-2.xsl" ?>
<placeholder/>

View File

@ -0,0 +1,19 @@
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">
<xsl:template match="/">
<html>
<head>
<title>XSLT script execution test</title>
</head>
<body>
<script defer="" src="data:text/javascript,parent.scriptRan(5);"></script>
<script>parent.scriptRan(1);</script>
<script async="" src="data:text/javascript,parent.asyncRan();"></script>
<script src="file_bug604660-3.js"></script>
<script>parent.scriptRan(3);</script>
<script src="file_bug604660-4.js"></script>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

View File

@ -0,0 +1 @@
parent.scriptRan(2);

View File

@ -0,0 +1 @@
parent.scriptRan(4);

View File

@ -0,0 +1,2 @@
<?xml version="1.0"?>
<placeholder/>

View File

@ -0,0 +1,9 @@
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">
<xsl:template match="/">
<html>
<script>xsltProcessorCreatedScriptRan();</script>
</html>
</xsl:template>
</xsl:stylesheet>

View File

@ -0,0 +1,77 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=604660
-->
<head>
<title>Test for Bug 604660</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=604660">Mozilla Bug 604660</a>
<script>
SimpleTest.waitForExplicitFinish();
var asyncState = false;
var scriptState = 0;
function scriptRan(num) {
++scriptState;
is(scriptState, num, "Scripts ran in the wrong sequence.");
}
function asyncRan() {
asyncState = true;
}
</script>
<p id="display"><iframe src="file_bug604660-1.xml" onload="iframeloaded()";></iframe></p>
<div id="content" style="display: none">
</div>
<pre id="test">
<script class="testbody" type="text/javascript">
var xlstProcessorState = false;
function xsltProcessorCreatedScriptRan() {
xlstProcessorState = true;
}
function iframeloaded() {
ok(asyncState, "Async script should have run.");
is(scriptState, 5, "Five scripts should have run.");
var processor = new XSLTProcessor();
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (this.readyState == 4) {
processor.importStylesheet(this.responseXML);
xhr.onreadystatechange = function() {
if (this.readyState == 4) {
var doc = processor.transformToDocument(this.responseXML);
var target = document.getElementById("display");
target.appendChild(doc.documentElement.firstChild);
ok(!xlstProcessorState, "Scripts created by transformToDocument should not run.");
var fragment = processor.transformToFragment(this.responseXML, document);
target.appendChild(fragment.firstChild.firstChild);
ok(xlstProcessorState, "Scripts created by transformToFragment should run.");
SimpleTest.finish();
}
}
xhr.open("GET", "file_bug604660-5.xml");
xhr.send();
}
}
xhr.open("GET", "file_bug604660-6.xsl");
xhr.send();
}
</script>
</pre>
</body>
</html>

View File

@ -259,8 +259,15 @@ txMozillaXMLOutput::endDocument(nsresult aResult)
return rv;
}
// This should really be handled by nsIDocument::EndLoad
mDocument->SetReadyStateInternal(nsIDocument::READYSTATE_INTERACTIVE);
if (mCreatingNewDocument) {
// This should really be handled by nsIDocument::EndLoad
mDocument->SetReadyStateInternal(nsIDocument::READYSTATE_INTERACTIVE);
nsScriptLoader* loader = mDocument->ScriptLoader();
if (loader) {
loader->ParsingComplete(PR_FALSE);
}
}
if (!mRefreshString.IsEmpty()) {
nsPIDOMWindow *win = mDocument->GetWindow();
@ -312,10 +319,18 @@ txMozillaXMLOutput::endElement()
NS_ENSURE_SUCCESS(rv, rv);
}
// Handle script elements
if (element->Tag() == nsGkAtoms::script &&
(element->IsHTML() ||
element->GetNameSpaceID() == kNameSpaceID_SVG)) {
// Handle elements that are different when parser-created
PRInt32 ns = element->GetNameSpaceID();
nsIAtom* localName = element->Tag();
if ((ns == kNameSpaceID_XHTML && (localName == nsGkAtoms::script ||
localName == nsGkAtoms::title ||
localName == nsGkAtoms::object ||
localName == nsGkAtoms::applet ||
localName == nsGkAtoms::select ||
localName == nsGkAtoms::textarea)) ||
(ns == kNameSpaceID_SVG && (localName == nsGkAtoms::script ||
localName == nsGkAtoms::title))) {
rv = element->DoneAddingChildren(PR_TRUE);
@ -326,6 +341,10 @@ txMozillaXMLOutput::endElement()
rv = mNotifier->AddScriptElement(sele);
NS_ENSURE_SUCCESS(rv, rv);
}
} else if (ns == kNameSpaceID_XHTML &&
(localName == nsGkAtoms::input ||
localName == nsGkAtoms::button)) {
element->DoneCreatingElement();
}
}
@ -433,6 +452,13 @@ txMozillaXMLOutput::startDocument()
mNotifier->OnTransformStart();
}
if (mCreatingNewDocument) {
nsScriptLoader* loader = mDocument->ScriptLoader();
if (loader) {
loader->BeginDeferringScripts();
}
}
return NS_OK;
}
@ -539,7 +565,8 @@ txMozillaXMLOutput::startElementInternal(nsIAtom* aPrefix,
NS_ENSURE_TRUE(ni, NS_ERROR_OUT_OF_MEMORY);
NS_NewElement(getter_AddRefs(mOpenedElement), aNsID, ni.forget(),
NOT_FROM_PARSER);
mCreatingNewDocument ?
FROM_PARSER_XSLT : FROM_PARSER_FRAGMENT);
// Set up the element and adjust state
if (!mNoFixup) {
@ -950,7 +977,9 @@ txMozillaXMLOutput::createHTMLElement(nsIAtom* aName,
kNameSpaceID_XHTML);
NS_ENSURE_TRUE(ni, NS_ERROR_OUT_OF_MEMORY);
return NS_NewHTMLElement(aResult, ni.forget(), NOT_FROM_PARSER);
return NS_NewHTMLElement(aResult, ni.forget(), mCreatingNewDocument ?
FROM_PARSER_XSLT : FROM_PARSER_FRAGMENT);
}
txTransformNotifier::txTransformNotifier()