Mirror: CSS prefixing helpers in less than 1KB ๐ŸŒˆ

Update prefixValue to allow full input

Changed files
+26 -18
scripts
src
+24 -13
scripts/generate-prefix-map.js
··· 83 !properties.some(({ name }) => name === x) 84 )); 85 86 /** Lists each prefixed property with the minimum substring that is needed to uniquely identity it */ 87 - const prefixPatterns = properties 88 - .map(prop => { 89 - let name = prop.name; 90 - for (let i = 2, l = name.length; i < l; i++) { 91 - const substr = name.slice(0, i); 92 - // Check for any name that conflicts with the substring in all known CSS properties 93 - if (stablePropertyNames.every(x => x === name || !x.startsWith(substr))) { 94 - name = substr; 95 - break; 96 - } 97 - } 98 99 - return { ...prop, name }; 100 - }); 101 102 /** Accepts a filter and builds a list of names in `prefixPatterns` */ 103 const reducePrefixes = (filter = x => !!x) => { ··· 112 }); 113 }; 114 115 const buildRegex = groups => `^(${groups.join('|')})`; 116 117 // Create all prefix sets for each prefix ··· 119 const mozPrefixes = buildRegex(reducePrefixes(x => x.moz)); 120 const webkitPrefixes = buildRegex(reducePrefixes(x => x.webkit)); 121 122 module.exports = ` 123 var msPrefixRe = /${msPrefixes}/; 124 var mozPrefixRe = /${mozPrefixes}/; 125 var webkitPrefixRe = /${webkitPrefixes}/; 126 `.trim();
··· 83 !properties.some(({ name }) => name === x) 84 )); 85 86 + /** Finds the minimum starting substring that uniquely identifier a property out of all known CSS properties */ 87 + const findMinimumSubstr = name => { 88 + for (let i = 2, l = name.length; i < l; i++) { 89 + const substr = name.slice(0, i); 90 + // Check for any name that conflicts with the substring in all known CSS properties 91 + if (stablePropertyNames.every(x => x === name || !x.startsWith(substr))) { 92 + return substr; 93 + } 94 + } 95 + 96 + return name; 97 + }; 98 + 99 /** Lists each prefixed property with the minimum substring that is needed to uniquely identity it */ 100 + const prefixPatterns = properties.map(prop => ({ 101 + ...prop, 102 + name: findMinimumSubstr(prop.name) 103 + })); 104 105 + /** For Webkit prefixes, these properties will need some values to be prefixed */ 106 + const prefixValuePatterns = ['position', 'background-clip'].map(findMinimumSubstr); 107 108 /** Accepts a filter and builds a list of names in `prefixPatterns` */ 109 const reducePrefixes = (filter = x => !!x) => { ··· 118 }); 119 }; 120 121 + /** Creates a regex matching property names */ 122 const buildRegex = groups => `^(${groups.join('|')})`; 123 124 // Create all prefix sets for each prefix ··· 126 const mozPrefixes = buildRegex(reducePrefixes(x => x.moz)); 127 const webkitPrefixes = buildRegex(reducePrefixes(x => x.webkit)); 128 129 + // Create a regex for webkit value transforms 130 + const webkitValuePrefix = buildRegex(prefixValuePatterns); 131 + 132 module.exports = ` 133 var msPrefixRe = /${msPrefixes}/; 134 var mozPrefixRe = /${mozPrefixes}/; 135 var webkitPrefixRe = /${webkitPrefixes}/; 136 + var webkitValuePrefixRe = /${webkitValuePrefix}/; 137 `.trim();
+2 -5
src/index.js
··· 13 }; 14 15 export const prefixValue = (prop, value) => { 16 - if ( 17 - (prop === 'position' && value === 'sticky') || 18 - (prop === 'background-clip' && value === 'text') 19 - ) { 20 - return `-webkit-${value}, ${value}`; 21 } 22 23 return value;
··· 13 }; 14 15 export const prefixValue = (prop, value) => { 16 + if (webkitValuePrefixRe.test(prop)) { 17 + return value.replace(/(sticky|text)/, '-webkit-$1, $1'); 18 } 19 20 return value;