Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit ec3c4f9

Browse files
tboschpetebacondarwin
authored andcommitted
refactor($sce): Use $sniffer instead of $document for feature detection.
Also adds `$sniffer.msieDocumentMode` property. Closes #4931 Closes #5045
1 parent 6b8bbe4 commit ec3c4f9

File tree

4 files changed

+30
-34
lines changed

4 files changed

+30
-34
lines changed

src/ng/sce.js

+8-12
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,7 @@ function $SceDelegateProvider() {
199199
return resourceUrlBlacklist;
200200
};
201201

202-
this.$get = ['$log', '$document', '$injector', function(
203-
$log, $document, $injector) {
202+
this.$get = ['$injector', function($injector) {
204203

205204
var htmlSanitizer = function htmlSanitizer(html) {
206205
throw $sceMinErr('unsafe', 'Attempting to use an unsafe value in a safe context.');
@@ -731,18 +730,15 @@ function $SceProvider() {
731730
* sce.js and sceSpecs.js would need to be aware of this detail.
732731
*/
733732

734-
this.$get = ['$parse', '$document', '$sceDelegate', function(
735-
$parse, $document, $sceDelegate) {
733+
this.$get = ['$parse', '$sniffer', '$sceDelegate', function(
734+
$parse, $sniffer, $sceDelegate) {
736735
// Prereq: Ensure that we're not running in IE8 quirks mode. In that mode, IE allows
737736
// the "expression(javascript expression)" syntax which is insecure.
738-
if (enabled && msie) {
739-
var documentMode = $document[0].documentMode;
740-
if (documentMode !== undefined && documentMode < 8) {
741-
throw $sceMinErr('iequirks',
742-
'Strict Contextual Escaping does not support Internet Explorer version < 9 in quirks ' +
743-
'mode. You can fix this by adding the text <!doctype html> to the top of your HTML ' +
744-
'document. See http://docs.angularjs.org/api/ng.$sce for more information.');
745-
}
737+
if (enabled && $sniffer.msie && $sniffer.msieDocumentMode < 8) {
738+
throw $sceMinErr('iequirks',
739+
'Strict Contextual Escaping does not support Internet Explorer version < 9 in quirks ' +
740+
'mode. You can fix this by adding the text <!doctype html> to the top of your HTML ' +
741+
'document. See http://docs.angularjs.org/api/ng.$sce for more information.');
746742
}
747743

748744
var sce = copy(SCE_CONTEXTS);

src/ng/sniffer.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ function $SnifferProvider() {
2222
int((/android (\d+)/.exec(lowercase(($window.navigator || {}).userAgent)) || [])[1]),
2323
boxee = /Boxee/i.test(($window.navigator || {}).userAgent),
2424
document = $document[0] || {},
25+
documentMode = document.documentMode,
2526
vendorPrefix,
2627
vendorRegex = /^(Moz|webkit|O|ms)(?=[A-Z])/,
2728
bodyStyle = document.body && document.body.style,
@@ -66,7 +67,7 @@ function $SnifferProvider() {
6667
// jshint +W018
6768
hashchange: 'onhashchange' in $window &&
6869
// IE8 compatible mode lies
69-
(!document.documentMode || document.documentMode > 7),
70+
(!documentMode || documentMode > 7),
7071
hasEvent: function(event) {
7172
// IE9 implements 'input' event it's so fubared that we rather pretend that it doesn't have
7273
// it. In particular the event is not fired when backspace or delete key are pressed or
@@ -84,7 +85,8 @@ function $SnifferProvider() {
8485
vendorPrefix: vendorPrefix,
8586
transitions : transitions,
8687
animations : animations,
87-
msie : msie
88+
msie : msie,
89+
msieDocumentMode: documentMode
8890
};
8991
}];
9092
}

test/ng/sceSpecs.js

+13-20
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ describe('SCE', function() {
2929
describe('IE8 quirks mode', function() {
3030
function runTest(enabled, documentMode, expectException) {
3131
module(function($provide) {
32-
$provide.value('$document', [{
33-
documentMode: documentMode,
34-
createElement: function() {}
35-
}]);
32+
$provide.value('$sniffer', {
33+
msie: documentMode,
34+
msieDocumentMode: documentMode
35+
});
3636
$provide.value('$sceDelegate', {trustAs: null, valueOf: null, getTrusted: null});
3737
});
3838

@@ -43,22 +43,15 @@ describe('SCE', function() {
4343
return $injector.invoke(sceProvider.$get, sceProvider);
4444
}
4545

46-
var origMsie = $window.msie;
47-
try {
48-
$window.msie = true;
49-
if (expectException) {
50-
expect(constructSce).toThrowMinErr(
51-
'$sce', 'iequirks', 'Strict Contextual Escaping does not support Internet Explorer ' +
52-
'version < 9 in quirks mode. You can fix this by adding the text <!doctype html> to ' +
53-
'the top of your HTML document. See http://docs.angularjs.org/api/ng.$sce for more ' +
54-
'information.');
55-
} else {
56-
// no exception.
57-
constructSce();
58-
}
59-
}
60-
finally {
61-
$window.msie = origMsie;
46+
if (expectException) {
47+
expect(constructSce).toThrowMinErr(
48+
'$sce', 'iequirks', 'Strict Contextual Escaping does not support Internet Explorer ' +
49+
'version < 9 in quirks mode. You can fix this by adding the text <!doctype html> to ' +
50+
'the top of your HTML document. See http://docs.angularjs.org/api/ng.$sce for more ' +
51+
'information.');
52+
} else {
53+
// no exception.
54+
constructSce();
6255
}
6356
});
6457
}

test/ng/snifferSpec.js

+5
Original file line numberDiff line numberDiff line change
@@ -337,4 +337,9 @@ describe('$sniffer', function() {
337337
it('should return true for msie when internet explorer is being used', inject(function($sniffer) {
338338
expect($sniffer.msie > 0).toBe(window.navigator.appName == 'Microsoft Internet Explorer');
339339
}));
340+
341+
it('should return document.documentMode as msieDocumentMode', function() {
342+
var someDocumentMode = 123;
343+
expect(sniffer({}, {documentMode: someDocumentMode}).msieDocumentMode).toBe(someDocumentMode);
344+
});
340345
});

0 commit comments

Comments
 (0)