1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg"
xmlns:h="http://www.w3.org/1999/xhtml">
<metadata>
<h:link rel="help" href="https://svgwg.org/specs/paths/#InterfaceSVGPathSegment"/>
</metadata>
<h:script src="/resources/testharness.js"/>
<h:script src="/resources/testharnessreport.js"/>
<path id="track" d="m 10 20 h 30"/>S
<script><![CDATA[
test(function() {
let track = document.getElementById('track');
assert_equals(Object.getPrototypeOf(track), SVGPathElement.prototype);
assert_not_equals(track.getPathData, undefined);
assert_not_equals(track.setPathData, undefined);
assert_not_equals(track.getPathSegmentAtLength, undefined);
}, "SVGPathSegment interface exists");
function checkObjects(actual, expected, epsilon) {
if (Array.isArray(expected)) {
if (actual.length != expected.length) {
return false;
}
for (let i = 0; i < expected.length; i++) {
if (!checkObjects(actual[i], expected[i], epsilon) ) {
return false;
}
}
return true;
}
if (typeof expected == "object") {
for (let key in expected) {
if (!checkObjects(actual[key], expected[key], epsilon)) {
return false;
}
}
return true;
}
if (typeof expected == "number") {
if (epsilon === undefined) {
epsilon = 0;
}
return Math.abs(actual - expected) <= epsilon;
}
return actual == expected;
}
function ToString(value) {
let output = "";
if (Array.isArray(value)) {
let arrayAsString = [];
for (let i = 0; i < value.length; i++) {
arrayAsString.push(ToString(value[i]));
}
return arrayAsString.join(' ');
}
if (typeof value == "object") {
let objectAsArray = [];
for (let key in value) {
objectAsArray.push(ToString(value[key]));
}
return objectAsArray.join(' ');
}
if (typeof value == "number") {
return "" + +value.toFixed(3);
}
return value;
}
var checkPathData = function(actual, expected, epsilon) {
assert_true(checkObjects(actual, expected, epsilon), "actual:" + ToString(actual) + " expected:" + ToString(expected));
};
let getPathDataTests = [
{
description: "straight lines",
input: "M 0 0 H 100 V 100 H 0 Z",
output: [
{ type: "M", values: [0, 0] },
{ type: "H", values: [100] },
{ type: "V", values: [100] },
{ type: "H", values: [0] },
{ type: "Z", values: [] }
]
},
{
description: "arc",
input: "M 0 0 A 10 10 0 0 1 20 20",
output: [
{ type: "M", values: [0, 0] },
{ type: "A", values: [10, 10, 0, 0, 1, 20, 20] }
]
},
{
description: "cubic",
input: "M 413 140 C 413 140 438 40 302 44",
output: [
{ type: "M", values: [413, 140] },
{ type: "C", values: [413, 140, 438, 40, 302, 44] }
]
}
];
getPathDataTests.forEach(t => {
test(_ => {
let path = document.createElementNS("http://www.w3.org/2000/svg", "path");
path.setAttribute("d", t.input);
let pathData = path.getPathData();
checkPathData(pathData, t.output);
}, "Test path.getPathData() " + t.description)
});
let getNormalizedPathDataTests = [
{
description: "straight lines",
input: "M 0 0 H 100 V 100 H 0 Z",
output: [
{ type: "M", values: [0, 0] },
{ type: "L", values: [100, 0] },
{ type: "L", values: [100, 100] },
{ type: "L", values: [0, 100] },
{ type: "Z", values: [] }
]
},
{
description: "cubic",
input: "M 0 0 C 30,90 25,10 50,10 S 70,90 90,90",
output: [
{ type: "M", values: [0, 0] },
{ type: "C", values: [30, 90, 25, 10, 50, 10] },
{ type: "C", values: [75, 10, 70, 90, 90, 90] },
]
},
{
description: "quadratic",
input: "M 0 0 Q 30 30 30 60 t 30 0 30 0 30 0 30 0 30 0",
output: [
{ type: "M", values: [0, 0] },
{ type: "C", values: [20, 20, 30, 40, 30, 60] },
{ type: "C", values: [30, 80, 40, 80, 60, 60] },
{ type: "C", values: [80, 40, 90, 40, 90, 60] },
{ type: "C", values: [90, 80, 100, 80, 120, 60] },
{ type: "C", values: [140, 40, 150, 40, 150, 60] },
{ type: "C", values: [150, 80, 160, 80, 180, 60] },
]
},
{
description: "elliptical arc",
input: "M 6 10 A 10 10 10 0 0 15 10",
output: [
{ type: "M", values: [6, 10] },
{ type: "C", values: [8.83, 11.426, 12.169, 11.426, 15, 10] },
],
epsilon: 0.01,
},
];
getNormalizedPathDataTests.forEach(t => {
test(_ => {
let path = document.createElementNS("http://www.w3.org/2000/svg", "path");
path.setAttribute("d", t.input);
let pathData = path.getPathData({normalize: true});
checkPathData(pathData, t.output, t.epsilon);
}, "Test path.getPathData({normalize: true}) " + t.description)
});
let setPathDataTests = [
{
description: "lines",
input: [
{ type: "M", values: [0, 0] },
{ type: "L", values: [10, 10] },
{ type: "Z", values: [] }
],
output: "M 0 0 L 10 10 Z"
},
{
description: "empty",
input: [],
output: ""
},
];
setPathDataTests.forEach(t => {
test(_ => {
let path = document.createElementNS("http://www.w3.org/2000/svg", "path");
path.setAttribute("d", t.output);
let segments = path.getPathData();
path.removeAttribute("d");
path.setPathData(segments);
assert_equals(path.getAttribute("d"), t.output);
}, "Test path.setPathData()" + t.description);
});
]]></script>
</svg>
|