Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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[] = [
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is unrelated but fixes warning

// 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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the actual bug

}
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;
}
2 changes: 1 addition & 1 deletion test/printer/samples/text-html-entities.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
&
<i data-icon="&#xE04E;">&amp;</i>