Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to suffix data-aphrodite to prevent collisions between multiple Aphrodite instances #356

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
addRenderedClassNames,
getRenderedClassNames,
getBufferedStyles,
setStyleTagSuffix,
} from './inject';
import {defaultSelectorHandlers} from './generate';

Expand Down Expand Up @@ -188,5 +189,6 @@ export default function makeExports(
flushToStyleTag,
injectAndGetClassName,
defaultSelectorHandlers,
setStyleTagSuffix,
};
}
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const {
flushToStyleTag,
injectAndGetClassName,
defaultSelectorHandlers,
setStyleTagSuffix,
} = Aphrodite;

export {
Expand All @@ -24,4 +25,5 @@ export {
flushToStyleTag,
injectAndGetClassName,
defaultSelectorHandlers,
setStyleTagSuffix,
};
11 changes: 9 additions & 2 deletions src/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ import type { SelectorHandler } from './generate.js';
// `document.querySelector("style[data-aphrodite"])`, but holding onto it is
// faster.
let styleTag /* : ?HTMLStyleElement */ = null;
let styleTagSuffix = '';
const styleTagAttribute = () => `data-aphrodite${styleTagSuffix ? '-' + styleTagSuffix : ''}`;

export const setStyleTagSuffix = suffix => {
styleTagSuffix = suffix;
};

// Inject a set of rules into a <style> tag in the head of the document. This
// will automatically create a style tag and then continue to use it for
Expand All @@ -25,7 +31,7 @@ let styleTag /* : ?HTMLStyleElement */ = null;
const injectStyleTag = (cssRules /* : string[] */) => {
if (styleTag == null) {
// Try to find a style tag with the `data-aphrodite` attribute first.
styleTag = ((document.querySelector("style[data-aphrodite]") /* : any */) /* : ?HTMLStyleElement */);
styleTag = ((document.querySelector(`style[${styleTagAttribute()}]`) /* : any */) /* : ?HTMLStyleElement */);

// If that doesn't work, generate a new style tag.
if (styleTag == null) {
Expand All @@ -35,7 +41,7 @@ const injectStyleTag = (cssRules /* : string[] */) => {
styleTag = document.createElement('style');

styleTag.type = 'text/css';
styleTag.setAttribute("data-aphrodite", "");
styleTag.setAttribute(`${styleTagAttribute()}`, "");
head.appendChild(styleTag);
}
}
Expand Down Expand Up @@ -201,6 +207,7 @@ export const reset = () => {
alreadyInjected = {};
isBuffering = false;
styleTag = null;
styleTagSuffix = '';
};

export const getBufferedStyles = () => {
Expand Down
2 changes: 2 additions & 0 deletions src/no-important.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const {
flushToStyleTag,
injectAndGetClassName,
defaultSelectorHandlers,
setStyleTagSuffix,
} = Aphrodite;

export {
Expand All @@ -28,4 +29,5 @@ export {
flushToStyleTag,
injectAndGetClassName,
defaultSelectorHandlers,
setStyleTagSuffix,
};
33 changes: 32 additions & 1 deletion tests/index_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
minify,
css
} from '../src/index.js';
import { reset } from '../src/inject.js';
import { reset, setStyleTagSuffix } from '../src/inject.js';
import { getSheetText } from './testUtils.js';

describe('css', () => {
Expand Down Expand Up @@ -143,6 +143,37 @@ describe('css', () => {
done();
});
});

it('automatically uses a style tag with the data-aphrodite-<suffix> attribute if suffix is set', done => {
const suffix = 'testing';
const style = document.createElement("style");
style.setAttribute(`data-aphrodite-${suffix}`, "");
document.head.appendChild(style);

setStyleTagSuffix(suffix);

const sheet = StyleSheet.create({
red: {
color: 'red',
},
blue: {
color: 'blue',
},
});

css(sheet.red);

asap(() => {
const styleTags = global.document.getElementsByTagName("style");
assert.equal(styleTags.length, 1);
const styles = getSheetText(styleTags[0].sheet);

assert.include(styles, `${sheet.red._name} {`);
assert.include(styles, 'color: red');

done();
});
});
});

describe('StyleSheet.create', () => {
Expand Down