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 @@ -50,6 +50,18 @@ describe('ScriptLoader', () => {
50
50
expect ( document . body . appendChild )
51
51
. toHaveBeenCalledTimes ( 1 ) ;
52
52
} ) ;
53
+
54
+ it ( 'attaches script tag to document with data attributes' , async ( ) => {
55
+ await loader . loadScript (
56
+ 'https://code.jquery.com/jquery-3.2.1.min.js' ,
57
+ { 'data-attribute1' : '1' , 'data-attribute2' : '2' } ) ;
58
+
59
+ expect ( script . attributes . getNamedItem ( 'data-attribute1' ) ! . value )
60
+ . toEqual ( '1' ) ;
61
+
62
+ expect ( script . attributes . getNamedItem ( 'data-attribute2' ) ! . value )
63
+ . toEqual ( '2' ) ;
64
+ } ) ;
53
65
} ) ;
54
66
55
67
describe ( 'when script fails to load' , ( ) => {
Original file line number Diff line number Diff line change @@ -2,15 +2,25 @@ export interface PreloadScriptOptions {
2
2
prefetch : boolean ;
3
3
}
4
4
5
+ export interface ScriptAttributes {
6
+ [ key : string ] : string ;
7
+ }
8
+
5
9
export default class ScriptLoader {
6
10
private _scripts : { [ key : string ] : Promise < Event > } = { } ;
7
11
private _preloadedScripts : { [ key : string ] : Promise < Event > } = { } ;
8
12
9
- loadScript ( src : string ) : Promise < Event > {
13
+ loadScript ( src : string , scriptAttributes ?: ScriptAttributes ) : Promise < Event > {
10
14
if ( ! this . _scripts [ src ] ) {
11
15
this . _scripts [ src ] = new Promise ( ( resolve , reject ) => {
12
16
const script = document . createElement ( 'script' ) as LegacyHTMLScriptElement ;
13
17
18
+ for ( const key in scriptAttributes ) {
19
+ if ( scriptAttributes . hasOwnProperty ( key ) ) {
20
+ script . setAttribute ( key , scriptAttributes [ key ] ) ;
21
+ }
22
+ }
23
+
14
24
script . onload = event => resolve ( event ) ;
15
25
script . onreadystatechange = event => resolve ( event ) ;
16
26
script . onerror = event => {
You can’t perform that action at this time.
0 commit comments