@@ -6,7 +6,7 @@ import { isString } from './is';
66 * e.g. [HTMLElement] => body > div > input#foo.btn[name=baz]
77 * @returns generated DOM path
88 */
9- export function htmlTreeAsString ( elem : unknown , keyAttr ?: string ) : string {
9+ export function htmlTreeAsString ( elem : unknown , keyAttrs ?: string [ ] ) : string {
1010 type SimpleNode = {
1111 parentNode : SimpleNode ;
1212 } | null ;
@@ -28,7 +28,7 @@ export function htmlTreeAsString(elem: unknown, keyAttr?: string): string {
2828
2929 // eslint-disable-next-line no-plusplus
3030 while ( currentElem && height ++ < MAX_TRAVERSE_HEIGHT ) {
31- nextStr = _htmlElementAsString ( currentElem , keyAttr ) ;
31+ nextStr = _htmlElementAsString ( currentElem , keyAttrs ) ;
3232 // bail out if
3333 // - nextStr is the 'html' element
3434 // - 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 {
5454 * e.g. [HTMLElement] => input#foo.btn[name=baz]
5555 * @returns generated DOM path
5656 */
57- function _htmlElementAsString ( el : unknown , keyAttr ?: string ) : string {
57+ function _htmlElementAsString ( el : unknown , keyAttrs ?: string [ ] ) : string {
5858 const elem = el as {
5959 tagName ?: string ;
6060 id ?: string ;
@@ -75,9 +75,15 @@ function _htmlElementAsString(el: unknown, keyAttr?: string): string {
7575
7676 out . push ( elem . tagName . toLowerCase ( ) ) ;
7777
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+ } ) ;
8187 } else {
8288 if ( elem . id ) {
8389 out . push ( `#${ elem . id } ` ) ;
0 commit comments