Upstream version 9.37.195.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / editing / surrounding-text / surrounding-text.html
1 <!DOCTYPE html>
2 <html>
3 <body>
4 <script src="../../resources/js-test.js"></script>
5 <div id="test">
6 </div>
7 <p id="description"></p>
8 <div id="console"></div>
9 <script>
10 description('Test the extraction of the text surrounding an element.');
11
12 function findOffsetCoordinates(node, offset) {
13     var nodeRange = document.createRange();
14     nodeRange.selectNode(node);
15
16     var offsetRange = document.createRange();
17     offsetRange.setStart(node, offset);
18     offsetRange.setEnd(node, offset + 1);
19
20     var nodeRect = nodeRange.getBoundingClientRect();
21     var offsetRect = offsetRange.getBoundingClientRect();
22     var x = (offsetRect.left + offsetRect.right) / 2 - nodeRect.left;
23     var y = (offsetRect.top + offsetRect.bottom) / 2 - nodeRect.top;
24
25     return { x: x, y: y };
26 }
27
28 function surroundingText(html, offset, maxLength) {
29     document.getElementById('test').innerHTML = html;
30
31     var here = document.getElementById('here');
32     if (here == null)
33         throw 'Test case needs an element with id "here"';
34
35     var node = here.hasChildNodes() ? here.firstChild : here.nextSibling;
36     if (node == null)
37         throw 'No node after "here" element';
38
39     var coords = findOffsetCoordinates(node, offset);
40     var text = window.internals.textSurroundingNode(node, coords.x, coords.y, maxLength);
41     return text.replace(/\s+|\ufffc/g, ' ').replace(/^\s*|\s*$/g, '');
42 }
43
44 function run() {
45     if (!window.internals)
46         return;
47
48     shouldBeEqualToString('surroundingText(\'<button>.</button>12345<p id="here">6789 12345</p>6789<button>.</button>\', 0, 100)', '12345 6789 12345 6789');
49     shouldBeEqualToString('surroundingText(\'<button>.</button>12345<p id="here">6789 12345</p>6789<button>.</button>\', 5, 6)', '89 123');
50     shouldBeEqualToString('surroundingText(\'<button>.</button>12345<p id="here">6789 12345</p>6789<button>.</button>\', 5, 0)', '');
51     shouldBeEqualToString('surroundingText(\'<button>.</button>12345<p id="here">6789 12345</p>6789<button>.</button>\', 5, 1)', '1');
52     shouldBeEqualToString('surroundingText(\'<button>.</button>12345<p id="here">6789 12345</p>6789<button>.</button>\', 6, 2)', '12');
53     shouldBeEqualToString('surroundingText(\'<select>.</select><div>57th Street and Lake Shore Drive</div> <span>Chicago</span> <span id="here">IL</span> <span>60637</span><select>.</select>\', 0, 100)', '57th Street and Lake Shore Drive Chicago IL 60637');
54     shouldBeEqualToString('surroundingText(\'<fieldset>.</fieldset>12345<button>abc</button><p>6789<br id="here"/>12345</p>6789<textarea>abc</textarea>0123<fieldset>.</fieldset>\', 0, 100)', '6789 12345 6789');
55     shouldBeEqualToString('surroundingText(\'<button>.</button><div id="here">This is <!-- comment --!>a test <\' + \'script language="javascript"><\' + \'/script>example<button>.</button>\', 0, 100)', 'This is a test example');
56     shouldBeEqualToString('surroundingText(\'<button>.</button><div id="here">012345678901234567890123456789</div><button>.</button>\', 15, 12)', '901234567890');
57     shouldBeEqualToString('surroundingText(\'<option>.</option>12345<button id="here">test</button><option>.</option>\', 0, 100)', '');
58     shouldBeEqualToString('surroundingText(\'<option>.</option>12345<button>te<span id="here">st</span></button><option>.</option>\', 0, 100)', '');
59     shouldBeEqualToString('surroundingText(\'<p id="here">.</p>\', 0, 2)', '.');
60
61     document.body.removeChild(document.getElementById('test'));
62     finishJSTest();
63 }
64
65 window.onload = run;
66 window.jsTestIsAsync = true;
67 window.successfullyParsed = true;
68 </script>
69 </body>
70 </html>