link_test.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. // Copyright 2008 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. goog.provide('goog.editor.LinkTest');
  15. goog.setTestOnly('goog.editor.LinkTest');
  16. goog.require('goog.dom');
  17. goog.require('goog.dom.NodeType');
  18. goog.require('goog.dom.Range');
  19. goog.require('goog.dom.TagName');
  20. goog.require('goog.editor.Link');
  21. goog.require('goog.testing.jsunit');
  22. goog.require('goog.userAgent');
  23. var anchor;
  24. function setUp() {
  25. anchor = goog.dom.createDom(goog.dom.TagName.A);
  26. document.body.appendChild(anchor);
  27. }
  28. function tearDown() {
  29. goog.dom.removeNode(anchor);
  30. }
  31. function testCreateNew() {
  32. var link = new goog.editor.Link(anchor, true);
  33. assertNotNull('Should have created object', link);
  34. assertTrue('Should be new', link.isNew());
  35. assertEquals('Should have correct anchor', anchor, link.getAnchor());
  36. assertEquals('Should be empty', '', link.getCurrentText());
  37. }
  38. function testCreateNotNew() {
  39. var link = new goog.editor.Link(anchor, false);
  40. assertNotNull('Should have created object', link);
  41. assertFalse('Should not be new', link.isNew());
  42. assertEquals('Should have correct anchor', anchor, link.getAnchor());
  43. assertEquals('Should be empty', '', link.getCurrentText());
  44. }
  45. function testCreateNewLinkFromText() {
  46. var url = 'http://www.google.com/';
  47. anchor.innerHTML = url;
  48. var link = goog.editor.Link.createNewLinkFromText(anchor);
  49. assertNotNull('Should have created object', link);
  50. assertEquals('Should have url in anchor', url, anchor.href);
  51. }
  52. function testCreateNewLinkFromTextLeadingTrailingWhitespace() {
  53. var url = 'http://www.google.com/';
  54. var urlWithSpaces = ' ' + url + ' ';
  55. anchor.innerHTML = urlWithSpaces;
  56. var urlWithSpacesUpdatedByBrowser = anchor.innerHTML;
  57. var link = goog.editor.Link.createNewLinkFromText(anchor);
  58. assertNotNull('Should have created object', link);
  59. assertEquals('Should have url in anchor', url, anchor.href);
  60. assertEquals(
  61. 'The text should still have spaces', urlWithSpacesUpdatedByBrowser,
  62. link.getCurrentText());
  63. }
  64. function testCreateNewLinkFromTextWithAnchor() {
  65. var url = 'https://www.google.com/';
  66. anchor.innerHTML = url;
  67. var link = goog.editor.Link.createNewLinkFromText(anchor, '_blank');
  68. assertNotNull('Should have created object', link);
  69. assertEquals('Should have url in anchor', url, anchor.href);
  70. assertEquals('Should have _blank target', '_blank', anchor.target);
  71. }
  72. function testInitialize() {
  73. var link = goog.editor.Link.createNewLink(anchor, 'http://www.google.com');
  74. assertNotNull('Should have created object', link);
  75. assertTrue('Should be new', link.isNew());
  76. assertEquals('Should have correct anchor', anchor, link.getAnchor());
  77. assertEquals('Should be empty', '', link.getCurrentText());
  78. }
  79. function testInitializeWithTarget() {
  80. var link =
  81. goog.editor.Link.createNewLink(anchor, 'http://www.google.com', '_blank');
  82. assertNotNull('Should have created object', link);
  83. assertTrue('Should be new', link.isNew());
  84. assertEquals('Should have correct anchor', anchor, link.getAnchor());
  85. assertEquals('Should be empty', '', link.getCurrentText());
  86. assertEquals('Should have _blank target', '_blank', anchor.target);
  87. }
  88. function testSetText() {
  89. var link =
  90. goog.editor.Link.createNewLink(anchor, 'http://www.google.com', '_blank');
  91. assertEquals('Should be empty', '', link.getCurrentText());
  92. link.setTextAndUrl('Text', 'http://docs.google.com/');
  93. assertEquals(
  94. 'Should point to http://docs.google.com/', 'http://docs.google.com/',
  95. anchor.href);
  96. assertEquals('Should have correct text', 'Text', link.getCurrentText());
  97. }
  98. function testSetBoldText() {
  99. anchor.innerHTML = '<b></b>';
  100. var link =
  101. goog.editor.Link.createNewLink(anchor, 'http://www.google.com', '_blank');
  102. assertEquals('Should be empty', '', link.getCurrentText());
  103. link.setTextAndUrl('Text', 'http://docs.google.com/');
  104. assertEquals(
  105. 'Should point to http://docs.google.com/', 'http://docs.google.com/',
  106. anchor.href);
  107. assertEquals('Should have correct text', 'Text', link.getCurrentText());
  108. assertEquals('Should still be bold',
  109. String(goog.dom.TagName.B), anchor.firstChild.tagName);
  110. }
  111. function testLinkImgTag() {
  112. anchor.innerHTML = '<img src="www.google.com" alt="alt_txt">';
  113. var link =
  114. goog.editor.Link.createNewLink(anchor, 'http://www.google.com', '_blank');
  115. assertEquals('Test getCurrentText', 'alt_txt', link.getCurrentText());
  116. link.setTextAndUrl('newText', 'http://docs.google.com/');
  117. assertEquals('Test getCurrentText', 'newText', link.getCurrentText());
  118. assertEquals(
  119. 'Should point to http://docs.google.com/', 'http://docs.google.com/',
  120. anchor.href);
  121. assertEquals(
  122. 'Should still have img tag', String(goog.dom.TagName.IMG),
  123. anchor.firstChild.tagName);
  124. assertEquals(
  125. 'Alt should equal "newText"', 'newText',
  126. anchor.firstChild.getAttribute('alt'));
  127. }
  128. function testSetMixed() {
  129. anchor.innerHTML = '<b>A</b>B';
  130. var link =
  131. goog.editor.Link.createNewLink(anchor, 'http://www.google.com', '_blank');
  132. assertEquals('Should have text: AB', 'AB', link.getCurrentText());
  133. link.setTextAndUrl('Text', 'http://docs.google.com/');
  134. assertEquals(
  135. 'Should point to http://docs.google.com/', 'http://docs.google.com/',
  136. anchor.href);
  137. assertEquals('Should have correct text', 'Text', link.getCurrentText());
  138. assertEquals(
  139. 'Should not be bold', goog.dom.NodeType.TEXT, anchor.firstChild.nodeType);
  140. }
  141. function testPlaceCursorRightOf() {
  142. // IE can only do selections properly if the region is editable.
  143. var ed = goog.dom.createDom(goog.dom.TagName.DIV);
  144. goog.dom.replaceNode(ed, anchor);
  145. ed.contentEditable = true;
  146. ed.appendChild(anchor);
  147. // In order to test the cursor placement properly, we need to have
  148. // link text. See more details in the test below.
  149. goog.dom.setTextContent(anchor, 'I am text');
  150. var link = goog.editor.Link.createNewLink(anchor, 'http://www.google.com');
  151. link.placeCursorRightOf();
  152. var range = goog.dom.Range.createFromWindow();
  153. assertTrue('Range should be collapsed', range.isCollapsed());
  154. var startNode = range.getStartNode();
  155. if (goog.userAgent.WEBKIT && !goog.userAgent.isVersionOrHigher('528')) {
  156. assertEquals(
  157. 'Selection should be to the right of the anchor', anchor,
  158. startNode.previousSibling);
  159. } else {
  160. // Check that the selection is the "right" place.
  161. //
  162. // If you query the selection, it is actually still inside the anchor,
  163. // but if you type, it types outside the anchor.
  164. //
  165. // Best we can do is test that it is at the end of the anchor text.
  166. assertEquals(
  167. 'Selection should be in anchor text', anchor.firstChild, startNode);
  168. assertEquals(
  169. 'Selection should be at the end of the text', anchor.firstChild.length,
  170. range.getStartOffset());
  171. }
  172. if (ed) {
  173. goog.dom.removeNode(ed);
  174. }
  175. }
  176. function testIsLikelyUrl() {
  177. var good = [
  178. // Proper URLs
  179. 'http://google.com', 'http://google.com/', 'http://192.168.1.103',
  180. 'http://www.google.com:8083', 'https://antoine', 'https://foo.foo.net',
  181. 'ftp://google.com:22/', 'http://user@site.com',
  182. 'ftp://user:pass@ftp.site.com', 'http://google.com/search?q=laser%20cats',
  183. 'aim:goim?screenname=en2es', 'mailto:x@y.com',
  184. // Bad URLs a browser will accept
  185. 'www.google.com', 'www.amazon.co.uk', 'amazon.co.uk', 'foo2.foo3.com',
  186. 'pandora.tv', 'marketing.us', 'del.icio.us', 'bridge-line.com',
  187. 'www.frigid.net:80', 'www.google.com?q=foo', 'www.foo.com/j%20.txt',
  188. 'foodtv.net', 'google.com', 'slashdot.org', '192.168.1.1',
  189. 'justin.edu?kumar&nbsp;something', 'google.com/search?q=hot%20pockets',
  190. // Due to TLD explosion, these could be URLs either now or soon.
  191. 'ww.jester', 'juicer.fake', 'abs.nonsense.something', 'filename.txt'
  192. ];
  193. for (var i = 0; i < good.length; i++) {
  194. assertTrue(
  195. good[i] + ' should be good', goog.editor.Link.isLikelyUrl(good[i]));
  196. }
  197. var bad = [
  198. // Definitely not URLs
  199. 'bananas', 'http google com', '<img>', 'Sad :/', '*garbage!.123', 'ftp',
  200. 'http', '/', 'https', 'this is', '*!&.banana!*&!', 'www.jester is gone.com',
  201. 'ftp .nospaces.net', 'www_foo_net', "www.'jester'.net", 'www:8080',
  202. 'www . notnsense.com', 'email@address.com',
  203. // URL-ish but not quite
  204. ' http://www.google.com', 'http://www.google.com:8081 ',
  205. 'www.google.com foo bar', 'google.com/search?q=not quite'
  206. ];
  207. for (i = 0; i < bad.length; i++) {
  208. assertFalse(
  209. bad[i] + ' should be bad', goog.editor.Link.isLikelyUrl(bad[i]));
  210. }
  211. }
  212. function testIsLikelyEmailAddress() {
  213. var good = [
  214. // Valid email addresses
  215. 'foo@foo.com', 'foo1@foo2.foo3.com', 'f45_1@goog13.org', 'user@gmail.co.uk',
  216. 'jon-smith@crazy.net', 'roland1@capuchino.gov', 'ernir@gshi.nl',
  217. 'JOON@jno.COM', 'media@meDIa.fREnology.FR', 'john.mail4me@del.icio.us',
  218. 'www9@wc3.madeup1.org', 'hi@192.168.1.103', 'hi@192.168.1.1'
  219. ];
  220. for (var i = 0; i < good.length; i++) {
  221. assertTrue(goog.editor.Link.isLikelyEmailAddress(good[i]));
  222. }
  223. var bad = [
  224. // Malformed/incomplete email addresses
  225. 'user',
  226. '@gmail.com',
  227. 'user@gmail',
  228. 'user@.com',
  229. 'user@gmail.c',
  230. 'user@gmail.co.u',
  231. '@ya.com',
  232. '.@hi3.nl',
  233. 'jim.com',
  234. 'ed:@gmail.com',
  235. '*!&.banana!*&!',
  236. ':jon@gmail.com',
  237. '3g?@bil.com',
  238. 'adam be@hi.net',
  239. 'john\nsmith@test.com',
  240. "www.'jester'.net",
  241. "'james'@covald.net",
  242. 'ftp://user@site.com/',
  243. 'aim:goim?screenname=en2es',
  244. 'user:pass@site.com',
  245. 'user@site.com yay'
  246. ];
  247. for (i = 0; i < bad.length; i++) {
  248. assertFalse(goog.editor.Link.isLikelyEmailAddress(bad[i]));
  249. }
  250. }
  251. function testIsMailToLink() {
  252. assertFalse(goog.editor.Link.isMailto());
  253. assertFalse(goog.editor.Link.isMailto(null));
  254. assertFalse(goog.editor.Link.isMailto(''));
  255. assertFalse(goog.editor.Link.isMailto('http://foo.com'));
  256. assertFalse(goog.editor.Link.isMailto('http://mailto:80'));
  257. assertTrue(goog.editor.Link.isMailto('mailto:'));
  258. assertTrue(goog.editor.Link.isMailto('mailto://'));
  259. assertTrue(goog.editor.Link.isMailto('mailto://ptucker@gmail.com'));
  260. }
  261. function testGetValidLinkFromText() {
  262. var textLinkPairs = [
  263. // input text, expected link output
  264. 'www.foo.com', 'http://www.foo.com', 'user@gmail.com',
  265. 'mailto:user@gmail.com', 'http://www.foo.com', 'http://www.foo.com',
  266. 'https://this.that.edu', 'https://this.that.edu', 'nothing to see here',
  267. null
  268. ];
  269. var link = new goog.editor.Link(anchor, true);
  270. for (var i = 0; i < textLinkPairs.length; i += 2) {
  271. link.currentText_ = textLinkPairs[i];
  272. var result = link.getValidLinkFromText();
  273. assertEquals(textLinkPairs[i + 1], result);
  274. }
  275. }