Search in sources :

Example 31 with IJsTranslation

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

the class TestHtmlTranslation method testMangleTagInJS.

public void testMangleTagInJS() {
    // get model
    String fileName = getName() + ".html";
    IStructuredModel structuredModel = getSharedModel(fileName, "<script> var a = <custom:tag/>5; if(a < 4) {} ; </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("tag included", translated.indexOf("<custom") < 0);
    assertTrue("tag 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)

Example 32 with IJsTranslation

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

the class TestHtmlTranslation method testMangleMultipleMixedServerSideAndClientTagInJS_and_CheckProblems.

public void testMangleMultipleMixedServerSideAndClientTagInJS_and_CheckProblems() {
    // get model
    String fileName = getName() + ".html";
    IStructuredModel structuredModel = getSharedModel(fileName, "<script> var text = <? serverObject.getText() ?>; <%=\"a\"%> <%=\"b\"%> <server:tag/> <? 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("translated contents not as expected", "         var text = _$tag_______________________; _$tag___ _$tag___ _$tag________ _$tag________________________$tag___ ", translated);
    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("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 33 with IJsTranslation

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

the class TestHtmlTranslation method testTwoFunctionReturnsInEventHandlers.

public void testTwoFunctionReturnsInEventHandlers() {
    // get model
    String fileName = getName() + ".html";
    IStructuredModel structuredModel = getSharedModel(fileName, "<div                                     onClick=\"return myMethod();\"  onblur=\"return myMethod2();\">\n<script >\ndojo.xhr(\"x\"); \n</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("dojo.xhr() invocation moved but not properly mapped", structuredModel.getStructuredDocument().get().indexOf("dojo.xhr(\"x\");"), ((JsTranslation) translation).getWebPageOffset(translated.indexOf("dojo.xhr(\"x\");")));
    assertEquals("translated content differs", "                                      (function(){return myMethod();;})();(function(){return myMethod2();;})(); \n         \ndojo.xhr(\"x\"); \n", translated);
    StringBuffer problems = new StringBuffer();
    translation.reconcileCompilationUnit();
    List problemList = translation.getProblems();
    for (int i = 0; i < problemList.size(); i++) {
        problems.append(((IProblem) problemList.get(i)).getMessage());
        problems.append('\n');
    }
    assertEquals("problems were found", "", problems.toString());
    assertEquals("offsets didn't match", structuredModel.getStructuredDocument().get().indexOf("dojo. \n"), translated.indexOf("dojo. \n"));
    // 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) List(java.util.List) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) JsTranslationAdapter(org.eclipse.wst.jsdt.web.core.javascript.JsTranslationAdapter)

Example 34 with IJsTranslation

use of org.eclipse.wst.jsdt.web.core.javascript.IJsTranslation 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 35 with IJsTranslation

use of org.eclipse.wst.jsdt.web.core.javascript.IJsTranslation 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)

Aggregations

IJsTranslation (org.eclipse.wst.jsdt.web.core.javascript.IJsTranslation)51 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)15 List (java.util.List)6 IJavaScriptElement (org.eclipse.wst.jsdt.core.IJavaScriptElement)6 IDocument (org.eclipse.jface.text.IDocument)5 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)5 IModelManager (org.eclipse.wst.sse.core.internal.provisional.IModelManager)4 CoreException (org.eclipse.core.runtime.CoreException)3 BadLocationException (org.eclipse.jface.text.BadLocationException)3 Document (org.eclipse.jface.text.Document)3 JavaScriptModelException (org.eclipse.wst.jsdt.core.JavaScriptModelException)3 ArrayList (java.util.ArrayList)2 IRegion (org.eclipse.jface.text.IRegion)2 ITypedRegion (org.eclipse.jface.text.ITypedRegion)2 ISelection (org.eclipse.jface.viewers.ISelection)2 IProblem (org.eclipse.wst.jsdt.core.compiler.IProblem)2 NodeImpl (org.eclipse.wst.xml.core.internal.document.NodeImpl)2