aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorViatrix2026-03-13 22:19:22 -0700
committerViatrix2026-03-13 22:19:22 -0700
commitc03729d9bb9c0902b89a15643e67b620f7dd669b (patch)
tree2e108dbfd49e8476dba06b167d7d88a5e4af5add
parenta6d891a0ddbcca22ee6aa946e90f7f8d42e1fec5 (diff)
Alt attribute now set correctly (+ test).
-rw-r--r--imagemap.py18
-rw-r--r--tests/data/refs/imagemap__--maptype__HTML__alt__svg.out4
-rw-r--r--tests/data/svg/alt.svg15
-rw-r--r--tests/test_imagemap_comparison.py1
4 files changed, 30 insertions, 8 deletions
diff --git a/imagemap.py b/imagemap.py
index a40116f..30fa0cd 100644
--- a/imagemap.py
+++ b/imagemap.py
@@ -20,7 +20,7 @@ def htmlval(val):
AREA_ATTRS={
'href':lambda a:a.get('href',a.get('{http://www.w3.org/1999/xlink}href')),
- 'alt':lambda a:a.get('{http://www.w3.org/1999/xlink}title')
+ 'alt':lambda a:a.get('aria-label',a.get('{http://www.w3.org/1999/xlink}title',a.title))
} # TODO target
SHAPE_MARKUP = {
'HTML': lambda shape, coords, href, alt:
@@ -96,8 +96,8 @@ class ImageMap(inkex.OutputExtension):
# (we re-set the existing style attribute in case it got unset on non-paths)
newbytes=inkscape_command(self.svg,actions=command)
self.svg=self.load(newbytes).getroot()
+ # preprocessing done, now for map generation
- seen=set()
shapes=[[] for i in range(len(links))]
for el in self.svg.iterdescendants():
if not isinstance(el,inkex.ShapeElement): continue
@@ -106,7 +106,6 @@ class ImageMap(inkex.OutputExtension):
linkindex=int(linkindex[len(CSS_LINK_INDEX)+3:-2])
link=links[linkindex]
href=link['href']
- alt=link['alt'] if int(linkindex) not in seen else None
path=el.get_path().transform(el.composed_transform()).to_superpath()
bezier.cspsubdiv(path,0.5)
for subpath in path:
@@ -115,12 +114,15 @@ class ImageMap(inkex.OutputExtension):
while i<len(coords):
if coords[i]==coords[(i+1)%len(coords)]: coords.pop(i)
else: i+=1
- if rectifiable(coords): shapes[linkindex].insert(0,shapemarkup('rect',rectify(coords),href,alt))
- elif len(coords)>=3: shapes[linkindex].insert(0,shapemarkup('poly',coords,href,alt))
+ if rectifiable(coords): shapes[linkindex].insert(0,{'shape':'rect','coords':rectify(coords),'href':href})
+ elif len(coords)>=3: shapes[linkindex].insert(0,{'shape':'poly','coords':coords,'href':href})
href=None # because subsequent subpaths must be enclaves
- alt=None # TODO make it come first even though the order's reversed
- seen.add(linkindex)
- stream.write(bytes(''.join(sum(shapes,start=[])),'utf-8'))
+ for i in range(len(shapes)):
+ alt=links[i]['alt']
+ if len(shapes[i])==0: inkex.errormsg(_("The hyperlink \"{}\" is not present in the output.").format(links[i]['href']))
+ for j in shapes[i]:
+ stream.write(bytes(shapemarkup(j['shape'],j['coords'],j['href'],alt),'utf-8'))
+ alt=None
if __name__ == "__main__":
ImageMap().run()
diff --git a/tests/data/refs/imagemap__--maptype__HTML__alt__svg.out b/tests/data/refs/imagemap__--maptype__HTML__alt__svg.out
new file mode 100644
index 0000000..d78824d
--- /dev/null
+++ b/tests/data/refs/imagemap__--maptype__HTML__alt__svg.out
@@ -0,0 +1,4 @@
+<area shape=rect coords=25,20,75,30 href=http://example.com/1 alt=A>
+<area shape=rect coords=25,45,45,55 href=http://example.com/2 alt=B>
+<area shape=rect coords=55,45,75,55 href=http://example.com/2>
+<area shape=rect coords=25,70,75,80 href=http://example.com/3 alt=C>
diff --git a/tests/data/svg/alt.svg b/tests/data/svg/alt.svg
new file mode 100644
index 0000000..93aff8d
--- /dev/null
+++ b/tests/data/svg/alt.svg
@@ -0,0 +1,15 @@
+<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
+ <a xlink:href="http://example.com/1">
+ <title>A</title>
+ <rect x="25" y="20" width="50" height="10"/>
+ </a>
+ <a xlink:href="http://example.com/2" xlink:title="B">
+ <title>A</title>
+ <rect x="25" y="45" width="20" height="10"/>
+ <rect x="55" y="45" width="20" height="10"/>
+ </a>
+ <a xlink:href="http://example.com/3" aria-label="C" xlink:title="B">
+ <title>A</title>
+ <rect x="25" y="70" width="50" height="10"/>
+ </a>
+</svg>
diff --git a/tests/test_imagemap_comparison.py b/tests/test_imagemap_comparison.py
index 994bc95..2dda137 100644
--- a/tests/test_imagemap_comparison.py
+++ b/tests/test_imagemap_comparison.py
@@ -4,6 +4,7 @@ from imagemap import ImageMap
class ImageMapComparisonTest(ComparisonMixin, TestCase):
effect_class = ImageMap
compare_file = (
+ 'svg/alt.svg',
'svg/enclave.svg',
'svg/fillstroke.svg',
'svg/image.svg',