aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2020-05-02 13:05:43 -0400
committerGitHub <noreply@github.com>2020-05-02 13:05:43 -0400
commitd4ae9aa501ece99ea6c5e6b8fb01c3005f5b7f03 (patch)
treef96211038ffac0be88da912cb40bd3980c212c18 /test
parentd581bffa15419b3b55773f1ed08a2e787e574f1f (diff)
DOMTextScanner (#458)
* Create new class for scanning text in a document * Update test styles * Add tests
Diffstat (limited to 'test')
-rw-r--r--test/data/html/test-dom-text-scanner.html393
-rw-r--r--test/data/html/test-stylesheet.css12
-rw-r--r--test/test-dom-text-scanner.js181
3 files changed, 583 insertions, 3 deletions
diff --git a/test/data/html/test-dom-text-scanner.html b/test/data/html/test-dom-text-scanner.html
new file mode 100644
index 00000000..6b78570a
--- /dev/null
+++ b/test/data/html/test-dom-text-scanner.html
@@ -0,0 +1,393 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <meta charset="UTF-8">
+ <meta name="viewport" content="width=device-width,initial-scale=1" />
+ <title>Yomichan DOMTextScanner Tests</title>
+ <link rel="icon" type="image/gif" href="data:image/gif;base64,R0lGODlhEAAQAKEBAAAAAP///////////yH5BAEKAAIALAAAAAAQABAAAAImFI6Zpt0B4YkS0TCpq07xbmEgcGVRUpLaI46ZG7ppalY0jDCwUAAAOw==" />
+ <link rel="stylesheet" href="test-stylesheet.css" />
+ </head>
+<body>
+
+ <h1>Yomichan DOMTextScanner Tests</h1>
+
+ <y-test
+ data-test-data='{
+ "node": "div:nth-of-type(1)",
+ "offset": 0,
+ "length": 15,
+ "expected": {
+ "node": "div:nth-of-type(2)>div::text",
+ "offset": 3,
+ "content": "小ぢん\nまり1\n小ぢん\nまり2"
+ }
+ }'
+ >
+ <y-description>Layout newlines expected due to entering and exiting display:block nodes.</y-description>
+<div><div>小ぢん</div>まり1</div>
+<div>小ぢん<div>まり2</div></div>
+ </y-test>
+
+ <y-test
+ data-test-data='{
+ "node": "div:nth-of-type(1)::text",
+ "offset": 0,
+ "length": 13,
+ "expected": {
+ "node": "div:nth-of-type(2)::text",
+ "offset": 6,
+ "content": "小ぢんまり1\n小ぢんまり2"
+ }
+ }'
+ >
+ <y-description>Layout newline expected due to sequential display:block elements.</y-description>
+<div>小ぢんまり1</div><div>小ぢんまり2</div>
+ </y-test>
+
+ <y-test
+ data-test-data='{
+ "node": "div:nth-of-type(1)::text",
+ "offset": 0,
+ "length": 13,
+ "expected": {
+ "node": "div:nth-of-type(2)::text",
+ "offset": 6,
+ "content": "小ぢんまり1\n小ぢんまり2"
+ }
+ }'
+ >
+ <y-description>Layout newline expected due to sequential display:block elements separated by a newline.</y-description>
+<div>小ぢんまり1</div>
+<div>小ぢんまり2</div>
+ </y-test>
+
+ <y-test
+ data-test-data='{
+ "node": "span:nth-of-type(1)::text",
+ "offset": 0,
+ "length": 12,
+ "expected": {
+ "node": "span:nth-of-type(2)::text",
+ "offset": 6,
+ "content": "小ぢんまり1小ぢんまり2"
+ }
+ }'
+ >
+ <y-description>No newlines expected due to display:inline.</y-description>
+<span>小ぢんまり1</span><span>小ぢんまり2</span>
+ </y-test>
+
+ <y-test
+ data-test-data='{
+ "node": "span:nth-of-type(1)::text",
+ "offset": 0,
+ "length": 13,
+ "expected": {
+ "node": "span:nth-of-type(2)::text",
+ "offset": 6,
+ "content": "小ぢんまり1\n小ぢんまり2"
+ }
+ }'
+ >
+ <y-description>No newlines expected due to white-space:normal.</y-description>
+<span>小ぢんまり1</span>
+<span>小ぢんまり2</span>
+ </y-test>
+
+ <y-test
+ data-test-data='{
+ "node": "span:nth-of-type(1)::text",
+ "offset": 0,
+ "length": 13,
+ "expected": {
+ "node": "span:nth-of-type(2)::text",
+ "offset": 6,
+ "content": "小ぢんまり1\n小ぢんまり2"
+ }
+ }'
+ >
+ <y-description>Newline expected due to white-space:pre.</y-description>
+<pre>
+<span>小ぢんまり1</span>
+<span>小ぢんまり2</span>
+</pre>
+ </y-test>
+
+ <y-test
+ data-test-data='{
+ "node": "span:nth-of-type(1)::text",
+ "offset": 0,
+ "length": 12,
+ "expected": {
+ "node": "span:nth-of-type(2)::text",
+ "offset": 6,
+ "content": "小ぢんまり1小ぢんまり2"
+ }
+ }'
+ >
+ <y-description>No newlines expected due to display:inline-block. Actual layout flow cannot be determined by DOM/CSS alone.</y-description>
+<span style="display: inline-block;">小ぢんまり1</span><span style="display: inline-block;">小ぢんまり2</span>
+ </y-test>
+
+ <y-test
+ style="position: relative;"
+ data-test-data='{
+ "node": "div:nth-of-type(1)::text",
+ "offset": 0,
+ "length": 13,
+ "expected": {
+ "node": "div:nth-of-type(2)::text",
+ "offset": 6,
+ "content": "小ぢんまり1\n小ぢんまり2"
+ }
+ }'
+ >
+ <y-description>Single newline expected due to display:block layout.</y-description>
+<div>小ぢんまり1</div><div style="position: relative;">小ぢんまり2</div>
+ </y-test>
+
+ <y-test
+ style="position: relative; overflow: hidden;"
+ data-test-data='{
+ "node": "div:nth-of-type(1)::text",
+ "offset": 0,
+ "length": 14,
+ "expected": {
+ "node": "div:nth-of-type(2)::text",
+ "offset": 6,
+ "content": "小ぢんまり1\n\n小ぢんまり2"
+ }
+ }'
+ >
+ <y-description>Two newlines expected due to position:absolute causing a significant layout change.</y-description>
+<div>小ぢんまり1</div><div style="position: absolute;">小ぢんまり2</div>
+ </y-test>
+
+ <y-test
+ style="position: relative; overflow: hidden;"
+ data-test-data='{
+ "node": "div:nth-of-type(1)::text",
+ "offset": 0,
+ "length": 14,
+ "expected": {
+ "node": "div:nth-of-type(2)::text",
+ "offset": 6,
+ "content": "小ぢんまり1\n\n小ぢんまり2"
+ }
+ }'
+ >
+ <y-description>Two newlines expected due to position:fixed causing a significant layout change.</y-description>
+<div>小ぢんまり1</div><div style="position: fixed;">小ぢんまり2</div>
+ </y-test>
+
+ <y-test
+ style="position: relative;"
+ data-test-data='{
+ "node": "div:nth-of-type(1)::text",
+ "offset": 0,
+ "length": 14,
+ "expected": {
+ "node": "div:nth-of-type(2)::text",
+ "offset": 6,
+ "content": "小ぢんまり1\n\n小ぢんまり2"
+ }
+ }'
+ >
+ <y-description>Two newlines expected due to position:sticky being able to cause a significant layout change.</y-description>
+<div>小ぢんまり1</div><div style="position: sticky;">小ぢんまり2</div>
+ </y-test>
+
+ <y-test
+ data-test-data='{
+ "node": "rt",
+ "offset": 0,
+ "length": 6,
+ "expected": {
+ "node": "div::text",
+ "offset": 5,
+ "content": "小ぢんまり1"
+ }
+ }'
+ >
+ <y-description>Scanning text starting in an &lt;rt&gt; element. Should start scanning at the start of the &lt;ruby&gt; tag instead.</y-description>
+<div><ruby>小<rp>(</rp><rt>こ</rt><rp>)</rp></ruby>ぢんまり1</div>
+ </y-test>
+
+ <y-test
+ data-test-data='{
+ "node": "div",
+ "offset": 0,
+ "length": 6,
+ "expected": {
+ "node": "div::nth-text(2)",
+ "offset": 3,
+ "content": "小ぢんまり1"
+ }
+ }'
+ >
+ <y-description>Skip &lt;script&gt; content.</y-description>
+<div>小ぢん<script>/*comment*/</script>まり1</div>
+ </y-test>
+
+ <y-test
+ data-test-data='{
+ "node": "div",
+ "offset": 0,
+ "length": 6,
+ "expected": {
+ "node": "div::nth-text(2)",
+ "offset": 3,
+ "content": "小ぢんまり1"
+ }
+ }'
+ >
+ <y-description>Skip &lt;style&gt; content.</y-description>
+<div>小ぢん<style>/*comment*/</style>まり1</div>
+ </y-test>
+
+ <y-test
+ data-test-data='{
+ "node": "div",
+ "offset": 0,
+ "length": 6,
+ "expected": {
+ "node": "div::nth-text(2)",
+ "offset": 3,
+ "content": "小ぢんまり1"
+ }
+ }'
+ >
+ <y-description>Skip &lt;textarea&gt; content.</y-description>
+<div>小ぢん<textarea>textarea content</textarea>まり1</div>
+ </y-test>
+
+ <y-test
+ data-test-data='{
+ "node": "div",
+ "offset": 0,
+ "length": 6,
+ "expected": {
+ "node": "div::nth-text(2)",
+ "offset": 3,
+ "content": "小ぢんまり1"
+ }
+ }'
+ >
+ <y-description>Skip &lt;input&gt; content.</y-description>
+<div>小ぢん<input value="content" />まり1</div>
+ </y-test>
+
+ <y-test
+ data-test-data='{
+ "node": "div",
+ "offset": 0,
+ "length": 6,
+ "expected": {
+ "node": "div::nth-text(2)",
+ "offset": 3,
+ "content": "小ぢんまり1"
+ }
+ }'
+ >
+ <y-description>Skip &lt;button&gt; content.</y-description>
+<div>小ぢん<button>content</button>まり1</div>
+ </y-test>
+
+ <y-test
+ data-test-data='{
+ "node": "div",
+ "offset": 0,
+ "length": 6,
+ "expected": {
+ "node": "div::nth-text(2)",
+ "offset": 3,
+ "content": "小ぢんまり1"
+ }
+ }'
+ >
+ <y-description>Skip content with font-size:0.</y-description>
+<div>小ぢん<span style="font-size: 0;">content</span>まり1</div>
+ </y-test>
+
+ <y-test
+ data-test-data='{
+ "node": "div",
+ "offset": 0,
+ "length": 6,
+ "expected": {
+ "node": "div::nth-text(2)",
+ "offset": 3,
+ "content": "小ぢんまり1"
+ }
+ }'
+ >
+ <y-description>Skip content with opacity:0.</y-description>
+<div>小ぢん<span style="opacity: 0;">content</span>まり1</div>
+ </y-test>
+
+ <y-test
+ data-test-data='{
+ "node": "div",
+ "offset": 0,
+ "length": 6,
+ "expected": {
+ "node": "div::nth-text(2)",
+ "offset": 3,
+ "content": "小ぢんまり1"
+ }
+ }'
+ >
+ <y-description>Skip content with visibility:hidden.</y-description>
+<div>小ぢん<span style="visibility: hidden;">content</span>まり1</div>
+ </y-test>
+
+ <y-test
+ data-test-data='{
+ "node": "div",
+ "offset": 0,
+ "length": 6,
+ "expected": {
+ "node": "div::nth-text(2)",
+ "offset": 3,
+ "content": "小ぢんまり1"
+ }
+ }'
+ >
+ <y-description>Skip content with display:none.</y-description>
+<div>小ぢん<span style="display: none;">content</span>まり1</div>
+ </y-test>
+
+ <y-test
+ data-test-data='{
+ "node": "div",
+ "offset": 0,
+ "length": 6,
+ "expected": {
+ "node": "div::nth-text(2)",
+ "offset": 3,
+ "content": "小ぢんまり1"
+ }
+ }'
+ >
+ <y-description>Don't skip content with user-select:none.</y-description>
+<div>小ぢ<span style="user-select: none;">ん</span>まり1</div>
+ </y-test>
+
+ <y-test
+ data-test-data='{
+ "node": "div",
+ "offset": 0,
+ "length": 6,
+ "expected": {
+ "node": "div::nth-text(2)",
+ "offset": 3,
+ "content": "小ぢんまり1"
+ }
+ }'
+ >
+ <y-description>Skip content with user-select:none <em>and</em> a transparent color.</y-description>
+<div>小ぢん<span style="user-select: none; color: rgba(0, 0, 0, 0);">content</span>まり1</div>
+ </y-test>
+
+</body>
+</html> \ No newline at end of file
diff --git a/test/data/html/test-stylesheet.css b/test/data/html/test-stylesheet.css
index f63d2481..2e9a2f52 100644
--- a/test/data/html/test-stylesheet.css
+++ b/test/data/html/test-stylesheet.css
@@ -28,7 +28,9 @@ a, a:visited {
text-decoration: underline;
}
-.test {
+.test,
+y-test {
+ display: block;
background-color: #ffffff;
margin: 1em 0;
padding: 0.5em;
@@ -36,7 +38,8 @@ a, a:visited {
border-radius: 4px;
}
-.test:before {
+.test:before,
+y-test:before {
content: "Test " counter(test-id);
display: block;
counter-increment: test-id;
@@ -45,7 +48,10 @@ a, a:visited {
font-weight: bold;
}
-.description {
+.description,
+y-description {
color: #444444;
font-style: italic;
+ display: block;
+ padding-bottom: 0.5em;
}
diff --git a/test/test-dom-text-scanner.js b/test/test-dom-text-scanner.js
new file mode 100644
index 00000000..41d6e307
--- /dev/null
+++ b/test/test-dom-text-scanner.js
@@ -0,0 +1,181 @@
+/*
+ * Copyright (C) 2020 Yomichan 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 <https://www.gnu.org/licenses/>.
+ */
+
+const fs = require('fs');
+const path = require('path');
+const assert = require('assert');
+const {JSDOM} = require('jsdom');
+const {VM} = require('./yomichan-vm');
+
+
+function createJSDOM(fileName) {
+ const domSource = fs.readFileSync(fileName, {encoding: 'utf8'});
+ return new JSDOM(domSource);
+}
+
+function querySelectorTextNode(element, selector) {
+ let textIndex = -1;
+ const match = /::text$|::nth-text\((\d+)\)$/.exec(selector);
+ if (match !== null) {
+ textIndex = (match[1] ? parseInt(match[1], 10) - 1 : 0);
+ selector = selector.substring(0, selector.length - match[0].length);
+ }
+ const result = element.querySelector(selector);
+ if (textIndex < 0) {
+ return result;
+ }
+ for (let n = result.firstChild; n !== null; n = n.nextSibling) {
+ if (n.nodeType === n.constructor.TEXT_NODE) {
+ if (textIndex === 0) {
+ return n;
+ }
+ --textIndex;
+ }
+ }
+ return null;
+}
+
+
+function getComputedFontSizeInPixels(window, getComputedStyle, element) {
+ for (; element !== null; element = element.parentNode) {
+ if (element.nodeType === window.Node.ELEMENT_NODE) {
+ const fontSize = getComputedStyle(element).fontSize;
+ if (fontSize.endsWith('px')) {
+ const value = parseFloat(fontSize.substring(0, fontSize.length - 2));
+ return value;
+ }
+ }
+ }
+ const defaultFontSize = 14;
+ return defaultFontSize;
+}
+
+function createAbsoluteGetComputedStyle(window) {
+ // Wrapper to convert em units to px units
+ const getComputedStyleOld = window.getComputedStyle.bind(window);
+ return (element, ...args) => {
+ const style = getComputedStyleOld(element, ...args);
+ return new Proxy(style, {
+ get: (target, property) => {
+ let result = target[property];
+ if (typeof result === 'string') {
+ result = result.replace(/([-+]?\d(?:\.\d)?(?:[eE][-+]?\d+)?)em/g, (g0, g1) => {
+ const fontSize = getComputedFontSizeInPixels(window, getComputedStyleOld, element);
+ return `${parseFloat(g1) * fontSize}px`;
+ });
+ }
+ return result;
+ }
+ });
+ };
+}
+
+
+async function testDomTextScanner(dom, {DOMTextScanner}) {
+ const document = dom.window.document;
+ for (const testElement of document.querySelectorAll('y-test')) {
+ let testData = JSON.parse(testElement.dataset.testData);
+ if (!Array.isArray(testData)) {
+ testData = [testData];
+ }
+ for (const testDataItem of testData) {
+ let {
+ node,
+ offset,
+ length,
+ forcePreserveWhitespace,
+ generateLayoutContent,
+ reversible,
+ expected: {
+ node: expectedNode,
+ offset: expectedOffset,
+ content: expectedContent
+ }
+ } = testDataItem;
+
+ node = querySelectorTextNode(testElement, node);
+ expectedNode = querySelectorTextNode(testElement, expectedNode);
+
+ // Standard test
+ {
+ const scanner = new DOMTextScanner(node, offset, forcePreserveWhitespace, generateLayoutContent);
+ scanner.seek(length);
+
+ const {node: actualNode1, offset: actualOffset1, content: actualContent1} = scanner;
+ assert.strictEqual(actualContent1, expectedContent);
+ assert.strictEqual(actualOffset1, expectedOffset);
+ assert.strictEqual(actualNode1, expectedNode);
+ }
+
+ // Substring tests
+ for (let i = 1; i <= length; ++i) {
+ const scanner = new DOMTextScanner(node, offset, forcePreserveWhitespace, generateLayoutContent);
+ scanner.seek(length - i);
+
+ const {content: actualContent} = scanner;
+ assert.strictEqual(actualContent, expectedContent.substring(0, expectedContent.length - i));
+ }
+
+ if (reversible === false) { continue; }
+
+ // Reversed test
+ {
+ const scanner = new DOMTextScanner(expectedNode, expectedOffset, forcePreserveWhitespace, generateLayoutContent);
+ scanner.seek(-length);
+
+ const {content: actualContent} = scanner;
+ assert.strictEqual(actualContent, expectedContent);
+ }
+
+ // Reversed substring tests
+ for (let i = 1; i <= length; ++i) {
+ const scanner = new DOMTextScanner(expectedNode, expectedOffset, forcePreserveWhitespace, generateLayoutContent);
+ scanner.seek(-(length - i));
+
+ const {content: actualContent} = scanner;
+ assert.strictEqual(actualContent, expectedContent.substring(i));
+ }
+ }
+ }
+}
+
+
+async function testDocument1() {
+ const dom = createJSDOM(path.join(__dirname, 'data', 'html', 'test-dom-text-scanner.html'));
+ const window = dom.window;
+ try {
+ const {document, Node, Range} = window;
+
+ window.getComputedStyle = createAbsoluteGetComputedStyle(window);
+
+ const vm = new VM({document, window, Range, Node});
+ vm.execute('fg/js/dom-text-scanner.js');
+ const DOMTextScanner = vm.get('DOMTextScanner');
+
+ await testDomTextScanner(dom, {DOMTextScanner});
+ } finally {
+ window.close();
+ }
+}
+
+
+async function main() {
+ await testDocument1();
+}
+
+
+if (require.main === module) { main(); }