-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Allow random expressions as long as they result in a className that begins with css #213
Merged
Merged
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6619142
👨🍳
41405d7
working on media query support
a638718
Random expressions working
abf6ed3
Merge branches 'master' and 'random-expressions' of https://github.co…
6cf18eb
Merge branch 'master' into random-expressions
578c0e9
Add more tests for styled.
7fab8fa
Merge remote-tracking branch 'origin/random-expressions' into random-…
5c40a1f
get example running again
39d921b
Bug fix: handle random expressions in selectors
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
// @flow | ||
import parse from 'postcss-safe-parser' | ||
import SafeParser from 'postcss-safe-parser/lib/safe-parser' | ||
import postcssNested from 'postcss-nested' | ||
import postcssJs from 'postcss-js' | ||
import objParse from 'postcss-js/parser' | ||
|
@@ -35,7 +35,7 @@ export function parseCSS ( | |
if (typeof css === 'object') { | ||
root = objParse(css, { from: filename }) | ||
} else { | ||
root = parse(css, { from: filename }) | ||
root = safeParse(css, { from: filename }) | ||
} | ||
let vars = 0 | ||
let composes: number = 0 | ||
|
@@ -56,7 +56,7 @@ export function parseCSS ( | |
} | ||
}) | ||
|
||
const styles = expandCSSFallbacks(prefixer(postcssJs.objectify(root))) | ||
const styles = expandCSSFallbacks(prefixer(objectifyPostcssRules(root))) | ||
|
||
return { | ||
styles, | ||
|
@@ -89,3 +89,88 @@ export function expandCSSFallbacks (style: { [string]: any }) { | |
// flatten arrays which haven't been flattened yet | ||
return flattened | ||
} | ||
|
||
var camelcase = require('camelcase-css') | ||
|
||
function atRule (node) { | ||
if (typeof node.nodes === 'undefined') { | ||
return true | ||
} else { | ||
return objectifyPostcssRules(node) | ||
} | ||
} | ||
|
||
// postcss -> js obj | ||
function objectifyPostcssRules (node) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function needs to be removed. |
||
const result = [{}] | ||
let name | ||
let cursor = 0 | ||
node.each(function (child) { | ||
var rules = {} | ||
// console.log(JSON.stringify(child, null, 2)) | ||
node.each(function (rule) { | ||
if (rule.type !== 'rule') { | ||
} else if (rules[rule.selector]) { | ||
// console.log(rules[rule.selector]) | ||
if (rules[rule.selector].append) { | ||
rules[rule.selector].append(rule.nodes) | ||
rule.remove() | ||
} | ||
} else { | ||
rules[rule.selector] = rule | ||
} | ||
}) | ||
|
||
if (child.type === 'atrule') { | ||
name = '@' + child.name | ||
|
||
if (child.params) name += ' ' + child.params | ||
if (typeof result[cursor][name] === 'undefined') { | ||
result[cursor][name] = atRule(child) | ||
} else if (Array.isArray(result[cursor][name])) { | ||
result[cursor][name].push(atRule(child)) | ||
} else { | ||
result[cursor][name] = [result[cursor][name], atRule(child)] | ||
} | ||
} else if (child.type === 'rule') { | ||
result[cursor][child.selector] = objectifyPostcssRules(child) | ||
} else if (child.type === 'decl') { | ||
name = camelcase(child.prop) | ||
child.value = child.important ? child.value + ' !important' : child.value | ||
if (typeof result[cursor][name] === 'undefined') { | ||
result[cursor][name] = child.value | ||
} else if (Array.isArray(result[cursor][name])) { | ||
result[cursor][name].push(child.value) | ||
} else { | ||
result[cursor][name] = [result[cursor][name], child.value] | ||
} | ||
} | ||
}) | ||
return result[0] | ||
} | ||
|
||
// Parser | ||
|
||
import Input from 'postcss/lib/input' | ||
|
||
export function safeParse (css, opts) { | ||
let input = new Input(css, opts) | ||
|
||
let parser = new EmotionSafeParser(input) | ||
parser.parse() | ||
|
||
return parser.root | ||
} | ||
|
||
export class EmotionSafeParser extends SafeParser { | ||
unknownWord (tokens) { | ||
if (tokens[0][0] === 'word') { | ||
if (/xxx(\d+)xxx/gm.exec(tokens[0][1])) { | ||
this.decl(tokens) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
return | ||
} | ||
} | ||
|
||
this.spaces += tokens.map(i => i[1]).join('') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
silly way to get babels object assign
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have an
assign
util