### Eclipse Workspace Patch 1.0
#P org.jboss.tools.vpe
Index: src/org/jboss/tools/vpe/editor/mapping/VpeDomMapping.java
===================================================================
--- src/org/jboss/tools/vpe/editor/mapping/VpeDomMapping.java	(revision 36825)
+++ src/org/jboss/tools/vpe/editor/mapping/VpeDomMapping.java	(working copy)
@@ -72,19 +72,11 @@
 	public VpeNodeMapping getNodeMappingAtVisualNode(nsIDOMNode visualNode) {
 		VpeNodeMapping nodeMapping = null;
 		/*
-		 * See https://issues.jboss.org/browse/JBIDE-9807
-		 * Method map.get(key) doesn't work correctly for this situation,
-		 * it happens because "visualMap.keySet().contains(visualNode)" 
-		 * not always correctly determines the "visualNode".
+		 * https://issues.jboss.org/browse/JBIDE-9932
+		 * Replaced with map.
 		 */
 		if (visualNode != null) {
-			Iterator<Map.Entry<nsIDOMNode, VpeNodeMapping>> iter = visualMap.entrySet().iterator();
-			while(iter.hasNext()) {
-				Map.Entry<nsIDOMNode,VpeNodeMapping> element = iter.next();
-				if (visualNode.equals(element.getKey())) {
-					nodeMapping = element.getValue();
-				}
-			}
+			nodeMapping = visualMap.get(visualNode);
 		}
 		return nodeMapping;
 	}
