Skip to content

Commit 6892ede

Browse files
committed
feat: add node property closeAs, close #48
1 parent d0508e2 commit 6892ede

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

src/index.ts

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
import {Attributes, Node} from 'posthtml-parser';
2-
import {Options, quoteStyleEnum} from '../types/index.d';
1+
import {Attributes, NodeText, NodeTag} from 'posthtml-parser';
2+
import {closingSingleTagOptionEnum, closingSingleTagTypeEnum, Options, quoteStyleEnum} from '../types/index.d';
3+
4+
type Node = NodeText | NodeTag & {
5+
closeAs?: closingSingleTagTypeEnum;
6+
};
37

48
const SINGLE_TAGS = [
59
'area',
@@ -107,23 +111,40 @@ function render(tree?: Node | Node[], options: Options = {}): string {
107111
result += attrs(node.attrs);
108112
}
109113

114+
const closeAs = {
115+
[closingSingleTagTypeEnum.tag]: `></${tag}>`,
116+
[closingSingleTagTypeEnum.slash]: ' />',
117+
[closingSingleTagTypeEnum.default]: '>'
118+
};
119+
110120
if (isSingleTag(tag)) {
111121
switch (closingSingleTag) {
112-
case 'tag':
113-
result += `></${tag}>`;
122+
case closingSingleTagOptionEnum.tag:
123+
result += closeAs[closingSingleTagTypeEnum.tag];
124+
125+
break;
126+
case closingSingleTagOptionEnum.slash:
127+
result += closeAs[closingSingleTagTypeEnum.slash];
114128

115129
break;
116-
case 'slash':
117-
result += ' />';
130+
case closingSingleTagOptionEnum.closeAs:
131+
result += closeAs[node.closeAs ?
132+
closingSingleTagTypeEnum[node.closeAs] :
133+
closingSingleTagTypeEnum.default];
118134

119135
break;
120136
default:
121-
result += '>';
137+
result += closeAs[closingSingleTagTypeEnum.default];
122138
}
123139

124140
if (node.content) {
125141
result += html(node.content);
126142
}
143+
} else if (closingSingleTag === closingSingleTagOptionEnum.closeAs && node.closeAs) {
144+
const type = node.closeAs ?
145+
closingSingleTagTypeEnum[node.closeAs] :
146+
closingSingleTagTypeEnum.default;
147+
result += `${closeAs[type]}${html(node.content)}`;
127148
} else {
128149
result += `>${html(node.content)}</${tag}>`;
129150
}

0 commit comments

Comments
 (0)