Passed
Push — develop ( 667b4c...58391b )
by Andrew
04:57
created

src/web/assets/src/safari-nomodule-fix.js   A

Complexity

Total Complexity 5
Complexity/F 2.5

Size

Lines of Code 19
Function Count 2

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 15
mnd 3
bc 3
fnc 2
dl 0
loc 19
rs 10
bpm 1.5
cpm 2.5
noi 4
c 0
b 0
f 0
1
/*
2
 * Copyright 2017 Google LLC
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
 *     https://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
/**
18
 * Safari 10.1 supports modules, but does not support the `nomodule` attribute - it will
19
 * load <script nomodule> anyway. This snippet solve this problem, but only for script
20
 * tags that load external code, e.g.: <script nomodule src="nomodule.js"></script>
21
 *
22
 * Again: this will **not** prevent inline script, e.g.:
23
 * <script nomodule>alert('no modules');</script>.
24
 *
25
 * This workaround is possible because Safari supports the non-standard 'beforeload' event.
26
 * This allows us to trap the module and nomodule load.
27
 *
28
 * Note also that `nomodule` is supported in later versions of Safari - it's just 10.1 that
29
 * omits this attribute.
30
 */
31
(function() {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
32
    var check = document.createElement('script');
33
    if (!('noModule' in check) && 'onbeforeload' in check) {
34
        var support = false;
35
        document.addEventListener('beforeload', function(e) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
36
            if (e.target === check) {
37
                support = true;
38
            } else if (!e.target.hasAttribute('nomodule') || !support) {
39
                return;
40
            }
41
            e.preventDefault();
42
        }, true);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
43
44
        check.type = 'module';
45
        check.src = '.';
46
        document.head.appendChild(check);
47
        check.remove();
48
    }
49
}());
50