|
| 1 | +var utils = require('../lib/utils'); |
| 2 | + |
| 3 | +describe('utils', function () { |
| 4 | + |
| 5 | + describe('get', function () { |
| 6 | + |
| 7 | + it('returns value at "#" key by default', function () { |
| 8 | + assert.strictEqual(utils.get({ '#': 'foo' }), 'foo'); |
| 9 | + }); |
| 10 | + |
| 11 | + it('returns value at specified subkey', function () { |
| 12 | + assert.strictEqual(utils.get({ bar: 'baz' }, 'bar'), 'baz'); |
| 13 | + }); |
| 14 | + |
| 15 | + it('returns null by default when key is missing', function () { |
| 16 | + assert.strictEqual(utils.get({ bar: 'baz' }), null); |
| 17 | + }); |
| 18 | + |
| 19 | + it('returns custom defaultValue when key is missing', function () { |
| 20 | + assert.strictEqual(utils.get({}, 'missing', 'fallback'), 'fallback'); |
| 21 | + }); |
| 22 | + |
| 23 | + it('uses "#" subkey on first element when obj is an array', function () { |
| 24 | + assert.strictEqual(utils.get([{ '#': 'first' }, { '#': 'second' }]), 'first'); |
| 25 | + }); |
| 26 | + |
| 27 | + it('uses specified subkey on first element when obj is an array', function () { |
| 28 | + assert.strictEqual(utils.get([{ foo: 'bar' }], 'foo'), 'bar'); |
| 29 | + }); |
| 30 | + |
| 31 | + it('returns null when array element is missing the key', function () { |
| 32 | + assert.strictEqual(utils.get([{}]), null); |
| 33 | + }); |
| 34 | + |
| 35 | + }); |
| 36 | + |
| 37 | + describe('safeTrim', function () { |
| 38 | + |
| 39 | + it('trims whitespace from strings', function () { |
| 40 | + assert.strictEqual(utils.safeTrim(' hello '), 'hello'); |
| 41 | + }); |
| 42 | + |
| 43 | + it('trims only leading whitespace', function () { |
| 44 | + assert.strictEqual(utils.safeTrim(' hello'), 'hello'); |
| 45 | + }); |
| 46 | + |
| 47 | + it('trims only trailing whitespace', function () { |
| 48 | + assert.strictEqual(utils.safeTrim('hello '), 'hello'); |
| 49 | + }); |
| 50 | + |
| 51 | + it('returns an empty string unchanged', function () { |
| 52 | + assert.strictEqual(utils.safeTrim(''), ''); |
| 53 | + }); |
| 54 | + |
| 55 | + it('passes through numbers without throwing', function () { |
| 56 | + assert.strictEqual(utils.safeTrim(42), 42); |
| 57 | + }); |
| 58 | + |
| 59 | + it('passes through null without throwing', function () { |
| 60 | + assert.strictEqual(utils.safeTrim(null), null); |
| 61 | + }); |
| 62 | + |
| 63 | + it('passes through undefined without throwing', function () { |
| 64 | + assert.strictEqual(utils.safeTrim(undefined), undefined); |
| 65 | + }); |
| 66 | + |
| 67 | + it('passes through objects without throwing', function () { |
| 68 | + var obj = { a: 1 }; |
| 69 | + assert.strictEqual(utils.safeTrim(obj), obj); |
| 70 | + }); |
| 71 | + |
| 72 | + }); |
| 73 | + |
| 74 | + describe('resolve', function () { |
| 75 | + |
| 76 | + it('resolves a relative path using "../" against a base URL', function () { |
| 77 | + assert.strictEqual( |
| 78 | + utils.resolve('http://example.com/foo/bar', '../baz'), |
| 79 | + 'http://example.com/baz' |
| 80 | + ); |
| 81 | + }); |
| 82 | + |
| 83 | + it('resolves a relative path using "./" against a base URL', function () { |
| 84 | + assert.strictEqual( |
| 85 | + utils.resolve('http://example.com/foo/bar', './baz'), |
| 86 | + 'http://example.com/foo/baz' |
| 87 | + ); |
| 88 | + }); |
| 89 | + |
| 90 | + it('resolves a relative path without path prefix against a base URL', function () { |
| 91 | + assert.strictEqual( |
| 92 | + utils.resolve('http://example.com/foo/bar', 'baz'), |
| 93 | + 'http://example.com/foo/baz' |
| 94 | + ); |
| 95 | + }); |
| 96 | + |
| 97 | + it('resolves an absolute path URL against a base URL', function () { |
| 98 | + assert.strictEqual( |
| 99 | + utils.resolve('http://example.com/foo/', '/images/pic.png'), |
| 100 | + 'http://example.com/images/pic.png' |
| 101 | + ); |
| 102 | + }); |
| 103 | + |
| 104 | + it('returns an absolute URL unchanged', function () { |
| 105 | + assert.strictEqual( |
| 106 | + utils.resolve('http://example.com/', 'http://other.com/img.png'), |
| 107 | + 'http://other.com/img.png' |
| 108 | + ); |
| 109 | + }); |
| 110 | + |
| 111 | + it('returns pathUrl when baseUrl is falsy', function () { |
| 112 | + assert.strictEqual(utils.resolve(null, '/path'), '/path'); |
| 113 | + assert.strictEqual(utils.resolve('', '/path'), '/path'); |
| 114 | + assert.strictEqual(utils.resolve(undefined, '/path'), '/path'); |
| 115 | + }); |
| 116 | + |
| 117 | + it('returns pathUrl when pathUrl is falsy', function () { |
| 118 | + assert.strictEqual(utils.resolve('http://example.com/', null), null); |
| 119 | + assert.strictEqual(utils.resolve('http://example.com/', ''), ''); |
| 120 | + assert.strictEqual(utils.resolve('http://example.com/', undefined), undefined); |
| 121 | + }); |
| 122 | + |
| 123 | + it('returns pathUrl for tag: URIs that the URL constructor rejects', function () { |
| 124 | + var tagUri = 'tag:example.com,2003:posts/1'; |
| 125 | + assert.strictEqual(utils.resolve('http://example.com/', tagUri), tagUri); |
| 126 | + }); |
| 127 | + |
| 128 | + it('returns pathUrl for other non-http schemes', function () { |
| 129 | + var urn = 'urn:isbn:0451450523'; |
| 130 | + assert.strictEqual(utils.resolve('http://example.com/', urn), urn); |
| 131 | + }); |
| 132 | + |
| 133 | + }); |
| 134 | + |
| 135 | + describe('isAbsoluteUrl', function () { |
| 136 | + |
| 137 | + it('returns true for http URLs', function () { |
| 138 | + assert.strictEqual(utils.isAbsoluteUrl('http://example.com/'), true); |
| 139 | + }); |
| 140 | + |
| 141 | + it('returns true for https URLs', function () { |
| 142 | + assert.strictEqual(utils.isAbsoluteUrl('https://example.com/path'), true); |
| 143 | + }); |
| 144 | + |
| 145 | + it('returns false for relative URLs', function () { |
| 146 | + assert.strictEqual(utils.isAbsoluteUrl('/relative/path'), false); |
| 147 | + }); |
| 148 | + |
| 149 | + it('returns false for relative paths without leading slash', function () { |
| 150 | + assert.strictEqual(utils.isAbsoluteUrl('relative/path'), false); |
| 151 | + }); |
| 152 | + |
| 153 | + it('returns false for tag: URIs (no host)', function () { |
| 154 | + assert.strictEqual(utils.isAbsoluteUrl('tag:example.com,2003:posts/1'), false); |
| 155 | + }); |
| 156 | + |
| 157 | + it('returns false for empty string', function () { |
| 158 | + assert.strictEqual(utils.isAbsoluteUrl(''), false); |
| 159 | + }); |
| 160 | + |
| 161 | + it('returns false for null', function () { |
| 162 | + assert.strictEqual(utils.isAbsoluteUrl(null), false); |
| 163 | + }); |
| 164 | + |
| 165 | + it('returns false for undefined', function () { |
| 166 | + assert.strictEqual(utils.isAbsoluteUrl(undefined), false); |
| 167 | + }); |
| 168 | + |
| 169 | + it('returns false for non-string values', function () { |
| 170 | + assert.strictEqual(utils.isAbsoluteUrl(42), false); |
| 171 | + assert.strictEqual(utils.isAbsoluteUrl({}), false); |
| 172 | + }); |
| 173 | + |
| 174 | + }); |
| 175 | + |
| 176 | + describe('nslookup', function () { |
| 177 | + |
| 178 | + it('returns true when URI matches the given default namespace', function () { |
| 179 | + assert.strictEqual(utils.nslookup('http://www.w3.org/2005/Atom', 'atom'), true); |
| 180 | + }); |
| 181 | + |
| 182 | + it('returns true for atom v0.3 URI', function () { |
| 183 | + assert.strictEqual(utils.nslookup('http://purl.org/atom/ns#', 'atom'), true); |
| 184 | + }); |
| 185 | + |
| 186 | + it('returns false when URI does not match the given default', function () { |
| 187 | + assert.strictEqual(utils.nslookup('http://www.w3.org/2005/Atom', 'rdf'), false); |
| 188 | + }); |
| 189 | + |
| 190 | + it('returns false for unknown URI', function () { |
| 191 | + assert.strictEqual(utils.nslookup('http://unknown.example.com/', 'atom'), false); |
| 192 | + }); |
| 193 | + |
| 194 | + it('returns false for undefined URI', function () { |
| 195 | + assert.strictEqual(utils.nslookup(undefined, 'atom'), false); |
| 196 | + }); |
| 197 | + |
| 198 | + }); |
| 199 | + |
| 200 | + describe('nsprefix', function () { |
| 201 | + |
| 202 | + it('returns the namespace prefix for a known URI', function () { |
| 203 | + assert.strictEqual(utils.nsprefix('http://www.w3.org/2005/Atom'), 'atom'); |
| 204 | + }); |
| 205 | + |
| 206 | + it('returns "dc" for Dublin Core URI', function () { |
| 207 | + assert.strictEqual(utils.nsprefix('http://purl.org/dc/elements/1.1/'), 'dc'); |
| 208 | + }); |
| 209 | + |
| 210 | + it('returns "media" for Yahoo media RSS URI', function () { |
| 211 | + assert.strictEqual(utils.nsprefix('http://search.yahoo.com/mrss/'), 'media'); |
| 212 | + }); |
| 213 | + |
| 214 | + it('returns undefined for unknown URI', function () { |
| 215 | + assert.strictEqual(utils.nsprefix('http://unknown.example.com/'), undefined); |
| 216 | + }); |
| 217 | + |
| 218 | + }); |
| 219 | + |
| 220 | + describe('reresolve', function () { |
| 221 | + |
| 222 | + it('returns false when node is falsy', function () { |
| 223 | + assert.strictEqual(utils.reresolve(null, 'http://example.com/'), false); |
| 224 | + assert.strictEqual(utils.reresolve(undefined, 'http://example.com/'), false); |
| 225 | + }); |
| 226 | + |
| 227 | + it('returns false when baseurl is falsy', function () { |
| 228 | + assert.strictEqual(utils.reresolve({ link: { '#': '/foo' } }, null), false); |
| 229 | + assert.strictEqual(utils.reresolve({ link: { '#': '/foo' } }, ''), false); |
| 230 | + }); |
| 231 | + |
| 232 | + it('resolves "#" on link elements', function () { |
| 233 | + var node = { link: { '#': '/foo' } }; |
| 234 | + utils.reresolve(node, 'http://example.com/'); |
| 235 | + assert.strictEqual(node.link['#'], 'http://example.com/foo'); |
| 236 | + }); |
| 237 | + |
| 238 | + it('resolves "#" on logo elements', function () { |
| 239 | + var node = { logo: { '#': '/logo.png' } }; |
| 240 | + utils.reresolve(node, 'http://example.com/'); |
| 241 | + assert.strictEqual(node.logo['#'], 'http://example.com/logo.png'); |
| 242 | + }); |
| 243 | + |
| 244 | + it('resolves "#" on icon elements', function () { |
| 245 | + var node = { icon: { '#': '/favicon.ico' } }; |
| 246 | + utils.reresolve(node, 'http://example.com/'); |
| 247 | + assert.strictEqual(node.icon['#'], 'http://example.com/favicon.ico'); |
| 248 | + }); |
| 249 | + |
| 250 | + it('resolves image.url["#"]', function () { |
| 251 | + var node = { image: { url: { '#': '/img.png' } } }; |
| 252 | + utils.reresolve(node, 'http://example.com/'); |
| 253 | + assert.strictEqual(node.image.url['#'], 'http://example.com/img.png'); |
| 254 | + }); |
| 255 | + |
| 256 | + it('resolves image.link["#"]', function () { |
| 257 | + var node = { image: { link: { '#': '/target' } } }; |
| 258 | + utils.reresolve(node, 'http://example.com/'); |
| 259 | + assert.strictEqual(node.image.link['#'], 'http://example.com/target'); |
| 260 | + }); |
| 261 | + |
| 262 | + it('resolves href attributes', function () { |
| 263 | + var node = { entry: { '@': { href: '/page' } } }; |
| 264 | + utils.reresolve(node, 'http://example.com/'); |
| 265 | + assert.strictEqual(node.entry['@'].href, 'http://example.com/page'); |
| 266 | + }); |
| 267 | + |
| 268 | + it('resolves src attributes', function () { |
| 269 | + var node = { img: { '@': { src: '/image.png' } } }; |
| 270 | + utils.reresolve(node, 'http://example.com/'); |
| 271 | + assert.strictEqual(node.img['@'].src, 'http://example.com/image.png'); |
| 272 | + }); |
| 273 | + |
| 274 | + it('resolves uri attributes', function () { |
| 275 | + var node = { el: { '@': { uri: '/resource' } } }; |
| 276 | + utils.reresolve(node, 'http://example.com/'); |
| 277 | + assert.strictEqual(node.el['@'].uri, 'http://example.com/resource'); |
| 278 | + }); |
| 279 | + |
| 280 | + it('handles array of element items', function () { |
| 281 | + var node = { |
| 282 | + link: [ |
| 283 | + { '#': '/first' }, |
| 284 | + { '#': '/second' } |
| 285 | + ] |
| 286 | + }; |
| 287 | + utils.reresolve(node, 'http://example.com/'); |
| 288 | + assert.strictEqual(node.link[0]['#'], 'http://example.com/first'); |
| 289 | + assert.strictEqual(node.link[1]['#'], 'http://example.com/second'); |
| 290 | + }); |
| 291 | + |
| 292 | + it('does not modify non-url attributes', function () { |
| 293 | + var node = { el: { '@': { type: 'text/html' } } }; |
| 294 | + utils.reresolve(node, 'http://example.com/'); |
| 295 | + assert.strictEqual(node.el['@'].type, 'text/html'); |
| 296 | + }); |
| 297 | + |
| 298 | + it('does not attempt to resolve non-string href values', function () { |
| 299 | + var node = { el: { '@': { href: 42 } } }; |
| 300 | + assert.doesNotThrow(function () { |
| 301 | + utils.reresolve(node, 'http://example.com/'); |
| 302 | + }); |
| 303 | + assert.strictEqual(node.el['@'].href, 42); |
| 304 | + }); |
| 305 | + |
| 306 | + }); |
| 307 | + |
| 308 | + describe('stripHtml', function () { |
| 309 | + |
| 310 | + it('removes simple HTML tags', function () { |
| 311 | + assert.strictEqual(utils.stripHtml('<b>bold</b>'), 'bold'); |
| 312 | + }); |
| 313 | + |
| 314 | + it('removes multiple tags', function () { |
| 315 | + assert.strictEqual(utils.stripHtml('<p>Hello <strong>world</strong></p>'), 'Hello world'); |
| 316 | + }); |
| 317 | + |
| 318 | + it('removes self-closing tags', function () { |
| 319 | + assert.strictEqual(utils.stripHtml('before<br/>after'), 'beforeafter'); |
| 320 | + }); |
| 321 | + |
| 322 | + it('removes tags with attributes', function () { |
| 323 | + assert.strictEqual(utils.stripHtml('<a href="http://example.com">link</a>'), 'link'); |
| 324 | + }); |
| 325 | + |
| 326 | + it('returns the string unchanged when there are no tags', function () { |
| 327 | + assert.strictEqual(utils.stripHtml('plain text'), 'plain text'); |
| 328 | + }); |
| 329 | + |
| 330 | + it('returns an empty string for an empty string', function () { |
| 331 | + assert.strictEqual(utils.stripHtml(''), ''); |
| 332 | + }); |
| 333 | + |
| 334 | + }); |
| 335 | + |
| 336 | +}); |
0 commit comments