Skip to content

Instantly share code, notes, and snippets.

@sergeche
Created August 10, 2012 12:48
Show Gist options
  • Save sergeche/3313962 to your computer and use it in GitHub Desktop.
Save sergeche/3313962 to your computer and use it in GitHub Desktop.
Trim b- prefix
// простая обётка, которая выполняет код в окружении ядра Zen Coding
// проедоставляет функции require (импорт модулей) и _ (библиотека underscore.js)
zen_coding.exec(function(require, _) {
var rePrefix = /\bb\-/g;
/**
* Рекурсивная функция, которая удаляет префикс b- у элемента и его потомков
* @param {AbbreviationNode} node
*/
function trimPrefix(node) {
var className = node.attribute('class');
if (className) {
node.attribute('class', className.replace(rePrefix, ''));
}
_.each(node.children, trimPrefix);
}
// регистрируем новый фильтр
require('filters').add('bembem', function process(tree) {
// рекурсивно определяем родительский элемент, в котором находится
// префикс b-. Это может быть как контекстный элемент,
// в котором разворачивается аббревиатура, либо какой-то
// дочерний элемент
if (rePrefix.test(tree.attribute('class') || '')) {
_.each(tree.children, trimPrefix);
} else {
_.each(tree.children, process);
}
return tree;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment