You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by an...@apache.org on 2006/11/20 16:59:34 UTC

svn commit: r477229 - in /tapestry/tapestry4/trunk/tapestry-framework/src/js: tapestry/html.js tests/test_html.js

Author: andyhot
Date: Mon Nov 20 07:59:30 2006
New Revision: 477229

URL: http://svn.apache.org/viewvc?view=rev&rev=477229
Log:
doc on getElementAsString...other functions made 'private'

Modified:
    tapestry/tapestry4/trunk/tapestry-framework/src/js/tapestry/html.js
    tapestry/tapestry4/trunk/tapestry-framework/src/js/tests/test_html.js

Modified: tapestry/tapestry4/trunk/tapestry-framework/src/js/tapestry/html.js
URL: http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-framework/src/js/tapestry/html.js?view=diff&rev=477229&r1=477228&r2=477229
==============================================================================
--- tapestry/tapestry4/trunk/tapestry-framework/src/js/tapestry/html.js (original)
+++ tapestry/tapestry4/trunk/tapestry-framework/src/js/tapestry/html.js Mon Nov 20 07:59:30 2006
@@ -3,8 +3,6 @@
 /**
  * package: tapestry.html
  * Provides functionality related to parsing and rendering dom nodes.
- *
- * TODO: Mark all functions (apart from getContentAsString) as 'private'.
  */
 tapestry.html={
 	
@@ -15,49 +13,88 @@
          *
          * The resulting string does NOT contain any markup (or attributes) of
          * the given node - only child nodes are rendered and returned.
+         *
+         * Implementation Note: This function tries to make use of browser 
+         * specific features (the xml attribute of nodes in IE and the XMLSerializer
+         * object in Mozilla derivatives) - if those fails, a generic implementation
+         * is guaranteed to work in all platforms.
 	 * 
 	 * Parameters: 
 	 * 
 	 *	node - The dom node.
 	 * Returns:
 	 * 
-	 * A string with the html content of the given node.
+	 * The string representation of the given node's contents.
 	 */    
 	getContentAsString:function(node){
 		if (typeof node.xml != "undefined")
-			return this.getContentAsStringIE(node);
+			return this._getContentAsStringIE(node);
 		else if (typeof XMLSerializer != "undefined" )
-			return this.getContentAsStringMozilla(node);
+			return this._getContentAsStringMozilla(node);
 		else
-			return this.getContentAsStringGeneric(node);
-	},
+			return this._getContentAsStringGeneric(node);
+	},        
+	
+        /**
+	 * Function: getElementAsString
+	 * 
+	 * Takes a dom node and returns itself and its contents rendered in a string.
+         *
+         * Implementation Note: This function uses a generic implementation in order
+         * to generate the returned string.
+	 * 
+	 * Parameters: 
+	 * 
+	 *	node - The dom node.
+	 * Returns:
+	 * 
+	 * The string representation of the given node.
+	 */         
+	getElementAsString:function(node){
+		if (!node) { return ""; }
+		
+		var s='<' + node.nodeName;
+		// add attributes
+		if (node.attributes && node.attributes.length > 0) {
+			for (var i=0; i < node.attributes.length; i++) {
+				s += " " + node.attributes[i].name + "=\"" + node.attributes[i].value + "\"";	
+			}
+		}
+		// close start tag
+		s += '>';
+		// content of tag
+		s += this._getContentAsStringGeneric(node);
+		// end tag
+		s += '</' + node.nodeName + '>';
+		return s;
+	},        
 
-	getContentAsStringIE:function(node){
+	_getContentAsStringIE:function(node){
 		var s="";
     	for (var i = 0; i < node.childNodes.length; i++)
         	s += node.childNodes[i].xml;
     	return s;
 	},
 	
-	getContentAsStringMozilla:function(node){
+	_getContentAsStringMozilla:function(node){
 		var xmlSerializer = new XMLSerializer();
 	    var s = "";
 	    for (var i = 0; i < node.childNodes.length; i++) {
 	        s += xmlSerializer.serializeToString(node.childNodes[i]);
 	        if (s == "undefined")
-		        return this.getContentAsStringGeneric(node);
+		        return this._getContentAsStringGeneric(node);
 	    }
 	    return s;
 	},
 	
-	getContentAsStringGeneric:function(node){
+	_getContentAsStringGeneric:function(node){
 		var s="";
 		if (node == null) { return s; }
 		for (var i = 0; i < node.childNodes.length; i++) {
 			switch (node.childNodes[i].nodeType) {
 				case 1: // ELEMENT_NODE
 				case 5: // ENTITY_REFERENCE_NODE
-					s += this.getElementAsStringGeneric(node.childNodes[i]);
+					s += this.getElementAsString(node.childNodes[i]);
 					break;
 				case 3: // TEXT_NODE
 				case 2: // ATTRIBUTE_NODE
@@ -69,24 +106,5 @@
 			}
 		}
 		return s;	
-	},
-	
-	getElementAsStringGeneric:function(node){
-		if (!node) { return ""; }
-		
-		var s='<' + node.nodeName;
-		// add attributes
-		if (node.attributes && node.attributes.length > 0) {
-			for (var i=0; i < node.attributes.length; i++) {
-				s += " " + node.attributes[i].name + "=\"" + node.attributes[i].value + "\"";	
-			}
-		}
-		// close start tag
-		s += '>';
-		// content of tag
-		s += this.getContentAsStringGeneric(node);
-		// end tag
-		s += '</' + node.nodeName + '>';
-		return s;
 	}
 }

Modified: tapestry/tapestry4/trunk/tapestry-framework/src/js/tests/test_html.js
URL: http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-framework/src/js/tests/test_html.js?view=diff&rev=477229&r1=477228&r2=477229
==============================================================================
--- tapestry/tapestry4/trunk/tapestry-framework/src/js/tests/test_html.js (original)
+++ tapestry/tapestry4/trunk/tapestry-framework/src/js/tests/test_html.js Mon Nov 20 07:59:30 2006
@@ -6,16 +6,39 @@
 
 
 function test_html_getContentAsString(){
+    
+        var node = _createTestNode();
+        
+        var data = tapestry.html.getContentAsString(node).toLowerCase();
+        jum.assertEquals("<div id=\"testid2\">content</div>", data);
+}
+
+function test_html_textarea(){
+    
+        var node = _createTestNode("textarea", true);
+        
+        var data = tapestry.html.getContentAsString(node).toLowerCase();
+        jum.assertEquals("<textarea id=\"testid2\"></textarea>", data);
+        // cannot test Mozilla's getContentAsString from here... 
+        // only browser based tests will show if this is working
+}
+
+function _createTestNode(element, empty){
 	var node = document.createElement("div");
 	node.setAttribute("id", "testid");
         
-	var node2 = document.createElement("div");
+        if (!element)
+            element="div";
+        
+	var node2 = document.createElement(element);
 	node2.setAttribute("id", "testid2");
-        var content = document.createTextNode("content");
-        node2.appendChild(content);
+        
+        if (!empty){
+            var content = document.createTextNode("content");
+            node2.appendChild(content);
+        }
         
         node.appendChild(node2);
         
-        var data = tapestry.html.getContentAsString(node).toLowerCase();
-        jum.assertEquals("<div id=\"testid2\">content</div>", data);
+        return node;
 }