Skip to content

Commit 7a7aea8

Browse files
aubreyandnturley
authored andcommitted
Fix invalid SVG output (#77)
#74 Support string type base case in onml Improve some type annotations
1 parent 86981ce commit 7a7aea8

File tree

6 files changed

+92
-75
lines changed

6 files changed

+92
-75
lines changed

built/Skin.js

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -80,42 +80,43 @@ var Skin;
8080
}
8181
Skin.findSkinType = findSkinType;
8282
function getLowPriorityAliases() {
83-
var properties = Skin.skin.find(function (el) {
84-
return el[0] === 's:properties';
85-
});
86-
// properties has no children
87-
if (properties.length < 3) {
88-
return [];
89-
}
90-
// find low priority aliases and return their values
91-
var ret = properties[2].filter(function (el) {
92-
return el[0] === 's:low_priority_alias';
93-
}).map(function (el) {
94-
return el[1].val;
83+
var ret = [];
84+
onml.t(Skin.skin, {
85+
enter: function (node) {
86+
if (node.name === 's:low_priority_alias') {
87+
ret.push(node.attr.value);
88+
}
89+
},
9590
});
9691
return ret;
9792
}
9893
Skin.getLowPriorityAliases = getLowPriorityAliases;
9994
function getProperties() {
100-
var properties = Skin.skin.find(function (el) {
101-
return el[0] === 's:properties';
102-
});
103-
var vals = _.mapValues(properties[1], function (val) {
104-
if (!isNaN(Number(val))) {
105-
return Number(val);
106-
}
107-
if (val === 'true') {
108-
return true;
109-
}
110-
if (val === 'false') {
111-
return false;
112-
}
113-
return val;
95+
var vals;
96+
onml.t(Skin.skin, {
97+
enter: function (node) {
98+
if (node.name === 's:properties') {
99+
vals = _.mapValues(node.attr, function (val) {
100+
if (!isNaN(Number(val))) {
101+
return Number(val);
102+
}
103+
if (val === 'true') {
104+
return true;
105+
}
106+
if (val === 'false') {
107+
return false;
108+
}
109+
return val;
110+
});
111+
}
112+
else if (node.name === 's:layoutEngine') {
113+
vals.layoutEngine = node.attr;
114+
}
115+
},
114116
});
115-
var layoutEngine = properties.find(function (el) {
116-
return el[0] === 's:layoutEngine';
117-
}) || {};
118-
vals.layoutEngine = layoutEngine[1];
117+
if (!vals.layoutEngine) {
118+
vals.layoutEngine = {};
119+
}
119120
return vals;
120121
}
121122
Skin.getProperties = getProperties;

built/drawModule.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,18 @@ function drawModule(g, module) {
5757
});
5858
});
5959
var svgAttrs = Skin_1.default.skin[1];
60-
var svg = Skin_1.default.skin.slice(0, 2);
6160
svgAttrs.width = g.width.toString();
6261
svgAttrs.height = g.height.toString();
63-
var styles = _.filter(Skin_1.default.skin, function (el) {
64-
return el[0] === 'style';
62+
var styles = ['style', {}, ''];
63+
onml.t(Skin_1.default.skin, {
64+
enter: function (node) {
65+
if (node.name === 'style') {
66+
styles[2] += node.full[2];
67+
}
68+
},
6569
});
66-
var elements = styles.concat(nodes).concat(lines);
67-
var ret = ['svg', svgAttrs, elements];
70+
var elements = [styles].concat(nodes, lines);
71+
var ret = ['svg', svgAttrs].concat(elements);
6872
return onml.s(ret);
6973
}
7074
exports.default = drawModule;

lib/@types/onml/index.d.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
declare module 'onml' {
22
// avoid the headache of recursive type
3+
export type Element = [
4+
string,
5+
Attributes?,
6+
string?,
7+
...Array<any>[],
8+
];
9+
310
export interface Attributes {
411
[attrName: string]: string;
512
}
6-
export type Element = [string, Attributes?, ...Array<any>[]];
13+
714
export function parse(dir: string): Element;
815
export function p(dir: string): Element;
916
export function stringify(o: Element): string;

lib/Cell.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ export default class Cell {
248248
return ret;
249249
}
250250

251-
public render(cell: ElkModel.Cell): any[] {
251+
public render(cell: ElkModel.Cell): onml.Element {
252252
const template = this.getTemplate();
253253
const tempclone = clone(template);
254254
for (const label of cell.labels) {

lib/Skin.ts

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -82,18 +82,13 @@ export namespace Skin {
8282
}
8383

8484
export function getLowPriorityAliases(): string[] {
85-
const properties = skin.find((el: onml.Element) => {
86-
return el[0] === 's:properties';
87-
});
88-
// properties has no children
89-
if (properties.length < 3) {
90-
return [];
91-
}
92-
// find low priority aliases and return their values
93-
const ret = properties[2].filter((el: onml.Element) => {
94-
return el[0] === 's:low_priority_alias';
95-
}).map((el: onml.Element) => {
96-
return el[1].val;
85+
const ret = [];
86+
onml.t(skin, {
87+
enter: (node) => {
88+
if (node.name === 's:low_priority_alias') {
89+
ret.push(node.attr.value);
90+
}
91+
},
9792
});
9893
return ret;
9994
}
@@ -102,26 +97,32 @@ export namespace Skin {
10297
}
10398

10499
export function getProperties(): SkinProperties {
105-
const properties = skin.find((el: onml.Element) => {
106-
return el[0] === 's:properties';
107-
}) as onml.Element;
108-
109-
const vals = _.mapValues(properties[1], (val: string) => {
110-
if (!isNaN(Number(val))) {
111-
return Number(val);
112-
}
113-
if (val === 'true') {
114-
return true;
115-
}
116-
if (val === 'false') {
117-
return false;
118-
}
119-
return val;
100+
let vals;
101+
onml.t(skin, {
102+
enter: (node) => {
103+
if (node.name === 's:properties') {
104+
vals = _.mapValues(node.attr, (val: string) => {
105+
if (!isNaN(Number(val))) {
106+
return Number(val);
107+
}
108+
if (val === 'true') {
109+
return true;
110+
}
111+
if (val === 'false') {
112+
return false;
113+
}
114+
return val;
115+
});
116+
} else if (node.name === 's:layoutEngine') {
117+
vals.layoutEngine = node.attr;
118+
}
119+
},
120120
});
121-
const layoutEngine = properties.find((el: onml.Element) => {
122-
return el[0] === 's:layoutEngine';
123-
}) || {};
124-
vals.layoutEngine = layoutEngine[1];
121+
122+
if (!vals.layoutEngine) {
123+
vals.layoutEngine = {};
124+
}
125+
125126
return vals;
126127
}
127128
}

lib/drawModule.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ enum WireDirection {
1212
}
1313

1414
export default function drawModule(g: ElkModel.Graph, module: FlatModule) {
15-
const nodes: any[] = module.nodes.map((n: Cell) => {
15+
const nodes: onml.Element[] = module.nodes.map((n: Cell) => {
1616
const kchild: ElkModel.Cell = _.find(g.children, (c) => c.id === n.Key);
1717
return n.render(kchild);
1818
});
1919
removeDummyEdges(g);
20-
const lines: any[] = _.flatMap(g.edges, (e: ElkModel.Edge) => {
20+
const lines: onml.Element[] = _.flatMap(g.edges, (e: ElkModel.Edge) => {
2121
const netId = ElkModel.wireNameLookup[e.id];
2222
const netName = 'net_' + netId.slice(1, netId.length - 1);
2323
return _.flatMap(e.sections, (s: ElkModel.Section) => {
@@ -56,15 +56,19 @@ export default function drawModule(g: ElkModel.Graph, module: FlatModule) {
5656
});
5757
});
5858
const svgAttrs: onml.Attributes = Skin.skin[1];
59-
const svg = Skin.skin.slice(0, 2);
6059
svgAttrs.width = g.width.toString();
6160
svgAttrs.height = g.height.toString();
6261

63-
const styles: any[] = _.filter(Skin.skin, (el) => {
64-
return el[0] === 'style';
62+
const styles: onml.Element = ['style', {}, ''];
63+
onml.t(Skin.skin, {
64+
enter: (node) => {
65+
if (node.name === 'style') {
66+
styles[2] += node.full[2];
67+
}
68+
},
6569
});
66-
const elements = styles.concat(nodes).concat(lines);
67-
const ret: onml.Element = ['svg', svgAttrs, elements];
70+
const elements: onml.Element[] = [styles, ...nodes, ...lines];
71+
const ret: onml.Element = ['svg', svgAttrs, ...elements];
6872
return onml.s(ret);
6973
}
7074

0 commit comments

Comments
 (0)