Bug 725907 - for-of improvements, part 3: Implement .iterator() for arraylike DOM objects. r=bzbarsky.

This commit is contained in:
Jason Orendorff 2012-07-03 16:34:56 -05:00
Родитель 82b55a13fe
Коммит 1e1b73d49c
5 изменённых файлов: 113 добавлений и 7 удалений

Просмотреть файл

@ -66,6 +66,8 @@ MOCHITEST_FILES = \
test_lookupGetter.html \ test_lookupGetter.html \
test_InstanceOf.html \ test_InstanceOf.html \
test_traceProtos.html \ test_traceProtos.html \
test_forOf.html \
forOf_iframe.html \
$(NULL) $(NULL)

Просмотреть файл

@ -0,0 +1,13 @@
<!DOCTYPE HTML>
<html>
<head>
<title>iframe content for test_forOf_iframe.html</title>
</head>
<body>
<div id="basket">
<span id="egg0"></span>
<span id="egg1"><span id="duckling1"></span></span>
<span id="egg2"></span>
</div>
</body>
</html>

Просмотреть файл

@ -0,0 +1,94 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=725907
-->
<head>
<meta charset="utf-8">
<title>Test for Bug 725907</title>
<script type="application/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=725907">Mozilla Bug 725907</a>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<div id="basket">
<span id="egg0"></span>
<span id="egg1"><span id="duckling1"></span></span>
<span id="egg2"></span>
</div>
<pre id="test">
<script type="application/javascript">
/** Test for Bug 725907 **/
function runTestsForDocument(document, msgSuffix) {
function is(a, b, msg) { SimpleTest.is(a, b, msg + msgSuffix); }
function isnot(a, b, msg) { SimpleTest.isnot(a, b, msg + msgSuffix); }
var basket = document.getElementById("basket");
var egg3 = document.createElement("span");
egg3.id = "egg3";
var log = '';
for (var x of basket.childNodes) {
if (x.nodeType != x.TEXT_NODE)
log += x.id + ";";
}
is(log, "egg0;egg1;egg2;", "'for (x of div.childNodes)' should iterate over child nodes");
log = '';
for (var x of basket.childNodes) {
if (x.nodeType != x.TEXT_NODE) {
log += x.id + ";";
if (x.id == "egg1")
basket.appendChild(egg3);
}
}
is(log, "egg0;egg1;egg2;egg3;", "'for (x of div.childNodes)' should see elements added during iteration");
var iter1 = basket.childNodes.iterator();
var iter2 = basket.childNodes.iterator();
isnot(iter1, iter2, "nodelist.iterator() returns a new iterator each time");
log = '';
basket.appendChild(document.createTextNode("some text"));
for (var x of basket.children)
log += x.id + ";";
is(log, "egg0;egg1;egg2;egg3;", "'for (x of div.children)' should iterate over child elements");
var iter1 = basket.children.iterator();
var iter2 = basket.children.iterator();
isnot(iter1, iter2, ".iterator() returns a new iterator each time");
var count = 0;
for (var x of document.getElementsByClassName("hazardous-materials"))
count++;
is(count, 0, "'for (x of emptyNodeList)' loop should run zero times");
var log = '';
for (var x of document.querySelectorAll("span"))
log += x.id + ";";
is(log, "egg0;egg1;duckling1;egg2;egg3;", "for-of loop should work with a querySelectorAll() NodeList");
}
/* All the tests run twice. First, in this document, so without any wrappers. */
runTestsForDocument(document, "");
/* And once using the document of an iframe, so working with cross-compartment wrappers. */
SimpleTest.waitForExplicitFinish();
function iframeLoaded(iframe) {
runTestsForDocument(iframe.contentWindow.document, " (in iframe)");
SimpleTest.finish();
}
</script>
<iframe src="forOf_iframe.html" onload="iframeLoaded(this)"></iframe>
</pre>
</body>
</html>

Просмотреть файл

@ -37,6 +37,8 @@ static jsid s_prototype_id = JSID_VOID;
static jsid s_length_id = JSID_VOID; static jsid s_length_id = JSID_VOID;
static jsid s_iterator_id = JSID_VOID;
static jsid s_VOID_id = JSID_VOID; static jsid s_VOID_id = JSID_VOID;
bool bool
@ -59,6 +61,7 @@ DefineStaticJSVals(JSContext *cx)
return SET_JSID_TO_STRING(cx, prototype) && return SET_JSID_TO_STRING(cx, prototype) &&
SET_JSID_TO_STRING(cx, length) && SET_JSID_TO_STRING(cx, length) &&
SET_JSID_TO_STRING(cx, iterator) &&
DefinePropertyStaticJSVals(cx); DefinePropertyStaticJSVals(cx);
} }
@ -1163,13 +1166,6 @@ template<class LC>
bool bool
ListBase<LC>::iterate(JSContext *cx, JSObject *proxy, unsigned flags, Value *vp) ListBase<LC>::iterate(JSContext *cx, JSObject *proxy, unsigned flags, Value *vp)
{ {
if (flags == JSITER_FOR_OF) {
JSObject *iterobj = JS_NewElementIterator(cx, proxy);
if (!iterobj)
return false;
vp->setObject(*iterobj);
return true;
}
return ProxyHandler::iterate(cx, proxy, flags, vp); return ProxyHandler::iterate(cx, proxy, flags, vp);
} }

Просмотреть файл

@ -696,6 +696,7 @@ def writeStubFile(filename, config, interfaces):
if clazz.indexGetter: if clazz.indexGetter:
#methodsList.append(" { s_%s_id, &item, 1 }" % clazz.indexGetter.name) #methodsList.append(" { s_%s_id, &item, 1 }" % clazz.indexGetter.name)
f.write(string.Template(indexGetterTemplate).substitute(clazz)) f.write(string.Template(indexGetterTemplate).substitute(clazz))
methodsList.append(" { s_iterator_id, JS_ArrayIterator, 0}")
if clazz.indexSetter: if clazz.indexSetter:
f.write(string.Template(indexSetterTemplate).substitute(clazz)) f.write(string.Template(indexSetterTemplate).substitute(clazz))
if clazz.nameGetter: if clazz.nameGetter: