Skip to content

Commit

Permalink
do not encode entities in attribute values
Browse files Browse the repository at this point in the history
  • Loading branch information
ehrencrona committed Oct 8, 2020
1 parent 2687215 commit 840f0e6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/lib/elements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export const inlineElements: TagName[] = [
/**
* HTML attributes that we may safely reformat (trim whitespace, add or remove newlines)
*/
export const formattableAttributes = [
export const formattableAttributes: string[] = [
// None at the moment
// Prettier HTML does not format attributes at all
// and to be consistent we leave this array empty for now
Expand Down
7 changes: 4 additions & 3 deletions src/print/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
isOrCanBeConvertedToShorthand,
getNextNode,
getNodeEnd,
getUnencodedText
} from './node-helpers';
import {
isLine,
Expand Down Expand Up @@ -146,7 +147,7 @@ export function print(path: FastPath, options: ParserOptions, print: PrintFn): D
* allow for flexible grouping of HTML tags in a particular indentation level,
* and is similar to how vanilla HTML is handled in Prettier core.
*/
keepIfLonely: /\n\r?\s*\n\r?/.test(node.raw || node.data),
keepIfLonely: /\n\r?\s*\n\r?/.test(getUnencodedText(node)),
};
}

Expand All @@ -156,9 +157,9 @@ export function print(path: FastPath, options: ParserOptions, print: PrintFn): D
* until this node's current line is out of room, at which `fill` will break at the
* most convenient instance of `line`.
*/
return fill(splitTextToDocs(node.raw || node.data));
return fill(splitTextToDocs(getUnencodedText(node)));
} else {
return node.data;
return getUnencodedText(node);
}
case 'Element':
case 'InlineComponent':
Expand Down
11 changes: 8 additions & 3 deletions src/print/node-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export function canBreakBefore(node: Node) {
export function isInlineNode(node: Node): boolean {
switch (node.type) {
case 'Text':
const text = node.raw || node.data;
const text = getUnencodedText(node);
const isAllWhitespace = text.trim() === '';

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

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

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

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

return false;
}

export function getUnencodedText(node: TextNode) {
// `raw` will contain HTML entities in unencoded form
return node.raw || node.data;
}

0 comments on commit 840f0e6

Please sign in to comment.