OLD | NEW |
1 library safe_dom_test; | 1 library safe_dom_test; |
2 | 2 |
3 import 'dart:async'; | 3 import 'dart:async'; |
4 import 'dart:html'; | 4 import 'dart:html'; |
5 import '../../pkg/unittest/lib/unittest.dart'; | 5 import '../../pkg/unittest/lib/unittest.dart'; |
6 import '../../pkg/unittest/lib/html_config.dart'; | 6 import '../../pkg/unittest/lib/html_config.dart'; |
7 | 7 |
8 main() { | 8 main() { |
9 useHtmlConfiguration(); | 9 useHtmlConfiguration(); |
10 | 10 |
(...skipping 21 matching lines...) Expand all Loading... |
32 test('Safe DOM', () { | 32 test('Safe DOM', () { |
33 var fragment = createContextualFragment(unsafeString); | 33 var fragment = createContextualFragment(unsafeString); |
34 | 34 |
35 expect(isSafe(), completion(true), | 35 expect(isSafe(), completion(true), |
36 reason: 'Expected no unsafe code executed.'); | 36 reason: 'Expected no unsafe code executed.'); |
37 }); | 37 }); |
38 | 38 |
39 // Make sure that scripts did get executed, so we know our detection works. | 39 // Make sure that scripts did get executed, so we know our detection works. |
40 test('Unsafe Execution', () { | 40 test('Unsafe Execution', () { |
41 var div = new DivElement(); | 41 var div = new DivElement(); |
42 div.innerHtml = unsafeString; | 42 div.$dom_innerHtml = unsafeString; |
43 // Crashing DRT ?? | 43 // Crashing DRT ?? |
44 // var fragment = createContextualFragment(unsafeString); | 44 // var fragment = createContextualFragment(unsafeString); |
45 // div.append(fragment); | 45 // div.append(fragment); |
46 // document.body.append(div) | 46 // document.body.append(div) |
47 | 47 |
48 expect(isSafe(), completion(false), | 48 expect(isSafe(), completion(false), |
49 reason: 'Expected unsafe code was executed.'); | 49 reason: 'Expected unsafe code was executed.'); |
50 }); | 50 }); |
51 | 51 |
52 test('Validity', () { | 52 test('Validity', () { |
(...skipping 14 matching lines...) Expand all Loading... |
67 contextElement = doc.$dom_createElement(contextTag); | 67 contextElement = doc.$dom_createElement(contextTag); |
68 } else { | 68 } else { |
69 contextElement = doc.body; | 69 contextElement = doc.body; |
70 } | 70 } |
71 | 71 |
72 if (Range.supportsCreateContextualFragment) { | 72 if (Range.supportsCreateContextualFragment) { |
73 var range = doc.$dom_createRange(); | 73 var range = doc.$dom_createRange(); |
74 range.selectNode(contextElement); | 74 range.selectNode(contextElement); |
75 return range.createContextualFragment(html); | 75 return range.createContextualFragment(html); |
76 } else { | 76 } else { |
77 contextElement.innerHtml = html; | 77 contextElement.unsafeInnerHtml = html; |
78 var fragment = new DocumentFragment();; | 78 var fragment = new DocumentFragment();; |
79 while (contextElement.firstChild != null) { | 79 while (contextElement.firstChild != null) { |
80 fragment.append(contextElement.firstChild); | 80 fragment.append(contextElement.firstChild); |
81 } | 81 } |
82 return fragment; | 82 return fragment; |
83 } | 83 } |
84 } | 84 } |
85 | 85 |
86 // Delay to wait for the image load to fail. | 86 // Delay to wait for the image load to fail. |
87 const Duration imageLoadDelay = const Duration(milliseconds: 500); | 87 const Duration imageLoadDelay = const Duration(milliseconds: 500); |
88 | 88 |
89 Future<bool> isSafe() { | 89 Future<bool> isSafe() { |
90 return new Future.delayed(imageLoadDelay).then((_) { | 90 return new Future.delayed(imageLoadDelay).then((_) { |
91 window.postMessage('check_unsafe', '*'); | 91 window.postMessage('check_unsafe', '*'); |
92 }).then((_) { | 92 }).then((_) { |
93 return window.onMessage.where( | 93 return window.onMessage.where( |
94 (e) => e.data.startsWith('unsafe_check')).first; | 94 (e) => e.data.startsWith('unsafe_check')).first; |
95 }).then((e) { | 95 }).then((e) { |
96 return e.data == 'unsafe_check_passed'; | 96 return e.data == 'unsafe_check_passed'; |
97 }); | 97 }); |
98 } | 98 } |
OLD | NEW |