@@ -6,7 +6,7 @@ import { isString } from './is';
6
6
* e.g. [HTMLElement] => body > div > input#foo.btn[name=baz]
7
7
* @returns generated DOM path
8
8
*/
9
- export function htmlTreeAsString ( elem : unknown , keyAttr ?: string ) : string {
9
+ export function htmlTreeAsString ( elem : unknown , keyAttrs ?: string [ ] ) : string {
10
10
type SimpleNode = {
11
11
parentNode : SimpleNode ;
12
12
} | null ;
@@ -28,7 +28,7 @@ export function htmlTreeAsString(elem: unknown, keyAttr?: string): string {
28
28
29
29
// eslint-disable-next-line no-plusplus
30
30
while ( currentElem && height ++ < MAX_TRAVERSE_HEIGHT ) {
31
- nextStr = _htmlElementAsString ( currentElem , keyAttr ) ;
31
+ nextStr = _htmlElementAsString ( currentElem , keyAttrs ) ;
32
32
// bail out if
33
33
// - nextStr is the 'html' element
34
34
// - the length of the string that would be created exceeds MAX_OUTPUT_LEN
@@ -54,7 +54,7 @@ export function htmlTreeAsString(elem: unknown, keyAttr?: string): string {
54
54
* e.g. [HTMLElement] => input#foo.btn[name=baz]
55
55
* @returns generated DOM path
56
56
*/
57
- function _htmlElementAsString ( el : unknown , keyAttr ?: string ) : string {
57
+ function _htmlElementAsString ( el : unknown , keyAttrs ?: string [ ] ) : string {
58
58
const elem = el as {
59
59
tagName ?: string ;
60
60
id ?: string ;
@@ -75,9 +75,15 @@ function _htmlElementAsString(el: unknown, keyAttr?: string): string {
75
75
76
76
out . push ( elem . tagName . toLowerCase ( ) ) ;
77
77
78
- const keyAttrValue = keyAttr ? elem . getAttribute ( keyAttr ) : null ;
79
- if ( keyAttrValue ) {
80
- out . push ( `[${ keyAttr } ="${ keyAttrValue } "]` ) ;
78
+ // Pairs of attribute keys defined in `serializeAttribute` and their values on element.
79
+ const keyAttrPairs = keyAttrs ?. length
80
+ ? keyAttrs . filter ( keyAttr => elem . getAttribute ( keyAttr ) ) . map ( keyAttr => [ keyAttr , elem . getAttribute ( keyAttr ) ] )
81
+ : null ;
82
+
83
+ if ( keyAttrPairs ?. length ) {
84
+ keyAttrPairs . forEach ( keyAttrPair => {
85
+ out . push ( `[${ keyAttrPair [ 0 ] } ="${ keyAttrPair [ 1 ] } "]` ) ;
86
+ } ) ;
81
87
} else {
82
88
if ( elem . id ) {
83
89
out . push ( `#${ elem . id } ` ) ;
0 commit comments