From 528ac5f9a74fd876bdeaba7d968173573d25c946 Mon Sep 17 00:00:00 2001 From: Ethosa Date: Tue, 30 Jul 2024 11:49:16 +0700 Subject: [PATCH 1/3] fix getEventIndex function --- src/happyx/spa/tag.nim | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/happyx/spa/tag.nim b/src/happyx/spa/tag.nim index b9afeb03..fa21b0a7 100644 --- a/src/happyx/spa/tag.nim +++ b/src/happyx/spa/tag.nim @@ -102,8 +102,11 @@ when defined(js): const _originCloneNode = Node.prototype.cloneNode; Node.prototype.__getEventIndex = function(target, targetArgs) { - if (!this._eventListeners) + if (!this._eventListeners) { this._eventListeners = []; + return -1; + } + if (!targetArgs) return -1; return this._eventListeners.findIndex(args => { for (let i = 0; i < args.length; i++) if (targetArgs[i] !== args[i]) return false; From 012a2651ea9114be7c175b9923e26598b96c778d Mon Sep 17 00:00:00 2001 From: Ethosa Date: Fri, 2 Aug 2024 08:58:38 +0700 Subject: [PATCH 2/3] solve #325 --- src/happyx/spa/tag.nim | 8 ++++---- tests/testc20.nim | 5 +++++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/happyx/spa/tag.nim b/src/happyx/spa/tag.nim index fa21b0a7..b46b12d3 100644 --- a/src/happyx/spa/tag.nim +++ b/src/happyx/spa/tag.nim @@ -601,12 +601,12 @@ func `$`*(self: TagRef): string = argsStr = self.args.join(" ") if self.isText: - return self.name + return xmltree.escape(self.name) var attrs = "" for key, value in self.attrs.pairs(): if value.len > 0: - attrs &= " " & key & "=" & "\"" & value & "\"" + attrs &= " " & key & "=" & "\"" & value.replace("\"", """) & "\"" else: attrs &= " " & key @@ -935,12 +935,12 @@ when defined(js): argsStr = self.args.join(" ") if self.isText: - return self.name + return xmltree.escape(self.name) var attrs = "" for key, value in self.attrs.pairs(): if value.len > 0: - attrs &= " " & key & "=" & "\"" & value & "\"" + attrs &= " " & key & "=" & "\"" & value.replace("\"", """) & "\"" else: attrs &= " " & key diff --git a/tests/testc20.nim b/tests/testc20.nim index 25d8d795..b58791b4 100644 --- a/tests/testc20.nim +++ b/tests/testc20.nim @@ -13,6 +13,11 @@ serve("127.0.0.1", 5000): post "/[data:Obj:json]": echo data return "" + + get "/": + buildHtml: + tDiv(class="hello\" style=\"color:green"): + "hello world" get "/issue287": statusCode = 200 From 6a9c029092aea0fefb882ccd7d0f3647c63093da Mon Sep 17 00:00:00 2001 From: Ethosa Date: Tue, 6 Aug 2024 14:19:48 +0700 Subject: [PATCH 3/3] update cors --- src/happyx/ssr/cors.nim | 10 ++++++++-- tests/testc6.nim | 2 +- tests/testjs18.nim | 17 +++++++++++++++++ 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/happyx/ssr/cors.nim b/src/happyx/ssr/cors.nim index 4c11e557..34768e58 100644 --- a/src/happyx/ssr/cors.nim +++ b/src/happyx/ssr/cors.nim @@ -45,7 +45,13 @@ when not defined(js) and not (exportJvm or exportPython or defined(napibuild)): if `allowMethods`.len > 0: `headers`["Access-Control-Allow-Methods"] = `allowMethods` if `allowOrigins`.len > 0: - `headers`["Access-Control-Allow-Origin"] = `allowOrigins` + if `allowOrigins` == @["*"]: + when not enableHttpx and not enableHttpBeast: + `headers`["Access-Control-Allow-Origin"] = req.hostname + else: + `headers`["Access-Control-Allow-Origin"] = req.ip + else: + `headers`["Access-Control-Allow-Origin"] = `allowOrigins` macro regCORS*(body: untyped): untyped = ## Register CORS @@ -148,7 +154,7 @@ else: if cors.allowOrigins.len > 0: for origin in cors.allowOrigins: if origin == "*": - `headers`["Access-Control-Allow-Origin"] = "*" + `headers`["Access-Control-Allow-Origin"] = host break elif origin == `host`: `headers`["Access-Control-Allow-Origin"] = origin diff --git a/tests/testc6.nim b/tests/testc6.nim index 5dc3e65b..e92da8a2 100644 --- a/tests/testc6.nim +++ b/tests/testc6.nim @@ -5,7 +5,7 @@ import regCORS: credentials: true - origins: "https://www.google.com" # You can send request from this address + origins: "*" # "https://www.google.com" # You can send request from this address methods: ["GET", "POST", "PUT"] headers: "*" diff --git a/tests/testjs18.nim b/tests/testjs18.nim index 7353c284..c89ec471 100644 --- a/tests/testjs18.nim +++ b/tests/testjs18.nim @@ -86,8 +86,25 @@ let C = buildHtml: text(x="35",y="20", style= "fill: black;"): "C: Anyone Here?" +component Hello: + html: + tDiv: + "hello" + +let b = buildHtml: + Hello + +let c = buildHtml: + tDiv: + "hello" + + appRoutes("app"): "/": + tDiv: {$b} + tDiv: + {$c} + c for i in 1..5: tDiv: {$htmlProcs[0]()}