Skip to content

Commit 47ee685

Browse files
author
Luis Sanchez
authored
Merge pull request #11 from vmparra/CHECKOUT-4909
feat(core): CHECKOUT-4909 Pass in attributes to stylesheet
2 parents 8db06ec + 8bfc8a4 commit 47ee685

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/stylesheet-loader.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,18 @@ describe('StylesheetLoader', () => {
6767
expect(document.head.appendChild)
6868
.toHaveBeenCalledTimes(1);
6969
});
70+
71+
it('attaches stylesheet tag to document with data attributes', async () => {
72+
await loader.loadStylesheet(
73+
'https://foo.bar/hello-world.css',
74+
{prepend: true, attributes: {'data-attribute1': '1', 'data-attribute2': '2'}});
75+
76+
expect(stylesheet.attributes.getNamedItem('data-attribute1')!.value)
77+
.toEqual('1');
78+
79+
expect(stylesheet.attributes.getNamedItem('data-attribute2')!.value)
80+
.toEqual('2');
81+
});
7082
});
7183

7284
describe('when stylesheet fails to load', () => {

src/stylesheet-loader.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,17 @@ import BrowserSupport from './browser-support';
44

55
export interface LoadStylesheetOptions {
66
prepend: boolean;
7+
attributes?: StylesheetAttributes;
78
}
89

910
export interface PreloadStylesheetOptions {
1011
prefetch: boolean;
1112
}
1213

14+
export interface StylesheetAttributes {
15+
[key: string]: string;
16+
}
17+
1318
export default class StylesheetLoader {
1419
private _stylesheets: { [key: string]: Promise<void> } = {};
1520
private _preloadedStylesheets: { [key: string]: Promise<void> } = {};
@@ -26,7 +31,12 @@ export default class StylesheetLoader {
2631
if (!this._stylesheets[src]) {
2732
this._stylesheets[src] = new Promise((resolve, reject) => {
2833
const stylesheet = document.createElement('link');
29-
const { prepend = false } = options || {};
34+
const { prepend = false, attributes = {} } = options || {};
35+
36+
Object.keys(attributes)
37+
.forEach(key => {
38+
stylesheet.setAttribute(key, attributes[key]);
39+
});
3040

3141
stylesheet.onload = () => resolve();
3242
stylesheet.onerror = event => {

0 commit comments

Comments
 (0)