Skip to content

Commit

Permalink
Add a filter for jsx
Browse files Browse the repository at this point in the history
For now it just transforms class= to className=
and for= to htmlFor=
  • Loading branch information
mrmurphy committed Feb 9, 2015
1 parent 268b43c commit 3b9ef13
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
34 changes: 34 additions & 0 deletions lib/filter/jsx.js
@@ -0,0 +1,34 @@
/**
* Changes `class=` to `className=` and 'for=' to `htmlFor=`
* for the sake of JSX!
*/
if (typeof module === 'object' && typeof define !== 'function') {
var define = function (factory) {
module.exports = factory(require, exports, module);
};
}

define(function(require, exports, module) {
var tokenMap = {
'class=': 'className=',
'for=': 'htmlFor='
};

function replaceTokens(str) {
Object.keys(tokenMap).forEach(function(k){
str = str.replace(k, tokenMap[k]);
})
return str
}

return function process(tree) {
tree.children.forEach(function(item) {
item.start = replaceTokens(item.start);
item.end = replaceTokens(item.end);
item.content = replaceTokens(item.content);
process(item);
});

return tree;
};
});
1 change: 1 addition & 0 deletions lib/filter/main.js
Expand Up @@ -17,6 +17,7 @@ define(function(require, exports, module) {
html: require('./html'),
haml: require('./haml'),
jade: require('./jade'),
jsx: require('./jsx'),
slim: require('./slim'),
xsl: require('./xsl'),
css: require('./css'),
Expand Down
6 changes: 6 additions & 0 deletions lib/snippets.json
Expand Up @@ -920,6 +920,12 @@
"profile": "xml"
},

"jsx": {
"filters": "html, jsx",
"extends": "html",
"profile": "xml"
},

"slim": {
"filters": "slim",
"extends": "html",
Expand Down
15 changes: 15 additions & 0 deletions test/filters.js
Expand Up @@ -204,6 +204,21 @@ describe('Filters', function() {
});
});

describe('JSX', function() {
it('should work', function() {
var oldSyntax = editor.getSyntax();
var oldProfile = editor.getProfileName();
editor.setSyntax('jsx');
editor.setProfileName('xml');

expand('.foo>input[name="bob"]+label[for="bob"]|html|jsx');
assert.equal(editor.getContent(), '<div className="foo">\n\t<input type="text" name="bob"/>\n\t<label htmlFor="bob"></label>\n</div>');

editor.setSyntax(oldSyntax);
editor.setProfileName(oldProfile);
});
});

describe('Slim', function() {
it('should work', function() {
var oldSyntax = editor.getSyntax();
Expand Down

0 comments on commit 3b9ef13

Please sign in to comment.