summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/css/mediaqueries/resources/matchmedia-utils.js
blob: 327e8f69fe27bb0d732db62d62cdc3be505ec022 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
'use strict';

function query_is_css_parseable(query) {
  const style = document.createElement('style');
  style.type = 'text/css';
  document.head.appendChild(style);

  const sheet = style.sheet;
  try {
    sheet.insertRule("@media " + query + "{}", 0);
    return sheet.cssRules.length == 1 &&
        sheet.cssRules[0].media.mediaText != "not all";
  } finally {
    while (sheet.cssRules.length)
      sheet.deleteRule(0);
    style.remove();
  }
}

function query_should_be_css_parseable(query) {
  test(() => {
    assert_true(query_is_css_parseable(query));
  }, "Should be parseable in a CSS stylesheet: '" + query + "'");
}

function query_should_not_be_css_parseable(query) {
  test(() => {
    assert_false(query_is_css_parseable(query));
  }, "Should not be parseable in a CSS stylesheet: '" + query + "'");
}

function query_is_js_parseable(query) {
  // We cannot rely on whether a given feature is on or off, so only check the
  // 'media' member of the result.
  const match = window.matchMedia(query);
  return match.media == query;
}

function query_should_be_js_parseable(query) {
  test(() => {
    assert_true(query_is_js_parseable(query));
  }, "Should be parseable in JS: '" + query + "'");
}

function query_should_not_be_js_parseable(query) {
  test(() => {
    assert_false(query_is_js_parseable(query));
  }, "Should not be parseable in JS: '" + query + "'");
}

function query_is_known(query) {
  return window.matchMedia(`${query}, not all and ${query}`).matches;
}

function query_is_unknown(query) {
  return !window.matchMedia(`${query}, not all and ${query}`).matches;
}

function query_should_be_known(query) {
  test(() => {
    assert_true(query_is_js_parseable(query), "Can parse with JS");
    assert_true(query_is_css_parseable(query), "Can parse with CSS");
    assert_true(query_is_known(query));
  }, "Should be known: '" + query + "'");
}

function query_should_be_unknown(query) {
  test(() => {
    assert_true(query_is_js_parseable(query), "Can parse with JS");
    assert_true(query_is_css_parseable(query), "Can parse with CSS");
  }, "Should be parseable: '" + query + "'");

  test(() => {
    assert_true(query_is_unknown(query));
  }, "Should be unknown: '" + query + "'");
}