-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
130 additions
and
35 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<html> | ||
<body> | ||
<script src="demo1.js"></script> | ||
</body> | ||
</html> |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const {create} = require('../index'); | ||
|
||
var renderer = create(); | ||
|
||
console.log(renderer); |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# `rule()` | ||
|
||
`rule()` is the only addon that comes pre-installed with `nano-style`. |
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,13 +1,76 @@ | ||
'use strict'; | ||
|
||
var addonConfig = require('./addon/config').addon; | ||
var addonPut = require('./addon/put').addon; | ||
var KEBAB_REGEX = /[A-Z]/g; | ||
|
||
var hash = function (str) { | ||
var hash = 5381, i = str.length; | ||
|
||
while (i) hash = (hash * 33) ^ str.charCodeAt(--i); | ||
|
||
return '_' + (hash >>> 0).toString(36); | ||
}; | ||
|
||
exports.create = function (config) { | ||
var renderer = {}; | ||
config = config || {}; | ||
var assign = config.assign || Object.assign; | ||
|
||
var renderer = assign({ | ||
raw: '', | ||
cns: {}, | ||
pfx: '_', | ||
client: typeof window === 'object', | ||
assign: assign, | ||
stringify: JSON.stringify, | ||
decl: function (key, value) { | ||
key = key.replace(KEBAB_REGEX, '-$&').toLowerCase(); | ||
return key + ':' + value + ';'; | ||
}, | ||
hash: function (obj) { | ||
return hash(renderer.stringify(obj)); | ||
}, | ||
selector: function (parent, selector) { | ||
return parent + (selector[0] === ':' ? '' : ' ') + selector; | ||
}, | ||
}, config); | ||
|
||
var putRaw; | ||
|
||
if (renderer.client) { | ||
var sheet = document.head.appendChild(document.createElement('style')).sheet; | ||
putRaw = function (rawCss) { | ||
sheet.insertRule(rawCss, 0); | ||
}; | ||
} else { | ||
putRaw = function (rawCss) { | ||
renderer.raw += rawCss; | ||
}; | ||
} | ||
|
||
var put = function (selector, decls, atrule) { | ||
var str = ''; | ||
var prop, value; | ||
|
||
for (prop in decls) { | ||
value = decls[prop]; | ||
|
||
if (value instanceof Object) { | ||
if (prop[0] === '@') { | ||
put(selector, value, prop); | ||
} else { | ||
put(renderer.selector(selector, prop), value, atrule); | ||
} | ||
} else { | ||
str += renderer.decl(prop, value); | ||
} | ||
} | ||
|
||
if (str) { | ||
str = '.' + selector + '{' + str + '}'; | ||
putRaw(atrule ? atrule + '{' + str + '}' : str); | ||
} | ||
}; | ||
|
||
addonConfig(renderer, config); | ||
addonPut(renderer); | ||
renderer.put = put; | ||
|
||
return renderer; | ||
}; |
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