Skip to content

Commit b9f3b68

Browse files
authored
Merge pull request #70 from courtneycb/codeCleanUp
Cleans up code in offsetPath.js
2 parents ac2a1f9 + 354e449 commit b9f3b68

File tree

1 file changed

+82
-66
lines changed

1 file changed

+82
-66
lines changed

src/offsetPath.js

Lines changed: 82 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
'use strict';
33

44
(function () {
5-
function basicShapePolygon (input) {
5+
function basicShapePolygonParse (input) {
66
// TODO: Support the fill-rule option and %
77
var argumentList = input.split(',');
88
var coordinate = null;
@@ -34,12 +34,7 @@
3434
return {type: 'path', path: path};
3535
}
3636

37-
function basicShapeInset (input) {
38-
// WIP
39-
return null;
40-
}
41-
42-
function basicShapeCircle (input) {
37+
function basicShapeCircleParse (input) {
4338
// TODO: Need element as an argument to this function
4439
var radius;
4540
var position = /at (.*?)$/.exec(input);
@@ -74,84 +69,105 @@
7469
return {type: 'path', path: pathString};
7570
}
7671

77-
function basicShapeEllipse (input) {
72+
function basicShapeInsetParse (input) {
7873
// WIP
7974
return null;
8075
}
8176

82-
function offsetPathParse (input) {
83-
var parseAngleAsDegrees = internalScope.parseAngleAsDegrees;
84-
var isInArray = internalScope.isInArray;
85-
// https://drafts.fxtf.org/motion-1/#offset-path-property
77+
function basicShapeEllipseParse (input) {
78+
// WIP
79+
return null;
80+
}
8681

82+
function parseNone (input) {
8783
if (input === 'none') {
8884
return {type: null, angle: null, path: null};
8985
}
86+
}
9087

88+
function parseRay (input) {
89+
var isInArray = internalScope.isInArray;
90+
var parseAngleAsDegrees = internalScope.parseAngleAsDegrees;
9191
var ray = /^ray\((.*)\)$/.exec(input);
92-
var path = /^path\(['"](.*)['"]\)$/.exec(input);
93-
// TODO: For basic shape check for closing brackets
94-
var shapeType = /^[^\(]*/.exec(input);
95-
96-
if (ray !== null) {
97-
var rayInput = ray[1].split(/\s+/);
98-
if (rayInput.length > 3) {
99-
return undefined;
100-
}
92+
if (ray === null) {
93+
return undefined;
94+
}
95+
var rayInput = ray[1].split(/\s+/);
96+
if (rayInput.length > 3) {
97+
return undefined;
98+
}
99+
var result = {type: 'ray', angle: null, path: null, contain: false, size: null};
100+
var validSizes = ['closest-side', 'farthest-side', 'closest-corner', 'farthest-corner'];
101101

102-
var result = {type: 'ray', angle: null, path: null, contain: false, size: null};
103-
var validSizes = ['closest-side', 'farthest-side', 'closest-corner', 'farthest-corner'];
104-
105-
for (var i = 0; i < rayInput.length; i++) {
106-
if (rayInput[i] === 'contain') {
107-
if (result.contain) {
108-
return undefined;
109-
}
110-
result.contain = true;
111-
} else if (isInArray(validSizes, rayInput[i])) {
112-
if (result.size) {
113-
return undefined;
114-
}
115-
result.size = rayInput[i];
116-
} else {
117-
if (result.angle) {
118-
return undefined;
119-
}
120-
var rayInputDegrees = parseAngleAsDegrees(rayInput[i]);
121-
if (rayInputDegrees === null) {
122-
return undefined;
123-
}
124-
result.angle = rayInputDegrees;
102+
for (var i = 0; i < rayInput.length; i++) {
103+
if (rayInput[i] === 'contain') {
104+
if (result.contain) {
105+
return undefined;
125106
}
107+
result.contain = true;
108+
} else if (isInArray(validSizes, rayInput[i])) {
109+
if (result.size) {
110+
return undefined;
111+
}
112+
result.size = rayInput[i];
113+
} else {
114+
if (result.angle) {
115+
return undefined;
116+
}
117+
var rayInputDegrees = parseAngleAsDegrees(rayInput[i]);
118+
if (rayInputDegrees === null) {
119+
return undefined;
120+
}
121+
result.angle = rayInputDegrees;
126122
}
127-
return result;
128-
} else if (path !== null) {
129-
var pathInput = path[1];
130-
return {type: 'path', path: pathInput};
131-
} else {
132-
var basicShapes = ['inset', 'circle', 'ellipse', 'polygon'];
133-
if (!isInArray(basicShapes, shapeType[0])) {
134-
return undefined;
135-
}
136-
137-
var shapeArguments = /\(([^)]+)\)/.exec(input);
138-
139-
if (shapeType[0] === 'inset') {
140-
return basicShapeInset(shapeArguments[1]);
141-
}
123+
}
124+
return result;
125+
}
142126

143-
if (shapeType[0] === 'circle') {
144-
return basicShapeCircle(shapeArguments[1]);
145-
}
127+
function parsePath (input) {
128+
var path = /^path\(['"](.*)['"]\)$/.exec(input);
129+
if (path === null) {
130+
return undefined;
131+
}
132+
var pathInput = path[1];
133+
return {type: 'path', path: pathInput};
134+
}
146135

147-
if (shapeType[0] === 'ellipse') {
148-
return basicShapeEllipse(shapeArguments[1]);
136+
function parseShape (input) {
137+
var isInArray = internalScope.isInArray;
138+
var shapeType = /^[^\(]*/.exec(input);
139+
if (shapeType == null) {
140+
return undefined;
141+
}
142+
var basicShapes = ['inset', 'circle', 'ellipse', 'polygon'];
143+
if (!isInArray(basicShapes, shapeType[0])) {
144+
return undefined;
145+
}
146+
// TODO: For basic shape check for closing brackets
147+
var shapeArguments = /\(([^)]+)\)/.exec(input);
148+
if (shapeArguments === null) {
149+
return undefined;
150+
}
151+
var toParse = [basicShapePolygonParse, basicShapeCircleParse, basicShapeInsetParse, basicShapeEllipseParse];
152+
for (var i = 0; i < toParse.length; i++) {
153+
var result = toParse[i](shapeArguments[1]);
154+
if (result) {
155+
return result;
149156
}
157+
}
158+
return undefined;
159+
}
150160

151-
if (shapeType[0] === 'polygon') {
152-
return basicShapePolygon(shapeArguments[1]);
161+
function offsetPathParse (input) {
162+
// https://drafts.fxtf.org/motion-1/#offset-path-property
163+
var toParse = [parseNone, parseRay, parsePath, parseShape];
164+
for (var i = 0; i < toParse.length; i++) {
165+
var result = toParse[i](input);
166+
if (result) {
167+
return result;
153168
}
154169
}
170+
return undefined;
155171
}
156172

157173
function offsetPathMerge (start, end) {

0 commit comments

Comments
 (0)