envjs 0.3.7 → 0.3.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -13,7 +13,7 @@ __extend__(CSS2Properties.prototype, {
13
13
  get cssText(){
14
14
  var css = '';
15
15
  for(var i=0;i<this.length;i++){
16
- css+=this[i]+":"+this.getPropertyValue(this[i])+';'
16
+ css+=this[i]+":"+this.getPropertyValue(this[i])+';';
17
17
  }
18
18
  return css;
19
19
  },
@@ -167,7 +167,7 @@ var __supportedStyles__ = function(){
167
167
  fontVariant: null,
168
168
  fontWeight: null,
169
169
  height: '1px',
170
- left: null,
170
+ left: '0px',
171
171
  letterSpacing: null,
172
172
  lineHeight: null,
173
173
  listStyle: null,
@@ -226,7 +226,7 @@ var __supportedStyles__ = function(){
226
226
  textIndent: null,
227
227
  textShadow: null,
228
228
  textTransform: null,
229
- top: null,
229
+ top: '0px',
230
230
  unicodeBidi: null,
231
231
  verticalAlign: null,
232
232
  visibility: null,
@@ -1,4 +1,366 @@
1
1
  $debug("Defining Document");
2
+
3
+ /**
4
+ * Defined 'globally' to this scope. Remember everything is wrapped in a closure so this doesnt show up
5
+ * in the outer most global scope.
6
+ */
7
+
8
+ /**
9
+ * process SAX events
10
+ *
11
+ * @author Jon van Noort (jon@webarcana.com.au), David Joham (djoham@yahoo.com) and Scott Severtson
12
+ *
13
+ * @param impl : DOMImplementation
14
+ * @param doc : DOMDocument - the Document to contain the parsed XML string
15
+ * @param p : XMLP - the SAX Parser
16
+ *
17
+ * @return : DOMDocument
18
+ */
19
+
20
+ function __parseLoop__(impl, doc, p, isWindowDocument) {
21
+ var iEvt, iNode, iAttr, strName;
22
+ var iNodeParent = doc;
23
+
24
+ var el_close_count = 0;
25
+
26
+ var entitiesList = new Array();
27
+ var textNodesList = new Array();
28
+
29
+ // if namespaceAware, add default namespace
30
+ if (impl.namespaceAware) {
31
+ var iNS = doc.createNamespace(""); // add the default-default namespace
32
+ iNS.value = "http://www.w3.org/2000/xmlns/";
33
+ doc._namespaces.setNamedItem(iNS);
34
+ }
35
+
36
+ // loop until SAX parser stops emitting events
37
+ var q = 0;
38
+ while(true) {
39
+ // get next event
40
+ iEvt = p.next();
41
+
42
+ if (iEvt == XMLP._ELM_B) { // Begin-Element Event
43
+ var pName = p.getName(); // get the Element name
44
+ pName = trim(pName, true, true); // strip spaces from Element name
45
+ if(pName.toLowerCase() == 'script')
46
+ p.replaceEntities = false;
47
+
48
+ if (!impl.namespaceAware) {
49
+ iNode = doc.createElement(p.getName()); // create the Element
50
+ // add attributes to Element
51
+ for(var i = 0; i < p.getAttributeCount(); i++) {
52
+ strName = p.getAttributeName(i); // get Attribute name
53
+ iAttr = iNode.getAttributeNode(strName); // if Attribute exists, use it
54
+
55
+ if(!iAttr) {
56
+ iAttr = doc.createAttribute(strName); // otherwise create it
57
+ }
58
+
59
+ iAttr.value = p.getAttributeValue(i); // set Attribute value
60
+ iNode.setAttributeNode(iAttr); // attach Attribute to Element
61
+ }
62
+ }
63
+ else { // Namespace Aware
64
+ // create element (with empty namespaceURI,
65
+ // resolve after namespace 'attributes' have been parsed)
66
+ iNode = doc.createElementNS("", p.getName());
67
+
68
+ // duplicate ParentNode's Namespace definitions
69
+ iNode._namespaces = __cloneNamedNodes__(iNodeParent._namespaces, iNode, true);
70
+
71
+ // add attributes to Element
72
+ for(var i = 0; i < p.getAttributeCount(); i++) {
73
+ strName = p.getAttributeName(i); // get Attribute name
74
+
75
+ // if attribute is a namespace declaration
76
+ if (__isNamespaceDeclaration__(strName)) {
77
+ // parse Namespace Declaration
78
+ var namespaceDec = __parseNSName__(strName);
79
+
80
+ if (strName != "xmlns") {
81
+ iNS = doc.createNamespace(strName); // define namespace
82
+ }
83
+ else {
84
+ iNS = doc.createNamespace(""); // redefine default namespace
85
+ }
86
+ iNS.value = p.getAttributeValue(i); // set value = namespaceURI
87
+
88
+ iNode._namespaces.setNamedItem(iNS); // attach namespace to namespace collection
89
+ }
90
+ else { // otherwise, it is a normal attribute
91
+ iAttr = iNode.getAttributeNode(strName); // if Attribute exists, use it
92
+
93
+ if(!iAttr) {
94
+ iAttr = doc.createAttributeNS("", strName); // otherwise create it
95
+ }
96
+
97
+ iAttr.value = p.getAttributeValue(i); // set Attribute value
98
+ iNode.setAttributeNodeNS(iAttr); // attach Attribute to Element
99
+
100
+ if (__isIdDeclaration__(strName)) {
101
+ iNode.id = p.getAttributeValue(i); // cache ID for getElementById()
102
+ }
103
+ }
104
+ }
105
+
106
+ // resolve namespaceURIs for this Element
107
+ if (iNode._namespaces.getNamedItem(iNode.prefix)) {
108
+ iNode.namespaceURI = iNode._namespaces.getNamedItem(iNode.prefix).value;
109
+ } else {
110
+ iNode.namespaceURI = iNodeParent.namespaceURI;
111
+ }
112
+
113
+ // for this Element's attributes
114
+ for (var i = 0; i < iNode.attributes.length; i++) {
115
+ if (iNode.attributes.item(i).prefix != "") { // attributes do not have a default namespace
116
+ if (iNode._namespaces.getNamedItem(iNode.attributes.item(i).prefix)) {
117
+ iNode.attributes.item(i).namespaceURI = iNode._namespaces.getNamedItem(iNode.attributes.item(i).prefix).value;
118
+ }
119
+ }
120
+ }
121
+
122
+ // We didn't know the NS of the node when we created it, which means we created a default DOM object.
123
+ // Now that we know the NS, if there is one, we clone this node so that it'll get created under
124
+ // with the right constructor. This makes things like SVG work. Might be nice not to create twice as
125
+ // as many nodes, but that's painfully given the complexity of namespaces.
126
+ if(iNode.namespaceURI != ""){
127
+ iNode = iNode.cloneNode();
128
+ }
129
+ }
130
+
131
+ // if this is the Root Element
132
+ if (iNodeParent.nodeType == DOMNode.DOCUMENT_NODE) {
133
+ iNodeParent._documentElement = iNode; // register this Element as the Document.documentElement
134
+ }
135
+
136
+ iNodeParent.appendChild(iNode); // attach Element to parentNode
137
+ iNodeParent = iNode; // descend one level of the DOM Tree
138
+ }
139
+
140
+ else if(iEvt == XMLP._ELM_E) { // End-Element Event
141
+ iNodeParent = iNodeParent.parentNode; // ascend one level of the DOM Tree
142
+ }
143
+
144
+ else if(iEvt == XMLP._ELM_EMP) { // Empty Element Event
145
+ pName = p.getName(); // get the Element name
146
+ pName = trim(pName, true, true); // strip spaces from Element name
147
+
148
+ if (!impl.namespaceAware) {
149
+ iNode = doc.createElement(pName); // create the Element
150
+
151
+ // add attributes to Element
152
+ for(var i = 0; i < p.getAttributeCount(); i++) {
153
+ strName = p.getAttributeName(i); // get Attribute name
154
+ iAttr = iNode.getAttributeNode(strName); // if Attribute exists, use it
155
+
156
+ if(!iAttr) {
157
+ iAttr = doc.createAttribute(strName); // otherwise create it
158
+ }
159
+
160
+ iAttr.value = p.getAttributeValue(i); // set Attribute value
161
+ iNode.setAttributeNode(iAttr); // attach Attribute to Element
162
+ }
163
+ }
164
+ else { // Namespace Aware
165
+ // create element (with empty namespaceURI,
166
+ // resolve after namespace 'attributes' have been parsed)
167
+ iNode = doc.createElementNS("", p.getName());
168
+
169
+ // duplicate ParentNode's Namespace definitions
170
+ iNode._namespaces = __cloneNamedNodes__(iNodeParent._namespaces, iNode);
171
+
172
+ // add attributes to Element
173
+ for(var i = 0; i < p.getAttributeCount(); i++) {
174
+ strName = p.getAttributeName(i); // get Attribute name
175
+
176
+ // if attribute is a namespace declaration
177
+ if (__isNamespaceDeclaration__(strName)) {
178
+ // parse Namespace Declaration
179
+ var namespaceDec = __parseNSName__(strName);
180
+
181
+ if (strName != "xmlns") {
182
+ iNS = doc.createNamespace(strName); // define namespace
183
+ }
184
+ else {
185
+ iNS = doc.createNamespace(""); // redefine default namespace
186
+ }
187
+ iNS.value = p.getAttributeValue(i); // set value = namespaceURI
188
+
189
+ iNode._namespaces.setNamedItem(iNS); // attach namespace to namespace collection
190
+ }
191
+ else { // otherwise, it is a normal attribute
192
+ iAttr = iNode.getAttributeNode(strName); // if Attribute exists, use it
193
+
194
+ if(!iAttr) {
195
+ iAttr = doc.createAttributeNS("", strName); // otherwise create it
196
+ }
197
+
198
+ iAttr.value = p.getAttributeValue(i); // set Attribute value
199
+ iNode.setAttributeNodeNS(iAttr); // attach Attribute to Element
200
+
201
+ if (__isIdDeclaration__(strName)) {
202
+ iNode.id = p.getAttributeValue(i); // cache ID for getElementById()
203
+ }
204
+ }
205
+ }
206
+
207
+ // resolve namespaceURIs for this Element
208
+ if (iNode._namespaces.getNamedItem(iNode.prefix)) {
209
+ iNode.namespaceURI = iNode._namespaces.getNamedItem(iNode.prefix).value;
210
+ }
211
+
212
+ // for this Element's attributes
213
+ for (var i = 0; i < iNode.attributes.length; i++) {
214
+ if (iNode.attributes.item(i).prefix != "") { // attributes do not have a default namespace
215
+ if (iNode._namespaces.getNamedItem(iNode.attributes.item(i).prefix)) {
216
+ iNode.attributes.item(i).namespaceURI = iNode._namespaces.getNamedItem(iNode.attributes.item(i).prefix).value;
217
+ }
218
+ }
219
+ }
220
+ }
221
+
222
+ // if this is the Root Element
223
+ if (iNodeParent.nodeType == DOMNode.DOCUMENT_NODE) {
224
+ iNodeParent._documentElement = iNode; // register this Element as the Document.documentElement
225
+ }
226
+
227
+ iNodeParent.appendChild(iNode); // attach Element to parentNode
228
+ }
229
+ else if(iEvt == XMLP._TEXT || iEvt == XMLP._ENTITY) { // TextNode and entity Events
230
+ // get Text content
231
+ var pContent = p.getContent().substring(p.getContentBegin(), p.getContentEnd());
232
+
233
+ if (!impl.preserveWhiteSpace ) {
234
+ if (trim(pContent, true, true) == "") {
235
+ pContent = ""; //this will cause us not to create the text node below
236
+ }
237
+ }
238
+
239
+ if (pContent.length > 0) { // ignore empty TextNodes
240
+ var textNode = doc.createTextNode(pContent);
241
+ iNodeParent.appendChild(textNode); // attach TextNode to parentNode
242
+
243
+ //the sax parser breaks up text nodes when it finds an entity. For
244
+ //example hello&lt;there will fire a text, an entity and another text
245
+ //this sucks for the dom parser because it looks to us in this logic
246
+ //as three text nodes. I fix this by keeping track of the entity nodes
247
+ //and when we're done parsing, calling normalize on their parent to
248
+ //turn the multiple text nodes into one, which is what DOM users expect
249
+ //the code to do this is at the bottom of this function
250
+ if (iEvt == XMLP._ENTITY) {
251
+ entitiesList[entitiesList.length] = textNode;
252
+ }
253
+ else {
254
+ //I can't properly decide how to handle preserve whitespace
255
+ //until the siblings of the text node are built due to
256
+ //the entitiy handling described above. I don't know that this
257
+ //will be all of the text node or not, so trimming is not appropriate
258
+ //at this time. Keep a list of all the text nodes for now
259
+ //and we'll process the preserve whitespace stuff at a later time.
260
+ textNodesList[textNodesList.length] = textNode;
261
+ }
262
+ }
263
+ }
264
+ else if(iEvt == XMLP._PI) { // ProcessingInstruction Event
265
+ // attach ProcessingInstruction to parentNode
266
+ iNodeParent.appendChild(doc.createProcessingInstruction(p.getName(), p.getContent().substring(p.getContentBegin(), p.getContentEnd())));
267
+ }
268
+ else if(iEvt == XMLP._CDATA) { // CDATA Event
269
+ // get CDATA data
270
+ pContent = p.getContent().substring(p.getContentBegin(), p.getContentEnd());
271
+
272
+ if (!impl.preserveWhiteSpace) {
273
+ pContent = trim(pContent, true, true); // trim whitespace
274
+ pContent.replace(/ +/g, ' '); // collapse multiple spaces to 1 space
275
+ }
276
+
277
+ if (pContent.length > 0) { // ignore empty CDATANodes
278
+ iNodeParent.appendChild(doc.createCDATASection(pContent)); // attach CDATA to parentNode
279
+ }
280
+ }
281
+ else if(iEvt == XMLP._COMMENT) { // Comment Event
282
+ // get COMMENT data
283
+ var pContent = p.getContent().substring(p.getContentBegin(), p.getContentEnd());
284
+
285
+ if (!impl.preserveWhiteSpace) {
286
+ pContent = trim(pContent, true, true); // trim whitespace
287
+ pContent.replace(/ +/g, ' '); // collapse multiple spaces to 1 space
288
+ }
289
+
290
+ if (pContent.length > 0) { // ignore empty CommentNodes
291
+ iNodeParent.appendChild(doc.createComment(pContent)); // attach Comment to parentNode
292
+ }
293
+ }
294
+ else if(iEvt == XMLP._DTD) { // ignore DTD events
295
+ }
296
+ else if(iEvt == XMLP._ERROR) {
297
+ $error("Fatal Error: " + p.getContent() +
298
+ "\nLine: " + p.getLineNumber() +
299
+ "\nColumn: " + p.getColumnNumber() + "\n");
300
+ throw(new DOMException(DOMException.SYNTAX_ERR));
301
+ }
302
+ else if(iEvt == XMLP._NONE) { // no more events
303
+ //steven woods notes that unclosed tags are rejected elsewhere and this check
304
+ //breaks a table patching routine
305
+ //if (iNodeParent == doc) { // confirm that we have recursed back up to root
306
+ // break;
307
+ //}
308
+ //else {
309
+ // throw(new DOMException(DOMException.SYNTAX_ERR)); // one or more Tags were not closed properly
310
+ //}
311
+ break;
312
+
313
+ }
314
+ }
315
+
316
+ //normalize any entities in the DOM to a single textNode
317
+ for (var i = 0; i < entitiesList.length; i++) {
318
+ var entity = entitiesList[i];
319
+ //its possible (if for example two entities were in the
320
+ //same domnode, that the normalize on the first entitiy
321
+ //will remove the parent for the second. Only do normalize
322
+ //if I can find a parent node
323
+ var parentNode = entity.parentNode;
324
+ if (parentNode) {
325
+ parentNode.normalize();
326
+
327
+ //now do whitespace (if necessary)
328
+ //it was not done for text nodes that have entities
329
+ if(!impl.preserveWhiteSpace) {
330
+ var children = parentNode.childNodes;
331
+ for ( var j = 0; j < children.length; j++) {
332
+ var child = children.item(j);
333
+ if (child.nodeType == DOMNode.TEXT_NODE) {
334
+ var childData = child.data;
335
+ childData.replace(/\s/g, ' ');
336
+ child.data = childData;
337
+ }
338
+ }
339
+ }
340
+ }
341
+ }
342
+
343
+ //do the preserve whitespace processing on the rest of the text nodes
344
+ //It's possible (due to the processing above) that the node will have been
345
+ //removed from the tree. Only do whitespace checking if parentNode is not null.
346
+ //This may duplicate the whitespace processing for some nodes that had entities in them
347
+ //but there's no way around that
348
+ if (!impl.preserveWhiteSpace) {
349
+ for (var i = 0; i < textNodesList.length; i++) {
350
+ var node = textNodesList[i];
351
+ if (node.parentNode != null) {
352
+ var nodeData = node.data;
353
+ nodeData.replace(/\s/g, ' ');
354
+ node.data = nodeData;
355
+ }
356
+ }
357
+
358
+ }
359
+ };
360
+
361
+
362
+
363
+
2
364
  /**
3
365
  * @class DOMDocument - The Document interface represents the entire HTML
4
366
  * or XML document. Conceptually, it is the root of the document tree,
@@ -76,11 +438,14 @@ __extend__(DOMDocument.prototype, {
76
438
  // create SAX Parser
77
439
  var parser = new XMLP(xmlString+'');
78
440
 
441
+ /*
79
442
  // create DOM Document
80
443
  if(this === $document){
81
444
  $debug("Setting internal window.document");
82
445
  $document = this;
83
446
  }
447
+ */
448
+
84
449
  // populate Document with Parsed Nodes
85
450
  try {
86
451
  // make sure thid document object is empty before we try to load ...
@@ -462,10 +827,12 @@ print("xpath failure: " + e);
462
827
  },
463
828
  get defaultView(){
464
829
  var doc = this;
465
- return { getComputedStyle: function(elem){
466
- return doc._parentWindow.getComputedStyle(elem);
467
- }};
468
- },
830
+ return {
831
+ document: this,
832
+ getComputedStyle: function(elem){
833
+ return doc._parentWindow.getComputedStyle(elem);
834
+ }};
835
+ },
469
836
  _genId : function() {
470
837
  this._lastId += 1; // increment lastId (to generate unique id)
471
838
  return this._lastId;
@@ -544,6 +911,109 @@ function __parseQName__(qualifiedName) {
544
911
  return resultQName;
545
912
  };
546
913
 
914
+ /**
915
+ * @method DOMImplementation._isNamespaceDeclaration - Return true, if attributeName is a namespace declaration
916
+ * @author Jon van Noort (jon@webarcana.com.au)
917
+ * @param attributeName : string - the attribute name
918
+ * @return : boolean
919
+ */
920
+ function __isNamespaceDeclaration__(attributeName) {
921
+ // test if attributeName is 'xmlns'
922
+ return (attributeName.indexOf('xmlns') > -1);
923
+ };
924
+
925
+ /**
926
+ * @method DOMImplementation._isIdDeclaration - Return true, if attributeName is an id declaration
927
+ * @author Jon van Noort (jon@webarcana.com.au)
928
+ * @param attributeName : string - the attribute name
929
+ * @return : boolean
930
+ */
931
+ function __isIdDeclaration__(attributeName) {
932
+ // test if attributeName is 'id' (case insensitive)
933
+ return attributeName?(attributeName.toLowerCase() == 'id'):false;
934
+ };
935
+
936
+ /**
937
+ * @method DOMImplementation._isValidName - Return true,
938
+ * if name contains no invalid characters
939
+ * @author Jon van Noort (jon@webarcana.com.au)
940
+ * @param name : string - the candidate name
941
+ * @return : boolean
942
+ */
943
+ function __isValidName__(name) {
944
+ // test if name contains only valid characters
945
+ return name.match(re_validName);
946
+ };
947
+ var re_validName = /^[a-zA-Z_:][a-zA-Z0-9\.\-_:]*$/;
948
+
949
+ /**
950
+ * @method DOMImplementation._isValidString - Return true, if string does not contain any illegal chars
951
+ * All of the characters 0 through 31 and character 127 are nonprinting control characters.
952
+ * With the exception of characters 09, 10, and 13, (Ox09, Ox0A, and Ox0D)
953
+ * Note: different from _isValidName in that ValidStrings may contain spaces
954
+ * @author Jon van Noort (jon@webarcana.com.au)
955
+ * @param name : string - the candidate string
956
+ * @return : boolean
957
+ */
958
+ function __isValidString__(name) {
959
+ // test that string does not contains invalid characters
960
+ return (name.search(re_invalidStringChars) < 0);
961
+ };
962
+ var re_invalidStringChars = /\x01|\x02|\x03|\x04|\x05|\x06|\x07|\x08|\x0B|\x0C|\x0E|\x0F|\x10|\x11|\x12|\x13|\x14|\x15|\x16|\x17|\x18|\x19|\x1A|\x1B|\x1C|\x1D|\x1E|\x1F|\x7F/;
963
+
964
+ /**
965
+ * @method DOMImplementation._parseNSName - parse the namespace name.
966
+ * if there is no colon, the
967
+ * @author Jon van Noort (jon@webarcana.com.au)
968
+ * @param qualifiedName : string - The qualified name
969
+ * @return : NSName - [
970
+ .prefix : string - The prefix part of the qname
971
+ .namespaceName : string - The namespaceURI part of the qname
972
+ ]
973
+ */
974
+ function __parseNSName__(qualifiedName) {
975
+ var resultNSName = new Object();
976
+
977
+ resultNSName.prefix = qualifiedName; // unless the qname has a namespaceName, the prefix is the entire String
978
+ resultNSName.namespaceName = "";
979
+
980
+ // split on ':'
981
+ var delimPos = qualifiedName.indexOf(':');
982
+ if (delimPos > -1) {
983
+ // get prefix
984
+ resultNSName.prefix = qualifiedName.substring(0, delimPos);
985
+ // get namespaceName
986
+ resultNSName.namespaceName = qualifiedName.substring(delimPos +1, qualifiedName.length);
987
+ }
988
+ return resultNSName;
989
+ };
990
+
991
+ /**
992
+ * @method DOMImplementation._parseQName - parse the qualified name
993
+ * @author Jon van Noort (jon@webarcana.com.au)
994
+ * @param qualifiedName : string - The qualified name
995
+ * @return : QName
996
+ */
997
+ function __parseQName__(qualifiedName) {
998
+ var resultQName = new Object();
999
+
1000
+ resultQName.localName = qualifiedName; // unless the qname has a prefix, the local name is the entire String
1001
+ resultQName.prefix = "";
1002
+
1003
+ // split on ':'
1004
+ var delimPos = qualifiedName.indexOf(':');
1005
+
1006
+ if (delimPos > -1) {
1007
+ // get prefix
1008
+ resultQName.prefix = qualifiedName.substring(0, delimPos);
1009
+
1010
+ // get localName
1011
+ resultQName.localName = qualifiedName.substring(delimPos +1, qualifiedName.length);
1012
+ }
1013
+
1014
+ return resultQName;
1015
+ };
1016
+
547
1017
  // $w.Document = DOMDocument;
548
1018
 
549
1019
  // Local Variables: