summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/css/cssom-view/elementFromPosition.html
blob: e7a7b4afee3bae8ee89340e1a11887362f890cdf (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
<!DOCTYPE HTML>
<html lang="en-US">
<head>
    <title>CSS Test: CSSOM View elementFromPoint</title>
    <meta charset="UTF-8">
    <link rel="author" title="Chris" href="mailto:pwx.frontend@gmail.com" />
    <link rel="help" href="https://www.w3.org/TR/cssom-view/#dom-document-elementfrompoint" />
    <meta name="flags" content="dom" />
    <script src="/resources/testharness.js" type="text/javascript"></script>
    <script src="/resources/testharnessreport.js" type="text/javascript"></script>
</head>
<body>
    <noscript>Test not run - JavaScript required!</noscript>
    <div id="log"></div>
    <script type="text/javascript">

        var body = document.getElementsByTagName( 'body' )[0];
        function createElement( id ) {
            var elem = document.createElement( 'div' );
            if ( id && typeof id == 'string' ) {
                elem.id = id;
            }
            body.appendChild( elem );
            return elem;
        }

        function setPosition( config ) {
            var target = config.target;
            target.style.position = 'absolute';
            target.style.left = config.left + 'px';
            target.style.top = config.top + 'px';
            target.style.width = config.width + 'px';
            target.style.height = config.height + 'px';
            if ( config['z-index'] ) {
                target.style.zIndex = config['z-index'];
            }
        }

        var target = createElement( 'dom-1' );
        setPosition( {
            width: 100,
            height: 100,
            left: 10,
            top: 10,
            target: target
        });

        test( function () {
            assert_inherits( document, 'elementFromPoint' );
        }, 'document.elementFromPoint');

        test( function () {
            assert_true( document.elementFromPoint instanceof Function );
        }, 'document.elementFromPoint is a Function');
        (function(){
            var wrap = [
                // 左上角.
                {x: 10, y: 10, r: 'top left corner'},
                // 上边线
                {x: 50, y: 10, r: 'top line'},
                // 右上角
                {x: 110, y: 10, r: 'top right corner'},
                // 左边线
                {x: 10, y: 50, r: 'left line'},
                // 元素范围内
                {x: 50, y: 50, r: 'inside'},
                // 右边线
                {x: 110, y: 50, r: 'right line'},
                // 左下角
                {x: 10, y: 110, r: 'bottom left corner'},
                // 下边线
                {x: 50, y: 110, r: 'bottom line'},
                // 右下角
                {x: 110, y: 110, r: 'bottom right corner'}
            ];
            var i = 0, len = wrap.length, item;
            for ( ; i < len; i++ ) {
                item = wrap[ i ];
                test( function () {
                    assert_equals( document.elementFromPoint( item.x, item.y).id == 'dom-1', true );
                }, 'test some point of the element: ' + item.r);
            }
        })();
        test( function () {
            var elem = document.elementFromPoint( 0, 0 );
            assert_equals( elem.nodeName, 'HTML' );
        }, 'Point (0, 0), return root element(HTML)' );

        test( function () {
            var elem = document.elementFromPoint( -1000, 0 );
            assert_equals( elem, null, 'negative x, return null' );
        }, ' test negative x ');

        test( function () {
            var elem = document.elementFromPoint( 0, -1000 );
            assert_equals( elem, null, 'negative y, return null' );
        }, ' test negative y ');

        test( function () {
            var elem = document.elementFromPoint( 100000, 0 );
            assert_equals( elem, null );
        }, 'test outside of viewport');

        test( function () {
            var config = {
                width: 100,
                height: 100,
                left: 5,
                top: 5
            };
            var target2 = createElement( 'dom-2' );
            config.target = target2;
            setPosition( config );

            var elem = document.elementFromPoint( 10, 10 );
            var elem2 = document.elementFromPoint( 10, 10 );
            assert_equals( elem.id, elem2.id );
        }, 'test the top of layer');
    </script>
</body>
</html>