Skip to content

Commit 869e35e

Browse files
authored
do not encode entities in attribute values (#150)
1 parent 2687215 commit 869e35e

File tree

4 files changed

+14
-8
lines changed

4 files changed

+14
-8
lines changed

src/lib/elements.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export const inlineElements: TagName[] = [
7676
/**
7777
* HTML attributes that we may safely reformat (trim whitespace, add or remove newlines)
7878
*/
79-
export const formattableAttributes = [
79+
export const formattableAttributes: string[] = [
8080
// None at the moment
8181
// Prettier HTML does not format attributes at all
8282
// and to be consistent we leave this array empty for now

src/print/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
isOrCanBeConvertedToShorthand,
1919
getNextNode,
2020
getNodeEnd,
21+
getUnencodedText
2122
} from './node-helpers';
2223
import {
2324
isLine,
@@ -146,7 +147,7 @@ export function print(path: FastPath, options: ParserOptions, print: PrintFn): D
146147
* allow for flexible grouping of HTML tags in a particular indentation level,
147148
* and is similar to how vanilla HTML is handled in Prettier core.
148149
*/
149-
keepIfLonely: /\n\r?\s*\n\r?/.test(node.raw || node.data),
150+
keepIfLonely: /\n\r?\s*\n\r?/.test(getUnencodedText(node)),
150151
};
151152
}
152153

@@ -156,9 +157,9 @@ export function print(path: FastPath, options: ParserOptions, print: PrintFn): D
156157
* until this node's current line is out of room, at which `fill` will break at the
157158
* most convenient instance of `line`.
158159
*/
159-
return fill(splitTextToDocs(node.raw || node.data));
160+
return fill(splitTextToDocs(getUnencodedText(node)));
160161
} else {
161-
return node.data;
162+
return getUnencodedText(node);
162163
}
163164
case 'Element':
164165
case 'InlineComponent':

src/print/node-helpers.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export function canBreakBefore(node: Node) {
5353
export function isInlineNode(node: Node): boolean {
5454
switch (node.type) {
5555
case 'Text':
56-
const text = node.raw || node.data;
56+
const text = getUnencodedText(node);
5757
const isAllWhitespace = text.trim() === '';
5858

5959
return !isAllWhitespace || text === '';
@@ -105,7 +105,7 @@ export function getNextNode(path: FastPath): Node | undefined {
105105
}
106106

107107
/**
108-
* Returns the position that used to be the end of the node before the embed elements were cut out.
108+
* Returns the position that used to be the end of the node before the embed elements were cut out.
109109
*/
110110
export function getNodeEnd(node: Node, path: FastPath) {
111111
const root = path.stack[0];
@@ -122,7 +122,7 @@ export function getNodeEnd(node: Node, path: FastPath) {
122122
}
123123

124124
export function isEmptyNode(node: Node): boolean {
125-
return node.type === 'Text' && (node.raw || node.data).trim() === '';
125+
return node.type === 'Text' && getUnencodedText(node).trim() === '';
126126
}
127127

128128
export function isIgnoreDirective(node: Node | undefined | null): boolean {
@@ -211,3 +211,8 @@ export function isOrCanBeConvertedToShorthand(node: AttributeNode): boolean {
211211

212212
return false;
213213
}
214+
215+
export function getUnencodedText(node: TextNode) {
216+
// `raw` will contain HTML entities in unencoded form
217+
return node.raw || node.data;
218+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
&
1+
<i data-icon="&#xE04E;">&amp;</i>

0 commit comments

Comments
 (0)