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

Commit e92f934

Browse files
JiaLiPassionmhevery
authored andcommitted
add web-api.ts to patch mediaQuery (#571)
1 parent 7cd570e commit e92f934

File tree

4 files changed

+78
-0
lines changed

4 files changed

+78
-0
lines changed

Diff for: gulpfile.js

+5
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ gulp.task('build/zone.min.js', ['compile-esm'], function(cb) {
8686
return generateScript('./lib/browser/rollup-main.ts', 'zone.min.js', true, cb);
8787
});
8888

89+
gulp.task('build/web-api.js', ['compile-esm'], function(cb) {
90+
return generateScript('./lib/browser/web-api.ts', 'web-api.js', true, cb);
91+
});
92+
8993
gulp.task('build/jasmine-patch.js', ['compile-esm'], function(cb) {
9094
return generateScript('./lib/jasmine/jasmine.ts', 'jasmine-patch.js', false, cb);
9195
});
@@ -151,6 +155,7 @@ gulp.task('build', [
151155
'build/zone.js.d.ts',
152156
'build/zone.min.js',
153157
'build/zone-node.js',
158+
'build/web-api.js',
154159
'build/jasmine-patch.js',
155160
'build/jasmine-patch.min.js',
156161
'build/mocha-patch.js',

Diff for: lib/browser/web-api.ts

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
import {NestedEventListenerOrEventListenerObject, patchEventTargetMethods} from '../common/utils';
9+
10+
((_global: any) => {
11+
// patch MediaQuery
12+
patchMediaQuery(_global);
13+
14+
function patchMediaQuery(_global: any) {
15+
if (!_global['MediaQueryList']) {
16+
return;
17+
}
18+
patchEventTargetMethods(
19+
_global['MediaQueryList'].prototype, 'addListener', 'removeListener', (self, args) => {
20+
return {
21+
useCapturing: false,
22+
eventName: 'mediaQuery',
23+
handler: args[0],
24+
target: self || _global,
25+
name: 'mediaQuery',
26+
invokeAddFunc: function(
27+
addFnSymbol: any, delegate: Task|NestedEventListenerOrEventListenerObject) {
28+
if (delegate && (<Task>delegate).invoke) {
29+
return this.target[addFnSymbol]((<Task>delegate).invoke);
30+
} else {
31+
return this.target[addFnSymbol](delegate);
32+
}
33+
},
34+
invokeRemoveFunc: function(
35+
removeFnSymbol: any, delegate: Task|NestedEventListenerOrEventListenerObject) {
36+
if (delegate && (<Task>delegate).invoke) {
37+
return this.target[removeFnSymbol]((<Task>delegate).invoke);
38+
} else {
39+
return this.target[removeFnSymbol](delegate);
40+
}
41+
}
42+
};
43+
});
44+
}
45+
})(typeof window === 'object' && window || typeof self === 'object' && self || global);

Diff for: test/browser/MediaQuery.spec.ts

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import '../../lib/browser/web-api';
10+
11+
import {zoneSymbol} from '../../lib/common/utils';
12+
import {ifEnvSupports} from '../test-util';
13+
14+
function supportMediaQuery() {
15+
const _global =
16+
typeof window === 'object' && window || typeof self === 'object' && self || global;
17+
return _global['MediaQueryList'] && _global['matchMedia'];
18+
}
19+
20+
describe('test mediaQuery patch', ifEnvSupports(supportMediaQuery, () => {
21+
it('test whether addListener is patched', () => {
22+
const mqList = window.matchMedia('min-width:500px');
23+
if (mqList && mqList['addListener']) {
24+
expect(mqList[zoneSymbol('addListener')]).not.toBe(undefined);
25+
}
26+
});
27+
}));

Diff for: test/browser_entry_point.ts

+1
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ import './browser/registerElement.spec';
1919
import './browser/requestAnimationFrame.spec';
2020
import './browser/WebSocket.spec';
2121
import './browser/XMLHttpRequest.spec';
22+
import './browser/MediaQuery.spec';
2223
import './mocha-patch.spec';

0 commit comments

Comments
 (0)