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;
1414 */
1515export function htmlTreeAsString (
1616 elem : unknown ,
17- options : { keyAttrs ?: string [ ] ; maxStringLength ?: number } = { } ,
17+ options : string [ ] | { keyAttrs ?: string [ ] ; maxStringLength ?: number } = { } ,
1818) : string {
1919 type SimpleNode = {
2020 parentNode : SimpleNode ;
@@ -33,7 +33,8 @@ export function htmlTreeAsString(
3333 const separator = ' > ' ;
3434 const sepLength = separator . length ;
3535 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 ;
3738
3839 while ( currentElem && height ++ < MAX_TRAVERSE_HEIGHT ) {
3940 nextStr = _htmlElementAsString ( currentElem , keyAttrs ) ;
Original file line number Diff line number Diff line change @@ -44,6 +44,10 @@ describe('htmlTreeAsString', () => {
4444 </li>` ;
4545 document . body . appendChild ( el ) ;
4646
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+ ) ;
4751 expect ( htmlTreeAsString ( document . getElementById ( 'cat-2' ) , { keyAttrs : [ 'test-id' ] } ) ) . toBe (
4852 'body > ul > li.li-class[title="li-title"] > img[test-id="cat-2-test-id"]' ,
4953 ) ;
You can’t perform that action at this time.
0 commit comments