Skip to content

Commit

Permalink
Merge pull request #44 from sarmadka/main
Browse files Browse the repository at this point in the history
Update for latest Json version and avoid innerHTML.
  • Loading branch information
sarmadka committed Sep 12, 2023
2 parents e6e2df9 + 2f1c100 commit 8faf5eb
Show file tree
Hide file tree
Showing 18 changed files with 98 additions and 101 deletions.
4 changes: 2 additions & 2 deletions Doc/chat_example.ar.md
Original file line number Diff line number Diff line change
Expand Up @@ -820,10 +820,10 @@ func main {
// نقوم بتغريف مغلف سيتم تتنفيذه عند جلب البيانات من الخادم
onFetch = closure (json: Json) {
// في البداية نتحقق من أنه قد تم جلب البيانات بدون حدوث أخطاء
def status: Int = json.getObject("eventData").getInt("status");
def status: Int = json("eventData")("status");
if status >= 200 and status < 300 {
// نستخرج البيانات
def data: String = json.getObject("eventData").getString("body");
def data: String = json("eventData")("body");
// نقوم بتحديث النص
if this.getText() != data {
this.setText(data);
Expand Down
4 changes: 2 additions & 2 deletions Doc/chat_example.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -338,10 +338,10 @@ func main {
// define a closure that will be called when fetching the data from the server
onFetch = closure (json: Json) {
// at first, check that data is fetched without errors
def status: Int = json.getObject("eventData").getInt("status");
def status: Int = json("eventData")("status");
if status >= 200 and status < 300 {
// extract the data
def data: String = json.getObject("eventData").getString("body");
def data: String = json("eventData")("body");
// update the text
if this.getText() != data {
this.setText(data);
Expand Down
24 changes: 12 additions & 12 deletions Examples/chat.alusus
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import "Build";
import "Apm";
Apm.importFile("Alusus/Threading");
Apm.importFile("Alusus/WebPlatform");
use Srl;
use WebPlatform;
Expand All @@ -9,19 +10,25 @@ use WebPlatform;

def MAX_MESSAGES: 12;
def messages: Array[String];
def mutex: Threading.Mutex;
Threading.initMutex(mutex~ptr, 0);

@beEndpoint["POST", "/messages"]
func postMessages (conn: ptr[Http.Connection]) {
def postData: array[Char, 1024];
def postDataSize: Int = Http.read(conn, postData~ptr, postData~size);
Threading.lockMutex(mutex~ptr);
if messages.getLength() >= MAX_MESSAGES messages.remove(0);
messages.add(String(postData~ptr, postDataSize));
Threading.unlockMutex(mutex~ptr);
Http.print(conn, "HTTP/1.1 200 Ok\r\n\r\n");
}

@beEndpoint["GET", "/messages"]
func getMessages (conn: ptr[Http.Connection]) {
def response: String = String.merge(messages, "<br>");
Threading.lockMutex(mutex~ptr);
def response: String = String.merge(messages, "\n");
Threading.unlockMutex(mutex~ptr);
Http.print(conn, "HTTP/1.1 200 Ok\r\n");
Http.print(conn, "Content-Type: text/plain\r\n");
Http.print(conn, "Cache-Control: no-cache\r\n");
Expand Down Expand Up @@ -185,7 +192,7 @@ class TextEntry {
};
onKeyUp.connect(closure (widget: ref[TextInput], payload: ref[String]) {
if payload == "Shift+Enter" {
def newData: String = String("- ") + encodeHtml(widget.getText()).trim();
def newData: String = String("- ") + widget.getText().trim();
widget.setText(String());
if not self.onNewEntry.isNull() self.onNewEntry(newData);
}
Expand All @@ -200,7 +207,7 @@ class TextEntry {
justify = Justify.CENTER;
};
onClick.connect(closure (widget: ref[Widget], payload: ref[Int]) {
def newData: String = String("- ") + encodeHtml(self.textInput.getText()).trim();
def newData: String = String("- ") + self.textInput.getText().trim();
self.textInput.setText(String());
if not self.onNewEntry.isNull() self.onNewEntry(newData);
});
Expand All @@ -214,13 +221,6 @@ class TextEntry {
}
}

func encodeHtml(str: String): String {
return str
.replace("\n", "<br>")
.replace(",", "&comma;")
.replace("\"", "&quot;");
}

//==============================================================================
// Frontend Pages

Expand Down Expand Up @@ -269,9 +269,9 @@ func main {
fontSize = Length.pt(20.0);
};
onFetch = closure (json: Json) {
def status: Int = json.getObject("eventData").getInt("status");
def status: Int[64] = json("eventData")("status");
if status >= 200 and status < 300 {
def data: String = json.getObject("eventData").getString("body");
def data: String = json("eventData")("body");
if this.getText() != data {
this.setText(data);
}
Expand Down
24 changes: 12 additions & 12 deletions Examples/دردشة.أسس
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
مـحا.اشمل_ملف("Alusus/WebPlatform"، "مـنصة_ويب.أسس")؛
مـحا.اشمل_ملف("Alusus/Http"، "بـننف.أسس")؛
مـحا.اشمل_ملف("Alusus/Json"، "جـيسون.أسس")؛
مـحا.اشمل_ملف("Alusus/Threading"، "تـوازي.أسس")؛
اشمل "مـتم/سندات"؛
اشمل "مـتم/مـصفوفة"؛
اشمل "مـتم/نـص"؛
Expand All @@ -18,19 +19,25 @@

عرف _حد_الرسائل_: 12؛
عرف رسائل : مـصفوفة[نـص]؛
عرف مزامن: تـوازي.مـزامن؛
تـوازي.هيئ_مزامنا(مزامن~مؤشر، 0)؛

@منفذ_بياني["POST"، "/messages"]
دالة أضف_رسالة (اتصال:مؤشر[بـننف.اتـصال]){
عرف بيانات: مصفوفة[مـحرف،1024]؛
عرف حجم_البيانات: صحيح = بـننف.اقرأ(اتصال، بيانات~مؤشر، بيانات~حجم)؛
تـوازي.اقفل_مزامنا(مزامن~مؤشر)؛
إذا رسائل.هات_الطول() >= _حد_الرسائل_ رسائل.أزل(0)؛
رسائل.أضف(نـص(بيانات~مؤشر، حجم_البيانات))؛
تـوازي.افتح_مزامنا(مزامن~مؤشر)؛
بـننف.اطبع(اتصال، "HTTP/1.1 200 Ok\r\n\r\n")؛
}

@منفذ_بياني["GET"، "/messages"]
دالة هات_الرسائل (اتصال: مؤشر[بـننف.اتـصال]) {
عرف الرد: نـص = نـص.ادمج(رسائل، "<br>")؛
تـوازي.اقفل_مزامنا(مزامن~مؤشر)؛
عرف الرد: نـص = نـص.ادمج(رسائل، "\ج")؛
تـوازي.افتح_مزامنا(مزامن~مؤشر)؛
بـننف.اطبع(اتصال، "HTTP/1.1 200 Ok\r\n")؛
بـننف.اطبع(اتصال، "Content-Type: text/plain\r\n")؛
بـننف.اطبع(اتصال، "Cache-Control: no-cache\r\n")؛
Expand Down Expand Up @@ -197,7 +204,7 @@
عند_انتهاء_الكبسة.اربط(مغلفة (ودجة: سند[مـدخل_نص]، الحمولة: سند[نـص]) {
إذا الحمولة == "Shift+Enter" {
عرف بيانات_جديده: نـص = نـص("- ") + رمّز_النص_الفائق(ودجة.هات_النص()).شذب()؛
عرف بيانات_جديده: نـص = نـص("- ") + ودجة.هات_النص().شذب()؛
ودجة.حدد_النص(نـص())؛
إذا ليس الكائن.قد_تم_الإدخال.أهو_عدم() الكائن.قد_تم_الإدخال(بيانات_جديده)؛
}
Expand All @@ -212,7 +219,7 @@
ملء_السطر = مـلء_سطر._وسط_؛
عند_الضغط.اربط(مغلفة (ودجة: سند[ودجـة]، الحمولة: سند[صحيح]) {
عرف بيانات_جديده: نـص = نـص("- ") + رمّز_النص_الفائق(الكائن.مدخل_النص.هات_النص()).شذب()؛
عرف بيانات_جديده: نـص = نـص("- ") + الكائن.مدخل_النص.هات_النص().شذب()؛
الكائن.مدخل_النص.حدد_النص(نـص())؛
إذا ليس الكائن.قد_تم_الإدخال.أهو_عدم() الكائن.قد_تم_الإدخال(بيانات_جديده)؛
})؛
Expand All @@ -226,13 +233,6 @@
}
}

دالة رمّز_النص_الفائق(نص: نـص): نـص {
أرجع نص
.استبدل("\ج"، "<br>")
.استبدل("،"، "&comma;")
.استبدل("\""، "&quote;")؛
}

//=============================================================================
// صـفحات واجهة المستخدم

Expand Down Expand Up @@ -287,9 +287,9 @@
حجم_الخط = مـسافة.نقاط(20.0)؛
عند_استلام_بيانات = مغلفة (جيسون: جـيسون) {
عرف الحالة: صحيح = جيسون.هات_كائن("eventData").هات_صحيح("status")؛
عرف الحالة: صحيح = جيسون("eventData")("status")؛
إذا الحالة >= 200 و الحالة < 300 {
عرف البيانات: نـص = جيسون.هات_كائن("eventData").هات_نص("body")؛
عرف البيانات: نـص = جيسون("eventData")("body")؛
إذا هذا.هات_النص() != البيانات {
هذا.حدد_النص(البيانات)؛
}
Expand Down
4 changes: 2 additions & 2 deletions WebPlatform/EventHandling/dom_event_signals.alusus
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
eventHtmlIentifier,
this.signalName.defaultPrevented,
closure (json: Json) {
def data: Json = json.getObject("eventData");
def data: Json = json("eventData");
emitSignal;
}
);
Expand All @@ -37,7 +37,7 @@
eventHtmlIentifier,
keysToSwallow,
closure (json: Json) {
def data: Json = json.getObject("eventData");
def data: Json = json("eventData");
emitSignal;
}
);
Expand Down
5 changes: 2 additions & 3 deletions WebPlatform/EventHandling/event_loop.alusus
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
def jsonStr: ptr[array[Char]] = fetchNextEvent();
if jsonStr == 0 break;
def json: Json(jsonStr);
// TODO: Use (and implement) json.getArchInt("cbId").
def index: ArchInt = findEventHandlerIndex(json.getInt("cbId"));
def index: ArchInt = findEventHandlerIndex(json("cbId"));
if index != -1 {
if json.getBool("recurring") {
if json("recurring") {
eventHandlers(index).cb(json);
} else {
def cb: closure (Json) = eventHandlers(index).cb;
Expand Down
6 changes: 3 additions & 3 deletions WebPlatform/Resources/AudioResource.alusus
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
def p: SrdRef[Promise[Int]] = Promise[Int].new();
this.url = u;
loadAudio(u, closure (json: Json) {
def data: Json = json.getObject("eventData");
def data: Json = json("eventData");
if this.id != 0 {
releaseResource(this.id);
this.id = 0;
}
if data.getBool("success") {
this.id = data.getInt("resourceId");
if data("success") {
this.id = data("resourceId");
p.resolve(0);
} else p.reject(GenericError.new(Errors.AUDIO_LOAD, "Loading failed"));
});
Expand Down
12 changes: 6 additions & 6 deletions WebPlatform/Resources/ImageResource.alusus
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
def p: SrdRef[Promise[Int]] = Promise[Int].new();
this.url = u;
loadImage(u, closure (json: Json) {
def data: Json = json.getObject("eventData");
def data: Json = json("eventData");
if this.id != 0 {
releaseResource(this.id);
this.id = 0;
}
if data.getBool("success") {
this.id = data.getInt("resourceId");
if data("success") {
this.id = data("resourceId");
p.resolve(0);
} else p.reject(GenericError.new(Errors.IMAGE_LOAD, "Loading failed"));
});
Expand All @@ -31,13 +31,13 @@
if this.id != 0 releaseResource(this.id);
def p: SrdRef[Promise[Int]] = Promise[Int].new();
createImageResourceFromCanvasResource(canvas.id, closure (json: Json) {
def data: Json = json.getObject("eventData");
def data: Json = json("eventData");
if this.id != 0 {
releaseResource(this.id);
this.id = 0;
}
if data.getBool("success") {
this.id = data.getInt("resourceId");
if data("success") {
this.id = data("resourceId");
p.resolve(0);
} else p.reject(GenericError.new(Errors.IMAGE_LOAD, "Initialization failed"));
});
Expand Down
2 changes: 1 addition & 1 deletion WebPlatform/Widgets/Button.alusus
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
handler (this:Widget).update() set_ptr {
if this.id.getLength() > 0 {
this.updateCommon();
setElementAttribute(this.id, "innerHTML", this.text);
setElementAttribute(this.id, "textContent", this.text);
}
}

Expand Down
8 changes: 4 additions & 4 deletions WebPlatform/Widgets/Input.alusus
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@
if signal == this.onKeyPress~ptr {
handleSignalConnectionChanged[this.id, onKeyPress, "keypress", "", {
def outPayload: String;
if data.getBool("shiftKey") outPayload += "Shift+";
outPayload += data.getString("code");
if data("shiftKey") outPayload += "Shift+";
outPayload += data("code")~cast[String];
this.onKeyPress.emit(outPayload);
}];
} else if signal == this.onKeyUp~ptr {
handleSignalConnectionChanged[this.id, onKeyUp, "keyup", "", {
def outPayload: String;
if data.getBool("shiftKey") outPayload += "Shift+";
outPayload += data.getString("code");
if data("shiftKey") outPayload += "Shift+";
outPayload += data("code")~cast[String];
this.onKeyUp.emit(outPayload);
}];
}
Expand Down
4 changes: 2 additions & 2 deletions WebPlatform/Widgets/Picture.alusus
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@
for i=0,i<this.sourceArray.getLength() , i+=1{
htmlVal += String.format(
"<source srcset=\"%s\" media=\"(max-width:%s)\">",
this.sourceArray(i).src.buf,
this.sourceArray(i).maxWidth.buf
this.sourceArray(i).src.replace("\"", "\\\"").buf,
this.sourceArray(i).maxWidth.replace("\"", "\\\"").buf
);
}

Expand Down
3 changes: 2 additions & 1 deletion WebPlatform/Widgets/Select.alusus
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@
def htmlVal: String;
for i = 0, i < this.items.getLength(), ++i {
htmlVal += String("<option value= \"") +
this.items.keyAt(i) + "\">" + this.items.valAt(i) + "</option>";
this.items.keyAt(i).replace("\"", "\\\"") + "\">" +
this.items.valAt(i).replace("<", "&lt;").replace(">", "&gt;") + "</option>";
}
setElementAttribute(this.id, "innerHTML", htmlVal);
if this.ariaHidden == true{
Expand Down
2 changes: 1 addition & 1 deletion WebPlatform/Widgets/Text.alusus
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
handler (this:Widget).update() set_ptr {
if this.id.getLength() > 0 {
this.updateCommon();
setElementAttribute(this.id, "innerHTML", this.text);
setElementAttribute(this.id, "innerText", this.text);
if this.ariaHidden == true {
setElementAttribute(this.id, String("aria-hidden"), String("true"));
}
Expand Down
8 changes: 4 additions & 4 deletions WebPlatform/Widgets/TextInput.alusus
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@
if signal == this.onKeyPress~ptr {
handleSignalConnectionChanged[this.id, onKeyPress, "keypress", "", {
def outPayload: String;
if data.getBool("shiftKey") outPayload += "Shift+";
outPayload += data.getString("code");
if data("shiftKey") outPayload += "Shift+";
outPayload += data("code")~cast[String];
this.onKeyPress.emit(outPayload);
}];
} else if signal == this.onKeyUp~ptr {
handleSignalConnectionChanged[this.id, onKeyUp, "keyup", "", {
def outPayload: String;
if data.getBool("shiftKey") outPayload += "Shift+";
outPayload += data.getString("code");
if data("shiftKey") outPayload += "Shift+";
outPayload += data("code")~cast[String];
this.onKeyUp.emit(outPayload);
}];
}
Expand Down

0 comments on commit 8faf5eb

Please sign in to comment.