Skip to content

Commit 1cc60bf

Browse files
committed
Fix adding default attributes to subgraphs
1 parent 4c7d627 commit 1cc60bf

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

packages/viz/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Unreleased
44

5+
* Fix that default attributes were added incorrectly, and would appear on the root graph as well as subgraphs.
56
* For graph object input, fix that subgraphs without names were given the name "undefined", and reject nodes and edges without names.
67

78
## 3.22.0

packages/viz/backend/viz.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,16 +139,25 @@ Agraph_t *viz_add_subgraph(Agraph_t *g, char *name) {
139139

140140
EMSCRIPTEN_KEEPALIVE
141141
void viz_set_default_graph_attribute(Agraph_t *graph, char *name, char *value) {
142+
if (agattr(graph, AGRAPH, name, NULL) == NULL) {
143+
agattr(graph, AGRAPH, name, "");
144+
}
142145
agattr(graph, AGRAPH, name, value);
143146
}
144147

145148
EMSCRIPTEN_KEEPALIVE
146149
void viz_set_default_node_attribute(Agraph_t *graph, char *name, char *value) {
150+
if (agattr(graph, AGNODE, name, NULL) == NULL) {
151+
agattr(graph, AGNODE, name, "");
152+
}
147153
agattr(graph, AGNODE, name, value);
148154
}
149155

150156
EMSCRIPTEN_KEEPALIVE
151157
void viz_set_default_edge_attribute(Agraph_t *graph, char *name, char *value) {
158+
if (agattr(graph, AGEDGE, name, NULL) == NULL) {
159+
agattr(graph, AGEDGE, name, "");
160+
}
152161
agattr(graph, AGEDGE, name, value);
153162
}
154163

packages/viz/test/graph-objects.test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,45 @@ describe("Viz", function() {
280280
width=0.75];
281281
}
282282
}
283+
`,
284+
errors: []
285+
});
286+
});
287+
288+
it("applies subgraph attributes correctly", function() {
289+
const result = viz.render({
290+
subgraphs: [
291+
{
292+
graphAttributes: {
293+
color: "red"
294+
},
295+
nodeAttributes: {
296+
color: "green"
297+
},
298+
edgeAttributes: {
299+
color: "blue"
300+
},
301+
nodes: [
302+
{ name: "a" }
303+
]
304+
}
305+
]
306+
});
307+
308+
assert.deepStrictEqual(result, {
309+
status: "success",
310+
output: `digraph {
311+
graph [bb="0,0,54,36"];
312+
node [label="\\N"];
313+
{
314+
graph [color=red];
315+
node [color=green];
316+
edge [color=blue];
317+
a [height=0.5,
318+
pos="27,18",
319+
width=0.75];
320+
}
321+
}
283322
`,
284323
errors: []
285324
});

0 commit comments

Comments
 (0)