mirror of
https://github.com/webodf/WebODF.git
synced 2026-11-30 16:47:58 +01:00
354 lines
11 KiB
JavaScript
354 lines
11 KiB
JavaScript
/**
|
|
* Copyright (C) 2013 KO GmbH <jos.van.den.oever@kogmbh.com>
|
|
*
|
|
* @licstart
|
|
* This file is part of WebODF.
|
|
*
|
|
* WebODF is free software: you can redistribute it and/or modify it
|
|
* under the terms of the GNU Affero General Public License (GNU AGPL)
|
|
* as published by the Free Software Foundation, either version 3 of
|
|
* the License, or (at your option) any later version.
|
|
*
|
|
* WebODF 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 Affero General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
* along with WebODF. If not, see <http://www.gnu.org/licenses/>.
|
|
* @licend
|
|
*
|
|
* @source: http://www.webodf.org/
|
|
* @source: https://github.com/kogmbh/WebODF/
|
|
*/
|
|
/*global runtime, xmled, document, xmldom, NodeFilter*/
|
|
runtime.loadClass("xmled.ValidationModel");
|
|
|
|
/**
|
|
* @constructor
|
|
* @param {core.UnitTestRunner} runner
|
|
* @implements {core.UnitTest}
|
|
*/
|
|
xmled.ValidationModelTests = function ValidationModelTests(runner) {
|
|
"use strict";
|
|
/**
|
|
* @constructor
|
|
* @extends NodeFilter
|
|
*/
|
|
function TestFilter(ns) {
|
|
this.acceptNode = function (node) {
|
|
if (node.namespaceURI === ns) {
|
|
return NodeFilter.FILTER_REJECT;
|
|
}
|
|
return NodeFilter.FILTER_ACCEPT;
|
|
};
|
|
}
|
|
var r = runner,
|
|
t,
|
|
replacements,
|
|
testns = "http://test.com/",
|
|
filter = new TestFilter(testns);
|
|
|
|
/**
|
|
* @param {?NodeFilter} filter
|
|
* @param {?Node} node
|
|
* @return {!boolean}
|
|
*/
|
|
function accept(filter, node) {
|
|
return node !== null
|
|
&& (!filter
|
|
|| filter.acceptNode(node) === NodeFilter.FILTER_ACCEPT);
|
|
}
|
|
|
|
function serialize(element) {
|
|
var serializer = new xmldom.LSSerializer();
|
|
return serializer.writeToString(element, {});
|
|
}
|
|
|
|
function loadReplacements(callback) {
|
|
if (replacements) {
|
|
return callback();
|
|
}
|
|
runtime.loadXML("xmled/replacements.xml", function (err, dom) {
|
|
t.err = err;
|
|
t.dom = dom;
|
|
r.shouldBeNull(t, "t.err");
|
|
r.shouldBeNonNull(t, "t.dom");
|
|
replacements = {};
|
|
var f, e;
|
|
e = dom.documentElement.firstElementChild;
|
|
while (e) {
|
|
f = dom.createDocumentFragment();
|
|
while (e.firstChild) {
|
|
f.appendChild(e.firstChild);
|
|
}
|
|
replacements[e.getAttribute("id")] = f;
|
|
e = e.nextElementSibling;
|
|
}
|
|
callback();
|
|
});
|
|
}
|
|
|
|
function checkReplacement(rep, ref) {
|
|
if (!r.areNodesEqual(rep, ref)) {
|
|
t.rep = serialize(rep);
|
|
t.ref = serialize(ref);
|
|
} else {
|
|
t.rep = t.ref = "OK";
|
|
}
|
|
r.shouldBe(t, "t.rep", "t.ref");
|
|
}
|
|
|
|
function checkReplacements(reps, ids) {
|
|
t.reps = reps;
|
|
r.shouldBe(t, "t.reps.length", String(ids.length));
|
|
var i, length = Math.min(ids.length, reps.length);
|
|
for (i = 0; i < length; i += 1) {
|
|
checkReplacement(reps[i].dom, replacements[ids[i]]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param {!Element} element
|
|
* @param {!string} ns
|
|
* @param {!string} localName
|
|
* @return {undefined}
|
|
*/
|
|
function addNodes(element, ns, localName) {
|
|
var doc = element.ownerDocument,
|
|
ne = doc.createElementNS(ns, localName),
|
|
e = element.firstElementChild;
|
|
while (e) {
|
|
element.insertBefore(ne, e);
|
|
ne = doc.createElementNS(ns, localName);
|
|
addNodes(e, ns, localName);
|
|
e = e.nextElementSibling;
|
|
}
|
|
element.appendChild(ne);
|
|
}
|
|
|
|
/**
|
|
* @param {!xmled.ValidationModel} model
|
|
* @param {!Element} documentElement
|
|
* @return {undefined}
|
|
*/
|
|
function validateWithIgnoredNodes(model, documentElement) {
|
|
addNodes(documentElement, testns, "test");
|
|
try {
|
|
t.err = model.validate(documentElement, filter);
|
|
} catch (e) {
|
|
t.err = e;
|
|
}
|
|
r.shouldBeNull(t, "t.err");
|
|
}
|
|
|
|
/**
|
|
* @param {?Node} n
|
|
* @return {?Element}
|
|
*/
|
|
function firstElementChild(n) {
|
|
n = n && n.firstChild;
|
|
var e = null;
|
|
while (n && !e) {
|
|
if (n.nodeType === 1) {
|
|
e = /**@type{!Element}*/(n);
|
|
}
|
|
n = n.nextSibling;
|
|
}
|
|
return e;
|
|
}
|
|
|
|
/**
|
|
* @param {!string} xsdfile
|
|
* @param {!string} xmlfile
|
|
* @return {!core.AsyncTestData}
|
|
*/
|
|
function testValidation(xsdfile, xmlfile) {
|
|
var f = function (callback) {
|
|
var model = new xmled.ValidationModel(xsdfile, function (err) {
|
|
t.err = err;
|
|
r.shouldBeNull(t, "t.err");
|
|
if (err) {
|
|
return callback();
|
|
}
|
|
runtime.loadXML(xmlfile, function (err, dom) {
|
|
t.err = err;
|
|
r.shouldBeNull(t, "t.err");
|
|
if (err) {
|
|
return callback();
|
|
}
|
|
var e = firstElementChild(dom || null);
|
|
t.documentElement = e;
|
|
r.shouldBeNonNull(t, "t.documentElement");
|
|
if (e) {
|
|
t.err = model.validate(e);
|
|
r.shouldBeNull(t, "t.err");
|
|
validateWithIgnoredNodes(model, e);
|
|
}
|
|
callback();
|
|
});
|
|
});
|
|
};
|
|
f.functionName = "validation-" + xsdfile + "-" + xmlfile;
|
|
return {f: f, name: xsdfile, expectFail: false};
|
|
}
|
|
|
|
/**
|
|
* @param {!string} xsd
|
|
* @param {!Array.<!string>} replacementIds
|
|
* @return {!core.AsyncTestData}
|
|
*/
|
|
function testRoot(xsd, replacementIds) {
|
|
var f = function (callback) {
|
|
var model = new xmled.ValidationModel(xsd, function (e) {
|
|
t.err = e;
|
|
r.shouldBeNull(t, "t.err");
|
|
if (e) {
|
|
return callback();
|
|
}
|
|
t.reps = model.getPossibleReplacements(document.documentElement);
|
|
loadReplacements(function () {
|
|
checkReplacements(t.reps, replacementIds);
|
|
callback();
|
|
});
|
|
});
|
|
};
|
|
f.functionName = "init-" + xsd;
|
|
return {f: f, name: xsd, expectFail: false};
|
|
}
|
|
|
|
/**
|
|
* @param {!Node} node
|
|
* @param {!Array.<!number>} list
|
|
* @param {?NodeFilter=} filter
|
|
* @return {{node:!Node, offset: !number }}
|
|
*/
|
|
function getPosition(node, list, filter) {
|
|
var i, j, l = list.length - 1, p, n = node, m;
|
|
filter = filter || null;
|
|
runtime.assert(l >= 0, "Position array must have at least one entry.");
|
|
for (i = 0; i < l; i += 1) {
|
|
n = n.firstChild;
|
|
while (!accept(filter, n)) {
|
|
n = n.nextSibling;
|
|
}
|
|
p = list[i];
|
|
for (j = 0; j < p; j += 1) {
|
|
do {
|
|
n = n.nextSibling;
|
|
} while (!accept(filter, n));
|
|
}
|
|
}
|
|
p = list[l];
|
|
if (filter) {
|
|
// adapt offset to take into account the junk elements
|
|
m = n.firstChild;
|
|
while (!accept(filter, m)) {
|
|
m = m.nextSibling;
|
|
}
|
|
for (j = 0; j < p; j += 1) {
|
|
do {
|
|
m = m.nextSibling;
|
|
} while (m && !accept(filter, m));
|
|
}
|
|
j = n.firstChild;
|
|
p = 0;
|
|
while (j !== m) {
|
|
p += 1;
|
|
j = j.nextSibling;
|
|
}
|
|
}
|
|
return {node: /**@type{!Node}*/(n), offset: p};
|
|
}
|
|
|
|
/**
|
|
* @param {!Node} node
|
|
* @param {!Array.<!number>} start
|
|
* @param {!Array.<!number>} end
|
|
* @param {!NodeFilter=} filter
|
|
* @return {!Range}
|
|
*/
|
|
function createRange(node, start, end, filter) {
|
|
var range = node.ownerDocument.createRange(),
|
|
pos = getPosition(node, start, filter);
|
|
range.setStart(pos.node, pos.offset);
|
|
pos = getPosition(node, end, filter);
|
|
range.setEnd(pos.node, pos.offset);
|
|
return range;
|
|
}
|
|
|
|
/**
|
|
* @param {!string} xsd
|
|
* @param {!string} initId
|
|
* @param {!Array.<!number>} start
|
|
* @param {!Array.<!number>} end
|
|
* @param {!Array.<!number>} replacementIds
|
|
* @return {!core.AsyncTestData}
|
|
*/
|
|
function testReplace(xsd, initId, start, end, replacementIds) {
|
|
var f = function (callback) {
|
|
var model = new xmled.ValidationModel(xsd, function (e) {
|
|
t.err = e;
|
|
r.shouldBeNull(t, "t.err");
|
|
if (e) {
|
|
return callback();
|
|
}
|
|
loadReplacements(function () {
|
|
var initial = replacements[initId],
|
|
range,
|
|
de = initial.ownerDocument.createElement("doc");
|
|
initial = initial.cloneNode(true);
|
|
de.appendChild(initial);
|
|
initial = de.firstElementChild;
|
|
range = createRange(de, start, end);
|
|
t.reps = model.getPossibleReplacements(initial, range);
|
|
checkReplacements(t.reps, replacementIds);
|
|
addNodes(de, testns, "test");
|
|
range = createRange(de, start, end, filter);
|
|
t.reps = model.getPossibleReplacements(initial, range, filter);
|
|
checkReplacements(t.reps, replacementIds);
|
|
callback();
|
|
});
|
|
});
|
|
};
|
|
f.functionName = "replace-" + xsd + "-" + initId;
|
|
return {f: f, name: xsd, expectFail: false};
|
|
}
|
|
|
|
this.setUp = function () {
|
|
t = {};
|
|
};
|
|
this.tearDown = function () {
|
|
t = {};
|
|
};
|
|
this.tests = function () {
|
|
return [];
|
|
};
|
|
this.asyncTests = function () {
|
|
return [
|
|
testRoot("xmled/empty.xsd", []),
|
|
testRoot("xmled/simple.xsd", ["a", "b", "c"]),
|
|
testRoot("xmled/complex01.xsd", ["a", "d", "e", "f", "ga", "ha", "j", "k"]),
|
|
// replace
|
|
testReplace("xmled/simple.xsd", "a", [0], [1], ["b", "c"]),
|
|
testReplace("xmled/complex01.xsd", "d", [0, 0], [0, 1], []),
|
|
testReplace("xmled/complex01.xsd", "g", [0, 0], [0, 1], ["a", "c"]),
|
|
testReplace("xmled/complex01.xsd", "gb", [0, 0], [0, 1], ["a", "c"]),
|
|
testReplace("xmled/complex01.xsd", "gbb", [0, 0], [0, 1], ["empty"]),
|
|
testReplace("xmled/complex01.xsd", "gbb", [0, 1], [0, 2], ["empty"]),
|
|
testReplace("xmled/complex01.xsd", "h", [0, 1, 0], [0, 1, 1], ["b", "c"]),
|
|
// prepend
|
|
testReplace("xmled/complex01.xsd", "d", [0, 0], [0, 0], ["a"]),
|
|
testValidation("xmled/XMLSchema.xsd", "xmled/XMLSchema.xsd")
|
|
];
|
|
};
|
|
};
|
|
xmled.ValidationModelTests.prototype.description = function () {
|
|
"use strict";
|
|
return "Test the ValidationModel class.";
|
|
};
|
|
(function () {
|
|
"use strict";
|
|
return xmled.ValidationModelTests;
|
|
}());
|