Skip to content

Instantly share code, notes, and snippets.

@quackingduck
Created November 17, 2009 12:30
Show Gist options
  • Select an option

  • Save quackingduck/236876 to your computer and use it in GitHub Desktop.

Select an option

Save quackingduck/236876 to your computer and use it in GitHub Desktop.
// FancyNodeBuilder
$ = (function(jquery) {
function jqueryWithFancyTagBuilder() {
if (arguments.length == 1 && isTagBuilderString(arguments[0]))
return jquery(compileTag(arguments[0]));
else
return jquery.apply(null, arguments);
}
var builderPattern = /^%(.+?)(#.+?)?(\..+?)?$/;
function isTagBuilderString(obj) {
return typeof obj === 'string' && builderPattern.test(obj);
}
function compileTag(str) {
var match = str.match(builderPattern);
var tag = match[1], id = match[2], classes = match[3];
id = id ? ' id="' + id.slice(1,id.length) + '"' : '';
classes = classes ? ' class="' + classes.slice(1,classes.length).split('.').join(' ') + '"' : '';
return '<'+tag+id+classes+'>';
}
jquery.compileTag = compileTag; // for testing
jquery.extend(jqueryWithFancyTagBuilder,jquery);
return jqueryWithFancyTagBuilder;
})($);
$('%div#foo.bar.baz') // compiles to:
$('<div id="foo" class="bar baz">');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment