[361606] Failures from fix 358545
diff --git a/bundles/org.eclipse.jst.jsp.ui/META-INF/MANIFEST.MF b/bundles/org.eclipse.jst.jsp.ui/META-INF/MANIFEST.MF
index 08edaef..1b771e8 100644
--- a/bundles/org.eclipse.jst.jsp.ui/META-INF/MANIFEST.MF
+++ b/bundles/org.eclipse.jst.jsp.ui/META-INF/MANIFEST.MF
@@ -2,7 +2,7 @@
 Bundle-ManifestVersion: 2
 Bundle-Name: %pluginName
 Bundle-SymbolicName: org.eclipse.jst.jsp.ui; singleton:=true
-Bundle-Version: 1.1.504.qualifier
+Bundle-Version: 1.1.505.qualifier
 Bundle-Activator: org.eclipse.jst.jsp.ui.internal.JSPUIPlugin
 Bundle-Vendor: %providerName
 Bundle-Localization: plugin
diff --git a/bundles/org.eclipse.wst.jsdt.web.ui/src/org/eclipse/wst/jsdt/web/ui/internal/format/FormattingStrategyJSDT.java b/bundles/org.eclipse.wst.jsdt.web.ui/src/org/eclipse/wst/jsdt/web/ui/internal/format/FormattingStrategyJSDT.java
index bca0886..b295d82 100644
--- a/bundles/org.eclipse.wst.jsdt.web.ui/src/org/eclipse/wst/jsdt/web/ui/internal/format/FormattingStrategyJSDT.java
+++ b/bundles/org.eclipse.wst.jsdt.web.ui/src/org/eclipse/wst/jsdt/web/ui/internal/format/FormattingStrategyJSDT.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2007, 2011 IBM Corporation and others.
+ * Copyright (c) 2007, 2010 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -22,13 +22,16 @@
 import org.eclipse.core.resources.ResourcesPlugin;
 import org.eclipse.core.runtime.IPath;
 import org.eclipse.jface.text.BadLocationException;
+import org.eclipse.jface.text.Document;
 import org.eclipse.jface.text.IDocument;
 import org.eclipse.jface.text.IDocumentPartitioningListener;
+import org.eclipse.jface.text.Region;
 import org.eclipse.jface.text.TextUtilities;
 import org.eclipse.jface.text.TypedPosition;
 import org.eclipse.jface.text.formatter.ContextBasedFormattingStrategy;
 import org.eclipse.jface.text.formatter.FormattingContextProperties;
 import org.eclipse.jface.text.formatter.IFormattingContext;
+import org.eclipse.text.edits.ReplaceEdit;
 import org.eclipse.text.edits.TextEdit;
 import org.eclipse.wst.jsdt.core.IJavaScriptProject;
 import org.eclipse.wst.jsdt.core.JavaScriptCore;
@@ -37,7 +40,10 @@
 import org.eclipse.wst.jsdt.internal.formatter.DefaultCodeFormatter;
 import org.eclipse.wst.jsdt.web.core.internal.Logger;
 import org.eclipse.wst.jsdt.web.core.javascript.IJsTranslation;
+import org.eclipse.wst.jsdt.web.core.javascript.IJsTranslator;
+import org.eclipse.wst.jsdt.web.core.javascript.JsTranslation;
 import org.eclipse.wst.jsdt.web.core.javascript.JsTranslationAdapter;
+import org.eclipse.wst.jsdt.web.core.javascript.JsTranslator;
 import org.eclipse.wst.sse.core.StructuredModelManager;
 import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument;
 import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion;
@@ -115,31 +121,60 @@
 				String jsTextNotTranslated = document.get(partition.getOffset(), partition.getLength());
 				
 				//deal with getting the JS text and unwrapping it from any <!-- //--> statements
-				int contentStart = partition.getOffset();
-				int contentLength = partition.getLength();
+				String preText = "";
+				String postText = lineDelim + scriptRegionIndent;
 
 				//find start comment tag
 				Pattern startPattern = Pattern.compile("(\\A(\\s*<!--.*(" + lineDelim + ")?))"); //$NON-NLS-1$
 				Matcher matcher = startPattern.matcher(jsTextNotTranslated);
 				if(matcher.find()) {
-					contentStart += matcher.end();
 					jsTextNotTranslated = matcher.replaceFirst(""); //$NON-NLS-1$
+					preText = lineDelim + scriptRegionIndent + matcher.group().trim();
 				}
 				
 				//find end tag
 				matcher = END_PATTERN.matcher(jsTextNotTranslated);
 				if(matcher.find()) {
 					jsTextNotTranslated = matcher.replaceFirst(""); //$NON-NLS-1$
-					contentLength = matcher.start();
+					postText = lineDelim + scriptRegionIndent + matcher.group().trim() + postText;
 				}
 				
+				//replace the text in the document with the none-translated JS text but without HTML leading and trailing comments
+				TextEdit replaceEdit = new ReplaceEdit(partition.getOffset(), partition.getLength(), jsTextNotTranslated);
+				replaceEdit.apply(document);
+				int jsRegionLength = jsTextNotTranslated.length();
+				
 				//translate the updated document
 				IJsTranslation translation = getTranslation(document);
 				String jsTextTranslated = translation.getJsText();
 				
 				//format the text translated text
-				TextEdit edit = CodeFormatterUtil.format2(CodeFormatter.K_JAVASCRIPT_UNIT, jsTextTranslated, contentStart, contentLength, startIndentLevel, lineDelim, getPreferences());
-				edit.apply(document);
+				TextEdit edit = CodeFormatterUtil.format2(CodeFormatter.K_JAVASCRIPT_UNIT, jsTextTranslated, partition.getOffset(), jsRegionLength, startIndentLevel, lineDelim, getPreferences());
+				IDocument jsDoc = new Document(jsTextTranslated);
+				
+				//Undo the text replacements done by the translator so that it could build a CU for the JS region
+				if(translation instanceof JsTranslation) {
+					IJsTranslator translator = ((JsTranslation)translation).getTranslator();
+					
+					if(translator instanceof JsTranslator) {
+						Region[] regions = ((JsTranslator)translator).getGeneratedRanges();
+						//for each generated range, replace it with the original text
+						for(int r = 0; r < regions.length; ++r) {
+							jsDoc.replace(regions[r].getOffset(), regions[r].getLength(),
+									document.get(regions[r].getOffset(), regions[r].getLength()));
+						}
+					}
+				}
+				
+				/* error formating the code so abort */
+				if(edit==null) return;
+				edit.apply(jsDoc);
+				String replaceText = lineDelim + getIndentationString(getPreferences(), startIndentLevel) + (jsDoc.get(edit.getOffset(), edit.getLength())).trim();
+				
+				//apply edit to html doc using the formated translated text and the possible leading and trailing html comments
+				replaceText = preText + replaceText + postText;
+				replaceEdit = new ReplaceEdit(partition.getOffset(), jsRegionLength, replaceText);
+				replaceEdit.apply(document);
 			} catch (BadLocationException e) {
 			}
 		}