XS.xs

filter: Statementstmt 94.4% 356 / 377 Branchbran 78.0% 334 / 428 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 85.7% 690 / 805 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 993 bool charIsSpace(char ch) {
18 993 ▶ if (ch == ' ') return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ch == ' ') return 1; 479 514 993
19 514 ▶ if (ch == '\t') return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ch == '\t') return 1; 3 511 514
20 511 return 0;
21 }
22 1019 bool charIsEndspace(char ch) {
23 1019 ▶ if (ch == '\n') return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ch == '\n') return 1; 183 836 1019
24 836 ▶ if (ch == '\r') return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ch == '\r') return 1; 5 831 836
25 831 ▶ if (ch == '\f') return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ch == '\f') return 1; 0 831 831
26 831 return 0;
27 }
28 993 bool charIsWhitespace(char ch) {
29 993 ▶ return charIsSpace(ch) || charIsEndspace(ch);
Branch: 2 branches
BranchTrueFalseTotal
return charIsSpace(ch) || charIsEndspace(ch); 511 482 993
return charIsSpace(ch) || charIsEndspace(ch); 119 392 511
30 }
31 799 bool charIsIdentifier(char ch) {
32 799 ▶ if ((ch >= 'a') && (ch <= 'z')) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if ((ch >= 'a') && (ch <= 'z')) return 1; 533 266 799
if ((ch >= 'a') && (ch <= 'z')) return 1; 519 14 533
33 280 ▶ if ((ch >= 'A') && (ch <= 'Z')) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if ((ch >= 'A') && (ch <= 'Z')) return 1; 29 251 280
if ((ch >= 'A') && (ch <= 'Z')) return 1; 1 28 29
34 279 ▶ if ((ch >= '0') && (ch <= '9')) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if ((ch >= '0') && (ch <= '9')) return 1; 131 148 279
if ((ch >= '0') && (ch <= '9')) return 1; 20 111 131
35 259 ▶ if (ch == '_') return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ch == '_') return 1; 7 252 259
36 252 ▶ if (ch == '$') return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ch == '$') return 1; 2 250 252
37 250 ▶ if (ch == '\\') return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ch == '\\') return 1; 0 250 250
38 250 ▶ if (ch > 126) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ch > 126) return 1; 0 250 250
39 250 return 0;
40 }
41 309 bool charIsInfix(char ch) {
42 /* EOL characters before+after these characters can be removed */
43 309 ▶ if (ch == ',') return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ch == ',') return 1; 12 297 309
44 297 ▶ if (ch == ';') return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ch == ';') return 1; 97 200 297
45 200 ▶ if (ch == ':') return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ch == ':') return 1; 0 200 200
46 200 ▶ if (ch == '=') return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ch == '=') return 1; 74 126 200
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 224 bool charIsPrefix(char ch) {
58 /* EOL characters after these characters can be removed */
59 224 ▶ if (ch == '{') return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ch == '{') return 1; 14 210 224
60 210 ▶ if (ch == '(') return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ch == '(') return 1; 23 187 210
61 187 ▶ if (ch == '[') return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ch == '[') return 1; 5 182 187
62 182 ▶ if (ch == '!') return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ch == '!') return 1; 0 182 182
63 182 return charIsInfix(ch);
64 }
65 168 bool charIsPostfix(char ch) {
66 /* EOL characters before these characters can be removed */
67 168 ▶ if (ch == '}') return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ch == '}') return 1; 13 155 168
68 155 ▶ if (ch == ')') return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ch == ')') return 1; 25 130 155
69 130 ▶ if (ch == ']') return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ch == ']') return 1; 3 127 130
70 127 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 449 Node* JsAllocNode(JsDoc* doc) {
215 Node* node;
216 449 NodeSet* set = doc->tail_set;
217  
218 /* if our current NodeSet is full, allocate a new NodeSet */
219 449 ▶ if (set->next_node >= NODE_SET_SIZE) {
Branch: 1 branch
BranchTrueFalseTotal
if (set->next_node >= NODE_SET_SIZE) { 0 449 449
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 449 node = set->nodes + set->next_node;
229 449 set->next_node ++;
230  
231 /* initialize the node */
232 449 node->prev = NULL;
233 449 node->next = NULL;
234 449 node->contents = NULL;
235 449 node->length = 0;
236 449 node->type = NODE_EMPTY;
237 449 return node;
238 }
239  
240 /* clears the contents of a node */
241 449 void JsClearNodeContents(Node* node) {
242 449 ▶ if (node->contents)
Branch: 1 branch
BranchTrueFalseTotal
if (node->contents) 0 449 449
243 0 Safefree(node->contents);
244 449 node->contents = NULL;
245 449 node->length = 0;
246 449 }
247  
248 /* sets the contents of a node */
249 451 void JsSetNodeContents(Node* node, const char* string, size_t len) {
250 /* if the buffer is already big enough, just overwrite it */
251 451 ▶ if (node->length >= len) {
Branch: 1 branch
BranchTrueFalseTotal
if (node->length >= len) { 2 449 451
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 449 JsClearNodeContents(node);
259 449 node->length = len;
260 /* allocate string, fill with NULLs, and copy */
261 449 Newz(0, node->contents, (len+1), char);
262 449 memcpy( node->contents, string, len );
263 }
264 451 }
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 415 void JsAppendNode(Node* element, Node* node) {
276 415 ▶ if (element->next)
Branch: 1 branch
BranchTrueFalseTotal
if (element->next) 0 415 415
277 0 element->next->prev = node;
278 415 node->next = element->next;
279 415 node->prev = element;
280 415 element->next = node;
281 415 }
282  
283 /* collapses a node to a single whitespace character */
284 160 void JsCollapseNodeToWhitespace(Node* node) {
285 160 ▶ if (node->contents && (node->length > 1)) {
Branch: 2 branches
BranchTrueFalseTotal
if (node->contents && (node->length > 1)) { 160 0 160
if (node->contents && (node->length > 1)) { 57 103 160
286 /* does the node contain endspace? */
287 57 size_t offset = 0;
288 57 bool hasEndspace = 0;
289 84 ▶ while (offset < node->length) {
Branch: 1 branch
BranchTrueFalseTotal
while (offset < node->length) { 78 6 84
290 78 ▶ if (charIsEndspace(node->contents[offset++])) {
Branch: 1 branch
BranchTrueFalseTotal
if (charIsEndspace(node->contents[offset++])) { 51 27 78
291 51 hasEndspace = 1;
292 51 break;
293 }
294 }
295  
296 /* collapse node, either to endspace, or to the first whitespace char */
297 57 node->length = 1;
298 57 ▶ if (hasEndspace) {
Branch: 1 branch
BranchTrueFalseTotal
if (hasEndspace) { 51 6 57
299 51 node->contents[0] = '\n';
300 }
301 57 node->contents[1] = '\0';
302 }
303 160 }
304  
305 /* ****************************************************************************
306 * TOKENIZING FUNCTIONS
307 * ****************************************************************************
308 */
309  
310 /* extracts a quoted literal string */
311 20 void _JsExtractLiteral(JsDoc* doc, Node* node) {
312 20 const char* buf = doc->buffer;
313 20 size_t offset = doc->offset;
314 20 char delimiter = buf[offset];
315 20 bool in_char_class = 0;
316 /* skip start of literal */
317 20 offset ++;
318 /* search for end of literal */
319 331 ▶ while (offset < doc->length) {
Branch: 1 branch
BranchTrueFalseTotal
while (offset < doc->length) { 331 0 331
320 331 ▶ if (buf[offset] == '\\') {
Branch: 1 branch
BranchTrueFalseTotal
if (buf[offset] == '\\') { 4 327 331
321 /* escaped character; skip */
322 4 offset ++;
323 }
324 else {
325 /* if in a regex, track if we're in a character class */
326 327 ▶ if (delimiter == '/') {
Branch: 1 branch
BranchTrueFalseTotal
if (delimiter == '/') { 42 285 327
327 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
328 2 in_char_class = 1;
329 }
330 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
331 2 in_char_class = 0;
332 }
333 }
334 /* if we have found the end of the literal, store it */
335 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
336 20 const char* start = buf + doc->offset;
337 20 size_t length = offset - doc->offset + 1;
338 20 JsSetNodeContents(node, start, length);
339 20 node->type = NODE_LITERAL;
340 20 return;
341 }
342 }
343 /* move onto next character */
344 311 offset ++;
345 }
346 0 croak( "unterminated quoted string literal" );
347 }
348  
349 /* extracts a block comment */
350 15 void _JsExtractBlockComment(JsDoc* doc, Node* node) {
351 15 const char* buf = doc->buffer;
352 15 size_t offset = doc->offset;
353  
354 /* skip start of comment */
355 15 offset ++; /* skip "/" */
356 15 offset ++; /* skip "*" */
357  
358 /* search for end of comment block */
359 368 ▶ while (offset < doc->length) {
Branch: 1 branch
BranchTrueFalseTotal
while (offset < doc->length) { 368 0 368
360 368 ▶ if (buf[offset] == '*') {
Branch: 1 branch
BranchTrueFalseTotal
if (buf[offset] == '*') { 15 353 368
361 15 ▶ if (buf[offset+1] == '/') {
Branch: 1 branch
BranchTrueFalseTotal
if (buf[offset+1] == '/') { 15 0 15
362 15 const char* start = buf + doc->offset;
363 15 size_t length = offset - doc->offset + 2;
364 15 JsSetNodeContents(node, start, length);
365 15 node->type = NODE_BLOCKCOMMENT;
366 15 return;
367 }
368 }
369 /* move onto next character */
370 353 offset ++;
371 }
372  
373 0 croak( "unterminated block comment" );
374 }
375  
376 /* extracts a line comment */
377 8 void _JsExtractLineComment(JsDoc* doc, Node* node) {
378 8 const char* buf = doc->buffer;
379 8 size_t offset = doc->offset;
380  
381 /* skip start of comment */
382 8 offset ++; /* skip "/" */
383 8 offset ++; /* skip "/" */
384  
385 /* search for end of line */
386 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
387 324 offset ++;
388  
389 /* found it ! */
390 {
391 8 const char* start = buf + doc->offset;
392 8 size_t length = offset - doc->offset;
393 8 JsSetNodeContents(node, start, length);
394 8 node->type = NODE_LINECOMMENT;
395 }
396 8 }
397  
398 /* extracts a run of whitespace characters */
399 160 void _JsExtractWhitespace(JsDoc* doc, Node* node) {
400 160 const char* buf = doc->buffer;
401 160 size_t offset = doc->offset;
402 601 ▶ while ((offset < doc->length) && charIsWhitespace(buf[offset]))
Branch: 2 branches
BranchTrueFalseTotal
while ((offset < doc->length) && charIsWhitespace(buf[offset])) 589 12 601
while ((offset < doc->length) && charIsWhitespace(buf[offset])) 441 148 589
403 441 offset ++;
404 160 JsSetNodeContents(node, doc->buffer+doc->offset, offset-doc->offset);
405 160 node->type = NODE_WHITESPACE;
406 160 }
407  
408 /* extracts an identifier */
409 111 void _JsExtractIdentifier(JsDoc* doc, Node* node) {
410 111 const char* buf = doc->buffer;
411 111 size_t offset = doc->offset;
412 548 ▶ while ((offset < doc->length) && charIsIdentifier(buf[offset]))
Branch: 2 branches
BranchTrueFalseTotal
while ((offset < doc->length) && charIsIdentifier(buf[offset])) 546 2 548
while ((offset < doc->length) && charIsIdentifier(buf[offset])) 437 109 546
413 437 offset ++;
414 111 JsSetNodeContents(node, doc->buffer+doc->offset, offset-doc->offset);
415 111 node->type = NODE_IDENTIFIER;
416 111 }
417  
418 /* extracts a -single- symbol/sigil */
419 135 void _JsExtractSigil(JsDoc* doc, Node* node) {
420 135 JsSetNodeContents(node, doc->buffer+doc->offset, 1);
421 135 node->type = NODE_SIGIL;
422 135 }
423  
424 /* tokenizes the given string and returns the list of nodes */
425 35 Node* JsTokenizeString(JsDoc* doc, const char* string) {
426 /* parse the JS */
427 484 ▶ while ((doc->offset < doc->length) && (doc->buffer[doc->offset])) {
Branch: 2 branches
BranchTrueFalseTotal
while ((doc->offset < doc->length) && (doc->buffer[doc->offset])) { 449 35 484
while ((doc->offset < doc->length) && (doc->buffer[doc->offset])) { 449 0 449
428 /* allocate a new node */
429 449 Node* node = JsAllocNode(doc);
430 449 ▶ if (!doc->head)
Branch: 1 branch
BranchTrueFalseTotal
if (!doc->head) 34 415 449
431 34 doc->head = node;
432 449 ▶ if (!doc->tail)
Branch: 1 branch
BranchTrueFalseTotal
if (!doc->tail) 34 415 449
433 34 doc->tail = node;
434  
435 /* parse the next node out of the JS */
436 449 ▶ if (doc->buffer[doc->offset] == '/') {
Branch: 1 branch
BranchTrueFalseTotal
if (doc->buffer[doc->offset] == '/') { 34 415 449
437 34 ▶ if (doc->buffer[doc->offset+1] == '*')
Branch: 1 branch
BranchTrueFalseTotal
if (doc->buffer[doc->offset+1] == '*') 15 19 34
438 15 _JsExtractBlockComment(doc, node);
439 19 ▶ else if (doc->buffer[doc->offset+1] == '/')
Branch: 1 branch
BranchTrueFalseTotal
else if (doc->buffer[doc->offset+1] == '/') 8 11 19
440 8 _JsExtractLineComment(doc, node);
441 else {
442 /* could be "division" or "regexp", but need to know more about
443 * our context...
444 */
445 11 Node* last = doc->tail;
446 11 char ch = 0;
447  
448 /* find last non-whitespace, non-comment node */
449 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
450 15 last = last->prev;
451  
452 11 ch = last->contents[last->length-1];
453  
454 /* see if we're "division" or "regexp" */
455 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
456 /* returning a regexp from a function */
457 1 _JsExtractLiteral(doc, node);
458 }
459 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
460 /* looks like an identifier; guess its division */
461 2 _JsExtractSigil(doc, node);
462 }
463 else {
464 /* presume its a regexp */
465 8 _JsExtractLiteral(doc, node);
466 }
467 }
468 }
469 415 ▶ 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] == '`')) 407 8 415
else if ((doc->buffer[doc->offset] == '"') || (doc->buffer[doc->offset] == '\'') || (doc->buffer[doc->offset] == '`')) 406 1 407
else if ((doc->buffer[doc->offset] == '"') || (doc->buffer[doc->offset] == '\'') || (doc->buffer[doc->offset] == '`')) 2 404 406
470 11 _JsExtractLiteral(doc, node);
471 404 ▶ else if (charIsWhitespace(doc->buffer[doc->offset]))
Branch: 1 branch
BranchTrueFalseTotal
else if (charIsWhitespace(doc->buffer[doc->offset])) 160 244 404
472 160 _JsExtractWhitespace(doc, node);
473 244 ▶ else if (charIsIdentifier(doc->buffer[doc->offset]))
Branch: 1 branch
BranchTrueFalseTotal
else if (charIsIdentifier(doc->buffer[doc->offset])) 111 133 244
474 111 _JsExtractIdentifier(doc, node);
475 else
476 133 _JsExtractSigil(doc, node);
477  
478 /* move ahead to the end of the parsed node */
479 449 doc->offset += node->length;
480  
481 /* add the node to our list of nodes */
482 449 ▶ if (node != doc->tail)
Branch: 1 branch
BranchTrueFalseTotal
if (node != doc->tail) 415 34 449
483 415 JsAppendNode(doc->tail, node);
484 449 doc->tail = node;
485  
486 /* some debugging info */
487 #ifdef DEBUG
488 {
489 int idx;
490 printf("----------------------------------------------------------------\n");
491 printf("%s: [%s]\n", strNodeTypes[node->type], node->contents);
492 printf("next: [");
493 for (idx=0; idx<=10; idx++) {
494 if ((doc->offset+idx) >= doc->length) break;
495 if (!doc->buffer[doc->offset+idx]) break;
496 printf("%c", doc->buffer[doc->offset+idx]);
497 }
498 printf("]\n");
499 }
500 #endif
501 }
502  
503 /* return the node list */
504 35 return doc->head;
505 }
506  
507 /* ****************************************************************************
508 * MINIFICATION FUNCTIONS
509 * ****************************************************************************
510 */
511  
512 /* collapses all of the nodes to their shortest possible representation */
513 34 void JsCollapseNodes(Node* curr) {
514 483 ▶ while (curr) {
Branch: 1 branch
BranchTrueFalseTotal
while (curr) { 449 34 483
515 449 Node* next = curr->next;
516 449 switch (curr->type) {
517 160 case NODE_WHITESPACE:
518 /* all WS gets collapsed */
519 160 JsCollapseNodeToWhitespace(curr);
520 160 break;
521 15 case NODE_BLOCKCOMMENT:
522 /* IE Conditional Compilation comments do not get collapsed */
523 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
524 1 break;
525 }
526 /* block comments get collapsed to WS if that's a side-affect
527 * of their placement in the JS document.
528 */
529 {
530 14 bool convert_to_ws = 0;
531 /* find surrounding non-WS nodes */
532 14 Node* nonws_prev = curr->prev;
533 14 Node* nonws_next = curr->next;
534 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
535 11 nonws_prev = nonws_prev->prev;
536 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
537 11 nonws_next = nonws_next->next;
538 /* check what we're between... */
539 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
540 /* between identifiers? convert to WS */
541 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
542 0 convert_to_ws = 1;
543 /* between possible pre/post increment? convert to WS */
544 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
545 1 convert_to_ws = 1;
546 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
547 1 convert_to_ws = 1;
548 }
549 /* convert to WS */
550 14 ▶ if (convert_to_ws) {
Branch: 1 branch
BranchTrueFalseTotal
if (convert_to_ws) { 2 12 14
551 2 JsSetNodeContents(curr," ",1);
552 2 curr->type = NODE_WHITESPACE;
553 }
554 }
555 14 break;
556 274 default:
557 274 break;
558 }
559 449 curr = next;
560 }
561 34 }
562  
563 /* checks to see whether we can prune the given node from the list.
564 *
565 * THIS is the function that controls the bulk of the minification process.
566 */
567 enum {
568 PRUNE_NO,
569 PRUNE_PREVIOUS,
570 PRUNE_CURRENT,
571 PRUNE_NEXT
572 };
573 474 int JsCanPrune(Node* node) {
574 474 Node* prev = node->prev;
575 474 Node* next = node->next;
576  
577 474 switch (node->type) {
578 0 case NODE_EMPTY:
579 /* prune empty nodes */
580 0 return PRUNE_CURRENT;
581 69 case NODE_WHITESPACE:
582 /* multiple whitespace gets pruned to preserve endspace */
583 69 ▶ if (prev && nodeIsENDSPACE(prev))
Branch: 3 branches
BranchTrueFalseTotal
if (prev && nodeIsENDSPACE(prev)) 55 14 69
if (prev && nodeIsENDSPACE(prev)) 2 53 55
if (prev && nodeIsENDSPACE(prev)) 2 0 2
584 2 return PRUNE_CURRENT;
585 67 ▶ if (prev && nodeIsWHITESPACE(prev))
Branch: 2 branches
BranchTrueFalseTotal
if (prev && nodeIsWHITESPACE(prev)) 53 14 67
if (prev && nodeIsWHITESPACE(prev)) 0 53 53
586 0 return PRUNE_PREVIOUS;
587 /* leading whitespace gets pruned */
588 67 ▶ if (!prev)
Branch: 1 branch
BranchTrueFalseTotal
if (!prev) 14 53 67
589 14 return PRUNE_CURRENT;
590 /* trailing whitespace gets pruned */
591 53 ▶ if (!next)
Branch: 1 branch
BranchTrueFalseTotal
if (!next) 7 46 53
592 7 return PRUNE_CURRENT;
593 /* keep all other whitespace */
594 46 return PRUNE_NO;
595 14 case NODE_BLOCKCOMMENT:
596 /* keep comments that contain the word "copyright" */
597 14 ▶ if (nodeContains(node, "copyright"))
Branch: 1 branch
BranchTrueFalseTotal
if (nodeContains(node, "copyright")) 1 13 14
598 1 return PRUNE_NO;
599 /* keep comments that are for IE Conditional Compilation */
600 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
601 2 return PRUNE_NO;
602 /* block comments get pruned */
603 11 return PRUNE_CURRENT;
604 8 case NODE_LINECOMMENT:
605 /* keep comments that contain the word "copyright" */
606 8 ▶ if (nodeContains(node, "copyright"))
Branch: 1 branch
BranchTrueFalseTotal
if (nodeContains(node, "copyright")) 2 6 8
607 2 return PRUNE_NO;
608 /* keep comments that are for IE Conditional Compilation */
609 6 ▶ if (nodeIsIECONDITIONALLINECOMMENT(node))
Branch: 2 branches
BranchTrueFalseTotal
if (nodeIsIECONDITIONALLINECOMMENT(node)) 6 0 6
if (nodeIsIECONDITIONALLINECOMMENT(node)) 0 6 6
610 0 return PRUNE_NO;
611 /* line comments get pruned */
612 6 return PRUNE_CURRENT;
613 139 case NODE_IDENTIFIER:
614 /* remove whitespace (but NOT endspace) after identifiers, provided
615 * that next thing is -NOT- another identifier
616 */
617 139 ▶ 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)) 137 2 139
if (next && nodeIsWHITESPACE(next) && !nodeIsENDSPACE(next) && next->next && !nodeIsIDENTIFIER(next->next)) 61 76 137
if (next && nodeIsWHITESPACE(next) && !nodeIsENDSPACE(next) && next->next && !nodeIsIDENTIFIER(next->next)) 61 0 61
if (next && nodeIsWHITESPACE(next) && !nodeIsENDSPACE(next) && next->next && !nodeIsIDENTIFIER(next->next)) 60 1 61
if (next && nodeIsWHITESPACE(next) && !nodeIsENDSPACE(next) && next->next && !nodeIsIDENTIFIER(next->next)) 60 0 60
if (next && nodeIsWHITESPACE(next) && !nodeIsENDSPACE(next) && next->next && !nodeIsIDENTIFIER(next->next)) 27 33 60
618 27 return PRUNE_NEXT;
619 /* keep all identifiers */
620 112 return PRUNE_NO;
621 20 case NODE_LITERAL:
622 /* keep all literals */
623 20 return PRUNE_NO;
624 224 case NODE_SIGIL:
625 /* remove whitespace after "prefix" sigils */
626 224 ▶ if (nodeIsPREFIXSIGIL(node) && next && nodeIsWHITESPACE(next))
Branch: 4 branches
BranchTrueFalseTotal
if (nodeIsPREFIXSIGIL(node) && next && nodeIsWHITESPACE(next)) 224 0 224
if (nodeIsPREFIXSIGIL(node) && next && nodeIsWHITESPACE(next)) 155 69 224
if (nodeIsPREFIXSIGIL(node) && next && nodeIsWHITESPACE(next)) 135 20 155
if (nodeIsPREFIXSIGIL(node) && next && nodeIsWHITESPACE(next)) 56 79 135
627 56 return PRUNE_NEXT;
628 /* remove whitespace before "postfix" sigils */
629 168 ▶ 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)) 168 0 168
if (nodeIsPOSTFIXSIGIL(node) && prev && nodeIsWHITESPACE(prev) && prev->prev && !nodeIsLINECOMMENT(prev->prev)) 111 57 168
if (nodeIsPOSTFIXSIGIL(node) && prev && nodeIsWHITESPACE(prev) && prev->prev && !nodeIsLINECOMMENT(prev->prev)) 107 4 111
if (nodeIsPOSTFIXSIGIL(node) && prev && nodeIsWHITESPACE(prev) && prev->prev && !nodeIsLINECOMMENT(prev->prev)) 5 102 107
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
630 3 return PRUNE_PREVIOUS;
631 /* remove whitespace (but NOT endspace) after closing brackets */
632 165 ▶ 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,']'))) 139 26 165
if (next && nodeIsWHITESPACE(next) && !nodeIsENDSPACE(next) && (nodeIsCHAR(node,')') || nodeIsCHAR(node,'}') || nodeIsCHAR(node,']'))) 25 114 139
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
633 5 return PRUNE_NEXT;
634 /* remove whitespace surrounding "/", EXCEPT where it'd cause "//" */
635 160 ▶ 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 157 160
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
636 0 return PRUNE_PREVIOUS;
637 160 ▶ 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 157 160
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
638 1 return PRUNE_NEXT;
639 /* remove whitespace (but NOT endspace) surrounding "-", EXCEPT where it'd cause "--" */
640 159 ▶ 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 150 159
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
641 0 return PRUNE_PREVIOUS;
642 159 ▶ 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 150 159
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
643 4 return PRUNE_NEXT;
644 /* remove whitespace (but NOT endspace) surrounding "+", EXCEPT where it'd cause "++" */
645 155 ▶ 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 146 155
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
646 0 return PRUNE_PREVIOUS;
647 155 ▶ 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 146 155
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
648 4 return PRUNE_NEXT;
649 /* keep all other sigils */
650 151 return PRUNE_NO;
651 }
652 /* keep anything else */
653 0 return PRUNE_NO;
654 }
655  
656 /* prune nodes from the list */
657 34 Node* JsPruneNodes(Node *head) {
658 34 Node* curr = head;
659 508 ▶ while (curr) {
Branch: 1 branch
BranchTrueFalseTotal
while (curr) { 474 34 508
660 /* see if/howe we can prune this node */
661 474 int prune = JsCanPrune(curr);
662 /* prune. each block is responsible for moving onto the next node */
663 474 Node* prev = curr->prev;
664 474 Node* next = curr->next;
665 474 switch (prune) {
666 3 case PRUNE_PREVIOUS:
667 /* discard previous node */
668 3 JsDiscardNode(prev);
669 /* reset "head" if that's what got pruned */
670 3 ▶ if (prev == head)
Branch: 1 branch
BranchTrueFalseTotal
if (prev == head) 0 3 3
671 0 prev = curr;
672 3 break;
673 40 case PRUNE_CURRENT:
674 /* discard current node */
675 40 JsDiscardNode(curr);
676 /* reset "head" if that's what got pruned */
677 40 ▶ if (curr == head)
Branch: 1 branch
BranchTrueFalseTotal
if (curr == head) 18 22 40
678 18 ▶ head = prev ? prev : next;
Branch: 1 branch
BranchTrueFalseTotal
head = prev ? prev : next; 0 18 18
679 /* backup and try again if possible */
680 40 ▶ curr = prev ? prev : next;
Branch: 1 branch
BranchTrueFalseTotal
curr = prev ? prev : next; 22 18 40
681 40 break;
682 97 case PRUNE_NEXT:
683 /* discard next node */
684 97 JsDiscardNode(next);
685 /* stay on current node, and try again */
686 97 break;
687 334 default:
688 /* move ahead to next node */
689 334 curr = next;
690 334 break;
691 }
692 }
693  
694 /* return the (possibly new) head node back to the caller */
695 34 return head;
696 }
697  
698 /* ****************************************************************************
699 * Minifies the given JavaScript, returning a newly allocated string back to
700 * the caller (YOU'RE responsible for freeing its memory).
701 * ****************************************************************************
702 */
703 35 char* JsMinify(const char* string) {
704 char* results;
705 JsDoc doc;
706  
707 /* initialize our JS document object */
708 35 doc.head = NULL;
709 35 doc.tail = NULL;
710 35 doc.buffer = string;
711 35 doc.length = strlen(string);
712 35 doc.offset = 0;
713 35 Newz(0, doc.head_set, 1, NodeSet);
714 35 doc.tail_set = doc.head_set;
715  
716 /* PASS 1: tokenize JS into a list of nodes */
717 35 Node* head = JsTokenizeString(&doc, string);
718 35 ▶ if (!head) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!head) return NULL; 1 34 35
719 /* PASS 2: collapse nodes */
720 34 JsCollapseNodes(head);
721 /* PASS 3: prune nodes */
722 34 head = JsPruneNodes(head);
723 34 ▶ if (!head) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!head) return NULL; 3 31 34
724 /* PASS 4: re-assemble JS into single string */
725 {
726 Node* curr;
727 char* ptr;
728 /* allocate the result buffer to the same size as the original JS; in a
729 * worst case scenario that's how much memory we'll need for it.
730 */
731 31 Newz(0, results, (strlen(string)+1), char);
732 31 ptr = results;
733 /* copy node contents into result buffer */
734 31 curr = head;
735 340 ▶ while (curr) {
Branch: 1 branch
BranchTrueFalseTotal
while (curr) { 309 31 340
736 309 memcpy(ptr, curr->contents, curr->length);
737 309 ptr += curr->length;
738 309 curr = curr->next;
739 }
740 31 *ptr = 0;
741 }
742 /* free memory used by the NodeSets */
743 {
744 31 NodeSet* curr = doc.head_set;
745 62 ▶ while (curr) {
Branch: 1 branch
BranchTrueFalseTotal
while (curr) { 31 31 62
746 31 NodeSet* next = curr->next;
747 31 Safefree(curr);
748 31 curr = next;
749 }
750 }
751 /* return resulting minified JS back to caller */
752 31 return results;
753 }
754  
755  
756  
757 MODULE = JavaScript::Minifier::XS PACKAGE = JavaScript::Minifier::XS
758  
759 PROTOTYPES: disable
760  
761 SV*
762 minify(string)
763 SV* string
764 INIT:
765 35 char* buffer = NULL;
766 35 RETVAL = &PL_sv_undef;
767 CODE:
768 /* minify the JavaScript */
769 35 buffer = JsMinify( SvPVX(string) );
770 /* hand back the minified JS (if we had any) */
771 35 ▶ if (buffer != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (buffer != NULL) { 31 4 35
772 31 RETVAL = newSVpv(buffer, 0);
773 31 Safefree( buffer );
774 }
775 OUTPUT:
776 RETVAL
↑ Summary blib/lib/JavaScript/Minifier/XS.pm »