Skip to content

Commit ab60189

Browse files
authored
(fix) print raw uses original text (#156)
Before that, the tags/attributes within would get erased and only their contents would be preserved. Also added `type` to the language attribute lookup.
1 parent e748b58 commit ab60189

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

src/print/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
isOrCanBeConvertedToShorthand,
1919
isIgnoreDirective,
2020
doesEmbedStartAt,
21-
getUnencodedText
21+
getUnencodedText,
2222
} from './node-helpers';
2323
import {
2424
isLine,
@@ -185,7 +185,7 @@ export function print(path: FastPath, options: ParserOptions, print: PrintFn): D
185185
if (isEmpty) {
186186
body = '';
187187
} else if (!isSupportedLanguage) {
188-
body = printRaw(node);
188+
body = printRaw(node, options.originalText);
189189
} else if (isInlineElement(node) || isPreTagContent(path)) {
190190
body = printIndentedPreservingWhitespace(path, print);
191191
} else {

src/print/node-helpers.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ import {
55
AttributeNode,
66
MustacheTagNode,
77
AttributeShorthandNode,
8+
HeadNode,
9+
InlineComponentNode,
10+
SlotNode,
11+
TitleNode,
12+
WindowNode,
813
} from './nodes';
914
import { inlineElements, TagName } from '../lib/elements';
1015
import { FastPath } from 'prettier';
@@ -109,14 +114,17 @@ export function isIgnoreDirective(node: Node | undefined | null): boolean {
109114
return !!node && node.type === 'Comment' && node.data.trim() === 'prettier-ignore';
110115
}
111116

112-
export function printRaw(node: Node): string {
113-
const children: Node[] | undefined = (node as ElementNode).children;
114-
115-
if (children) {
116-
return children.map(printRaw).join('');
117-
} else {
118-
return (node as TextNode).raw || '';
117+
export function printRaw(
118+
node: ElementNode | InlineComponentNode | SlotNode | WindowNode | HeadNode | TitleNode,
119+
originalText: string,
120+
): string {
121+
if (node.children.length === 0) {
122+
return '';
119123
}
124+
125+
const firstChild = node.children[0];
126+
const lastChild = node.children[node.children.length - 1];
127+
return originalText.substring(firstChild.start, lastChild.end);
120128
}
121129

122130
function isTextNode(node: Node): node is TextNode {
@@ -148,7 +156,7 @@ export function getAttributeTextValue(attributeName: string, node: Node): string
148156
}
149157

150158
function getLangAttribute(node: Node): string | null {
151-
const value = getAttributeTextValue('lang', node);
159+
const value = getAttributeTextValue('lang', node) || getAttributeTextValue('type', node);
152160

153161
if (value != null) {
154162
return value.replace(/^text\//, '');

test/printer/samples/unsupported-language.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,13 @@
1616

1717
div
1818
p Hey
19+
<p>ok</p>
20+
</template>
21+
22+
<template type="text/pug">
23+
h1 My Title
24+
25+
div
26+
p Hey
27+
<p>ok</p>
1928
</template>

0 commit comments

Comments
 (0)