Skip to content

Commit cde65be

Browse files
authored
Update cirq.contrib.svg to escape < and > characters (#6055)
* Update cirq.contrib.svg to escape < and > characters * Add type: ignore for mypy
1 parent 6a97cca commit cde65be

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

cirq-core/cirq/contrib/svg/svg.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ def fixup_text(text: str):
2323
if '[cirq.VirtualTag()]' in text:
2424
# https://github.com/quantumlib/Cirq/issues/2905
2525
return text.replace('[cirq.VirtualTag()]', '')
26+
if '<' in text:
27+
return text.replace('<', '&lt;')
28+
if '>' in text:
29+
return text.replace('>', '&gt;')
2630
return text
2731

2832

cirq-core/cirq/contrib/svg/svg_test.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,22 @@ def test_empty_moments():
5757
svg_2 = circuit_to_svg(cirq.Circuit(cirq.Moment()))
5858
assert '<svg' in svg_2
5959
assert '</svg>' in svg_2
60+
61+
62+
def test_gate_with_less_greater_str():
63+
class CustomGate(cirq.Gate):
64+
def _num_qubits_(self) -> int:
65+
return 4
66+
67+
def _circuit_diagram_info_(self, _) -> cirq.CircuitDiagramInfo:
68+
return cirq.CircuitDiagramInfo(wire_symbols=['<a', '<=b', '>c', '>=d'])
69+
70+
circuit = cirq.Circuit(CustomGate().on(*cirq.LineQubit.range(4)))
71+
svg = circuit_to_svg(circuit)
72+
import IPython.display # type: ignore
73+
74+
_ = IPython.display.SVG(svg)
75+
assert '&lt;a' in svg
76+
assert '&lt;=b' in svg
77+
assert '&gt;c' in svg
78+
assert '&gt;=d' in svg

0 commit comments

Comments
 (0)