4 <script src="../../resources/js-test.js"></script>
7 <p id="description"></p>
8 <div id="console"></div>
10 description('Test the extraction of the text surrounding an element.');
12 function findOffsetCoordinates(node, offset) {
13 var nodeRange = document.createRange();
14 nodeRange.selectNode(node);
16 var offsetRange = document.createRange();
17 offsetRange.setStart(node, offset);
18 offsetRange.setEnd(node, offset + 1);
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;
25 return { x: x, y: y };
28 function surroundingText(html, offset, maxLength) {
29 document.getElementById('test').innerHTML = html;
31 var here = document.getElementById('here');
33 throw 'Test case needs an element with id "here"';
35 var node = here.hasChildNodes() ? here.firstChild : here.nextSibling;
37 throw 'No node after "here" element';
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, '');
45 if (!window.internals)
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)', '.');
61 document.body.removeChild(document.getElementById('test'));
66 window.jsTestIsAsync = true;
67 window.successfullyParsed = true;