Skip to content

Commit

Permalink
feat(html): smart quote for attributes (#5590)
Browse files Browse the repository at this point in the history
  • Loading branch information
ikatyang authored Dec 5, 2018
1 parent 525c076 commit cd28c22
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 15 deletions.
39 changes: 26 additions & 13 deletions src/language-html/printer-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const {
softline
} = builders;
const {
countChars,
countParents,
dedentString,
forceBreakChildren,
Expand All @@ -36,7 +37,8 @@ const {
replaceDocNewlines,
replaceNewlines,
shouldNotPrintClosingTag,
shouldPreserveContent
shouldPreserveContent,
unescapeQuoteEntities
} = require("./utils");
const preprocess = require("./preprocess");
const assert = require("assert");
Expand Down Expand Up @@ -325,19 +327,31 @@ function genericPrint(path, options, print) {
printClosingTagSuffix(node, options)
]);
}
case "attribute":
case "attribute": {
if (node.value === null) {
return node.rawName;
}
const value = unescapeQuoteEntities(node.value);
const singleQuoteCount = countChars(value, "'");
const doubleQuoteCount = countChars(value, '"');
const quote = singleQuoteCount < doubleQuoteCount ? "'" : '"';
return concat([
node.rawName,
node.value === null
? ""
: concat([
'="',
concat(
replaceNewlines(node.value.replace(/"/g, "&quot;"), literalline)
),
'"'
])
concat([
"=",
quote,
concat(
replaceNewlines(
quote === '"'
? value.replace(/"/g, "&quot;")
: value.replace(/'/g, "&apos;"),
literalline
)
),
quote
])
]);
}
case "yaml":
case "toml":
return node.raw;
Expand Down Expand Up @@ -892,8 +906,7 @@ function getTextValueParts(node, value = node.value) {
function printEmbeddedAttributeValue(node, originalTextToDoc, options) {
const isKeyMatched = patterns =>
new RegExp(patterns.join("|")).test(node.fullName);
const getValue = () =>
node.value.replace(/&quot;/g, '"').replace(/&apos;/g, "'");
const getValue = () => unescapeQuoteEntities(node.value);

let shouldHug = false;

Expand Down
18 changes: 17 additions & 1 deletion src/language-html/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -602,10 +602,25 @@ function shouldNotPrintClosingTag(node, options) {
);
}

function countChars(text, char) {
let counter = 0;
for (let i = 0; i < text.length; i++) {
if (text[i] === char) {
counter++;
}
}
return counter;
}

function unescapeQuoteEntities(text) {
return text.replace(/&apos;/g, "'").replace(/&quot;/g, '"');
}

module.exports = {
HTML_ELEMENT_ATTRIBUTES,
HTML_TAGS,
canHaveInterpolation,
countChars,
countParents,
dedentString,
forceBreakChildren,
Expand Down Expand Up @@ -633,5 +648,6 @@ module.exports = {
replaceDocNewlines,
replaceNewlines,
shouldNotPrintClosingTag,
shouldPreserveContent
shouldPreserveContent,
unescapeQuoteEntities
};
24 changes: 23 additions & 1 deletion tests/html_attributes/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,29 @@ printWidth: 80
<img src="test.png" alt='John "ShotGun" Nelson'>
=====================================output=====================================
<img src="test.png" alt="John &quot;ShotGun&quot; Nelson" />
<img src="test.png" alt='John "ShotGun" Nelson' />
================================================================================
`;
exports[`smart-quotes.html 1`] = `
====================================options=====================================
parsers: ["html"]
printWidth: 80
| printWidth
=====================================input======================================
<div
smart-quotes='123 " 456'
smart-quotes="123 ' 456"
smart-quotes='123 &apos;&quot; 456'
></div>
=====================================output=====================================
<div
smart-quotes='123 " 456'
smart-quotes="123 ' 456"
smart-quotes="123 '&quot; 456"
></div>
================================================================================
`;
Expand Down
5 changes: 5 additions & 0 deletions tests/html_attributes/smart-quotes.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div
smart-quotes='123 " 456'
smart-quotes="123 ' 456"
smart-quotes='123 &apos;&quot; 456'
></div>

0 comments on commit cd28c22

Please sign in to comment.