Skip to content

Commit

Permalink
do not use innerHTML in browser test runner
Browse files Browse the repository at this point in the history
  • Loading branch information
nightwing committed Jan 14, 2024
1 parent d4a340c commit ba40343
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 42 deletions.
6 changes: 4 additions & 2 deletions src/keyboard/vim.js
Original file line number Diff line number Diff line change
Expand Up @@ -1818,6 +1818,8 @@ domLib.importCssString(`.normal-mode .ace_cursor{
return function() { return true; };
} else {
return function() {
if ((command.operator || command.isEdit) && cm.getOption('readOnly'))
return; // ace_patch
return cm.operation(function() {
cm.curOp.isVimOp = true;
try {
Expand Down Expand Up @@ -7034,7 +7036,7 @@ domLib.importCssString(`.normal-mode .ace_cursor{
handleExternalSelection(cm, vim);
}
}
function handleExternalSelection(cm, vim) {
function handleExternalSelection(cm, vim, keepHPos) {
var anchor = cm.getCursor('anchor');
var head = cm.getCursor('head');
// Enter or exit visual mode to match mouse selection.
Expand All @@ -7058,7 +7060,7 @@ domLib.importCssString(`.normal-mode .ace_cursor{
};
updateMark(cm, vim, '<', cursorMin(head, anchor));
updateMark(cm, vim, '>', cursorMax(head, anchor));
} else if (!vim.insertMode) {
} else if (!vim.insertMode && !keepHPos) {
// Reset lastHPos if selection was modified by something outside of vim mode e.g. by mouse.
vim.lastHPos = cm.getCursor().ch;
}
Expand Down
91 changes: 51 additions & 40 deletions src/test/all_browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@ require("ace/lib/fixoldbrowsers");
var mockdom = require("../test/mockdom");
var AsyncTest = require("asyncjs").test;
var async = require("asyncjs");
var buildDom = require("../lib/dom").buildDom;
var escapeRegExp = require("ace/lib/lang").escapeRegExp;

var useMockdom = location.search.indexOf("mock=1") != -1;
var forceShow = location.search.indexOf("show=1") != -1;

var passed = 0;
var failed = 0;
var log = document.getElementById("log");
var el = document.createElement.bind(document);

// change buildDom to use real document in mockdom
var createElement = document.createElement.bind(document);
var createTextNode = document.createTextNode.bind(document);
var buildDom = eval("(" + buildDom.toString().replace(/document\./g, "") + ")");

var testNames = [
"ace/ace_test",
Expand Down Expand Up @@ -81,21 +88,32 @@ var testNames = [
];

var html = [
"<a href='?mock=1'>use mockdom</a><br>",
"<a href='?runall'>Run all tests</a><br>"
useMockdom
? ["a", {href: normalizeHref(location.search.replace('mock=1', '')) + location.hash}, "do not use mockdom"]
: ["a", {href: normalizeHref(location.search + '&mock=1') + location.hash}, "use mockdom"],
["br"],
forceShow
? ["a", {href: normalizeHref(location.search.replace('show=1', '')) + location.hash}, "hide mock renderer"]
: ["a", {href: normalizeHref(location.search + '&show=1') + location.hash}, "show mock renderer"],
["br"],
["a", {href: '?runall' + (useMockdom ? "&mock=1" : "")}, "Run all tests"], ["br"],
["hr"]
];
for (var i in testNames) {
var href = testNames[i];
html.push("<a href='?", href, (useMockdom ? "&mock=1" : ""), "'>", href.replace(/^ace\//, "") ,"</a><br>");
for (var i in testNames) {
html.push(testLink(testNames[i]), ["br"]);
}

var nav = el("div");
nav.innerHTML = html.join("");
nav.style.cssText = "position:absolute;right:0;top:0";
document.body.appendChild(nav);
function testLink(name) {
return ["a", {href:'?' + name + (useMockdom ? "&mock=1" : "")}, name.replace(/^ace\//, "")];
}
function normalizeHref(str) {
return str.replace(/([?&])&+/g, "$1");
}

var nav = buildDom(["div", {style: "position:absolute;right:0;top:0"}, html], document.body);

if (location.search.indexOf("show=1") != -1) {

if (forceShow) {
require(["ace/virtual_renderer", "ace/test/mockrenderer"], function(real, mock) {
var VirtualRenderer = real.VirtualRenderer;
mock.MockRenderer = function() {
Expand Down Expand Up @@ -148,34 +166,32 @@ require(selectedTests, function() {
.each(function(test, next) {
if (test.index == 1 && test.context.href) {
var href = test.context.href;
var node = el("div");
node.innerHTML = "<a href='?" + href + "'>" + href.replace(/^ace\//, "") + "</a>";
log.appendChild(node);
buildDom(["div", {}, testLink(href)], log);
}
var node = el("div");
node.className = test.passed ? "passed" : "failed";

var name = test.name;
if (test.suiteName)
name = test.suiteName + ": " + test.name;

var msg = "[" + test.count + "/" + test.index + "] " + name + " " + (test.passed ? "OK" : "FAIL");

var messageHeader = "[" + test.index + "/" + test.count + "]";

var node = buildDom(["div", {class: test.passed ? "passed" : "failed"},
["a", {href: "#" + escapeRegExp(test.name.replace(/^test\s*/, ""))}, messageHeader],
" ",
(test.suiteName ? test.suiteName + ": " : ""),
test.name,
(test.passed ? " OK" : " FAIL")
], log);

if (!test.passed) {
if (test.err.stack)
var err = test.err.stack;
else
var err = test.err;

console.error(msg);
console.error(node.textContent);
console.error(err);
msg += "<pre class='error'>" + err + "</pre>";
buildDom(["pre", {class: "error"}, err + ""], node);
} else {
console.log(msg);
console.log(node.textContent);
}

node.innerHTML = msg;
log.appendChild(node);

next();
})
.each(function(test) {
Expand All @@ -185,17 +201,12 @@ require(selectedTests, function() {
failed += 1;
})
.end(function() {
log.innerHTML += [
"<div class='summary'>",
"<br>",
"Summary: <br>",
"<br>",
"Total number of tests: " + (passed + failed) + "<br>",
(passed ? "Passed tests: " + passed + "<br>" : ""),
(failed ? "Failed tests: " + failed + "<br>" : "")
].join("");
console.log("Total number of tests: " + (passed + failed));
console.log("Passed tests: " + passed);
console.log("Failed tests: " + failed);
var node = buildDom(["div", {class: "summary"},
["br"], "Summary:", ["br"], ["br"],
"Total number of tests: " + (passed + failed), ["br"],
(passed && [null, "Passed tests: " + passed, ["br"]]),
(failed && [null, "Failed tests: " + failed])
], log);
console.log(node.innerText);
});
});

0 comments on commit ba40343

Please sign in to comment.