@acusti/styling 1.1.1 → 2.0.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,178 +0,0 @@
1
- /**
2
- * Adapted from:
3
- * https://github.com/jbleuzen/node-cssmin/blob/master/cssmin.js
4
- * node-cssmin
5
- * A simple module for Node.js that minify CSS
6
- * Author : Johan Bleuzen
7
- */
8
-
9
- /**
10
- * cssmin.js
11
- * Author: Stoyan Stefanov - http://phpied.com/
12
- * This is a JavaScript port of the CSS minification tool
13
- * distributed with YUICompressor, itself a port
14
- * of the cssmin utility by Isaac Schlueter - http://foohack.com/
15
- * Permission is hereby granted to use the JavaScript version under the same
16
- * conditions as the YUICompressor (original YUICompressor note below).
17
- */
18
-
19
- /*
20
- * YUI Compressor
21
- * http://developer.yahoo.com/yui/compressor/
22
- * Author: Julien Lecomte - http://www.julienlecomte.net/
23
- * Copyright (c) 2011 Yahoo! Inc. All rights reserved.
24
- * The copyrights embodied in the content of this file are licensed
25
- * by Yahoo! Inc. under the BSD (revised) open source license.
26
- */
27
-
28
- export function minifyStyles(css: string) {
29
- const preservedTokens: Array<string> = [];
30
- const comments: Array<string> = [];
31
- const totalLength = css.length;
32
- let endIndex = 0,
33
- placeholder = '',
34
- startIndex = 0,
35
- token = '';
36
-
37
- // collect all comment blocks...
38
- while ((startIndex = css.indexOf('/*', startIndex)) >= 0) {
39
- endIndex = css.indexOf('*/', startIndex + 2);
40
- if (endIndex < 0) {
41
- endIndex = totalLength;
42
- }
43
- token = css.slice(startIndex + 2, endIndex);
44
- comments.push(token);
45
- css =
46
- css.slice(0, startIndex + 2) +
47
- '___PRESERVE_CANDIDATE_COMMENT_' +
48
- (comments.length - 1) +
49
- '___' +
50
- css.slice(endIndex);
51
- startIndex += 2;
52
- }
53
-
54
- // preserve strings so their content doesn't get accidentally minified
55
- css = css.replace(/("([^\\"]|\\.|\\)*")|('([^\\']|\\.|\\)*')/g, function (match) {
56
- const quote = match.substring(0, 1);
57
-
58
- match = match.slice(1, -1);
59
-
60
- // maybe the string contains a comment-like substring?
61
- // one, maybe more? put'em back then
62
- if (match.indexOf('___PRESERVE_CANDIDATE_COMMENT_') >= 0) {
63
- for (let i = 0, max = comments.length; i < max; i++) {
64
- match = match.replace(
65
- '___PRESERVE_CANDIDATE_COMMENT_' + i + '___',
66
- comments[i],
67
- );
68
- }
69
- }
70
-
71
- preservedTokens.push(match);
72
- return (
73
- quote + '___PRESERVED_TOKEN_' + (preservedTokens.length - 1) + '___' + quote
74
- );
75
- });
76
-
77
- // strings are safe, now wrestle the comments
78
- for (let i = 0, max = comments.length; i < max; i = i + 1) {
79
- token = comments[i];
80
- placeholder = '___PRESERVE_CANDIDATE_COMMENT_' + i + '___';
81
-
82
- // ! in the first position of the comment means preserve
83
- // so push to the preserved tokens keeping the !
84
- if (token.charAt(0) === '!') {
85
- preservedTokens.push(token);
86
- css = css.replace(
87
- placeholder,
88
- '___PRESERVED_TOKEN_' + (preservedTokens.length - 1) + '___',
89
- );
90
- continue;
91
- }
92
-
93
- // otherwise, kill the comment
94
- css = css.replace('/*' + placeholder + '*/', '');
95
- }
96
-
97
- // Normalize all whitespace strings to single spaces. Easier to work with that way.
98
- css = css.replace(/\s+/g, ' ');
99
-
100
- // Remove the spaces before the things that should not have spaces before them.
101
- // But, be careful not to turn "p :link {...}" into "p:link{...}"
102
- // Swap out any pseudo-class colons with the token, and then swap back.
103
- css = css.replace(/(^|\})(([^{:])+:)+([^{]*\{)/g, function (m) {
104
- return m.replace(/:/g, '___PSEUDOCLASSCOLON___');
105
- });
106
-
107
- // Preserve spaces in calc expressions
108
- css = css.replace(/calc\s*\(\s*(.*?)\s*\)/g, function (m, c: string) {
109
- return m.replace(c, c.replace(/\s+/g, '___SPACE_IN_CALC___'));
110
- });
111
-
112
- css = css.replace(/\s+([!{};:>+()\],])/g, '$1');
113
- css = css.replace(/___PSEUDOCLASSCOLON___/g, ':');
114
-
115
- // no space after the end of a preserved comment
116
- css = css.replace(/\*\/ /g, '*/');
117
-
118
- // If there is a @charset, then only allow one, and push to the top of the file.
119
- css = css.replace(/^(.*)(@charset "[^"]*";)/gi, '$2$1');
120
- css = css.replace(/^(\s*@charset [^;]+;\s*)+/gi, '$1');
121
-
122
- // Put the space back in some cases, to support stuff like
123
- // @media screen and (-webkit-min-device-pixel-ratio:0){
124
- css = css.replace(/\band\(/gi, 'and (');
125
-
126
- // Remove the spaces after the things that should not have spaces after them.
127
- css = css.replace(/([!{}:;>+([,])\s+/g, '$1');
128
-
129
- // Restore preserved spaces in calc expressions
130
- css = css.replace(/___SPACE_IN_CALC___/g, ' ');
131
-
132
- // remove unnecessary semicolons
133
- css = css.replace(/;+\}/g, '}');
134
-
135
- // Replace 0(px,em,%) with 0.
136
- css = css.replace(/([\s:])(0)(px|em|%|in|cm|mm|pc|pt|ex)/gi, '$1$2');
137
-
138
- // Replace 0 0 0 0; with 0.
139
- css = css.replace(/:0 0 0 0(;|\})/g, ':0$1');
140
- css = css.replace(/:0 0 0(;|\})/g, ':0$1');
141
- css = css.replace(/:0 0(;|\})/g, ':0$1');
142
-
143
- // Replace background-position:0; with background-position:0 0;
144
- // same for transform-origin
145
- css = css.replace(
146
- /(background-position|transform-origin):0(;|\})/gi,
147
- function (_all, prop: string, tail: string) {
148
- return prop.toLowerCase() + ':0 0' + tail;
149
- },
150
- );
151
-
152
- // Replace 0.6 to .6, but only when preceded by : or a white-space
153
- css = css.replace(/(:|\s)0+\.(\d+)/g, '$1.$2');
154
-
155
- // border: none -> border:0
156
- css = css.replace(
157
- /(border|border-top|border-right|border-bottom|border-right|outline|background):none(;|\})/gi,
158
- function (_all, prop: string, tail: string) {
159
- return prop.toLowerCase() + ':0' + tail;
160
- },
161
- );
162
-
163
- // Remove empty rules.
164
- css = css.replace(/[^};{/]+\{\}/g, '');
165
-
166
- // Replace multiple semi-colons in a row by a single one
167
- // See SF bug #1980989
168
- css = css.replace(/;;+/g, ';');
169
-
170
- // restore preserved comments and strings
171
- for (let i = 0, max = preservedTokens.length; i < max; i = i + 1) {
172
- css = css.replace('___PRESERVED_TOKEN_' + i + '___', preservedTokens[i]);
173
- }
174
-
175
- return css.trim();
176
- }
177
-
178
- export default minifyStyles;
@@ -1,111 +0,0 @@
1
- // @vitest-environment happy-dom
2
- import { render } from '@testing-library/react';
3
- import React from 'react';
4
- import { beforeEach, describe, expect, it } from 'vitest';
5
-
6
- import Style from './Style.js';
7
- import { getStyleCache } from './useStyles.js';
8
-
9
- describe('@acusti/styling', () => {
10
- describe('useStyles.ts', () => {
11
- const mockStylesA = '.test-a {\n color: cyan;\n}';
12
- const mockStylesB = '.test-b {\n padding: 10px;\n}';
13
-
14
- // reset styleCache before each test
15
- beforeEach(() => {
16
- getStyleCache().clear();
17
- });
18
-
19
- it('should cache minified styles in the registry keyed by the style string', () => {
20
- const styleCache = getStyleCache();
21
-
22
- const { rerender } = render(
23
- <React.Fragment>
24
- <Style>{mockStylesA}</Style>
25
- <Style>{mockStylesA}</Style>
26
- </React.Fragment>,
27
- );
28
-
29
- let stylesItemA = styleCache.get(mockStylesA);
30
- expect(stylesItemA?.referenceCount).toBe(2);
31
- expect(stylesItemA?.styles).toBe('.test-a{color:cyan}');
32
- expect(styleCache.size).toBe(1);
33
-
34
- rerender(<Style>{mockStylesA}</Style>);
35
- expect(stylesItemA?.referenceCount).toBe(1);
36
- expect(stylesItemA).toBe(styleCache.get(mockStylesA));
37
- expect(styleCache.size).toBe(1);
38
-
39
- rerender(<Style>{mockStylesB}</Style>);
40
- stylesItemA = styleCache.get(mockStylesA);
41
- expect(stylesItemA).toBe(undefined);
42
- let stylesItemB = styleCache.get(mockStylesB);
43
- expect(stylesItemB?.referenceCount).toBe(1);
44
- expect(styleCache.size).toBe(1);
45
-
46
- rerender(
47
- <React.Fragment>
48
- <Style>{mockStylesA}</Style>
49
- <Style>{mockStylesB}</Style>
50
- </React.Fragment>,
51
- );
52
- stylesItemA = styleCache.get(mockStylesA);
53
- expect(stylesItemA?.referenceCount).toBe(1);
54
- expect(stylesItemA).toBe(styleCache.get(mockStylesA));
55
- stylesItemB = styleCache.get(mockStylesB);
56
- expect(stylesItemB?.referenceCount).toBe(1);
57
- expect(styleCache.size).toBe(2);
58
-
59
- rerender(<div />);
60
- expect(styleCache.size).toBe(0);
61
- });
62
-
63
- it('should preserve style cache across component position changes and re-keying', () => {
64
- const styleCache = getStyleCache();
65
-
66
- const { rerender } = render(
67
- <React.Fragment>
68
- <Style>{mockStylesA}</Style>
69
- </React.Fragment>,
70
- );
71
-
72
- const stylesItemA = styleCache.get(mockStylesA);
73
- expect(stylesItemA?.referenceCount).toBe(1);
74
- expect(stylesItemA?.styles).toBe('.test-a{color:cyan}');
75
- expect(styleCache.size).toBe(1);
76
-
77
- rerender(
78
- <React.Fragment>
79
- <Style>{mockStylesB}</Style>
80
- <Style>{mockStylesA}</Style>
81
- </React.Fragment>,
82
- );
83
- expect(stylesItemA?.referenceCount).toBe(1);
84
- expect(stylesItemA).toBe(styleCache.get(mockStylesA));
85
-
86
- rerender(<Style key="new-a">{mockStylesA}</Style>);
87
- expect(stylesItemA?.referenceCount).toBe(1);
88
- expect(stylesItemA).toBe(styleCache.get(mockStylesA));
89
-
90
- rerender(
91
- <React.Fragment>
92
- <Style>{mockStylesA}</Style>
93
- <Style key="new-a">{mockStylesA}</Style>
94
- </React.Fragment>,
95
- );
96
- expect(stylesItemA?.referenceCount).toBe(2);
97
- expect(stylesItemA).toBe(styleCache.get(mockStylesA));
98
-
99
- rerender(<div />);
100
- expect(stylesItemA?.referenceCount).toBe(0);
101
- expect(styleCache.size).toBe(0);
102
- });
103
-
104
- it('should sanitize styles used as href prop if no href prop provided', () => {
105
- render(<Style>{`div[data-foo-bar] { color: cyan; }`}</Style>);
106
- // the two-dash attribute selector results in “Range out of order in character class”
107
- // and render() fails with SyntaxError: Invalid regular expression if not sanitized
108
- expect(true).toBeTruthy();
109
- });
110
- });
111
- });
package/src/useStyles.ts DELETED
@@ -1,76 +0,0 @@
1
- import { useEffect, useState } from 'react';
2
-
3
- import { minifyStyles } from './minifyStyles.js';
4
-
5
- type StyleCache = Map<string, { href: string; referenceCount: number; styles: string }>;
6
-
7
- const styleCache: StyleCache = new Map();
8
-
9
- const EMPTY_STYLES_ITEM = { href: '', referenceCount: 0, styles: '' };
10
-
11
- export function useStyles(styles: string, initialHref?: string) {
12
- const [stylesItem, setStylesItem] = useState(() => {
13
- if (!styles) return EMPTY_STYLES_ITEM;
14
-
15
- const key = initialHref ?? styles;
16
- let item = styleCache.get(key);
17
-
18
- if (item) {
19
- item.referenceCount++;
20
- } else {
21
- const minified = minifyStyles(styles);
22
- item = {
23
- href: sanitizeHref(initialHref ?? minified),
24
- referenceCount: 1,
25
- styles: minified,
26
- };
27
- styleCache.set(key, item);
28
- }
29
-
30
- return item;
31
- });
32
-
33
- useEffect(() => {
34
- if (!styles) return;
35
-
36
- const key = initialHref ?? styles;
37
-
38
- if (!styleCache.get(key)) {
39
- const minified = minifyStyles(styles);
40
- const item = {
41
- href: sanitizeHref(initialHref ?? minified),
42
- referenceCount: 1,
43
- styles: minified,
44
- };
45
- styleCache.set(key, item);
46
- setStylesItem(item);
47
- }
48
-
49
- return () => {
50
- const existingItem = styleCache.get(styles);
51
- if (existingItem) {
52
- existingItem.referenceCount--;
53
- if (!existingItem.referenceCount) {
54
- // TODO try scheduling this via setTimeout
55
- // and add another referenceCount check
56
- // to deal with instance where existing <Style>
57
- // component is moved in the tree or re-keyed
58
- styleCache.delete(styles);
59
- }
60
- }
61
- };
62
- }, [initialHref, styles]);
63
-
64
- return stylesItem;
65
- }
66
-
67
- export default useStyles;
68
-
69
- // Dashes in selectors in href prop create happy-dom / jsdom test errors:
70
- // Invalid regular expression (“Range out of order in character class”)
71
- function sanitizeHref(text: string) {
72
- return text.replace(/-/g, '');
73
- }
74
-
75
- // The following export is for test usage only
76
- export const getStyleCache = () => styleCache;