Created
August 10, 2012 12:48
-
-
Save sergeche/3313962 to your computer and use it in GitHub Desktop.
Trim b- prefix
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
// простая обётка, которая выполняет код в окружении ядра 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