function tick() {
// MediaQueryList events are guaranteed to run before requestAnimationFrame
// per spec.
return new Promise(r => requestAnimationFrame(r));
}
async function run() { variframe = document.getElementById("subdoc"); var subdoc = iframe.contentDocument; var subwin = iframe.contentWindow; var subroot = subdoc.documentElement;
var content_div = document.getElementById("content");
content_div.style.font = "initial"; var em_size =
getComputedStyle(content_div).fontSize.match(/^(\d+)px$/)[1];
var w = Math.floor(em_size * 9.3); var h = Math.floor(em_size * 4.2); iframe.style.width = w + "px"; iframe.style.height = h + "px";
subroot.offsetWidth; // flush layout
await tick();
function setup_mql(str) { var obj = {
str: str,
mql: subwin.matchMedia(str),
notifyCount: 0,
listener: function(event) {
ok(event instanceof subwin.MediaQueryListEvent, "correct argument to listener: " + obj.str);
is(event.media, obj.mql.media, "correct media in the event: " + obj.str);
is(event.target, obj.mql, "correct target in the event: " + obj.str);
++obj.notifyCount;
// Test the last match result only on odd
// notifications.
if (obj.notifyCount & 1) {
obj.lastOddMatchResult = event.target.matches;
}
}
}
obj.mql.addListener(obj.listener);
return obj;
}
function finish_mql(obj) {
obj.mql.removeListener(obj.listener);
}
var w_exact_w = setup_mql("(width: " + w + "px)"); var w_min_9em = setup_mql("(min-width : 9em)"); var w_min_10em = setup_mql("( min-width: 10em ) "); var w_max_9em = setup_mql("(max-width: 9em)"); var w_max_10em = setup_mql("(max-width: 10em)");
check_match(w_exact_w, false, "after width increase to around 10.3em");
check_notify(w_exact_w, 1, "after width increase to around 10.3em");
check_match(w_min_9em, true, "after width increase to around 10.3em");
check_notify(w_min_9em, 0, "after width increase to around 10.3em");
check_match(w_min_10em, true, "after width increase to around 10.3em");
check_notify(w_min_10em, 1, "after width increase to around 10.3em");
check_match(w_max_9em, false, "after width increase to around 10.3em");
check_notify(w_max_9em, 0, "after width increase to around 10.3em");
check_match(w_max_10em, false, "after width increase to around 10.3em");
check_notify(w_max_10em, 1, "after width increase to around 10.3em");
var w3 = w * 2; iframe.style.width = w3 + "px";
subroot.offsetWidth; // flush layout
await tick();
check_match(w_exact_w, false, "after width double from original");
check_notify(w_exact_w, 1, "after width double from original");
check_match(w_min_9em, true, "after width double from original");
check_notify(w_min_9em, 0, "after width double from original");
check_match(w_min_10em, true, "after width double from original");
check_notify(w_min_10em, 1, "after width double from original");
check_match(w_max_9em, false, "after width double from original");
check_notify(w_max_9em, 0, "after width double from original");
check_match(w_max_10em, false, "after width double from original");
check_notify(w_max_10em, 1, "after width double from original");
// Additional tests of listener mutation.
{
let received = [];
let received_mql = [];
function listener1(event) {
received.push(1);
received_mql.push(event.target);
}
function listener2(event) {
received.push(2);
received_mql.push(event.target);
}
// mql (1, 2), mql2 (1)
is(JSON.stringify(received), "[1,2,1]", "notification of lists in order created");
is(received_mql[0], mql, "notification of lists in order created");
is(received_mql[1], mql, "notification of lists in order created");
is(received_mql[2], mql2, "notification of lists in order created");
received = [];
received_mql = [];
function removing_listener(event) {
received.push(3);
received_mql.push(event.target);
event.target.removeListener(listener2);
mql2.removeListener(listener1);
}
mql.addListener(removing_listener);
mql.removeListener(listener2);
mql.addListener(listener2); // after removing_listener (3)
// mql(1, 3)
is(JSON.stringify(received), "[1,3]", "listeners still notified after removed if change was before");
is(received_mql[0], mql, "notification order (removal tests)");
is(received_mql[1], mql, "notification order (removal tests)");
received = [];
received_mql = [];
// mql(1, 3)
is(JSON.stringify(received), "[1,3]", "listeners not notified for changes after their removal");
is(received_mql[0], mql, "notification order (removal tests)");
is(received_mql[1], mql, "notification order (removal tests)");
}
/* Bug 753777: test that things work in a freshly-created iframe */
{
let newIframe = document.createElement("iframe");
document.body.appendChild(newIframe);
is(newIframe.contentWindow.matchMedia("all").matches, true, "matchMedia should work in newly-created iframe");
is(newIframe.contentWindow.matchMedia("(min-width: 1px)").matches, true, "(min-width: 1px) should match in newly-created iframe");
is(newIframe.contentWindow.matchMedia("(max-width: 1px)").matches, false, "(max-width: 1px) should not match in newly-created iframe");
document.body.removeChild(newIframe);
}
/* Bug 716751: listeners lost due to GC */ var gc_received = [];
{
let received = [];
let listener1 = function(event) {
gc_received.push(1);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.