You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by hl...@apache.org on 2008/09/04 23:15:01 UTC

svn commit: r692249 - in /tapestry/tapestry5/trunk/tapestry-core/src: main/java/org/apache/tapestry5/dom/Element.java main/java/org/apache/tapestry5/dom/Node.java test/java/org/apache/tapestry5/dom/DOMTest.java

Author: hlship
Date: Thu Sep  4 14:15:01 2008
New Revision: 692249

URL: http://svn.apache.org/viewvc?rev=692249&view=rev
Log:
TAPESTRY-2541: Tapestry DOM needs basic methods to manipulate the DOM post-render

Modified:
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/dom/Element.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/dom/Node.java
    tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/dom/DOMTest.java

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/dom/Element.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/dom/Element.java?rev=692249&r1=692248&r2=692249&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/dom/Element.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/dom/Element.java Thu Sep  4 14:15:01 2008
@@ -124,9 +124,9 @@
      * @param value the value for the attribute. A value of null is allowed, and no attribute will be added to the
      *              element.
      */
-    public void attribute(String name, String value)
+    public Element attribute(String name, String value)
     {
-        attribute(null, name, value);
+        return attribute(null, name, value);
     }
 
     /**
@@ -137,15 +137,17 @@
      * @param value     the value for the attribute. A value of null is allowed, and no attribute will be added to the
      *                  element.
      */
-    public void attribute(String namespace, String name, String value)
+    public Element attribute(String namespace, String name, String value)
     {
         notBlank(name, "name");
 
-        if (value == null) return;
+        if (value == null) return this;
 
         if (attributes == null) attributes = newMap();
 
         if (!attributes.containsKey(name)) attributes.put(name, new Attribute(namespace, name, value));
+
+        return this;
     }
 
 
@@ -154,7 +156,7 @@
      *
      * @param namesAndValues alternating attribute names and attribute values
      */
-    public void attributes(String... namesAndValues)
+    public Element attributes(String... namesAndValues)
     {
         int i = 0;
         while (i < namesAndValues.length)
@@ -164,12 +166,14 @@
 
             attribute(name, value);
         }
+
+        return this;
     }
 
     /**
      * Forces changes to a number of attributes. The new attributes <em>overwrite</em> previous values.
      */
-    public void forceAttributes(String... namesAndValues)
+    public Element forceAttributes(String... namesAndValues)
     {
         if (attributes == null) attributes = newMap();
 
@@ -188,6 +192,8 @@
 
             attributes.put(name, new Attribute(null, name, value));
         }
+
+        return this;
     }
 
     /**

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/dom/Node.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/dom/Node.java?rev=692249&r1=692248&r2=692249&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/dom/Node.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/dom/Node.java Thu Sep  4 14:15:01 2008
@@ -279,7 +279,7 @@
     }
 
 
-    private int indexOfNode(Node node)
+    int indexOfNode(Node node)
     {
         ensureChildren();
 
@@ -294,4 +294,28 @@
     {
         children = null;
     }
+
+    /**
+     * Wraps a node inside a new element.  The new element is created before the node, then the node is moved inside the
+     * new element.
+     *
+     * @param elementName    name of new element to create
+     * @param namesAndValues to set attributes of new element
+     * @return the created element
+     */
+    public Element wrap(String elementName, String... namesAndValues)
+    {
+        Element containerElement = container.asElement();
+
+        int index = containerElement.indexOfNode(this);
+
+        // Insert the new element just before this node.
+        Element element =
+                containerElement.elementAt(index, elementName, namesAndValues);
+
+        // Move this node inside the new element.
+        moveToTop(element);
+
+        return element;
+    }
 }

Modified: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/dom/DOMTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/dom/DOMTest.java?rev=692249&r1=692248&r2=692249&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/dom/DOMTest.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/dom/DOMTest.java Thu Sep  4 14:15:01 2008
@@ -624,4 +624,26 @@
             assertEquals(ex.getMessage(), "Unable to move a node relative to itself.");
         }
     }
+
+    @Test
+    public void wrap()
+    {
+        Document d = new Document();
+
+        Element root = d.newRootElement("doc");
+
+        Element target = root.element("target");
+        target.element("placeholder");
+        Element mobile = root.element("source").element("mobile");
+
+        Node text = mobile.text("On the move");
+
+        assertEquals(d.toString(),
+                     "<doc><target><placeholder></placeholder></target><source><mobile>On the move</mobile></source></doc>");
+
+        text.wrap("em", "class", "bold");
+
+        assertEquals(d.toString(),
+                     "<doc><target><placeholder></placeholder></target><source><mobile><em class=\"bold\">On the move</em></mobile></source></doc>");
+    }
 }