Skip to content

Commit 1f9071c

Browse files
author
kiran
committed
Add the option to pass required element in which code should render
1 parent 49c6404 commit 1f9071c

File tree

6 files changed

+89
-17
lines changed

6 files changed

+89
-17
lines changed

.eslintrc

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"rules": {
3+
"no-extra-parens": 0,
4+
"react/jsx-uses-vars": 1,
5+
"global-strict": 1,
6+
"quotes": [2, "single"],
7+
"no-underscore-dangle": 0,
8+
"space-infix-ops": 0,
9+
"no-alert": 0
10+
},
11+
"ecmaFeatures": {
12+
"jsx": true,
13+
},
14+
"env": {
15+
"node": true,
16+
"browser": true,
17+
"es6": true,
18+
"jasmine": true
19+
},
20+
"parser": "babel-eslint",
21+
"plugins": [
22+
"react"
23+
]
24+
}

karma.conf.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ module.exports = function(config) {
1414

1515
// list of files / patterns to load in the browser
1616
files: [
17-
'node_modules/es5-shim/es5-shim.js',
18-
'node_modules/react/dist/react-with-addons.js',
19-
'test/**/*.jsx',
17+
'node_modules/es5-shim/es5-shim.js',
18+
'node_modules/react/dist/react-with-addons.js',
19+
'test/**/*.js',
2020
],
2121

2222

@@ -27,17 +27,20 @@ module.exports = function(config) {
2727
// preprocess matching files before serving them to the browser
2828
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
2929
preprocessors: {
30-
'test/**/*.jsx': ['webpack']
30+
'test/**/*.js': ['webpack'],
3131
},
3232

3333
webpack: {
3434
module: {
3535
loaders: [
36-
{test: /\.(js|jsx)$/, loaders: ['babel']}
36+
{test: /\.(js|jsx)$/, loaders: ['babel']},
3737
]
3838
},
3939
externals: {
4040
react: 'React'
41+
},
42+
resolve: {
43+
root: __dirname
4144
}
4245
},
4346
webpackServer: {

package.json

+7-4
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,30 @@
3030
"autoprefixer-core": "^4.0.2",
3131
"babel": "^5.6.14",
3232
"babel-core": "^5.6.18",
33+
"babel-eslint": "^6.0.2",
3334
"babel-loader": "^5.3.1",
3435
"es5-shim": "^4.1.7",
36+
"eslint": "^2.8.0",
37+
"eslint-plugin-react": "^4.3.0",
3538
"gulp": "^3.8.10",
3639
"gulp-ruby-sass": "^0.7.1",
3740
"highlight.js": "^8.6.0",
3841
"html-loader": "^0.2.3",
3942
"jasmine-core": "^2.3.4",
4043
"jsx-loader": "^0.12.2",
4144
"karma": "^0.13.3",
45+
"karma-jasmine": "^0.3.6",
46+
"karma-phantomjs-launcher": "^0.2.1",
4247
"karma-webpack": "^1.7.0",
4348
"markdown-loader": "^0.1.2",
4449
"multiline": "^1.0.1",
4550
"node-libs-browser": "^0.5.2",
4651
"phantomjs": "^1.9.17",
4752
"raw-loader": "^0.5.1",
4853
"react": "^0.14.0",
54+
"react-addons-test-utils": "^0.14.3",
4955
"webpack": "^1.12.2",
50-
"webpack-dev-server": "^1.6.6",
51-
"karma-jasmine": "^0.3.6",
52-
"karma-phantomjs-launcher": "^0.2.1",
53-
"react-addons-test-utils": "^0.14.3"
56+
"webpack-dev-server": "^1.6.6"
5457
},
5558
"dependencies": {
5659
"highlight.js": "^8.4.x",

src/index.jsx renamed to src/index.js

+18-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ var Highlight = React.createClass({
66
getDefaultProps: function () {
77
return {
88
innerHTML: false,
9-
className: null
9+
className: null,
10+
element: null,
1011
};
1112
},
1213
componentDidMount: function () {
@@ -25,10 +26,24 @@ var Highlight = React.createClass({
2526
}
2627
},
2728
render: function () {
29+
var Element = this.props.element ? React.DOM[this.props.element]: null;
30+
2831
if (this.props.innerHTML) {
29-
return <div dangerouslySetInnerHTML={{__html: this.props.children}} className={this.props.className || null}></div>;
32+
if (!Element) {
33+
Element = React.DOM.div
34+
}
35+
return Element({
36+
dangerouslySetInnerHTML: {__html: this.props.children},
37+
className: this.props.className || null
38+
}, null);
3039
} else {
31-
return <pre><code className={this.props.className}>{this.props.children}</code></pre>;
40+
if (Element) {
41+
return Element({
42+
className:this.props.className
43+
}, this.props.children);
44+
} else {
45+
return <pre><code className={this.props.className}>{this.props.children}</code></pre>;
46+
}
3247
}
3348
}
3449
});

src/optimized.jsx renamed to src/optimized.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,24 @@ var Highlight = React.createClass({
3333
}
3434
},
3535
render: function () {
36+
var Element = this.props.element ? React.DOM[this.props.element]: null;
37+
3638
if (this.props.innerHTML) {
37-
return <div dangerouslySetInnerHTML={{__html: this.props.children}} className={this.props.className || null}></div>;
39+
if (!Element) {
40+
Element = React.DOM.div
41+
}
42+
return Element({
43+
dangerouslySetInnerHTML: {__html: this.props.children},
44+
className: this.props.className || null
45+
}, null);
3846
} else {
39-
return <pre><code className={this.props.className}>{this.props.children}</code></pre>;
47+
if (Element) {
48+
return Element({
49+
className:this.props.className
50+
}, this.props.children);
51+
} else {
52+
return <pre><code className={this.props.className}>{this.props.children}</code></pre>;
53+
}
4054
}
4155
}
4256
});

test/highlight.test.jsx renamed to test/highlight.test.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var Highlight = require('../');
1+
var Highlight = require('../src');
22
var ReactDOM = require('react-dom');
33
var TestUtils = require('react-addons-test-utils');
44
var ReactDOMServer = require('react-dom/server');
@@ -25,14 +25,27 @@ describe('highlight', function() {
2525
expect(text).toBe('<pre><code class="html">Some text</code></pre>');
2626
});
2727

28+
it('should render children in span', function() {
29+
var text = ReactDOMServer.renderToStaticMarkup(
30+
<Highlight element='span'>Some text</Highlight>
31+
);
32+
expect(text).toBe('<span>Some text</span>');
33+
});
34+
35+
it('should render innerHTML in span', function() {
36+
var text = ReactDOMServer.renderToStaticMarkup(
37+
<Highlight innerHTML={true} element='span'>Some text</Highlight>
38+
);
39+
expect(text).toBe('<span>Some text</span>');
40+
});
41+
2842
it('should accept innerHTML prop', function() {
2943
var text = TestUtils.renderIntoDocument(
3044
<Highlight innerHTML={true}>{"<div>Sometext</div>"}</Highlight>
3145
);
3246
expect(ReactDOM.findDOMNode(text).textContent).toBe('Sometext');
3347
});
3448

35-
});
36-
3749

3850

51+
});

0 commit comments

Comments
 (0)