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', () => {
7676 expect ( document . body . appendChild )
7777 . toHaveBeenCalledTimes ( 1 ) ;
7878 } ) ;
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+ } ) ;
7991 } ) ;
8092
8193 describe ( 'when script fails to load' , ( ) => {
Original file line number Diff line number Diff line change @@ -10,6 +10,10 @@ export interface PreloadScriptOptions {
1010 prefetch : boolean ;
1111}
1212
13+ export interface ScriptAttributes {
14+ [ key : string ] : string ;
15+ }
16+
1317export default class ScriptLoader {
1418 private _scripts : { [ key : string ] : Promise < void > } = { } ;
1519 private _preloadedScripts : { [ key : string ] : Promise < void > } = { } ;
@@ -22,12 +26,18 @@ export default class ScriptLoader {
2226 private _requestSender : RequestSender
2327 ) { }
2428
25- loadScript ( src : string , options ?: LoadScriptOptions ) : Promise < void > {
29+ loadScript ( src : string , options ?: LoadScriptOptions , scriptAttributes ?: ScriptAttributes ) : Promise < void > {
2630 if ( ! this . _scripts [ src ] ) {
2731 this . _scripts [ src ] = new Promise ( ( resolve , reject ) => {
2832 const script = document . createElement ( 'script' ) as LegacyHTMLScriptElement ;
2933 const { async = false } = options || { } ;
3034
35+ for ( const key in scriptAttributes ) {
36+ if ( scriptAttributes . hasOwnProperty ( key ) ) {
37+ script . setAttribute ( key , scriptAttributes [ key ] ) ;
38+ }
39+ }
40+
3141 script . onload = ( ) => resolve ( ) ;
3242 script . onreadystatechange = ( ) => resolve ( ) ;
3343 script . onerror = event => {
You can’t perform that action at this time.
0 commit comments