From b5f2a36597da9920081c65f826404cf054627745 Mon Sep 17 00:00:00 2001
From: Cashew <52880648+Scrub1492@users.noreply.github.com>
Date: Mon, 1 Jan 2024 08:08:43 +0700
Subject: document-util to use JSON attribute instead of multiple data-*
attributes (#492)
* data-* to json
* update tests
* fix tests
* fix tests
* test fixes
* fix types
* fix types
---
test/data/html/document-util.html | 446 +++++++++++++++++++++-----------------
test/document-util.test.js | 39 ++--
types/test/document-util.d.ts | 42 ++++
3 files changed, 305 insertions(+), 222 deletions(-)
create mode 100644 types/test/document-util.d.ts
diff --git a/test/data/html/document-util.html b/test/data/html/document-util.html
index d8ac012e..8e007392 100644
--- a/test/data/html/document-util.html
+++ b/test/data/html/document-util.html
@@ -15,183 +15,207 @@
真白「心配してくださって、ありがとございます」
真白「心配してくださって、ありがとございます」
真白「心配して「くださって」、ありがと「ございます」」
ありがとございます。ありがとございます。
ありがとございます。ありがとございます。
ありがとございます。!?ありがとございます。!?
ありがとございます!!!ありがとございます!!!
ありがとございます1
ありがとございます2
@@ -202,16 +226,18 @@
ありがとございます1
ありがとございます2
@@ -221,27 +247,31 @@
あいうえお
@@ -259,26 +289,30 @@
あいうえお
@@ -296,26 +330,30 @@
あいうえお
@@ -333,26 +371,30 @@
あいうえお
diff --git a/test/document-util.test.js b/test/document-util.test.js
index 109345d1..cc8db706 100644
--- a/test/document-util.test.js
+++ b/test/document-util.test.js
@@ -24,6 +24,7 @@ import {DOMTextScanner} from '../ext/js/dom/dom-text-scanner.js';
import {TextSourceElement} from '../ext/js/dom/text-source-element.js';
import {TextSourceRange} from '../ext/js/dom/text-source-range.js';
import {createDomTest} from './fixtures/dom-test.js';
+import {parseJson} from '../dev/json.js';
const dirname = path.dirname(fileURLToPath(import.meta.url));
@@ -116,6 +117,7 @@ describe('DocumentUtil', () => {
const {document} = window;
for (const testElement of /** @type {NodeListOf} */ (document.querySelectorAll('test-case[data-test-type=scan]'))) {
// Get test parameters
+ /** @type {import('test/document-util').DocumentUtilTestData} */
const {
elementFromPointSelector,
caretRangeFromPointSelector,
@@ -128,17 +130,15 @@ describe('DocumentUtil', () => {
sentence,
hasImposter,
terminateAtNewlines
- } = testElement.dataset;
+ } = parseJson(/** @type {string} */ (testElement.dataset.testData));
const elementFromPointValue = querySelectorChildOrSelf(testElement, elementFromPointSelector);
const caretRangeFromPointValue = querySelectorChildOrSelf(testElement, caretRangeFromPointSelector);
const startNode = getChildTextNodeOrSelf(window, querySelectorChildOrSelf(testElement, startNodeSelector));
const endNode = getChildTextNodeOrSelf(window, querySelectorChildOrSelf(testElement, endNodeSelector));
- const startOffset2 = parseInt(/** @type {string} */ (startOffset), 10);
- const endOffset2 = parseInt(/** @type {string} */ (endOffset), 10);
- const sentenceScanExtent2 = parseInt(/** @type {string} */ (sentenceScanExtent), 10);
- const terminateAtNewlines2 = (terminateAtNewlines !== 'false');
+ // Defaults to true
+ const terminateAtNewlines2 = typeof terminateAtNewlines === 'boolean' ? terminateAtNewlines : true;
expect(elementFromPointValue).not.toStrictEqual(null);
expect(caretRangeFromPointValue).not.toStrictEqual(null);
@@ -150,11 +150,11 @@ describe('DocumentUtil', () => {
document.caretRangeFromPoint = (x, y) => {
const imposter = getChildTextNodeOrSelf(window, findImposterElement(document));
- expect(!!imposter).toStrictEqual(hasImposter === 'true');
+ expect(!!imposter).toStrictEqual(!!hasImposter);
const range = document.createRange();
- range.setStart(/** @type {Node} */ (imposter ? imposter : startNode), startOffset2);
- range.setEnd(/** @type {Node} */ (imposter ? imposter : startNode), endOffset2);
+ range.setStart(/** @type {Node} */ (imposter ? imposter : startNode), startOffset);
+ range.setEnd(/** @type {Node} */ (imposter ? imposter : startNode), endOffset);
// Override getClientRects to return a rect guaranteed to contain (x, y)
range.getClientRects = () => {
@@ -214,7 +214,7 @@ describe('DocumentUtil', () => {
const sentenceActual = DocumentUtil.extractSentence(
source,
false,
- sentenceScanExtent2,
+ sentenceScanExtent,
terminateAtNewlines2,
terminatorMap,
forwardQuoteMap,
@@ -233,6 +233,7 @@ describe('DOMTextScanner', () => {
const {document} = window;
for (const testElement of /** @type {NodeListOf} */ (document.querySelectorAll('test-case[data-test-type=text-source-range-seek]'))) {
// Get test parameters
+ /** @type {import('test/document-util').DOMTextScannerTestData} */
const {
seekNodeSelector,
seekNodeIsText,
@@ -243,33 +244,31 @@ describe('DOMTextScanner', () => {
expectedResultNodeIsText,
expectedResultOffset,
expectedResultContent
- } = testElement.dataset;
-
- const seekOffset2 = parseInt(/** @type {string} */ (seekOffset), 10);
- const seekLength2 = parseInt(/** @type {string} */ (seekLength), 10);
- const expectedResultOffset2 = parseInt(/** @type {string} */ (expectedResultOffset), 10);
+ } = parseJson(/** @type {string} */ (testElement.dataset.testData));
/** @type {?Node} */
let seekNode = testElement.querySelector(/** @type {string} */ (seekNodeSelector));
- if (seekNodeIsText === 'true' && seekNode !== null) {
+ if (seekNodeIsText && seekNode !== null) {
seekNode = seekNode.firstChild;
}
+ const expectedResultContent2 = expectedResultContent.join('\n');
+
/** @type {?Node} */
let expectedResultNode = testElement.querySelector(/** @type {string} */ (expectedResultNodeSelector));
- if (expectedResultNodeIsText === 'true' && expectedResultNode !== null) {
+ if (expectedResultNodeIsText && expectedResultNode !== null) {
expectedResultNode = expectedResultNode.firstChild;
}
const {node, offset, content} = (
seekDirection === 'forward' ?
- new DOMTextScanner(/** @type {Node} */ (seekNode), seekOffset2, true, false).seek(seekLength2) :
- new DOMTextScanner(/** @type {Node} */ (seekNode), seekOffset2, true, false).seek(-seekLength2)
+ new DOMTextScanner(/** @type {Node} */ (seekNode), seekOffset, true, false).seek(seekLength) :
+ new DOMTextScanner(/** @type {Node} */ (seekNode), seekOffset, true, false).seek(-seekLength)
);
expect(node).toStrictEqual(expectedResultNode);
- expect(offset).toStrictEqual(expectedResultOffset2);
- expect(content).toStrictEqual(expectedResultContent);
+ expect(offset).toStrictEqual(expectedResultOffset);
+ expect(content).toStrictEqual(expectedResultContent2);
}
});
});
diff --git a/types/test/document-util.d.ts b/types/test/document-util.d.ts
new file mode 100644
index 00000000..3c09f7f0
--- /dev/null
+++ b/types/test/document-util.d.ts
@@ -0,0 +1,42 @@
+/*
+ * Copyright (C) 2023 Yomitan Authors
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ */
+
+export type DocumentUtilTestData = {
+ elementFromPointSelector: string;
+ caretRangeFromPointSelector: string;
+ startNodeSelector: string;
+ startOffset: number;
+ endNodeSelector: string;
+ endOffset: number;
+ resultType: string;
+ sentenceScanExtent: number;
+ sentence: string;
+ hasImposter: boolean | undefined;
+ terminateAtNewlines: boolean | undefined;
+};
+
+export type DOMTextScannerTestData = {
+ seekNodeSelector: string;
+ seekNodeIsText: boolean;
+ seekOffset: number;
+ seekLength: number;
+ seekDirection: string;
+ expectedResultNodeSelector: string;
+ expectedResultNodeIsText: boolean;
+ expectedResultOffset: number;
+ expectedResultContent: string[];
+};
--
cgit v1.2.3