File tree Expand file tree Collapse file tree 2 files changed +7
-2
lines changed Expand file tree Collapse file tree 2 files changed +7
-2
lines changed Original file line number Diff line number Diff line change @@ -14,7 +14,7 @@ const DEFAULT_MAX_STRING_LENGTH = 80;
14
14
*/
15
15
export function htmlTreeAsString (
16
16
elem : unknown ,
17
- options : { keyAttrs ?: string [ ] ; maxStringLength ?: number } = { } ,
17
+ options : string [ ] | { keyAttrs ?: string [ ] ; maxStringLength ?: number } = { } ,
18
18
) : string {
19
19
type SimpleNode = {
20
20
parentNode : SimpleNode ;
@@ -33,7 +33,8 @@ export function htmlTreeAsString(
33
33
const separator = ' > ' ;
34
34
const sepLength = separator . length ;
35
35
let nextStr ;
36
- const { keyAttrs, maxStringLength = DEFAULT_MAX_STRING_LENGTH } = options ;
36
+ const keyAttrs = Array . isArray ( options ) ? options : options . keyAttrs ;
37
+ const maxStringLength = ( ! Array . isArray ( options ) && options . maxStringLength ) || DEFAULT_MAX_STRING_LENGTH ;
37
38
38
39
while ( currentElem && height ++ < MAX_TRAVERSE_HEIGHT ) {
39
40
nextStr = _htmlElementAsString ( currentElem , keyAttrs ) ;
Original file line number Diff line number Diff line change @@ -44,6 +44,10 @@ describe('htmlTreeAsString', () => {
44
44
</li>` ;
45
45
document . body . appendChild ( el ) ;
46
46
47
+ // Two formats for specifying keyAttrs
48
+ expect ( htmlTreeAsString ( document . getElementById ( 'cat-2' ) , [ 'test-id' ] ) ) . toBe (
49
+ 'body > ul > li.li-class[title="li-title"] > img[test-id="cat-2-test-id"]' ,
50
+ ) ;
47
51
expect ( htmlTreeAsString ( document . getElementById ( 'cat-2' ) , { keyAttrs : [ 'test-id' ] } ) ) . toBe (
48
52
'body > ul > li.li-class[title="li-title"] > img[test-id="cat-2-test-id"]' ,
49
53
) ;
You can’t perform that action at this time.
0 commit comments