Skip to content

Commit

Permalink
Export component specifically for rendering html
Browse files Browse the repository at this point in the history
  • Loading branch information
bryceosterhaus committed May 26, 2017
1 parent d49b2cc commit a8bfe2e
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
32 changes: 32 additions & 0 deletions packages/metal-jsx/src/DangerouslySetHTML.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';

import Component from './JSXComponent';
import { Config } from 'metal-state';

/**
* JSXComponent that renders html passed in.
* @class
*/
class DangerouslySetHTML extends Component {
/**
* @return {Component}
*/
render() {
const {content, tag} = this.props;

IncrementalDOM.elementOpen(tag, null, null);

const node = IncrementalDOM.elementClose(tag);

node.innerHTML = content;

return node;
}
}

DangerouslySetHTML.PROPS = {
content: Config.string(),
tag: Config.string().value('span'),
};

export default DangerouslySetHTML;
3 changes: 2 additions & 1 deletion packages/metal-jsx/src/JSXComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import './iDOMHelpers';
import { validators, Config } from 'metal-state';
import Component from 'metal-component';
import DangerouslySetHTML from './DangerouslySetHTML';
import IncrementalDomRenderer from 'metal-incremental-dom';
import JSXDataManager from './JSXDataManager';
import JSXRenderer from './JSXRenderer';
Expand Down Expand Up @@ -38,4 +39,4 @@ JSXComponent.DATA_MANAGER = JSXDataManager;
JSXComponent.RENDERER = JSXRenderer;

export default JSXComponent;
export { validators, Config, JSXComponent };
export { DangerouslySetHTML, validators, Config, JSXComponent };
30 changes: 30 additions & 0 deletions packages/metal-jsx/test/DangerouslySetHTML.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

import DangerouslySetHTML from '../src/DangerouslySetHTML';

describe('JSXComponent', function() {
var component;

afterEach(function() {
if (component) {
component.dispose();
}
});

it('should render', function() {
component = new DangerouslySetHTML();
assert.strictEqual('SPAN', component.element.tagName);
});

it('should render with custom tag', function() {
component = new DangerouslySetHTML({tag: 'div'});
assert.strictEqual('DIV', component.element.tagName);
});

it('should render with html content', function() {
var content = '<h2>hello</h2><div><span>world</span></div>';

component = new DangerouslySetHTML({content: content});
assert.strictEqual(content, component.element.innerHTML);
});
});

0 comments on commit a8bfe2e

Please sign in to comment.