@@ -2,13 +2,12 @@ import { selectAll, selectOne } from 'css-select';
2
2
import he from 'he' ;
3
3
import arr_back from '../back' ;
4
4
import Matcher from '../matcher' ;
5
+ import VoidTag from '../void-tag' ;
5
6
import CommentNode from './comment' ;
6
7
import Node from './node' ;
7
8
import TextNode from './text' ;
8
9
import NodeType from './type' ;
9
10
10
- const voidTags = new Set ( [ 'area' , 'base' , 'br' , 'col' , 'embed' , 'hr' , 'img' , 'input' , 'link' , 'meta' , 'param' , 'source' , 'track' , 'wbr' ] ) ;
11
-
12
11
type IRawTagName =
13
12
| 'LI'
14
13
| 'P'
@@ -173,7 +172,8 @@ export default class HTMLElement extends Node {
173
172
keyAttrs : KeyAttributes ,
174
173
public rawAttrs = '' ,
175
174
parentNode : HTMLElement | null ,
176
- range ?: [ number , number ]
175
+ range : [ number , number ] ,
176
+ private voidTag = new VoidTag ( )
177
177
) {
178
178
super ( parentNode , range ) ;
179
179
this . rawTagName = tagName ;
@@ -237,7 +237,7 @@ export default class HTMLElement extends Node {
237
237
}
238
238
239
239
public get isVoidElement ( ) {
240
- return voidTags . has ( this . localName ) ;
240
+ return this . voidTag . isVoidElement ( this . localName ) ;
241
241
}
242
242
243
243
/**
@@ -313,7 +313,7 @@ export default class HTMLElement extends Node {
313
313
const tag = this . rawTagName ;
314
314
if ( tag ) {
315
315
const attrs = this . rawAttrs ? ` ${ this . rawAttrs } ` : '' ;
316
- return this . isVoidElement ? `< ${ tag } ${ attrs } >` : `< ${ tag } ${ attrs } > ${ this . innerHTML } </ ${ tag } >` ;
316
+ return this . voidTag . formatNode ( tag , attrs , this . innerHTML ) ;
317
317
}
318
318
return this . innerHTML ;
319
319
}
@@ -986,6 +986,16 @@ export interface Options {
986
986
blockTextElements : {
987
987
[ tag : string ] : boolean ;
988
988
} ;
989
+ voidTag ?: {
990
+ /**
991
+ * options, default value is ['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source', 'track', 'wbr']
992
+ */
993
+ tags ?: string [ ] ;
994
+ /**
995
+ * void tag serialisation, add a final slash <br/>
996
+ */
997
+ closingSlash ?: boolean ;
998
+ }
989
999
}
990
1000
991
1001
const frameflag = 'documentfragmentcontainer' ;
@@ -997,6 +1007,7 @@ const frameflag = 'documentfragmentcontainer';
997
1007
* @return {HTMLElement } root element
998
1008
*/
999
1009
export function base_parse ( data : string , options = { lowerCaseTagName : false , comment : false } as Partial < Options > ) {
1010
+ const voidTag = new VoidTag ( options ?. voidTag ?. closingSlash , options ?. voidTag ?. tags ) ;
1000
1011
const elements = options . blockTextElements || {
1001
1012
script : true ,
1002
1013
noscript : true ,
@@ -1016,7 +1027,7 @@ export function base_parse(data: string, options = { lowerCaseTagName: false, co
1016
1027
}
1017
1028
1018
1029
const createRange = ( startPos : number , endPos : number ) : [ number , number ] => [ startPos - frameFlagOffset , endPos - frameFlagOffset ] ;
1019
- const root = new HTMLElement ( null , { } , '' , null , [ 0 , data . length ] ) ;
1030
+ const root = new HTMLElement ( null , { } , '' , null , [ 0 , data . length ] , voidTag ) ;
1020
1031
1021
1032
let currentParent = root ;
1022
1033
const stack = [ root ] ;
@@ -1099,7 +1110,7 @@ export function base_parse(data: string, options = { lowerCaseTagName: false, co
1099
1110
1100
1111
currentParent = currentParent . appendChild (
1101
1112
// Initialize range (end position updated later for closed tags)
1102
- new HTMLElement ( tagName , attrs , attributes . slice ( 1 ) , null , createRange ( tagStartPos , tagEndPos ) )
1113
+ new HTMLElement ( tagName , attrs , attributes . slice ( 1 ) , null , createRange ( tagStartPos , tagEndPos ) , voidTag )
1103
1114
) ;
1104
1115
stack . push ( currentParent ) ;
1105
1116
0 commit comments