From fe07c6e36833f365a58957b73b8187e77d5acce2 Mon Sep 17 00:00:00 2001 From: Andrii Fetisov Date: Wed, 2 Oct 2019 18:26:41 +0300 Subject: [PATCH] feat(common): PAYPAL-7 Pass in merchant ID on PayPal button script for PayPal Express Checkout --- src/script-loader.spec.ts | 12 ++++++++++++ src/script-loader.ts | 12 +++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/script-loader.spec.ts b/src/script-loader.spec.ts index f0bcb22..8375892 100644 --- a/src/script-loader.spec.ts +++ b/src/script-loader.spec.ts @@ -76,6 +76,18 @@ describe('ScriptLoader', () => { expect(document.body.appendChild) .toHaveBeenCalledTimes(1); }); + + it('attaches script tag to document with data attributes', async () => { + await loader.loadScript( + 'https://code.jquery.com/jquery-3.2.1.min.js', + {'data-attribute1': '1', 'data-attribute2': '2'}); + + expect(script.attributes.getNamedItem('data-attribute1')!.value) + .toEqual('1'); + + expect(script.attributes.getNamedItem('data-attribute2')!.value) + .toEqual('2'); + }); }); describe('when script fails to load', () => { diff --git a/src/script-loader.ts b/src/script-loader.ts index f5f2635..eabd6d6 100644 --- a/src/script-loader.ts +++ b/src/script-loader.ts @@ -10,6 +10,10 @@ export interface PreloadScriptOptions { prefetch: boolean; } +export interface ScriptAttributes { + [key: string]: string; +} + export default class ScriptLoader { private _scripts: { [key: string]: Promise } = {}; private _preloadedScripts: { [key: string]: Promise } = {}; @@ -22,12 +26,18 @@ export default class ScriptLoader { private _requestSender: RequestSender ) {} - loadScript(src: string, options?: LoadScriptOptions): Promise { + loadScript(src: string, options?: LoadScriptOptions, scriptAttributes?: ScriptAttributes): Promise { if (!this._scripts[src]) { this._scripts[src] = new Promise((resolve, reject) => { const script = document.createElement('script') as LegacyHTMLScriptElement; const { async = false } = options || {}; + for (const key in scriptAttributes) { + if (scriptAttributes.hasOwnProperty(key)) { + script.setAttribute(key, scriptAttributes[key]); + } + } + script.onload = () => resolve(); script.onreadystatechange = () => resolve(); script.onerror = event => {