Skip to content

Commit

Permalink
Merge pull request #271 from fmtvp/IPTV-358
Browse files Browse the repository at this point in the history
Added in Array.prototype.indexOf es5 shim.
  • Loading branch information
dhurrell committed Jul 28, 2015
2 parents eec87e2 + 0c9857d commit f36df49
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 53 deletions.
33 changes: 3 additions & 30 deletions static/script-tests/tests/sanitisers/whitelisted.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,17 @@
require(
[
'antie/devices/sanitiser',
'antie/runtimecontext',
'antie/devices/browserdevice',
'antie/devices/sanitisers/whitelisted'
],
function(Sanitiser, RuntimeContext, BrowserDevice) {
function(Sanitiser) {
"use strict";

describe('Basic Sanitisation', function() {

var el, mockDevice, mockApp;
var el;

beforeEach(function () {
el = document.createElement('div');
mockDevice = new BrowserDevice({});
mockApp = {
getDevice: function() { return mockDevice;}
};
RuntimeContext.setCurrentApplication(mockApp);
});

afterEach(function () {
RuntimeContext.clearCurrentApplication();
});

it('returns the text string when no dom elements are present', function() {
Expand Down Expand Up @@ -146,28 +135,12 @@
expect(el.firstChild.childNodes[1].firstChild.data).toEqual('my string');
expect(el.firstChild.childNodes[2].data).toEqual('another string');
});

it ('uses arrayIndexOf from browserdevice', function () {
var string = "<p>my string</p>",
sanitiser = new Sanitiser(string);
spyOn(mockDevice, 'arrayIndexOf');

sanitiser.setElementContent(el);

expect(mockDevice.arrayIndexOf).toHaveBeenCalled();
});
});

describe('Mixed Type Sanitzation', function() {

var el = document.createElement('div');
it ('returns mixed string with sanitsation implemented', function () {
var mockDevice = new BrowserDevice({});
var mockApp = {
getDevice: function() { return mockDevice;}
};
RuntimeContext.setCurrentApplication(mockApp);

var string = "<div><h1>Title</h1><ul><li>list 1</li><li><script>nastiness</script>OK</li></ul></div>",
result = "<h1>Title</h1><ul><li>list 1</li><li>OK</li></ul>",
sanitiser = new Sanitiser(string);
Expand All @@ -178,4 +151,4 @@

});
}
);
);
10 changes: 2 additions & 8 deletions static/script-tests/tests/widgets/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
};

this.WidgetTest.prototype.testAddEventListener = function(queue) {
expectAsserts(3);
expectAsserts(2);

queuedApplicationInit(
queue,
Expand All @@ -142,20 +142,17 @@
var widget = new Widget();
var handler = this.sandbox.stub(),
handler2 = this.sandbox.stub();
var indexOfSpy = this.sandbox.spy(application.getDevice(), 'arrayIndexOf');

widget.addEventListener('anevent', handler);
widget.addEventListener('anevent', handler2);
widget.fireEvent(new Event('anevent'));
assert(handler.called);
assert(handler2.called);
assert(indexOfSpy.calledTwice);
}
);
};

this.WidgetTest.prototype.testRemoveEventListener = function(queue) {
expectAsserts(2);
expectAsserts(1);

queuedApplicationInit(
queue,
Expand All @@ -165,12 +162,9 @@
var widget = new Widget();
var handler = this.sandbox.stub();
widget.addEventListener('anevent', handler);
var indexOfSpy = this.sandbox.spy(application.getDevice(), 'arrayIndexOf');

widget.removeEventListener('anevent', handler);
widget.fireEvent(new Event('anevent'));
assertFalse(handler.called);
assert(indexOfSpy.calledOnce);
}
);
};
Expand Down
18 changes: 9 additions & 9 deletions static/script/devices/sanitisers/whitelisted.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
* All rights reserved
* Please contact us for an alternative licence
*/
require.def('antie/devices/sanitisers/whitelisted',
require.def('antie/devices/sanitisers/whitelisted',
[
'antie/devices/sanitiser',
'antie/runtimecontext'
'antie/devices/sanitiser',
'antie/lib/array.indexof' // Adds Array.prototype.indexOf()
],
function (Sanitiser, RuntimeContext) {
function (Sanitiser) {

'use strict';

Expand Down Expand Up @@ -79,7 +79,7 @@
if (window.DOMParser) {
var parser = new DOMParser();
xmlDoc = parser.parseFromString(string, "text/xml");

}
else // Internet Explorer
{
Expand All @@ -91,7 +91,7 @@
},

_replaceEntities: function (string) {

var replaced = {},
regexp,
matches = string.match(/&[a-zA-Z0-9]*;/g);
Expand All @@ -110,7 +110,7 @@
string = string.replace(regexp, this._entities[matches[i]]);
}
}

return string;

},
Expand All @@ -122,7 +122,7 @@

for (var i = 0; i < content.length; i++) {
if (content[i].tagName) {
if (RuntimeContext.getDevice().arrayIndexOf(this._whitelist, content[i].tagName) !== -1) {
if (this._whitelist.indexOf(content[i].tagName) !== -1) {
el = document.createElement(content[i].tagName);
el = this._processDomElement(content[i], el);
originalDom.appendChild(el);
Expand All @@ -136,4 +136,4 @@
}
};

});
});
10 changes: 7 additions & 3 deletions static/script/events/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@
*/

