jsxstyle-utils
Advanced tools
Comparing version 0.0.0-canary-20217216441 to 0.0.0-canary-202172171529
# jsxstyle-utils | ||
## 0.0.0-canary-20217216441 | ||
## 0.0.0-canary-202172171529 | ||
@@ -8,2 +8,3 @@ ### Major Changes | ||
- f6408ad: Replaced `getClassName` method with `getComponentProps`—it generates a className as before but now adds it to a filtered object of component props. | ||
- 4d2bc2a: Implemented one className per style prop functionality. | ||
@@ -10,0 +11,0 @@ ### Minor Changes |
@@ -9,8 +9,7 @@ export { addStyleToHead } from './addStyleToHead'; | ||
export { getStyleCache } from './getStyleCache'; | ||
export { getStyleKeysForProps, ComponentProp } from './getStyleKeysForProps'; | ||
export { processProps } from './processProps'; | ||
export { pseudoelements, pseudoclasses } from './getStyleKeysForProps'; | ||
export { hyphenateStyleName } from './hyphenateStyleName'; | ||
export { stringHash } from './stringHash'; | ||
export { CSSProperties } from './types'; | ||
export type { ComponentProp } from './parseStyleProps'; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -216,22 +216,2 @@ 'use strict'; | ||
function _extends() { | ||
_extends = | ||
Object.assign || | ||
function (target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i]; | ||
for (var key in source) { | ||
if (Object.prototype.hasOwnProperty.call(source, key)) { | ||
target[key] = source[key]; | ||
} | ||
} | ||
} | ||
return target; | ||
}; | ||
return _extends.apply(this, arguments); | ||
} | ||
var uppercasePattern = /([A-Z])/g; | ||
@@ -253,381 +233,12 @@ var msPattern = /^ms-/; | ||
/* tslint:disable no-bitwise */ | ||
// thx darksky: https://git.io/v9kWO | ||
function stringHash(str) { | ||
var hash = 5381; | ||
var i = str.length; | ||
function _extends() { | ||
_extends = | ||
Object.assign || | ||
function (target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i]; | ||
while (i) { | ||
hash = (hash * 33) ^ str.charCodeAt(--i); | ||
} | ||
/* JavaScript does bitwise operations (like XOR, above) on 32-bit signed | ||
* integers. Since we want the results to be always positive, convert the | ||
* signed int to an unsigned by doing an unsigned bitshift. */ | ||
return hash >>> 0; | ||
} | ||
var capRegex = /[A-Z]/g; | ||
var componentProps = { | ||
// class (preact) and className (React) are handled separately | ||
checked: true, | ||
children: true, | ||
href: true, | ||
id: true, | ||
name: true, | ||
placeholder: true, | ||
style: true, | ||
type: true, | ||
value: true, | ||
}; | ||
var pseudoelements = { | ||
after: true, | ||
before: true, | ||
placeholder: true, | ||
selection: true, | ||
}; | ||
var pseudoclasses = { | ||
active: true, | ||
checked: true, | ||
disabled: true, | ||
empty: true, | ||
enabled: true, | ||
focus: true, | ||
hover: true, | ||
invalid: true, | ||
link: true, | ||
required: true, | ||
target: true, | ||
valid: true, | ||
}; | ||
/** Props that are used internally and not passed on to the underlying component */ | ||
var skippedProps = { | ||
component: true, | ||
mediaQueries: true, | ||
props: true, | ||
}; | ||
var sameAxisPropNames = { | ||
paddingH: ['paddingLeft', 'paddingRight'], | ||
paddingV: ['paddingTop', 'paddingBottom'], | ||
marginH: ['marginLeft', 'marginRight'], | ||
marginV: ['marginTop', 'marginBottom'], | ||
}; | ||
function getStyleKeysForProps(props, classNamePropKey, pretty) { | ||
if (pretty === void 0) { | ||
pretty = false; | ||
} | ||
if (typeof props !== 'object' || props === null) { | ||
return null; | ||
} | ||
var propKeys = Object.keys(props).sort(); | ||
var keyCount = propKeys.length; | ||
if (keyCount === 0) { | ||
return null; | ||
} | ||
var mediaQueries = props.mediaQueries; | ||
var usesMediaQueries = false; | ||
var stylesByKey = {}; | ||
var styleKeyObj = { | ||
classNameKey: '', | ||
stylesByKey: stylesByKey, | ||
props: typeof props.props === 'object' ? _extends({}, props.props) : null, | ||
}; | ||
var classNameKey = ''; | ||
var animations; | ||
var seenMQs = {}; | ||
var mqSortKeys = {}; | ||
if (mediaQueries != null) { | ||
var idx = -1; | ||
for (var k in mediaQueries) { | ||
mqSortKeys[k] = '@' + (1000 + ++idx); | ||
} | ||
} | ||
for (var _idx = -1; ++_idx < keyCount; ) { | ||
var originalPropName = propKeys[_idx]; // separate known component props from style props | ||
if (componentProps.hasOwnProperty(originalPropName)) { | ||
(styleKeyObj.props = styleKeyObj.props || {})[originalPropName] = | ||
props[originalPropName]; | ||
continue; | ||
} | ||
if ( | ||
skippedProps.hasOwnProperty(originalPropName) || | ||
originalPropName === classNamePropKey || | ||
!props.hasOwnProperty(originalPropName) | ||
) { | ||
continue; | ||
} | ||
var propName = originalPropName; | ||
var propSansMQ = void 0; | ||
var pseudoelement = void 0; | ||
var pseudoclass = void 0; | ||
var mqKey = void 0; | ||
capRegex.lastIndex = 0; | ||
var splitIndex = 0; | ||
var prefix = | ||
capRegex.test(originalPropName) && | ||
capRegex.lastIndex > 1 && | ||
originalPropName.slice(0, capRegex.lastIndex - 1); // all /^on[A-Z]/ props get passed through to the underlying component | ||
if (prefix === 'on') { | ||
(styleKeyObj.props = styleKeyObj.props || {})[originalPropName] = | ||
props[originalPropName]; | ||
continue; | ||
} // check for media query prefix | ||
if (prefix && mediaQueries != null && mediaQueries.hasOwnProperty(prefix)) { | ||
usesMediaQueries = true; | ||
mqKey = prefix; | ||
splitIndex = capRegex.lastIndex - 1; | ||
propSansMQ = | ||
originalPropName[splitIndex].toLowerCase() + | ||
originalPropName.slice(splitIndex + 1); | ||
prefix = | ||
capRegex.test(originalPropName) && | ||
propSansMQ.slice(0, capRegex.lastIndex - splitIndex - 1); | ||
} // check for pseudoelement prefix | ||
if (prefix && pseudoelements.hasOwnProperty(prefix)) { | ||
pseudoelement = prefix; | ||
splitIndex = capRegex.lastIndex - 1; | ||
prefix = | ||
capRegex.test(originalPropName) && | ||
originalPropName[splitIndex].toLowerCase() + | ||
originalPropName.slice(splitIndex + 1, capRegex.lastIndex - 1); | ||
} // check for pseudoclass prefix | ||
if (prefix && pseudoclasses.hasOwnProperty(prefix)) { | ||
pseudoclass = prefix; | ||
splitIndex = capRegex.lastIndex - 1; | ||
} // trim prefixes off propName | ||
if (splitIndex > 0) { | ||
propName = | ||
originalPropName[splitIndex].toLowerCase() + | ||
originalPropName.slice(splitIndex + 1); | ||
} | ||
var styleValue = props[originalPropName]; | ||
var space = pretty ? ' ' : ''; | ||
var colon = ':' + space; | ||
var newline = pretty ? '\n' : ''; | ||
var semicolon = ';' + newline; | ||
var indent = pretty ? ' ' : ''; | ||
if ( | ||
propName === 'animation' && | ||
styleValue && | ||
typeof styleValue === 'object' | ||
) { | ||
var animationValue = newline; | ||
for (var _k in styleValue) { | ||
var obj = styleValue[_k]; | ||
animationValue += _k + space + '{' + newline; | ||
for (var childPropName in obj) { | ||
var _value = dangerousStyleValue(childPropName, obj[childPropName]); | ||
animationValue += | ||
indent + | ||
hyphenateStyleName(childPropName) + | ||
colon + | ||
_value + | ||
semicolon; | ||
} | ||
animationValue += '}' + newline; | ||
} | ||
var animationKey = 'jsxstyle_' + stringHash(animationValue).toString(36); | ||
propName = 'animationName'; | ||
styleValue = animationKey; | ||
animations = animations || {}; | ||
animations[animationKey] = animationValue; | ||
} else { | ||
styleValue = dangerousStyleValue(propName, props[originalPropName]); | ||
if (styleValue === '') { | ||
continue; | ||
} | ||
} | ||
var mediaQuery = mqKey && mediaQueries[mqKey]; | ||
var mqSortKey = mqKey && mqSortKeys[mqKey]; | ||
var key = | ||
'.' + | ||
(mqSortKey || '') + | ||
(pseudoclass ? ':' + pseudoclass : '') + | ||
(pseudoelement ? '::' + pseudoelement : ''); | ||
if (!stylesByKey.hasOwnProperty(key)) { | ||
stylesByKey[key] = { | ||
styles: newline, | ||
}; | ||
if (mediaQuery) { | ||
stylesByKey[key].mediaQuery = mediaQuery; | ||
} | ||
if (pseudoclass) { | ||
stylesByKey[key].pseudoclass = pseudoclass; | ||
} | ||
if (pseudoelement) { | ||
stylesByKey[key].pseudoelement = pseudoelement; | ||
} | ||
} | ||
if (mediaQuery) { | ||
seenMQs[mediaQuery] = seenMQs[mediaQuery] || ''; | ||
seenMQs[mediaQuery] += propSansMQ + ':' + styleValue + ';'; | ||
} else { | ||
classNameKey += originalPropName + ':' + styleValue + ';'; | ||
} | ||
var value = colon + styleValue + semicolon; | ||
var propArray = sameAxisPropNames[propName]; | ||
if (propArray) { | ||
stylesByKey[key].styles += | ||
indent + | ||
hyphenateStyleName(propArray[0]) + | ||
value + | ||
indent + | ||
hyphenateStyleName(propArray[1]) + | ||
value; | ||
} else { | ||
stylesByKey[key].styles += indent + hyphenateStyleName(propName) + value; | ||
} | ||
} // append media query key | ||
if (usesMediaQueries) { | ||
var mqKeys = Object.keys(seenMQs).sort(); | ||
for (var _idx2 = -1, len = mqKeys.length; ++_idx2 < len; ) { | ||
var _mediaQuery = mqKeys[_idx2]; | ||
classNameKey += '@' + _mediaQuery + '~' + seenMQs[_mediaQuery]; | ||
} | ||
} | ||
if (animations) { | ||
styleKeyObj.animations = animations; | ||
} | ||
styleKeyObj.classNameKey = classNameKey || null; | ||
return styleKeyObj; | ||
} | ||
function cannotInject() { | ||
throw new Error( | ||
'jsxstyle error: `injectOptions` must be called before any jsxstyle components mount.' | ||
); | ||
} | ||
function alreadyInjected() { | ||
throw new Error( | ||
'jsxstyle error: `injectOptions` should be called once and only once.' | ||
); | ||
} | ||
var getStringHash = function getStringHash(key) { | ||
return '_' + stringHash(key).toString(36); | ||
}; | ||
function getStyleCache() { | ||
var _classNameCache = {}; | ||
var getClassNameForKey = getStringHash; | ||
var onInsertRule; | ||
var pretty = false; | ||
var styleCache = { | ||
reset: function reset() { | ||
_classNameCache = {}; | ||
}, | ||
injectOptions: function injectOptions(options) { | ||
if (options) { | ||
if (options.getClassName) { | ||
getClassNameForKey = options.getClassName; | ||
} | ||
if (options.onInsertRule) { | ||
onInsertRule = options.onInsertRule; | ||
} | ||
if (options.pretty) { | ||
pretty = options.pretty; | ||
} | ||
} | ||
styleCache.injectOptions = alreadyInjected; | ||
}, | ||
getComponentProps: function getComponentProps(props, classNamePropKey) { | ||
if (classNamePropKey === void 0) { | ||
classNamePropKey = 'className'; | ||
} | ||
styleCache.injectOptions = cannotInject; | ||
var styleObj = getStyleKeysForProps(props, classNamePropKey, pretty); | ||
if (styleObj == null) { | ||
return null; | ||
} | ||
var key = styleObj.classNameKey; | ||
if (key && !_classNameCache.hasOwnProperty(key)) { | ||
_classNameCache[key] = getClassNameForKey(key, props); | ||
Object.keys(styleObj.stylesByKey) | ||
.sort() | ||
.forEach(function (k) { | ||
var selector = '.' + _classNameCache[key]; // prettier-ignore | ||
var _styleObj$stylesByKey = styleObj.stylesByKey[k], | ||
pseudoclass = _styleObj$stylesByKey.pseudoclass, | ||
pseudoelement = _styleObj$stylesByKey.pseudoelement, | ||
mediaQuery = _styleObj$stylesByKey.mediaQuery, | ||
styles = _styleObj$stylesByKey.styles; | ||
var rule = | ||
selector + | ||
(pseudoclass ? ':' + pseudoclass : '') + | ||
(pseudoelement ? '::' + pseudoelement : '') + | ||
(' {' + styles + '}'); | ||
if (mediaQuery) { | ||
rule = '@media ' + mediaQuery + ' { ' + rule + ' }'; | ||
} | ||
if ( | ||
onInsertRule && // if the function returns false, bail. | ||
onInsertRule(rule, props) === false | ||
) { | ||
return; | ||
} | ||
addStyleToHead(rule); | ||
}); | ||
} | ||
var animations = styleObj.animations; | ||
if (animations) { | ||
for (var animationKey in animations) { | ||
var rule = | ||
'@keyframes ' + | ||
animationKey + | ||
' {' + | ||
animations[animationKey] + | ||
'}'; | ||
if (!onInsertRule || onInsertRule(rule, props) !== false) { | ||
addStyleToHead(rule); | ||
for (var key in source) { | ||
if (Object.prototype.hasOwnProperty.call(source, key)) { | ||
target[key] = source[key]; | ||
} | ||
@@ -637,23 +248,10 @@ } | ||
var classNameProp = props[classNamePropKey]; | ||
var classNameForKey = key && _classNameCache[key]; | ||
var className = | ||
classNameForKey && classNameProp | ||
? classNameProp + ' ' + classNameForKey | ||
: classNameForKey || classNameProp || null; | ||
return target; | ||
}; | ||
var finalProps = _extends({}, styleObj.props); | ||
if (className) { | ||
finalProps[classNamePropKey] = className; | ||
} | ||
return finalProps; | ||
}, | ||
}; | ||
return styleCache; | ||
return _extends.apply(this, arguments); | ||
} | ||
// global flag makes subsequent calls of capRegex.test advance to the next match | ||
var capRegex$1 = /[A-Z]/g; | ||
var capRegex = /[A-Z]/g; | ||
var commonComponentProps = { | ||
@@ -671,3 +269,3 @@ // class (preact) and className (React) are handled separately | ||
}; | ||
var pseudoelements$1 = { | ||
var pseudoelements = { | ||
after: true, | ||
@@ -678,3 +276,3 @@ before: true, | ||
}; | ||
var pseudoclasses$1 = { | ||
var pseudoclasses = { | ||
active: true, | ||
@@ -695,3 +293,3 @@ checked: true, | ||
var skippedProps$1 = { | ||
var skippedProps = { | ||
component: true, | ||
@@ -749,3 +347,3 @@ mediaQueries: true, | ||
if ( | ||
skippedProps$1.hasOwnProperty(originalPropName) || | ||
skippedProps.hasOwnProperty(originalPropName) || | ||
originalPropName === classNamePropKey || | ||
@@ -761,8 +359,8 @@ !props.hasOwnProperty(originalPropName) | ||
var specificity = 0; | ||
capRegex$1.lastIndex = 0; | ||
capRegex.lastIndex = 0; | ||
var splitIndex = 0; | ||
var propNamePrefix = | ||
capRegex$1.test(originalPropName) && | ||
capRegex$1.lastIndex > 1 && | ||
originalPropName.slice(0, capRegex$1.lastIndex - 1); // all /^on[A-Z]/ props get passed through to the underlying component | ||
capRegex.test(originalPropName) && | ||
capRegex.lastIndex > 1 && | ||
originalPropName.slice(0, capRegex.lastIndex - 1); // all /^on[A-Z]/ props get passed through to the underlying component | ||
@@ -774,18 +372,18 @@ if (propNamePrefix === 'on') { | ||
if (propNamePrefix && pseudoelements$1[propNamePrefix]) { | ||
if (propNamePrefix && pseudoelements[propNamePrefix]) { | ||
pseudoelement = propNamePrefix; | ||
splitIndex = capRegex$1.lastIndex - 1; | ||
splitIndex = capRegex.lastIndex - 1; | ||
propNamePrefix = | ||
capRegex$1.test(originalPropName) && | ||
capRegex.test(originalPropName) && | ||
originalPropName[splitIndex].toLowerCase() + | ||
originalPropName.slice(splitIndex + 1, capRegex$1.lastIndex - 1); | ||
originalPropName.slice(splitIndex + 1, capRegex.lastIndex - 1); | ||
} // check for pseudoclass prefix | ||
if (propNamePrefix && pseudoclasses$1[propNamePrefix]) { | ||
if (propNamePrefix && pseudoclasses[propNamePrefix]) { | ||
pseudoclass = propNamePrefix; | ||
splitIndex = capRegex$1.lastIndex - 1; | ||
splitIndex = capRegex.lastIndex - 1; | ||
propNamePrefix = | ||
capRegex$1.test(originalPropName) && | ||
capRegex.test(originalPropName) && | ||
originalPropName[splitIndex].toLowerCase() + | ||
originalPropName.slice(splitIndex + 1, capRegex$1.lastIndex - 1); | ||
originalPropName.slice(splitIndex + 1, capRegex.lastIndex - 1); | ||
} // check if we need to bump specificity | ||
@@ -994,2 +592,96 @@ | ||
/* tslint:disable no-bitwise */ | ||
// thx darksky: https://git.io/v9kWO | ||
function stringHash(str) { | ||
var hash = 5381; | ||
var i = str.length; | ||
while (i) { | ||
hash = (hash * 33) ^ str.charCodeAt(--i); | ||
} | ||
/* JavaScript does bitwise operations (like XOR, above) on 32-bit signed | ||
* integers. Since we want the results to be always positive, convert the | ||
* signed int to an unsigned by doing an unsigned bitshift. */ | ||
return hash >>> 0; | ||
} | ||
function cannotInject() { | ||
throw new Error( | ||
'jsxstyle error: `injectOptions` must be called before any jsxstyle components mount.' | ||
); | ||
} | ||
function alreadyInjected() { | ||
throw new Error( | ||
'jsxstyle error: `injectOptions` should be called once and only once.' | ||
); | ||
} | ||
var getStringHash = function getStringHash(key) { | ||
return '_' + stringHash(key).toString(36); | ||
}; | ||
function getStyleCache() { | ||
var _classNameCache = {}; | ||
var getClassNameForKey = getStringHash; | ||
var onInsertRule; | ||
var memoizedGetClassNameForKey = function memoizedGetClassNameForKey(key) { | ||
if (!_classNameCache[key]) { | ||
_classNameCache[key] = getClassNameForKey(key); | ||
} | ||
return _classNameCache[key]; | ||
}; | ||
var styleCache = { | ||
reset: function reset() { | ||
_classNameCache = {}; | ||
}, | ||
injectOptions: function injectOptions(options) { | ||
if (options) { | ||
if (options.getClassName) { | ||
getClassNameForKey = options.getClassName; | ||
} | ||
if (options.onInsertRule) { | ||
onInsertRule = options.onInsertRule; | ||
} | ||
} | ||
styleCache.injectOptions = alreadyInjected; | ||
}, | ||
getComponentProps: function getComponentProps(props, classNamePropKey) { | ||
if (classNamePropKey === void 0) { | ||
classNamePropKey = 'className'; | ||
} | ||
styleCache.injectOptions = cannotInject; | ||
var componentProps = processProps( | ||
props, | ||
classNamePropKey, | ||
memoizedGetClassNameForKey | ||
); | ||
if (!componentProps) { | ||
return null; | ||
} | ||
componentProps.rules.forEach(function (rule) { | ||
if ( | ||
onInsertRule && // if the function returns false, bail. | ||
onInsertRule(rule, props) === false | ||
) { | ||
return; | ||
} | ||
addStyleToHead(rule); | ||
}); | ||
return componentProps.props; | ||
}, | ||
}; | ||
return styleCache; | ||
} | ||
exports.addStyleToHead = addStyleToHead; | ||
@@ -999,7 +691,4 @@ exports.componentStyles = componentStyles; | ||
exports.getStyleCache = getStyleCache; | ||
exports.getStyleKeysForProps = getStyleKeysForProps; | ||
exports.hyphenateStyleName = hyphenateStyleName; | ||
exports.processProps = processProps; | ||
exports.pseudoclasses = pseudoclasses; | ||
exports.pseudoelements = pseudoelements; | ||
exports.stringHash = stringHash; |
@@ -212,22 +212,2 @@ var canUseDOM = !!( | ||
function _extends() { | ||
_extends = | ||
Object.assign || | ||
function (target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i]; | ||
for (var key in source) { | ||
if (Object.prototype.hasOwnProperty.call(source, key)) { | ||
target[key] = source[key]; | ||
} | ||
} | ||
} | ||
return target; | ||
}; | ||
return _extends.apply(this, arguments); | ||
} | ||
var uppercasePattern = /([A-Z])/g; | ||
@@ -249,381 +229,12 @@ var msPattern = /^ms-/; | ||
/* tslint:disable no-bitwise */ | ||
// thx darksky: https://git.io/v9kWO | ||
function stringHash(str) { | ||
var hash = 5381; | ||
var i = str.length; | ||
function _extends() { | ||
_extends = | ||
Object.assign || | ||
function (target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i]; | ||
while (i) { | ||
hash = (hash * 33) ^ str.charCodeAt(--i); | ||
} | ||
/* JavaScript does bitwise operations (like XOR, above) on 32-bit signed | ||
* integers. Since we want the results to be always positive, convert the | ||
* signed int to an unsigned by doing an unsigned bitshift. */ | ||
return hash >>> 0; | ||
} | ||
var capRegex = /[A-Z]/g; | ||
var componentProps = { | ||
// class (preact) and className (React) are handled separately | ||
checked: true, | ||
children: true, | ||
href: true, | ||
id: true, | ||
name: true, | ||
placeholder: true, | ||
style: true, | ||
type: true, | ||
value: true, | ||
}; | ||
var pseudoelements = { | ||
after: true, | ||
before: true, | ||
placeholder: true, | ||
selection: true, | ||
}; | ||
var pseudoclasses = { | ||
active: true, | ||
checked: true, | ||
disabled: true, | ||
empty: true, | ||
enabled: true, | ||
focus: true, | ||
hover: true, | ||
invalid: true, | ||
link: true, | ||
required: true, | ||
target: true, | ||
valid: true, | ||
}; | ||
/** Props that are used internally and not passed on to the underlying component */ | ||
var skippedProps = { | ||
component: true, | ||
mediaQueries: true, | ||
props: true, | ||
}; | ||
var sameAxisPropNames = { | ||
paddingH: ['paddingLeft', 'paddingRight'], | ||
paddingV: ['paddingTop', 'paddingBottom'], | ||
marginH: ['marginLeft', 'marginRight'], | ||
marginV: ['marginTop', 'marginBottom'], | ||
}; | ||
function getStyleKeysForProps(props, classNamePropKey, pretty) { | ||
if (pretty === void 0) { | ||
pretty = false; | ||
} | ||
if (typeof props !== 'object' || props === null) { | ||
return null; | ||
} | ||
var propKeys = Object.keys(props).sort(); | ||
var keyCount = propKeys.length; | ||
if (keyCount === 0) { | ||
return null; | ||
} | ||
var mediaQueries = props.mediaQueries; | ||
var usesMediaQueries = false; | ||
var stylesByKey = {}; | ||
var styleKeyObj = { | ||
classNameKey: '', | ||
stylesByKey: stylesByKey, | ||
props: typeof props.props === 'object' ? _extends({}, props.props) : null, | ||
}; | ||
var classNameKey = ''; | ||
var animations; | ||
var seenMQs = {}; | ||
var mqSortKeys = {}; | ||
if (mediaQueries != null) { | ||
var idx = -1; | ||
for (var k in mediaQueries) { | ||
mqSortKeys[k] = '@' + (1000 + ++idx); | ||
} | ||
} | ||
for (var _idx = -1; ++_idx < keyCount; ) { | ||
var originalPropName = propKeys[_idx]; // separate known component props from style props | ||
if (componentProps.hasOwnProperty(originalPropName)) { | ||
(styleKeyObj.props = styleKeyObj.props || {})[originalPropName] = | ||
props[originalPropName]; | ||
continue; | ||
} | ||
if ( | ||
skippedProps.hasOwnProperty(originalPropName) || | ||
originalPropName === classNamePropKey || | ||
!props.hasOwnProperty(originalPropName) | ||
) { | ||
continue; | ||
} | ||
var propName = originalPropName; | ||
var propSansMQ = void 0; | ||
var pseudoelement = void 0; | ||
var pseudoclass = void 0; | ||
var mqKey = void 0; | ||
capRegex.lastIndex = 0; | ||
var splitIndex = 0; | ||
var prefix = | ||
capRegex.test(originalPropName) && | ||
capRegex.lastIndex > 1 && | ||
originalPropName.slice(0, capRegex.lastIndex - 1); // all /^on[A-Z]/ props get passed through to the underlying component | ||
if (prefix === 'on') { | ||
(styleKeyObj.props = styleKeyObj.props || {})[originalPropName] = | ||
props[originalPropName]; | ||
continue; | ||
} // check for media query prefix | ||
if (prefix && mediaQueries != null && mediaQueries.hasOwnProperty(prefix)) { | ||
usesMediaQueries = true; | ||
mqKey = prefix; | ||
splitIndex = capRegex.lastIndex - 1; | ||
propSansMQ = | ||
originalPropName[splitIndex].toLowerCase() + | ||
originalPropName.slice(splitIndex + 1); | ||
prefix = | ||
capRegex.test(originalPropName) && | ||
propSansMQ.slice(0, capRegex.lastIndex - splitIndex - 1); | ||
} // check for pseudoelement prefix | ||
if (prefix && pseudoelements.hasOwnProperty(prefix)) { | ||
pseudoelement = prefix; | ||
splitIndex = capRegex.lastIndex - 1; | ||
prefix = | ||
capRegex.test(originalPropName) && | ||
originalPropName[splitIndex].toLowerCase() + | ||
originalPropName.slice(splitIndex + 1, capRegex.lastIndex - 1); | ||
} // check for pseudoclass prefix | ||
if (prefix && pseudoclasses.hasOwnProperty(prefix)) { | ||
pseudoclass = prefix; | ||
splitIndex = capRegex.lastIndex - 1; | ||
} // trim prefixes off propName | ||
if (splitIndex > 0) { | ||
propName = | ||
originalPropName[splitIndex].toLowerCase() + | ||
originalPropName.slice(splitIndex + 1); | ||
} | ||
var styleValue = props[originalPropName]; | ||
var space = pretty ? ' ' : ''; | ||
var colon = ':' + space; | ||
var newline = pretty ? '\n' : ''; | ||
var semicolon = ';' + newline; | ||
var indent = pretty ? ' ' : ''; | ||
if ( | ||
propName === 'animation' && | ||
styleValue && | ||
typeof styleValue === 'object' | ||
) { | ||
var animationValue = newline; | ||
for (var _k in styleValue) { | ||
var obj = styleValue[_k]; | ||
animationValue += _k + space + '{' + newline; | ||
for (var childPropName in obj) { | ||
var _value = dangerousStyleValue(childPropName, obj[childPropName]); | ||
animationValue += | ||
indent + | ||
hyphenateStyleName(childPropName) + | ||
colon + | ||
_value + | ||
semicolon; | ||
} | ||
animationValue += '}' + newline; | ||
} | ||
var animationKey = 'jsxstyle_' + stringHash(animationValue).toString(36); | ||
propName = 'animationName'; | ||
styleValue = animationKey; | ||
animations = animations || {}; | ||
animations[animationKey] = animationValue; | ||
} else { | ||
styleValue = dangerousStyleValue(propName, props[originalPropName]); | ||
if (styleValue === '') { | ||
continue; | ||
} | ||
} | ||
var mediaQuery = mqKey && mediaQueries[mqKey]; | ||
var mqSortKey = mqKey && mqSortKeys[mqKey]; | ||
var key = | ||
'.' + | ||
(mqSortKey || '') + | ||
(pseudoclass ? ':' + pseudoclass : '') + | ||
(pseudoelement ? '::' + pseudoelement : ''); | ||
if (!stylesByKey.hasOwnProperty(key)) { | ||
stylesByKey[key] = { | ||
styles: newline, | ||
}; | ||
if (mediaQuery) { | ||
stylesByKey[key].mediaQuery = mediaQuery; | ||
} | ||
if (pseudoclass) { | ||
stylesByKey[key].pseudoclass = pseudoclass; | ||
} | ||
if (pseudoelement) { | ||
stylesByKey[key].pseudoelement = pseudoelement; | ||
} | ||
} | ||
if (mediaQuery) { | ||
seenMQs[mediaQuery] = seenMQs[mediaQuery] || ''; | ||
seenMQs[mediaQuery] += propSansMQ + ':' + styleValue + ';'; | ||
} else { | ||
classNameKey += originalPropName + ':' + styleValue + ';'; | ||
} | ||
var value = colon + styleValue + semicolon; | ||
var propArray = sameAxisPropNames[propName]; | ||
if (propArray) { | ||
stylesByKey[key].styles += | ||
indent + | ||
hyphenateStyleName(propArray[0]) + | ||
value + | ||
indent + | ||
hyphenateStyleName(propArray[1]) + | ||
value; | ||
} else { | ||
stylesByKey[key].styles += indent + hyphenateStyleName(propName) + value; | ||
} | ||
} // append media query key | ||
if (usesMediaQueries) { | ||
var mqKeys = Object.keys(seenMQs).sort(); | ||
for (var _idx2 = -1, len = mqKeys.length; ++_idx2 < len; ) { | ||
var _mediaQuery = mqKeys[_idx2]; | ||
classNameKey += '@' + _mediaQuery + '~' + seenMQs[_mediaQuery]; | ||
} | ||
} | ||
if (animations) { | ||
styleKeyObj.animations = animations; | ||
} | ||
styleKeyObj.classNameKey = classNameKey || null; | ||
return styleKeyObj; | ||
} | ||
function cannotInject() { | ||
throw new Error( | ||
'jsxstyle error: `injectOptions` must be called before any jsxstyle components mount.' | ||
); | ||
} | ||
function alreadyInjected() { | ||
throw new Error( | ||
'jsxstyle error: `injectOptions` should be called once and only once.' | ||
); | ||
} | ||
var getStringHash = function getStringHash(key) { | ||
return '_' + stringHash(key).toString(36); | ||
}; | ||
function getStyleCache() { | ||
var _classNameCache = {}; | ||
var getClassNameForKey = getStringHash; | ||
var onInsertRule; | ||
var pretty = false; | ||
var styleCache = { | ||
reset: function reset() { | ||
_classNameCache = {}; | ||
}, | ||
injectOptions: function injectOptions(options) { | ||
if (options) { | ||
if (options.getClassName) { | ||
getClassNameForKey = options.getClassName; | ||
} | ||
if (options.onInsertRule) { | ||
onInsertRule = options.onInsertRule; | ||
} | ||
if (options.pretty) { | ||
pretty = options.pretty; | ||
} | ||
} | ||
styleCache.injectOptions = alreadyInjected; | ||
}, | ||
getComponentProps: function getComponentProps(props, classNamePropKey) { | ||
if (classNamePropKey === void 0) { | ||
classNamePropKey = 'className'; | ||
} | ||
styleCache.injectOptions = cannotInject; | ||
var styleObj = getStyleKeysForProps(props, classNamePropKey, pretty); | ||
if (styleObj == null) { | ||
return null; | ||
} | ||
var key = styleObj.classNameKey; | ||
if (key && !_classNameCache.hasOwnProperty(key)) { | ||
_classNameCache[key] = getClassNameForKey(key, props); | ||
Object.keys(styleObj.stylesByKey) | ||
.sort() | ||
.forEach(function (k) { | ||
var selector = '.' + _classNameCache[key]; // prettier-ignore | ||
var _styleObj$stylesByKey = styleObj.stylesByKey[k], | ||
pseudoclass = _styleObj$stylesByKey.pseudoclass, | ||
pseudoelement = _styleObj$stylesByKey.pseudoelement, | ||
mediaQuery = _styleObj$stylesByKey.mediaQuery, | ||
styles = _styleObj$stylesByKey.styles; | ||
var rule = | ||
selector + | ||
(pseudoclass ? ':' + pseudoclass : '') + | ||
(pseudoelement ? '::' + pseudoelement : '') + | ||
(' {' + styles + '}'); | ||
if (mediaQuery) { | ||
rule = '@media ' + mediaQuery + ' { ' + rule + ' }'; | ||
} | ||
if ( | ||
onInsertRule && // if the function returns false, bail. | ||
onInsertRule(rule, props) === false | ||
) { | ||
return; | ||
} | ||
addStyleToHead(rule); | ||
}); | ||
} | ||
var animations = styleObj.animations; | ||
if (animations) { | ||
for (var animationKey in animations) { | ||
var rule = | ||
'@keyframes ' + | ||
animationKey + | ||
' {' + | ||
animations[animationKey] + | ||
'}'; | ||
if (!onInsertRule || onInsertRule(rule, props) !== false) { | ||
addStyleToHead(rule); | ||
for (var key in source) { | ||
if (Object.prototype.hasOwnProperty.call(source, key)) { | ||
target[key] = source[key]; | ||
} | ||
@@ -633,23 +244,10 @@ } | ||
var classNameProp = props[classNamePropKey]; | ||
var classNameForKey = key && _classNameCache[key]; | ||
var className = | ||
classNameForKey && classNameProp | ||
? classNameProp + ' ' + classNameForKey | ||
: classNameForKey || classNameProp || null; | ||
return target; | ||
}; | ||
var finalProps = _extends({}, styleObj.props); | ||
if (className) { | ||
finalProps[classNamePropKey] = className; | ||
} | ||
return finalProps; | ||
}, | ||
}; | ||
return styleCache; | ||
return _extends.apply(this, arguments); | ||
} | ||
// global flag makes subsequent calls of capRegex.test advance to the next match | ||
var capRegex$1 = /[A-Z]/g; | ||
var capRegex = /[A-Z]/g; | ||
var commonComponentProps = { | ||
@@ -667,3 +265,3 @@ // class (preact) and className (React) are handled separately | ||
}; | ||
var pseudoelements$1 = { | ||
var pseudoelements = { | ||
after: true, | ||
@@ -674,3 +272,3 @@ before: true, | ||
}; | ||
var pseudoclasses$1 = { | ||
var pseudoclasses = { | ||
active: true, | ||
@@ -691,3 +289,3 @@ checked: true, | ||
var skippedProps$1 = { | ||
var skippedProps = { | ||
component: true, | ||
@@ -745,3 +343,3 @@ mediaQueries: true, | ||
if ( | ||
skippedProps$1.hasOwnProperty(originalPropName) || | ||
skippedProps.hasOwnProperty(originalPropName) || | ||
originalPropName === classNamePropKey || | ||
@@ -757,8 +355,8 @@ !props.hasOwnProperty(originalPropName) | ||
var specificity = 0; | ||
capRegex$1.lastIndex = 0; | ||
capRegex.lastIndex = 0; | ||
var splitIndex = 0; | ||
var propNamePrefix = | ||
capRegex$1.test(originalPropName) && | ||
capRegex$1.lastIndex > 1 && | ||
originalPropName.slice(0, capRegex$1.lastIndex - 1); // all /^on[A-Z]/ props get passed through to the underlying component | ||
capRegex.test(originalPropName) && | ||
capRegex.lastIndex > 1 && | ||
originalPropName.slice(0, capRegex.lastIndex - 1); // all /^on[A-Z]/ props get passed through to the underlying component | ||
@@ -770,18 +368,18 @@ if (propNamePrefix === 'on') { | ||
if (propNamePrefix && pseudoelements$1[propNamePrefix]) { | ||
if (propNamePrefix && pseudoelements[propNamePrefix]) { | ||
pseudoelement = propNamePrefix; | ||
splitIndex = capRegex$1.lastIndex - 1; | ||
splitIndex = capRegex.lastIndex - 1; | ||
propNamePrefix = | ||
capRegex$1.test(originalPropName) && | ||
capRegex.test(originalPropName) && | ||
originalPropName[splitIndex].toLowerCase() + | ||
originalPropName.slice(splitIndex + 1, capRegex$1.lastIndex - 1); | ||
originalPropName.slice(splitIndex + 1, capRegex.lastIndex - 1); | ||
} // check for pseudoclass prefix | ||
if (propNamePrefix && pseudoclasses$1[propNamePrefix]) { | ||
if (propNamePrefix && pseudoclasses[propNamePrefix]) { | ||
pseudoclass = propNamePrefix; | ||
splitIndex = capRegex$1.lastIndex - 1; | ||
splitIndex = capRegex.lastIndex - 1; | ||
propNamePrefix = | ||
capRegex$1.test(originalPropName) && | ||
capRegex.test(originalPropName) && | ||
originalPropName[splitIndex].toLowerCase() + | ||
originalPropName.slice(splitIndex + 1, capRegex$1.lastIndex - 1); | ||
originalPropName.slice(splitIndex + 1, capRegex.lastIndex - 1); | ||
} // check if we need to bump specificity | ||
@@ -990,2 +588,96 @@ | ||
/* tslint:disable no-bitwise */ | ||
// thx darksky: https://git.io/v9kWO | ||
function stringHash(str) { | ||
var hash = 5381; | ||
var i = str.length; | ||
while (i) { | ||
hash = (hash * 33) ^ str.charCodeAt(--i); | ||
} | ||
/* JavaScript does bitwise operations (like XOR, above) on 32-bit signed | ||
* integers. Since we want the results to be always positive, convert the | ||
* signed int to an unsigned by doing an unsigned bitshift. */ | ||
return hash >>> 0; | ||
} | ||
function cannotInject() { | ||
throw new Error( | ||
'jsxstyle error: `injectOptions` must be called before any jsxstyle components mount.' | ||
); | ||
} | ||
function alreadyInjected() { | ||
throw new Error( | ||
'jsxstyle error: `injectOptions` should be called once and only once.' | ||
); | ||
} | ||
var getStringHash = function getStringHash(key) { | ||
return '_' + stringHash(key).toString(36); | ||
}; | ||
function getStyleCache() { | ||
var _classNameCache = {}; | ||
var getClassNameForKey = getStringHash; | ||
var onInsertRule; | ||
var memoizedGetClassNameForKey = function memoizedGetClassNameForKey(key) { | ||
if (!_classNameCache[key]) { | ||
_classNameCache[key] = getClassNameForKey(key); | ||
} | ||
return _classNameCache[key]; | ||
}; | ||
var styleCache = { | ||
reset: function reset() { | ||
_classNameCache = {}; | ||
}, | ||
injectOptions: function injectOptions(options) { | ||
if (options) { | ||
if (options.getClassName) { | ||
getClassNameForKey = options.getClassName; | ||
} | ||
if (options.onInsertRule) { | ||
onInsertRule = options.onInsertRule; | ||
} | ||
} | ||
styleCache.injectOptions = alreadyInjected; | ||
}, | ||
getComponentProps: function getComponentProps(props, classNamePropKey) { | ||
if (classNamePropKey === void 0) { | ||
classNamePropKey = 'className'; | ||
} | ||
styleCache.injectOptions = cannotInject; | ||
var componentProps = processProps( | ||
props, | ||
classNamePropKey, | ||
memoizedGetClassNameForKey | ||
); | ||
if (!componentProps) { | ||
return null; | ||
} | ||
componentProps.rules.forEach(function (rule) { | ||
if ( | ||
onInsertRule && // if the function returns false, bail. | ||
onInsertRule(rule, props) === false | ||
) { | ||
return; | ||
} | ||
addStyleToHead(rule); | ||
}); | ||
return componentProps.props; | ||
}, | ||
}; | ||
return styleCache; | ||
} | ||
export { | ||
@@ -996,8 +688,5 @@ addStyleToHead, | ||
getStyleCache, | ||
getStyleKeysForProps, | ||
hyphenateStyleName, | ||
processProps, | ||
pseudoclasses, | ||
pseudoelements, | ||
stringHash, | ||
}; |
@@ -215,22 +215,2 @@ define('jsxstyle-utils', ['exports'], function (exports) { | ||
function _extends() { | ||
_extends = | ||
Object.assign || | ||
function (target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i]; | ||
for (var key in source) { | ||
if (Object.prototype.hasOwnProperty.call(source, key)) { | ||
target[key] = source[key]; | ||
} | ||
} | ||
} | ||
return target; | ||
}; | ||
return _extends.apply(this, arguments); | ||
} | ||
var uppercasePattern = /([A-Z])/g; | ||
@@ -252,387 +232,12 @@ var msPattern = /^ms-/; | ||
/* tslint:disable no-bitwise */ | ||
// thx darksky: https://git.io/v9kWO | ||
function stringHash(str) { | ||
var hash = 5381; | ||
var i = str.length; | ||
function _extends() { | ||
_extends = | ||
Object.assign || | ||
function (target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i]; | ||
while (i) { | ||
hash = (hash * 33) ^ str.charCodeAt(--i); | ||
} | ||
/* JavaScript does bitwise operations (like XOR, above) on 32-bit signed | ||
* integers. Since we want the results to be always positive, convert the | ||
* signed int to an unsigned by doing an unsigned bitshift. */ | ||
return hash >>> 0; | ||
} | ||
var capRegex = /[A-Z]/g; | ||
var componentProps = { | ||
// class (preact) and className (React) are handled separately | ||
checked: true, | ||
children: true, | ||
href: true, | ||
id: true, | ||
name: true, | ||
placeholder: true, | ||
style: true, | ||
type: true, | ||
value: true, | ||
}; | ||
var pseudoelements = { | ||
after: true, | ||
before: true, | ||
placeholder: true, | ||
selection: true, | ||
}; | ||
var pseudoclasses = { | ||
active: true, | ||
checked: true, | ||
disabled: true, | ||
empty: true, | ||
enabled: true, | ||
focus: true, | ||
hover: true, | ||
invalid: true, | ||
link: true, | ||
required: true, | ||
target: true, | ||
valid: true, | ||
}; | ||
/** Props that are used internally and not passed on to the underlying component */ | ||
var skippedProps = { | ||
component: true, | ||
mediaQueries: true, | ||
props: true, | ||
}; | ||
var sameAxisPropNames = { | ||
paddingH: ['paddingLeft', 'paddingRight'], | ||
paddingV: ['paddingTop', 'paddingBottom'], | ||
marginH: ['marginLeft', 'marginRight'], | ||
marginV: ['marginTop', 'marginBottom'], | ||
}; | ||
function getStyleKeysForProps(props, classNamePropKey, pretty) { | ||
if (pretty === void 0) { | ||
pretty = false; | ||
} | ||
if (typeof props !== 'object' || props === null) { | ||
return null; | ||
} | ||
var propKeys = Object.keys(props).sort(); | ||
var keyCount = propKeys.length; | ||
if (keyCount === 0) { | ||
return null; | ||
} | ||
var mediaQueries = props.mediaQueries; | ||
var usesMediaQueries = false; | ||
var stylesByKey = {}; | ||
var styleKeyObj = { | ||
classNameKey: '', | ||
stylesByKey: stylesByKey, | ||
props: typeof props.props === 'object' ? _extends({}, props.props) : null, | ||
}; | ||
var classNameKey = ''; | ||
var animations; | ||
var seenMQs = {}; | ||
var mqSortKeys = {}; | ||
if (mediaQueries != null) { | ||
var idx = -1; | ||
for (var k in mediaQueries) { | ||
mqSortKeys[k] = '@' + (1000 + ++idx); | ||
} | ||
} | ||
for (var _idx = -1; ++_idx < keyCount; ) { | ||
var originalPropName = propKeys[_idx]; // separate known component props from style props | ||
if (componentProps.hasOwnProperty(originalPropName)) { | ||
(styleKeyObj.props = styleKeyObj.props || {})[originalPropName] = | ||
props[originalPropName]; | ||
continue; | ||
} | ||
if ( | ||
skippedProps.hasOwnProperty(originalPropName) || | ||
originalPropName === classNamePropKey || | ||
!props.hasOwnProperty(originalPropName) | ||
) { | ||
continue; | ||
} | ||
var propName = originalPropName; | ||
var propSansMQ = void 0; | ||
var pseudoelement = void 0; | ||
var pseudoclass = void 0; | ||
var mqKey = void 0; | ||
capRegex.lastIndex = 0; | ||
var splitIndex = 0; | ||
var prefix = | ||
capRegex.test(originalPropName) && | ||
capRegex.lastIndex > 1 && | ||
originalPropName.slice(0, capRegex.lastIndex - 1); // all /^on[A-Z]/ props get passed through to the underlying component | ||
if (prefix === 'on') { | ||
(styleKeyObj.props = styleKeyObj.props || {})[originalPropName] = | ||
props[originalPropName]; | ||
continue; | ||
} // check for media query prefix | ||
if ( | ||
prefix && | ||
mediaQueries != null && | ||
mediaQueries.hasOwnProperty(prefix) | ||
) { | ||
usesMediaQueries = true; | ||
mqKey = prefix; | ||
splitIndex = capRegex.lastIndex - 1; | ||
propSansMQ = | ||
originalPropName[splitIndex].toLowerCase() + | ||
originalPropName.slice(splitIndex + 1); | ||
prefix = | ||
capRegex.test(originalPropName) && | ||
propSansMQ.slice(0, capRegex.lastIndex - splitIndex - 1); | ||
} // check for pseudoelement prefix | ||
if (prefix && pseudoelements.hasOwnProperty(prefix)) { | ||
pseudoelement = prefix; | ||
splitIndex = capRegex.lastIndex - 1; | ||
prefix = | ||
capRegex.test(originalPropName) && | ||
originalPropName[splitIndex].toLowerCase() + | ||
originalPropName.slice(splitIndex + 1, capRegex.lastIndex - 1); | ||
} // check for pseudoclass prefix | ||
if (prefix && pseudoclasses.hasOwnProperty(prefix)) { | ||
pseudoclass = prefix; | ||
splitIndex = capRegex.lastIndex - 1; | ||
} // trim prefixes off propName | ||
if (splitIndex > 0) { | ||
propName = | ||
originalPropName[splitIndex].toLowerCase() + | ||
originalPropName.slice(splitIndex + 1); | ||
} | ||
var styleValue = props[originalPropName]; | ||
var space = pretty ? ' ' : ''; | ||
var colon = ':' + space; | ||
var newline = pretty ? '\n' : ''; | ||
var semicolon = ';' + newline; | ||
var indent = pretty ? ' ' : ''; | ||
if ( | ||
propName === 'animation' && | ||
styleValue && | ||
typeof styleValue === 'object' | ||
) { | ||
var animationValue = newline; | ||
for (var _k in styleValue) { | ||
var obj = styleValue[_k]; | ||
animationValue += _k + space + '{' + newline; | ||
for (var childPropName in obj) { | ||
var _value = dangerousStyleValue(childPropName, obj[childPropName]); | ||
animationValue += | ||
indent + | ||
hyphenateStyleName(childPropName) + | ||
colon + | ||
_value + | ||
semicolon; | ||
} | ||
animationValue += '}' + newline; | ||
} | ||
var animationKey = | ||
'jsxstyle_' + stringHash(animationValue).toString(36); | ||
propName = 'animationName'; | ||
styleValue = animationKey; | ||
animations = animations || {}; | ||
animations[animationKey] = animationValue; | ||
} else { | ||
styleValue = dangerousStyleValue(propName, props[originalPropName]); | ||
if (styleValue === '') { | ||
continue; | ||
} | ||
} | ||
var mediaQuery = mqKey && mediaQueries[mqKey]; | ||
var mqSortKey = mqKey && mqSortKeys[mqKey]; | ||
var key = | ||
'.' + | ||
(mqSortKey || '') + | ||
(pseudoclass ? ':' + pseudoclass : '') + | ||
(pseudoelement ? '::' + pseudoelement : ''); | ||
if (!stylesByKey.hasOwnProperty(key)) { | ||
stylesByKey[key] = { | ||
styles: newline, | ||
}; | ||
if (mediaQuery) { | ||
stylesByKey[key].mediaQuery = mediaQuery; | ||
} | ||
if (pseudoclass) { | ||
stylesByKey[key].pseudoclass = pseudoclass; | ||
} | ||
if (pseudoelement) { | ||
stylesByKey[key].pseudoelement = pseudoelement; | ||
} | ||
} | ||
if (mediaQuery) { | ||
seenMQs[mediaQuery] = seenMQs[mediaQuery] || ''; | ||
seenMQs[mediaQuery] += propSansMQ + ':' + styleValue + ';'; | ||
} else { | ||
classNameKey += originalPropName + ':' + styleValue + ';'; | ||
} | ||
var value = colon + styleValue + semicolon; | ||
var propArray = sameAxisPropNames[propName]; | ||
if (propArray) { | ||
stylesByKey[key].styles += | ||
indent + | ||
hyphenateStyleName(propArray[0]) + | ||
value + | ||
indent + | ||
hyphenateStyleName(propArray[1]) + | ||
value; | ||
} else { | ||
stylesByKey[key].styles += | ||
indent + hyphenateStyleName(propName) + value; | ||
} | ||
} // append media query key | ||
if (usesMediaQueries) { | ||
var mqKeys = Object.keys(seenMQs).sort(); | ||
for (var _idx2 = -1, len = mqKeys.length; ++_idx2 < len; ) { | ||
var _mediaQuery = mqKeys[_idx2]; | ||
classNameKey += '@' + _mediaQuery + '~' + seenMQs[_mediaQuery]; | ||
} | ||
} | ||
if (animations) { | ||
styleKeyObj.animations = animations; | ||
} | ||
styleKeyObj.classNameKey = classNameKey || null; | ||
return styleKeyObj; | ||
} | ||
function cannotInject() { | ||
throw new Error( | ||
'jsxstyle error: `injectOptions` must be called before any jsxstyle components mount.' | ||
); | ||
} | ||
function alreadyInjected() { | ||
throw new Error( | ||
'jsxstyle error: `injectOptions` should be called once and only once.' | ||
); | ||
} | ||
var getStringHash = function getStringHash(key) { | ||
return '_' + stringHash(key).toString(36); | ||
}; | ||
function getStyleCache() { | ||
var _classNameCache = {}; | ||
var getClassNameForKey = getStringHash; | ||
var onInsertRule; | ||
var pretty = false; | ||
var styleCache = { | ||
reset: function reset() { | ||
_classNameCache = {}; | ||
}, | ||
injectOptions: function injectOptions(options) { | ||
if (options) { | ||
if (options.getClassName) { | ||
getClassNameForKey = options.getClassName; | ||
} | ||
if (options.onInsertRule) { | ||
onInsertRule = options.onInsertRule; | ||
} | ||
if (options.pretty) { | ||
pretty = options.pretty; | ||
} | ||
} | ||
styleCache.injectOptions = alreadyInjected; | ||
}, | ||
getComponentProps: function getComponentProps(props, classNamePropKey) { | ||
if (classNamePropKey === void 0) { | ||
classNamePropKey = 'className'; | ||
} | ||
styleCache.injectOptions = cannotInject; | ||
var styleObj = getStyleKeysForProps(props, classNamePropKey, pretty); | ||
if (styleObj == null) { | ||
return null; | ||
} | ||
var key = styleObj.classNameKey; | ||
if (key && !_classNameCache.hasOwnProperty(key)) { | ||
_classNameCache[key] = getClassNameForKey(key, props); | ||
Object.keys(styleObj.stylesByKey) | ||
.sort() | ||
.forEach(function (k) { | ||
var selector = '.' + _classNameCache[key]; // prettier-ignore | ||
var _styleObj$stylesByKey = styleObj.stylesByKey[k], | ||
pseudoclass = _styleObj$stylesByKey.pseudoclass, | ||
pseudoelement = _styleObj$stylesByKey.pseudoelement, | ||
mediaQuery = _styleObj$stylesByKey.mediaQuery, | ||
styles = _styleObj$stylesByKey.styles; | ||
var rule = | ||
selector + | ||
(pseudoclass ? ':' + pseudoclass : '') + | ||
(pseudoelement ? '::' + pseudoelement : '') + | ||
(' {' + styles + '}'); | ||
if (mediaQuery) { | ||
rule = '@media ' + mediaQuery + ' { ' + rule + ' }'; | ||
} | ||
if ( | ||
onInsertRule && // if the function returns false, bail. | ||
onInsertRule(rule, props) === false | ||
) { | ||
return; | ||
} | ||
addStyleToHead(rule); | ||
}); | ||
} | ||
var animations = styleObj.animations; | ||
if (animations) { | ||
for (var animationKey in animations) { | ||
var rule = | ||
'@keyframes ' + | ||
animationKey + | ||
' {' + | ||
animations[animationKey] + | ||
'}'; | ||
if (!onInsertRule || onInsertRule(rule, props) !== false) { | ||
addStyleToHead(rule); | ||
for (var key in source) { | ||
if (Object.prototype.hasOwnProperty.call(source, key)) { | ||
target[key] = source[key]; | ||
} | ||
@@ -642,23 +247,10 @@ } | ||
var classNameProp = props[classNamePropKey]; | ||
var classNameForKey = key && _classNameCache[key]; | ||
var className = | ||
classNameForKey && classNameProp | ||
? classNameProp + ' ' + classNameForKey | ||
: classNameForKey || classNameProp || null; | ||
return target; | ||
}; | ||
var finalProps = _extends({}, styleObj.props); | ||
if (className) { | ||
finalProps[classNamePropKey] = className; | ||
} | ||
return finalProps; | ||
}, | ||
}; | ||
return styleCache; | ||
return _extends.apply(this, arguments); | ||
} | ||
// global flag makes subsequent calls of capRegex.test advance to the next match | ||
var capRegex$1 = /[A-Z]/g; | ||
var capRegex = /[A-Z]/g; | ||
var commonComponentProps = { | ||
@@ -676,3 +268,3 @@ // class (preact) and className (React) are handled separately | ||
}; | ||
var pseudoelements$1 = { | ||
var pseudoelements = { | ||
after: true, | ||
@@ -683,3 +275,3 @@ before: true, | ||
}; | ||
var pseudoclasses$1 = { | ||
var pseudoclasses = { | ||
active: true, | ||
@@ -700,3 +292,3 @@ checked: true, | ||
var skippedProps$1 = { | ||
var skippedProps = { | ||
component: true, | ||
@@ -754,3 +346,3 @@ mediaQueries: true, | ||
if ( | ||
skippedProps$1.hasOwnProperty(originalPropName) || | ||
skippedProps.hasOwnProperty(originalPropName) || | ||
originalPropName === classNamePropKey || | ||
@@ -766,8 +358,8 @@ !props.hasOwnProperty(originalPropName) | ||
var specificity = 0; | ||
capRegex$1.lastIndex = 0; | ||
capRegex.lastIndex = 0; | ||
var splitIndex = 0; | ||
var propNamePrefix = | ||
capRegex$1.test(originalPropName) && | ||
capRegex$1.lastIndex > 1 && | ||
originalPropName.slice(0, capRegex$1.lastIndex - 1); // all /^on[A-Z]/ props get passed through to the underlying component | ||
capRegex.test(originalPropName) && | ||
capRegex.lastIndex > 1 && | ||
originalPropName.slice(0, capRegex.lastIndex - 1); // all /^on[A-Z]/ props get passed through to the underlying component | ||
@@ -779,18 +371,18 @@ if (propNamePrefix === 'on') { | ||
if (propNamePrefix && pseudoelements$1[propNamePrefix]) { | ||
if (propNamePrefix && pseudoelements[propNamePrefix]) { | ||
pseudoelement = propNamePrefix; | ||
splitIndex = capRegex$1.lastIndex - 1; | ||
splitIndex = capRegex.lastIndex - 1; | ||
propNamePrefix = | ||
capRegex$1.test(originalPropName) && | ||
capRegex.test(originalPropName) && | ||
originalPropName[splitIndex].toLowerCase() + | ||
originalPropName.slice(splitIndex + 1, capRegex$1.lastIndex - 1); | ||
originalPropName.slice(splitIndex + 1, capRegex.lastIndex - 1); | ||
} // check for pseudoclass prefix | ||
if (propNamePrefix && pseudoclasses$1[propNamePrefix]) { | ||
if (propNamePrefix && pseudoclasses[propNamePrefix]) { | ||
pseudoclass = propNamePrefix; | ||
splitIndex = capRegex$1.lastIndex - 1; | ||
splitIndex = capRegex.lastIndex - 1; | ||
propNamePrefix = | ||
capRegex$1.test(originalPropName) && | ||
capRegex.test(originalPropName) && | ||
originalPropName[splitIndex].toLowerCase() + | ||
originalPropName.slice(splitIndex + 1, capRegex$1.lastIndex - 1); | ||
originalPropName.slice(splitIndex + 1, capRegex.lastIndex - 1); | ||
} // check if we need to bump specificity | ||
@@ -1004,2 +596,96 @@ | ||
/* tslint:disable no-bitwise */ | ||
// thx darksky: https://git.io/v9kWO | ||
function stringHash(str) { | ||
var hash = 5381; | ||
var i = str.length; | ||
while (i) { | ||
hash = (hash * 33) ^ str.charCodeAt(--i); | ||
} | ||
/* JavaScript does bitwise operations (like XOR, above) on 32-bit signed | ||
* integers. Since we want the results to be always positive, convert the | ||
* signed int to an unsigned by doing an unsigned bitshift. */ | ||
return hash >>> 0; | ||
} | ||
function cannotInject() { | ||
throw new Error( | ||
'jsxstyle error: `injectOptions` must be called before any jsxstyle components mount.' | ||
); | ||
} | ||
function alreadyInjected() { | ||
throw new Error( | ||
'jsxstyle error: `injectOptions` should be called once and only once.' | ||
); | ||
} | ||
var getStringHash = function getStringHash(key) { | ||
return '_' + stringHash(key).toString(36); | ||
}; | ||
function getStyleCache() { | ||
var _classNameCache = {}; | ||
var getClassNameForKey = getStringHash; | ||
var onInsertRule; | ||
var memoizedGetClassNameForKey = function memoizedGetClassNameForKey(key) { | ||
if (!_classNameCache[key]) { | ||
_classNameCache[key] = getClassNameForKey(key); | ||
} | ||
return _classNameCache[key]; | ||
}; | ||
var styleCache = { | ||
reset: function reset() { | ||
_classNameCache = {}; | ||
}, | ||
injectOptions: function injectOptions(options) { | ||
if (options) { | ||
if (options.getClassName) { | ||
getClassNameForKey = options.getClassName; | ||
} | ||
if (options.onInsertRule) { | ||
onInsertRule = options.onInsertRule; | ||
} | ||
} | ||
styleCache.injectOptions = alreadyInjected; | ||
}, | ||
getComponentProps: function getComponentProps(props, classNamePropKey) { | ||
if (classNamePropKey === void 0) { | ||
classNamePropKey = 'className'; | ||
} | ||
styleCache.injectOptions = cannotInject; | ||
var componentProps = processProps( | ||
props, | ||
classNamePropKey, | ||
memoizedGetClassNameForKey | ||
); | ||
if (!componentProps) { | ||
return null; | ||
} | ||
componentProps.rules.forEach(function (rule) { | ||
if ( | ||
onInsertRule && // if the function returns false, bail. | ||
onInsertRule(rule, props) === false | ||
) { | ||
return; | ||
} | ||
addStyleToHead(rule); | ||
}); | ||
return componentProps.props; | ||
}, | ||
}; | ||
return styleCache; | ||
} | ||
exports.addStyleToHead = addStyleToHead; | ||
@@ -1009,7 +695,4 @@ exports.componentStyles = componentStyles; | ||
exports.getStyleCache = getStyleCache; | ||
exports.getStyleKeysForProps = getStyleKeysForProps; | ||
exports.hyphenateStyleName = hyphenateStyleName; | ||
exports.processProps = processProps; | ||
exports.pseudoclasses = pseudoclasses; | ||
exports.pseudoelements = pseudoelements; | ||
exports.stringHash = stringHash; | ||
@@ -1016,0 +699,0 @@ |
{ | ||
"name": "jsxstyle-utils", | ||
"version": "0.0.0-canary-20217216441", | ||
"version": "0.0.0-canary-202172171529", | ||
"description": "Utilities used by jsxstyle and tooling built for jsxstyle", | ||
@@ -5,0 +5,0 @@ "author": "Pete Hunt", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
72341
27
2003