diff --git a/src/printer.js b/src/printer.js index 9e41a6a39..d8126378e 100644 --- a/src/printer.js +++ b/src/printer.js @@ -1038,8 +1038,18 @@ function genericPrintNoParens(path, options, print) { case "JSXAttribute": parts.push(path.call(print, "name")); - if (n.value) - parts.push("=", path.call(print, "value")); + if (n.value) { + let res; + if ( + (n.value.type === 'StringLiteral' || n.value.type === 'Literal') && + typeof n.value.value === 'string' + ) { + res = '"' + util.htmlEscapeInsideDoubleQuote(n.value.value) + '"'; + } else { + res = path.call(print, "value"); + } + parts.push("=", res); + } return concat(parts); case "JSXIdentifier": diff --git a/src/util.js b/src/util.js index e10c75517..42bab63f6 100644 --- a/src/util.js +++ b/src/util.js @@ -311,3 +311,16 @@ function setLocEnd(node, index) { } } util.setLocEnd = setLocEnd; + +// http://stackoverflow.com/a/7124052 +function htmlEscapeInsideDoubleQuote(str) { + return str + .replace(/&/g, '&') + .replace(/"/g, '"'); +// Intentionally disable the following since it is safe inside of a +// double quote context +// .replace(/'/g, ''') +// .replace(//g, '>'); +} +util.htmlEscapeInsideDoubleQuote = htmlEscapeInsideDoubleQuote; diff --git a/tests/jsx/__snapshots__/jsfmt.spec.js.snap b/tests/jsx/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 000000000..0603c8a21 --- /dev/null +++ b/tests/jsx/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,10 @@ +exports[`test quotes.js 1`] = ` +"