Simon Hunt | 1eecfa2 | 2014-12-16 14:46:29 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Simon Hunt | 1eecfa2 | 2014-12-16 14:46:29 -0800 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* |
Simon Hunt | dc6362a | 2014-12-18 19:55:23 -0800 | [diff] [blame] | 18 | ONOS GUI -- Util -- General Purpose Functions |
Simon Hunt | 1eecfa2 | 2014-12-16 14:46:29 -0800 | [diff] [blame] | 19 | */ |
Simon Hunt | dc6362a | 2014-12-18 19:55:23 -0800 | [diff] [blame] | 20 | (function () { |
Simon Hunt | 1eecfa2 | 2014-12-16 14:46:29 -0800 | [diff] [blame] | 21 | 'use strict'; |
| 22 | |
Simon Hunt | 4deb0e8 | 2015-06-10 16:18:25 -0700 | [diff] [blame] | 23 | // injected services |
| 24 | var $window, $log; |
| 25 | |
| 26 | // internal state |
| 27 | var debugFlags = {}; |
| 28 | |
Simon Hunt | b1d35e8 | 2016-01-26 13:54:06 -0800 | [diff] [blame] | 29 | // function references |
| 30 | var fcc = String.fromCharCode, |
| 31 | cca = String.prototype.charCodeAt; |
Simon Hunt | 4deb0e8 | 2015-06-10 16:18:25 -0700 | [diff] [blame] | 32 | |
| 33 | function _parseDebugFlags(dbgstr) { |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 34 | var bits = dbgstr ? dbgstr.split(',') : []; |
Simon Hunt | 4deb0e8 | 2015-06-10 16:18:25 -0700 | [diff] [blame] | 35 | bits.forEach(function (key) { |
| 36 | debugFlags[key] = true; |
| 37 | }); |
| 38 | $log.debug('Debug flags:', dbgstr); |
| 39 | } |
Simon Hunt | a11b4eb | 2015-01-28 16:20:50 -0800 | [diff] [blame] | 40 | |
Simon Hunt | 59df0b2 | 2014-12-17 10:32:25 -0800 | [diff] [blame] | 41 | function isF(f) { |
Simon Hunt | 404f6b2 | 2015-01-21 14:00:56 -0800 | [diff] [blame] | 42 | return typeof f === 'function' ? f : null; |
Simon Hunt | 59df0b2 | 2014-12-17 10:32:25 -0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | function isA(a) { |
Simon Hunt | 404f6b2 | 2015-01-21 14:00:56 -0800 | [diff] [blame] | 46 | // NOTE: Array.isArray() is part of EMCAScript 5.1 |
| 47 | return Array.isArray(a) ? a : null; |
Simon Hunt | 59df0b2 | 2014-12-17 10:32:25 -0800 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | function isS(s) { |
| 51 | return typeof s === 'string' ? s : null; |
| 52 | } |
| 53 | |
| 54 | function isO(o) { |
Simon Hunt | 404f6b2 | 2015-01-21 14:00:56 -0800 | [diff] [blame] | 55 | return (o && typeof o === 'object' && o.constructor === Object) ? o : null; |
Simon Hunt | 59df0b2 | 2014-12-17 10:32:25 -0800 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | function contains(a, x) { |
| 59 | return isA(a) && a.indexOf(x) > -1; |
| 60 | } |
| 61 | |
Simon Hunt | 51fc40b | 2015-01-06 13:56:12 -0800 | [diff] [blame] | 62 | // Returns true if all names in the array are defined as functions |
| 63 | // on the given api object; false otherwise. |
Simon Hunt | 48e6167 | 2015-01-30 14:48:25 -0800 | [diff] [blame] | 64 | // Also returns false if there are properties on the api that are NOT |
| 65 | // listed in the array of names. |
Simon Hunt | 51fc40b | 2015-01-06 13:56:12 -0800 | [diff] [blame] | 66 | function areFunctions(api, fnNames) { |
Simon Hunt | 48e6167 | 2015-01-30 14:48:25 -0800 | [diff] [blame] | 67 | var fnLookup = {}, |
| 68 | extraFound = false; |
| 69 | |
| 70 | if (!isA(fnNames)) { |
| 71 | return false; |
| 72 | } |
| 73 | var n = fnNames.length, |
| 74 | i, name; |
| 75 | for (i=0; i<n; i++) { |
| 76 | name = fnNames[i]; |
| 77 | if (!isF(api[name])) { |
| 78 | return false; |
| 79 | } |
| 80 | fnLookup[name] = true; |
| 81 | } |
| 82 | |
| 83 | // check for properties on the API that are not listed in the array, |
| 84 | angular.forEach(api, function (value, key) { |
| 85 | if (!fnLookup[key]) { |
| 86 | extraFound = true; |
| 87 | } |
| 88 | }); |
| 89 | return !extraFound; |
| 90 | } |
| 91 | |
| 92 | // Returns true if all names in the array are defined as functions |
| 93 | // on the given api object; false otherwise. This is a non-strict version |
| 94 | // that does not care about other properties on the api. |
| 95 | function areFunctionsNonStrict(api, fnNames) { |
Simon Hunt | 51fc40b | 2015-01-06 13:56:12 -0800 | [diff] [blame] | 96 | if (!isA(fnNames)) { |
| 97 | return false; |
| 98 | } |
| 99 | var n = fnNames.length, |
| 100 | i, name; |
| 101 | for (i=0; i<n; i++) { |
| 102 | name = fnNames[i]; |
| 103 | if (!isF(api[name])) { |
| 104 | return false; |
| 105 | } |
| 106 | } |
| 107 | return true; |
| 108 | } |
| 109 | |
Simon Hunt | a11b4eb | 2015-01-28 16:20:50 -0800 | [diff] [blame] | 110 | // Returns width and height of window inner dimensions. |
| 111 | // offH, offW : offset width/height are subtracted, if present |
| 112 | function windowSize(offH, offW) { |
| 113 | var oh = offH || 0, |
| 114 | ow = offW || 0; |
| 115 | return { |
| 116 | height: $window.innerHeight - oh, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 117 | width: $window.innerWidth - ow, |
Simon Hunt | a11b4eb | 2015-01-28 16:20:50 -0800 | [diff] [blame] | 118 | }; |
| 119 | } |
| 120 | |
Simon Hunt | 31bb01f | 2015-03-31 16:50:41 -0700 | [diff] [blame] | 121 | // Returns true if current browser determined to be a mobile device |
| 122 | function isMobile() { |
| 123 | var ua = $window.navigator.userAgent, |
| 124 | patt = /iPhone|iPod|iPad|Silk|Android|BlackBerry|Opera Mini|IEMobile/; |
| 125 | return patt.test(ua); |
| 126 | } |
| 127 | |
Bri Prebilic Cole | 55ee09b | 2015-08-04 14:34:07 -0700 | [diff] [blame] | 128 | // Returns true if the current browser determined to be Chrome |
| 129 | function isChrome() { |
| 130 | var isChromium = $window.chrome, |
| 131 | vendorName = $window.navigator.vendor, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 132 | isOpera = $window.navigator.userAgent.indexOf('OPR') > -1; |
Bri Prebilic Cole | 55ee09b | 2015-08-04 14:34:07 -0700 | [diff] [blame] | 133 | return (isChromium !== null && |
| 134 | isChromium !== undefined && |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 135 | vendorName === 'Google Inc.' && |
Bri Prebilic Cole | 55ee09b | 2015-08-04 14:34:07 -0700 | [diff] [blame] | 136 | isOpera == false); |
| 137 | } |
| 138 | |
| 139 | // Returns true if the current browser determined to be Safari |
| 140 | function isSafari() { |
| 141 | return ($window.navigator.userAgent.indexOf('Safari') !== -1 && |
| 142 | $window.navigator.userAgent.indexOf('Chrome') === -1); |
| 143 | } |
| 144 | |
| 145 | // Returns true if the current browser determined to be Firefox |
| 146 | function isFirefox() { |
| 147 | return typeof InstallTrigger !== 'undefined'; |
| 148 | } |
| 149 | |
Simon Hunt | 48e6167 | 2015-01-30 14:48:25 -0800 | [diff] [blame] | 150 | // search through an array of objects, looking for the one with the |
| 151 | // tagged property matching the given key. tag defaults to 'id'. |
| 152 | // returns the index of the matching object, or -1 for no match. |
| 153 | function find(key, array, tag) { |
| 154 | var _tag = tag || 'id', |
| 155 | idx, n, d; |
| 156 | for (idx = 0, n = array.length; idx < n; idx++) { |
| 157 | d = array[idx]; |
| 158 | if (d[_tag] === key) { |
| 159 | return idx; |
| 160 | } |
| 161 | } |
| 162 | return -1; |
| 163 | } |
| 164 | |
Simon Hunt | 205099e | 2015-02-07 13:12:01 -0800 | [diff] [blame] | 165 | // search through array to find (the first occurrence of) item, |
| 166 | // returning its index if found; otherwise returning -1. |
| 167 | function inArray(item, array) { |
| 168 | var i; |
| 169 | if (isA(array)) { |
| 170 | for (i=0; i<array.length; i++) { |
| 171 | if (array[i] === item) { |
| 172 | return i; |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | return -1; |
| 177 | } |
| 178 | |
| 179 | // remove (the first occurrence of) the specified item from the given |
| 180 | // array, if any. Return true if the removal was made; false otherwise. |
| 181 | function removeFromArray(item, array) { |
| 182 | var found = false, |
| 183 | i = inArray(item, array); |
| 184 | if (i >= 0) { |
| 185 | array.splice(i, 1); |
| 186 | found = true; |
| 187 | } |
| 188 | return found; |
| 189 | } |
| 190 | |
Bri Prebilic Cole | 08d08d4 | 2015-04-01 14:37:02 -0700 | [diff] [blame] | 191 | // return true if the object is empty, return false otherwise |
Bri Prebilic Cole | 19a32dd | 2015-03-26 18:00:03 -0700 | [diff] [blame] | 192 | function isEmptyObject(obj) { |
| 193 | var key; |
| 194 | for (key in obj) { |
| 195 | return false; |
| 196 | } |
| 197 | return true; |
| 198 | } |
| 199 | |
Bri Prebilic Cole | 0bc4de2 | 2015-07-20 17:07:55 -0700 | [diff] [blame] | 200 | // returns true if the two objects have all the same properties |
| 201 | function sameObjProps(obj1, obj2) { |
| 202 | var key; |
| 203 | for (key in obj1) { |
| 204 | if (obj1.hasOwnProperty(key)) { |
| 205 | if (!(obj1[key] === obj2[key])) { |
| 206 | return false; |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | return true; |
| 211 | } |
| 212 | |
| 213 | // returns true if the array contains the object |
| 214 | // does NOT use strict object reference equality, |
| 215 | // instead checks each property individually for equality |
| 216 | function containsObj(arr, obj) { |
Bri Prebilic Cole | 70aacc4 | 2015-07-22 11:28:34 -0700 | [diff] [blame] | 217 | var i, |
| 218 | len = arr.length; |
| 219 | for (i = 0; i < len; i++) { |
Bri Prebilic Cole | 0bc4de2 | 2015-07-20 17:07:55 -0700 | [diff] [blame] | 220 | if (sameObjProps(arr[i], obj)) { |
| 221 | return true; |
| 222 | } |
| 223 | } |
| 224 | return false; |
| 225 | } |
| 226 | |
Simon Hunt | 27a5cc8 | 2015-02-19 14:49:55 -0800 | [diff] [blame] | 227 | // return the given string with the first character capitalized. |
| 228 | function cap(s) { |
Matteo Scandolo | 209c6c6 | 2016-05-21 10:08:57 -0700 | [diff] [blame] | 229 | return s ? s[0].toUpperCase() + s.slice(1).toLowerCase() : s; |
Simon Hunt | 27a5cc8 | 2015-02-19 14:49:55 -0800 | [diff] [blame] | 230 | } |
| 231 | |
Simon Hunt | b1d35e8 | 2016-01-26 13:54:06 -0800 | [diff] [blame] | 232 | // return encoding structure for given parameters |
| 233 | function eecode(h, w) { |
| 234 | var m = 65, |
| 235 | x = 90, |
| 236 | d = x - m + 1, |
| 237 | s = x + m, |
| 238 | o = [], |
| 239 | n, i, c, e; |
| 240 | |
| 241 | for (i = 0, n = w.length; i<n; i++) { |
| 242 | c = cca.call(w, i); |
| 243 | e = s - c + h; |
| 244 | e = e > x ? e - d : e; |
| 245 | o.push(e); |
| 246 | } |
| 247 | return { |
| 248 | o: w, |
| 249 | d: o.join(''), |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 250 | e: fcc.apply(o, o), |
Simon Hunt | b1d35e8 | 2016-01-26 13:54:06 -0800 | [diff] [blame] | 251 | }; |
| 252 | } |
| 253 | |
Bri Prebilic Cole | 08d08d4 | 2015-04-01 14:37:02 -0700 | [diff] [blame] | 254 | // return the parameter without a px suffix |
| 255 | function noPx(num) { |
| 256 | return Number(num.replace(/px$/, '')); |
| 257 | } |
| 258 | |
| 259 | // return an element's given style property without px suffix |
| 260 | function noPxStyle(elem, prop) { |
| 261 | return Number(elem.style(prop).replace(/px$/, '')); |
| 262 | } |
| 263 | |
Simon Hunt | 9664137 | 2015-06-02 15:53:25 -0700 | [diff] [blame] | 264 | function endsWith(str, suffix) { |
| 265 | return str.indexOf(suffix, str.length - suffix.length) !== -1; |
| 266 | } |
| 267 | |
Simon Hunt | 4deb0e8 | 2015-06-10 16:18:25 -0700 | [diff] [blame] | 268 | // return true if the given debug flag was specified in the query params |
| 269 | function debugOn(tag) { |
| 270 | return debugFlags[tag]; |
| 271 | } |
Simon Hunt | 9664137 | 2015-06-02 15:53:25 -0700 | [diff] [blame] | 272 | |
Simon Hunt | f90c18b | 2016-01-25 15:38:58 -0800 | [diff] [blame] | 273 | // output debug message to console, if debug tag set... |
| 274 | // e.g. fs.debug('mytag', arg1, arg2, ...) |
| 275 | function debug(tag) { |
| 276 | var args; |
| 277 | if (debugOn(tag)) { |
| 278 | args = Array.prototype.slice.call(arguments, 1); |
| 279 | args.unshift('['+tag+']'); |
| 280 | $log.debug.apply(this, args); |
| 281 | } |
| 282 | } |
| 283 | |
Simon Hunt | 8b28c6b | 2016-02-03 12:33:15 -0800 | [diff] [blame] | 284 | // trie operation |
| 285 | function _trieOp(op, trie, word, data) { |
| 286 | var p = trie, |
| 287 | w = word.toUpperCase(), |
| 288 | s = w.split(''), |
| 289 | c = { p: p, s: s }, |
| 290 | t = [], |
| 291 | x = 0, |
| 292 | f1 = op === '+' ? add : probe, |
| 293 | f2 = op === '+' ? insert : remove; |
Simon Hunt | 737ba48 | 2016-01-30 16:11:13 -0800 | [diff] [blame] | 294 | |
| 295 | function add(c) { |
| 296 | var q = c.s.shift(), |
| 297 | np = c.p[q]; |
| 298 | |
| 299 | if (!np) { |
| 300 | c.p[q] = {}; |
| 301 | np = c.p[q]; |
Simon Hunt | 8b28c6b | 2016-02-03 12:33:15 -0800 | [diff] [blame] | 302 | x = 1; |
Simon Hunt | 737ba48 | 2016-01-30 16:11:13 -0800 | [diff] [blame] | 303 | } |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 304 | return { p: np, s: c.s }; |
Simon Hunt | 737ba48 | 2016-01-30 16:11:13 -0800 | [diff] [blame] | 305 | } |
| 306 | |
Simon Hunt | 8b28c6b | 2016-02-03 12:33:15 -0800 | [diff] [blame] | 307 | function probe(c) { |
| 308 | var q = c.s.shift(), |
| 309 | k = Object.keys(c.p).length, |
| 310 | np = c.p[q]; |
Simon Hunt | 737ba48 | 2016-01-30 16:11:13 -0800 | [diff] [blame] | 311 | |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 312 | t.push({ q: q, k: k, p: c.p }); |
Simon Hunt | 8b28c6b | 2016-02-03 12:33:15 -0800 | [diff] [blame] | 313 | if (!np) { |
| 314 | t = []; |
| 315 | return { s: [] }; |
Simon Hunt | 737ba48 | 2016-01-30 16:11:13 -0800 | [diff] [blame] | 316 | } |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 317 | return { p: np, s: c.s }; |
Simon Hunt | 8b28c6b | 2016-02-03 12:33:15 -0800 | [diff] [blame] | 318 | } |
Simon Hunt | 737ba48 | 2016-01-30 16:11:13 -0800 | [diff] [blame] | 319 | |
Simon Hunt | 8b28c6b | 2016-02-03 12:33:15 -0800 | [diff] [blame] | 320 | function insert() { |
| 321 | c.p._data = data; |
| 322 | return x ? 'added' : 'updated'; |
| 323 | } |
| 324 | |
| 325 | function remove() { |
| 326 | if (t.length) { |
| 327 | t = t.reverse(); |
| 328 | while (t.length) { |
| 329 | c = t.shift(); |
| 330 | delete c.p[c.q]; |
| 331 | if (c.k > 1) { |
| 332 | t = []; |
| 333 | } |
| 334 | } |
| 335 | return 'removed'; |
| 336 | } |
| 337 | return 'absent'; |
| 338 | } |
| 339 | |
| 340 | while (c.s.length) { |
| 341 | c = f1(c); |
| 342 | } |
| 343 | return f2(); |
Simon Hunt | 737ba48 | 2016-01-30 16:11:13 -0800 | [diff] [blame] | 344 | } |
| 345 | |
Simon Hunt | 8b28c6b | 2016-02-03 12:33:15 -0800 | [diff] [blame] | 346 | // add word to trie (word will be converted to uppercase) |
| 347 | // data associated with the word |
| 348 | // returns 'added' or 'updated' |
| 349 | function addToTrie(trie, word, data) { |
| 350 | return _trieOp('+', trie, word, data); |
| 351 | } |
| 352 | |
| 353 | // remove word from trie (word will be converted to uppercase) |
| 354 | // returns 'removed' or 'absent' |
| 355 | function removeFromTrie(trie, word) { |
| 356 | return _trieOp('-', trie, word); |
| 357 | } |
| 358 | |
| 359 | // lookup word (converted to uppercase) in trie |
| 360 | // returns: |
| 361 | // undefined if the word is not in the trie |
| 362 | // -1 for a partial match (word is a prefix to an existing word) |
| 363 | // data for the word for an exact match |
| 364 | function trieLookup(trie, word) { |
| 365 | var s = word.toUpperCase().split(''), |
| 366 | p = trie, |
| 367 | n; |
| 368 | |
| 369 | while (s.length) { |
| 370 | n = s.shift(); |
| 371 | p = p[n]; |
| 372 | if (!p) { |
| 373 | return undefined; |
| 374 | } |
| 375 | } |
| 376 | if (p._data) { |
| 377 | return p._data; |
| 378 | } |
| 379 | return -1; |
| 380 | } |
| 381 | |
| 382 | |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 383 | var hasOwn = {}.hasOwnProperty; |
| 384 | |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 385 | function classNames() { |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 386 | var classes = []; |
| 387 | |
| 388 | for (var i = 0; i < arguments.length; i++) { |
| 389 | var arg = arguments[i]; |
| 390 | if (!arg) continue; |
| 391 | |
| 392 | var argType = typeof arg; |
| 393 | |
| 394 | if (argType === 'string' || argType === 'number') { |
| 395 | classes.push(arg); |
| 396 | } else if (Array.isArray(arg)) { |
| 397 | classes.push(classNames.apply(null, arg)); |
| 398 | } else if (argType === 'object') { |
| 399 | for (var key in arg) { |
| 400 | if (hasOwn.call(arg, key) && arg[key]) { |
| 401 | classes.push(key); |
| 402 | } |
| 403 | } |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | return classes.join(' '); |
| 408 | } |
| 409 | |
Steven Burrows | 86af435 | 2016-11-16 18:19:12 -0600 | [diff] [blame] | 410 | function extend(protoProps, staticProps) { |
| 411 | |
| 412 | var parent = this, |
| 413 | child; |
| 414 | |
| 415 | child = function () { |
| 416 | return parent.apply(this, arguments); |
| 417 | }; |
| 418 | |
| 419 | angular.extend(child, parent, staticProps); |
| 420 | |
| 421 | // Set the prototype chain to inherit from `parent`, without calling |
| 422 | // `parent`'s constructor function and add the prototype properties. |
| 423 | child.prototype = angular.extend({}, parent.prototype, protoProps); |
| 424 | child.prototype.constructor = child; |
| 425 | |
| 426 | // Set a convenience property in case the parent's prototype is needed |
| 427 | // later. |
| 428 | child.__super__ = parent.prototype; |
| 429 | |
| 430 | return child; |
| 431 | } |
| 432 | |
Simon Hunt | 0fe05d6 | 2017-05-17 16:42:32 -0700 | [diff] [blame] | 433 | // ----------------------------------------------------------------- |
| 434 | // The next section deals with sanitizing external strings destined |
| 435 | // to be loaded via a .html() function call. |
| 436 | |
| 437 | var matcher = /<\/?([a-zA-Z0-9]+)*(.*?)\/?>/igm, |
Simon Hunt | 239f09e | 2017-05-18 13:10:09 -0700 | [diff] [blame] | 438 | whitelist = ['b', 'i', 'p', 'em', 'strong', 'br'], |
Simon Hunt | 4a8de82 | 2017-05-17 17:09:49 -0700 | [diff] [blame] | 439 | evillist = ['script', 'style', 'iframe']; |
Simon Hunt | 0fe05d6 | 2017-05-17 16:42:32 -0700 | [diff] [blame] | 440 | |
Simon Hunt | 4a8de82 | 2017-05-17 17:09:49 -0700 | [diff] [blame] | 441 | // Returns true if the tag is in the evil list, (and is not an end-tag) |
| 442 | function inEvilList(tag) { |
| 443 | return (evillist.indexOf(tag.name) !== -1 && tag.full.indexOf('/') === -1); |
Simon Hunt | 0fe05d6 | 2017-05-17 16:42:32 -0700 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | function analyze(html) { |
| 447 | html = String(html) || ''; |
| 448 | |
| 449 | var matches = [], |
| 450 | match; |
| 451 | |
| 452 | // extract all tags |
| 453 | while ((match = matcher.exec(html)) !== null) { |
| 454 | matches.push({ |
| 455 | full: match[0], |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 456 | name: match[1], |
Simon Hunt | 0fe05d6 | 2017-05-17 16:42:32 -0700 | [diff] [blame] | 457 | // NOTE: ignoring attributes {match[2].split(' ')} for now |
| 458 | }); |
| 459 | } |
| 460 | |
| 461 | return matches; |
| 462 | } |
| 463 | |
| 464 | function sanitize(html) { |
| 465 | html = String(html) || ''; |
| 466 | |
| 467 | var matches = analyze(html); |
| 468 | |
Simon Hunt | 4a8de82 | 2017-05-17 17:09:49 -0700 | [diff] [blame] | 469 | // completely obliterate evil tags and their contents... |
| 470 | evillist.forEach(function (tag) { |
| 471 | var re = new RegExp('<' + tag + '(.*?)>(.*?[\r\n])*?(.*?)(.*?[\r\n])*?<\/' + tag + '>', 'gim'); |
| 472 | html = html.replace(re, ''); |
| 473 | }); |
Simon Hunt | 0fe05d6 | 2017-05-17 16:42:32 -0700 | [diff] [blame] | 474 | |
Simon Hunt | 4a8de82 | 2017-05-17 17:09:49 -0700 | [diff] [blame] | 475 | // filter out all but white-listed tags and end-tags |
Simon Hunt | 0fe05d6 | 2017-05-17 16:42:32 -0700 | [diff] [blame] | 476 | matches.forEach(function (tag) { |
| 477 | if (whitelist.indexOf(tag.name) === -1) { |
| 478 | html = html.replace(tag.full, ''); |
Simon Hunt | 4a8de82 | 2017-05-17 17:09:49 -0700 | [diff] [blame] | 479 | if (inEvilList(tag)) { |
Simon Hunt | 0fe05d6 | 2017-05-17 16:42:32 -0700 | [diff] [blame] | 480 | $log.warn('Unsanitary HTML input -- ' + tag.full + ' detected!'); |
| 481 | } |
| 482 | } |
| 483 | }); |
| 484 | |
| 485 | // TODO: consider encoding HTML entities, e.g. '&' -> '&' |
| 486 | |
| 487 | return html; |
| 488 | } |
| 489 | |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 490 | |
Simon Hunt | dc6362a | 2014-12-18 19:55:23 -0800 | [diff] [blame] | 491 | angular.module('onosUtil') |
Simon Hunt | 4deb0e8 | 2015-06-10 16:18:25 -0700 | [diff] [blame] | 492 | .factory('FnService', |
| 493 | ['$window', '$location', '$log', function (_$window_, $loc, _$log_) { |
Simon Hunt | a11b4eb | 2015-01-28 16:20:50 -0800 | [diff] [blame] | 494 | $window = _$window_; |
Simon Hunt | 4deb0e8 | 2015-06-10 16:18:25 -0700 | [diff] [blame] | 495 | $log = _$log_; |
| 496 | |
| 497 | _parseDebugFlags($loc.search().debug); |
Simon Hunt | a11b4eb | 2015-01-28 16:20:50 -0800 | [diff] [blame] | 498 | |
Simon Hunt | dc6362a | 2014-12-18 19:55:23 -0800 | [diff] [blame] | 499 | return { |
| 500 | isF: isF, |
| 501 | isA: isA, |
| 502 | isS: isS, |
| 503 | isO: isO, |
Simon Hunt | 51fc40b | 2015-01-06 13:56:12 -0800 | [diff] [blame] | 504 | contains: contains, |
Simon Hunt | a11b4eb | 2015-01-28 16:20:50 -0800 | [diff] [blame] | 505 | areFunctions: areFunctions, |
Simon Hunt | 48e6167 | 2015-01-30 14:48:25 -0800 | [diff] [blame] | 506 | areFunctionsNonStrict: areFunctionsNonStrict, |
| 507 | windowSize: windowSize, |
Simon Hunt | 31bb01f | 2015-03-31 16:50:41 -0700 | [diff] [blame] | 508 | isMobile: isMobile, |
Bri Prebilic Cole | 55ee09b | 2015-08-04 14:34:07 -0700 | [diff] [blame] | 509 | isChrome: isChrome, |
| 510 | isSafari: isSafari, |
| 511 | isFirefox: isFirefox, |
Simon Hunt | 4deb0e8 | 2015-06-10 16:18:25 -0700 | [diff] [blame] | 512 | debugOn: debugOn, |
Simon Hunt | f90c18b | 2016-01-25 15:38:58 -0800 | [diff] [blame] | 513 | debug: debug, |
Simon Hunt | 205099e | 2015-02-07 13:12:01 -0800 | [diff] [blame] | 514 | find: find, |
| 515 | inArray: inArray, |
Simon Hunt | 27a5cc8 | 2015-02-19 14:49:55 -0800 | [diff] [blame] | 516 | removeFromArray: removeFromArray, |
Bri Prebilic Cole | 19a32dd | 2015-03-26 18:00:03 -0700 | [diff] [blame] | 517 | isEmptyObject: isEmptyObject, |
Bri Prebilic Cole | 0bc4de2 | 2015-07-20 17:07:55 -0700 | [diff] [blame] | 518 | sameObjProps: sameObjProps, |
| 519 | containsObj: containsObj, |
Bri Prebilic Cole | 08d08d4 | 2015-04-01 14:37:02 -0700 | [diff] [blame] | 520 | cap: cap, |
Simon Hunt | b1d35e8 | 2016-01-26 13:54:06 -0800 | [diff] [blame] | 521 | eecode: eecode, |
Bri Prebilic Cole | 08d08d4 | 2015-04-01 14:37:02 -0700 | [diff] [blame] | 522 | noPx: noPx, |
Simon Hunt | 9664137 | 2015-06-02 15:53:25 -0700 | [diff] [blame] | 523 | noPxStyle: noPxStyle, |
| 524 | endsWith: endsWith, |
Simon Hunt | 8b28c6b | 2016-02-03 12:33:15 -0800 | [diff] [blame] | 525 | addToTrie: addToTrie, |
| 526 | removeFromTrie: removeFromTrie, |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 527 | trieLookup: trieLookup, |
Steven Burrows | 86af435 | 2016-11-16 18:19:12 -0600 | [diff] [blame] | 528 | classNames: classNames, |
Simon Hunt | 0fe05d6 | 2017-05-17 16:42:32 -0700 | [diff] [blame] | 529 | extend: extend, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 530 | sanitize: sanitize, |
Simon Hunt | dc6362a | 2014-12-18 19:55:23 -0800 | [diff] [blame] | 531 | }; |
Simon Hunt | 1eecfa2 | 2014-12-16 14:46:29 -0800 | [diff] [blame] | 532 | }]); |
| 533 | |
Simon Hunt | dc6362a | 2014-12-18 19:55:23 -0800 | [diff] [blame] | 534 | }()); |