@acusti/styling 0.7.1 → 1.0.0-rc.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,129 +0,0 @@
1
- // @vitest-environment happy-dom
2
- import { beforeEach, describe, expect, it } from 'vitest';
3
-
4
- import {
5
- clearRegistry,
6
- getRegisteredStyles,
7
- getStyleRegistryKeys,
8
- registerStyles,
9
- unregisterStyles,
10
- updateStyles,
11
- } from './style-registry.js';
12
-
13
- describe('@acusti/styling', () => {
14
- describe('style-registry.ts', () => {
15
- const mockStyles = '.test { color: red; }';
16
-
17
- // reset styleRegistry before each test
18
- beforeEach(clearRegistry);
19
-
20
- describe('registerStyles', () => {
21
- it('should add styles to the registry keyed by the style string', () => {
22
- const payload = { ownerDocument: document, styles: mockStyles };
23
- registerStyles(payload);
24
- const styleRegistryKeys = getStyleRegistryKeys();
25
- const keysArray = [...styleRegistryKeys];
26
- expect(keysArray.length).toBe(1);
27
- expect(keysArray[0]).toBe(mockStyles);
28
- const result = getRegisteredStyles(payload);
29
- expect(result!.element).toBeDefined();
30
- expect(result!.referenceCount).toBe(1);
31
- });
32
-
33
- it('should allow registering styles without a DOM via ownerDocument: "global"', () => {
34
- const payload = Object.freeze({
35
- ownerDocument: 'global',
36
- styles: mockStyles,
37
- });
38
- registerStyles(payload);
39
- const registryKeys = [...getStyleRegistryKeys()];
40
- expect(registryKeys.length).toBe(1);
41
- expect(registryKeys[0]).toBe(mockStyles);
42
- const result = getRegisteredStyles(payload);
43
- expect(result!.element).toBeNull();
44
- expect(result!.referenceCount).toBe(1);
45
- });
46
- });
47
-
48
- describe('getRegisteredStyles', () => {
49
- it('should retrieve registered styles', () => {
50
- const payload = { ownerDocument: document, styles: mockStyles };
51
- registerStyles(payload);
52
- const result = getRegisteredStyles(payload);
53
- expect(result!.element!.tagName).toBe('STYLE');
54
- expect(result!.referenceCount).toBe(1);
55
- });
56
- });
57
-
58
- describe('unregisterStyles', () => {
59
- it('should remove styles from the registry if no other references to same styles exist', () => {
60
- const payload = { ownerDocument: document, styles: mockStyles };
61
- const otherPayload = Object.freeze({
62
- ownerDocument: 'global',
63
- styles: mockStyles,
64
- });
65
- registerStyles(payload);
66
- registerStyles(otherPayload);
67
- expect(getStyleRegistryKeys().next().value).toBe(mockStyles);
68
- expect(getRegisteredStyles(payload)!.referenceCount).toBe(1);
69
- expect(getRegisteredStyles(otherPayload)!.referenceCount).toBe(1);
70
- unregisterStyles(payload);
71
- // style registry for this style should still exist
72
- expect(getStyleRegistryKeys().next().value).toBe(mockStyles);
73
- // but this document’s styles item should be cleared
74
- expect(getRegisteredStyles(payload)).toBeNull();
75
- unregisterStyles(otherPayload);
76
- // now the style registry should be empty
77
- expect([...getStyleRegistryKeys()].length).toBe(0);
78
- });
79
- });
80
-
81
- describe('updateStyles', () => {
82
- it('should update styles in the registry, reusing the existing DOM element if no other reference', () => {
83
- const previousStyles = '.previous { color: blue; }';
84
- const payload = {
85
- ownerDocument: document,
86
- styles: previousStyles,
87
- };
88
- registerStyles(payload);
89
- const previousStylesItem = getRegisteredStyles(payload)!;
90
- expect(previousStylesItem.element!.innerText).toBe(previousStyles);
91
- updateStyles({
92
- ownerDocument: document,
93
- previousStyles,
94
- styles: mockStyles,
95
- });
96
- const stylesItem = getRegisteredStyles({
97
- ownerDocument: document,
98
- styles: mockStyles,
99
- })!;
100
- expect(previousStylesItem.element).toBe(stylesItem.element);
101
- expect(stylesItem.element!.innerText).toBe(mockStyles);
102
- expect(getRegisteredStyles(payload)).toBeNull();
103
- });
104
-
105
- it('should update styles in the registry, creating a new DOM element if there are other references', () => {
106
- const previousStyles = '.previous { color: blue; }';
107
- const payload = { ownerDocument: document, styles: previousStyles };
108
- registerStyles(payload);
109
- // create multiple references to the same styles and document
110
- registerStyles(payload);
111
- const previousStylesItem = getRegisteredStyles(payload)!;
112
- expect(previousStylesItem.referenceCount).toBe(2);
113
- expect(previousStylesItem.element!.innerText).toBe(previousStyles);
114
- updateStyles({
115
- ownerDocument: document,
116
- previousStyles,
117
- styles: mockStyles,
118
- });
119
- const stylesItem = getRegisteredStyles({
120
- ownerDocument: document,
121
- styles: mockStyles,
122
- })!;
123
- expect(previousStylesItem.element).not.toBe(stylesItem.element);
124
- expect(stylesItem.element!.innerText).toBe(mockStyles);
125
- expect(getRegisteredStyles(payload)!.referenceCount).toBe(1);
126
- });
127
- });
128
- });
129
- });
@@ -1,115 +0,0 @@
1
- type StyleRegistry = Map<
2
- string,
3
- Map<Document | 'global', { element: HTMLStyleElement | null; referenceCount: number }>
4
- >;
5
-
6
- const styleRegistry: StyleRegistry = new Map();
7
-
8
- type Payload = { ownerDocument: Document | 'global'; styles: string };
9
-
10
- export const getRegisteredStyles = ({ ownerDocument, styles }: Payload) => {
11
- if (!styles) return null;
12
- const stylesMap = styleRegistry.get(styles);
13
- if (!stylesMap) return null;
14
- return stylesMap.get(ownerDocument) ?? null;
15
- };
16
-
17
- // NOTE a more idiomatic API than (register|unregister)Styles would be
18
- // to make registerStyles a thunk that returns a cleanup function
19
- export const registerStyles = ({ ownerDocument, styles }: Payload) => {
20
- if (!styles) return;
21
-
22
- const existingStylesItem = getRegisteredStyles({ ownerDocument, styles });
23
- if (existingStylesItem) {
24
- existingStylesItem.referenceCount++;
25
- return;
26
- }
27
-
28
- if (ownerDocument === 'global') {
29
- const stylesItem = { element: null, referenceCount: 1 };
30
- let stylesMap = styleRegistry.get(styles);
31
- if (stylesMap) {
32
- stylesMap.set(ownerDocument, stylesItem);
33
- } else {
34
- stylesMap = new Map([[ownerDocument, stylesItem]]);
35
- }
36
- styleRegistry.set(styles, stylesMap);
37
- return;
38
- }
39
-
40
- const element = ownerDocument.createElement('style');
41
- element.setAttribute('data-ukt-styling', '');
42
- element.innerHTML = styles;
43
- ownerDocument.head.appendChild(element);
44
- const stylesItem = { element, referenceCount: 1 };
45
-
46
- const stylesMap = styleRegistry.get(styles);
47
- if (stylesMap) {
48
- stylesMap.set(ownerDocument, stylesItem);
49
- return;
50
- }
51
-
52
- styleRegistry.set(styles, new Map([[ownerDocument, stylesItem]]));
53
- };
54
-
55
- export const unregisterStyles = ({ ownerDocument, styles }: Payload) => {
56
- if (!styles) return;
57
-
58
- const stylesItem = getRegisteredStyles({ ownerDocument, styles });
59
- if (!stylesItem) return;
60
-
61
- stylesItem.referenceCount--;
62
- if (stylesItem.referenceCount) return;
63
-
64
- // If no more references to these styles in this document, remove <style> element from the DOM
65
- if (stylesItem.element) {
66
- const { parentElement } = stylesItem.element;
67
- if (parentElement) {
68
- parentElement.removeChild(stylesItem.element);
69
- }
70
- }
71
-
72
- // Then remove the document Map
73
- const stylesMap = styleRegistry.get(styles)!;
74
- stylesMap.delete(ownerDocument);
75
-
76
- if (stylesMap.size) return;
77
- // If no more references to these styles in any document, remove it entirely
78
- styleRegistry.delete(styles);
79
- };
80
-
81
- type UpdatePayload = { ownerDocument: Document; previousStyles: string; styles: string };
82
-
83
- export const updateStyles = ({
84
- ownerDocument,
85
- previousStyles,
86
- styles,
87
- }: UpdatePayload) => {
88
- if (previousStyles === styles) return;
89
-
90
- const stylesMap = styleRegistry.get(previousStyles);
91
- const stylesItem = stylesMap?.get(ownerDocument);
92
- if (stylesMap && stylesItem?.element && stylesItem?.referenceCount === 1) {
93
- // Mutate existing <style> element with updated styles
94
- stylesItem.element.innerHTML = styles;
95
- styleRegistry.set(styles, new Map([[ownerDocument, stylesItem]]));
96
- // Cleanup previous stylesMap
97
- stylesMap.delete(ownerDocument);
98
- if (!stylesMap.size) {
99
- styleRegistry.delete(previousStyles);
100
- }
101
- return;
102
- }
103
-
104
- if (previousStyles) {
105
- unregisterStyles({ ownerDocument, styles: previousStyles });
106
- }
107
-
108
- registerStyles({ ownerDocument, styles });
109
- };
110
-
111
- export const getStyleRegistryKeys = () => styleRegistry.keys();
112
-
113
- export const clearRegistry = () => {
114
- styleRegistry.clear();
115
- };