Merge pull request #204 from JsSucks/normalizer

Fix class normalizer
This commit is contained in:
Alexei Stukov 2018-08-09 09:07:06 +03:00 committed by GitHub
commit 8f95ba290a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 41 additions and 9 deletions

View File

@ -10,28 +10,40 @@
import { Module, WebpackModules } from 'modules';
const normalizedPrefix = 'da';
const randClass = new RegExp(`^(?!${normalizedPrefix}-)((?:[A-Za-z]|[0-9]|-)+)-(?:[A-Za-z]|[0-9]|-|_){6}$`);
export default class ClassNormaliser extends Module {
init() {
this.patchClassModules(WebpackModules.getModule(this.moduleFilter, false));
this.patchClassModules(WebpackModules.getModule(this.moduleFilter.bind(this), false));
this.normalizeElement(document.querySelector('#app-mount'));
}
patchClassModules(modules) {
for (let module of modules) {
this.patchClassModule('da', module);
this.patchClassModule(normalizedPrefix, module);
}
}
shouldIgnore(value) {
if (!isNaN(value)) return true;
if (value.endsWith('px') || value.endsWith('ch') || value.endsWith('em') || value.endsWith('ms')) return true;
if (value.startsWith('rgba')) return true;
if (value.includes('calc(')) return true;
return false;
}
moduleFilter(module) {
if (typeof module !== 'object' || Array.isArray(module)) return false;
if (Array.isArray(module)) return false;
if (module.__esModule) return false;
if (!Object.keys(module).length) return false;
for (let baseClassName in module) {
if (typeof module[baseClassName] !== 'string') return false;
if (module[baseClassName].split('-').length === 1) return false;
const alphaNumeric = module[baseClassName].split(/-(.+)/)[1].split(' ')[0];
if (alphaNumeric.length !== 6) return false;
const value = module[baseClassName];
if (typeof value !== 'string') return false;
if (this.shouldIgnore(value)) continue;
if (value.split('-').length === 1) return false;
if (!randClass.test(value.split(' ')[0])) return false;
}
return true;
@ -39,8 +51,28 @@ export default class ClassNormaliser extends Module {
patchClassModule(componentName, classNames) {
for (let baseClassName in classNames) {
const normalised = baseClassName.split('-')[0].replace(/[A-Z]/g, m => `-${m}`).toLowerCase();
classNames[baseClassName] += ` ${componentName}-${normalised}`;
const value = classNames[baseClassName];
if (this.shouldIgnore(value)) continue;
const classList = value.split(' ');
for (let normalClass of classList) {
const match = normalClass.match(randClass)[1];
if (!match) continue; // Shouldn't ever happen since they passed the moduleFilter, but you never know
const camelCase = match.split('-').map((s, i) => i ? s[0].toUpperCase() + s.slice(1) : s).join('');
classNames[baseClassName] += ` ${componentName}-${camelCase}`;
}
}
}
normalizeElement(element) {
if (!(element instanceof Element)) return;
if (element.children && element.children.length) this.normalizeElement(element.children[0]);
if (element.nextElementSibling) this.normalizeElement(element.nextElementSibling);
const classes = element.classList;
for (let c = 0, clen = classes.length; c < clen; c++) {
if (!randClass.test(classes[c])) continue;
const match = classes[c].match(randClass)[1];
const newClass = match.split('-').map((s, i) => i ? s[0].toUpperCase() + s.slice(1) : s).join('');
element.classList.add(`${normalizedPrefix}-${newClass}`);
}
}