Search in sources :

Example 26 with JsTranslationAdapter

use of org.eclipse.wst.jsdt.web.core.javascript.JsTranslationAdapter in project webtools.sourceediting by eclipse.

the class TestHtmlTranslation method testEmptyEventHandlerValueCausesStringIndexOutOfBounds.

public void testEmptyEventHandlerValueCausesStringIndexOutOfBounds() throws Exception {
    String fileName = getName() + ".html";
    IStructuredModel structuredModel = getSharedModel(fileName, "<html><body><span onclick=\"\"></body></html>");
    assertNotNull("missing test model", structuredModel);
    JsTranslationAdapterFactory.setupAdapterFactory(structuredModel);
    try {
        // do translation
        JsTranslationAdapter translationAdapter = (JsTranslationAdapter) ((IDOMModel) structuredModel).getDocument().getAdapterFor(IJsTranslation.class);
        IJsTranslation translation = translationAdapter.getJsTranslation(false);
        String translated = translation.getJsText();
    } catch (StringIndexOutOfBoundsException e) {
        fail(e.getMessage());
    } finally {
        if (structuredModel != null) {
            structuredModel.releaseFromRead();
        }
    }
}
Also used : IJsTranslation(org.eclipse.wst.jsdt.web.core.javascript.IJsTranslation) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) JsTranslationAdapter(org.eclipse.wst.jsdt.web.core.javascript.JsTranslationAdapter)

Example 27 with JsTranslationAdapter

use of org.eclipse.wst.jsdt.web.core.javascript.JsTranslationAdapter in project webtools.sourceediting by eclipse.

the class TestHtmlTranslation method testCDATAAroundJS2.

public void testCDATAAroundJS2() {
    // get model
    String fileName = getName() + ".html";
    IStructuredModel structuredModel = getSharedModel(fileName, "<script><![CDATA[ var text =  serverObject.getText() ]]></script>");
    assertNotNull("missing test model", structuredModel);
    // do translation
    JsTranslationAdapterFactory.setupAdapterFactory(structuredModel);
    JsTranslationAdapter translationAdapter = (JsTranslationAdapter) ((IDOMModel) structuredModel).getDocument().getAdapterFor(IJsTranslation.class);
    IJsTranslation translation = translationAdapter.getJsTranslation(false);
    String translated = translation.getJsText();
    assertTrue("translation empty", translated.length() > 5);
    assertTrue("CDATA start found", translated.indexOf("CDATA") < 0);
    assertTrue("CDATA start found", translated.indexOf("[") < 0);
    assertTrue("CDATA end found", translated.indexOf("]") < 0);
    assertTrue("problems found in translation ", translation.getProblems().isEmpty());
    // release model
    structuredModel.releaseFromRead();
}
Also used : IJsTranslation(org.eclipse.wst.jsdt.web.core.javascript.IJsTranslation) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) JsTranslationAdapter(org.eclipse.wst.jsdt.web.core.javascript.JsTranslationAdapter)

Example 28 with JsTranslationAdapter

use of org.eclipse.wst.jsdt.web.core.javascript.JsTranslationAdapter in project webtools.sourceediting by eclipse.

the class TestHtmlTranslation method testXMLComment.

public void testXMLComment() {
    // get model
    String fileName = getName() + ".html";
    IStructuredModel structuredModel = getSharedModel(fileName, "<script> if(a) <!-- --> </script>");
    assertNotNull("missing test model", structuredModel);
    // do translation
    JsTranslationAdapterFactory.setupAdapterFactory(structuredModel);
    JsTranslationAdapter translationAdapter = (JsTranslationAdapter) ((IDOMModel) structuredModel).getDocument().getAdapterFor(IJsTranslation.class);
    IJsTranslation translation = translationAdapter.getJsTranslation(false);
    String translated = translation.getJsText();
    assertEquals("translated content differs", "         if(a) _$ta _$t ", translated);
    // release model
    structuredModel.releaseFromRead();
}
Also used : IJsTranslation(org.eclipse.wst.jsdt.web.core.javascript.IJsTranslation) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) JsTranslationAdapter(org.eclipse.wst.jsdt.web.core.javascript.JsTranslationAdapter)

Example 29 with JsTranslationAdapter

use of org.eclipse.wst.jsdt.web.core.javascript.JsTranslationAdapter in project webtools.sourceediting by eclipse.

