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
+ { '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 @@ -10,6 +10,10 @@ export interface PreloadScriptOptions {
10
10
prefetch : boolean ;
11
11
}
12
12
13
+ export interface ScriptAttributes {
14
+ [ key : string ] : string ;
15
+ }
16
+
13
17
export default class ScriptLoader {
14
18
private _scripts : { [ key : string ] : Promise < void > } = { } ;
15
19
private _preloadedScripts : { [ key : string ] : Promise < void > } = { } ;
@@ -22,12 +26,18 @@ export default class ScriptLoader {
22
26
private _requestSender : RequestSender
23
27
) { }
24
28
25
- loadScript ( src : string , options ?: LoadScriptOptions ) : Promise < void > {
29
+ loadScript ( src : string , options ?: LoadScriptOptions , scriptAttributes ?: ScriptAttributes ) : Promise < void > {
26
30
if ( ! this . _scripts [ src ] ) {
27
31
this . _scripts [ src ] = new Promise ( ( resolve , reject ) => {
28
32
const script = document . createElement ( 'script' ) as LegacyHTMLScriptElement ;
29
33
const { async = false } = options || { } ;
30
34
35
+ for ( const key in scriptAttributes ) {
36
+ if ( scriptAttributes . hasOwnProperty ( key ) ) {
37
+ script . setAttribute ( key , scriptAttributes [ key ] ) ;
38
+ }
39
+ }
40
+
31
41
script . onload = ( ) => resolve ( ) ;
32
42
script . onreadystatechange = ( ) => resolve ( ) ;
33
43
script . onerror = event => {
You can’t perform that action at this time.
0 commit comments