@@ -16,6 +16,10 @@ export type SetTextContent = {
1616 textContent : string ;
1717} ;
1818
19+ export type Attributes = Partial < Record < string , string | null > > ;
20+
21+ export type AttributesNS = Partial < Record < string , Attributes > > ;
22+
1923/** Intent to set or remove (if `null`) `attributes`(-`NS`) on `element` */
2024export type SetAttributes = {
2125 element : Element ;
@@ -31,43 +35,69 @@ export type EditV2 =
3135 | Remove
3236 | EditV2 [ ] ;
3337
34- export function isComplex ( edit : EditV2 ) : edit is EditV2 [ ] {
35- return edit instanceof Array ;
38+ export function isAttributes ( attributes : unknown ) : attributes is Attributes {
39+ if ( typeof attributes !== 'object' || attributes === null ) {
40+ return false ;
41+ }
42+ return Object . entries ( attributes ) . every (
43+ ( [ key , value ] ) =>
44+ typeof key === 'string' && ( value === null || typeof value === 'string' ) ,
45+ ) ;
46+ }
47+
48+ export function isAttributesNS (
49+ attributesNS : unknown ,
50+ ) : attributesNS is AttributesNS {
51+ if ( typeof attributesNS !== 'object' || attributesNS === null ) {
52+ return false ;
53+ }
54+ return Object . entries ( attributesNS ) . every (
55+ ( [ namespace , attributes ] ) =>
56+ typeof namespace === 'string' &&
57+ isAttributes ( attributes as Record < string , string | null > ) ,
58+ ) ;
59+ }
60+
61+ export function isComplex ( edit : unknown ) : edit is EditV2 [ ] {
62+ return edit instanceof Array && edit . every ( e => isEditV2 ( e ) ) ;
3663}
3764
38- export function isSetTextContent ( edit : EditV2 ) : edit is SetTextContent {
65+ export function isSetTextContent ( edit : unknown ) : edit is SetTextContent {
3966 return (
40- ( edit as SetTextContent ) . element !== undefined &&
41- ( edit as SetTextContent ) . textContent !== undefined
67+ ( edit as SetTextContent ) . element instanceof Element &&
68+ typeof ( edit as SetTextContent ) . textContent === 'string'
4269 ) ;
4370}
4471
45- export function isRemove ( edit : EditV2 ) : edit is Remove {
72+ export function isRemove ( edit : unknown ) : edit is Remove {
4673 return (
47- ( edit as Insert ) . parent === undefined && ( edit as Remove ) . node !== undefined
74+ ( edit as Insert ) . parent === undefined &&
75+ ( edit as Remove ) . node instanceof Node
4876 ) ;
4977}
5078
51- export function isSetAttributes ( edit : EditV2 ) : edit is SetAttributes {
79+ export function isSetAttributes ( edit : unknown ) : edit is SetAttributes {
5280 return (
53- ( edit as SetAttributes ) . element !== undefined &&
54- ( edit as SetAttributes ) . attributes !== undefined &&
55- ( edit as SetAttributes ) . attributesNS !== undefined
81+ ( edit as SetAttributes ) . element instanceof Element &&
82+ isAttributes ( ( edit as SetAttributes ) . attributes ) &&
83+ isAttributesNS ( ( edit as SetAttributes ) . attributesNS )
5684 ) ;
5785}
5886
59- export function isInsert ( edit : EditV2 ) : edit is Insert {
87+ export function isInsert ( edit : unknown ) : edit is Insert {
6088 return (
61- ( edit as Insert ) . parent !== undefined &&
62- ( edit as Insert ) . node !== undefined &&
63- ( edit as Insert ) . reference !== undefined
89+ ( ( edit as Insert ) . parent instanceof Element ||
90+ ( edit as Insert ) . parent instanceof Document ||
91+ ( edit as Insert ) . parent instanceof DocumentFragment ) &&
92+ ( edit as Insert ) . node instanceof Node &&
93+ ( ( edit as Insert ) . reference instanceof Node ||
94+ ( edit as Insert ) . reference === null )
6495 ) ;
6596}
6697
67- // eslint-disable-next-line @typescript-eslint/no-explicit-any
68- export function isEditV2 ( edit : any ) : edit is EditV2 {
98+ export function isEditV2 ( edit : unknown ) : edit is EditV2 {
6999 if ( isComplex ( edit ) ) {
70- return ! edit . some ( e => ! isEditV2 ( e ) ) ;
100+ return true ;
71101 }
72102
73103 return (
0 commit comments