the class TestHtmlTranslation method testMangleMultipleServerSide1InJSCheckProblems.

public void testMangleMultipleServerSide1InJSCheckProblems() {
    // get model
    String fileName = getName() + ".html";
    IStructuredModel structuredModel = getSharedModel(fileName, "<script> var text = <%= javaObject.getText() %>;  <%= javaObject.getText() %></script>");
    assertNotNull("missing test model", structuredModel);
    // do translation
    JsTranslationAdapterFactory.setupAdapterFactory(structuredModel);
    JsTranslationAdapter translationAdapter = (JsTranslationAdapter) ((IDOMModel) structuredModel).getDocument().getAdapterFor(IJsTranslation.class);
    IJsTranslation translation = translationAdapter.getJsTranslation(false);
    String translated = translation.getJsText();
    assertTrue("translation empty", translated.length() > 5);
    assertTrue("server-side script block included", translated.indexOf("<%") < 0);
    assertTrue("server-side script block included", translated.indexOf("%>") < 0);
    assertTrue("var dropped", translated.indexOf("var text = ") > -1);
    assertTrue("problems found in translation ", translation.getProblems().isEmpty());
    // release model
    structuredModel.releaseFromRead();
}
Also used : IJsTranslation(org.eclipse.wst.jsdt.web.core.javascript.IJsTranslation) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) JsTranslationAdapter(org.eclipse.wst.jsdt.web.core.javascript.JsTranslationAdapter)

Example 30 with JsTranslationAdapter

use of org.eclipse.wst.jsdt.web.core.javascript.JsTranslationAdapter in project webtools.sourceediting by eclipse.

the class TestHtmlTranslation method testMangleMultipleMixedServerSideInJSwithXMLcommentOnSameLine.

public void testMangleMultipleMixedServerSideInJSwithXMLcommentOnSameLine() {
    // get model
    String fileName = getName() + ".html";
    IStructuredModel structuredModel = getSharedModel(fileName, "<script> <!-- var text = <? serverObject.getText() ?>; <%=\"a\"%> <%=\"b\"%> <? serverObject.getText() ?><%=\"c\"%> </script>");
    assertNotNull("missing test model", structuredModel);
    // do translation
    JsTranslationAdapterFactory.setupAdapterFactory(structuredModel);
    JsTranslationAdapter translationAdapter = (JsTranslationAdapter) ((IDOMModel) structuredModel).getDocument().getAdapterFor(IJsTranslation.class);
    IJsTranslation translation = translationAdapter.getJsTranslation(false);
    String translated = translation.getJsText();
    assertEquals("script with leading comment but no new line should be blank", "                                                                                                              ", translated);
    assertTrue("server-side script block included", translated.indexOf("<?") < 0);
    assertTrue("server-side script block included", translated.indexOf("?>") < 0);
    assertTrue("server-side script block included", translated.indexOf("<%") < 0);
    assertTrue("server-side script block included", translated.indexOf("%>") < 0);
    assertTrue("problems found in translation ", translation.getProblems().isEmpty());
    // release model
    structuredModel.releaseFromRead();
}
Also used : IJsTranslation(org.eclipse.wst.jsdt.web.core.javascript.IJsTranslation) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) JsTranslationAdapter(org.eclipse.wst.jsdt.web.core.javascript.JsTranslationAdapter)

Aggregations

IJsTranslation (org.eclipse.wst.jsdt.web.core.javascript.IJsTranslation)41 JsTranslationAdapter (org.eclipse.wst.jsdt.web.core.javascript.JsTranslationAdapter)41 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)41 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)32 IDOMDocument (org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)14 List (java.util.List)5 IModelManager (org.eclipse.wst.sse.core.internal.provisional.IModelManager)4 CoreException (org.eclipse.core.runtime.CoreException)3 IJavaScriptElement (org.eclipse.wst.jsdt.core.IJavaScriptElement)3 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)3 ISelection (org.eclipse.jface.viewers.ISelection)2 JavaScriptModelException (org.eclipse.wst.jsdt.core.JavaScriptModelException)2 IProblem (org.eclipse.wst.jsdt.core.compiler.IProblem)2 NodeImpl (org.eclipse.wst.xml.core.internal.document.NodeImpl)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 IFile (org.eclipse.core.resources.IFile)1 IMarker (org.eclipse.core.resources.IMarker)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 Job (org.eclipse.core.runtime.jobs.Job)1