| 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 |
|
| const char* start_ie_hack = "/*\\*/"; |
| 11 |
|
| const char* end_ie_hack = "/**/"; |
| 12 |
|
| |
| 13 |
|
| /* **************************************************************************** |
| 14 |
|
| * CHARACTER CLASS METHODS |
| 15 |
|
| * **************************************************************************** |
| 16 |
|
| */ |
| 17 |
1370 |
| bool charIsSpace(char ch) { |
| 18 |
1370 |
▶ | if (ch == ' ') return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ch == ' ') return 1; |
583 |
787 |
1370 |
|
| 19 |
787 |
▶ | if (ch == '\t') return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ch == '\t') return 1; |
6 |
781 |
787 |
|
| 20 |
781 |
| return 0; |
| 21 |
|
| } |
| 22 |
781 |
| bool charIsEndspace(char ch) { |
| 23 |
781 |
▶ | if (ch == '\n') return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ch == '\n') return 1; |
22 |
759 |
781 |
|
| 24 |
759 |
▶ | if (ch == '\r') return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ch == '\r') return 1; |
2 |
757 |
759 |
|
| 25 |
757 |
▶ | if (ch == '\f') return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ch == '\f') return 1; |
0 |
757 |
757 |
|
| 26 |
757 |
| return 0; |
| 27 |
|
| } |
| 28 |
1370 |
| bool charIsWhitespace(char ch) { |
| 29 |
1370 |
▶ | return charIsSpace(ch) || charIsEndspace(ch); |
Branch: 2 branches
| Branch | True | False | Total |
| return charIsSpace(ch) || charIsEndspace(ch); |
781 |
589 |
1370 |
| return charIsSpace(ch) || charIsEndspace(ch); |
24 |
757 |
781 |
|
| 30 |
|
| } |
| 31 |
19 |
| bool charIsNumeric(char ch) { |
| 32 |
19 |
▶ | if ((ch >= '0') && (ch <= '9')) return 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if ((ch >= '0') && (ch <= '9')) return 1; |
14 |
5 |
19 |
| if ((ch >= '0') && (ch <= '9')) return 1; |
8 |
6 |
14 |
|
| 33 |
11 |
| return 0; |
| 34 |
|
| } |
| 35 |
1595 |
| bool charIsIdentifier(char ch) { |
| 36 |
1595 |
▶ | if ((ch >= 'a') && (ch <= 'z')) return 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if ((ch >= 'a') && (ch <= 'z')) return 1; |
982 |
613 |
1595 |
| if ((ch >= 'a') && (ch <= 'z')) return 1; |
869 |
113 |
982 |
|
| 37 |
726 |
▶ | if ((ch >= 'A') && (ch <= 'Z')) return 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if ((ch >= 'A') && (ch <= 'Z')) return 1; |
115 |
611 |
726 |
| if ((ch >= 'A') && (ch <= 'Z')) return 1; |
2 |
113 |
115 |
|
| 38 |
724 |
▶ | if ((ch >= '0') && (ch <= '9')) return 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if ((ch >= '0') && (ch <= '9')) return 1; |
450 |
274 |
724 |
| if ((ch >= '0') && (ch <= '9')) return 1; |
196 |
254 |
450 |
|
| 39 |
528 |
▶ | if (ch == '_') return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ch == '_') return 1; |
0 |
528 |
528 |
|
| 40 |
528 |
▶ | if (ch == '.') return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ch == '.') return 1; |
35 |
493 |
528 |
|
| 41 |
493 |
▶ | if (ch == '#') return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ch == '#') return 1; |
5 |
488 |
493 |
|
| 42 |
488 |
▶ | if (ch == '@') return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ch == '@') return 1; |
6 |
482 |
488 |
|
| 43 |
482 |
▶ | if (ch == '%') return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ch == '%') return 1; |
12 |
470 |
482 |
|
| 44 |
470 |
| return 0; |
| 45 |
|
| } |
| 46 |
688 |
| bool charIsInfix(char ch) { |
| 47 |
|
| /* WS before+after these characters can be removed */ |
| 48 |
688 |
▶ | if (ch == '{') return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ch == '{') return 1; |
282 |
406 |
688 |
|
| 49 |
406 |
▶ | if (ch == '}') return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ch == '}') return 1; |
203 |
203 |
406 |
|
| 50 |
203 |
▶ | if (ch == ';') return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ch == ';') return 1; |
35 |
168 |
203 |
|
| 51 |
168 |
▶ | if (ch == ',') return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ch == ',') return 1; |
9 |
159 |
168 |
|
| 52 |
159 |
▶ | if (ch == '~') return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ch == '~') return 1; |
5 |
154 |
159 |
|
| 53 |
154 |
▶ | if (ch == '>') return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ch == '>') return 1; |
5 |
149 |
154 |
|
| 54 |
149 |
| return 0; |
| 55 |
|
| } |
| 56 |
488 |
| bool charIsPrefix(char ch) { |
| 57 |
|
| /* WS after these characters can be removed */ |
| 58 |
488 |
▶ | if (ch == '(') return 1; /* requires leading WS when used in @media */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (ch == '(') return 1; /* requires leading WS when used in @media */ |
20 |
468 |
488 |
|
| 59 |
468 |
▶ | if (ch == ':') return 1; /* requires leading WS when used in pseudo-selector */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (ch == ':') return 1; /* requires leading WS when used in pseudo-selector */ |
112 |
356 |
468 |
|
| 60 |
356 |
| return charIsInfix(ch); |
| 61 |
|
| } |
| 62 |
355 |
| bool charIsPostfix(char ch) { |
| 63 |
|
| /* WS before these characters can be removed */ |
| 64 |
355 |
▶ | if (ch == ')') return 1; /* requires trailing WS for MSIE */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (ch == ')') return 1; /* requires trailing WS for MSIE */ |
23 |
332 |
355 |
|
| 65 |
332 |
| return charIsInfix(ch); |
| 66 |
|
| } |
| 67 |
|
| |
| 68 |
|
| /* **************************************************************************** |
| 69 |
|
| * TYPE DEFINITIONS |
| 70 |
|
| * **************************************************************************** |
| 71 |
|
| */ |
| 72 |
|
| typedef enum { |
| 73 |
|
| NODE_EMPTY, |
| 74 |
|
| NODE_WHITESPACE, |
| 75 |
|
| NODE_BLOCKCOMMENT, |
| 76 |
|
| NODE_IDENTIFIER, |
| 77 |
|
| NODE_LITERAL, |
| 78 |
|
| NODE_SIGIL |
| 79 |
|
| } NodeType; |
| 80 |
|
| |
| 81 |
|
| struct _Node; |
| 82 |
|
| typedef struct _Node Node; |
| 83 |
|
| struct _Node { |
| 84 |
|
| /* linked list pointers */ |
| 85 |
|
| Node* prev; |
| 86 |
|
| Node* next; |
| 87 |
|
| /* node internals */ |
| 88 |
|
| const char* contents; |
| 89 |
|
| size_t length; |
| 90 |
|
| NodeType type; |
| 91 |
|
| bool can_prune; |
| 92 |
|
| }; |
| 93 |
|
| |
| 94 |
|
| #define NODE_SET_SIZE 50000 |
| 95 |
|
| |
| 96 |
|
| struct _NodeSet; |
| 97 |
|
| typedef struct _NodeSet NodeSet; |
| 98 |
|
| struct _NodeSet { |
| 99 |
|
| /* link to next NodeSet */ |
| 100 |
|
| NodeSet* next; |
| 101 |
|
| /* Nodes in this Set */ |
| 102 |
|
| Node nodes[NODE_SET_SIZE]; |
| 103 |
|
| size_t next_node; |
| 104 |
|
| }; |
| 105 |
|
| |
| 106 |
|
| typedef struct { |
| 107 |
|
| /* singly linked list of NodeSets */ |
| 108 |
|
| NodeSet* head_set; |
| 109 |
|
| NodeSet* tail_set; |
| 110 |
|
| /* doubly linked list of Nodes */ |
| 111 |
|
| Node* head; |
| 112 |
|
| Node* tail; |
| 113 |
|
| /* doc internals */ |
| 114 |
|
| const char* buffer; |
| 115 |
|
| size_t length; |
| 116 |
|
| size_t offset; |
| 117 |
|
| } CssDoc; |
| 118 |
|
| |
| 119 |
|
| /* **************************************************************************** |
| 120 |
|
| * NODE CHECKING MACROS/FUNCTIONS |
| 121 |
|
| * **************************************************************************** |
| 122 |
|
| */ |
| 123 |
|
| |
| 124 |
|
| /* checks to see if the node is the given string, case INSENSITIVELY */ |
| 125 |
1 |
| bool nodeEquals(Node* node, const char* string) { |
| 126 |
|
| /* not the same length? Not equal */ |
| 127 |
1 |
| size_t len = strlen(string); |
| 128 |
1 |
▶ | if (len != node->length) |
Branch: 1 branch
| Branch | True | False | Total |
| if (len != node->length) |
0 |
1 |
1 |
|
| 129 |
0 |
| return 0; |
| 130 |
|
| /* compare contents to see if they're equal */ |
| 131 |
1 |
| return (strncasecmp(node->contents, string, node->length) == 0); |
| 132 |
|
| } |
| 133 |
|
| |
| 134 |
|
| /* checks to see if the node contains the given string, case INSENSITIVELY */ |
| 135 |
8 |
| bool nodeContains(Node* node, const char* string) { |
| 136 |
8 |
| const char* haystack = node->contents; |
| 137 |
8 |
| const char* endofhay = haystack + node->length; |
| 138 |
8 |
| size_t len = strlen(string); |
| 139 |
8 |
| char ul_start[2] = { tolower(*string), toupper(*string) }; |
| 140 |
|
| |
| 141 |
|
| /* if node is shorter we know we're not going to have a match */ |
| 142 |
8 |
▶ | if (len > node->length) |
Branch: 1 branch
| Branch | True | False | Total |
| if (len > node->length) |
1 |
7 |
8 |
|
| 143 |
1 |
| return 0; |
| 144 |
|
| |
| 145 |
|
| /* find the needle in the haystack */ |
| 146 |
13 |
▶ | while (haystack && *haystack) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (haystack && *haystack) { |
13 |
0 |
13 |
| while (haystack && *haystack) { |
13 |
0 |
13 |
|
| 147 |
|
| /* find first char of needle */ |
| 148 |
13 |
| haystack = strpbrk( haystack, ul_start ); |
| 149 |
|
| /* didn't find it? Oh well. */ |
| 150 |
13 |
▶ | if (haystack == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (haystack == NULL) |
0 |
13 |
13 |
|
| 151 |
0 |
| return 0; |
| 152 |
|
| /* found it, but will the end be past the end of our node? */ |
| 153 |
13 |
▶ | if ((haystack+len) > endofhay) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((haystack+len) > endofhay) |
3 |
10 |
13 |
|
| 154 |
3 |
| return 0; |
| 155 |
|
| /* see if it matches */ |
| 156 |
10 |
▶ | if (strncasecmp(haystack, string, len) == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (strncasecmp(haystack, string, len) == 0) |
4 |
6 |
10 |
|
| 157 |
4 |
| return 1; |
| 158 |
|
| /* nope, move onto next character in the haystack */ |
| 159 |
6 |
| haystack ++; |
| 160 |
|
| } |
| 161 |
|
| |
| 162 |
|
| /* no match */ |
| 163 |
0 |
| return 0; |
| 164 |
|
| } |
| 165 |
|
| |
| 166 |
|
| /* checks to see if the node begins with the given string, case INSENSITIVELY. |
| 167 |
|
| */ |
| 168 |
0 |
| bool nodeBeginsWith(Node* node, const char* string) { |
| 169 |
|
| /* If the string is longer than the node, it's not going to match */ |
| 170 |
0 |
| size_t len = strlen(string); |
| 171 |
0 |
▶ | if (len > node->length) |
Branch: 1 branch
| Branch | True | False | Total |
| if (len > node->length) |
0 |
0 |
0 |
|
| 172 |
0 |
| return 0; |
| 173 |
|
| /* check for match */ |
| 174 |
0 |
| return (strncasecmp(node->contents, string, len) == 0); |
| 175 |
|
| } |
| 176 |
|
| |
| 177 |
|
| /* checks to see if the node ends with the given string, case INSENSITVELY. */ |
| 178 |
10 |
| bool nodeEndsWith(Node* node, const char* string) { |
| 179 |
|
| /* If the string is longer than the node, it's not going to match */ |
| 180 |
10 |
| size_t len = strlen(string); |
| 181 |
10 |
▶ | if (len > node->length) |
Branch: 1 branch
| Branch | True | False | Total |
| if (len > node->length) |
0 |
10 |
10 |
|
| 182 |
0 |
| return 0; |
| 183 |
|
| /* check for match */ |
| 184 |
10 |
| size_t off = node->length - len; |
| 185 |
10 |
| return (strncasecmp(node->contents+off, string, len) == 0); |
| 186 |
|
| } |
| 187 |
|
| |
| 188 |
|
| /* macros to help see what kind of node we've got */ |
| 189 |
|
| #define nodeIsWHITESPACE(node) ((node->type == NODE_WHITESPACE)) |
| 190 |
|
| #define nodeIsBLOCKCOMMENT(node) ((node->type == NODE_BLOCKCOMMENT)) |
| 191 |
|
| #define nodeIsIDENTIFIER(node) ((node->type == NODE_IDENTIFIER)) |
| 192 |
|
| #define nodeIsLITERAL(node) ((node->type == NODE_LITERAL)) |
| 193 |
|
| #define nodeIsSIGIL(node) ((node->type == NODE_SIGIL)) |
| 194 |
|
| |
| 195 |
|
| #define nodeIsEMPTY(node) ((node->type == NODE_EMPTY) || ((node->length==0) || (node->contents==NULL))) |
| 196 |
|
| #define nodeIsMACIECOMMENTHACK(node) (nodeIsBLOCKCOMMENT(node) && nodeEndsWith(node,"\\*/")) |
| 197 |
|
| #define nodeIsPREFIXSIGIL(node) (nodeIsSIGIL(node) && charIsPrefix(node->contents[0])) |
| 198 |
|
| #define nodeIsPOSTFIXSIGIL(node) (nodeIsSIGIL(node) && charIsPostfix(node->contents[0])) |
| 199 |
|
| #define nodeIsCHAR(node,ch) ((node->contents[0]==ch) && (node->length==1)) |
| 200 |
|
| |
| 201 |
|
| /* checks if this node is the start of "!important" (with optional intravening |
| 202 |
|
| * whitespace. */ |
| 203 |
151 |
| bool nodeStartsBANGIMPORTANT(Node* node) { |
| 204 |
151 |
▶ | if (!node) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!node) return 0; |
0 |
151 |
151 |
|
| 205 |
|
| |
| 206 |
|
| /* Doesn't start with a "!", nope */ |
| 207 |
151 |
▶ | if (!nodeIsCHAR(node,'!')) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!nodeIsCHAR(node,'!')) return 0; |
1 |
150 |
151 |
| if (!nodeIsCHAR(node,'!')) return 0; |
0 |
1 |
1 |
|
| 208 |
|
| |
| 209 |
|
| /* Skip any following whitespace */ |
| 210 |
1 |
| Node* next = node->next; |
| 211 |
1 |
▶ | while (next && nodeIsWHITESPACE(next)) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (next && nodeIsWHITESPACE(next)) { |
1 |
0 |
1 |
| while (next && nodeIsWHITESPACE(next)) { |
0 |
1 |
1 |
|
| 212 |
0 |
| next = node->next; |
| 213 |
|
| } |
| 214 |
1 |
▶ | if (!next) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!next) return 0; |
0 |
1 |
1 |
|
| 215 |
|
| |
| 216 |
|
| /* Next node _better be_ "important" */ |
| 217 |
1 |
▶ | if (!nodeIsIDENTIFIER(next)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!nodeIsIDENTIFIER(next)) return 0; |
0 |
1 |
1 |
|
| 218 |
1 |
▶ | if (nodeEquals(next, "important")) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (nodeEquals(next, "important")) return 1; |
1 |
0 |
1 |
|
| 219 |
0 |
| return 0; |
| 220 |
|
| } |
| 221 |
|
| |
| 222 |
|
| /* **************************************************************************** |
| 223 |
|
| * NODE MANIPULATION FUNCTIONS |
| 224 |
|
| * **************************************************************************** |
| 225 |
|
| */ |
| 226 |
|
| /* allocates a new node */ |
| 227 |
769 |
| Node* CssAllocNode(CssDoc* doc) { |
| 228 |
|
| Node* node; |
| 229 |
769 |
| NodeSet* set = doc->tail_set; |
| 230 |
|
| |
| 231 |
|
| /* if our current NodeSet is full, allocate a new NodeSet */ |
| 232 |
769 |
▶ | if (set->next_node >= NODE_SET_SIZE) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (set->next_node >= NODE_SET_SIZE) { |
0 |
769 |
769 |
|
| 233 |
|
| NodeSet* next_set; |
| 234 |
0 |
| Newz(0, next_set, 1, NodeSet); |
| 235 |
0 |
| set->next = next_set; |
| 236 |
0 |
| doc->tail_set = next_set; |
| 237 |
0 |
| set = next_set; |
| 238 |
|
| } |
| 239 |
|
| |
| 240 |
|
| /* grab the next Node out of the NodeSet */ |
| 241 |
769 |
| node = set->nodes + set->next_node; |
| 242 |
769 |
| set->next_node ++; |
| 243 |
|
| |
| 244 |
|
| /* initialize the node */ |
| 245 |
769 |
| node->prev = NULL; |
| 246 |
769 |
| node->next = NULL; |
| 247 |
769 |
| node->contents = NULL; |
| 248 |
769 |
| node->length = 0; |
| 249 |
769 |
| node->type = NODE_EMPTY; |
| 250 |
769 |
| node->can_prune = 1; |
| 251 |
769 |
| return node; |
| 252 |
|
| } |
| 253 |
|
| |
| 254 |
|
| /* sets the contents of a node */ |
| 255 |
796 |
| void CssSetNodeContents(Node* node, const char* string, size_t len) { |
| 256 |
796 |
| node->contents = string; |
| 257 |
796 |
| node->length = len; |
| 258 |
796 |
| return; |
| 259 |
|
| } |
| 260 |
|
| |
| 261 |
|
| /* removes the node from the list and discards it entirely */ |
| 262 |
261 |
| void CssDiscardNode(Node* node) { |
| 263 |
261 |
▶ | if (node->prev) |
Branch: 1 branch
| Branch | True | False | Total |
| if (node->prev) |
255 |
6 |
261 |
|
| 264 |
255 |
| node->prev->next = node->next; |
| 265 |
261 |
▶ | if (node->next) |
Branch: 1 branch
| Branch | True | False | Total |
| if (node->next) |
259 |
2 |
261 |
|
| 266 |
259 |
| node->next->prev = node->prev; |
| 267 |
261 |
| } |
| 268 |
|
| |
| 269 |
|
| /* appends the node to the given element */ |
| 270 |
715 |
| void CssAppendNode(Node* element, Node* node) { |
| 271 |
715 |
▶ | if (element->next) |
Branch: 1 branch
| Branch | True | False | Total |
| if (element->next) |
0 |
715 |
715 |
|
| 272 |
0 |
| element->next->prev = node; |
| 273 |
715 |
| node->next = element->next; |
| 274 |
715 |
| node->prev = element; |
| 275 |
715 |
| element->next = node; |
| 276 |
715 |
| } |
| 277 |
|
| |
| 278 |
|
| /* **************************************************************************** |
| 279 |
|
| * TOKENIZING FUNCTIONS |
| 280 |
|
| * **************************************************************************** |
| 281 |
|
| */ |
| 282 |
|
| |
| 283 |
|
| /* extracts a quoted literal string */ |
| 284 |
1 |
| void _CssExtractLiteral(CssDoc* doc, Node* node) { |
| 285 |
1 |
| const char* buf = doc->buffer; |
| 286 |
1 |
| size_t offset = doc->offset; |
| 287 |
1 |
| char delimiter = buf[offset]; |
| 288 |
|
| /* skip start of literal */ |
| 289 |
1 |
| offset ++; |
| 290 |
|
| /* search for end of literal */ |
| 291 |
20 |
▶ | while (offset < doc->length) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (offset < doc->length) { |
20 |
0 |
20 |
|
| 292 |
20 |
▶ | if (buf[offset] == '\\') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (buf[offset] == '\\') { |
0 |
20 |
20 |
|
| 293 |
|
| /* escaped character; skip */ |
| 294 |
0 |
| offset ++; |
| 295 |
|
| } |
| 296 |
20 |
▶ | else if (buf[offset] == delimiter) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (buf[offset] == delimiter) { |
1 |
19 |
20 |
|
| 297 |
1 |
| const char* start = buf + doc->offset; |
| 298 |
1 |
| size_t length = offset - doc->offset + 1; |
| 299 |
1 |
| CssSetNodeContents(node, start, length); |
| 300 |
1 |
| node->type = NODE_LITERAL; |
| 301 |
1 |
| return; |
| 302 |
|
| } |
| 303 |
|
| /* move onto next character */ |
| 304 |
19 |
| offset ++; |
| 305 |
|
| } |
| 306 |
0 |
| croak( "unterminated quoted string literal" ); |
| 307 |
|
| } |
| 308 |
|
| |
| 309 |
|
| /* extracts a block comment */ |
| 310 |
10 |
| void _CssExtractBlockComment(CssDoc* doc, Node* node) { |
| 311 |
10 |
| const char* buf = doc->buffer; |
| 312 |
10 |
| size_t offset = doc->offset; |
| 313 |
|
| |
| 314 |
|
| /* skip start of comment */ |
| 315 |
10 |
| offset ++; /* skip "/" */ |
| 316 |
10 |
| offset ++; /* skip "*" */ |
| 317 |
|
| |
| 318 |
|
| /* search for end of comment block */ |
| 319 |
125 |
▶ | while (offset < doc->length) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (offset < doc->length) { |
125 |
0 |
125 |
|
| 320 |
125 |
▶ | if (buf[offset] == '*') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (buf[offset] == '*') { |
10 |
115 |
125 |
|
| 321 |
10 |
▶ | if (buf[offset+1] == '/') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (buf[offset+1] == '/') { |
10 |
0 |
10 |
|
| 322 |
10 |
| const char* start = buf + doc->offset; |
| 323 |
10 |
| size_t length = offset - doc->offset + 2; |
| 324 |
10 |
| CssSetNodeContents(node, start, length); |
| 325 |
10 |
| node->type = NODE_BLOCKCOMMENT; |
| 326 |
10 |
| return; |
| 327 |
|
| } |
| 328 |
|
| } |
| 329 |
|
| /* move onto next character */ |
| 330 |
115 |
| offset ++; |
| 331 |
|
| } |
| 332 |
|
| |
| 333 |
0 |
| croak( "unterminated block comment" ); |
| 334 |
|
| } |
| 335 |
|
| |
| 336 |
|
| /* extracts a run of whitespace characters */ |
| 337 |
288 |
| void _CssExtractWhitespace(CssDoc* doc, Node* node) { |
| 338 |
288 |
| const char* buf = doc->buffer; |
| 339 |
288 |
| size_t offset = doc->offset; |
| 340 |
613 |
▶ | while ((offset < doc->length) && charIsWhitespace(buf[offset])) |
Branch: 2 branches
| Branch | True | False | Total |
| while ((offset < doc->length) && charIsWhitespace(buf[offset])) |
612 |
1 |
613 |
| while ((offset < doc->length) && charIsWhitespace(buf[offset])) |
325 |
287 |
612 |
|
| 341 |
325 |
| offset ++; |
| 342 |
288 |
| CssSetNodeContents(node, doc->buffer+doc->offset, offset-doc->offset); |
| 343 |
288 |
| node->type = NODE_WHITESPACE; |
| 344 |
288 |
| } |
| 345 |
|
| |
| 346 |
|
| /* extracts an identifier */ |
| 347 |
224 |
| void _CssExtractIdentifier(CssDoc* doc, Node* node) { |
| 348 |
224 |
| const char* buf = doc->buffer; |
| 349 |
224 |
| size_t offset = doc->offset; |
| 350 |
1125 |
▶ | while ((offset < doc->length) && charIsIdentifier(buf[offset])) |
Branch: 2 branches
| Branch | True | False | Total |
| while ((offset < doc->length) && charIsIdentifier(buf[offset])) |
1125 |
0 |
1125 |
| while ((offset < doc->length) && charIsIdentifier(buf[offset])) |
901 |
224 |
1125 |
|
| 351 |
901 |
| offset++; |
| 352 |
224 |
| CssSetNodeContents(node, doc->buffer+doc->offset, offset-doc->offset); |
| 353 |
224 |
| node->type = NODE_IDENTIFIER; |
| 354 |
224 |
| } |
| 355 |
|
| |
| 356 |
|
| /* extracts a -single- symbol/sigil */ |
| 357 |
246 |
| void _CssExtractSigil(CssDoc* doc, Node* node) { |
| 358 |
246 |
| CssSetNodeContents(node, doc->buffer+doc->offset, 1); |
| 359 |
246 |
| node->type = NODE_SIGIL; |
| 360 |
246 |
| } |
| 361 |
|
| |
| 362 |
|
| /* tokenizes the given string and returns the list of nodes */ |
| 363 |
54 |
| Node* CssTokenizeString(CssDoc* doc, const char* string) { |
| 364 |
|
| /* parse the CSS */ |
| 365 |
823 |
▶ | while ((doc->offset < doc->length) && (doc->buffer[doc->offset])) { |
Branch: 2 branches
| Branch | True | False | Total |
| while ((doc->offset < doc->length) && (doc->buffer[doc->offset])) { |
769 |
54 |
823 |
| while ((doc->offset < doc->length) && (doc->buffer[doc->offset])) { |
769 |
0 |
769 |
|
| 366 |
|
| /* allocate a new node */ |
| 367 |
769 |
| Node* node = CssAllocNode(doc); |
| 368 |
769 |
▶ | if (!doc->head) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!doc->head) |
54 |
715 |
769 |
|
| 369 |
54 |
| doc->head = node; |
| 370 |
769 |
▶ | if (!doc->tail) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!doc->tail) |
54 |
715 |
769 |
|
| 371 |
54 |
| doc->tail = node; |
| 372 |
|
| |
| 373 |
|
| /* parse the next node out of the CSS */ |
| 374 |
769 |
▶ | if ((doc->buffer[doc->offset] == '/') && (doc->buffer[doc->offset+1] == '*')) |
Branch: 2 branches
| Branch | True | False | Total |
| if ((doc->buffer[doc->offset] == '/') && (doc->buffer[doc->offset+1] == '*')) |
11 |
758 |
769 |
| if ((doc->buffer[doc->offset] == '/') && (doc->buffer[doc->offset+1] == '*')) |
10 |
1 |
11 |
|
| 375 |
10 |
| _CssExtractBlockComment(doc, node); |
| 376 |
759 |
▶ | else if ((doc->buffer[doc->offset] == '"') || (doc->buffer[doc->offset] == '\'')) |
Branch: 2 branches
| Branch | True | False | Total |
| else if ((doc->buffer[doc->offset] == '"') || (doc->buffer[doc->offset] == '\'')) |
759 |
0 |
759 |
| else if ((doc->buffer[doc->offset] == '"') || (doc->buffer[doc->offset] == '\'')) |
1 |
758 |
759 |
|
| 377 |
1 |
| _CssExtractLiteral(doc, node); |
| 378 |
758 |
▶ | else if (charIsWhitespace(doc->buffer[doc->offset])) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (charIsWhitespace(doc->buffer[doc->offset])) |
288 |
470 |
758 |
|
| 379 |
288 |
| _CssExtractWhitespace(doc, node); |
| 380 |
470 |
▶ | else if (charIsIdentifier(doc->buffer[doc->offset])) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (charIsIdentifier(doc->buffer[doc->offset])) |
224 |
246 |
470 |
|
| 381 |
224 |
| _CssExtractIdentifier(doc, node); |
| 382 |
|
| else |
| 383 |
246 |
| _CssExtractSigil(doc, node); |
| 384 |
|
| |
| 385 |
|
| /* move ahead to the end of the parsed node */ |
| 386 |
769 |
| doc->offset += node->length; |
| 387 |
|
| |
| 388 |
|
| /* add the node to our list of nodes */ |
| 389 |
769 |
▶ | if (node != doc->tail) |
Branch: 1 branch
| Branch | True | False | Total |
| if (node != doc->tail) |
715 |
54 |
769 |
|
| 390 |
715 |
| CssAppendNode(doc->tail, node); |
| 391 |
769 |
| doc->tail = node; |
| 392 |
|
| } |
| 393 |
|
| |
| 394 |
|
| /* return the node list */ |
| 395 |
54 |
| return doc->head; |
| 396 |
|
| } |
| 397 |
|
| |
| 398 |
|
| /* **************************************************************************** |
| 399 |
|
| * MINIFICATION FUNCTIONS |
| 400 |
|
| * **************************************************************************** |
| 401 |
|
| */ |
| 402 |
|
| |
| 403 |
|
| /* Skips over any "zero value" found in the provided string, returning a |
| 404 |
|
| * pointer to the next character after those zeros (which may be the same |
| 405 |
|
| * as the pointer to ther original string, if no zeros were found). |
| 406 |
|
| */ |
| 407 |
28 |
| const char* CssSkipZeroValue(const char* str) { |
| 408 |
|
| /* Skip leading zeros */ |
| 409 |
67 |
▶ | while (*str == '0') { str ++; } |
Branch: 1 branch
| Branch | True | False | Total |
| while (*str == '0') { str ++; } |
39 |
28 |
67 |
|
| 410 |
28 |
| const char* after_leading_zeros = str; |
| 411 |
|
| |
| 412 |
|
| /* Decimal point, followed by more zeros? */ |
| 413 |
28 |
▶ | if (*str == '.') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*str == '.') { |
19 |
9 |
28 |
|
| 414 |
19 |
| str ++; |
| 415 |
54 |
▶ | while (*str == '0') { str ++; } |
Branch: 1 branch
| Branch | True | False | Total |
| while (*str == '0') { str ++; } |
35 |
19 |
54 |
|
| 416 |
19 |
▶ | if (charIsNumeric(*str)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (charIsNumeric(*str)) { |
8 |
11 |
19 |
|
| 417 |
|
| /* ends in digit; significant at the decimal point */ |
| 418 |
8 |
| return after_leading_zeros; |
| 419 |
|
| } |
| 420 |
11 |
| return str; |
| 421 |
|
| } |
| 422 |
|
| |
| 423 |
|
| /* Done. */ |
| 424 |
9 |
| return after_leading_zeros; |
| 425 |
|
| } |
| 426 |
|
| |
| 427 |
|
| /* checks to see if the string contains a known CSS unit */ |
| 428 |
8 |
| bool CssIsKnownUnit(const char* str) { |
| 429 |
|
| /* If it ends with a known Unit, its a Zero Unit */ |
| 430 |
8 |
▶ | if (0 == strncmp(str, "em", 2)) { return 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (0 == strncmp(str, "em", 2)) { return 1; } |
0 |
8 |
8 |
|
| 431 |
8 |
▶ | if (0 == strncmp(str, "ex", 2)) { return 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (0 == strncmp(str, "ex", 2)) { return 1; } |
0 |
8 |
8 |
|
| 432 |
8 |
▶ | if (0 == strncmp(str, "ch", 2)) { return 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (0 == strncmp(str, "ch", 2)) { return 1; } |
0 |
8 |
8 |
|
| 433 |
8 |
▶ | if (0 == strncmp(str, "rem", 3)) { return 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (0 == strncmp(str, "rem", 3)) { return 1; } |
0 |
8 |
8 |
|
| 434 |
8 |
▶ | if (0 == strncmp(str, "vw", 2)) { return 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (0 == strncmp(str, "vw", 2)) { return 1; } |
0 |
8 |
8 |
|
| 435 |
8 |
▶ | if (0 == strncmp(str, "vh", 2)) { return 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (0 == strncmp(str, "vh", 2)) { return 1; } |
0 |
8 |
8 |
|
| 436 |
8 |
▶ | if (0 == strncmp(str, "vmin", 3)) { return 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (0 == strncmp(str, "vmin", 3)) { return 1; } |
0 |
8 |
8 |
|
| 437 |
8 |
▶ | if (0 == strncmp(str, "vmax", 3)) { return 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (0 == strncmp(str, "vmax", 3)) { return 1; } |
0 |
8 |
8 |
|
| 438 |
8 |
▶ | if (0 == strncmp(str, "cm", 2)) { return 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (0 == strncmp(str, "cm", 2)) { return 1; } |
0 |
8 |
8 |
|
| 439 |
8 |
▶ | if (0 == strncmp(str, "mm", 2)) { return 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (0 == strncmp(str, "mm", 2)) { return 1; } |
0 |
8 |
8 |
|
| 440 |
8 |
▶ | if (0 == strncmp(str, "in", 2)) { return 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (0 == strncmp(str, "in", 2)) { return 1; } |
0 |
8 |
8 |
|
| 441 |
8 |
▶ | if (0 == strncmp(str, "px", 2)) { return 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (0 == strncmp(str, "px", 2)) { return 1; } |
5 |
3 |
8 |
|
| 442 |
3 |
▶ | if (0 == strncmp(str, "pt", 2)) { return 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (0 == strncmp(str, "pt", 2)) { return 1; } |
0 |
3 |
3 |
|
| 443 |
3 |
▶ | if (0 == strncmp(str, "pc", 2)) { return 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (0 == strncmp(str, "pc", 2)) { return 1; } |
0 |
3 |
3 |
|
| 444 |
3 |
▶ | if (0 == strncmp(str, "%", 1)) { return 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (0 == strncmp(str, "%", 1)) { return 1; } |
0 |
3 |
3 |
|
| 445 |
|
| |
| 446 |
|
| /* Nope */ |
| 447 |
3 |
| return 0; |
| 448 |
|
| } |
| 449 |
|
| |
| 450 |
|
| /* collapses all of the nodes to their shortest possible representation */ |
| 451 |
54 |
| void CssCollapseNodes(Node* curr) { |
| 452 |
54 |
| bool inMacIeCommentHack = 0; |
| 453 |
54 |
| bool inFunction = 0; |
| 454 |
823 |
▶ | while (curr) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (curr) { |
769 |
54 |
823 |
|
| 455 |
769 |
| Node* next = curr->next; |
| 456 |
769 |
| switch (curr->type) { |
| 457 |
288 |
| case NODE_WHITESPACE: |
| 458 |
|
| /* collapse to a single whitespace character */ |
| 459 |
288 |
| curr->length = 1; |
| 460 |
288 |
| break; |
| 461 |
10 |
| case NODE_BLOCKCOMMENT: |
| 462 |
10 |
▶ | if (!inMacIeCommentHack && nodeIsMACIECOMMENTHACK(curr)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (!inMacIeCommentHack && nodeIsMACIECOMMENTHACK(curr)) { |
7 |
3 |
10 |
| if (!inMacIeCommentHack && nodeIsMACIECOMMENTHACK(curr)) { |
7 |
0 |
7 |
| if (!inMacIeCommentHack && nodeIsMACIECOMMENTHACK(curr)) { |
2 |
5 |
7 |
|
| 463 |
|
| /* START of mac/ie hack */ |
| 464 |
2 |
| CssSetNodeContents(curr, start_ie_hack, strlen(start_ie_hack)); |
| 465 |
2 |
| curr->can_prune = 0; |
| 466 |
2 |
| inMacIeCommentHack = 1; |
| 467 |
|
| } |
| 468 |
8 |
▶ | else if (inMacIeCommentHack && !nodeIsMACIECOMMENTHACK(curr)) { |
Branch: 3 branches
| Branch | True | False | Total |
| else if (inMacIeCommentHack && !nodeIsMACIECOMMENTHACK(curr)) { |
3 |
5 |
8 |
| else if (inMacIeCommentHack && !nodeIsMACIECOMMENTHACK(curr)) { |
3 |
0 |
3 |
| else if (inMacIeCommentHack && !nodeIsMACIECOMMENTHACK(curr)) { |
2 |
1 |
3 |
|
| 469 |
|
| /* END of mac/ie hack */ |
| 470 |
2 |
| CssSetNodeContents(curr, end_ie_hack, strlen(end_ie_hack)); |
| 471 |
2 |
| curr->can_prune = 0; |
| 472 |
2 |
| inMacIeCommentHack = 0; |
| 473 |
|
| } |
| 474 |
10 |
| break; |
| 475 |
224 |
| case NODE_IDENTIFIER: |
| 476 |
|
| { |
| 477 |
|
| /* if the node doesn't begin with a "zero", nothing to collapse */ |
| 478 |
224 |
| const char* ptr = curr->contents; |
| 479 |
224 |
▶ | if ( (*ptr != '0') && (*ptr != '.' )) { |
Branch: 2 branches
| Branch | True | False | Total |
| if ( (*ptr != '0') && (*ptr != '.' )) { |
204 |
20 |
224 |
| if ( (*ptr != '0') && (*ptr != '.' )) { |
195 |
9 |
204 |
|
| 480 |
|
| /* not "0" and not "point-something" */ |
| 481 |
195 |
| break; |
| 482 |
|
| } |
| 483 |
29 |
▶ | if ( (*ptr == '.') && (*(ptr+1) != '0') ) { |
Branch: 2 branches
| Branch | True | False | Total |
| if ( (*ptr == '.') && (*(ptr+1) != '0') ) { |
9 |
20 |
29 |
| if ( (*ptr == '.') && (*(ptr+1) != '0') ) { |
1 |
8 |
9 |
|
| 484 |
|
| /* "point-something", but not "point-zero" */ |
| 485 |
1 |
| break; |
| 486 |
|
| } |
| 487 |
|
| |
| 488 |
|
| /* skip all leading zeros */ |
| 489 |
28 |
| ptr = CssSkipZeroValue(curr->contents); |
| 490 |
|
| |
| 491 |
|
| /* if we didn't skip anything, no Zeros to collapse */ |
| 492 |
28 |
▶ | if (ptr == curr->contents) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ptr == curr->contents) { |
4 |
24 |
28 |
|
| 493 |
4 |
| break; |
| 494 |
|
| } |
| 495 |
|
| |
| 496 |
|
| /* did we skip the entire thing, and thus the Node is "all zeros"? */ |
| 497 |
24 |
| size_t skipped = ptr - curr->contents; |
| 498 |
24 |
▶ | if (skipped == curr->length) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (skipped == curr->length) { |
5 |
19 |
24 |
|
| 499 |
|
| /* nothing but zeros, so truncate to "0" */ |
| 500 |
5 |
| CssSetNodeContents(curr, "0", 1); |
| 501 |
5 |
| break; |
| 502 |
|
| } |
| 503 |
|
| |
| 504 |
|
| /* was it a zero percentage? */ |
| 505 |
19 |
▶ | if (*ptr == '%') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*ptr == '%') { |
5 |
14 |
19 |
|
| 506 |
|
| /* a zero percentage; truncate to "0%" */ |
| 507 |
5 |
| CssSetNodeContents(curr, "0%", 2); |
| 508 |
5 |
| break; |
| 509 |
|
| } |
| 510 |
|
| |
| 511 |
|
| /* if all we're left with is a known CSS unit, and we're NOT in |
| 512 |
|
| * a function (where we have to preserve units), just truncate |
| 513 |
|
| * to "0" |
| 514 |
|
| */ |
| 515 |
14 |
▶ | if (!inFunction && CssIsKnownUnit(ptr)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!inFunction && CssIsKnownUnit(ptr)) { |
8 |
6 |
14 |
| if (!inFunction && CssIsKnownUnit(ptr)) { |
5 |
3 |
8 |
|
| 516 |
|
| /* not in a function, and is a zero unit; truncate to "0" */ |
| 517 |
5 |
| CssSetNodeContents(curr, "0", 1); |
| 518 |
5 |
| break; |
| 519 |
|
| } |
| 520 |
|
| |
| 521 |
|
| /* otherwise, just skip leading zeros, and preserve any unit */ |
| 522 |
|
| /* ... do we need to back up one char to find a significant zero? */ |
| 523 |
9 |
▶ | if (*ptr != '.') { ptr --; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (*ptr != '.') { ptr --; } |
5 |
4 |
9 |
|
| 524 |
|
| /* ... if that's not the start of the buffer ... */ |
| 525 |
9 |
▶ | if (ptr != curr->contents) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ptr != curr->contents) { |
8 |
1 |
9 |
|
| 526 |
|
| /* set the buffer to "0 + units", blowing away the earlier bits */ |
| 527 |
8 |
| size_t len = curr->length - (ptr - curr->contents); |
| 528 |
8 |
| CssSetNodeContents(curr, ptr, len); |
| 529 |
|
| } |
| 530 |
9 |
| break; |
| 531 |
|
| } |
| 532 |
246 |
| case NODE_SIGIL: |
| 533 |
246 |
▶ | if (nodeIsCHAR(curr,'(')) { inFunction = 1; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (nodeIsCHAR(curr,'(')) { inFunction = 1; } |
17 |
229 |
246 |
| if (nodeIsCHAR(curr,'(')) { inFunction = 1; } |
17 |
0 |
17 |
|
| 534 |
246 |
▶ | if (nodeIsCHAR(curr,')')) { inFunction = 0; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (nodeIsCHAR(curr,')')) { inFunction = 0; } |
17 |
229 |
246 |
| if (nodeIsCHAR(curr,')')) { inFunction = 0; } |
17 |
0 |
17 |
|
| 535 |
246 |
| break; |
| 536 |
1 |
| default: |
| 537 |
1 |
| break; |
| 538 |
|
| } |
| 539 |
769 |
| curr = next; |
| 540 |
|
| } |
| 541 |
54 |
| } |
| 542 |
|
| |
| 543 |
|
| /* checks to see whether we can prune the given node from the list. |
| 544 |
|
| * |
| 545 |
|
| * THIS is the function that controls the bulk of the minification process. |
| 546 |
|
| */ |
| 547 |
|
| enum { |
| 548 |
|
| PRUNE_NO, |
| 549 |
|
| PRUNE_PREVIOUS, |
| 550 |
|
| PRUNE_CURRENT, |
| 551 |
|
| PRUNE_NEXT |
| 552 |
|
| }; |
| 553 |
891 |
| int CssCanPrune(Node* node) { |
| 554 |
891 |
| Node* prev = node->prev; |
| 555 |
891 |
| Node* next = node->next; |
| 556 |
|
| |
| 557 |
|
| /* only if node is prunable */ |
| 558 |
891 |
▶ | if (!node->can_prune) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!node->can_prune) |
6 |
885 |
891 |
|
| 559 |
6 |
| return PRUNE_NO; |
| 560 |
|
| |
| 561 |
885 |
| switch (node->type) { |
| 562 |
0 |
| case NODE_EMPTY: |
| 563 |
|
| /* prune empty nodes */ |
| 564 |
0 |
| return PRUNE_CURRENT; |
| 565 |
155 |
| case NODE_WHITESPACE: |
| 566 |
|
| /* remove whitespace before comment blocks */ |
| 567 |
155 |
▶ | if (next && nodeIsBLOCKCOMMENT(next)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (next && nodeIsBLOCKCOMMENT(next)) |
155 |
0 |
155 |
| if (next && nodeIsBLOCKCOMMENT(next)) |
0 |
155 |
155 |
|
| 568 |
0 |
| return PRUNE_CURRENT; |
| 569 |
|
| /* remove whitespace after comment blocks */ |
| 570 |
155 |
▶ | if (prev && nodeIsBLOCKCOMMENT(prev)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (prev && nodeIsBLOCKCOMMENT(prev)) |
152 |
3 |
155 |
| if (prev && nodeIsBLOCKCOMMENT(prev)) |
4 |
148 |
152 |
|
| 571 |
4 |
| return PRUNE_CURRENT; |
| 572 |
|
| /* remove whitespace before "!important" */ |
| 573 |
151 |
▶ | if (next && nodeStartsBANGIMPORTANT(next)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (next && nodeStartsBANGIMPORTANT(next)) { |
151 |
0 |
151 |
| if (next && nodeStartsBANGIMPORTANT(next)) { |
1 |
150 |
151 |
|
| 574 |
1 |
| return PRUNE_CURRENT; |
| 575 |
|
| } |
| 576 |
|
| /* leading whitespace gets pruned */ |
| 577 |
150 |
▶ | if (!prev) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!prev) |
3 |
147 |
150 |
|
| 578 |
3 |
| return PRUNE_CURRENT; |
| 579 |
|
| /* trailing whitespace gets pruned */ |
| 580 |
147 |
▶ | if (!next) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!next) |
0 |
147 |
147 |
|
| 581 |
0 |
| return PRUNE_CURRENT; |
| 582 |
|
| /* keep all other whitespace */ |
| 583 |
147 |
| return PRUNE_NO; |
| 584 |
8 |
| case NODE_BLOCKCOMMENT: |
| 585 |
|
| /* keep comments that contain the word "copyright" */ |
| 586 |
8 |
▶ | if (nodeContains(node,"copyright")) |
Branch: 1 branch
| Branch | True | False | Total |
| if (nodeContains(node,"copyright")) |
4 |
4 |
8 |
|
| 587 |
4 |
| return PRUNE_NO; |
| 588 |
|
| /* remove comment blocks */ |
| 589 |
4 |
| return PRUNE_CURRENT; |
| 590 |
232 |
| case NODE_IDENTIFIER: |
| 591 |
|
| /* keep all identifiers */ |
| 592 |
232 |
| return PRUNE_NO; |
| 593 |
2 |
| case NODE_LITERAL: |
| 594 |
|
| /* keep all literals */ |
| 595 |
2 |
| return PRUNE_NO; |
| 596 |
488 |
| case NODE_SIGIL: |
| 597 |
|
| /* remove whitespace after "prefix" sigils */ |
| 598 |
488 |
▶ | if (nodeIsPREFIXSIGIL(node) && next && nodeIsWHITESPACE(next)) |
Branch: 4 branches
| Branch | True | False | Total |
| if (nodeIsPREFIXSIGIL(node) && next && nodeIsWHITESPACE(next)) |
488 |
0 |
488 |
| if (nodeIsPREFIXSIGIL(node) && next && nodeIsWHITESPACE(next)) |
439 |
49 |
488 |
| if (nodeIsPREFIXSIGIL(node) && next && nodeIsWHITESPACE(next)) |
348 |
91 |
439 |
| if (nodeIsPREFIXSIGIL(node) && next && nodeIsWHITESPACE(next)) |
133 |
215 |
348 |
|
| 599 |
133 |
| return PRUNE_NEXT; |
| 600 |
|
| /* remove whitespace before "postfix" sigils */ |
| 601 |
355 |
▶ | if (nodeIsPOSTFIXSIGIL(node) && prev && nodeIsWHITESPACE(prev)) |
Branch: 4 branches
| Branch | True | False | Total |
| if (nodeIsPOSTFIXSIGIL(node) && prev && nodeIsWHITESPACE(prev)) |
355 |
0 |
355 |
| if (nodeIsPOSTFIXSIGIL(node) && prev && nodeIsWHITESPACE(prev)) |
255 |
100 |
355 |
| if (nodeIsPOSTFIXSIGIL(node) && prev && nodeIsWHITESPACE(prev)) |
255 |
0 |
255 |
| if (nodeIsPOSTFIXSIGIL(node) && prev && nodeIsWHITESPACE(prev)) |
107 |
148 |
255 |
|
| 602 |
107 |
| return PRUNE_PREVIOUS; |
| 603 |
|
| /* remove ";" characters at end of selector groups */ |
| 604 |
248 |
▶ | if (nodeIsCHAR(node,';') && next && nodeIsSIGIL(next) && nodeIsCHAR(next,'}')) |
Branch: 6 branches
| Branch | True | False | Total |
| if (nodeIsCHAR(node,';') && next && nodeIsSIGIL(next) && nodeIsCHAR(next,'}')) |
12 |
236 |
248 |
| if (nodeIsCHAR(node,';') && next && nodeIsSIGIL(next) && nodeIsCHAR(next,'}')) |
12 |
0 |
12 |
| if (nodeIsCHAR(node,';') && next && nodeIsSIGIL(next) && nodeIsCHAR(next,'}')) |
12 |
0 |
12 |
| if (nodeIsCHAR(node,';') && next && nodeIsSIGIL(next) && nodeIsCHAR(next,'}')) |
9 |
3 |
12 |
| if (nodeIsCHAR(node,';') && next && nodeIsSIGIL(next) && nodeIsCHAR(next,'}')) |
9 |
0 |
9 |
| if (nodeIsCHAR(node,';') && next && nodeIsSIGIL(next) && nodeIsCHAR(next,'}')) |
9 |
0 |
9 |
|
| 605 |
9 |
| return PRUNE_CURRENT; |
| 606 |
|
| /* keep all other sigils */ |
| 607 |
239 |
| return PRUNE_NO; |
| 608 |
|
| } |
| 609 |
|
| /* keep anything else */ |
| 610 |
0 |
| return PRUNE_NO; |
| 611 |
|
| } |
| 612 |
|
| |
| 613 |
|
| /* prune nodes from the list */ |
| 614 |
54 |
| Node* CssPruneNodes(Node *head) { |
| 615 |
54 |
| Node* curr = head; |
| 616 |
945 |
▶ | while (curr) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (curr) { |
891 |
54 |
945 |
|
| 617 |
|
| /* see if/how we can prune this node */ |
| 618 |
891 |
| int prune = CssCanPrune(curr); |
| 619 |
|
| /* prune. each block is responsible for moving onto the next node */ |
| 620 |
891 |
| Node* prev = curr->prev; |
| 621 |
891 |
| Node* next = curr->next; |
| 622 |
891 |
| switch (prune) { |
| 623 |
107 |
| case PRUNE_PREVIOUS: |
| 624 |
|
| /* discard previous node */ |
| 625 |
107 |
| CssDiscardNode(prev); |
| 626 |
|
| /* reset "head" if that's what got pruned */ |
| 627 |
107 |
▶ | if (prev == head) |
Branch: 1 branch
| Branch | True | False | Total |
| if (prev == head) |
0 |
107 |
107 |
|
| 628 |
0 |
| head = curr; |
| 629 |
107 |
| break; |
| 630 |
21 |
| case PRUNE_CURRENT: |
| 631 |
|
| /* discard current node */ |
| 632 |
21 |
| CssDiscardNode(curr); |
| 633 |
|
| /* reset "head" if that's what got pruned */ |
| 634 |
21 |
▶ | if (curr == head) |
Branch: 1 branch
| Branch | True | False | Total |
| if (curr == head) |
6 |
15 |
21 |
|
| 635 |
6 |
▶ | head = prev ? prev : next; |
Branch: 1 branch
| Branch | True | False | Total |
| head = prev ? prev : next; |
0 |
6 |
6 |
|
| 636 |
|
| /* backup and try again if possible */ |
| 637 |
21 |
▶ | curr = prev ? prev : next; |
Branch: 1 branch
| Branch | True | False | Total |
| curr = prev ? prev : next; |
15 |
6 |
21 |
|
| 638 |
21 |
| break; |
| 639 |
133 |
| case PRUNE_NEXT: |
| 640 |
|
| /* discard next node */ |
| 641 |
133 |
| CssDiscardNode(next); |
| 642 |
|
| /* stay on current node, and try again */ |
| 643 |
133 |
| break; |
| 644 |
630 |
| default: |
| 645 |
|
| /* move ahead to next node */ |
| 646 |
630 |
| curr = next; |
| 647 |
630 |
| break; |
| 648 |
|
| } |
| 649 |
|
| } |
| 650 |
|
| |
| 651 |
|
| /* return the (possibly new) head node back to the caller */ |
| 652 |
54 |
| return head; |
| 653 |
|
| } |
| 654 |
|
| |
| 655 |
|
| /* **************************************************************************** |
| 656 |
|
| * Minifies the given CSS, returning a newly allocated string back to the |
| 657 |
|
| * caller (YOU'RE responsible for freeing its memory). |
| 658 |
|
| * **************************************************************************** |
| 659 |
|
| */ |
| 660 |
54 |
| char* CssMinify(const char* string) { |
| 661 |
|
| char* results; |
| 662 |
|
| CssDoc doc; |
| 663 |
|
| |
| 664 |
|
| /* initialize our CSS document object */ |
| 665 |
54 |
| doc.head = NULL; |
| 666 |
54 |
| doc.tail = NULL; |
| 667 |
54 |
| doc.buffer = string; |
| 668 |
54 |
| doc.length = strlen(string); |
| 669 |
54 |
| doc.offset = 0; |
| 670 |
54 |
| Newz(0, doc.head_set, 1, NodeSet); |
| 671 |
54 |
| doc.tail_set = doc.head_set; |
| 672 |
|
| |
| 673 |
|
| /* PASS 1: tokenize CSS into a list of nodes */ |
| 674 |
54 |
| Node* head = CssTokenizeString(&doc, string); |
| 675 |
54 |
▶ | if (!head) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!head) return NULL; |
0 |
54 |
54 |
|
| 676 |
|
| /* PASS 2: collapse nodes */ |
| 677 |
54 |
| CssCollapseNodes(head); |
| 678 |
|
| /* PASS 3: prune nodes */ |
| 679 |
54 |
| head = CssPruneNodes(head); |
| 680 |
54 |
▶ | if (!head) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!head) return NULL; |
1 |
53 |
54 |
|
| 681 |
|
| /* PASS 4: re-assemble CSS into single string */ |
| 682 |
|
| { |
| 683 |
|
| Node* curr; |
| 684 |
|
| char* ptr; |
| 685 |
|
| /* allocate the result buffer to the same size as the original CSS; in |
| 686 |
|
| * a worst case scenario that's how much memory we'll need for it. |
| 687 |
|
| */ |
| 688 |
53 |
| Newz(0, results, (strlen(string)+1), char); |
| 689 |
53 |
| ptr = results; |
| 690 |
|
| /* copy node contents into result buffer */ |
| 691 |
53 |
| curr = head; |
| 692 |
561 |
▶ | while (curr) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (curr) { |
508 |
53 |
561 |
|
| 693 |
508 |
| memcpy(ptr, curr->contents, curr->length); |
| 694 |
508 |
| ptr += curr->length; |
| 695 |
508 |
| curr = curr->next; |
| 696 |
|
| } |
| 697 |
53 |
| *ptr = 0; |
| 698 |
|
| } |
| 699 |
|
| /* free memory used by the NodeSets */ |
| 700 |
|
| { |
| 701 |
53 |
| NodeSet* curr = doc.head_set; |
| 702 |
106 |
▶ | while (curr) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (curr) { |
53 |
53 |
106 |
|
| 703 |
53 |
| NodeSet* next = curr->next; |
| 704 |
53 |
| Safefree(curr); |
| 705 |
53 |
| curr = next; |
| 706 |
|
| } |
| 707 |
|
| } |
| 708 |
|
| /* return resulting minified CSS back to caller */ |
| 709 |
53 |
| return results; |
| 710 |
|
| } |
| 711 |
|
| |
| 712 |
|
| |
| 713 |
|
| |
| 714 |
|
| MODULE = CSS::Minifier::XS PACKAGE = CSS::Minifier::XS |
| 715 |
|
| |
| 716 |
|
| PROTOTYPES: disable |
| 717 |
|
| |
| 718 |
|
| SV* |
| 719 |
|
| minify(string) |
| 720 |
|
| SV* string |
| 721 |
|
| INIT: |
| 722 |
54 |
| char* buffer = NULL; |
| 723 |
54 |
| RETVAL = &PL_sv_undef; |
| 724 |
|
| CODE: |
| 725 |
|
| /* minify the CSS */ |
| 726 |
54 |
| buffer = CssMinify( SvPVX(string) ); |
| 727 |
|
| /* hand back the minified CSS (if we had any) */ |
| 728 |
54 |
▶ | if (buffer != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (buffer != NULL) { |
53 |
1 |
54 |
|
| 729 |
53 |
| RETVAL = newSVpv(buffer, 0); |
| 730 |
53 |
| Safefree( buffer ); |
| 731 |
|
| } |
| 732 |
|
| OUTPUT: |
| 733 |
|
| RETVAL |