Created
November 17, 2009 12:30
-
-
Save quackingduck/236876 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // 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