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+ { 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+ } ) ;
7991 } ) ;
8092
8193 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';
44
55export interface LoadScriptOptions {
66 async : boolean ;
7+ attributes : ScriptAttributes ;
78}
89
910export interface PreloadScriptOptions {
1011 prefetch : boolean ;
1112}
1213
14+ export interface ScriptAttributes {
15+ [ key : string ] : string ;
16+ }
17+
1318export default class ScriptLoader {
1419 private _scripts : { [ key : string ] : Promise < void > } = { } ;
1520 private _preloadedScripts : { [ key : string ] : Promise < void > } = { } ;
@@ -26,7 +31,12 @@ export default class ScriptLoader {
2631 if ( ! this . _scripts [ src ] ) {
2732 this . _scripts [ src ] = new Promise ( ( resolve , reject ) => {
2833 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+ } ) ;
3040
3141 script . onload = ( ) => resolve ( ) ;
3242 script . onreadystatechange = ( ) => resolve ( ) ;
You can’t perform that action at this time.
0 commit comments