about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorfox <fox@fakecake.org>2026-06-26 18:12:24 +0300
committerfox <fox@fakecake.org>2026-06-26 18:12:24 +0300
commitd134234182b77a42840470f5dd8534faf9d5d619 (patch)
tree10c19807f625e1c76741ed33e8fe3e31da1a4c26
parent04524943b6b6b1ed314df320630fa3eed1783b2c (diff)
parenta5e34bf3572a5eb8c4f67e8014fee8587c74c8d8 (diff)
downloadthe-epube-d134234182b77a42840470f5dd8534faf9d5d619.tar.gz
the-epube-d134234182b77a42840470f5dd8534faf9d5d619.tar.xz
Merge branch 'dev' into 'master'
switch to regex-based escapeHtml() because DOM is more expensive

See merge request git/the-epube!70
-rw-r--r--classes/Handler/DefineHandler.php4
-rw-r--r--read.html2
-rw-r--r--src/app.js30
-rw-r--r--src/reader.js21
-rw-r--r--tests_integration/DefineHandlerTest.php8
5 files changed, 44 insertions, 21 deletions
diff --git a/classes/Handler/DefineHandler.php b/classes/Handler/DefineHandler.php
index 3da21da..53958f2 100644
--- a/classes/Handler/DefineHandler.php
+++ b/classes/Handler/DefineHandler.php
@@ -46,7 +46,7 @@ class DefineHandler implements RequestHandlerInterface, AuthRequired
                 $retParsed = $this->parseDictReply($dictReply);
 
                 if (count($retParsed) > 0) {
-                    array_push($result, "<strong>" . htmlspecialchars($word, ENT_QUOTES, 'UTF-8') . "</strong>");
+                    array_push($result, htmlspecialchars($word, ENT_QUOTES, 'UTF-8') . ":");
                     $result = array_merge($result, $retParsed);
                     break;
                 } else {
@@ -64,7 +64,7 @@ class DefineHandler implements RequestHandlerInterface, AuthRequired
         }
 
         if (count($result) == 0) {
-            array_push($result, "No results for: <b>" . htmlspecialchars($origWord, ENT_QUOTES, 'UTF-8') . "</b>");
+            array_push($result, "No results for: " . htmlspecialchars($origWord, ENT_QUOTES, 'UTF-8'));
         }
 
         return (new Response())->withBody(json_encode(["result" => $result]));
diff --git a/read.html b/read.html
index 5a97ea5..60f9301 100644
--- a/read.html
+++ b/read.html
@@ -176,7 +176,7 @@
    </div>
    <div class="modal-body">
 
-			<form class="form-horizontal" >
+			<form class="form-horizontal" onsubmit="return false">
 				<div class="form-group">
 					<label class="col-sm-4 control-label">Lookup word:</label>
 					<div class="col-sm-8">
diff --git a/src/app.js b/src/app.js
index 96c7a0e..03aefd1 100644
--- a/src/app.js
+++ b/src/app.js
@@ -141,11 +141,31 @@ class AppCommon {
             "lib/bootstrap/v3/css/" + (is_night ? "theme-dark.min.css" : "bootstrap-theme.min.css"));
     }
 
-    escapeHtml(str) {
-        if (!str) return "";
-        const div = document.createElement("div");
-        div.appendChild(document.createTextNode(str));
-        return div.innerHTML;
+    /** htmlspecialchars()-alike for headlines data-content attribute
+      * @param {string | number | boolean} p
+      *
+      * @return {string}
+      */
+    escapeHtml(p) {
+        if (p === null || p === undefined) {
+            return '';
+        }
+
+        if (typeof p !== "string") {
+            return String(p);
+        } else {
+            const map = {
+                '&': '&amp;',
+                '<': '&lt;',
+                '>': '&gt;',
+                '"': '&quot;',
+                '/': '&#x2F;',
+                "'": '&#039;',
+                '`': '&#x60;'
+            };
+
+            return p.replace(/[&<>"'`/]/g, function(m) { return map[m]; });
+        }
     }
 }
 
diff --git a/src/reader.js b/src/reader.js
index a45ce9c..a3b81b7 100644
--- a/src/reader.js
+++ b/src/reader.js
@@ -242,7 +242,7 @@ class ReaderClass extends AppCommon {
 
                 console.error(err);
 
-                $(".loading-message").html(`Unable to load book info.<br/><small>${this.escapeHtml(err.responseText)}</small>`);
+                $(".loading-message").html(`Unable to load book info.<br/><small>${this.escapeHtml((String)(err.responseText))}</small>`);
                 return;
             }
         }
@@ -582,7 +582,7 @@ class ReaderClass extends AppCommon {
                     });
 
                     if (toc_entry && toc_entry.label)
