Skip to content

Commit

Permalink
(fix) Replace newlines with empty space (#448)
Browse files Browse the repository at this point in the history
* (fix) Replace newlines with empty space

* Tests fix

* Smaller

* Hoist to vars
  • Loading branch information
cristianbote authored Apr 27, 2022
1 parent 0e689b7 commit 08d9ac7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
21 changes: 18 additions & 3 deletions src/core/__tests__/astish.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('astish', () => {
).toEqual({
prop: 'value',
opacity: '0',
'.class,&:hover': {
'.class, &:hover': {
'-webkit-touch': 'none'
},
'@keyframes foo': {
Expand Down Expand Up @@ -181,7 +181,7 @@ describe('astish', () => {
center/contain;
`)
).toEqual({
background: `url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" fill="white"><path d="M7.5 36.7h58.4v10.6H7.5V36.7zm0-15.9h58.4v10.6H7.5V20.8zm0 31.9h58.4v10.6H7.5V52.7zm0 15.9h58.4v10.6H7.5V68.6zm63.8-15.9l10.6 15.9 10.6-15.9H71.3zm21.2-5.4L81.9 31.4 71.3 47.3h21.2z"/></svg>')center/contain`
background: `url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" fill="white"><path d="M7.5 36.7h58.4v10.6H7.5V36.7zm0-15.9h58.4v10.6H7.5V20.8zm0 31.9h58.4v10.6H7.5V52.7zm0 15.9h58.4v10.6H7.5V68.6zm63.8-15.9l10.6 15.9 10.6-15.9H71.3zm21.2-5.4L81.9 31.4 71.3 47.3h21.2z"/></svg>') center/contain`
});
});

Expand All @@ -195,10 +195,25 @@ describe('astish', () => {
'font-size': '1rem'
},
'@media only screen and (min-width: 850px)': {
' h1': {
h1: {
'font-size': '2rem'
}
}
});
});

it('should handle newlines as part of the rule value', () => {
expect(
astish(
`tag {
font-size: first
second;
}`
)
).toEqual({
tag: {
'font-size': 'first second'
}
});
});
});
11 changes: 7 additions & 4 deletions src/core/astish.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
let newRule = /(?:([\u0080-\uFFFF\w-%@]+) *:? *([^{;]+?);|([^;}{]*?) *{)|(}\s*)/g;
let ruleClean = /\/\*[^]*?\*\/|\s\s+|\n/g;
let ruleClean = /\/\*[^]*?\*\/| +/g;
let ruleNewline = /\n+/g;
let empty = ' ';

/**
* Convert a css style string into a object
Expand All @@ -8,16 +10,17 @@ let ruleClean = /\/\*[^]*?\*\/|\s\s+|\n/g;
*/
export let astish = (val) => {
let tree = [{}];
let block;
let block, left;

while ((block = newRule.exec(val.replace(ruleClean, '')))) {
// Remove the current entry
if (block[4]) {
tree.shift();
} else if (block[3]) {
tree.unshift((tree[0][block[3]] = tree[0][block[3]] || {}));
left = block[3].replace(ruleNewline, empty).trim();
tree.unshift((tree[0][left] = tree[0][left] || {}));
} else {
tree[0][block[1]] = block[2];
tree[0][block[1]] = block[2].replace(ruleNewline, empty).trim();
}
}

Expand Down

1 comment on commit 08d9ac7

@vercel
Copy link

@vercel vercel bot commented on 08d9ac7 Apr 27, 2022

Choose a reason for hiding this comment

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

Please sign in to comment.