Skip to content

Commit

Permalink
Added Support for Inline JS
Browse files Browse the repository at this point in the history
  • Loading branch information
usernane committed Sep 10, 2023
1 parent 6177530 commit c668570
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 3 deletions.
41 changes: 41 additions & 0 deletions tests/webfiori/test/ui/LoadTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public function test07() {
$node = $compiler->getCompiled();
$this->assertEquals("<div>\n No posts.\n</div>", $node->toHTML());
}

/**
* @test
*/
Expand All @@ -135,6 +136,46 @@ public function test08() {
. "</div>"
. "</div>", $compiler->getCompiled()->toHTML());
}
/**
* @test
*/
public function test09() {
$compiler = new TemplateCompiler('template.html');
$this->assertEquals("<div v-if=\"someVar <= 6 || someVar >= 8 || someVar === 6\">\r\n"
. " <script>\r\n"
. " \r\n"
. " function allIsGood() {\r\n"
. " if (a > 6) {\r\n"
. " alert(\"Oh. A is > 6 but probably < 100.\");\r\n"
. " }\r\n"
. " if (a < 100) {\r\n"
. " alert('Oh. A is < 100.');\r\n"
. " }\r\n"
. " }\r\n"
. " \r\n"
. " </script>\r\n"
. "</div>\r\n", $compiler->getCompiled()->toHTML(true));
}
/**
* @test
*/
public function test10() {
$compiler = new TemplateCompiler('template2.php');
$this->assertEquals("<div v-if=\"someVar <= 6 || someVar >= 8 || someVar === 6\">\r\n"
. " <script>\r\n"
. " \r\n"
. " function allIsGood() {\r\n"
. " if (a > 6) {\r\n"
. " alert(\"Oh. A is > 6 but probably < 100.\");\r\n"
. " }\r\n"
. " if (a < 100) {\r\n"
. " alert('Oh. A is < 100.');\r\n"
. " }\r\n"
. " }\r\n"
. " \r\n"
. " </script>\r\n"
. "</div>\r\n", $compiler->getCompiled()->toHTML(true));
}
/**
* @test
*/
Expand Down
12 changes: 12 additions & 0 deletions tests/webfiori/test/ui/template.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div v-if="someVar <= 6 || someVar >= 8 || someVar === 6">
<script>
function allIsGood() {
if (a > 6) {
alert("Oh. A is > 6 but probably < 100.");
}
if (a < 100) {
alert('Oh. A is < 100.');
}
}
</script>
</div>
14 changes: 14 additions & 0 deletions tests/webfiori/test/ui/template2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
?>
<div v-if="someVar <= 6 || someVar >= 8 || someVar === 6">
<script>
function allIsGood() {
if (a > 6) {
alert("Oh. A is > 6 but probably < 100.");
}
if (a < 100) {
alert('Oh. A is < 100.');
}
}
</script>
</div>
19 changes: 16 additions & 3 deletions webfiori/ui/TemplateCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -762,10 +762,12 @@ private static function parseAttributesHelper(Queue $queue, bool $isEqualFound,
* with the hashes.
*/
private static function replaceAttrsValues(string $htmlStr) : array {
$scripts = [];
preg_match_all("/(?<=<script>)(.*?)(?=<\/script>)/s", $htmlStr, $scripts);

//For double quotation
$attrsArr = [];
//After every attribute value, there must be a space if more than one
//attribute.
//After every attribute value, there must be a space if more than one attribute.
preg_match_all('/"[\t-!#-~]+" |"[\t-!#-~]+">|""/', $htmlStr, $attrsArr);
$tempValuesArr = [];

Expand All @@ -778,6 +780,17 @@ private static function replaceAttrsValues(string $htmlStr) : array {
$tempValuesArr[$key] = $trimmed;
$htmlStr = str_replace($value, '"'.$key.'"', $htmlStr);
}

$tempScriptsArr = [];

foreach ($scripts[0] as $scriptArr) {
//Used to always generate a unique key based on time.
$now = date('H:i:s.').gettimeofday()["usec"];
$scriptTxt = $scriptArr;
$key = hash('sha256', $scriptTxt.$now).'-'.$now;
$tempScriptsArr[$key] = $scriptTxt;
$htmlStr = str_replace($scriptTxt, $key, $htmlStr);
}

//For single quotes
$attrsArr2 = [];
Expand All @@ -794,7 +807,7 @@ private static function replaceAttrsValues(string $htmlStr) : array {
}

return [
'replacements' => $tempValuesArr,
'replacements' => array_merge($tempValuesArr, $tempScriptsArr),
'html-string' => $htmlStr
];
}
Expand Down

0 comments on commit c668570

Please sign in to comment.