code-editor

added sanity check

6/2/2023 10:43:19 AM

Details

diff --git a/codeeditor-app/elements/code-editor/code-editor.ts b/codeeditor-app/elements/code-editor/code-editor.ts
index 82ae5de..e46387e 100644
--- a/codeeditor-app/elements/code-editor/code-editor.ts
+++ b/codeeditor-app/elements/code-editor/code-editor.ts
@@ -5,6 +5,7 @@ import { OutputFrame } from "../output-frame/output-frame";
 import { debounceManager } from "../../../shared/ensure-debounce";
 import * as sessionApi from '../../../shared/session-api';
 import { isSessionPage } from "../../../shared/page";
+import { sanityConvert } from "../../../shared/sanity-code-check";
 
 export class CodeEditor extends BaseElement {
 
@@ -45,13 +46,13 @@ export class CodeEditor extends BaseElement {
                 localStorage.setItem(this.language + "LastInputCache", this.input);
                 debounce.ensureDebounce(() => {
                     sessionApi.setSessionData(this.language, this.input);
-                    this.outputFrame.setContent(this.language, this.input);
+                    this.outputFrame.setContent(this.language, this.language.toLowerCase() === "javascript" ? sanityConvert(this.input) : this.input);
                 });
             }
         });
 
         this.waitFor(this.outputFrame, () => {
-            this.outputFrame.setContent(this.language, isSessionPage() ? "" : this.input);
+            this.outputFrame.setContent(this.language, isSessionPage() ? "" : (this.language.toLowerCase() === "javascript" ? sanityConvert(this.input) : this.input));
         });
 
     }
@@ -62,7 +63,7 @@ export class CodeEditor extends BaseElement {
             this.lastInputCache = this.input;
             localStorage.setItem(this.language + "LastInputCache", this.input);
             this.editor.setValue(input);
-            this.outputFrame.setContent(this.language, this.input);
+            this.outputFrame.setContent(this.language, this.language.toLowerCase() === "javascript" ? sanityConvert(this.input) : this.input);
         }
     }
 
