Skip to content

Commit

Permalink
feat(core): CHECKOUT-4909 Pass in attributes to stylesheet
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Parra committed May 22, 2020
1 parent 8db06ec commit a9d5318
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/stylesheet-loader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@ describe('StylesheetLoader', () => {
expect(document.head.appendChild)
.toHaveBeenCalledTimes(1);
});

it('attaches script tag to document with data attributes', async () => {
await loader.loadStylesheet(
'https://code.jquery.com/jquery-3.2.1.min.js',
{prepend: true, attributes: {'data-attribute1': '1', 'data-attribute2': '2'}});

expect(stylesheet.attributes.getNamedItem('data-attribute1')!.value)
.toEqual('1');

expect(stylesheet.attributes.getNamedItem('data-attribute2')!.value)
.toEqual('2');
});
});

describe('when stylesheet fails to load', () => {
Expand Down
12 changes: 11 additions & 1 deletion src/stylesheet-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ import BrowserSupport from './browser-support';

export interface LoadStylesheetOptions {
prepend: boolean;
attributes: StylesheetAttributes;
}

export interface PreloadStylesheetOptions {
prefetch: boolean;
}

export interface StylesheetAttributes {
[key: string]: string;
}

export default class StylesheetLoader {
private _stylesheets: { [key: string]: Promise<void> } = {};
private _preloadedStylesheets: { [key: string]: Promise<void> } = {};
Expand All @@ -26,7 +31,12 @@ export default class StylesheetLoader {
if (!this._stylesheets[src]) {
this._stylesheets[src] = new Promise((resolve, reject) => {
const stylesheet = document.createElement('link');
const { prepend = false } = options || {};
const { prepend = false, attributes = {} } = options || {};

Object.keys(attributes)
.forEach(key => {
stylesheet.setAttribute(key, attributes[key]);
});

stylesheet.onload = () => resolve();
stylesheet.onerror = event => {
Expand Down

0 comments on commit a9d5318

Please sign in to comment.