Source code
Revision control
Copy as Markdown
Other Tools
<!DOCTYPE html>
<meta charset="utf-8">
<link rel="author" href="mailto:masonf@chromium.org">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id="container"></div>
<script>
test((t) => {
const container = document.getElementById('container');
// Create a sandboxed iframe without allow-scripts so its script context
// is not initialized until AddNamedItem accesses its WindowProxy.
const sandbox = document.createElement('iframe');
sandbox.sandbox = 'allow-same-origin';
container.appendChild(sandbox);
t.add_cleanup(() => sandbox.remove());
const innerDoc = sandbox.contentDocument;
const img = innerDoc.createElement('img');
// An empty name attribute exists in the attribute vector at index 0,
// but does not register a named item on insertion, leaving the sandboxed
// frame's WindowProxy uninitialized.
img.setAttribute('name', '');
for (let i = 0; i < 7; i++) {
img.setAttribute('data-a' + i, 'v');
}
innerDoc.body.appendChild(img);
// Name the child frame 'chrome' so that when LocalWindowProxy::Initialize()
// runs DidCreateScriptContext, content::GetOrCreateChromeObject() looks up
// window.chrome on innerDoc's global, gets child.contentWindow via named
// frame access, and sets 'loadTimes' on it, invoking the JS setter below
// synchronously inside WillModifyAttribute().
const child = innerDoc.createElement('iframe');
child.name = 'chrome';
innerDoc.body.appendChild(child);
Object.defineProperty(child.contentWindow, 'loadTimes', {
configurable: true,
set() {
for (let i = 7; i < 20; i++) {
img.setAttribute('data-a' + i, 'v');
}
}
});
// Modifying the existing 'name' attribute calls WillModifyAttribute ->
// UpdateName -> AddNamedItem, which initializes the WindowProxy and invokes
// the loadTimes setter above, reallocating img's attribute vector.
img.setAttribute('name', 'target_item');
assert_equals(img.getAttribute('name'), 'target_item');
assert_equals(innerDoc.target_item, img);
}, 'Reallocating attributes during WillModifyAttribute in SetAttributeInternal should not dangle or lose the new value');
test((t) => {
const container = document.getElementById('container');
const sandbox = document.createElement('iframe');
sandbox.sandbox = 'allow-same-origin';
container.appendChild(sandbox);
t.add_cleanup(() => sandbox.remove());
const innerDoc = sandbox.contentDocument;
const img = innerDoc.createElement('img');
img.setAttribute('data-first', '1');
img.setAttribute('name', '');
for (let i = 0; i < 7; i++) {
img.setAttribute('data-a' + i, 'v');
}
innerDoc.body.appendChild(img);
// Name the child frame 'chrome' so that when LocalWindowProxy::Initialize()
// runs DidCreateScriptContext, content::GetOrCreateChromeObject() looks up
// window.chrome on innerDoc's global, gets child.contentWindow via named
// frame access, and sets 'loadTimes' on it, invoking the JS setter below
// synchronously inside WillModifyAttribute().
const child = innerDoc.createElement('iframe');
child.name = 'chrome';
innerDoc.body.appendChild(child);
let setterRan = false;
Object.defineProperty(child.contentWindow, 'loadTimes', {
configurable: true,
set() {
setterRan = true;
// Remove an earlier attribute so 'name' shifts from index 1 to index 0,
// and append enough attributes to reallocate the vector.
img.removeAttribute('data-first');
for (let i = 7; i < 20; i++) {
img.setAttribute('data-a' + i, 'v');
}
}
});
img.setAttribute('name', 'shifted_item');
if (setterRan) {
assert_equals(img.getAttribute('data-first'), null);
}
assert_equals(img.getAttribute('name'), 'shifted_item');
assert_equals(img.getAttribute('data-a0'), 'v');
assert_equals(innerDoc.shifted_item, img);
}, 'Removing earlier attributes during WillModifyAttribute in SetAttributeInternal should update the re-validated index');
test((t) => {
const container = document.getElementById('container');
const sandbox = document.createElement('iframe');
sandbox.sandbox = 'allow-same-origin';
container.appendChild(sandbox);
t.add_cleanup(() => sandbox.remove());
const innerDoc = sandbox.contentDocument;
const img = innerDoc.createElement('img');
innerDoc.body.appendChild(img);
// Name the child frame 'chrome' so that when LocalWindowProxy::Initialize()
// runs DidCreateScriptContext, content::GetOrCreateChromeObject() looks up
// window.chrome on innerDoc's global, gets child.contentWindow via named
// frame access, and sets 'loadTimes' on it, invoking the JS setter below
// synchronously inside WillModifyAttribute().
const child = innerDoc.createElement('iframe');
child.name = 'chrome';
innerDoc.body.appendChild(child);
Object.defineProperty(child.contentWindow, 'loadTimes', {
configurable: true,
set() {
img.setAttribute('name', 'inner_name');
for (let i = 0; i < 20; i++) {
img.setAttribute('data-a' + i, 'v');
}
}
});
img.setAttribute('name', 'outer_name');
assert_equals(img.getAttribute('name'), 'outer_name');
let nameAttrCount = 0;
for (const attr of img.attributes) {
if (attr.name === 'name') {
nameAttrCount++;
}
}
assert_equals(nameAttrCount, 1);
}, 'Setting the same attribute re-entrantly during AppendAttributeInternal should not create duplicate attributes');
test((t) => {
const container = document.getElementById('container');
const quirksFrame = document.createElement('iframe');
container.appendChild(quirksFrame);
t.add_cleanup(() => quirksFrame.remove());
const quirksDoc = quirksFrame.contentDocument;
quirksDoc.open();
quirksDoc.write('<div id="MyUpperId" class="MyUpperClass"></div>');
quirksDoc.close();
assert_equals(quirksDoc.compatMode, 'BackCompat');
const div = quirksDoc.getElementById('MyUpperId');
document.adoptNode(div);
assert_equals(div.id, 'MyUpperId');
assert_equals(div.className, 'MyUpperClass');
}, 'Adopting an element with uppercase id and class between quirks and standards mode documents');
</script>