Skip to content

Commit

Permalink
CURRENT TESTS PASSED
Browse files Browse the repository at this point in the history
	eval("<script>console.log('ok!')</script>");
	eval("<script>alert('alert ok!')</script>");// // pop up window NOT supported by WebView, so we use print instead
  • Loading branch information
pannous committed Dec 28, 2023
1 parent 665bf28 commit ec02feb
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 29 deletions.
5 changes: 5 additions & 0 deletions source/Angle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1783,6 +1783,10 @@ Node &analyze(Node &node, Function &function) {
functions["createHtml"].is_used = true;
return node; // html builder currently not parsed
}
if (name == "js" or name == "script" or name == "javascript") {
functions["addScript"].is_used = true;
return node;
}
if (name == "if")return groupIf(node, function);
if (name == "while")return groupWhile(node, function);
if (name == "?")return groupIf(node, function);
Expand Down Expand Up @@ -1920,6 +1924,7 @@ void preRegisterFunctions() {
functions["getDocumentBody"].import().signature.returns(externref);
functions["createHtml"].import();
functions["createHtml"].signature.add(externref, "parent").add(charp, "innerHtml").returns(externref);
functions["addScript"].import().signature.add(charp, "js");

functions["getElementById"].import();//.builtin();
functions["getElementById"].signature.add(charp).returns(externref /*!!*/);
Expand Down
6 changes: 4 additions & 2 deletions source/String.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -562,8 +562,10 @@ codepoint *String::extractCodepoints(bool again) {
return codepoints;
}

bool String::startsWith(chars string) {
return indexOf(string) == 0;
bool String::startsWith(chars string, int from) {
int len1 = strlen(string);
if (len1 > length)return false;
return eq(data + from, string, len1);
}

bool String::endsWith(const char *string) {
Expand Down
2 changes: 1 addition & 1 deletion source/String.h
Original file line number Diff line number Diff line change
Expand Up @@ -1106,7 +1106,7 @@ class String {

codepoint *end();

bool startsWith(chars string);
bool startsWith(chars string, int from = 0);

bool endsWith(const char *string);

Expand Down
38 changes: 24 additions & 14 deletions source/Wasp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1494,17 +1494,27 @@ class Wasp {
actual.add(operatorr());
break;
case '<':
if (text.substring(at, at + 6) == "<html>") {
if (text.substring(at, at + 5) == "<html") {
int to = text.find("</html>", at);
if (to > 0) {
auto innerHtml = text.substring(at + 6, to);
// actual.add(Node("html").add(Node(innerHtml)));
actual.add(Node("html", strings).setValue(*new Value{.string=&innerHtml.clone()}));
at = to + 7;
previous = '>';
proceed();
break;
}
if (to < 0) to = text.length;// warn("unclosed html tag");
auto html = Node("html", strings);
html.value.string = &text.substring(text.find('>', at + 5) + 1, to).clone();
actual.add(html);
at = to + 7;
previous = '>';
proceed();
break;
}
if (text.startsWith("<script", at)) {
int to = text.find("</script>", at);
if (to < 0) to = text.length;// warn("unclosed script tag");
auto html = Node("script", strings);
html.value.string = &text.substring(text.find('>', at + 5) + 1, to).clone();
actual.add(html);
at = to + 9;
previous = '>';
proceed();
break;
}
case '>':
if (not(parserOptions.use_tags or parserOptions.use_generics) or
Expand Down Expand Up @@ -2027,10 +2037,10 @@ int main(int argc, char **argv) {
if (args.endsWith(".html") or args.endsWith(".htm")) {
#if WEBAPP
// start_server(SERVER_PORT);
std::thread go(start_server, SERVER_PORT);
auto arg = "http://localhost:"s + SERVER_PORT + "/" + args;
print("Serving "s + arg);
open_webview(arg);
std::thread go(start_server, SERVER_PORT);
auto arg = "http://localhost:"s + SERVER_PORT + "/" + args;
print("Serving "s + arg);
open_webview(arg);
// arg.replaceMatch(".*\/", "http://localhost:SERVER_PORT/");
#else
print("wasp compiled without webview");
Expand Down
7 changes: 7 additions & 0 deletions source/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@
}
identity = x => x

alert = print // pop up window NOT supported by WebView, so we use print instead

const utf8_decoder = new TextDecoder('utf8');

const string = function (pointer, length, format) {
Expand All @@ -251,6 +253,11 @@
parent.appendChild(element);
return element;
},
addScript: (scriptContent) => {
let script = document.createElement('script');
script.textContent = string(scriptContent);
document.body.appendChild(script);
},
createHtmlElement: (tag, id) => {
let element = document.createElement(string(tag));
element.id = string(id);
Expand Down
4 changes: 4 additions & 0 deletions source/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,12 @@ void testInnerHtml() {
check_is(*html.value.string, "<bold>test</bold>");
auto serialized = html.serialize();
check_is(serialized, "<html><bold>test</bold></html>");
// eval("<html><script>alert('ok')");
// eval("<html><script>alert('ok')</script></html>");
eval("<html><bold id=b ok=123>test</bold></html>");
assert_is("$b.ok", 123);
eval("<script>console.log('ok!')</script>");
eval("<script>alert('alert ok!')</script>");// // pop up window NOT supported by WebView, so we use print instead
// eval("$b.innerHTML='<i>ok</i>'");
// eval("<html><bold id='anchor'>…</bold></html>");
// eval("$anchor.innerHTML='<i>ok</i>'");
Expand Down
36 changes: 24 additions & 12 deletions source/wasm_emitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,27 @@ Code emitPrimitiveArray(Node &node, Function &context) {
return code;
}


// extern "C" ExternRef createHtml(ExternRef parent /*0*/,chars innerHTML); // html{bold{Hello}} => appendChild bold to body
[[nodiscard]]
Code emitHtml(Node &node, Function &function, ExternRef parent = 0) {
Code code;
if (parent)code.add(emitData(*new Node(parent), function));
else code.add(emitCall(*new Node("getDocumentBody"), function));
code.add(emitString(node, function));
code.add(emitCall(*new Node("createHtml"), function));
return code;
}

[[nodiscard]]
Code emitScript(Node &node, Function &function) {
Code code;
code.add(emitString(node, function));
code.add(emitCall(*new Node("addScript"), function));
return code;
}


short arrayElementSize(Node &node);

Primitive addTypeFromSize(Node &array, short size);
Expand Down Expand Up @@ -1845,6 +1866,9 @@ Code emitExpression(Node &node, Function &context/*="wasp_main"*/) { // expressi
// }
if (name == "html")
return emitHtml(node, context, 0);
if (name == "script" or name == "js" or name == "javascript")
return emitScript(node, context);
// or name == "wasm" or name == "wasm32" or name == "wasm64"
if (name == "if")
return emitIf(node, context);
if (name == "while")
Expand Down Expand Up @@ -2045,18 +2069,6 @@ Code emitExpression(Node &node, Function &context/*="wasp_main"*/) { // expressi
// return code;
//}

// extern "C" ExternRef createHtml(ExternRef parent /*0*/,chars innerHTML); // html{bold{Hello}} => appendChild bold to body
[[nodiscard]]
Code emitHtml(Node &node, Function &function, ExternRef parent = 0) {
Code code;
if (parent)code.add(emitData(*new Node(parent), function));
else code.add(emitCall(*new Node("getDocumentBody"), function));
code.add(emitString(node, function));
code.add(emitCall(*new Node("createHtml"), function));
return code;
}


Primitive elementType(Type type32) {
if (type32 == stringp)return byte_char;
if (type32 == longs)return Primitive::wasm_int64;// todo should not have reached here
Expand Down

0 comments on commit ec02feb

Please sign in to comment.