XS.xs

filter: Statementstmt 94.3% 348 / 369 Branchbran 77.3% 325 / 420 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 85.3% 673 / 789 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary blib/lib/JavaScript/Minifier/XS.pm »
1 #include <EXTERN.h>
2 #include <perl.h>
3 #include <XSUB.h>
4  
5 #include <string.h>
6 #include <strings.h>
7 #include <stdlib.h>
8 #include <ctype.h>
9  
10 /* uncomment to enable debugging output */
11 /* #define DEBUG 1 */
12  
13 /* ****************************************************************************
14 * CHARACTER CLASS METHODS
15 * ****************************************************************************
16 */
17 972 bool charIsSpace(char ch) {
18 972 ▶ if (ch == ' ') return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ch == ' ') return 1; 471 501 972
19 501 ▶ if (ch == '\t') return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ch == '\t') return 1; 3 498 501
20 498 return 0;
21 }
22 925 bool charIsEndspace(char ch) {
23 925 ▶ if (ch == '\n') return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ch == '\n') return 1; 131 794 925
24 794 ▶ if (ch == '\r') return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ch == '\r') return 1; 3 791 794
25 791 ▶ if (ch == '\f') return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ch == '\f') return 1; 0 791 791
26 791 return 0;
27 }
28 972 bool charIsWhitespace(char ch) {
29 972 ▶ return charIsSpace(ch) || charIsEndspace(ch);
Branch: 2 branches
BranchTrueFalseTotal
return charIsSpace(ch) || charIsEndspace(ch); 498 474 972
return charIsSpace(ch) || charIsEndspace(ch); 117 381 498
30 }
31 776 bool charIsIdentifier(char ch) {
32 776 ▶ if ((ch >= 'a') && (ch <= 'z')) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if ((ch >= 'a') && (ch <= 'z')) return 1; 521 255 776
if ((ch >= 'a') && (ch <= 'z')) return 1; 507 14 521
33 269 ▶ if ((ch >= 'A') && (ch <= 'Z')) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if ((ch >= 'A') && (ch <= 'Z')) return 1; 29 240 269
if ((ch >= 'A') && (ch <= 'Z')) return 1; 1 28 29
34 268 ▶ if ((ch >= '0') && (ch <= '9')) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if ((ch >= '0') && (ch <= '9')) return 1; 123 145 268
if ((ch >= '0') && (ch <= '9')) return 1; 16 107 123
35 252 ▶ if (ch == '_') return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ch == '_') return 1; 7 245 252
36 245 ▶ if (ch == '$') return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ch == '$') return 1; 2 243 245
37 243 ▶ if (ch == '\\') return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ch == '\\') return 1; 0 243 243
38 243 ▶ if (ch > 126) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ch > 126) return 1; 0 243 243
39 243 return 0;
40 }
41 305 bool charIsInfix(char ch) {
42 /* EOL characters before+after these characters can be removed */
43 305 ▶ if (ch == ',') return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ch == ',') return 1; 12 293 305
44 293 ▶ if (ch == ';') return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ch == ';') return 1; 97 196 293
45 196 ▶ if (ch == ':') return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ch == ':') return 1; 0 196 196
46 196 ▶ if (ch == '=') return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ch == '=') return 1; 70 126 196
47 126 ▶ if (ch == '&') return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ch == '&') return 1; 0 126 126
48 126 ▶ if (ch == '%') return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ch == '%') return 1; 0 126 126
49 126 ▶ if (ch == '*') return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ch == '*') return 1; 0 126 126
50 126 ▶ if (ch == '<') return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ch == '<') return 1; 0 126 126
51 126 ▶ if (ch == '>') return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ch == '>') return 1; 0 126 126
52 126 ▶ if (ch == '?') return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ch == '?') return 1; 0 126 126
53 126 ▶ if (ch == '|') return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ch == '|') return 1; 0 126 126
54 126 ▶ if (ch == '\n') return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ch == '\n') return 1; 0 126 126
55 126 return 0;
56 }
57 222 bool charIsPrefix(char ch) {
58 /* EOL characters after these characters can be removed */
59 222 ▶ if (ch == '{') return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ch == '{') return 1; 14 208 222
60 208 ▶ if (ch == '(') return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ch == '(') return 1; 23 185 208
61 185 ▶ if (ch == '[') return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ch == '[') return 1; 5 180 185
62 180 ▶ if (ch == '!') return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ch == '!') return 1; 0 180 180
63 180 return charIsInfix(ch);
64 }
65 166 bool charIsPostfix(char ch) {
66 /* EOL characters before these characters can be removed */
67 166 ▶ if (ch == '}') return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ch == '}') return 1; 13 153 166
68 153 ▶ if (ch == ')') return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ch == ')') return 1; 25 128 153
69 128 ▶ if (ch == ']') return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ch == ']') return 1; 3 125 128
70 125 return charIsInfix(ch);
71 }
72  
73 /* ****************************************************************************
74 * TYPE DEFINITIONS
75 * ****************************************************************************
76 */
77 typedef enum {
78 NODE_EMPTY,
79 NODE_WHITESPACE,
80 NODE_BLOCKCOMMENT,
81 NODE_LINECOMMENT,
82 NODE_IDENTIFIER,
83 NODE_LITERAL,
84 NODE_SIGIL
85 } NodeType;
86 #ifdef DEBUG
87 static char* strNodeTypes[] = {
88 "empty",
89 "whitespace",
90 "block comment",
91 "line comment",
92 "identifier",
93 "literal",
94 "sigil"
95 };
96 #endif
97  
98 struct _Node;
99 typedef struct _Node Node;
100 struct _Node {
101 /* linked list pointers */
102 Node* prev;
103 Node* next;
104 /* node internals */
105 char* contents;
106 size_t length;
107 NodeType type;
108 };
109  
110 #define NODE_SET_SIZE 50000
111  
112 struct _NodeSet;
113 typedef struct _NodeSet NodeSet;
114 struct _NodeSet {
115 /* link to next NodeSet */
116 NodeSet* next;
117 /* Nodes in this Set */
118 Node nodes[NODE_SET_SIZE];
119 size_t next_node;
120 };
121  
122 typedef struct {
123 /* singly linked list of NodeSets */
124 NodeSet* head_set;
125 NodeSet* tail_set;
126 /* linked list pointers */
127 Node* head;
128 Node* tail;
129 /* doc internals */
130 const char* buffer;
131 size_t length;
132 size_t offset;
133 } JsDoc;
134  
135  
136 /* ****************************************************************************
137 * NODE CHECKING MACROS/FUNCTIONS
138 * ****************************************************************************
139 */
140  
141 /* checks to see if the node is the given string, case INSENSITIVELY */
142 2 bool nodeEquals(Node* node, const char* string) {
143 2 return (strcasecmp(node->contents, string) == 0);
144 }
145  
146 /* checks to see if the node contains the given string, case INSENSITIVELY */
147 22 bool nodeContains(Node* node, const char* string) {
148 22 const char* haystack = node->contents;
149 22 size_t len = strlen(string);
150 22 char ul_start[2] = { tolower(*string), toupper(*string) };
151  
152 /* if node is shorter we know we're not going to have a match */
153 22 ▶ if (len > node->length)
Branch: 1 branch
BranchTrueFalseTotal
if (len > node->length) 2 20 22
154 2 return 0;
155  
156 /* find the needle in the haystack */
157 40 ▶ while (haystack && *haystack) {
Branch: 2 branches
BranchTrueFalseTotal
while (haystack && *haystack) { 40 0 40
while (haystack && *haystack) { 40 0 40
158 /* find first char of needle */
159 40 haystack = strpbrk( haystack, ul_start );
160 40 ▶ if (haystack == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (haystack == NULL) 17 23 40
161 17 return 0;
162 /* check if the rest matches */
163 23 ▶ if (strncasecmp(haystack, string, len) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (strncasecmp(haystack, string, len) == 0) 3 20 23
164 3 return 1;
165 /* nope, move onto next character in the haystack */
166 20 haystack ++;
167 }
168  
169 /* no match */
170 0 return 0;
171 }
172  
173 /* checks to see if the node begins with the given string, case INSENSITIVELY
174 */
175 35 bool nodeBeginsWith(Node* node, const char* string) {
176 35 size_t len = strlen(string);
177 35 ▶ if (len > node->length)
Branch: 1 branch
BranchTrueFalseTotal
if (len > node->length) 0 35 35
178 0 return 0;
179 35 return (strncasecmp(node->contents, string, len) == 0);
180 }
181  
182 /* checks to see if the node ends with the given string, case INSENSITIVELY */
183 5 bool nodeEndsWith(Node* node, const char* string) {
184 5 size_t len = strlen(string);
185 5 size_t off = node->length - len;
186 5 ▶ if (len > node->length)
Branch: 1 branch
BranchTrueFalseTotal
if (len > node->length) 0 5 5
187 0 return 0;
188 5 return (strncasecmp(node->contents+off, string, len) == 0);
189 }
190  
191 /* macros to help see what kind of node we've got */
192 #define nodeIsWHITESPACE(node) ((node->type == NODE_WHITESPACE))
193 #define nodeIsBLOCKCOMMENT(node) ((node->type == NODE_BLOCKCOMMENT))
194 #define nodeIsLINECOMMENT(node) ((node->type == NODE_LINECOMMENT))
195 #define nodeIsIDENTIFIER(node) ((node->type == NODE_IDENTIFIER))
196 #define nodeIsLITERAL(node) ((node->type == NODE_LITERAL))
197 #define nodeIsSIGIL(node) ((node->type == NODE_SIGIL))
198  
199 #define nodeIsEMPTY(node) ((node->type == NODE_EMPTY) || (node->length==0) || (node->contents=NULL))
200 #define nodeIsCOMMENT(node) (nodeIsBLOCKCOMMENT(node) || nodeIsLINECOMMENT(node))
201 #define nodeIsIECONDITIONALBLOCKCOMMENT(node) (nodeIsBLOCKCOMMENT(node) && nodeBeginsWith(node,"/*@") && nodeEndsWith(node,"@*/"))
202 #define nodeIsIECONDITIONALLINECOMMENT(node) (nodeIsLINECOMMENT(node) && nodeBeginsWith(node,"//@"))
203 #define nodeIsIECONDITIONALCOMMENT(node) (nodeIsIECONDITIONALBLOCKCOMMENT(node) || nodeIsIECONDITIONALLINECOMMENT(node))
204 #define nodeIsPREFIXSIGIL(node) (nodeIsSIGIL(node) && charIsPrefix(node->contents[0]))
205 #define nodeIsPOSTFIXSIGIL(node) (nodeIsSIGIL(node) && charIsPostfix(node->contents[0]))
206 #define nodeIsENDSPACE(node) (nodeIsWHITESPACE(node) && charIsEndspace(node->contents[0]))
207 #define nodeIsCHAR(node,ch) ((node->contents[0]==ch) && (node->length==1))
208  
209 /* ****************************************************************************
210 * NODE MANIPULATION FUNCTIONS
211 * ****************************************************************************
212 */
213 /* allocates a new node */
214 438 Node* JsAllocNode(JsDoc* doc) {
215 Node* node;
216 438 NodeSet* set = doc->tail_set;
217  
218 /* if our current NodeSet is full, allocate a new NodeSet */
219 438 ▶ if (set->next_node >= NODE_SET_SIZE) {
Branch: 1 branch
BranchTrueFalseTotal
if (set->next_node >= NODE_SET_SIZE) { 0 438 438
220 NodeSet* next_set;
221 0 Newz(0, next_set, 1, NodeSet);
222 0 set->next = next_set;
223 0 doc->tail_set = next_set;
224 0 set = next_set;
225 }
226  
227 /* grab the next Node out of the NodeSet */
228 438 node = set->nodes + set->next_node;
229 438 set->next_node ++;
230  
231 /* initialize the node */
232 438 node->prev = NULL;
233 438 node->next = NULL;
234 438 node->contents = NULL;
235 438 node->length = 0;
236 438 node->type = NODE_EMPTY;
237 438 return node;
238 }
239  
240 /* clears the contents of a node */
241 438 void JsClearNodeContents(Node* node) {
242 438 ▶ if (node->contents)
Branch: 1 branch
BranchTrueFalseTotal
if (node->contents) 0 438 438
243 0 Safefree(node->contents);
244 438 node->contents = NULL;
245 438 node->length = 0;
246 438 }
247  
248 /* sets the contents of a node */
249 440 void JsSetNodeContents(Node* node, const char* string, size_t len) {
250 /* if the buffer is already big enough, just overwrite it */
251 440 ▶ if (node->length >= len) {
Branch: 1 branch
BranchTrueFalseTotal
if (node->length >= len) { 2 438 440
252 2 memcpy( node->contents, string, len );
253 2 node->contents[len] = '\0';
254 2 node->length = len;
255 }
256 /* otherwise free the buffer, allocate a new one, and copy it in */
257 else {
258 438 JsClearNodeContents(node);
259 438 node->length = len;
260 /* allocate string, fill with NULLs, and copy */
261 438 Newz(0, node->contents, (len+1), char);
262 438 memcpy( node->contents, string, len );
263 }
264 440 }
265  
266 /* removes the node from the list and discards it entirely */
267 140 void JsDiscardNode(Node* node) {
268 140 ▶ if (node->prev)
Branch: 1 branch
BranchTrueFalseTotal
if (node->prev) 122 18 140
269 122 node->prev->next = node->next;
270 140 ▶ if (node->next)
Branch: 1 branch
BranchTrueFalseTotal
if (node->next) 125 15 140
271 125 node->next->prev = node->prev;
272 140 }
273  
274 /* appends the node to the given element */
275 405 void JsAppendNode(Node* element, Node* node) {
276 405 ▶ if (element->next)
Branch: 1 branch
BranchTrueFalseTotal
if (element->next) 0 405 405
277 0 element->next->prev = node;
278 405 node->next = element->next;
279 405 node->prev = element;
280 405 element->next = node;
281 405 }
282  
283 /* collapses a node to a single whitespace character */
284 157 void JsCollapseNodeToWhitespace(Node* node) {
285 157 ▶ if (node->contents) {
Branch: 1 branch
BranchTrueFalseTotal
if (node->contents) { 157 0 157
286 157 node->length = 1;
287 157 node->contents[1] = '\0';
288 }
289 157 }
290  
291 /* ****************************************************************************
292 * TOKENIZING FUNCTIONS
293 * ****************************************************************************
294 */
295  
296 /* extracts a quoted literal string */
297 20 void _JsExtractLiteral(JsDoc* doc, Node* node) {
298 20 const char* buf = doc->buffer;
299 20 size_t offset = doc->offset;
300 20 char delimiter = buf[offset];
301 20 bool in_char_class = 0;
302 /* skip start of literal */
303 20 offset ++;
304 /* search for end of literal */
305 331 ▶ while (offset < doc->length) {
Branch: 1 branch
BranchTrueFalseTotal
while (offset < doc->length) { 331 0 331
306 331 ▶ if (buf[offset] == '\\') {
Branch: 1 branch
BranchTrueFalseTotal
if (buf[offset] == '\\') { 4 327 331
307 /* escaped character; skip */
308 4 offset ++;
309 }
310 else {
311 /* if in a regex, track if we're in a character class */
312 327 ▶ if (delimiter == '/') {
Branch: 1 branch
BranchTrueFalseTotal
if (delimiter == '/') { 42 285 327
313 42 ▶ if ((buf[offset] == '[') && !in_char_class) {
Branch: 2 branches
BranchTrueFalseTotal
if ((buf[offset] == '[') && !in_char_class) { 2 40 42
if ((buf[offset] == '[') && !in_char_class) { 2 0 2
314 2 in_char_class = 1;
315 }
316 42 ▶ if ((buf[offset] == ']') && in_char_class) {
Branch: 2 branches
BranchTrueFalseTotal
if ((buf[offset] == ']') && in_char_class) { 2 40 42
if ((buf[offset] == ']') && in_char_class) { 2 0 2
317 2 in_char_class = 0;
318 }
319 }
320 /* if we have found the end of the literal, store it */
321 327 ▶ if ((buf[offset] == delimiter) && !in_char_class) {
Branch: 2 branches
BranchTrueFalseTotal
if ((buf[offset] == delimiter) && !in_char_class) { 21 306 327
if ((buf[offset] == delimiter) && !in_char_class) { 20 1 21
322 20 const char* start = buf + doc->offset;
323 20 size_t length = offset - doc->offset + 1;
324 20 JsSetNodeContents(node, start, length);
325 20 node->type = NODE_LITERAL;
326 20 return;
327 }
328 }
329 /* move onto next character */
330 311 offset ++;
331 }
332 0 croak( "unterminated quoted string literal" );
333 }
334  
335 /* extracts a block comment */
336 15 void _JsExtractBlockComment(JsDoc* doc, Node* node) {
337 15 const char* buf = doc->buffer;
338 15 size_t offset = doc->offset;
339  
340 /* skip start of comment */
341 15 offset ++; /* skip "/" */
342 15 offset ++; /* skip "*" */
343  
344 /* search for end of comment block */
345 368 ▶ while (offset < doc->length) {
Branch: 1 branch
BranchTrueFalseTotal
while (offset < doc->length) { 368 0 368
346 368 ▶ if (buf[offset] == '*') {
Branch: 1 branch
BranchTrueFalseTotal
if (buf[offset] == '*') { 15 353 368
347 15 ▶ if (buf[offset+1] == '/') {
Branch: 1 branch
BranchTrueFalseTotal
if (buf[offset+1] == '/') { 15 0 15
348 15 const char* start = buf + doc->offset;
349 15 size_t length = offset - doc->offset + 2;
350 15 JsSetNodeContents(node, start, length);
351 15 node->type = NODE_BLOCKCOMMENT;
352 15 return;
353 }
354 }
355 /* move onto next character */
356 353 offset ++;
357 }
358  
359 0 croak( "unterminated block comment" );
360 }
361  
362 /* extracts a line comment */
363 8 void _JsExtractLineComment(JsDoc* doc, Node* node) {
364 8 const char* buf = doc->buffer;
365 8 size_t offset = doc->offset;
366  
367 /* skip start of comment */
368 8 offset ++; /* skip "/" */
369 8 offset ++; /* skip "/" */
370  
371 /* search for end of line */
372 332 ▶ while ((offset < doc->length) && !charIsEndspace(buf[offset]))
Branch: 2 branches
BranchTrueFalseTotal
while ((offset < doc->length) && !charIsEndspace(buf[offset])) 330 2 332
while ((offset < doc->length) && !charIsEndspace(buf[offset])) 324 6 330
373 324 offset ++;
374  
375 /* found it ! */
376 {
377 8 const char* start = buf + doc->offset;
378 8 size_t length = offset - doc->offset;
379 8 JsSetNodeContents(node, start, length);
380 8 node->type = NODE_LINECOMMENT;
381 }
382 8 }
383  
384 /* extracts a run of whitespace characters */
385 157 void _JsExtractWhitespace(JsDoc* doc, Node* node) {
386 157 const char* buf = doc->buffer;
387 157 size_t offset = doc->offset;
388 591 ▶ while ((offset < doc->length) && charIsWhitespace(buf[offset]))
Branch: 2 branches
BranchTrueFalseTotal
while ((offset < doc->length) && charIsWhitespace(buf[offset])) 579 12 591
while ((offset < doc->length) && charIsWhitespace(buf[offset])) 434 145 579
389 434 offset ++;
390 157 JsSetNodeContents(node, doc->buffer+doc->offset, offset-doc->offset);
391 157 node->type = NODE_WHITESPACE;
392 157 }
393  
394 /* extracts an identifier */
395 105 void _JsExtractIdentifier(JsDoc* doc, Node* node) {
396 105 const char* buf = doc->buffer;
397 105 size_t offset = doc->offset;
398 532 ▶ while ((offset < doc->length) && charIsIdentifier(buf[offset]))
Branch: 2 branches
BranchTrueFalseTotal
while ((offset < doc->length) && charIsIdentifier(buf[offset])) 531 1 532
while ((offset < doc->length) && charIsIdentifier(buf[offset])) 427 104 531
399 427 offset ++;
400 105 JsSetNodeContents(node, doc->buffer+doc->offset, offset-doc->offset);
401 105 node->type = NODE_IDENTIFIER;
402 105 }
403  
404 /* extracts a -single- symbol/sigil */
405 133 void _JsExtractSigil(JsDoc* doc, Node* node) {
406 133 JsSetNodeContents(node, doc->buffer+doc->offset, 1);
407 133 node->type = NODE_SIGIL;
408 133 }
409  
410 /* tokenizes the given string and returns the list of nodes */
411 34 Node* JsTokenizeString(JsDoc* doc, const char* string) {
412 /* parse the JS */
413 472 ▶ while ((doc->offset < doc->length) && (doc->buffer[doc->offset])) {
Branch: 2 branches
BranchTrueFalseTotal
while ((doc->offset < doc->length) && (doc->buffer[doc->offset])) { 438 34 472
while ((doc->offset < doc->length) && (doc->buffer[doc->offset])) { 438 0 438
414 /* allocate a new node */
415 438 Node* node = JsAllocNode(doc);
416 438 ▶ if (!doc->head)
Branch: 1 branch
BranchTrueFalseTotal
if (!doc->head) 33 405 438
417 33 doc->head = node;
418 438 ▶ if (!doc->tail)
Branch: 1 branch
BranchTrueFalseTotal
if (!doc->tail) 33 405 438
419 33 doc->tail = node;
420  
421 /* parse the next node out of the JS */
422 438 ▶ if (doc->buffer[doc->offset] == '/') {
Branch: 1 branch
BranchTrueFalseTotal
if (doc->buffer[doc->offset] == '/') { 34 404 438
423 34 ▶ if (doc->buffer[doc->offset+1] == '*')
Branch: 1 branch
BranchTrueFalseTotal
if (doc->buffer[doc->offset+1] == '*') 15 19 34
424 15 _JsExtractBlockComment(doc, node);
425 19 ▶ else if (doc->buffer[doc->offset+1] == '/')
Branch: 1 branch
BranchTrueFalseTotal
else if (doc->buffer[doc->offset+1] == '/') 8 11 19
426 8 _JsExtractLineComment(doc, node);
427 else {
428 /* could be "division" or "regexp", but need to know more about
429 * our context...
430 */
431 11 Node* last = doc->tail;
432 11 char ch = 0;
433  
434 /* find last non-whitespace, non-comment node */
435 26 ▶ while (nodeIsWHITESPACE(last) || nodeIsCOMMENT(last))
Branch: 3 branches
BranchTrueFalseTotal
while (nodeIsWHITESPACE(last) || nodeIsCOMMENT(last)) 12 14 26
while (nodeIsWHITESPACE(last) || nodeIsCOMMENT(last)) 0 14 14
while (nodeIsWHITESPACE(last) || nodeIsCOMMENT(last)) 3 11 14
436 15 last = last->prev;
437  
438 11 ch = last->contents[last->length-1];
439  
440 /* see if we're "division" or "regexp" */
441 11 ▶ if (nodeIsIDENTIFIER(last) && nodeEquals(last, "return")) {
Branch: 2 branches
BranchTrueFalseTotal
if (nodeIsIDENTIFIER(last) && nodeEquals(last, "return")) { 2 9 11
if (nodeIsIDENTIFIER(last) && nodeEquals(last, "return")) { 1 1 2
442 /* returning a regexp from a function */
443 1 _JsExtractLiteral(doc, node);
444 }
445 10 ▶ else if (ch && ((ch == ')') || (ch == '.') || (ch == ']') || (charIsIdentifier(ch)))) {
Branch: 5 branches
BranchTrueFalseTotal
else if (ch && ((ch == ')') || (ch == '.') || (ch == ']') || (charIsIdentifier(ch)))) { 10 0 10
else if (ch && ((ch == ')') || (ch == '.') || (ch == ']') || (charIsIdentifier(ch)))) { 10 0 10
else if (ch && ((ch == ')') || (ch == '.') || (ch == ']') || (charIsIdentifier(ch)))) { 10 0 10
else if (ch && ((ch == ')') || (ch == '.') || (ch == ']') || (charIsIdentifier(ch)))) { 9 1 10
else if (ch && ((ch == ')') || (ch == '.') || (ch == ']') || (charIsIdentifier(ch)))) { 1 8 9
446 /* looks like an identifier; guess its division */
447 2 _JsExtractSigil(doc, node);
448 }
449 else {
450 /* presume its a regexp */
451 8 _JsExtractLiteral(doc, node);
452 }
453 }
454 }
455 404 ▶ else if ((doc->buffer[doc->offset] == '"') || (doc->buffer[doc->offset] == '\'') || (doc->buffer[doc->offset] == '`'))
Branch: 3 branches
BranchTrueFalseTotal
else if ((doc->buffer[doc->offset] == '"') || (doc->buffer[doc->offset] == '\'') || (doc->buffer[doc->offset] == '`')) 396 8 404
else if ((doc->buffer[doc->offset] == '"') || (doc->buffer[doc->offset] == '\'') || (doc->buffer[doc->offset] == '`')) 395 1 396
else if ((doc->buffer[doc->offset] == '"') || (doc->buffer[doc->offset] == '\'') || (doc->buffer[doc->offset] == '`')) 2 393 395
456 11 _JsExtractLiteral(doc, node);
457 393 ▶ else if (charIsWhitespace(doc->buffer[doc->offset]))
Branch: 1 branch
BranchTrueFalseTotal
else if (charIsWhitespace(doc->buffer[doc->offset])) 157 236 393
458 157 _JsExtractWhitespace(doc, node);
459 236 ▶ else if (charIsIdentifier(doc->buffer[doc->offset]))
Branch: 1 branch
BranchTrueFalseTotal
else if (charIsIdentifier(doc->buffer[doc->offset])) 105 131 236
460 105 _JsExtractIdentifier(doc, node);
461 else
462 131 _JsExtractSigil(doc, node);
463  
464 /* move ahead to the end of the parsed node */
465 438 doc->offset += node->length;
466  
467 /* add the node to our list of nodes */
468 438 ▶ if (node != doc->tail)
Branch: 1 branch
BranchTrueFalseTotal
if (node != doc->tail) 405 33 438
469 405 JsAppendNode(doc->tail, node);
470 438 doc->tail = node;
471  
472 /* some debugging info */
473 #ifdef DEBUG
474 {
475 int idx;
476 printf("----------------------------------------------------------------\n");
477 printf("%s: [%s]\n", strNodeTypes[node->type], node->contents);
478 printf("next: [");
479 for (idx=0; idx<=10; idx++) {
480 if ((doc->offset+idx) >= doc->length) break;
481 if (!doc->buffer[doc->offset+idx]) break;
482 printf("%c", doc->buffer[doc->offset+idx]);
483 }
484 printf("]\n");
485 }
486 #endif
487 }
488  
489 /* return the node list */
490 34 return doc->head;
491 }
492  
493 /* ****************************************************************************
494 * MINIFICATION FUNCTIONS
495 * ****************************************************************************
496 */
497  
498 /* collapses all of the nodes to their shortest possible representation */
499 33 void JsCollapseNodes(Node* curr) {
500 471 ▶ while (curr) {
Branch: 1 branch
BranchTrueFalseTotal
while (curr) { 438 33 471
501 438 Node* next = curr->next;
502 438 switch (curr->type) {
503 157 case NODE_WHITESPACE:
504 /* all WS gets collapsed */
505 157 JsCollapseNodeToWhitespace(curr);
506 157 break;
507 15 case NODE_BLOCKCOMMENT:
508 /* IE Conditional Compilation comments do not get collapsed */
509 15 ▶ if (nodeIsIECONDITIONALBLOCKCOMMENT(curr)) {
Branch: 3 branches
BranchTrueFalseTotal
if (nodeIsIECONDITIONALBLOCKCOMMENT(curr)) { 15 0 15
if (nodeIsIECONDITIONALBLOCKCOMMENT(curr)) { 2 13 15
if (nodeIsIECONDITIONALBLOCKCOMMENT(curr)) { 1 1 2
510 1 break;
511 }
512 /* block comments get collapsed to WS if that's a side-affect
513 * of their placement in the JS document.
514 */
515 {
516 14 bool convert_to_ws = 0;
517 /* find surrounding non-WS nodes */
518 14 Node* nonws_prev = curr->prev;
519 14 Node* nonws_next = curr->next;
520 25 ▶ while (nonws_prev && nodeIsWHITESPACE(nonws_prev))
Branch: 2 branches
BranchTrueFalseTotal
while (nonws_prev && nodeIsWHITESPACE(nonws_prev)) 21 4 25
while (nonws_prev && nodeIsWHITESPACE(nonws_prev)) 11 10 21
521 11 nonws_prev = nonws_prev->prev;
522 25 ▶ while (nonws_next && nodeIsWHITESPACE(nonws_next))
Branch: 2 branches
BranchTrueFalseTotal
while (nonws_next && nodeIsWHITESPACE(nonws_next)) 22 3 25
while (nonws_next && nodeIsWHITESPACE(nonws_next)) 11 11 22
523 11 nonws_next = nonws_next->next;
524 /* check what we're between... */
525 14 ▶ if (nonws_prev && nonws_next) {
Branch: 2 branches
BranchTrueFalseTotal
if (nonws_prev && nonws_next) { 10 4 14
if (nonws_prev && nonws_next) { 9 1 10
526 /* between identifiers? convert to WS */
527 9 ▶ if (nodeIsIDENTIFIER(nonws_prev) && nodeIsIDENTIFIER(nonws_next))
Branch: 2 branches
BranchTrueFalseTotal
if (nodeIsIDENTIFIER(nonws_prev) && nodeIsIDENTIFIER(nonws_next)) 1 8 9
if (nodeIsIDENTIFIER(nonws_prev) && nodeIsIDENTIFIER(nonws_next)) 0 1 1
528 0 convert_to_ws = 1;
529 /* between possible pre/post increment? convert to WS */
530 9 ▶ if (nodeIsCHAR(nonws_prev,'-') && nodeIsCHAR(nonws_next,'-'))
Branch: 4 branches
BranchTrueFalseTotal
if (nodeIsCHAR(nonws_prev,'-') && nodeIsCHAR(nonws_next,'-')) 2 7 9
if (nodeIsCHAR(nonws_prev,'-') && nodeIsCHAR(nonws_next,'-')) 2 0 2
if (nodeIsCHAR(nonws_prev,'-') && nodeIsCHAR(nonws_next,'-')) 1 1 2
if (nodeIsCHAR(nonws_prev,'-') && nodeIsCHAR(nonws_next,'-')) 1 0 1
531 1 convert_to_ws = 1;
532 9 ▶ if (nodeIsCHAR(nonws_prev,'+') && nodeIsCHAR(nonws_next,'+'))
Branch: 4 branches
BranchTrueFalseTotal
if (nodeIsCHAR(nonws_prev,'+') && nodeIsCHAR(nonws_next,'+')) 2 7 9
if (nodeIsCHAR(nonws_prev,'+') && nodeIsCHAR(nonws_next,'+')) 2 0 2
if (nodeIsCHAR(nonws_prev,'+') && nodeIsCHAR(nonws_next,'+')) 1 1 2
if (nodeIsCHAR(nonws_prev,'+') && nodeIsCHAR(nonws_next,'+')) 1 0 1
533 1 convert_to_ws = 1;
534 }
535 /* convert to WS */
536 14 ▶ if (convert_to_ws) {
Branch: 1 branch
BranchTrueFalseTotal
if (convert_to_ws) { 2 12 14
537 2 JsSetNodeContents(curr," ",1);
538 2 curr->type = NODE_WHITESPACE;
539 }
540 }
541 14 break;
542 266 default:
543 266 break;
544 }
545 438 curr = next;
546 }
547 33 }
548  
549 /* checks to see whether we can prune the given node from the list.
550 *
551 * THIS is the function that controls the bulk of the minification process.
552 */
553 enum {
554 PRUNE_NO,
555 PRUNE_PREVIOUS,
556 PRUNE_CURRENT,
557 PRUNE_NEXT
558 };
559 463 int JsCanPrune(Node* node) {
560 463 Node* prev = node->prev;
561 463 Node* next = node->next;
562  
563 463 switch (node->type) {
564 0 case NODE_EMPTY:
565 /* prune empty nodes */
566 0 return PRUNE_CURRENT;
567 66 case NODE_WHITESPACE:
568 /* multiple whitespace gets pruned to preserve endspace */
569 66 ▶ if (prev && nodeIsENDSPACE(prev))
Branch: 3 branches
BranchTrueFalseTotal
if (prev && nodeIsENDSPACE(prev)) 52 14 66
if (prev && nodeIsENDSPACE(prev)) 2 50 52
if (prev && nodeIsENDSPACE(prev)) 2 0 2
570 2 return PRUNE_CURRENT;
571 64 ▶ if (prev && nodeIsWHITESPACE(prev))
Branch: 2 branches
BranchTrueFalseTotal
if (prev && nodeIsWHITESPACE(prev)) 50 14 64
if (prev && nodeIsWHITESPACE(prev)) 0 50 50
572 0 return PRUNE_PREVIOUS;
573 /* leading whitespace gets pruned */
574 64 ▶ if (!prev)
Branch: 1 branch
BranchTrueFalseTotal
if (!prev) 14 50 64
575 14 return PRUNE_CURRENT;
576 /* trailing whitespace gets pruned */
577 50 ▶ if (!next)
Branch: 1 branch
BranchTrueFalseTotal
if (!next) 7 43 50
578 7 return PRUNE_CURRENT;
579 /* keep all other whitespace */
580 43 return PRUNE_NO;
581 14 case NODE_BLOCKCOMMENT:
582 /* keep comments that contain the word "copyright" */
583 14 ▶ if (nodeContains(node, "copyright"))
Branch: 1 branch
BranchTrueFalseTotal
if (nodeContains(node, "copyright")) 1 13 14
584 1 return PRUNE_NO;
585 /* keep comments that are for IE Conditional Compilation */
586 13 ▶ if (nodeIsIECONDITIONALBLOCKCOMMENT(node))
Branch: 3 branches
BranchTrueFalseTotal
if (nodeIsIECONDITIONALBLOCKCOMMENT(node)) 13 0 13
if (nodeIsIECONDITIONALBLOCKCOMMENT(node)) 3 10 13
if (nodeIsIECONDITIONALBLOCKCOMMENT(node)) 2 1 3
587 2 return PRUNE_NO;
588 /* block comments get pruned */
589 11 return PRUNE_CURRENT;
590 8 case NODE_LINECOMMENT:
591 /* keep comments that contain the word "copyright" */
592 8 ▶ if (nodeContains(node, "copyright"))
Branch: 1 branch
BranchTrueFalseTotal
if (nodeContains(node, "copyright")) 2 6 8
593 2 return PRUNE_NO;
594 /* keep comments that are for IE Conditional Compilation */
595 6 ▶ if (nodeIsIECONDITIONALLINECOMMENT(node))
Branch: 2 branches
BranchTrueFalseTotal
if (nodeIsIECONDITIONALLINECOMMENT(node)) 6 0 6
if (nodeIsIECONDITIONALLINECOMMENT(node)) 0 6 6
596 0 return PRUNE_NO;
597 /* line comments get pruned */
598 6 return PRUNE_CURRENT;
599 133 case NODE_IDENTIFIER:
600 /* remove whitespace (but NOT endspace) after identifiers, provided
601 * that next thing is -NOT- another identifier
602 */
603 133 ▶ if (next && nodeIsWHITESPACE(next) && !nodeIsENDSPACE(next) && next->next && !nodeIsIDENTIFIER(next->next))
Branch: 6 branches
BranchTrueFalseTotal
if (next && nodeIsWHITESPACE(next) && !nodeIsENDSPACE(next) && next->next && !nodeIsIDENTIFIER(next->next)) 132 1 133
if (next && nodeIsWHITESPACE(next) && !nodeIsENDSPACE(next) && next->next && !nodeIsIDENTIFIER(next->next)) 58 74 132
if (next && nodeIsWHITESPACE(next) && !nodeIsENDSPACE(next) && next->next && !nodeIsIDENTIFIER(next->next)) 58 0 58
if (next && nodeIsWHITESPACE(next) && !nodeIsENDSPACE(next) && next->next && !nodeIsIDENTIFIER(next->next)) 58 0 58
if (next && nodeIsWHITESPACE(next) && !nodeIsENDSPACE(next) && next->next && !nodeIsIDENTIFIER(next->next)) 58 0 58
if (next && nodeIsWHITESPACE(next) && !nodeIsENDSPACE(next) && next->next && !nodeIsIDENTIFIER(next->next)) 27 31 58
604 27 return PRUNE_NEXT;
605 /* keep all identifiers */
606 106 return PRUNE_NO;
607 20 case NODE_LITERAL:
608 /* keep all literals */
609 20 return PRUNE_NO;
610 222 case NODE_SIGIL:
611 /* remove whitespace after "prefix" sigils */
612 222 ▶ if (nodeIsPREFIXSIGIL(node) && next && nodeIsWHITESPACE(next))
Branch: 4 branches
BranchTrueFalseTotal
if (nodeIsPREFIXSIGIL(node) && next && nodeIsWHITESPACE(next)) 222 0 222
if (nodeIsPREFIXSIGIL(node) && next && nodeIsWHITESPACE(next)) 153 69 222
if (nodeIsPREFIXSIGIL(node) && next && nodeIsWHITESPACE(next)) 133 20 153
if (nodeIsPREFIXSIGIL(node) && next && nodeIsWHITESPACE(next)) 56 77 133
613 56 return PRUNE_NEXT;
614 /* remove whitespace before "postfix" sigils */
615 166 ▶ if (nodeIsPOSTFIXSIGIL(node) && prev && nodeIsWHITESPACE(prev) && prev->prev && !nodeIsLINECOMMENT(prev->prev))
Branch: 6 branches
BranchTrueFalseTotal
if (nodeIsPOSTFIXSIGIL(node) && prev && nodeIsWHITESPACE(prev) && prev->prev && !nodeIsLINECOMMENT(prev->prev)) 166 0 166
if (nodeIsPOSTFIXSIGIL(node) && prev && nodeIsWHITESPACE(prev) && prev->prev && !nodeIsLINECOMMENT(prev->prev)) 109 57 166
if (nodeIsPOSTFIXSIGIL(node) && prev && nodeIsWHITESPACE(prev) && prev->prev && !nodeIsLINECOMMENT(prev->prev)) 105 4 109
if (nodeIsPOSTFIXSIGIL(node) && prev && nodeIsWHITESPACE(prev) && prev->prev && !nodeIsLINECOMMENT(prev->prev)) 5 100 105
if (nodeIsPOSTFIXSIGIL(node) && prev && nodeIsWHITESPACE(prev) && prev->prev && !nodeIsLINECOMMENT(prev->prev)) 5 0 5
if (nodeIsPOSTFIXSIGIL(node) && prev && nodeIsWHITESPACE(prev) && prev->prev && !nodeIsLINECOMMENT(prev->prev)) 3 2 5
616 3 return PRUNE_PREVIOUS;
617 /* remove whitespace (but NOT endspace) after closing brackets */
618 163 ▶ if (next && nodeIsWHITESPACE(next) && !nodeIsENDSPACE(next) && (nodeIsCHAR(node,')') || nodeIsCHAR(node,'}') || nodeIsCHAR(node,']')))
Branch: 10 branches
BranchTrueFalseTotal
if (next && nodeIsWHITESPACE(next) && !nodeIsENDSPACE(next) && (nodeIsCHAR(node,')') || nodeIsCHAR(node,'}') || nodeIsCHAR(node,']'))) 137 26 163
if (next && nodeIsWHITESPACE(next) && !nodeIsENDSPACE(next) && (nodeIsCHAR(node,')') || nodeIsCHAR(node,'}') || nodeIsCHAR(node,']'))) 25 112 137
if (next && nodeIsWHITESPACE(next) && !nodeIsENDSPACE(next) && (nodeIsCHAR(node,')') || nodeIsCHAR(node,'}') || nodeIsCHAR(node,']'))) 25 0 25
if (next && nodeIsWHITESPACE(next) && !nodeIsENDSPACE(next) && (nodeIsCHAR(node,')') || nodeIsCHAR(node,'}') || nodeIsCHAR(node,']'))) 16 9 25
if (next && nodeIsWHITESPACE(next) && !nodeIsENDSPACE(next) && (nodeIsCHAR(node,')') || nodeIsCHAR(node,'}') || nodeIsCHAR(node,']'))) 5 11 16
if (next && nodeIsWHITESPACE(next) && !nodeIsENDSPACE(next) && (nodeIsCHAR(node,')') || nodeIsCHAR(node,'}') || nodeIsCHAR(node,']'))) 0 5 5
if (next && nodeIsWHITESPACE(next) && !nodeIsENDSPACE(next) && (nodeIsCHAR(node,')') || nodeIsCHAR(node,'}') || nodeIsCHAR(node,']'))) 0 11 11
if (next && nodeIsWHITESPACE(next) && !nodeIsENDSPACE(next) && (nodeIsCHAR(node,')') || nodeIsCHAR(node,'}') || nodeIsCHAR(node,']'))) 0 0 0
if (next && nodeIsWHITESPACE(next) && !nodeIsENDSPACE(next) && (nodeIsCHAR(node,')') || nodeIsCHAR(node,'}') || nodeIsCHAR(node,']'))) 0 11 11
if (next && nodeIsWHITESPACE(next) && !nodeIsENDSPACE(next) && (nodeIsCHAR(node,')') || nodeIsCHAR(node,'}') || nodeIsCHAR(node,']'))) 0 0 0
619 5 return PRUNE_NEXT;
620 /* remove whitespace surrounding "/", EXCEPT where it'd cause "//" */
621 158 ▶ if (nodeIsCHAR(node,'/') && prev && nodeIsWHITESPACE(prev) && prev->prev && !nodeEndsWith(prev->prev,"/"))
Branch: 6 branches
BranchTrueFalseTotal
if (nodeIsCHAR(node,'/') && prev && nodeIsWHITESPACE(prev) && prev->prev && !nodeEndsWith(prev->prev,"/")) 3 155 158
if (nodeIsCHAR(node,'/') && prev && nodeIsWHITESPACE(prev) && prev->prev && !nodeEndsWith(prev->prev,"/")) 3 0 3
if (nodeIsCHAR(node,'/') && prev && nodeIsWHITESPACE(prev) && prev->prev && !nodeEndsWith(prev->prev,"/")) 3 0 3
if (nodeIsCHAR(node,'/') && prev && nodeIsWHITESPACE(prev) && prev->prev && !nodeEndsWith(prev->prev,"/")) 0 3 3
if (nodeIsCHAR(node,'/') && prev && nodeIsWHITESPACE(prev) && prev->prev && !nodeEndsWith(prev->prev,"/")) 0 0 0
if (nodeIsCHAR(node,'/') && prev && nodeIsWHITESPACE(prev) && prev->prev && !nodeEndsWith(prev->prev,"/")) 0 0 0
622 0 return PRUNE_PREVIOUS;
623 158 ▶ if (nodeIsCHAR(node,'/') && next && nodeIsWHITESPACE(next) && next->next && !nodeBeginsWith(next->next,"/"))
Branch: 6 branches
BranchTrueFalseTotal
if (nodeIsCHAR(node,'/') && next && nodeIsWHITESPACE(next) && next->next && !nodeBeginsWith(next->next,"/")) 3 155 158
if (nodeIsCHAR(node,'/') && next && nodeIsWHITESPACE(next) && next->next && !nodeBeginsWith(next->next,"/")) 3 0 3
if (nodeIsCHAR(node,'/') && next && nodeIsWHITESPACE(next) && next->next && !nodeBeginsWith(next->next,"/")) 3 0 3
if (nodeIsCHAR(node,'/') && next && nodeIsWHITESPACE(next) && next->next && !nodeBeginsWith(next->next,"/")) 1 2 3
if (nodeIsCHAR(node,'/') && next && nodeIsWHITESPACE(next) && next->next && !nodeBeginsWith(next->next,"/")) 1 0 1
if (nodeIsCHAR(node,'/') && next && nodeIsWHITESPACE(next) && next->next && !nodeBeginsWith(next->next,"/")) 1 0 1
624 1 return PRUNE_NEXT;
625 /* remove whitespace (but NOT endspace) surrounding "-", EXCEPT where it'd cause "--" */
626 157 ▶ if (nodeIsCHAR(node,'-') && prev && nodeIsWHITESPACE(prev) && !nodeIsENDSPACE(prev) && prev->prev && !nodeIsCHAR(prev->prev,'-'))
Branch: 9 branches
BranchTrueFalseTotal
if (nodeIsCHAR(node,'-') && prev && nodeIsWHITESPACE(prev) && !nodeIsENDSPACE(prev) && prev->prev && !nodeIsCHAR(prev->prev,'-')) 9 148 157
if (nodeIsCHAR(node,'-') && prev && nodeIsWHITESPACE(prev) && !nodeIsENDSPACE(prev) && prev->prev && !nodeIsCHAR(prev->prev,'-')) 9 0 9
if (nodeIsCHAR(node,'-') && prev && nodeIsWHITESPACE(prev) && !nodeIsENDSPACE(prev) && prev->prev && !nodeIsCHAR(prev->prev,'-')) 9 0 9
if (nodeIsCHAR(node,'-') && prev && nodeIsWHITESPACE(prev) && !nodeIsENDSPACE(prev) && prev->prev && !nodeIsCHAR(prev->prev,'-')) 1 8 9
if (nodeIsCHAR(node,'-') && prev && nodeIsWHITESPACE(prev) && !nodeIsENDSPACE(prev) && prev->prev && !nodeIsCHAR(prev->prev,'-')) 1 0 1
if (nodeIsCHAR(node,'-') && prev && nodeIsWHITESPACE(prev) && !nodeIsENDSPACE(prev) && prev->prev && !nodeIsCHAR(prev->prev,'-')) 1 0 1
if (nodeIsCHAR(node,'-') && prev && nodeIsWHITESPACE(prev) && !nodeIsENDSPACE(prev) && prev->prev && !nodeIsCHAR(prev->prev,'-')) 1 0 1
if (nodeIsCHAR(node,'-') && prev && nodeIsWHITESPACE(prev) && !nodeIsENDSPACE(prev) && prev->prev && !nodeIsCHAR(prev->prev,'-')) 1 0 1
if (nodeIsCHAR(node,'-') && prev && nodeIsWHITESPACE(prev) && !nodeIsENDSPACE(prev) && prev->prev && !nodeIsCHAR(prev->prev,'-')) 0 1 1
627 0 return PRUNE_PREVIOUS;
628 157 ▶ if (nodeIsCHAR(node,'-') && next && nodeIsWHITESPACE(next) && !nodeIsENDSPACE(next) && next->next && !nodeIsCHAR(next->next,'-'))
Branch: 9 branches
BranchTrueFalseTotal
if (nodeIsCHAR(node,'-') && next && nodeIsWHITESPACE(next) && !nodeIsENDSPACE(next) && next->next && !nodeIsCHAR(next->next,'-')) 9 148 157
if (nodeIsCHAR(node,'-') && next && nodeIsWHITESPACE(next) && !nodeIsENDSPACE(next) && next->next && !nodeIsCHAR(next->next,'-')) 9 0 9
if (nodeIsCHAR(node,'-') && next && nodeIsWHITESPACE(next) && !nodeIsENDSPACE(next) && next->next && !nodeIsCHAR(next->next,'-')) 9 0 9
if (nodeIsCHAR(node,'-') && next && nodeIsWHITESPACE(next) && !nodeIsENDSPACE(next) && next->next && !nodeIsCHAR(next->next,'-')) 5 4 9
if (nodeIsCHAR(node,'-') && next && nodeIsWHITESPACE(next) && !nodeIsENDSPACE(next) && next->next && !nodeIsCHAR(next->next,'-')) 5 0 5
if (nodeIsCHAR(node,'-') && next && nodeIsWHITESPACE(next) && !nodeIsENDSPACE(next) && next->next && !nodeIsCHAR(next->next,'-')) 5 0 5
if (nodeIsCHAR(node,'-') && next && nodeIsWHITESPACE(next) && !nodeIsENDSPACE(next) && next->next && !nodeIsCHAR(next->next,'-')) 5 0 5
if (nodeIsCHAR(node,'-') && next && nodeIsWHITESPACE(next) && !nodeIsENDSPACE(next) && next->next && !nodeIsCHAR(next->next,'-')) 1 4 5
if (nodeIsCHAR(node,'-') && next && nodeIsWHITESPACE(next) && !nodeIsENDSPACE(next) && next->next && !nodeIsCHAR(next->next,'-')) 0 1 1
629 4 return PRUNE_NEXT;
630 /* remove whitespace (but NOT endspace) surrounding "+", EXCEPT where it'd cause "++" */
631 153 ▶ if (nodeIsCHAR(node,'+') && prev && nodeIsWHITESPACE(prev) && !nodeIsENDSPACE(prev) && prev->prev && !nodeIsCHAR(prev->prev,'+'))
Branch: 9 branches
BranchTrueFalseTotal
if (nodeIsCHAR(node,'+') && prev && nodeIsWHITESPACE(prev) && !nodeIsENDSPACE(prev) && prev->prev && !nodeIsCHAR(prev->prev,'+')) 9 144 153
if (nodeIsCHAR(node,'+') && prev && nodeIsWHITESPACE(prev) && !nodeIsENDSPACE(prev) && prev->prev && !nodeIsCHAR(prev->prev,'+')) 9 0 9
if (nodeIsCHAR(node,'+') && prev && nodeIsWHITESPACE(prev) && !nodeIsENDSPACE(prev) && prev->prev && !nodeIsCHAR(prev->prev,'+')) 9 0 9
if (nodeIsCHAR(node,'+') && prev && nodeIsWHITESPACE(prev) && !nodeIsENDSPACE(prev) && prev->prev && !nodeIsCHAR(prev->prev,'+')) 1 8 9
if (nodeIsCHAR(node,'+') && prev && nodeIsWHITESPACE(prev) && !nodeIsENDSPACE(prev) && prev->prev && !nodeIsCHAR(prev->prev,'+')) 1 0 1
if (nodeIsCHAR(node,'+') && prev && nodeIsWHITESPACE(prev) && !nodeIsENDSPACE(prev) && prev->prev && !nodeIsCHAR(prev->prev,'+')) 1 0 1
if (nodeIsCHAR(node,'+') && prev && nodeIsWHITESPACE(prev) && !nodeIsENDSPACE(prev) && prev->prev && !nodeIsCHAR(prev->prev,'+')) 1 0 1
if (nodeIsCHAR(node,'+') && prev && nodeIsWHITESPACE(prev) && !nodeIsENDSPACE(prev) && prev->prev && !nodeIsCHAR(prev->prev,'+')) 1 0 1
if (nodeIsCHAR(node,'+') && prev && nodeIsWHITESPACE(prev) && !nodeIsENDSPACE(prev) && prev->prev && !nodeIsCHAR(prev->prev,'+')) 0 1 1
632 0 return PRUNE_PREVIOUS;
633 153 ▶ if (nodeIsCHAR(node,'+') && next && nodeIsWHITESPACE(next) && !nodeIsENDSPACE(next) && next->next && !nodeIsCHAR(next->next,'+'))
Branch: 9 branches
BranchTrueFalseTotal
if (nodeIsCHAR(node,'+') && next && nodeIsWHITESPACE(next) && !nodeIsENDSPACE(next) && next->next && !nodeIsCHAR(next->next,'+')) 9 144 153
if (nodeIsCHAR(node,'+') && next && nodeIsWHITESPACE(next) && !nodeIsENDSPACE(next) && next->next && !nodeIsCHAR(next->next,'+')) 9 0 9
if (nodeIsCHAR(node,'+') && next && nodeIsWHITESPACE(next) && !nodeIsENDSPACE(next) && next->next && !nodeIsCHAR(next->next,'+')) 9 0 9
if (nodeIsCHAR(node,'+') && next && nodeIsWHITESPACE(next) && !nodeIsENDSPACE(next) && next->next && !nodeIsCHAR(next->next,'+')) 5 4 9
if (nodeIsCHAR(node,'+') && next && nodeIsWHITESPACE(next) && !nodeIsENDSPACE(next) && next->next && !nodeIsCHAR(next->next,'+')) 5 0 5
if (nodeIsCHAR(node,'+') && next && nodeIsWHITESPACE(next) && !nodeIsENDSPACE(next) && next->next && !nodeIsCHAR(next->next,'+')) 5 0 5
if (nodeIsCHAR(node,'+') && next && nodeIsWHITESPACE(next) && !nodeIsENDSPACE(next) && next->next && !nodeIsCHAR(next->next,'+')) 5 0 5
if (nodeIsCHAR(node,'+') && next && nodeIsWHITESPACE(next) && !nodeIsENDSPACE(next) && next->next && !nodeIsCHAR(next->next,'+')) 1 4 5
if (nodeIsCHAR(node,'+') && next && nodeIsWHITESPACE(next) && !nodeIsENDSPACE(next) && next->next && !nodeIsCHAR(next->next,'+')) 0 1 1
634 4 return PRUNE_NEXT;
635 /* keep all other sigils */
636 149 return PRUNE_NO;
637 }
638 /* keep anything else */
639 0 return PRUNE_NO;
640 }
641  
642 /* prune nodes from the list */
643 33 Node* JsPruneNodes(Node *head) {
644 33 Node* curr = head;
645 496 ▶ while (curr) {
Branch: 1 branch
BranchTrueFalseTotal
while (curr) { 463 33 496
646 /* see if/howe we can prune this node */
647 463 int prune = JsCanPrune(curr);
648 /* prune. each block is responsible for moving onto the next node */
649 463 Node* prev = curr->prev;
650 463 Node* next = curr->next;
651 463 switch (prune) {
652 3 case PRUNE_PREVIOUS:
653 /* discard previous node */
654 3 JsDiscardNode(prev);
655 /* reset "head" if that's what got pruned */
656 3 ▶ if (prev == head)
Branch: 1 branch
BranchTrueFalseTotal
if (prev == head) 0 3 3
657 0 prev = curr;
658 3 break;
659 40 case PRUNE_CURRENT:
660 /* discard current node */
661 40 JsDiscardNode(curr);
662 /* reset "head" if that's what got pruned */
663 40 ▶ if (curr == head)
Branch: 1 branch
BranchTrueFalseTotal
if (curr == head) 18 22 40
664 18 ▶ head = prev ? prev : next;
Branch: 1 branch
BranchTrueFalseTotal
head = prev ? prev : next; 0 18 18
665 /* backup and try again if possible */
666 40 ▶ curr = prev ? prev : next;
Branch: 1 branch
BranchTrueFalseTotal
curr = prev ? prev : next; 22 18 40
667 40 break;
668 97 case PRUNE_NEXT:
669 /* discard next node */
670 97 JsDiscardNode(next);
671 /* stay on current node, and try again */
672 97 break;
673 323 default:
674 /* move ahead to next node */
675 323 curr = next;
676 323 break;
677 }
678 }
679  
680 /* return the (possibly new) head node back to the caller */
681 33 return head;
682 }
683  
684 /* ****************************************************************************
685 * Minifies the given JavaScript, returning a newly allocated string back to
686 * the caller (YOU'RE responsible for freeing its memory).
687 * ****************************************************************************
688 */
689 34 char* JsMinify(const char* string) {
690 char* results;
691 JsDoc doc;
692  
693 /* initialize our JS document object */
694 34 doc.head = NULL;
695 34 doc.tail = NULL;
696 34 doc.buffer = string;
697 34 doc.length = strlen(string);
698 34 doc.offset = 0;
699 34 Newz(0, doc.head_set, 1, NodeSet);
700 34 doc.tail_set = doc.head_set;
701  
702 /* PASS 1: tokenize JS into a list of nodes */
703 34 Node* head = JsTokenizeString(&doc, string);
704 34 ▶ if (!head) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!head) return NULL; 1 33 34
705 /* PASS 2: collapse nodes */
706 33 JsCollapseNodes(head);
707 /* PASS 3: prune nodes */
708 33 head = JsPruneNodes(head);
709 33 ▶ if (!head) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!head) return NULL; 3 30 33
710 /* PASS 4: re-assemble JS into single string */
711 {
712 Node* curr;
713 char* ptr;
714 /* allocate the result buffer to the same size as the original JS; in a
715 * worst case scenario that's how much memory we'll need for it.
716 */
717 30 Newz(0, results, (strlen(string)+1), char);
718 30 ptr = results;
719 /* copy node contents into result buffer */
720 30 curr = head;
721 328 ▶ while (curr) {
Branch: 1 branch
BranchTrueFalseTotal
while (curr) { 298 30 328
722 298 memcpy(ptr, curr->contents, curr->length);
723 298 ptr += curr->length;
724 298 curr = curr->next;
725 }
726 30 *ptr = 0;
727 }
728 /* free memory used by the NodeSets */
729 {
730 30 NodeSet* curr = doc.head_set;
731 60 ▶ while (curr) {
Branch: 1 branch
BranchTrueFalseTotal
while (curr) { 30 30 60
732 30 NodeSet* next = curr->next;
733 30 Safefree(curr);
734 30 curr = next;
735 }
736 }
737 /* return resulting minified JS back to caller */
738 30 return results;
739 }
740  
741  
742  
743 MODULE = JavaScript::Minifier::XS PACKAGE = JavaScript::Minifier::XS
744  
745 PROTOTYPES: disable
746  
747 SV*
748 minify(string)
749 SV* string
750 INIT:
751 34 char* buffer = NULL;
752 34 RETVAL = &PL_sv_undef;
753 CODE:
754 /* minify the JavaScript */
755 34 buffer = JsMinify( SvPVX(string) );
756 /* hand back the minified JS (if we had any) */
757 34 ▶ if (buffer != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (buffer != NULL) { 30 4 34
758 30 RETVAL = newSVpv(buffer, 0);
759 30 Safefree( buffer );
760 }
761 OUTPUT:
762 RETVAL
↑ Summary blib/lib/JavaScript/Minifier/XS.pm »