Parcourir la source

Listing 6: dangerouslySetInnerHTML.

Frederic G. MARAND il y a 8 ans
Parent
commit
4394427f35
2 fichiers modifiés avec 6 ajouts et 2 suppressions
  1. 1 1
      .eslintrc.js
  2. 5 1
      public/scripts/tutorial.js

+ 1 - 1
.eslintrc.js

@@ -189,7 +189,7 @@ module.exports = {
     "no-ternary": 0, // disallow the use of ternary operators (off by default)
     "no-trailing-spaces": 1, // disallow trailing whitespace at the end of lines
     "no-underscore-dangle": 1, // disallow dangling underscores in identifiers
-    "object-curly-spacing": [2, "never"],
+    "object-curly-spacing": [2, "always"],
     "one-var": [2, "never"], // allow just one var statement per function (off by default)
     "operator-assignment": [1, "never"], // require assignment operator shorthand where possible or prohibit it entirely (off by default)
     "padded-blocks": [1, "never"], // enforce padding within blocks (off by default)

+ 5 - 1
public/scripts/tutorial.js

@@ -1,11 +1,15 @@
 let Comment = React.createClass({
+  rawMarkup: function () {
+    let rawMarkup = marked(this.props.children.toString(), { sanitize: true });
+    return { __html: rawMarkup };
+  },
   render: function () {
     return (
       <div className="comment">
         <h2 className="commentAuthor">
           {this.props.author}
         </h2>
-        {marked(this.props.children.toString())}
+        <span dangerouslySetInnerHTML={this.rawMarkup()} />
       </div>
     );
   }