-                        $(".chapter_title").html(toc_entry.label.trim() + " | ");
+                        $(".chapter_title").text(this.escapeHtml(toc_entry.label.trim()) + " | ");
 
                     this.generateTocBar(book, this.flattenToc(book));
                 }
@@ -735,9 +735,12 @@ class ReaderClass extends AppCommon {
 
         $('#dict-modal').on('shown.bs.modal', () => {
             $(".dict_query")
-                .off("change")
-                .on("change", () => {
-                    this.lookupWord(null, null, '.dict_query');
+                .off("keyup")
+                .on("keyup", (evt) => {
+                    if (evt.which === 13) {
+                        evt.preventDefault();
+                        this.lookupWord(null, null, '.dict_query');
+                    }
                 });
 
             $(".dict_result").scrollTop(0);
@@ -792,7 +795,7 @@ class ReaderClass extends AppCommon {
 
                     const a = $("<a>")
                         .attr('href', '#')
-                        .html("<b class='pull-right'>" + toc_loc_msg(row.href) + "</b>" + row.label)
+                        .html("<b class='pull-right'>" + toc_loc_msg(row.href) + "</b>" + _reader.escapeHtml(row.label))
                         .attr('data-href', row.href)
                         .click(function() {
                             book.rendition.display(a.attr('data-href'));
@@ -821,7 +824,7 @@ class ReaderClass extends AppCommon {
                 try {
                     const a = $("<a>")
                         .attr('href', '#')
-                        .html("<b class='pull-right'>" + toc_loc_msg(row.href) + "</b>" + row.label)
+                        .html("<b class='pull-right'>" + toc_loc_msg(row.href) + "</b>" + _reader.escapeHtml(row.label))
                         .attr('data-href', row.href)
                         .click(function() {
                             book.rendition.display(a.attr('data-href'));
@@ -1395,7 +1398,7 @@ class ReaderClass extends AppCommon {
 
             if (data.data) {
 
-                $(".dict_result").html(data.data.result.join("<br/>"));
+                $(".dict_result").html(data.data.result.map((r) => this.escapeHtml(String(r))).join("<br/>"));
                 $(".dict_query").val(word);
 
                 if (callback) callback();
@@ -1406,7 +1409,7 @@ class ReaderClass extends AppCommon {
 
             console.error(err);
 
-            $(".dict_result").html(`Network error while looking up word: ${this.escapeHtml(err.responseText)}.`);
+            $(".dict_result").html(`Network error while looking up word: ${this.escapeHtml((String)(err.responseText))}.`);
         }
     }
 
diff --git a/tests_integration/DefineHandlerTest.php b/tests_integration/DefineHandlerTest.php
index 6dcce49..c695f6b 100644
--- a/tests_integration/DefineHandlerTest.php
+++ b/tests_integration/DefineHandlerTest.php
@@ -41,7 +41,7 @@ final class DefineHandlerTest extends ApiTestCase
 		$this->assertArrayHasKey('result', $body);
 		// Either disabled message or definitions
 		$isDisabled = isset($body['result'][0]) && $body['result'][0] === 'Dictionary lookups are disabled.';
-		$isDefined = isset($body['result'][0]) && str_contains($body['result'][0], '<strong>');
+		$isDefined = isset($body['result'][0]) && str_contains($body['result'][0], 'cat:');
 		$this->assertTrue($isDisabled || $isDefined, 'Expected either disabled message or definitions');
 	}
 
@@ -65,8 +65,8 @@ final class DefineHandlerTest extends ApiTestCase
 		$this->assertEquals(200, $status);
 		$this->assertArrayHasKey('result', $body);
 		$this->assertNotEmpty($body['result']);
-		// First result should be the word with <strong> tags
-		$this->assertEquals('<strong>cat</strong>', $body['result'][0]);
+		// First result should be the word with :
+		$this->assertEquals('cat:', $body['result'][0]);
 		// Followed by definition(s)
 		$this->assertGreaterThan(1, count($body['result']));
 	}
@@ -91,7 +91,7 @@ final class DefineHandlerTest extends ApiTestCase
 		$this->assertArrayHasKey('result', $body);
 		$this->assertNotEmpty($body['result']);
 		// Should match "cat" definition
-		$this->assertEquals('<strong>cats</strong>', $body['result'][0]);
+		$this->assertEquals('cats:', $body['result'][0]);
 	}
 
 	public function test_define_returns_no_results_for_unknown_word(): void