Skip to content

Commit 8d2cf23

Browse files
committed
feat: warn if trying to add malformed CSS
1 parent 5b5a652 commit 8d2cf23

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

src/base/plugin/addStyle.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ wrap(() => {
1414
}
1515

1616
function mod(plugin) {
17-
return (...styles) => getStyle(plugin.name).add(...styles);
17+
return (...styles) => getStyle(plugin).add(...styles);
1818
}
1919

2020
registerModule(name, mod);

src/utils/charCount.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default function charCount(string = '', char = '') {
2+
const regex = new RegExp(char, 'g');
3+
const matches = string.match(regex);
4+
return matches?.length ?? 0;
5+
}

src/utils/style.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import charCount from './charCount.js';
12
import eventManager from './eventManager.js';
23
import last from './last.js';
34

@@ -7,7 +8,7 @@ export function newStyle(plugin = false) {
78
const el = document.createElement('style');
89
function appendStyle() {
910
if (el.parentElement) return;
10-
if (plugin) el.dataset.underscriptPlugin = plugin;
11+
if (plugin) el.dataset.underscriptPlugin = plugin.name;
1112
else el.dataset.underscript = '';
1213
document.head.append(el);
1314
}
@@ -26,6 +27,11 @@ export function newStyle(plugin = false) {
2627

2728
function append(styles = [], nodes = []) {
2829
styles.flat().forEach((s) => {
30+
if (charCount(s, '{') !== charCount(s, '}')) {
31+
const logger = plugin?.logger ?? console;
32+
logger.error('Malformed CSS (missing { or }):\n', s);
33+
return;
34+
}
2935
el.append(s);
3036
nodes.push(last(el.childNodes));
3137
});

0 commit comments

Comments
 (0)