11'use strict' ;
22
3+ //Conversion tables for DOM Level1 structure emulation
4+ var nodeTypes = {
5+ element : 1 ,
6+ text : 3 ,
7+ cdata : 4 ,
8+ comment : 8
9+ } ;
10+
11+ var nodePropertyShorthands = {
12+ tagName : 'name' ,
13+ childNodes : 'children' ,
14+ parentNode : 'parent' ,
15+ previousSibling : 'prev' ,
16+ nextSibling : 'next' ,
17+ nodeValue : 'data'
18+ } ;
19+
20+ //Node
21+ var Node = function ( props ) {
22+ for ( var key in props ) {
23+ if ( props . hasOwnProperty ( key ) )
24+ this [ key ] = props [ key ] ;
25+ }
26+ } ;
27+
28+ Node . prototype = {
29+ get firstChild ( ) {
30+ var children = this . children ;
31+ return children && children [ 0 ] || null ;
32+ } ,
33+
34+ get lastChild ( ) {
35+ var children = this . children ;
36+ return children && children [ children . length - 1 ] || null ;
37+ } ,
38+
39+ get nodeType ( ) {
40+ return nodeTypes [ this . type ] || nodeTypes . element ;
41+ }
42+ } ;
43+
44+ Object . keys ( nodePropertyShorthands ) . forEach ( function ( key ) {
45+ var shorthand = nodePropertyShorthands [ key ] ;
46+
47+ Object . defineProperty ( Node . prototype , key , {
48+ get : function ( ) {
49+ return this [ shorthand ] || null ;
50+ } ,
51+ set : function ( val ) {
52+ this [ shorthand ] = val ;
53+ return val ;
54+ }
55+ } ) ;
56+ } ) ;
57+
58+
359//Node construction
460exports . createDocument =
561exports . createDocumentFragment = function ( ) {
6- return {
62+ return new Node ( {
763 type : 'root' ,
864 name : 'root' ,
965 parent : null ,
1066 prev : null ,
1167 next : null ,
1268 children : [ ]
13- } ;
69+ } ) ;
1470} ;
1571
1672exports . createElement = function ( tagName , namespaceURI , attrs ) {
@@ -26,10 +82,9 @@ exports.createElement = function (tagName, namespaceURI, attrs) {
2682 attribsPrefix [ attrName ] = attrs [ i ] . prefix ;
2783 }
2884
29- return {
85+ return new Node ( {
3086 type : tagName === 'script' || tagName === 'style' ? tagName : 'tag' ,
3187 name : tagName ,
32- tagName : tagName ,
3388 namespace : namespaceURI ,
3489 attribs : attribs ,
3590 'x-attribsNamespace' : attribsNamespace ,
@@ -38,27 +93,27 @@ exports.createElement = function (tagName, namespaceURI, attrs) {
3893 parent : null ,
3994 prev : null ,
4095 next : null
41- } ;
96+ } ) ;
4297} ;
4398
4499exports . createCommentNode = function ( data ) {
45- return {
100+ return new Node ( {
46101 type : 'comment' ,
47102 data : data ,
48103 parent : null ,
49104 prev : null ,
50105 next : null
51- } ;
106+ } ) ;
52107} ;
53108
54109var createTextNode = function ( value ) {
55- return {
110+ return new Node ( {
56111 type : 'text' ,
57112 data : value ,
58113 parent : null ,
59114 prev : null ,
60115 next : null
61- }
116+ } ) ;
62117} ;
63118
64119
@@ -92,14 +147,14 @@ exports.setDocumentType = function (document, name, publicId, systemId) {
92147 }
93148
94149 else {
95- appendChild ( document , {
150+ appendChild ( document , new Node ( {
96151 type : 'directive' ,
97152 name : '!doctype' ,
98153 data : data ,
99154 'x-name' : name ,
100155 'x-publicId' : publicId ,
101156 'x-systemId' : systemId
102- } ) ;
157+ } ) ) ;
103158 }
104159
105160} ;
0 commit comments