11import { asciiUppercase , asciiLowercase } from './ascii.ts' ;
2+ import { validateElementName , validateElementNamespace } from './validation.ts' ;
23
3- /**
4- * Container type for valid names of createElement()
5- */
6- export interface Elements {
7- document : Element ;
8- }
4+ const HTML_NS = 'http://www.w3.org/1999/xhtml' ;
95
106export class Document {
117 readonly documentElement : Element ;
128 readonly notify : ( ) => void ;
139
10+ #registry: Map < string , typeof Element > = new Map ( [ [ 'span' , HTMLSpanElement ] ] ) ;
11+
1412 constructor ( notify : ( ) => void ) {
1513 this . notify = notify ;
1614 this . documentElement = new Element ( this , 'document' , notify ) ;
1715 }
1816
19- createElement ( tagName : keyof Elements ) : Element {
17+ createElement ( tagName : string ) : Element {
2018 if ( arguments . length === 0 ) {
2119 // oxlint-disable-next-line bombshell-dev/no-generic-error -- Web IDL requires TypeError for missing arguments.
2220 throw new TypeError ( 'createElement requires a tag name' ) ;
2321 }
24- if (
25- ! / ^ (?: [ A - Z a - z ] [ ^ \0 \t \n \f \r \u0020 / > ] * | [: _ \u0080 - \u{10FFFF} ] [ A - Z a - z 0 - 9 _ . : \- \u0080 - \u{10FFFF} ] * ) $ / u. test (
26- tagName ,
27- )
28- ) {
29- throw new DOMException ( 'Invalid element name' , 'InvalidCharacterError' ) ;
22+ const localName = `${ tagName } ` ;
23+ validateElementName ( localName ) ;
24+ return this . #createElement( asciiLowercase ( localName ) , { namespaceURI : HTML_NS , prefix : null } ) ;
25+ }
26+
27+ createElementNS ( namespace : string | null , qualifiedName : string ) : Element {
28+ if ( arguments . length < 2 ) {
29+ // oxlint-disable-next-line bombshell-dev/no-generic-error -- Web IDL requires TypeError for missing arguments.
30+ throw new TypeError ( 'createElementNS requires a namespace and qualified name' ) ;
3031 }
31- return new Element ( this , asciiLowercase ( String ( tagName ) ) , this . notify ) ;
32+ const namespaceURI = namespace == null ? null : `${ namespace } ` || null ;
33+ const name = `${ qualifiedName } ` ;
34+ const { prefix, localName } = validateElementNamespace ( namespaceURI , name ) ;
35+ return this . #createElement( localName , { namespaceURI, prefix } ) ;
3236 }
3337
3438 createTextNode ( data : string ) : Text {
@@ -42,6 +46,15 @@ export class Document {
4246 createProcessingInstruction ( target : string , data : string ) : ProcessingInstruction {
4347 return new ProcessingInstruction ( this , target , data , this . notify ) ;
4448 }
49+
50+ #createElement( localName : string , namespace : ElementNamespace ) : Element {
51+ if ( namespace . namespaceURI !== HTML_NS ) {
52+ return new Element ( this , localName , this . notify , namespace ) ;
53+ } else {
54+ const ElementConstructor = this . #registry. get ( localName ) ?? HTMLUnknownElement ;
55+ return new ElementConstructor ( this , localName , this . notify , namespace ) ;
56+ }
57+ }
4558}
4659
4760export abstract class Node {
@@ -51,6 +64,12 @@ export abstract class Node {
5164 static readonly PROCESSING_INSTRUCTION_NODE = 7 ;
5265 static readonly COMMENT_NODE = 8 ;
5366
67+ readonly ELEMENT_NODE = Node . ELEMENT_NODE ;
68+ readonly ATTRIBUTE_NODE = Node . ATTRIBUTE_NODE ;
69+ readonly TEXT_NODE = Node . TEXT_NODE ;
70+ readonly PROCESSING_INSTRUCTION_NODE = Node . PROCESSING_INSTRUCTION_NODE ;
71+ readonly COMMENT_NODE = Node . COMMENT_NODE ;
72+
5473 readonly #childNodes: Node [ ] = [ ] ;
5574 readonly ownerDocument : Document ;
5675
@@ -161,11 +180,21 @@ export abstract class Node {
161180export class Element extends Node {
162181 readonly localName : string ;
163182 readonly nodeName : string ;
164-
165- constructor ( owner : Document , localName : string , notify : Node [ 'notify' ] ) {
183+ readonly namespaceURI : string | null ;
184+ readonly prefix : string | null ;
185+
186+ constructor (
187+ owner : Document ,
188+ localName : string ,
189+ notify : Node [ 'notify' ] ,
190+ { namespaceURI = HTML_NS , prefix = null } : Partial < ElementNamespace > = { } ,
191+ ) {
166192 super ( owner , notify ) ;
167193 this . localName = localName ;
168- this . nodeName = asciiUppercase ( String ( localName ) ) ;
194+ this . namespaceURI = namespaceURI ;
195+ this . prefix = prefix ;
196+ const qualifiedName = prefix === null ? localName : `${ prefix } :${ localName } ` ;
197+ this . nodeName = namespaceURI === HTML_NS ? asciiUppercase ( qualifiedName ) : qualifiedName ;
169198 }
170199
171200 get nodeType ( ) : number {
@@ -179,14 +208,14 @@ export class Element extends Node {
179208 get children ( ) : Element [ ] {
180209 return this . childNodes . filter ( ( node ) => node instanceof Element ) ;
181210 }
182- get prefix ( ) : null {
183- return null ;
184- }
185- get namespaceURI ( ) : string {
186- return 'http://www.w3.org/1999/xhtml' ;
187- }
188211}
189212
213+ export class HTMLElement extends Element { }
214+
215+ export class HTMLSpanElement extends HTMLElement { }
216+
217+ export class HTMLUnknownElement extends HTMLElement { }
218+
190219export abstract class CharacterData extends Node {
191220 #data: string ;
192221 constructor ( owner : Document , data : string , notify : Node [ 'notify' ] ) {
@@ -263,3 +292,8 @@ export class ProcessingInstruction extends CharacterData {
263292 return this . target ;
264293 }
265294}
295+
296+ interface ElementNamespace {
297+ namespaceURI : string | null ;
298+ prefix : string | null ;
299+ }
0 commit comments