File tree Expand file tree Collapse file tree 2 files changed +23
-1
lines changed Expand file tree Collapse file tree 2 files changed +23
-1
lines changed Original file line number Diff line number Diff line change @@ -76,6 +76,18 @@ describe('ScriptLoader', () => {
76
76
expect ( document . body . appendChild )
77
77
. toHaveBeenCalledTimes ( 1 ) ;
78
78
} ) ;
79
+
80
+ it ( 'attaches script tag to document with data attributes' , async ( ) => {
81
+ await loader . loadScript (
82
+ 'https://code.jquery.com/jquery-3.2.1.min.js' ,
83
+ { async : true , attributes : { 'data-attribute1' : '1' , 'data-attribute2' : '2' } } ) ;
84
+
85
+ expect ( script . attributes . getNamedItem ( 'data-attribute1' ) ! . value )
86
+ . toEqual ( '1' ) ;
87
+
88
+ expect ( script . attributes . getNamedItem ( 'data-attribute2' ) ! . value )
89
+ . toEqual ( '2' ) ;
90
+ } ) ;
79
91
} ) ;
80
92
81
93
describe ( 'when script fails to load' , ( ) => {
Original file line number Diff line number Diff line change @@ -4,12 +4,17 @@ import BrowserSupport from './browser-support';
4
4
5
5
export interface LoadScriptOptions {
6
6
async : boolean ;
7
+ attributes : ScriptAttributes ;
7
8
}
8
9
9
10
export interface PreloadScriptOptions {
10
11
prefetch : boolean ;
11
12
}
12
13
14
+ export interface ScriptAttributes {
15
+ [ key : string ] : string ;
16
+ }
17
+
13
18
export default class ScriptLoader {
14
19
private _scripts : { [ key : string ] : Promise < void > } = { } ;
15
20
private _preloadedScripts : { [ key : string ] : Promise < void > } = { } ;
@@ -26,7 +31,12 @@ export default class ScriptLoader {
26
31
if ( ! this . _scripts [ src ] ) {
27
32
this . _scripts [ src ] = new Promise ( ( resolve , reject ) => {
28
33
const script = document . createElement ( 'script' ) as LegacyHTMLScriptElement ;
29
- const { async = false } = options || { } ;
34
+ const { async = false , attributes = { } } = options || { } ;
35
+
36
+ Object . keys ( attributes )
37
+ . forEach ( key => {
38
+ script . setAttribute ( key , attributes [ key ] ) ;
39
+ } ) ;
30
40
31
41
script . onload = ( ) => resolve ( ) ;
32
42
script . onreadystatechange = ( ) => resolve ( ) ;
You can’t perform that action at this time.
0 commit comments