summaryrefslogtreecommitdiffstats
path: root/third_party/WebKit/LayoutTests/inspector-protocol/css/css-get-media-queries.html
blob: a2ccc3b48f92e3efc4f05c62d372203bebf9ba2c (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<html>
<head>
<script type="text/javascript" src="../../http/tests/inspector-protocol/inspector-protocol-test.js"></script>
<script type="text/javascript">
function test()
{
    InspectorTest.sendCommandOrDie("DOM.enable", {});
    InspectorTest.sendCommandOrDie("CSS.enable", {}, onCSSEnabled);

    function onCSSEnabled()
    {
        InspectorTest.sendCommandOrDie("CSS.getMediaQueries", {}, onMediaQueries);
    }

    function onMediaQueries(response)
    {
        function compareValue(value1, value2)
        {
            return value1 < value2 ? -1 : value1 > value2 ? 1 : 0;
        }

        function mediaComparator(media1, media2)
        {
            if (media1.text != media2.text)
                return compareValue(media1.text, media2.text);
            var hasSource1 = !!(media1.parentStyleSheetId && media1.range);
            var hasSource2 = !!(media2.parentStyleSheetId && media2.range);
            if (hasSource1 != hasSource2)
                return compareValue(hasSource1, hasSource2);
            if (media1.range.startLine != media2.range.startLine)
                return compareValue(media1.range.startLine, media2.range.startLine);
            if (media1.range.startColumn != media2.range.startColumn)
                return compareValue(media1.range.startColumn, media2.range.startColumn);
            if (media1.range.endLine != media2.range.endLine)
                return compareValue(media1.range.endLine, media2.range.endLine);
            if (media1.range.endColumn != media2.range.endColumn)
                return compareValue(media1.range.endColumn, media2.range.endColumn);
            return 0;
        }
        response.medias.sort(mediaComparator);

        var styleSheetIds = [];
        for (var i = 0; i < response.medias.length; ++i) {
            var styleSheetId = response.medias[i].parentStyleSheetId;
            if (styleSheetId && styleSheetIds.indexOf(styleSheetId) === -1)
                styleSheetIds.push(styleSheetId);
        }
        if (!styleSheetIds.length) {
            dumpMediaQueries(response.medias, {});
            return;
        }

        var styleSheetTextMap = {};
        for (var i = 0; i < styleSheetIds.length; ++i) {
            var callback = i === styleSheetIds.length - 1 ? dumpMediaQueries.bind(null, response.medias, styleSheetTextMap) : null;
            InspectorTest.sendCommandOrDie("CSS.getStyleSheetText", {styleSheetId: styleSheetIds[i]}, onStyleSheetText.bind(null, styleSheetIds[i], styleSheetTextMap, callback));
        }
    }

    function onStyleSheetText(styleSheetId, styleSheetTextMap, callback, response)
    {
        styleSheetTextMap[styleSheetId] = response.text;
        if (callback)
            callback();
    }

    function dumpMediaQueries(medias, styleSheetTextMap)
    {
        for (var i = 0; i < medias.length; ++i) {
            var mediaRule = medias[i];
            InspectorTest.log("mediaRule #" + i);
            InspectorTest.log("    text: " + mediaRule.text);
            InspectorTest.log("    source: " + mediaRule.source);
            InspectorTest.log("    range: " + JSON.stringify(mediaRule.range));
            if (mediaRule.parentStyleSheetId && mediaRule.range)
                InspectorTest.log("    computedText: " + getComputedText(styleSheetTextMap[mediaRule.parentStyleSheetId], mediaRule.range));
            if (!mediaRule.mediaList) {
                InspectorTest.log("    mediaList is empty");
                continue;
            }
            for (var j = 0; j < mediaRule.mediaList.length; ++j) {
                var mediaQuery = mediaRule.mediaList[j];
                var suffix = mediaRule.sourceURL.indexOf("active-media-queries.css") !== -1 ? " active: " + mediaQuery.active : "";
                InspectorTest.log("    mediaQuery #" + j + suffix);
                for (var k = 0; k < mediaQuery.expressions.length; ++k) {
                    var expression = mediaQuery.expressions[k];
                    InspectorTest.log("        mediaExpression #" + k);
                    InspectorTest.log("            feature: " + expression.feature);
                    InspectorTest.log("            value: " + expression.value);
                    InspectorTest.log("            unit: " + expression.unit);
                    if (expression.valueRange) {
                        InspectorTest.log("            valueRange: " + JSON.stringify(expression.valueRange));
                        if (mediaRule.parentStyleSheetId)
                            InspectorTest.log("            computedText: " + getComputedText(styleSheetTextMap[mediaRule.parentStyleSheetId], expression.valueRange));
                    }
                    if (expression.computedLength)
                        InspectorTest.log("            computed length: " + expression.computedLength);
                }
            }
        }
        InspectorTest.completeTest();
    }

    function getComputedText(text, range)
    {
        var lines = text.split("\n");
        var result = [];
        for (var line = range.startLine; line <= range.endLine; ++line) {
            var start = line === range.startLine ? range.startColumn : 0;
            var end = line === range.endLine ? range.endColumn : lines[line].length;
            result.push(lines[line].substring(start, end));
        }
        return result.join("\\n");
    }
};
</script>
<link rel="stylesheet" media="print and (min-width: 8.5in)" href="./resources/media-queries.css"></link>
<link rel="stylesheet" href="./resources/active-media-queries.css"></link>
<style>
@media screen and (device-aspect-ratio: 16/9), screen and (device-aspect-ratio: 16/10) {
    * {
        padding: 1em;
    }

    @media (max-width: 200px) and (min-width: 100px) {
        * {
            margin: 1in;
        }
    }
}
</style>
</head>
<body onload="runTest()">
</body>
</html>