summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/css/css-syntax/whitespace.html
blob: bc7aa7ebda518a42a32a154c3ed13f00be86429a (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
<!doctype html>
<title>CSS Whitespace</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>

<meta name="author" title="Tab Atkins-Bittner">
<link rel=help href="https://drafts.csswg.org/css-syntax/#whitespace">

<div class=a><b></b></div>
<div id=foo></div>

<!--
CSS's definition of "whitespace" matches HTML,
and includes only the five ASCII characters
U+0009, U+000A, U+000C, U+000D, and U+0020.
The rest of Unicode's whitespace characters,
many of which are recognized as whitespace by JS,
are not valid whitespace in CSS.
-->

<script>

function isWhitespace(codepoint) {
    const char = String.fromCodePoint(codepoint);
    const codepointName = "U+" + codepoint.toString(16).padStart(4, "0");
    test(()=>{
        const withSpace = document.querySelector(".a b");
        const withChar = document.querySelector(`.a${char}b`);
        assert_equals(withSpace, withChar);
    }, `${codepointName} is CSS whitespace`);
}
function isNotWhitespace(codepoint) {
    const char = String.fromCodePoint(codepoint);
    const codepointName = "U+" + codepoint.toString(16).padStart(4, "0");
    test(()=>{
        const withSpace = document.querySelector(".a b");
        document.querySelector("#foo").setAttribute("class", `.a${char}b`);
        try {
            var withChar = document.querySelector(`.a${char}b`);
        } catch(e) {
            assert_true(true, `${codepointName} isn't valid in a selector at all`);
            return;
        }
        assert_not_equals(withSpace, withChar);
    }, `${codepointName} is *not* CSS whitespace`);
}

// CSS Whitespace characters
var whitespace = [0x9, 0xa, 0xc, 0xd, 0x20];

// Unicode Whitespace characters not recognized by CSS
// https://en.wikipedia.org/wiki/Whitespace_character#Unicode
var notWhitespace = [0xb, 0x85, 0xa0, 0x1680, 0x2000, 0x2001, 0x2002, 0x2003, 0x2004, 0x2005, 0x2006, 0x2007, 0x2008, 0x2009, 0x200a, 0x2928, 0x2029, 0x202f, 0x205f, 0x3000, 0x180e, 0x200b, 0x200c, 0x200d, 0x2060, 0xfeff];

for(var codepoint of whitespace) {
    isWhitespace(codepoint);
}
for(var codepoint of notWhitespace) {
    isNotWhitespace(codepoint);
}

</script>