Skip to content

Commit

Permalink
Improved JSX support
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeche committed Apr 1, 2015
1 parent 8434453 commit 668ca33
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 19 deletions.
30 changes: 13 additions & 17 deletions lib/filter/jsx.js
@@ -1,34 +1,30 @@
/**
* Changes `class=` to `className=` and 'for=' to `htmlFor=`
* for the sake of JSX!
*/
* A filter for React.js (JSX):
* ranames attributes like `class` and `for`
* for proper representation in 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='
var attrMap = {
'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);
item._attributes.forEach(function(attr) {
if (attr.name in attrMap) {
attr.name = attrMap[attr.name]
}
});
process(item);
});

return tree;
};
});
});
2 changes: 1 addition & 1 deletion lib/snippets.json
Expand Up @@ -921,7 +921,7 @@
},

"jsx": {
"filters": "html, jsx",
"filters": "jsx, html",
"extends": "html",
"profile": "xml"
},
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -28,7 +28,7 @@
"gulp-rename": "^1.2.0",
"gulp-streamify": "0.0.5",
"gulp-uglify": "^1.0.1",
"mocha": "~1.13.0",
"mocha": "^2.2.1",
"through2": "^0.6.3",
"vinyl-source-stream": "^1.0.0"
}
Expand Down

0 comments on commit 668ca33

Please sign in to comment.