1 description('Tests to ensure MakeTextWritingDirection* modifies the direction of text and embed texts properly.');
3 if (!window.testRunner)
4 testFailed('This test requires testRunner');
6 internals.settings.setEditingBehavior('win');
8 var testContainer = document.createElement("div");
9 testContainer.contentEditable = true;
10 document.body.appendChild(testContainer);
12 function recursivelyRemoveExtraenousSpan(node) {
13 for (var i = 0; i < node.childNodes.length; i++)
14 recursivelyRemoveExtraenousSpan(node.childNodes[i]);
15 if (node.localName == 'span' && node.attributes.length == 0) {
16 var childNodes = node.childNodes;
17 while (node.firstChild)
18 node.parentNode.insertBefore(node.firstChild, node);
19 node.parentNode.removeChild(node);
23 function modifyWritingDirection(content, selector, command, expected)
25 if (!window.testRunner)
28 testContainer.innerHTML = content;
29 var selected = selector(testContainer);
30 window.testRunner.execCommand('MakeTextWritingDirection' + command);
32 // Remove Apple-style-span because it does not need to be tested here.
33 var actual = testContainer.innerHTML.replace(/ class="Apple-style-span"/g, '');
34 var action = command + ' on ' + selected + ' of "' + content + '"';
35 if (actual == expected)
38 error = ' yielded ' + actual + ' but expected ' + expected;
39 recursivelyRemoveExtraenousSpan(testContainer);
40 if (testContainer.innerHTML.replace(/ class="Apple-style-span"/g, '') == expected)
41 testFailed('(due to the bug 44359) ' + action + error);
43 testFailed(action + error);
47 function selectFirstWord(container) {
48 document.getSelection().setBaseAndExtent(container, 0, container, 0);
49 document.getSelection().modify('extend', 'forward', 'word');
53 function selectSecondAndThirdWords(container) {
54 document.getSelection().setBaseAndExtent(container, 0, container, 0);
55 document.getSelection().modify('move', 'forward', 'word');
56 document.getSelection().modify('extend', 'forward', 'word');
57 document.getSelection().modify('extend', 'forward', 'word');
58 return 'second and third words';
61 function selectThirdWord(container) {
62 document.getSelection().setBaseAndExtent(container, 0, container, 0);
63 document.getSelection().modify('move', 'forward', 'word');
64 document.getSelection().modify('move', 'forward', 'word');
65 document.getSelection().modify('extend', 'forward', 'word');
69 // left to right language
70 modifyWritingDirection('hello world', selectFirstWord, 'Natural', 'hello world');
71 modifyWritingDirection('hello world', selectFirstWord, 'LeftToRight', '<span style="unicode-bidi: embed;">hello </span>world');
72 modifyWritingDirection('hello world', selectFirstWord, 'RightToLeft', '<span style="unicode-bidi: embed; direction: rtl;">hello </span>world');
73 modifyWritingDirection('<b>hello world</b> webkit', selectSecondAndThirdWords, 'Natural', '<b>hello world</b> webkit');
74 modifyWritingDirection('<b>hello world</b> webkit', selectSecondAndThirdWords, 'LeftToRight', '<b>hello <span style="unicode-bidi: embed;">world</span></b><span style="unicode-bidi: embed;"> webkit</span>');
75 modifyWritingDirection('<b>hello world</b> webkit', selectSecondAndThirdWords, 'RightToLeft', '<b>hello <span style="unicode-bidi: embed; direction: rtl;">world</span></b><span style="unicode-bidi: embed; direction: rtl;"> webkit</span>');
76 modifyWritingDirection('<span dir="rtl">hello <span dir="ltr">world webkit rocks</span></span>', selectThirdWord, 'Natural',
77 '<span dir="rtl">hello <span dir="ltr">world </span></span>webkit <span dir="rtl"><span dir="ltr">rocks</span></span>');
78 modifyWritingDirection('<span dir="rtl">hello <span dir="ltr">world webkit rocks</span></span>', selectThirdWord, 'LeftToRight',
79 '<span dir="rtl">hello <span dir="ltr">world </span></span><span style="unicode-bidi: embed;">webkit </span><span dir="rtl"><span dir="ltr">rocks</span></span>');
80 modifyWritingDirection('<span dir="rtl">hello <span dir="ltr">world webkit rocks</span></span>', selectThirdWord, 'RightToLeft',
81 '<span dir="rtl">hello <span dir="ltr">world </span>webkit <span dir="ltr">rocks</span></span>');
83 // right to left language
84 modifyWritingDirection('هنا يكتب النص العربي', selectFirstWord, 'Natural', 'هنا يكتب النص العربي');
85 modifyWritingDirection('هنا يكتب النص العربي', selectFirstWord, 'LeftToRight', '<span style="unicode-bidi: embed;">هنا </span>يكتب النص العربي');
86 modifyWritingDirection('هنا يكتب النص العربي', selectFirstWord, 'RightToLeft', '<span style="unicode-bidi: embed; direction: rtl;">هنا </span>يكتب النص العربي');
88 modifyWritingDirection('<b>هنا يكتب</b> النص العربي', selectSecondAndThirdWords, 'Natural', '<b>هنا يكتب</b> النص العربي');
89 modifyWritingDirection('<b>هنا يكتب</b> النص العربي', selectSecondAndThirdWords, 'LeftToRight', '<b>هنا <span style="unicode-bidi: embed;">يكتب</span></b><span style="unicode-bidi: embed;"> النص </span>العربي');
90 modifyWritingDirection('<b>هنا يكتب</b> النص العربي', selectSecondAndThirdWords, 'RightToLeft', '<b>هنا <span style="unicode-bidi: embed; direction: rtl;">يكتب</span></b><span style="unicode-bidi: embed; direction: rtl;"> النص </span>العربي');
92 modifyWritingDirection('<div dir="rtl">هنا يكتب النص العربي</div>', selectFirstWord, 'Natural', '<div dir="rtl">هنا يكتب النص العربي</div>');
93 modifyWritingDirection('<div dir="rtl">هنا يكتب النص العربي</div>', selectFirstWord, 'LeftToRight', '<div dir="rtl"><span style="unicode-bidi: embed; direction: ltr;">هنا </span>يكتب النص العربي</div>');
94 modifyWritingDirection('<div dir="rtl">هنا يكتب النص العربي</div>', selectFirstWord, 'RightToLeft', '<div dir="rtl"><span style="unicode-bidi: embed;">هنا </span>يكتب النص العربي</div>');
96 modifyWritingDirection('<div dir="rtl"><b>هنا يكتب</b> النص العربي</div>', selectSecondAndThirdWords, 'Natural', '<div dir="rtl"><b>هنا يكتب</b> النص العربي</div>');
97 modifyWritingDirection('<div dir="rtl"><b>هنا يكتب</b> النص العربي</div>', selectSecondAndThirdWords, 'LeftToRight', '<div dir="rtl"><b>هنا <span style="unicode-bidi: embed; direction: ltr;">يكتب</span></b><span style="unicode-bidi: embed; direction: ltr;"> النص </span>العربي</div>');
98 modifyWritingDirection('<div dir="rtl"><b>هنا يكتب</b> النص العربي</div>', selectSecondAndThirdWords, 'RightToLeft', '<div dir="rtl"><b>هنا <span style="unicode-bidi: embed;">يكتب</span></b><span style="unicode-bidi: embed;"> النص </span>العربي</div>');
99 modifyWritingDirection('<div dir="rtl">هنا <span dir="ltr">يكتب النص العربي</span></div>', selectThirdWord, 'Natural',
100 '<div dir="rtl">هنا <span dir="ltr">يكتب </span>النص <span dir="ltr">العربي</span></div>');
101 modifyWritingDirection('<div dir="rtl">هنا <span dir="ltr">يكتب النص العربي</span></div>', selectThirdWord, 'LeftToRight',
102 '<div dir="rtl"><span style="unicode-bidi: embed; direction: ltr;">هنا يكتب النص العربي</span></div>');
103 modifyWritingDirection('<div dir="rtl">هنا <span dir="ltr">يكتب النص العربي</span></div>', selectThirdWord, 'RightToLeft',
104 '<div dir="rtl">هنا <span dir="ltr">يكتب </span><span style="unicode-bidi: embed;">النص </span><span dir="ltr">العربي</span></div>');
106 // bidirectional langauge
107 modifyWritingDirection('写中文', selectFirstWord, 'Natural', '写中文');
108 modifyWritingDirection('写中文', selectFirstWord, 'LeftToRight', '<span style="unicode-bidi: embed;">写</span>中文');
109 modifyWritingDirection('写中文', selectFirstWord, 'RightToLeft', '<span style="unicode-bidi: embed; direction: rtl;">写</span>中文');
111 modifyWritingDirection('<div dir="rtl">写中文</div>', selectFirstWord, 'Natural', '<div dir="rtl">写中文</div>');
112 modifyWritingDirection('<div dir="rtl">写中文</div>', selectFirstWord, 'LeftToRight', '<div dir="rtl"><span style="unicode-bidi: embed; direction: ltr;">写</span>中文</div>');
113 modifyWritingDirection('<div dir="rtl">写中文</div>', selectFirstWord, 'RightToLeft', '<div dir="rtl"><span style="unicode-bidi: embed;">写</span>中文</div>');
115 document.body.removeChild(testContainer);
117 var successfullyParsed = true;