| 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 |
|
| 15 |
goog.provide('goog.editor.LinkTest'); |
| 16 |
goog.setTestOnly('goog.editor.LinkTest'); |
| 17 |
|
| 18 |
goog.require('goog.dom'); |
| 19 |
goog.require('goog.dom.NodeType'); |
| 20 |
goog.require('goog.dom.Range'); |
| 21 |
goog.require('goog.dom.TagName'); |
| 22 |
goog.require('goog.editor.Link'); |
| 23 |
goog.require('goog.testing.jsunit'); |
| 24 |
goog.require('goog.userAgent'); |
| 25 |
|
| 26 |
var anchor; |
| 27 |
|
| 28 |
function setUp() { |
| 29 |
anchor = goog.dom.createDom(goog.dom.TagName.A); |
| 30 |
document.body.appendChild(anchor); |
| 31 |
} |
| 32 |
|
| 33 |
function tearDown() { |
| 34 |
goog.dom.removeNode(anchor); |
| 35 |
} |
| 36 |
|
| 37 |
function testCreateNew() { |
| 38 |
var link = new goog.editor.Link(anchor, true); |
| 39 |
assertNotNull('Should have created object', link); |
| 40 |
assertTrue('Should be new', link.isNew()); |
| 41 |
assertEquals('Should have correct anchor', anchor, link.getAnchor()); |
| 42 |
assertEquals('Should be empty', '', link.getCurrentText()); |
| 43 |
} |
| 44 |
|
| 45 |
function testCreateNotNew() { |
| 46 |
var link = new goog.editor.Link(anchor, false); |
| 47 |
assertNotNull('Should have created object', link); |
| 48 |
assertFalse('Should not be new', link.isNew()); |
| 49 |
assertEquals('Should have correct anchor', anchor, link.getAnchor()); |
| 50 |
assertEquals('Should be empty', '', link.getCurrentText()); |
| 51 |
} |
| 52 |
|
| 53 |
function testCreateNewLinkFromText() { |
| 54 |
var url = 'http://www.google.com/'; |
| 55 |
anchor.innerHTML = url; |
| 56 |
var link = goog.editor.Link.createNewLinkFromText(anchor); |
| 57 |
assertNotNull('Should have created object', link); |
| 58 |
assertEquals('Should have url in anchor', url, anchor.href); |
| 59 |
} |
| 60 |
|
| 61 |
function testCreateNewLinkFromTextLeadingTrailingWhitespace() { |
| 62 |
var url = 'http://www.google.com/'; |
| 63 |
var urlWithSpaces = ' ' + url + ' '; |
| 64 |
anchor.innerHTML = urlWithSpaces; |
| 65 |
var urlWithSpacesUpdatedByBrowser = anchor.innerHTML; |
| 66 |
var link = goog.editor.Link.createNewLinkFromText(anchor); |
| 67 |
assertNotNull('Should have created object', link); |
| 68 |
assertEquals('Should have url in anchor', url, anchor.href); |
| 69 |
assertEquals( |
| 70 |
'The text should still have spaces', urlWithSpacesUpdatedByBrowser, |
| 71 |
link.getCurrentText()); |
| 72 |
} |
| 73 |
|
| 74 |
function testCreateNewLinkFromTextWithAnchor() { |
| 75 |
var url = 'https://www.google.com/'; |
| 76 |
anchor.innerHTML = url; |
| 77 |
var link = goog.editor.Link.createNewLinkFromText(anchor, '_blank'); |
| 78 |
assertNotNull('Should have created object', link); |
| 79 |
assertEquals('Should have url in anchor', url, anchor.href); |
| 80 |
assertEquals('Should have _blank target', '_blank', anchor.target); |
| 81 |
} |
| 82 |
|
| 83 |
function testInitialize() { |
| 84 |
var link = goog.editor.Link.createNewLink(anchor, 'http://www.google.com'); |
| 85 |
assertNotNull('Should have created object', link); |
| 86 |
assertTrue('Should be new', link.isNew()); |
| 87 |
assertEquals('Should have correct anchor', anchor, link.getAnchor()); |
| 88 |
assertEquals('Should be empty', '', link.getCurrentText()); |
| 89 |
} |
| 90 |
|
| 91 |
function testInitializeWithTarget() { |
| 92 |
var link = |
| 93 |
goog.editor.Link.createNewLink(anchor, 'http://www.google.com', '_blank'); |
| 94 |
assertNotNull('Should have created object', link); |
| 95 |
assertTrue('Should be new', link.isNew()); |
| 96 |
assertEquals('Should have correct anchor', anchor, link.getAnchor()); |
| 97 |
assertEquals('Should be empty', '', link.getCurrentText()); |
| 98 |
assertEquals('Should have _blank target', '_blank', anchor.target); |
| 99 |
} |
| 100 |
|
| 101 |
function testSetText() { |
| 102 |
var link = |
| 103 |
goog.editor.Link.createNewLink(anchor, 'http://www.google.com', '_blank'); |
| 104 |
assertEquals('Should be empty', '', link.getCurrentText()); |
| 105 |
link.setTextAndUrl('Text', 'http://docs.google.com/'); |
| 106 |
assertEquals( |
| 107 |
'Should point to http://docs.google.com/', 'http://docs.google.com/', |
| 108 |
anchor.href); |
| 109 |
assertEquals('Should have correct text', 'Text', link.getCurrentText()); |
| 110 |
} |
| 111 |
|
| 112 |
function testSetBoldText() { |
| 113 |
anchor.innerHTML = '<b></b>'; |
| 114 |
var link = |
| 115 |
goog.editor.Link.createNewLink(anchor, 'http://www.google.com', '_blank'); |
| 116 |
assertEquals('Should be empty', '', link.getCurrentText()); |
| 117 |
link.setTextAndUrl('Text', 'http://docs.google.com/'); |
| 118 |
assertEquals( |
| 119 |
'Should point to http://docs.google.com/', 'http://docs.google.com/', |
| 120 |
anchor.href); |
| 121 |
assertEquals('Should have correct text', 'Text', link.getCurrentText()); |
| 122 |
assertEquals('Should still be bold', |
| 123 |
String(goog.dom.TagName.B), anchor.firstChild.tagName); |
| 124 |
} |
| 125 |
|
| 126 |
function testLinkImgTag() { |
| 127 |
anchor.innerHTML = '<img src="www.google.com" alt="alt_txt">'; |
| 128 |
var link = |
| 129 |
goog.editor.Link.createNewLink(anchor, 'http://www.google.com', '_blank'); |
| 130 |
assertEquals('Test getCurrentText', 'alt_txt', link.getCurrentText()); |
| 131 |
link.setTextAndUrl('newText', 'http://docs.google.com/'); |
| 132 |
assertEquals('Test getCurrentText', 'newText', link.getCurrentText()); |
| 133 |
assertEquals( |
| 134 |
'Should point to http://docs.google.com/', 'http://docs.google.com/', |
| 135 |
anchor.href); |
| 136 |
|
| 137 |
assertEquals( |
| 138 |
'Should still have img tag', String(goog.dom.TagName.IMG), |
| 139 |
anchor.firstChild.tagName); |
| 140 |
|
| 141 |
assertEquals( |
| 142 |
'Alt should equal "newText"', 'newText', |
| 143 |
anchor.firstChild.getAttribute('alt')); |
| 144 |
} |
| 145 |
|
| 146 |
function testSetMixed() { |
| 147 |
anchor.innerHTML = '<b>A</b>B'; |
| 148 |
var link = |
| 149 |
goog.editor.Link.createNewLink(anchor, 'http://www.google.com', '_blank'); |
| 150 |
assertEquals('Should have text: AB', 'AB', link.getCurrentText()); |
| 151 |
link.setTextAndUrl('Text', 'http://docs.google.com/'); |
| 152 |
assertEquals( |
| 153 |
'Should point to http://docs.google.com/', 'http://docs.google.com/', |
| 154 |
anchor.href); |
| 155 |
assertEquals('Should have correct text', 'Text', link.getCurrentText()); |
| 156 |
assertEquals( |
| 157 |
'Should not be bold', goog.dom.NodeType.TEXT, anchor.firstChild.nodeType); |
| 158 |
} |
| 159 |
|
| 160 |
function testPlaceCursorRightOf() { |
| 161 |
// IE can only do selections properly if the region is editable. |
| 162 |
var ed = goog.dom.createDom(goog.dom.TagName.DIV); |
| 163 |
goog.dom.replaceNode(ed, anchor); |
| 164 |
ed.contentEditable = true; |
| 165 |
ed.appendChild(anchor); |
| 166 |
|
| 167 |
// In order to test the cursor placement properly, we need to have |
| 168 |
// link text. See more details in the test below. |
| 169 |
goog.dom.setTextContent(anchor, 'I am text'); |
| 170 |
|
| 171 |
var link = goog.editor.Link.createNewLink(anchor, 'http://www.google.com'); |
| 172 |
link.placeCursorRightOf(); |
| 173 |
|
| 174 |
var range = goog.dom.Range.createFromWindow(); |
| 175 |
assertTrue('Range should be collapsed', range.isCollapsed()); |
| 176 |
var startNode = range.getStartNode(); |
| 177 |
|
| 178 |
if (goog.userAgent.WEBKIT && !goog.userAgent.isVersionOrHigher('528')) { |
| 179 |
assertEquals( |
| 180 |
'Selection should be to the right of the anchor', anchor, |
| 181 |
startNode.previousSibling); |
| 182 |
} else { |
| 183 |
// Check that the selection is the "right" place. |
| 184 |
// |
| 185 |
// If you query the selection, it is actually still inside the anchor, |
| 186 |
// but if you type, it types outside the anchor. |
| 187 |
// |
| 188 |
// Best we can do is test that it is at the end of the anchor text. |
| 189 |
assertEquals( |
| 190 |
'Selection should be in anchor text', anchor.firstChild, startNode); |
| 191 |
assertEquals( |
| 192 |
'Selection should be at the end of the text', anchor.firstChild.length, |
| 193 |
range.getStartOffset()); |
| 194 |
} |
| 195 |
|
| 196 |
if (ed) { |
| 197 |
goog.dom.removeNode(ed); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
function testIsLikelyUrl() { |
| 202 |
var good = [ |
| 203 |
// Proper URLs |
| 204 |
'http://google.com', 'http://google.com/', 'http://192.168.1.103', |
| 205 |
'http://www.google.com:8083', 'https://antoine', 'https://foo.foo.net', |
| 206 |
'ftp://google.com:22/', 'http://[email protected]', |
| 207 |
'ftp://user:[email protected]', 'http://google.com/search?q=laser%20cats', |
| 208 |
'aim:goim?screenname=en2es', 'mailto:[email protected]', |
| 209 |
|
| 210 |
// Bad URLs a browser will accept |
| 211 |
'www.google.com', 'www.amazon.co.uk', 'amazon.co.uk', 'foo2.foo3.com', |
| 212 |
'pandora.tv', 'marketing.us', 'del.icio.us', 'bridge-line.com', |
| 213 |
'www.frigid.net:80', 'www.google.com?q=foo', 'www.foo.com/j%20.txt', |
| 214 |
'foodtv.net', 'google.com', 'slashdot.org', '192.168.1.1', |
| 215 |
'justin.edu?kumar something', 'google.com/search?q=hot%20pockets', |
| 216 |
|
| 217 |
// Due to TLD explosion, these could be URLs either now or soon. |
| 218 |
'ww.jester', 'juicer.fake', 'abs.nonsense.something', 'filename.txt' |
| 219 |
]; |
| 220 |
for (var i = 0; i < good.length; i++) { |
| 221 |
assertTrue( |
| 222 |
good[i] + ' should be good', goog.editor.Link.isLikelyUrl(good[i])); |
| 223 |
} |
| 224 |
|
| 225 |
var bad = [ |
| 226 |
// Definitely not URLs |
| 227 |
'bananas', 'http google com', '<img>', 'Sad :/', '*garbage!.123', 'ftp', |
| 228 |
'http', '/', 'https', 'this is', '*!&.banana!*&!', 'www.jester is gone.com', |
| 229 |
'ftp .nospaces.net', 'www_foo_net', "www.'jester'.net", 'www:8080', |
| 230 |
'www . notnsense.com', '[email protected]', |
| 231 |
|
| 232 |
// URL-ish but not quite |
| 233 |
' http://www.google.com', 'http://www.google.com:8081 ', |
| 234 |
'www.google.com foo bar', 'google.com/search?q=not quite' |
| 235 |
]; |
| 236 |
|
| 237 |
for (i = 0; i < bad.length; i++) { |
| 238 |
assertFalse( |
| 239 |
bad[i] + ' should be bad', goog.editor.Link.isLikelyUrl(bad[i])); |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
function testIsLikelyEmailAddress() { |
| 244 |
var good = [ |
| 245 |
// Valid email addresses |
| 246 |
'[email protected]', '[email protected]', '[email protected]', '[email protected]', |
| 247 |
'[email protected]', '[email protected]', '[email protected]', |
| 248 |
'[email protected]', '[email protected]', '[email protected]', |
| 249 |
'[email protected]', '[email protected]', '[email protected]' |
| 250 |
]; |
| 251 |
for (var i = 0; i < good.length; i++) { |
| 252 |
assertTrue(goog.editor.Link.isLikelyEmailAddress(good[i])); |
| 253 |
} |
| 254 |
|
| 255 |
var bad = [ |
| 256 |
// Malformed/incomplete email addresses |
| 257 |
'user', |
| 258 |
'@gmail.com', |
| 259 |
'user@gmail', |
| 260 |
'[email protected]', |
| 261 |
'[email protected]', |
| 262 |
'[email protected]', |
| 263 |
'@ya.com', |
| 264 |
'[email protected]', |
| 265 |
'jim.com', |
| 266 |
'ed:@gmail.com', |
| 267 |
'*!&.banana!*&!', |
| 268 |
':[email protected]', |
| 269 |
'[email protected]', |
| 270 |
'adam [email protected]', |
| 271 |
'john\[email protected]', |
| 272 |
"www.'jester'.net", |
| 273 |
"'james'@covald.net", |
| 274 |
'ftp://[email protected]/', |
| 275 |
'aim:goim?screenname=en2es', |
| 276 |
'user:[email protected]', |
| 277 |
'[email protected] yay' |
| 278 |
]; |
| 279 |
|
| 280 |
for (i = 0; i < bad.length; i++) { |
| 281 |
assertFalse(goog.editor.Link.isLikelyEmailAddress(bad[i])); |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
function testIsMailToLink() { |
| 286 |
assertFalse(goog.editor.Link.isMailto()); |
| 287 |
assertFalse(goog.editor.Link.isMailto(null)); |
| 288 |
assertFalse(goog.editor.Link.isMailto('')); |
| 289 |
assertFalse(goog.editor.Link.isMailto('http://foo.com')); |
| 290 |
assertFalse(goog.editor.Link.isMailto('http://mailto:80')); |
| 291 |
|
| 292 |
assertTrue(goog.editor.Link.isMailto('mailto:')); |
| 293 |
assertTrue(goog.editor.Link.isMailto('mailto://')); |
| 294 |
assertTrue(goog.editor.Link.isMailto('mailto://[email protected]')); |
| 295 |
} |
| 296 |
|
| 297 |
function testGetValidLinkFromText() { |
| 298 |
var textLinkPairs = [ |
| 299 |
// input text, expected link output |
| 300 |
'www.foo.com', 'http://www.foo.com', '[email protected]', |
| 301 |
'mailto:[email protected]', 'http://www.foo.com', 'http://www.foo.com', |
| 302 |
'https://this.that.edu', 'https://this.that.edu', 'nothing to see here', |
| 303 |
null |
| 304 |
]; |
| 305 |
var link = new goog.editor.Link(anchor, true); |
| 306 |
|
| 307 |
for (var i = 0; i < textLinkPairs.length; i += 2) { |
| 308 |
link.currentText_ = textLinkPairs[i]; |
| 309 |
var result = link.getValidLinkFromText(); |
| 310 |
assertEquals(textLinkPairs[i + 1], result); |
| 311 |
} |
| 312 |
} |