11import {
22 AttrNamespace ,
33 ElementNamespace ,
4+ InsertPosition ,
45 Namespace ,
6+ NodeType ,
57 SimpleAttr ,
68 SimpleChildNodes ,
79 SimpleComment ,
@@ -10,7 +12,6 @@ import {
1012 SimpleDocumentType ,
1113 SimpleElement ,
1214 SimpleNode ,
13- SimpleNodeType ,
1415 SimpleRawHTMLSection ,
1516 SimpleText ,
1617} from '@simple-dom/interface' ;
@@ -31,11 +32,11 @@ import {
3132 parseQualifiedName ,
3233} from './qualified-name' ;
3334
34- export type SimpleElementImpl = SimpleNodeImpl < SimpleNodeType . ELEMENT_NODE , SimpleDocument , null , ElementNamespace > ;
35- export type SimpleDocumentImpl = SimpleNodeImpl < SimpleNodeType . DOCUMENT_NODE , null , null , Namespace . HTML > ;
35+ export type SimpleElementImpl = SimpleNodeImpl < NodeType . ELEMENT_NODE , SimpleDocument , null , ElementNamespace > ;
36+ export type SimpleDocumentImpl = SimpleNodeImpl < NodeType . DOCUMENT_NODE , null , null , Namespace . HTML > ;
3637
3738export default class SimpleNodeImpl <
38- T extends SimpleNodeType ,
39+ T extends NodeType ,
3940 O extends SimpleDocument | null ,
4041 V extends string | null ,
4142 N extends ElementNamespace | undefined
@@ -92,6 +93,35 @@ export default class SimpleNodeImpl<
9293 return oldChild ;
9394 }
9495
96+ public insertAdjacentHTML ( this : SimpleElementImpl , position : InsertPosition , html : string ) : void {
97+ const raw = new SimpleNodeImpl ( this . ownerDocument , NodeType . RAW_NODE , '#raw' , html , void 0 ) ;
98+ let parentNode : SimpleNode | null ;
99+ let nextSibling : SimpleNode | null ;
100+ switch ( position ) {
101+ case 'beforebegin' :
102+ parentNode = this . parentNode ;
103+ nextSibling = this ;
104+ break ;
105+ case 'afterbegin' :
106+ parentNode = this ;
107+ nextSibling = this . firstChild ;
108+ break ;
109+ case 'beforeend' :
110+ parentNode = this ;
111+ nextSibling = null ;
112+ break ;
113+ case 'afterend' :
114+ parentNode = this . parentNode ;
115+ nextSibling = this . nextSibling ;
116+ break ;
117+ default : throw new Error ( 'invalid position' ) ;
118+ }
119+ if ( parentNode === null ) {
120+ throw new Error ( `${ position } requires a parentNode` ) ;
121+ }
122+ insertBefore ( parentNode , raw , nextSibling ) ;
123+ }
124+
95125 public getAttribute ( this : SimpleElementImpl , name : string ) : string | null {
96126 const localName = adjustAttrName ( this . namespaceURI , name ) ;
97127 return getAttribute ( this . attributes , null , localName ) ;
@@ -142,28 +172,32 @@ export default class SimpleNodeImpl<
142172 }
143173
144174 public createElement ( this : SimpleDocumentImpl , name : string ) : SimpleElement {
145- return new SimpleNodeImpl ( this , SimpleNodeType . ELEMENT_NODE , name . toUpperCase ( ) , null , Namespace . HTML ) ;
175+ return new SimpleNodeImpl ( this , NodeType . ELEMENT_NODE , name . toUpperCase ( ) , null , Namespace . HTML ) ;
146176 }
147177
148178 public createElementNS ( this : SimpleDocumentImpl , namespace : ElementNamespace , qualifiedName : string ) : SimpleElement {
149179 // we don't care to parse the qualified name because we only support HTML documents
150180 // which don't support prefixed elements
151- return new SimpleNodeImpl ( this , SimpleNodeType . ELEMENT_NODE , qualifiedName , null , namespace ) ;
181+ return new SimpleNodeImpl ( this , NodeType . ELEMENT_NODE , qualifiedName , null , namespace ) ;
152182 }
153183
154184 public createTextNode ( this : SimpleDocumentImpl , text : string ) : SimpleText {
155- return new SimpleNodeImpl ( this , SimpleNodeType . TEXT_NODE , '#text' , text , void 0 ) ;
185+ return new SimpleNodeImpl ( this , NodeType . TEXT_NODE , '#text' , text , void 0 ) ;
156186 }
157187
158188 public createComment ( this : SimpleDocumentImpl , text : string ) : SimpleComment {
159- return new SimpleNodeImpl ( this , SimpleNodeType . COMMENT_NODE , '#comment' , text , void 0 ) ;
189+ return new SimpleNodeImpl ( this , NodeType . COMMENT_NODE , '#comment' , text , void 0 ) ;
160190 }
161191
192+ /**
193+ * Backwards compat
194+ * @deprecated
195+ */
162196 public createRawHTMLSection ( this : SimpleDocumentImpl , text : string ) : SimpleRawHTMLSection {
163- return new SimpleNodeImpl ( this , SimpleNodeType . RAW , '#raw' , text , void 0 ) ;
197+ return new SimpleNodeImpl ( this , NodeType . RAW_NODE , '#raw' , text , void 0 ) ;
164198 }
165199
166200 public createDocumentFragment ( this : SimpleDocumentImpl ) : SimpleDocumentFragment {
167- return new SimpleNodeImpl ( this , SimpleNodeType . DOCUMENT_FRAGMENT_NODE , '#document-fragment' , null , void 0 ) ;
201+ return new SimpleNodeImpl ( this , NodeType . DOCUMENT_FRAGMENT_NODE , '#document-fragment' , null , void 0 ) ;
168202 }
169203}
0 commit comments