2021-05-09 17:29:45 +00:00
|
|
|
|
import * as utilities from '../../../instrumented/svgcanvas/utilities.js';
|
2021-09-21 19:30:44 +00:00
|
|
|
|
import { NS } from '../../../instrumented/svgcanvas/namespaces.js';
|
2019-11-24 03:29:24 +00:00
|
|
|
|
|
|
|
|
|
describe('utilities', function () {
|
|
|
|
|
/**
|
|
|
|
|
* Create an element for test.
|
|
|
|
|
* @param {module:utilities.SVGElementJSON} jsonMap
|
|
|
|
|
* @returns {SVGElement}
|
|
|
|
|
*/
|
|
|
|
|
function mockCreateSVGElement (jsonMap) {
|
|
|
|
|
const elem = document.createElementNS(NS.SVG, jsonMap.element);
|
2021-05-12 23:00:09 +00:00
|
|
|
|
Object.entries(jsonMap.attr).forEach(([ attr, value ]) => {
|
2019-11-24 03:29:24 +00:00
|
|
|
|
elem.setAttribute(attr, value);
|
|
|
|
|
});
|
|
|
|
|
return elem;
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* Adds SVG Element per parameters and appends to root.
|
|
|
|
|
* @param {module:utilities.SVGElementJSON} json
|
|
|
|
|
* @returns {SVGElement}
|
|
|
|
|
*/
|
2021-12-25 20:25:12 +00:00
|
|
|
|
function mockaddSVGElemensFromJson (json) {
|
2019-11-24 03:29:24 +00:00
|
|
|
|
const elem = mockCreateSVGElement(json);
|
|
|
|
|
svgroot.append(elem);
|
|
|
|
|
return elem;
|
|
|
|
|
}
|
2021-05-12 23:00:09 +00:00
|
|
|
|
const mockPathActions = { resetOrientation () { /* empty fn */ } };
|
2019-11-24 03:29:24 +00:00
|
|
|
|
let mockHistorySubCommands = [];
|
|
|
|
|
const mockHistory = {
|
|
|
|
|
BatchCommand: class {
|
|
|
|
|
addSubCommand (cmd) {
|
|
|
|
|
mockHistorySubCommands.push(cmd);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
RemoveElementCommand: class {
|
|
|
|
|
// Longhand needed since used as a constructor
|
|
|
|
|
constructor (elem, nextSibling, parent) {
|
|
|
|
|
this.elem = elem;
|
|
|
|
|
this.nextSibling = nextSibling;
|
|
|
|
|
this.parent = parent;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
InsertElementCommand: class {
|
|
|
|
|
constructor (path) { // Longhand needed since used as a constructor
|
|
|
|
|
this.path = path;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
const mockCount = {
|
|
|
|
|
clearSelection: 0,
|
|
|
|
|
addToSelection: 0,
|
|
|
|
|
addCommandToHistory: 0
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Increments clear seleciton count for mock test.
|
|
|
|
|
* @returns {void}
|
|
|
|
|
*/
|
|
|
|
|
function mockClearSelection () {
|
|
|
|
|
mockCount.clearSelection++;
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* Increments add selection count for mock test.
|
|
|
|
|
* @returns {void}
|
|
|
|
|
*/
|
|
|
|
|
function mockAddToSelection () {
|
|
|
|
|
mockCount.addToSelection++;
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* Increments add command to history count for mock test.
|
|
|
|
|
* @returns {void}
|
|
|
|
|
*/
|
|
|
|
|
function mockAddCommandToHistory () {
|
|
|
|
|
mockCount.addCommandToHistory++;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-28 15:03:29 +00:00
|
|
|
|
let svg; let svgroot;
|
2019-11-24 03:29:24 +00:00
|
|
|
|
beforeEach(() => {
|
|
|
|
|
document.body.textContent = '';
|
|
|
|
|
|
|
|
|
|
mockHistorySubCommands = [];
|
|
|
|
|
mockCount.clearSelection = 0;
|
|
|
|
|
mockCount.addToSelection = 0;
|
|
|
|
|
mockCount.addCommandToHistory = 0;
|
|
|
|
|
|
|
|
|
|
const sandbox = document.createElement('div');
|
|
|
|
|
svg = document.createElementNS(NS.SVG, 'svg');
|
|
|
|
|
svgroot = mockCreateSVGElement({
|
|
|
|
|
element: 'svg',
|
2021-05-12 23:00:09 +00:00
|
|
|
|
attr: { id: 'svgroot' }
|
2019-11-24 03:29:24 +00:00
|
|
|
|
});
|
|
|
|
|
sandbox.append(svgroot);
|
|
|
|
|
document.body.append(sandbox);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('Test svgedit.utilities package', function () {
|
|
|
|
|
assert.ok(utilities);
|
|
|
|
|
assert.ok(utilities.toXml);
|
2020-10-04 21:07:31 +00:00
|
|
|
|
assert.equal(typeof utilities.toXml, typeof function () { /* empty fn */ });
|
2019-11-24 03:29:24 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('Test svgedit.utilities.toXml() function', function () {
|
2021-05-12 23:00:09 +00:00
|
|
|
|
const { toXml } = utilities;
|
2019-11-24 03:29:24 +00:00
|
|
|
|
|
|
|
|
|
assert.equal(toXml('a'), 'a');
|
|
|
|
|
assert.equal(toXml('ABC_'), 'ABC_');
|
|
|
|
|
assert.equal(toXml('PB&J'), 'PB&J');
|
|
|
|
|
assert.equal(toXml('2 < 5'), '2 < 5');
|
|
|
|
|
assert.equal(toXml('5 > 2'), '5 > 2');
|
|
|
|
|
assert.equal(toXml('\'<&>"'), ''<&>"');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('Test svgedit.utilities.encode64() function', function () {
|
2021-05-12 23:00:09 +00:00
|
|
|
|
const { encode64 } = utilities;
|
2019-11-24 03:29:24 +00:00
|
|
|
|
|
|
|
|
|
assert.equal(encode64('abcdef'), 'YWJjZGVm');
|
|
|
|
|
assert.equal(encode64('12345'), 'MTIzNDU=');
|
|
|
|
|
assert.equal(encode64(' '), 'IA==');
|
|
|
|
|
assert.equal(encode64('`~!@#$%^&*()-_=+[{]}\\|;:\'",<.>/?'), 'YH4hQCMkJV4mKigpLV89K1t7XX1cfDs6JyIsPC4+Lz8=');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('Test svgedit.utilities.decode64() function', function () {
|
2021-05-12 23:00:09 +00:00
|
|
|
|
const { decode64 } = utilities;
|
2019-11-24 03:29:24 +00:00
|
|
|
|
|
|
|
|
|
assert.equal(decode64('YWJjZGVm'), 'abcdef');
|
|
|
|
|
assert.equal(decode64('MTIzNDU='), '12345');
|
|
|
|
|
assert.equal(decode64('IA=='), ' ');
|
|
|
|
|
assert.equal(decode64('YH4hQCMkJV4mKigpLV89K1t7XX1cfDs6JyIsPC4+Lz8='), '`~!@#$%^&*()-_=+[{]}\\|;:\'",<.>/?');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('Test svgedit.utilities.convertToXMLReferences() function', function () {
|
|
|
|
|
const convert = utilities.convertToXMLReferences;
|
|
|
|
|
assert.equal(convert('ABC'), 'ABC');
|
|
|
|
|
// assert.equal(convert('<27>BC'), 'ÀBC');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('Test svgedit.utilities.bboxToObj() function', function () {
|
2021-05-12 23:00:09 +00:00
|
|
|
|
const { bboxToObj } = utilities;
|
2019-11-24 03:29:24 +00:00
|
|
|
|
|
|
|
|
|
const rect = svg.createSVGRect();
|
|
|
|
|
rect.x = 1;
|
|
|
|
|
rect.y = 2;
|
|
|
|
|
rect.width = 3;
|
|
|
|
|
rect.height = 4;
|
|
|
|
|
|
|
|
|
|
const obj = bboxToObj(rect);
|
|
|
|
|
assert.equal(typeof obj, typeof {});
|
|
|
|
|
assert.equal(obj.x, 1);
|
|
|
|
|
assert.equal(obj.y, 2);
|
|
|
|
|
assert.equal(obj.width, 3);
|
|
|
|
|
assert.equal(obj.height, 4);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('Test getUrlFromAttr', function () {
|
|
|
|
|
assert.equal(utilities.getUrlFromAttr('url(#foo)'), '#foo');
|
|
|
|
|
assert.equal(utilities.getUrlFromAttr('url(somefile.svg#foo)'), 'somefile.svg#foo');
|
|
|
|
|
assert.equal(utilities.getUrlFromAttr('url("#foo")'), '#foo');
|
|
|
|
|
assert.equal(utilities.getUrlFromAttr('url("#foo")'), '#foo');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('Test getPathDFromSegments', function () {
|
2021-05-12 23:00:09 +00:00
|
|
|
|
const { getPathDFromSegments } = utilities;
|
2019-11-24 03:29:24 +00:00
|
|
|
|
|
|
|
|
|
const doc = utilities.text2xml('<svg></svg>');
|
|
|
|
|
const path = doc.createElementNS(NS.SVG, 'path');
|
|
|
|
|
path.setAttribute('d', 'm0,0l5,0l0,5l-5,0l0,-5z');
|
|
|
|
|
let d = getPathDFromSegments([
|
2021-05-12 23:00:09 +00:00
|
|
|
|
[ 'M', [ 1, 2 ] ],
|
|
|
|
|
[ 'Z', [] ]
|
2019-11-24 03:29:24 +00:00
|
|
|
|
]);
|
|
|
|
|
assert.equal(d, 'M1,2 Z');
|
|
|
|
|
|
|
|
|
|
d = getPathDFromSegments([
|
2021-05-12 23:00:09 +00:00
|
|
|
|
[ 'M', [ 1, 2 ] ],
|
|
|
|
|
[ 'M', [ 3, 4 ] ],
|
|
|
|
|
[ 'Z', [] ]
|
2019-11-24 03:29:24 +00:00
|
|
|
|
]);
|
|
|
|
|
assert.equal(d, 'M1,2 M3,4 Z');
|
|
|
|
|
|
|
|
|
|
d = getPathDFromSegments([
|
2021-05-12 23:00:09 +00:00
|
|
|
|
[ 'M', [ 1, 2 ] ],
|
|
|
|
|
[ 'C', [ 3, 4, 5, 6 ] ],
|
|
|
|
|
[ 'Z', [] ]
|
2019-11-24 03:29:24 +00:00
|
|
|
|
]);
|
|
|
|
|
assert.equal(d, 'M1,2 C3,4 5,6 Z');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('Test getPathDFromElement', function () {
|
2021-05-12 23:00:09 +00:00
|
|
|
|
const { getPathDFromElement } = utilities;
|
2019-11-24 03:29:24 +00:00
|
|
|
|
|
|
|
|
|
let elem = mockCreateSVGElement({
|
|
|
|
|
element: 'path',
|
2021-05-12 23:00:09 +00:00
|
|
|
|
attr: { id: 'path', d: 'M0,1 Z' }
|
2019-11-24 03:29:24 +00:00
|
|
|
|
});
|
|
|
|
|
svgroot.append(elem);
|
|
|
|
|
assert.equal(getPathDFromElement(elem), 'M0,1 Z');
|
|
|
|
|
elem.remove();
|
|
|
|
|
|
|
|
|
|
elem = mockCreateSVGElement({
|
|
|
|
|
element: 'rect',
|
2021-05-12 23:00:09 +00:00
|
|
|
|
attr: { id: 'rect', x: '0', y: '1', width: '5', height: '10' }
|
2019-11-24 03:29:24 +00:00
|
|
|
|
});
|
|
|
|
|
svgroot.append(elem);
|
|
|
|
|
assert.equal(getPathDFromElement(elem), 'M0,1 L5,1 L5,11 L0,11 L0,1 Z');
|
|
|
|
|
elem.remove();
|
|
|
|
|
|
|
|
|
|
elem = mockCreateSVGElement({
|
|
|
|
|
element: 'rect',
|
2021-05-12 23:00:09 +00:00
|
|
|
|
attr: { id: 'roundrect', x: '0', y: '1', rx: '2', ry: '3', width: '10', height: '11' }
|
2019-11-24 03:29:24 +00:00
|
|
|
|
});
|
|
|
|
|
svgroot.append(elem);
|
2021-08-30 10:09:45 +00:00
|
|
|
|
const closeEnough = /M0,4 C0,2.3\d* 0.9\d*,1 2,1 L8,1 C9.0\d*,1 10,2.3\d* 10,4 L10,9 C10,10.6\d* 9.0\d*,12 8,12 L2,12 C0.9\d*,12 0,10.6\d* 0,9 L0,4 Z/;
|
2019-11-24 03:29:24 +00:00
|
|
|
|
assert.equal(closeEnough.test(getPathDFromElement(elem)), true);
|
|
|
|
|
elem.remove();
|
|
|
|
|
|
|
|
|
|
elem = mockCreateSVGElement({
|
|
|
|
|
element: 'line',
|
2021-05-12 23:00:09 +00:00
|
|
|
|
attr: { id: 'line', x1: '0', y1: '1', x2: '5', y2: '6' }
|
2019-11-24 03:29:24 +00:00
|
|
|
|
});
|
|
|
|
|
svgroot.append(elem);
|
|
|
|
|
assert.equal(getPathDFromElement(elem), 'M0,1L5,6');
|
|
|
|
|
elem.remove();
|
|
|
|
|
|
|
|
|
|
elem = mockCreateSVGElement({
|
|
|
|
|
element: 'circle',
|
2021-05-12 23:00:09 +00:00
|
|
|
|
attr: { id: 'circle', cx: '10', cy: '11', rx: '5', ry: '10' }
|
2019-11-24 03:29:24 +00:00
|
|
|
|
});
|
|
|
|
|
svgroot.append(elem);
|
2021-08-30 10:09:45 +00:00
|
|
|
|
assert.equal(getPathDFromElement(elem), 'M5,11 C5,5.475138121546961 7.237569060773481,1 10,1 C12.762430939226519,1 15,5.475138121546961 15,11 C15,16.524861878453038 12.762430939226519,21 10,21 C7.237569060773481,21 5,16.524861878453038 5,11 Z');
|
2019-11-24 03:29:24 +00:00
|
|
|
|
elem.remove();
|
|
|
|
|
|
|
|
|
|
elem = mockCreateSVGElement({
|
|
|
|
|
element: 'polyline',
|
2021-05-12 23:00:09 +00:00
|
|
|
|
attr: { id: 'polyline', points: '0,1 5,1 5,11 0,11' }
|
2019-11-24 03:29:24 +00:00
|
|
|
|
});
|
|
|
|
|
svgroot.append(elem);
|
|
|
|
|
assert.equal(getPathDFromElement(elem), 'M0,1 5,1 5,11 0,11');
|
|
|
|
|
elem.remove();
|
|
|
|
|
|
2021-05-12 23:00:09 +00:00
|
|
|
|
assert.equal(getPathDFromElement({ tagName: 'something unknown' }), undefined);
|
2019-11-24 03:29:24 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('Test getBBoxOfElementAsPath', function () {
|
|
|
|
|
/**
|
|
|
|
|
* Wrap `utilities.getBBoxOfElementAsPath` to convert bbox to object for testing.
|
|
|
|
|
* @type {module:utilities.getBBoxOfElementAsPath}
|
|
|
|
|
*/
|
2021-12-25 20:25:12 +00:00
|
|
|
|
function getBBoxOfElementAsPath (elem, addSVGElemensFromJson, pathActions) {
|
|
|
|
|
const bbox = utilities.getBBoxOfElementAsPath(elem, addSVGElemensFromJson, pathActions);
|
2019-11-24 03:29:24 +00:00
|
|
|
|
return utilities.bboxToObj(bbox); // need this for assert.equal() to work.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let elem = mockCreateSVGElement({
|
|
|
|
|
element: 'path',
|
2021-05-12 23:00:09 +00:00
|
|
|
|
attr: { id: 'path', d: 'M0,1 Z' }
|
2019-11-24 03:29:24 +00:00
|
|
|
|
});
|
|
|
|
|
svgroot.append(elem);
|
2021-12-25 20:25:12 +00:00
|
|
|
|
let bbox = getBBoxOfElementAsPath(elem, mockaddSVGElemensFromJson, mockPathActions);
|
2021-05-12 23:00:09 +00:00
|
|
|
|
assert.deepEqual(bbox, { x: 0, y: 1, width: 0, height: 0 });
|
2019-11-24 03:29:24 +00:00
|
|
|
|
elem.remove();
|
|
|
|
|
|
|
|
|
|
elem = mockCreateSVGElement({
|
|
|
|
|
element: 'rect',
|
2021-05-12 23:00:09 +00:00
|
|
|
|
attr: { id: 'rect', x: '0', y: '1', width: '5', height: '10' }
|
2019-11-24 03:29:24 +00:00
|
|
|
|
});
|
|
|
|
|
svgroot.append(elem);
|
2021-12-25 20:25:12 +00:00
|
|
|
|
bbox = getBBoxOfElementAsPath(elem, mockaddSVGElemensFromJson, mockPathActions);
|
2021-05-12 23:00:09 +00:00
|
|
|
|
assert.deepEqual(bbox, { x: 0, y: 1, width: 5, height: 10 });
|
2019-11-24 03:29:24 +00:00
|
|
|
|
elem.remove();
|
|
|
|
|
|
|
|
|
|
elem = mockCreateSVGElement({
|
|
|
|
|
element: 'line',
|
2021-05-12 23:00:09 +00:00
|
|
|
|
attr: { id: 'line', x1: '0', y1: '1', x2: '5', y2: '6' }
|
2019-11-24 03:29:24 +00:00
|
|
|
|
});
|
|
|
|
|
svgroot.append(elem);
|
2021-12-25 20:25:12 +00:00
|
|
|
|
bbox = getBBoxOfElementAsPath(elem, mockaddSVGElemensFromJson, mockPathActions);
|
2021-05-12 23:00:09 +00:00
|
|
|
|
assert.deepEqual(bbox, { x: 0, y: 1, width: 5, height: 5 });
|
2019-11-24 03:29:24 +00:00
|
|
|
|
elem.remove();
|
|
|
|
|
|
|
|
|
|
// TODO: test element with transform. Need resetOrientation above to be working or mock it.
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('Test convertToPath rect', function () {
|
2021-05-12 23:00:09 +00:00
|
|
|
|
const { convertToPath } = utilities;
|
2019-11-24 03:29:24 +00:00
|
|
|
|
const attrs = {
|
|
|
|
|
fill: 'red',
|
|
|
|
|
stroke: 'white',
|
|
|
|
|
'stroke-width': '1',
|
|
|
|
|
visibility: 'hidden'
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const elem = mockCreateSVGElement({
|
|
|
|
|
element: 'rect',
|
2021-05-12 23:00:09 +00:00
|
|
|
|
attr: { id: 'rect', x: '0', y: '1', width: '5', height: '10' }
|
2019-11-24 03:29:24 +00:00
|
|
|
|
});
|
|
|
|
|
svgroot.append(elem);
|
2021-12-25 20:25:12 +00:00
|
|
|
|
const path = convertToPath(elem, attrs, mockaddSVGElemensFromJson, mockPathActions, mockClearSelection, mockAddToSelection, mockHistory, mockAddCommandToHistory);
|
2019-11-24 03:29:24 +00:00
|
|
|
|
assert.equal(path.getAttribute('d'), 'M0,1 L5,1 L5,11 L0,11 L0,1 Z');
|
|
|
|
|
assert.equal(path.getAttribute('visibilituy'), null);
|
|
|
|
|
assert.equal(path.id, 'rect');
|
|
|
|
|
assert.equal(path.parentNode, svgroot);
|
|
|
|
|
assert.equal(elem.parentNode, null);
|
|
|
|
|
assert.equal(mockHistorySubCommands.length, 2);
|
|
|
|
|
assert.equal(mockCount.clearSelection, 1);
|
|
|
|
|
assert.equal(mockCount.addToSelection, 1);
|
|
|
|
|
assert.equal(mockCount.addCommandToHistory, 1);
|
|
|
|
|
path.remove();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('Test convertToPath unknown element', function () {
|
2021-05-12 23:00:09 +00:00
|
|
|
|
const { convertToPath } = utilities;
|
2019-11-24 03:29:24 +00:00
|
|
|
|
const attrs = {
|
|
|
|
|
fill: 'red',
|
|
|
|
|
stroke: 'white',
|
|
|
|
|
'stroke-width': '1',
|
|
|
|
|
visibility: 'hidden'
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const elem = {
|
|
|
|
|
tagName: 'something unknown',
|
|
|
|
|
id: 'something-unknown',
|
2021-05-09 17:29:45 +00:00
|
|
|
|
getAttribute () { return ''; },
|
2019-11-24 03:29:24 +00:00
|
|
|
|
parentNode: svgroot
|
|
|
|
|
};
|
2021-12-25 20:25:12 +00:00
|
|
|
|
const path = convertToPath(elem, attrs, mockaddSVGElemensFromJson, mockPathActions, mockClearSelection, mockAddToSelection, mockHistory, mockAddCommandToHistory);
|
2019-11-24 03:29:24 +00:00
|
|
|
|
assert.equal(path, null);
|
|
|
|
|
assert.equal(elem.parentNode, svgroot);
|
|
|
|
|
assert.equal(mockHistorySubCommands.length, 0);
|
|
|
|
|
assert.equal(mockCount.clearSelection, 0);
|
|
|
|
|
assert.equal(mockCount.addToSelection, 0);
|
|
|
|
|
assert.equal(mockCount.addCommandToHistory, 0);
|
|
|
|
|
});
|
|
|
|
|
});
|