Search in sources :

Example 21 with IStructuredModel

use of org.eclipse.wst.sse.core.internal.provisional.IStructuredModel in project webtools.sourceediting by eclipse.

the class TestHtmlTranslation method testCDATAInJS.

public void testCDATAInJS() {
    // get model
    String fileName = getName() + ".html";
    IStructuredModel structuredModel = getSharedModel(fileName, "<script> var text = <![CDATA[ serverObject.getText() ;\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();
    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 22 with IStructuredModel

use of org.eclipse.wst.sse.core.internal.provisional.IStructuredModel in project webtools.sourceediting by eclipse.

the class TestHtmlTranslation method testMangleMultipleMixedServerSideInJSwithXMLcomment_and_CheckProblems.

public void testMangleMultipleMixedServerSideInJSwithXMLcomment_and_CheckProblems() {
    // get model
    String fileName = getName() + ".html";
    IStructuredModel structuredModel = getSharedModel(fileName, "<script> <!-- \nvar 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("translated contents not as expected", "              \nvar text = _$tag_______________________; _$tag___ _$tag___ _$tag________________________$tag___ ", 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("var declaration not found ", 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 23 with IStructuredModel

use of org.eclipse.wst.sse.core.internal.provisional.IStructuredModel in project webtools.sourceediting by eclipse.

the class TestHtmlTranslation method testScriptLocationsWereRecorded.

public void testScriptLocationsWereRecorded() {
    // get model
    String fileName = getName() + ".html";
    String dojoCall = "\ndojo.xhr(\"x\"); \n";
    IStructuredModel structuredModel = getSharedModel(fileName, "<div                                   onClick=\"return myMethod();\"  onblur=\"return myMethod2();\">\n<script >" + dojoCall + "</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"));
    assertNotSame("script locations not returned by translation", null, translation.getScriptPositions());
    int expectedOffset = structuredModel.getStructuredDocument().get().indexOf(dojoCall);
    assertEquals("script location not returned by translation", expectedOffset, translation.getScriptPositions()[0].getOffset());
    assertEquals("script location length incorrect in translation", dojoCall.length(), translation.getScriptPositions()[0].getLength());
    // 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 24 with IStructuredModel

use of org.eclipse.wst.sse.core.internal.provisional.IStructuredModel in project webtools.sourceediting by eclipse.

the class TestHtmlTranslation method testMangleServerSide2InJSCheckProblems.

public void testMangleServerSide2InJSCheckProblems() {
    // get model
    String fileName = getName() + ".html";
    IStructuredModel structuredModel = getSharedModel(fileName, "<script> 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("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 25 with IStructuredModel

use of org.eclipse.wst.sse.core.internal.provisional.IStructuredModel in project webtools.sourceediting by eclipse.

the class TestHtmlTranslation method testMangleServerSideAndClientTagInJS.

public void testMangleServerSideAndClientTagInJS() {
    // get model
    String fileName = getName() + ".html";
    IStructuredModel structuredModel = getSharedModel(fileName, "<script> var a = <custom:tag/>5;\nif(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("translation empty", translated.length() > 5);
    assertTrue("server-side script block included\n" + translated, translated.indexOf("<%") < 0);
    assertTrue("server-side script block included\n" + translated, translated.indexOf("%>") < 0);
    assertTrue("tag included\n" + translated, translated.indexOf("custom") < 0);
    assertTrue("tag included\n" + translated, translated.indexOf("/>") < 0);
    assertTrue("var dropped\n" + translated, translated.indexOf("var a = ") > -1);
    assertTrue("if dropped\n" + translated, translated.indexOf("5;\nif(a <") > -1);
    assertTrue("block dropped\n" + translated, translated.indexOf(") {} ; ") > -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)

Aggregations

IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)482 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)110 IModelManager (org.eclipse.wst.sse.core.internal.provisional.IModelManager)102 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)81 IFile (org.eclipse.core.resources.IFile)75 IOException (java.io.IOException)69 CoreException (org.eclipse.core.runtime.CoreException)49 IDocument (org.eclipse.jface.text.IDocument)46 IDOMDocument (org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)42 InputStream (java.io.InputStream)40 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)36 Path (org.eclipse.core.runtime.Path)35 BadLocationException (org.eclipse.jface.text.BadLocationException)34 IJsTranslation (org.eclipse.wst.jsdt.web.core.javascript.IJsTranslation)32 JsTranslationAdapter (org.eclipse.wst.jsdt.web.core.javascript.JsTranslationAdapter)32 IProject (org.eclipse.core.resources.IProject)31 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)31 IPath (org.eclipse.core.runtime.IPath)27 ByteArrayInputStream (java.io.ByteArrayInputStream)22 Document (org.w3c.dom.Document)19