require.def('antie/events/event',
['antie/class', 'antie/runtimecontext'],
[
'antie/class',
'antie/runtimecontext',
'antie/lib/array.indexof' // Adds Array.prototype.indexOf()
],
function(Class, RuntimeContext) {
'use strict';

Expand Down Expand Up @@ -104,7 +108,7 @@ require.def('antie/events/event',
listeners = [];
eventListeners[ev] = listeners;
}
if (!~RuntimeContext.getDevice().arrayIndexOf(listeners, func)) {
if (!~listeners.indexOf(func)) {
listeners.push(func);
}
},
Expand All @@ -124,7 +128,7 @@ require.def('antie/events/event',
return false;
}

listener = RuntimeContext.getDevice().arrayIndexOf(listeners, func);
listener = listeners.indexOf(func);
if (~listener) {
listeners.splice(listener, 1);
}
Expand Down
72 changes: 72 additions & 0 deletions static/script/lib/array.indexof.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
require.def('antie/lib/array.indexof', function() {

// From: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf
// License: CC-BY-SA 2.5. http://creativecommons.org/licenses/by-sa/2.5/
// Changes: No changes from original

// Production steps of ECMA-262, Edition 5, 15.4.4.14
// Reference: http://es5.github.io/#x15.4.4.14
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function(searchElement, fromIndex) {

var k;

// 1. Let O be the result of calling ToObject passing
// the this value as the argument.
if (this == null) {
throw new TypeError('"this" is null or not defined');
}

var O = Object(this);

// 2. Let lenValue be the result of calling the Get
// internal method of O with the argument "length".
// 3. Let len be ToUint32(lenValue).
var len = O.length >>> 0;

// 4. If len is 0, return -1.
if (len === 0) {
return -1;
}

// 5. If argument fromIndex was passed let n be
// ToInteger(fromIndex); else let n be 0.
var n = +fromIndex || 0;

if (Math.abs(n) === Infinity) {
n = 0;
}

// 6. If n >= len, return -1.
if (n >= len) {
return -1;
}

// 7. If n >= 0, then Let k be n.
// 8. Else, n<0, Let k be len - abs(n).
// If k is less than 0, then let k be 0.
k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);

// 9. Repeat, while k < len
while (k < len) {
// a. Let Pk be ToString(k).
// This is implicit for LHS operands of the in operator
// b. Let kPresent be the result of calling the
// HasProperty internal method of O with argument Pk.
// This step can be combined with c
// c. If kPresent is true, then
// i. Let elementK be the result of calling the Get
// internal method of O with the argument ToString(k).
// ii. Let same be the result of applying the
// Strict Equality Comparison Algorithm to
// searchElement and elementK.
// iii. If same is true, return k.
if (k in O && O[k] === searchElement) {
return k;
}
k++;
}
return -1;
};
}
});
7 changes: 4 additions & 3 deletions static/script/widgets/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
require.def('antie/widgets/widget',
[
'antie/class',
'antie/runtimecontext'
'antie/runtimecontext',
'antie/lib/array.indexof' // Adds Array.prototype.indexOf()
],
function(Class, RuntimeContext) {
'use strict';
Expand Down Expand Up @@ -138,7 +139,7 @@ require.def('antie/widgets/widget',
listeners = [];
this._eventListeners[ev] = listeners;
}
if (!~RuntimeContext.getDevice().arrayIndexOf(listeners, func)) {
if (!~listeners.indexOf(func)) {
listeners.push(func);
}
},
Expand All @@ -157,7 +158,7 @@ require.def('antie/widgets/widget',
return false;
}

listener = RuntimeContext.getDevice().arrayIndexOf(listeners, func);
listener = listeners.indexOf(func);
if (~listener) {
listeners.splice(listener, 1);
}
Expand Down

0 comments on commit f36df49

Please sign in to comment.