Skip to content
Merged
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
41 changes: 41 additions & 0 deletions src/Dom/dynamicCSS.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import canUseDom from './canUseDom';

const MARK_KEY = `rc-util-key` as any;

interface Options {
attachTo?: Element;
csp?: string;
}

function getContainer(option: Options) {
return option.attachTo || document.body;
}

export function injectCSS(css: string, option: Options = {}) {
if (!canUseDom()) {
return null;
}

const styleNode = document.createElement('style');
styleNode.nonce = option.csp;
styleNode.innerHTML = css;

getContainer(option).appendChild(styleNode);

return styleNode;
}

export function updateCSS(css: string, key: string, option: Options = {}) {
const container = getContainer(option);
const existNode = [...container.children].find(
node => node.tagName === 'STYLE' && node[MARK_KEY] === key,
);

if (existNode) {
existNode.parentElement.removeChild(existNode);
}

const newNode = injectCSS(css, option);
newNode[MARK_KEY] = key;
Copy link
Member

@shaodahong shaodahong Mar 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里设置有问题吧,rc-util-key 是不是太定向了点

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个是为了让 update 能找到原始的 style,这边故意做的很定制。我补一下 test case。

return newNode;
}
70 changes: 70 additions & 0 deletions tests/dynamicCSS.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/* eslint-disable no-eval */
import { injectCSS, updateCSS } from '../src/Dom/dynamicCSS';

const TEST_STYLE = '.bamboo { context: "light" }';

describe('dynamicCSS', () => {
describe('injectCSS', () => {
beforeEach(() => {
expect(document.querySelectorAll('style')).toHaveLength(0);
});

afterEach(() => {
const styles = document.querySelectorAll('style');
styles.forEach(style => {
style.parentNode.removeChild(style);
});
});

it('basic', () => {
const style = injectCSS(TEST_STYLE);
expect(document.body.contains(style));
expect(document.body.querySelector('style').innerHTML).toEqual(
TEST_STYLE,
);
});

it('with CSP', () => {
const style = injectCSS(TEST_STYLE, { csp: 'light' });
expect(document.body.contains(style));
expect(document.body.querySelector('style').innerHTML).toEqual(
TEST_STYLE,
);
expect(document.body.querySelector('style').nonce).toEqual('light');
});
});

describe('updateCSS', () => {
beforeAll(() => {
updateCSS(TEST_STYLE, 'unique');
});

afterAll(() => {
const styles = document.querySelectorAll('style');
styles.forEach(style => {
style.parentNode.removeChild(style);
});
});

it('replace', () => {
const REPLACE_STYLE = '.light { context: "bamboo" }';
updateCSS(REPLACE_STYLE, 'unique');

expect(document.querySelectorAll('style')).toHaveLength(1);
expect(document.body.querySelector('style').innerHTML).toEqual(
REPLACE_STYLE,
);
});

it('replace with CSP', () => {
const REPLACE_STYLE = '.bamboo { context: "little" }';
updateCSS(REPLACE_STYLE, 'unique', { csp: 'only' });

expect(document.querySelectorAll('style')).toHaveLength(1);
expect(document.body.querySelector('style').innerHTML).toEqual(
REPLACE_STYLE,
);
expect(document.body.querySelector('style').nonce).toEqual('only');
});
});
});