Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(fix) Replace newlines with empty space #448

Merged
merged 5 commits into from
Apr 27, 2022
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
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