1+ import { asciiUppercase , asciiLowercase } from './ascii.ts' ;
2+
13/**
24 * Container type for valid names of createElement()
35 */
@@ -11,58 +13,113 @@ export class Document {
1113
1214 constructor ( notify : ( ) => void ) {
1315 this . notify = notify ;
14- this . documentElement = new Element ( 'document' , notify ) ;
16+ this . documentElement = new Element ( this , 'document' , notify ) ;
1517 }
1618
17- createElement ( tagName : keyof Elements ) {
18- return new Element ( tagName , this . notify ) ;
19+ createElement ( tagName : keyof Elements ) : Element {
20+ if ( arguments . length === 0 ) {
21+ // oxlint-disable-next-line bombshell-dev/no-generic-error -- Web IDL requires TypeError for missing arguments.
22+ throw new TypeError ( 'createElement requires a tag name' ) ;
23+ }
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' ) ;
30+ }
31+ return new Element ( this , asciiLowercase ( String ( tagName ) ) , this . notify ) ;
1932 }
2033
2134 createTextNode ( data : string ) : Text {
22- return new Text ( data , this . notify ) ;
35+ return new Text ( this , String ( data ) , this . notify ) ;
36+ }
37+
38+ createComment ( data : string ) : Text {
39+ return new Comment ( this , String ( data ) , this . notify ) ;
2340 }
2441}
2542
26- export class Node {
27- readonly nodeName : string ;
43+ export abstract class Node {
44+ static readonly ELEMENT_NODE = 1 ;
45+ static readonly ATTRIBUTE_NODE = 2 ;
46+ static readonly TEXT_NODE = 3 ;
47+ static readonly COMMENT_NODE = 8 ;
48+
2849 readonly childNodes : Node [ ] ;
50+ readonly ownerDocument : Document ;
51+
52+ abstract readonly nodeType : number ;
53+ abstract readonly nodeName : string ;
2954
3055 protected readonly notify : ( ) => void ;
3156
32- constructor ( nodeName : string , notify : Node [ 'notify' ] ) {
57+ constructor ( owner : Document , notify : Node [ 'notify' ] ) {
3358 this . notify = notify ;
34- this . nodeName = nodeName ;
3559 this . childNodes = [ ] ;
60+ this . ownerDocument = owner ;
61+ }
62+
63+ hasChildNodes ( ) : boolean {
64+ return this . childNodes . length > 0 ;
65+ }
66+
67+ get firstChild ( ) : Node | null {
68+ return this . childNodes [ 0 ] ?? null ;
69+ }
70+
71+ get lastChild ( ) : Node | null {
72+ return this . childNodes [ this . childNodes . length - 1 ] ?? null ;
3673 }
3774
3875 appendChild ( node : Node ) : Node {
3976 this . childNodes . push ( node ) ;
4077 this . notify ( ) ;
4178 return node ;
4279 }
80+
81+ get nodeValue ( ) : string | null {
82+ return null ;
83+ }
4384}
4485
4586export class Element extends Node {
46- readonly tagName : string ;
87+ readonly localName : string ;
88+ readonly nodeName : string ;
4789
48- constructor ( tagName : keyof Elements , notify : Node [ 'notify' ] ) {
49- super ( tagName . toUpperCase ( ) , notify ) ;
50- this . tagName = tagName ;
90+ constructor ( owner : Document , localName : string , notify : Node [ 'notify' ] ) {
91+ super ( owner , notify ) ;
92+ this . localName = localName ;
93+ this . nodeName = asciiUppercase ( String ( localName ) ) ;
94+ }
95+
96+ get nodeType ( ) : number {
97+ return Node . ELEMENT_NODE ;
98+ }
99+
100+ get tagName ( ) : string {
101+ return this . nodeName ;
51102 }
52103
53104 get children ( ) : Element [ ] {
54105 return this . childNodes . filter ( ( node ) => node instanceof Element ) ;
55106 }
107+ get prefix ( ) : null {
108+ return null ;
109+ }
110+ get namespaceURI ( ) : string {
111+ return 'http://www.w3.org/1999/xhtml' ;
112+ }
56113}
57114
58- export class Text extends Node {
115+ export abstract class CharacterData extends Node {
59116 #data: string ;
60- constructor ( data : string , notify : Node [ 'notify' ] ) {
61- super ( '#text' , notify ) ;
117+ constructor ( owner : Document , data : string , notify : Node [ 'notify' ] ) {
118+ super ( owner , notify ) ;
62119 this . #data = data ;
63120 }
64121
65- get data ( ) {
122+ get data ( ) : string {
66123 return this . #data;
67124 }
68125
@@ -72,4 +129,36 @@ export class Text extends Node {
72129 this . notify ( ) ;
73130 }
74131 }
132+
133+ override get nodeValue ( ) : string {
134+ return this . data ;
135+ }
136+
137+ get textContent ( ) : string {
138+ return this . data ;
139+ }
140+
141+ get length ( ) : number {
142+ return this . data . length ;
143+ }
144+ }
145+
146+ export class Text extends CharacterData {
147+ get nodeType ( ) : number {
148+ return Node . TEXT_NODE ;
149+ }
150+
151+ get nodeName ( ) : string {
152+ return '#text' ;
153+ }
154+ }
155+
156+ export class Comment extends CharacterData {
157+ get nodeType ( ) : number {
158+ return Node . COMMENT_NODE ;
159+ }
160+
161+ get nodeName ( ) : string {
162+ return '#comment' ;
163+ }
75164}
0 commit comments