diff --git a/codeeditor-app/elements/output-frame/output-frame.ts b/codeeditor-app/elements/output-frame/output-frame.ts
index 6296b55..2c5492a 100644
--- a/codeeditor-app/elements/output-frame/output-frame.ts
+++ b/codeeditor-app/elements/output-frame/output-frame.ts
@@ -29,7 +29,6 @@ export class OutputFrame extends BaseElement {
 
         if (this.Iframe && this.hasChild(this.Iframe)) {
             this.Iframe.setAttribute("sandbox", "allow-pointer-lock allow-same-origin");
-            console.log("REMOVING IFRAME!");
             this.removeChild(this.Iframe);
         }
 
@@ -38,28 +37,21 @@ export class OutputFrame extends BaseElement {
         this.Iframe.setAttribute("sandbox", "allow-pointer-lock allow-same-origin allow-scripts");
         if (onload) {
             this.Iframe.onload = ((ev) => {
-                console.log("IFRAME LOADED");
                 onload(ev);
                 if (this.Iframe)
                     this.Iframe.onload = () => { };
             });
         }
-        console.log("IF SOURCE BEFORE", this.Iframe.src);
         if (source != null) {
-            console.log("CUSOMSRC S:"+source, source);
             this.Iframe.src = source;
         }
         else {
             this.Iframe.src = GetOutputFrameUrl();
         }
-        console.log("IF SOURCE AFTER", this.Iframe.src);
 
         if (source !== "") {
             this.appendChild(this.Iframe);
         }
-        else {
-            console.log("SPPCHD");
-        }
 
         if (resetNotificationBubbles) {
             var bubbles = GetNotificationBubbles();
@@ -70,18 +62,16 @@ export class OutputFrame extends BaseElement {
     }
 
     setError() {
-        console.log("RCALLED");
         this.reset(() => {
+            console.log("ERROR RESET");
             if (this.Iframe) {
-                // this.Iframe.contentWindow?.location.reload();                
-                console.log("RESET AFTER", this.Iframe.src);
+                // this.Iframe.contentWindow?.location.reload();
             }
-        }, true, "");//GetOutputFrameUrl("execution-time-error.html"));
+        }, true, "");
 
     }
 
     setContent(language: string, value: string) {
-        console.log("setting content", language, value);
         this.contents[language] = value;
         this.reset(() => {
             for (const lang in this.contents) {
@@ -97,7 +87,6 @@ export class OutputFrame extends BaseElement {
     doPostMessage(language: string, value: string) {
         this.CurrentIframeScriptRemoveResponseListener = postMessage(this.Iframe?.contentWindow, language, this.contents[language], language == 'javascript' ?
             (executeTimeInMs, exceedTimeInMs) => {
-                console.log("onresponse", executeTimeInMs + " " + exceedTimeInMs);
                 if (executeTimeInMs === -1) {
                     this.setError();
                 }
diff --git a/codeeditor-app/elements/window-control/window-control connector.ts b/codeeditor-app/elements/window-control/window-control connector.ts
index 93030d2..1f85844 100644
--- a/codeeditor-app/elements/window-control/window-control connector.ts
+++ b/codeeditor-app/elements/window-control/window-control connector.ts
@@ -51,7 +51,6 @@ export class WindowControlConnector extends BaseElement {
 
         if (distTarget) {
             var visibleTargetChildren = this.visibleChildren(distTarget);
-            console.log(visibleTargetChildren);
 
             if (visibleTargetChildren.length === 0) {
                 distTarget.classList.add("force-close");
diff --git a/outputframe-app/app.html b/outputframe-app/app.html
index 08bc178..cc66c3e 100644
--- a/outputframe-app/app.html
+++ b/outputframe-app/app.html
@@ -1,3 +1,2 @@
 <render-canvas></render-canvas>
-<loading-spinner></loading-spinner>
-hi
\ No newline at end of file
+<loading-spinner></loading-spinner>
\ No newline at end of file
diff --git a/shared/sanity-code-check.ts b/shared/sanity-code-check.ts
new file mode 100644
index 0000000..627b333
--- /dev/null
+++ b/shared/sanity-code-check.ts
@@ -0,0 +1,17 @@
+export function sanityConvert(code: string, maxLoops: number = 2000) {
+    var beforeConidition = "var conditionCount = " + maxLoops + "; \n";
+    var insideCondition = "\n --conditionCount; \n if (conditionCount <= 0) { break; } \n";
+
+    const regex = /(while|for|foreach)(\s*\([^)]*\)\s*)({[^}]*}|[^{;]*;?)/gs;
+    const output = code.replace(regex, (match, p1, p2, p3) => {
+        let statement = p3.trim();
+        if (statement.startsWith('{') && statement.endsWith('}')) {
+            statement = statement.replace('{', '{' + insideCondition);
+        } else {
+            statement = statement.endsWith(';') ? statement.slice(0, -1) : statement;
+            statement = `{${insideCondition}${statement};}`;
+        }
+        return `${beforeConidition}${p1}${p2}${statement}`;
+    });
+    return output;
+}
\ No newline at end of file
diff --git a/shared/session-api.ts b/shared/session-api.ts
index d04ab51..8828805 100644
--- a/shared/session-api.ts
+++ b/shared/session-api.ts
@@ -1,5 +1,5 @@
-var apiUrl = "https://codeeditor-api.davidssoft.com/";
-//var apiUrl = "https://codeeditor-api.azurewebsites.net/";
+//var apiUrl = "https://codeeditor-api.davidssoft.com/";
+var apiUrl = "https://codeeditor-api.azurewebsites.net/";
 
 export function hasSession() {
     var localstorageSessionId = localStorage.getItem("sessionId");
diff --git a/shared/url-helpers.ts b/shared/url-helpers.ts
index e8852b2..439eaea 100644
--- a/shared/url-helpers.ts
+++ b/shared/url-helpers.ts
@@ -9,7 +9,7 @@ export function IsLocalhost(): boolean {
 export function GetOutputFrameUrl(path: string = ""): string {
     var v = "?v=" + (+new Date());
     if (IsLocalhost()) {
-        return "http://127.0.0.1:8021/" + path + v;
+        return "http://localhost:8021/" + path + v;
     }
     else {
         return "https://outputframe.davidmeincke.dk/" + path + v;