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.
@@ -37,7 +37,7 @@ __extend__(DOMImplementation.prototype,{
37
37
  createDocument : function(nsuri, qname, doctype){
38
38
  //TODO - this currently returns an empty doc
39
39
  //but needs to handle the args
40
- return new Document(this, null);
40
+ return new DOMDocument(this, null);
41
41
  },
42
42
  createHTMLDocument : function(title){
43
43
  // N.B. explict window on purpose ...
@@ -126,468 +126,6 @@ __extend__(DOMImplementation.prototype,{
126
126
  });
127
127
 
128
128
 
129
- /**
130
- * Defined 'globally' to this scope. Remember everything is wrapped in a closure so this doesnt show up
131
- * in the outer most global scope.
132
- */
133
-
134
- /**
135
- * process SAX events
136
- *
137
- * @author Jon van Noort (jon@webarcana.com.au), David Joham (djoham@yahoo.com) and Scott Severtson
138
- *
139
- * @param impl : DOMImplementation
140
- * @param doc : DOMDocument - the Document to contain the parsed XML string
141
- * @param p : XMLP - the SAX Parser
142
- *
143
- * @return : DOMDocument
144
- */
145
-
146
- function __parseLoop__(impl, doc, p, isWindowDocument) {
147
- var iEvt, iNode, iAttr, strName;
148
- var iNodeParent = doc;
149
-
150
- var el_close_count = 0;
151
-
152
- var entitiesList = new Array();
153
- var textNodesList = new Array();
154
-
155
- // if namespaceAware, add default namespace
156
- if (impl.namespaceAware) {
157
- var iNS = doc.createNamespace(""); // add the default-default namespace
158
- iNS.value = "http://www.w3.org/2000/xmlns/";
159
- doc._namespaces.setNamedItem(iNS);
160
- }
161
-
162
- // loop until SAX parser stops emitting events
163
- var q = 0;
164
- while(true) {
165
- // get next event
166
- iEvt = p.next();
167
-
168
- if (iEvt == XMLP._ELM_B) { // Begin-Element Event
169
- var pName = p.getName(); // get the Element name
170
- pName = trim(pName, true, true); // strip spaces from Element name
171
- if(pName.toLowerCase() == 'script')
172
- p.replaceEntities = false;
173
-
174
- if (!impl.namespaceAware) {
175
- iNode = doc.createElement(p.getName()); // create the Element
176
- // add attributes to Element
177
- for(var i = 0; i < p.getAttributeCount(); i++) {
178
- strName = p.getAttributeName(i); // get Attribute name
179
- iAttr = iNode.getAttributeNode(strName); // if Attribute exists, use it
180
-
181
- if(!iAttr) {
182
- iAttr = doc.createAttribute(strName); // otherwise create it
183
- }
184
-
185
- iAttr.value = p.getAttributeValue(i); // set Attribute value
186
- iNode.setAttributeNode(iAttr); // attach Attribute to Element
187
- }
188
- }
189
- else { // Namespace Aware
190
- // create element (with empty namespaceURI,
191
- // resolve after namespace 'attributes' have been parsed)
192
- iNode = doc.createElementNS("", p.getName());
193
-
194
- // duplicate ParentNode's Namespace definitions
195
- iNode._namespaces = __cloneNamedNodes__(iNodeParent._namespaces, iNode, true);
196
-
197
- // add attributes to Element
198
- for(var i = 0; i < p.getAttributeCount(); i++) {
199
- strName = p.getAttributeName(i); // get Attribute name
200
-
201
- // if attribute is a namespace declaration
202
- if (__isNamespaceDeclaration__(strName)) {
203
- // parse Namespace Declaration
204
- var namespaceDec = __parseNSName__(strName);
205
-
206
- if (strName != "xmlns") {
207
- iNS = doc.createNamespace(strName); // define namespace
208
- }
209
- else {
210
- iNS = doc.createNamespace(""); // redefine default namespace
211
- }
212
- iNS.value = p.getAttributeValue(i); // set value = namespaceURI
213
-
214
- iNode._namespaces.setNamedItem(iNS); // attach namespace to namespace collection
215
- }
216
- else { // otherwise, it is a normal attribute
217
- iAttr = iNode.getAttributeNode(strName); // if Attribute exists, use it
218
-
219
- if(!iAttr) {
220
- iAttr = doc.createAttributeNS("", strName); // otherwise create it
221
- }
222
-
223
- iAttr.value = p.getAttributeValue(i); // set Attribute value
224
- iNode.setAttributeNodeNS(iAttr); // attach Attribute to Element
225
-
226
- if (__isIdDeclaration__(strName)) {
227
- iNode.id = p.getAttributeValue(i); // cache ID for getElementById()
228
- }
229
- }
230
- }
231
-
232
- // resolve namespaceURIs for this Element
233
- if (iNode._namespaces.getNamedItem(iNode.prefix)) {
234
- iNode.namespaceURI = iNode._namespaces.getNamedItem(iNode.prefix).value;
235
- } else {
236
- iNode.namespaceURI = iNodeParent.namespaceURI;
237
- }
238
-
239
- // for this Element's attributes
240
- for (var i = 0; i < iNode.attributes.length; i++) {
241
- if (iNode.attributes.item(i).prefix != "") { // attributes do not have a default namespace
242
- if (iNode._namespaces.getNamedItem(iNode.attributes.item(i).prefix)) {
243
- iNode.attributes.item(i).namespaceURI = iNode._namespaces.getNamedItem(iNode.attributes.item(i).prefix).value;
244
- }
245
- }
246
- }
247
-
248
- // We didn't know the NS of the node when we created it, which means we created a default DOM object.
249
- // Now that we know the NS, if there is one, we clone this node so that it'll get created under
250
- // with the right constructor. This makes things like SVG work. Might be nice not to create twice as
251
- // as many nodes, but that's painfully given the complexity of namespaces.
252
- if(iNode.namespaceURI != ""){
253
- iNode = iNode.cloneNode();
254
- }
255
- }
256
-
257
- // if this is the Root Element
258
- if (iNodeParent.nodeType == DOMNode.DOCUMENT_NODE) {
259
- iNodeParent._documentElement = iNode; // register this Element as the Document.documentElement
260
- }
261
-
262
- iNodeParent.appendChild(iNode); // attach Element to parentNode
263
- iNodeParent = iNode; // descend one level of the DOM Tree
264
- }
265
-
266
- else if(iEvt == XMLP._ELM_E) { // End-Element Event
267
- iNodeParent = iNodeParent.parentNode; // ascend one level of the DOM Tree
268
- }
269
-
270
- else if(iEvt == XMLP._ELM_EMP) { // Empty Element Event
271
- pName = p.getName(); // get the Element name
272
- pName = trim(pName, true, true); // strip spaces from Element name
273
-
274
- if (!impl.namespaceAware) {
275
- iNode = doc.createElement(pName); // create the Element
276
-
277
- // add attributes to Element
278
- for(var i = 0; i < p.getAttributeCount(); i++) {
279
- strName = p.getAttributeName(i); // get Attribute name
280
- iAttr = iNode.getAttributeNode(strName); // if Attribute exists, use it
281
-
282
- if(!iAttr) {
283
- iAttr = doc.createAttribute(strName); // otherwise create it
284
- }
285
-
286
- iAttr.value = p.getAttributeValue(i); // set Attribute value
287
- iNode.setAttributeNode(iAttr); // attach Attribute to Element
288
- }
289
- }
290
- else { // Namespace Aware
291
- // create element (with empty namespaceURI,
292
- // resolve after namespace 'attributes' have been parsed)
293
- iNode = doc.createElementNS("", p.getName());
294
-
295
- // duplicate ParentNode's Namespace definitions
296
- iNode._namespaces = __cloneNamedNodes__(iNodeParent._namespaces, iNode);
297
-
298
- // add attributes to Element
299
- for(var i = 0; i < p.getAttributeCount(); i++) {
300
- strName = p.getAttributeName(i); // get Attribute name
301
-
302
- // if attribute is a namespace declaration
303
- if (__isNamespaceDeclaration__(strName)) {
304
- // parse Namespace Declaration
305
- var namespaceDec = __parseNSName__(strName);
306
-
307
- if (strName != "xmlns") {
308
- iNS = doc.createNamespace(strName); // define namespace
309
- }
310
- else {
311
- iNS = doc.createNamespace(""); // redefine default namespace
312
- }
313
- iNS.value = p.getAttributeValue(i); // set value = namespaceURI
314
-
315
- iNode._namespaces.setNamedItem(iNS); // attach namespace to namespace collection
316
- }
317
- else { // otherwise, it is a normal attribute
318
- iAttr = iNode.getAttributeNode(strName); // if Attribute exists, use it
319
-
320
- if(!iAttr) {
321
- iAttr = doc.createAttributeNS("", strName); // otherwise create it
322
- }
323
-
324
- iAttr.value = p.getAttributeValue(i); // set Attribute value
325
- iNode.setAttributeNodeNS(iAttr); // attach Attribute to Element
326
-
327
- if (__isIdDeclaration__(strName)) {
328
- iNode.id = p.getAttributeValue(i); // cache ID for getElementById()
329
- }
330
- }
331
- }
332
-
333
- // resolve namespaceURIs for this Element
334
- if (iNode._namespaces.getNamedItem(iNode.prefix)) {
335
- iNode.namespaceURI = iNode._namespaces.getNamedItem(iNode.prefix).value;
336
- }
337
-
338
- // for this Element's attributes
339
- for (var i = 0; i < iNode.attributes.length; i++) {
340
- if (iNode.attributes.item(i).prefix != "") { // attributes do not have a default namespace
341
- if (iNode._namespaces.getNamedItem(iNode.attributes.item(i).prefix)) {
342
- iNode.attributes.item(i).namespaceURI = iNode._namespaces.getNamedItem(iNode.attributes.item(i).prefix).value;
343
- }
344
- }
345
- }
346
- }
347
-
348
- // if this is the Root Element
349
- if (iNodeParent.nodeType == DOMNode.DOCUMENT_NODE) {
350
- iNodeParent._documentElement = iNode; // register this Element as the Document.documentElement
351
- }
352
-
353
- iNodeParent.appendChild(iNode); // attach Element to parentNode
354
- }
355
- else if(iEvt == XMLP._TEXT || iEvt == XMLP._ENTITY) { // TextNode and entity Events
356
- // get Text content
357
- var pContent = p.getContent().substring(p.getContentBegin(), p.getContentEnd());
358
-
359
- if (!impl.preserveWhiteSpace ) {
360
- if (trim(pContent, true, true) == "") {
361
- pContent = ""; //this will cause us not to create the text node below
362
- }
363
- }
364
-
365
- if (pContent.length > 0) { // ignore empty TextNodes
366
- var textNode = doc.createTextNode(pContent);
367
- iNodeParent.appendChild(textNode); // attach TextNode to parentNode
368
-
369
- //the sax parser breaks up text nodes when it finds an entity. For
370
- //example hello&lt;there will fire a text, an entity and another text
371
- //this sucks for the dom parser because it looks to us in this logic
372
- //as three text nodes. I fix this by keeping track of the entity nodes
373
- //and when we're done parsing, calling normalize on their parent to
374
- //turn the multiple text nodes into one, which is what DOM users expect
375
- //the code to do this is at the bottom of this function
376
- if (iEvt == XMLP._ENTITY) {
377
- entitiesList[entitiesList.length] = textNode;
378
- }
379
- else {
380
- //I can't properly decide how to handle preserve whitespace
381
- //until the siblings of the text node are built due to
382
- //the entitiy handling described above. I don't know that this
383
- //will be all of the text node or not, so trimming is not appropriate
384
- //at this time. Keep a list of all the text nodes for now
385
- //and we'll process the preserve whitespace stuff at a later time.
386
- textNodesList[textNodesList.length] = textNode;
387
- }
388
- }
389
- }
390
- else if(iEvt == XMLP._PI) { // ProcessingInstruction Event
391
- // attach ProcessingInstruction to parentNode
392
- iNodeParent.appendChild(doc.createProcessingInstruction(p.getName(), p.getContent().substring(p.getContentBegin(), p.getContentEnd())));
393
- }
394
- else if(iEvt == XMLP._CDATA) { // CDATA Event
395
- // get CDATA data
396
- pContent = p.getContent().substring(p.getContentBegin(), p.getContentEnd());
397
-
398
- if (!impl.preserveWhiteSpace) {
399
- pContent = trim(pContent, true, true); // trim whitespace
400
- pContent.replace(/ +/g, ' '); // collapse multiple spaces to 1 space
401
- }
402
-
403
- if (pContent.length > 0) { // ignore empty CDATANodes
404
- iNodeParent.appendChild(doc.createCDATASection(pContent)); // attach CDATA to parentNode
405
- }
406
- }
407
- else if(iEvt == XMLP._COMMENT) { // Comment Event
408
- // get COMMENT data
409
- var pContent = p.getContent().substring(p.getContentBegin(), p.getContentEnd());
410
-
411
- if (!impl.preserveWhiteSpace) {
412
- pContent = trim(pContent, true, true); // trim whitespace
413
- pContent.replace(/ +/g, ' '); // collapse multiple spaces to 1 space
414
- }
415
-
416
- if (pContent.length > 0) { // ignore empty CommentNodes
417
- iNodeParent.appendChild(doc.createComment(pContent)); // attach Comment to parentNode
418
- }
419
- }
420
- else if(iEvt == XMLP._DTD) { // ignore DTD events
421
- }
422
- else if(iEvt == XMLP._ERROR) {
423
- $error("Fatal Error: " + p.getContent() +
424
- "\nLine: " + p.getLineNumber() +
425
- "\nColumn: " + p.getColumnNumber() + "\n");
426
- throw(new DOMException(DOMException.SYNTAX_ERR));
427
- }
428
- else if(iEvt == XMLP._NONE) { // no more events
429
- //steven woods notes that unclosed tags are rejected elsewhere and this check
430
- //breaks a table patching routine
431
- //if (iNodeParent == doc) { // confirm that we have recursed back up to root
432
- // break;
433
- //}
434
- //else {
435
- // throw(new DOMException(DOMException.SYNTAX_ERR)); // one or more Tags were not closed properly
436
- //}
437
- break;
438
-
439
- }
440
- }
441
-
442
- //normalize any entities in the DOM to a single textNode
443
- for (var i = 0; i < entitiesList.length; i++) {
444
- var entity = entitiesList[i];
445
- //its possible (if for example two entities were in the
446
- //same domnode, that the normalize on the first entitiy
447
- //will remove the parent for the second. Only do normalize
448
- //if I can find a parent node
449
- var parentNode = entity.parentNode;
450
- if (parentNode) {
451
- parentNode.normalize();
452
-
453
- //now do whitespace (if necessary)
454
- //it was not done for text nodes that have entities
455
- if(!impl.preserveWhiteSpace) {
456
- var children = parentNode.childNodes;
457
- for ( var j = 0; j < children.length; j++) {
458
- var child = children.item(j);
459
- if (child.nodeType == DOMNode.TEXT_NODE) {
460
- var childData = child.data;
461
- childData.replace(/\s/g, ' ');
462
- child.data = childData;
463
- }
464
- }
465
- }
466
- }
467
- }
468
-
469
- //do the preserve whitespace processing on the rest of the text nodes
470
- //It's possible (due to the processing above) that the node will have been
471
- //removed from the tree. Only do whitespace checking if parentNode is not null.
472
- //This may duplicate the whitespace processing for some nodes that had entities in them
473
- //but there's no way around that
474
- if (!impl.preserveWhiteSpace) {
475
- for (var i = 0; i < textNodesList.length; i++) {
476
- var node = textNodesList[i];
477
- if (node.parentNode != null) {
478
- var nodeData = node.data;
479
- nodeData.replace(/\s/g, ' ');
480
- node.data = nodeData;
481
- }
482
- }
483
-
484
- }
485
- };
486
-
487
-
488
- /**
489
- * @method DOMImplementation._isNamespaceDeclaration - Return true, if attributeName is a namespace declaration
490
- * @author Jon van Noort (jon@webarcana.com.au)
491
- * @param attributeName : string - the attribute name
492
- * @return : boolean
493
- */
494
- function __isNamespaceDeclaration__(attributeName) {
495
- // test if attributeName is 'xmlns'
496
- return (attributeName.indexOf('xmlns') > -1);
497
- };
498
-
499
- /**
500
- * @method DOMImplementation._isIdDeclaration - Return true, if attributeName is an id declaration
501
- * @author Jon van Noort (jon@webarcana.com.au)
502
- * @param attributeName : string - the attribute name
503
- * @return : boolean
504
- */
505
- function __isIdDeclaration__(attributeName) {
506
- // test if attributeName is 'id' (case insensitive)
507
- return attributeName?(attributeName.toLowerCase() == 'id'):false;
508
- };
509
-
510
- /**
511
- * @method DOMImplementation._isValidName - Return true,
512
- * if name contains no invalid characters
513
- * @author Jon van Noort (jon@webarcana.com.au)
514
- * @param name : string - the candidate name
515
- * @return : boolean
516
- */
517
- function __isValidName__(name) {
518
- // test if name contains only valid characters
519
- return name.match(re_validName);
520
- };
521
- var re_validName = /^[a-zA-Z_:][a-zA-Z0-9\.\-_:]*$/;
522
-
523
- /**
524
- * @method DOMImplementation._isValidString - Return true, if string does not contain any illegal chars
525
- * All of the characters 0 through 31 and character 127 are nonprinting control characters.
526
- * With the exception of characters 09, 10, and 13, (Ox09, Ox0A, and Ox0D)
527
- * Note: different from _isValidName in that ValidStrings may contain spaces
528
- * @author Jon van Noort (jon@webarcana.com.au)
529
- * @param name : string - the candidate string
530
- * @return : boolean
531
- */
532
- function __isValidString__(name) {
533
- // test that string does not contains invalid characters
534
- return (name.search(re_invalidStringChars) < 0);
535
- };
536
- 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/;
537
-
538
- /**
539
- * @method DOMImplementation._parseNSName - parse the namespace name.
540
- * if there is no colon, the
541
- * @author Jon van Noort (jon@webarcana.com.au)
542
- * @param qualifiedName : string - The qualified name
543
- * @return : NSName - [
544
- .prefix : string - The prefix part of the qname
545
- .namespaceName : string - The namespaceURI part of the qname
546
- ]
547
- */
548
- function __parseNSName__(qualifiedName) {
549
- var resultNSName = new Object();
550
-
551
- resultNSName.prefix = qualifiedName; // unless the qname has a namespaceName, the prefix is the entire String
552
- resultNSName.namespaceName = "";
553
-
554
- // split on ':'
555
- var delimPos = qualifiedName.indexOf(':');
556
- if (delimPos > -1) {
557
- // get prefix
558
- resultNSName.prefix = qualifiedName.substring(0, delimPos);
559
- // get namespaceName
560
- resultNSName.namespaceName = qualifiedName.substring(delimPos +1, qualifiedName.length);
561
- }
562
- return resultNSName;
563
- };
564
-
565
- /**
566
- * @method DOMImplementation._parseQName - parse the qualified name
567
- * @author Jon van Noort (jon@webarcana.com.au)
568
- * @param qualifiedName : string - The qualified name
569
- * @return : QName
570
- */
571
- function __parseQName__(qualifiedName) {
572
- var resultQName = new Object();
573
-
574
- resultQName.localName = qualifiedName; // unless the qname has a prefix, the local name is the entire String
575
- resultQName.prefix = "";
576
-
577
- // split on ':'
578
- var delimPos = qualifiedName.indexOf(':');
579
-
580
- if (delimPos > -1) {
581
- // get prefix
582
- resultQName.prefix = qualifiedName.substring(0, delimPos);
583
-
584
- // get localName
585
- resultQName.localName = qualifiedName.substring(delimPos +1, qualifiedName.length);
586
- }
587
-
588
- return resultQName;
589
- };
590
-
591
129
  if(false){
592
130
  $debug("Initializing document.implementation");
593
131
  var $implementation = new DOMImplementation();