Truly independent web browser
<!DOCTYPE html>
<script src="../include.js"></script>
<style>
@font-face {
font-family: Ahem;
src: url("../../../Ref/data/fonts/Ahem.ttf");
}
body {
margin: 0;
}
svg {
display: block;
font: 20px Ahem;
}
</style>
<svg width="800" height="200">
<text id="link" x="10" y="30">S: <a id="link-a" href="#">LINK</a> E</text>
<text id="link-in-tspan" x="10" y="60">S<tspan id="link-in-tspan-tspan"><a id="link-in-tspan-a" href="#">L</a>M</tspan>E</text>
<text id="tspan-in-link" x="10" y="90">A<a id="tspan-in-link-a" href="#"> <tspan id="tspan-in-link-tspan">B</tspan> </a>C</text>
<text id="link-with-anchor" x="300" y="120" text-anchor="middle">AB<a id="link-with-anchor-a" href="#">CD</a>EF</text>
<a id="link-around-text" href="#"><text id="text-in-link" x="10" y="150">AB</text></a>
<text id="link-last" x="10" y="180">S:<a id="link-last-a" href="#">LINK</a></text>
</svg>
<script>
const round = value => Math.round(value * 100) / 100;
const characters = element => {
const positions = [];
for (let index = 0; index < element.getNumberOfChars(); ++index) {
const cell = element.getExtentOfChar(index);
positions.push(`${round(cell.x)},${round(cell.y)}`);
}
return `${positions.length} characters, length ${round(element.getComputedTextLength())}: ${positions.join(" ")}`;
};
const box = element => {
const rect = element.getBBox();
return `${round(rect.x)},${round(rect.y)} ${round(rect.width)}x${round(rect.height)}`;
};
asyncTest(async done => {
await document.fonts.load("20px Ahem");
for (const element of document.querySelectorAll("svg [id]")) {
const queries = element.getNumberOfChars ? `${characters(element)}; ` : "";
println(`${element.id}: ${queries}box ${box(element)}`);
}
// The link's text is what a click on it lands on.
const svg = document.querySelector("svg").getBoundingClientRect();
for (const [x, y] of [[80, 170], [20, 170], [80, 20], [20, 20], [40, 50], [60, 80], [300, 110]]) {
const hit = document.elementFromPoint(svg.x + x, svg.y + y);
println(`elementFromPoint(${x}, ${y}): ${hit.id || hit.localName}`);
}
done();
});
</script>