If click handlers are assigned to an SVG diagram, this can lead to errors and therefore to unexpected behavior.
The cause of the error lies in the order and the way in which the SVG is constructed. Each part of the SVG is added to the SVG via the SVGBuilder.java. A click handler is also a part of the SVG, but it is not added to an element as a child like the other parts, but is assigned directly to the last element(_current) as an onClick event.
However, the SVGBuilder resets the _current variable after the element has been created if the part was a polyline, a polygon, a rect or a text. Example:
@Override public void endText() { _current = null; }
If a click handler is added directly after one of the parts mentioned, the error shown above occurs.
In several draw(SvgWriter) methods, the list of all parts of the SVG is iterated over and created in full one by one (including calling the "end" methods in the SVGBuilder).
Solutions
Depending on which element the click handlers are intended for, there are different solutions to the problem:
- If the previously created element is not intended as the target of the click:
Instead of the _current -element, select the _parent or with _root the SVG itself as click target.
- If there is no reason why the _current is set to null:
Remove these statements so that nothing happens when the end methods are called, as with endPath() or endData().
- If the click handler should actually be attached to a specific element, regardless of the part order of the SVG:
In this case, adding a click handler should be integrated into the build process of an SVG element instead of being considered a separate part of the SVG. For this purpose, the calling of the creation of such elements would also have to be adapted.