3 body { font-family: monospace; }
4 #margin:before { content:
"MARGIN"; color: red; margin-left: -
60px; }
5 #margin { margin-left:
60px; }
6 #noMargin:before { content:
"BEFORE"; color: red; }
8 <script src=
"../../../../resources/js-test.js"></script>
10 // Since, CSSOM View introduces document.caretPositionFromPoint to replace
11 // document.caretRangeFromPoint. We make a polyfill to make this script works
13 if (!document
.caretPositionFromPoint
) {
14 document
.caretPositionFromPoint = function(x
, y
) {
15 var range
= document
.caretRangeFromPoint(x
, y
);
18 return {offset
: range
.startOffset
, offsetNode
: range
.startContainer
};
22 function testIt(id
, charIndex
) {
23 var sample
= document
.getElementById(id
).firstChild
;
24 var charWidth
= sample
.offsetWidth
/ sample
.textContent
.length
;
25 var x
= sample
.offsetLeft
+ charWidth
* charIndex
+ 1;
26 var y
= sample
.offsetTop
+ sample
.offsetHeight
/ 2;
27 var position
= document
.caretPositionFromPoint(x
, y
);
30 if (position
.offsetNode
.nodeType
!= Node
.TEXT_NODE
)
32 return position
.offsetNode
.textContent
[position
.offset
];
36 description('caretRangeFromPoint() should work with :before style');
38 shouldBeEqualToString('testIt("margin", -2)', 'a');
39 shouldBeEqualToString('testIt("margin", -1)', 'a');
40 shouldBeEqualToString('testIt("margin", 0)', 'a');
41 shouldBeEqualToString('testIt("margin", 1)', 'b');
43 shouldBeEqualToString('testIt("noMargin", -2)', 'a');
44 shouldBeEqualToString('testIt("noMargin", -1)', 'a');
45 shouldBeEqualToString('testIt("noMargin", 0)', 'a');
46 shouldBeEqualToString('testIt("noMargin", 1)', 'b');
48 if (window
.testRunner
)
49 document
.getElementById('container').textContent
= '';
53 <p id=
"description"></p>
54 <p id=
"margin"><span>abcde
</span></p>
55 <p id=
"noMargin"><span>abcde
</span></p>