Index: src/org/jboss/tools/vpe/editor/VpeVisualDomBuilder.java
===================================================================
--- src/org/jboss/tools/vpe/editor/VpeVisualDomBuilder.java	(revision 36825)
+++ src/org/jboss/tools/vpe/editor/VpeVisualDomBuilder.java	(working copy)
@@ -262,10 +262,7 @@
 	 */
 	private boolean addNode(Node sourceNode, nsIDOMNode visualNextNode, nsIDOMNode visualContainer) {
 		try {
-
-		nsIDOMNode visualNewNode = createNode(sourceNode, visualContainer);
-
-			
+		VpeElementMapping elementMapping = createNode(sourceNode, visualContainer);
 // Commented as fix for JBIDE-3012.	
 //		// Fix for JBIDE-1097
 //		try {
@@ -277,35 +274,46 @@
 //		} catch (XPCOMException ex) {
 //			// just ignore this exception
 //		}
-
-		if (visualNewNode != null) {
-			/*
-			 * https://jira.jboss.org/jira/browse/JBIDE-3373
-			 * Do not add additional visual node for f:facet
-			 * when it is inserted into existing one.
-			 */
-			nsIDOMElement element = null;
-			try {
-				element = queryInterface(visualNewNode, nsIDOMElement.class);
-			} catch (org.mozilla.xpcom.XPCOMException e) {
-				/*
-				 * Cannot parse node to element,
-				 * do nothing
-				 */
-		 	}
-			if (!((null != element) && element.hasAttribute(VPE_FACET))) {
-				if (visualNextNode == null) {
-					visualContainer.appendChild(visualNewNode);
-				} else {
-					visualContainer.insertBefore(visualNewNode, visualNextNode);
+			if (elementMapping != null) {
+				nsIDOMNode visualNewNode = elementMapping.getVisualNode();
+				if (visualNewNode != null) {
+					/*
+					 * https://jira.jboss.org/jira/browse/JBIDE-3373 Do not add
+					 * additional visual node for f:facet when it is inserted into
+					 * existing one.
+					 */
+					nsIDOMElement element = null;
+					try {
+						element = queryInterface(visualNewNode, nsIDOMElement.class);
+					} catch (org.mozilla.xpcom.XPCOMException e) {
+						/*
+						 * Cannot parse node to element, do nothing
+						 */
+					}
+					if (!((null != element) && element.hasAttribute(VPE_FACET))) {
+						nsIDOMNode registeredVisualNewNode = null;
+						if (visualNextNode == null) {
+							registeredVisualNewNode = visualContainer.appendChild(visualNewNode);
+						} else {
+							registeredVisualNewNode = visualContainer.insertBefore(visualNewNode, visualNextNode);
+						}
+						/*
+						 * https://issues.jboss.org/browse/JBIDE-9932
+						 * Only nodes that have been added into the DOM
+						 * should be registered via elementMapping.
+						 */
+						if (isCurrentMainDocument()) {
+							elementMapping.setVisualNode(registeredVisualNewNode);
+							registerNodes(elementMapping);
+						}
+						
+					}
+					return true;
 				}
 			}
-			return true;
-		}
-		} catch(XPCOMException xpcomException) {
+		} catch (XPCOMException xpcomException) {
 			VpePlugin.reportProblem(xpcomException);
 		}
-
 		return false;
 	}
 
@@ -316,10 +324,7 @@
 	 * @param visualOldContainer visual node, in which the caller plans to insert new visual node 
 	 * @return new visual node
 	 */
-	public nsIDOMNode createNode(Node sourceNode,
-			nsIDOMNode visualOldContainer) throws VpeDisposeException {
-
-		boolean registerFlag = isCurrentMainDocument();
+	public VpeElementMapping createNode(Node sourceNode, nsIDOMNode visualOldContainer) throws VpeDisposeException {
 		//it's check for initialization visualController,
 		//if we trying to process some event when controller
 		//hasn't been initialized, it's causes 
@@ -444,7 +449,8 @@
 				&& visualNewNode.getNodeType() == nsIDOMNode.ELEMENT_NODE) {
 			setTooltip((Element) sourceNode, queryInterface(visualNewNode, nsIDOMElement.class));
 		}
-		if (registerFlag) {
+		VpeElementMapping elementMapping = null;
+		if (isCurrentMainDocument()) {
 			final VpeElementData data = creationData.getElementData();
 			if ((sourceNodeProxy != null) && (data != null)
 					&& (data.getNodesData() != null)
@@ -453,8 +459,8 @@
 					if (nodeData.getSourceNode() != null) {
 						Node attr = null;
 						if(sourceNode.getAttributes()!=null) {
-							attr =	sourceNode.getAttributes().getNamedItem(nodeData
-								.getSourceNode().getNodeName());
+							attr = sourceNode.getAttributes().getNamedItem(
+									nodeData.getSourceNode().getNodeName());
 						} else {
 							//Text node haven't child nodes, but it's  node.
 							attr = sourceNode;
@@ -463,12 +469,10 @@
 						nodeData.setEditable(false);
 					}
 				}
-
 			}
-			VpeElementMapping elementMapping = new VpeElementMapping(
+			elementMapping = new VpeElementMapping(
 					sourceNode, visualNewNode, template,
 					ifDependencySet, creationData.getData(), data);
-			registerNodes(elementMapping);
 		}
 		/*
 		 * When in templates xml file specified that particular template
@@ -511,7 +515,17 @@
 			VpePlugin.getPluginLog().logError(ex);
 		}
 		getPageContext().setCurrentVisualNode(null);
-		return visualNewNode;
+		/*
+		 * https://issues.jboss.org/browse/JBIDE-9932
+		 * Create "fake" mapping for the node that will not
+		 * be registered. 
+		 */
+		if (elementMapping == null) {
+			elementMapping = new VpeElementMapping(
+					sourceNode, visualNewNode, template,
+					ifDependencySet, creationData.getData(), null);
+		}
+		return elementMapping;
 	}
 
 	protected void correctVisualAttribute(nsIDOMElement element) {
@@ -544,22 +558,19 @@
 	 * @param sourceContainer
 	 * @param visualContainer
 	 */
-	protected void addChildren(VpeTemplate containerTemplate,
-			Node sourceContainer, nsIDOMNode visualContainer) {
+	protected void addChildren(VpeTemplate containerTemplate, Node sourceContainer, nsIDOMNode visualContainer) {
 		NodeList sourceNodes = sourceContainer.getChildNodes();
 		int len = sourceNodes.getLength();
 		int childrenCount = 0;
 		for (int i = 0; i < len; i++) {
 			Node sourceNode = sourceNodes.item(i);
 			if (addNode(sourceNode, null, visualContainer)) {
-				if (Node.ELEMENT_NODE == sourceNode.getNodeType()) {
-				}
+//				if (Node.ELEMENT_NODE == sourceNode.getNodeType()) { }
 				childrenCount++;
 			}
 		}
 		if (childrenCount == 0) {
-			setPseudoContent(containerTemplate, sourceContainer,
-					visualContainer);
+			setPseudoContent(containerTemplate, sourceContainer,visualContainer);
 		}
 	}
 
Index: src/org/jboss/tools/vpe/editor/mapping/VpeElementMapping.java
===================================================================
--- src/org/jboss/tools/vpe/editor/mapping/VpeElementMapping.java	(revision 36825)
+++ src/org/jboss/tools/vpe/editor/mapping/VpeElementMapping.java	(working copy)
@@ -93,4 +93,12 @@
 	public void setElementData(VpeElementData elementData) {
 		this.elementData = elementData;
 	}
+	
+	/**
+	 * Fix for https://issues.jboss.org/browse/JBIDE-9932
+	 * @param visualNode the visual node
+	 */
+	public void setVisualNode(nsIDOMNode visualNode) {
+		this.visualNode = visualNode;
+	}
 }
Index: src/org/jboss/tools/vpe/editor/VpePreviewDomBuilder.java
===================================================================
--- src/org/jboss/tools/vpe/editor/VpePreviewDomBuilder.java	(revision 36825)
+++ src/org/jboss/tools/vpe/editor/VpePreviewDomBuilder.java	(working copy)
@@ -20,6 +20,7 @@
 import org.jboss.tools.vpe.VpePlugin;
 import org.jboss.tools.vpe.editor.context.VpePageContext;
 import org.jboss.tools.vpe.editor.mapping.VpeDomMapping;
+import org.jboss.tools.vpe.editor.mapping.VpeElementMapping;
 import org.jboss.tools.vpe.editor.mozilla.MozillaEditor;
 import org.jboss.tools.vpe.editor.proxy.VpeProxyUtil;
 import org.jboss.tools.vpe.editor.template.VpeCreationData;
@@ -56,10 +57,8 @@
 	 * @return
 	 */
 	@Override
-	public nsIDOMNode createNode(Node sourceNode, nsIDOMNode visualOldContainer) {
-
+	public VpeElementMapping createNode(Node sourceNode, nsIDOMNode visualOldContainer) {
 			Set<?> ifDependencySet = new HashSet();
-			
 		    if(sourceNode==null||(
 		    		sourceNode.getNodeType()!=Node.TEXT_NODE
 		    		&&sourceNode.getNodeType()!=Node.ELEMENT_NODE
@@ -74,8 +73,8 @@
 			//FIX FOR JBIDE-1568, added by Max Areshkau
 			try {
 		          if (getPageContext().getElService().isELNode(sourceNode)) {
-                    final Node sourceNodeProxy =  VpeProxyUtil.createProxyForELExpressionNode(getPageContext(),
-        					sourceNode);
+                    final Node sourceNodeProxy =  VpeProxyUtil.createProxyForELExpressionNode(
+                    		getPageContext(), sourceNode);
     				try {
     					creationData = template.create(getPageContext(),
     							sourceNodeProxy, getVisualDocument());
@@ -84,10 +83,9 @@
     					} catch(ClassCastException ex) {
     						VpePlugin.reportProblem(ex);
     						//then we create template without using proxy
-    						creationData = template.create(getPageContext(), sourceNode,
-    								getVisualDocument());
+    						creationData = template.create(getPageContext(), 
+    								sourceNode, getVisualDocument());
     					}
-                
 		          } else {
                     creationData = template.create(getPageContext(), sourceNode, getVisualDocument());
                 }
@@ -123,8 +121,13 @@
 			getPageContext().setCurrentVisualNode(visualOldContainer);
 			template.validate(getPageContext(), sourceNode, getVisualDocument(), creationData);
 			getPageContext().setCurrentVisualNode(null);
-			
-			return visualNewNode;
+			/*
+			 * https://issues.jboss.org/browse/JBIDE-9932
+			 * Return VpeElementMapping instead of node itself.
+			 */
+			return new VpeElementMapping(
+					sourceNode, visualNewNode, template,
+					ifDependencySet, creationData.getData(), null); 
 	}
 
 }
#P org.jboss.tools.vpe.base.test
Index: src/org/jboss/tools/vpe/base/test/VpeTest.java
===================================================================
--- src/org/jboss/tools/vpe/base/test/VpeTest.java	(revision 36825)
+++ src/org/jboss/tools/vpe/base/test/VpeTest.java	(working copy)
@@ -11,6 +11,8 @@
 
 package org.jboss.tools.vpe.base.test;
 
+import static org.jboss.tools.vpe.xulrunner.util.XPCOM.queryInterface;
+
 import java.io.IOException;
 import java.util.Collection;
 import java.util.Map;
@@ -428,18 +430,24 @@
 	 * @param elementId
 	 * @return
 	 */
-	protected nsIDOMElement findElementById(VpeController controller,
-			String elementId) {
-
+	protected nsIDOMElement findElementById(VpeController controller,String elementId) {
 		Element sourceElement = findSourceElementById(controller, elementId);
-
-		VpeNodeMapping nodeMapping = controller.getDomMapping().getNodeMapping(
-				sourceElement);
-
-		if (nodeMapping == null)
-			return null;
-
-		return (nsIDOMElement) nodeMapping.getVisualNode();
+		nsIDOMElement element = null;
+		VpeNodeMapping nodeMapping = controller
+				.getDomMapping().getNodeMapping(sourceElement);
+		if (nodeMapping != null) {
+			nsIDOMNode node = nodeMapping.getVisualNode();
+			if (node != null) {
+				try {
+					element = queryInterface(node, nsIDOMElement.class);
+				} catch (org.mozilla.xpcom.XPCOMException e) { 
+					//Do nothing
+				} catch (NullPointerException e) { 
+					//Do nothing					
+				}
+			}
+		}
+		return element; 
 	}
 	
 	protected String getEditorID(){
Index: src/org/jboss/tools/vpe/base/test/ComponentContentTest.java
===================================================================
--- src/org/jboss/tools/vpe/base/test/ComponentContentTest.java	(revision 36825)
+++ src/org/jboss/tools/vpe/base/test/ComponentContentTest.java	(working copy)
@@ -133,7 +133,7 @@
 				throws DOMComparisonException {
 		// get element by id
 		nsIDOMElement vpeElement = findElementById(controller, elementId);
-		assertNotNull("Cann't find element with id="+elementId,vpeElement); //$NON-NLS-1$
+		assertNotNull("Cannot find element with id="+elementId,vpeElement); //$NON-NLS-1$
 
 		// DOMTreeDumper dumper = new DOMTreeDumper(
 		// VpeDebug.VISUAL_DUMP_PRINT_HASH);
