|
| 1 | +""" |
| 2 | +A rewrite of Brython's SVG module, to remove JavaScript / document related |
| 3 | +interactions (so this can be used within a web worker, where document is not |
| 4 | +conveniently available). |
| 5 | +
|
| 6 | +Author: Nicholas H.Tollervey (ntollervey@anaconda.com) |
| 7 | +Based on original work by: Romain Casati |
| 8 | +
|
| 9 | +License: GPL v3 or higher. |
| 10 | +""" |
| 11 | + |
| 12 | + |
| 13 | +class Node: |
| 14 | + """ |
| 15 | + Represents a node in the DOM. |
| 16 | + """ |
| 17 | + |
| 18 | + def __init__(self, **kwargs): |
| 19 | + self._node = kwargs |
| 20 | + self.parent = kwargs.get("parent") |
| 21 | + |
| 22 | + @property |
| 23 | + def outerHTML(self): |
| 24 | + """ |
| 25 | + Get a string representation of the element's outer HTML. |
| 26 | + """ |
| 27 | + return NotImplemented |
| 28 | + |
| 29 | + |
| 30 | +class ElementNode(Node): |
| 31 | + """ |
| 32 | + An element defined by a tag, may have attributes and children. |
| 33 | + """ |
| 34 | + |
| 35 | + def __init__(self, **kwargs): |
| 36 | + super().__init__(**kwargs) |
| 37 | + self.tagName = kwargs["tagName"] |
| 38 | + self.attributes = kwargs.get("attributes", {}) |
| 39 | + self.value = kwargs.get("value") |
| 40 | + self.childNodes = [] |
| 41 | + |
| 42 | + def appendChild(self, child): |
| 43 | + """ |
| 44 | + Add a child node to the children of this node. Using DOM API naming |
| 45 | + conventions. |
| 46 | + """ |
| 47 | + child.parent = self |
| 48 | + self.childNodes.append(child) |
| 49 | + |
| 50 | + def setAttribute(self, key, value): |
| 51 | + """ |
| 52 | + Sets an attribute on the node. |
| 53 | + """ |
| 54 | + self.attributes[key] = value |
| 55 | + |
| 56 | + @property |
| 57 | + def outerHTML(self): |
| 58 | + """ |
| 59 | + Get a string representation of the element's outer HTML. Using DOM API |
| 60 | + naming conventions. |
| 61 | + """ |
| 62 | + result = "<" + self.tagName |
| 63 | + for attr, val in self.attributes.items(): |
| 64 | + result += " " + attr + '="' + str(val) + '"' |
| 65 | + result += ">" |
| 66 | + result += self.innerHTML |
| 67 | + result += "</" + self.tagName + ">" |
| 68 | + return result |
| 69 | + |
| 70 | + @property |
| 71 | + def innerHTML(self): |
| 72 | + """ |
| 73 | + Get a string representation of the element's inner HTML. Using DOM API |
| 74 | + naming conventions. |
| 75 | + """ |
| 76 | + result = "" |
| 77 | + for child in self.childNodes: |
| 78 | + result += child.outerHTML |
| 79 | + return result |
| 80 | + |
| 81 | + |
| 82 | +class TextNode(Node): |
| 83 | + """ |
| 84 | + Textual content inside an ElementNode. |
| 85 | + """ |
| 86 | + |
| 87 | + def __init__(self, **kwargs): |
| 88 | + super().__init__(**kwargs) |
| 89 | + self.nodeValue = kwargs.get("nodeValue") |
| 90 | + |
| 91 | + @property |
| 92 | + def outerHTML(self): |
| 93 | + """ |
| 94 | + Get a string representation of the element's outer HTML. |
| 95 | + """ |
| 96 | + return self.nodeValue |
| 97 | + |
| 98 | + |
| 99 | +_svg_ns = "http://www.w3.org/2000/svg" |
| 100 | +_xlink_ns = "http://www.w3.org/1999/xlink" |
| 101 | + |
| 102 | + |
| 103 | +def _tag_func(tag): |
| 104 | + def func(*args, **kwargs): |
| 105 | + node = ElementNode(tagName=tag) |
| 106 | + # this is mandatory to display svg properly |
| 107 | + if tag == "svg": |
| 108 | + node.setAttribute("xmlns", _svg_ns) |
| 109 | + for arg in args: |
| 110 | + if isinstance(arg, (str, int, float)): |
| 111 | + arg = TextNode(nodeValue=str(arg)) |
| 112 | + node.appendChild(arg) |
| 113 | + for key, value in kwargs.items(): |
| 114 | + key = key.lower() |
| 115 | + if key[0:2] == "on": |
| 116 | + # Ignore event handlers within the SVG. This shouldn't happen. |
| 117 | + pass |
| 118 | + elif key == "style": |
| 119 | + node.setAttribute( |
| 120 | + "style", ";".join(f"{k}: {v}" for k, v in value.items()) |
| 121 | + ) |
| 122 | + elif value is not False: |
| 123 | + node.setAttribute(key.replace("_", "-"), str(value)) |
| 124 | + return node |
| 125 | + |
| 126 | + return func |
| 127 | + |
| 128 | + |
| 129 | +a = _tag_func("a") |
| 130 | +altGlyph = _tag_func("altGlyph") |
| 131 | +altGlyphDef = _tag_func("altGlyphDef") |
| 132 | +altGlyphItem = _tag_func("altGlyphItem") |
| 133 | +animate = _tag_func("animate") |
| 134 | +animateColor = _tag_func("animateColor") |
| 135 | +animateMotion = _tag_func("animateMotion") |
| 136 | +animateTransform = _tag_func("animateTransform") |
| 137 | +circle = _tag_func("circle") |
| 138 | +clipPath = _tag_func("clipPath") |
| 139 | +color_profile = _tag_func("color_profile") |
| 140 | +cursor = _tag_func("cursor") |
| 141 | +defs = _tag_func("defs") |
| 142 | +desc = _tag_func("desc") |
| 143 | +ellipse = _tag_func("ellipse") |
| 144 | +feBlend = _tag_func("feBlend") |
| 145 | +foreignObject = _tag_func("foreignObject") |
| 146 | +g = _tag_func("g") |
| 147 | +image = _tag_func("image") |
| 148 | +line = _tag_func("line") |
| 149 | +linearGradient = _tag_func("linearGradient") |
| 150 | +marker = _tag_func("marker") |
| 151 | +mask = _tag_func("mask") |
| 152 | +path = _tag_func("path") |
| 153 | +pattern = _tag_func("pattern") |
| 154 | +polygon = _tag_func("polygon") |
| 155 | +polyline = _tag_func("polyline") |
| 156 | +radialGradient = _tag_func("radialGradient") |
| 157 | +rect = _tag_func("rect") |
| 158 | +set = _tag_func("set") |
| 159 | +stop = _tag_func("stop") |
| 160 | +svg = _tag_func("svg") |
| 161 | +text = _tag_func("text") |
| 162 | +tref = _tag_func("tref") |
| 163 | +tspan = _tag_func("tspan") |
| 164 | +use = _tag_func("use") |
0 commit comments