Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
vovkasm committed Dec 16, 2016
1 parent 26c73f9 commit e6df6ed
Showing 1 changed file with 28 additions and 23 deletions.
51 changes: 28 additions & 23 deletions src/stylesheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,33 @@ function mergeTo (target, source) {
}
}

function mergeRules (props, rules) {
const ownStyle = props.style
const style = {}
let isStyle = false
for (let i in rules) {
const rule = rules[i]
if (rule.props !== undefined) {
mergeTo(props, rule.props)
}
if (rule.style !== undefined) {
mergeTo(style, rule.style)
isStyle = true
}
}
if (isStyle) {
if (ownStyle === undefined) {
props.style = style
} else if (Array.isArray(ownStyle)) {
const newStyle = ownStyle.slice()
newStyle.unshift(style)
props.style = newStyle
} else {
props.style = [style, ownStyle]
}
}
}

function shallowClone (props) {
const ret = {}
if (props !== undefined) {
Expand Down Expand Up @@ -139,29 +166,7 @@ class Stylesheet {
const ctx = parseContext(context)
const rules = []
this.collectRules(rules, ctx)
const style = {}
let isStyle = false
for (let i in rules) {
const rule = rules[i]
if (rule.props !== undefined) {
mergeTo(props, rule.props)
}
if (rule.style !== undefined) {
mergeTo(style, rule.style)
isStyle = true
}
}
if (isStyle) {
if (props.style === undefined) {
props.style = style
} else if (Array.isArray(props.style)) {
const newStyle = props.style.slice()
newStyle.unshift(style)
props.style = newStyle
} else {
props.style = [style, props.style]
}
}
mergeRules(props, rules)
return props
}
collectRules (target, ctx) {
Expand Down

0 comments on commit e6df6ed

Please sign in to comment.