Bug 640158 - restrict restoring persisted attributes when loading overlays after the first document load, r=bz

This commit is contained in:
Gijs Kruitbosch 2013-12-04 17:15:52 +01:00
parent 6e876b5db7
commit 547162ab7c
5 changed files with 86 additions and 0 deletions

View File

@ -217,6 +217,9 @@ XULDocument::~XULDocument()
// In case we failed somewhere early on and the forward observer
// decls never got resolved.
mForwardReferences.Clear();
// Likewise for any references we have to IDs where we might
// look for persisted data:
mPersistenceIds.Clear();
// Destroy our broadcaster map.
if (mBroadcasterMap) {
@ -2180,6 +2183,11 @@ XULDocument::ApplyPersistentAttributes()
ApplyPersistentAttributesInternal();
mApplyingPersistedAttrs = false;
// After we've applied persistence once, we should only reapply
// it to nodes created by overlays
mRestrictPersistence = true;
mPersistenceIds.Clear();
return NS_OK;
}
@ -2224,6 +2232,9 @@ XULDocument::ApplyPersistentAttributesInternal()
if (id.IsEmpty())
continue;
if (mRestrictPersistence && !mPersistenceIds.Contains(id))
continue;
// This will clear the array if there are no elements.
GetElementsForID(id, elements);
@ -2976,6 +2987,15 @@ XULDocument::ResumeWalk()
rv = element->AppendChildTo(child, false);
if (NS_FAILED(rv)) return rv;
// If we're only restoring persisted things on
// some elements, store the ID here to do that.
if (mRestrictPersistence) {
nsIAtom* id = child->GetID();
if (id) {
mPersistenceIds.PutEntry(nsDependentAtomString(id));
}
}
// do pre-order document-level hookup, but only if
// we're in the master document. For an overlay,
// this will happen when the overlay is

View File

@ -325,6 +325,12 @@ protected:
*/
bool mStillWalking;
/**
* These two values control where persistent attributes get applied.
*/
bool mRestrictPersistence;
nsTHashtable<nsStringHashKey> mPersistenceIds;
/**
* An array of style sheets, that will be added (preserving order) to the
* document after all of them are loaded (in DoneWalking).

View File

@ -18,4 +18,5 @@ support-files =
[test_bug468176.xul]
[test_bug497875.xul]
[test_bug583948.xul]
[test_bug640158_overlay_persist.xul]
[test_bug757137.xul]

View File

@ -0,0 +1,8 @@
<?xml version="1.0"?>
<overlay id="overlay1"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<window id="rootwin">
<box id="bar" testattr="original"/>
</window>
</overlay>

View File

@ -0,0 +1,51 @@
<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css" type="text/css"?>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=640158
-->
<window title="Mozilla Bug 640158" id="rootwin"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
<!-- test results are displayed in the html:body -->
<body xmlns="http://www.w3.org/1999/xhtml">
<a href="https://bugzilla.mozilla.org/show_bug.cgi?id=640158"
target="_blank">Mozilla Bug 640158</a>
</body>
<!-- test code goes here -->
<script type="application/javascript"><![CDATA[
SimpleTest.waitForExplicitFinish();
window.onload = function onload() {
is($("foo").getAttribute("testattr"), "original", "Attribute should be in original state");
// Change and persist another value:
$("foo").setAttribute("testattr", "changed");
document.persist("foo", "testattr");
$("foo").setAttribute("testattr", "original");
// Hacky times: check that items which are overlaid do get persisted into correctly,
// by first creating an extra element and persisting the value before loading an
// overlay that changes that value - the persisted value should be reinstated.
let root = document.documentElement;
let bar = document.createElement("box");
bar.id = "bar";
bar.setAttribute("testattr", "changed"); // The overlay we load has 'original'
root.appendChild(bar);
document.persist("bar", "testattr");
document.loadOverlay(location.href.replace(/[^\\\/]*.xul/, "overlay_bug640158.xul"), function() {
is($("foo").getAttribute("testattr"), "original",
"Non-overlaid attribute should still be in original state");
is($("bar").getAttribute("testattr"), "changed",
"Overlaid attribute should have been changed.");
SimpleTest.finish();
});
}
]]></script>
<box id="foo" testattr="original"/>
</window>