CSS-Minifier-XS-0.13000755001750001750 014007640721 14037 5ustar00grahamgraham000000000000XS.xs100644001750001750 5716014007640721 15057 0ustar00grahamgraham000000000000CSS-Minifier-XS-0.13#include #include #include #include #include #include #include const char* start_ie_hack = "/*\\*/"; const char* end_ie_hack = "/**/"; /* **************************************************************************** * CHARACTER CLASS METHODS * **************************************************************************** */ bool charIsSpace(char ch) { if (ch == ' ') return 1; if (ch == '\t') return 1; return 0; } bool charIsEndspace(char ch) { if (ch == '\n') return 1; if (ch == '\r') return 1; if (ch == '\f') return 1; return 0; } bool charIsWhitespace(char ch) { return charIsSpace(ch) || charIsEndspace(ch); } bool charIsNumeric(char ch) { if ((ch >= '0') && (ch <= '9')) return 1; return 0; } bool charIsIdentifier(char ch) { if ((ch >= 'a') && (ch <= 'z')) return 1; if ((ch >= 'A') && (ch <= 'Z')) return 1; if ((ch >= '0') && (ch <= '9')) return 1; if (ch == '_') return 1; if (ch == '.') return 1; if (ch == '#') return 1; if (ch == '@') return 1; if (ch == '%') return 1; return 0; } bool charIsInfix(char ch) { /* WS before+after these characters can be removed */ if (ch == '{') return 1; if (ch == '}') return 1; if (ch == ';') return 1; if (ch == ',') return 1; if (ch == '~') return 1; if (ch == '>') return 1; return 0; } bool charIsPrefix(char ch) { /* WS after these characters can be removed */ if (ch == '(') return 1; /* requires leading WS when used in @media */ if (ch == ':') return 1; /* requires leading WS when used in pseudo-selector */ return charIsInfix(ch); } bool charIsPostfix(char ch) { /* WS before these characters can be removed */ if (ch == ')') return 1; /* requires trailing WS for MSIE */ return charIsInfix(ch); } /* **************************************************************************** * TYPE DEFINITIONS * **************************************************************************** */ typedef enum { NODE_EMPTY, NODE_WHITESPACE, NODE_BLOCKCOMMENT, NODE_IDENTIFIER, NODE_LITERAL, NODE_SIGIL } NodeType; struct _Node; typedef struct _Node Node; struct _Node { /* linked list pointers */ Node* prev; Node* next; /* node internals */ const char* contents; size_t length; NodeType type; bool can_prune; }; #define NODE_SET_SIZE 50000 struct _NodeSet; typedef struct _NodeSet NodeSet; struct _NodeSet { /* link to next NodeSet */ NodeSet* next; /* Nodes in this Set */ Node nodes[NODE_SET_SIZE]; size_t next_node; }; typedef struct { /* singly linked list of NodeSets */ NodeSet* head_set; NodeSet* tail_set; /* doubly linked list of Nodes */ Node* head; Node* tail; /* doc internals */ const char* buffer; size_t length; size_t offset; } CssDoc; /* **************************************************************************** * NODE CHECKING MACROS/FUNCTIONS * **************************************************************************** */ /* checks to see if the node is the given string, case INSENSITIVELY */ bool nodeEquals(Node* node, const char* string) { /* not the same length? Not equal */ size_t len = strlen(string); if (len != node->length) return 0; /* compare contents to see if they're equal */ return (strncasecmp(node->contents, string, node->length) == 0); } /* checks to see if the node contains the given string, case INSENSITIVELY */ bool nodeContains(Node* node, const char* string) { const char* haystack = node->contents; const char* endofhay = haystack + node->length; size_t len = strlen(string); char ul_start[2] = { tolower(*string), toupper(*string) }; /* if node is shorter we know we're not going to have a match */ if (len > node->length) return 0; /* find the needle in the haystack */ while (haystack && *haystack) { /* find first char of needle */ haystack = strpbrk( haystack, ul_start ); /* didn't find it? Oh well. */ if (haystack == NULL) return 0; /* found it, but will the end be past the end of our node? */ if ((haystack+len) > endofhay) return 0; /* see if it matches */ if (strncasecmp(haystack, string, len) == 0) return 1; /* nope, move onto next character in the haystack */ haystack ++; } /* no match */ return 0; } /* checks to see if the node begins with the given string, case INSENSITIVELY. */ bool nodeBeginsWith(Node* node, const char* string) { /* If the string is longer than the node, it's not going to match */ size_t len = strlen(string); if (len > node->length) return 0; /* check for match */ return (strncasecmp(node->contents, string, len) == 0); } /* checks to see if the node ends with the given string, case INSENSITVELY. */ bool nodeEndsWith(Node* node, const char* string) { /* If the string is longer than the node, it's not going to match */ size_t len = strlen(string); if (len > node->length) return 0; /* check for match */ size_t off = node->length - len; return (strncasecmp(node->contents+off, string, len) == 0); } /* macros to help see what kind of node we've got */ #define nodeIsWHITESPACE(node) ((node->type == NODE_WHITESPACE)) #define nodeIsBLOCKCOMMENT(node) ((node->type == NODE_BLOCKCOMMENT)) #define nodeIsIDENTIFIER(node) ((node->type == NODE_IDENTIFIER)) #define nodeIsLITERAL(node) ((node->type == NODE_LITERAL)) #define nodeIsSIGIL(node) ((node->type == NODE_SIGIL)) #define nodeIsEMPTY(node) ((node->type == NODE_EMPTY) || ((node->length==0) || (node->contents==NULL))) #define nodeIsMACIECOMMENTHACK(node) (nodeIsBLOCKCOMMENT(node) && nodeEndsWith(node,"\\*/")) #define nodeIsPREFIXSIGIL(node) (nodeIsSIGIL(node) && charIsPrefix(node->contents[0])) #define nodeIsPOSTFIXSIGIL(node) (nodeIsSIGIL(node) && charIsPostfix(node->contents[0])) #define nodeIsCHAR(node,ch) ((node->contents[0]==ch) && (node->length==1)) /* checks if this node is the start of "!important" (with optional intravening * whitespace. */ bool nodeStartsBANGIMPORTANT(Node* node) { if (!node) return 0; /* Doesn't start with a "!", nope */ if (!nodeIsCHAR(node,'!')) return 0; /* Skip any following whitespace */ Node* next = node->next; while (next && nodeIsWHITESPACE(next)) { next = node->next; } if (!next) return 0; /* Next node _better be_ "important" */ if (!nodeIsIDENTIFIER(next)) return 0; if (nodeEquals(next, "important")) return 1; return 0; } /* **************************************************************************** * NODE MANIPULATION FUNCTIONS * **************************************************************************** */ /* allocates a new node */ Node* CssAllocNode(CssDoc* doc) { Node* node; NodeSet* set = doc->tail_set; /* if our current NodeSet is full, allocate a new NodeSet */ if (set->next_node >= NODE_SET_SIZE) { NodeSet* next_set; Newz(0, next_set, 1, NodeSet); set->next = next_set; doc->tail_set = next_set; set = next_set; } /* grab the next Node out of the NodeSet */ node = set->nodes + set->next_node; set->next_node ++; /* initialize the node */ node->prev = NULL; node->next = NULL; node->contents = NULL; node->length = 0; node->type = NODE_EMPTY; node->can_prune = 1; return node; } /* sets the contents of a node */ void CssSetNodeContents(Node* node, const char* string, size_t len) { node->contents = string; node->length = len; return; } /* removes the node from the list and discards it entirely */ void CssDiscardNode(Node* node) { if (node->prev) node->prev->next = node->next; if (node->next) node->next->prev = node->prev; } /* appends the node to the given element */ void CssAppendNode(Node* element, Node* node) { if (element->next) element->next->prev = node; node->next = element->next; node->prev = element; element->next = node; } /* **************************************************************************** * TOKENIZING FUNCTIONS * **************************************************************************** */ /* extracts a quoted literal string */ void _CssExtractLiteral(CssDoc* doc, Node* node) { const char* buf = doc->buffer; size_t offset = doc->offset; char delimiter = buf[offset]; /* skip start of literal */ offset ++; /* search for end of literal */ while (offset < doc->length) { if (buf[offset] == '\\') { /* escaped character; skip */ offset ++; } else if (buf[offset] == delimiter) { const char* start = buf + doc->offset; size_t length = offset - doc->offset + 1; CssSetNodeContents(node, start, length); node->type = NODE_LITERAL; return; } /* move onto next character */ offset ++; } croak( "unterminated quoted string literal" ); } /* extracts a block comment */ void _CssExtractBlockComment(CssDoc* doc, Node* node) { const char* buf = doc->buffer; size_t offset = doc->offset; /* skip start of comment */ offset ++; /* skip "/" */ offset ++; /* skip "*" */ /* search for end of comment block */ while (offset < doc->length) { if (buf[offset] == '*') { if (buf[offset+1] == '/') { const char* start = buf + doc->offset; size_t length = offset - doc->offset + 2; CssSetNodeContents(node, start, length); node->type = NODE_BLOCKCOMMENT; return; } } /* move onto next character */ offset ++; } croak( "unterminated block comment" ); } /* extracts a run of whitespace characters */ void _CssExtractWhitespace(CssDoc* doc, Node* node) { const char* buf = doc->buffer; size_t offset = doc->offset; while ((offset < doc->length) && charIsWhitespace(buf[offset])) offset ++; CssSetNodeContents(node, doc->buffer+doc->offset, offset-doc->offset); node->type = NODE_WHITESPACE; } /* extracts an identifier */ void _CssExtractIdentifier(CssDoc* doc, Node* node) { const char* buf = doc->buffer; size_t offset = doc->offset; while ((offset < doc->length) && charIsIdentifier(buf[offset])) offset++; CssSetNodeContents(node, doc->buffer+doc->offset, offset-doc->offset); node->type = NODE_IDENTIFIER; } /* extracts a -single- symbol/sigil */ void _CssExtractSigil(CssDoc* doc, Node* node) { CssSetNodeContents(node, doc->buffer+doc->offset, 1); node->type = NODE_SIGIL; } /* tokenizes the given string and returns the list of nodes */ Node* CssTokenizeString(CssDoc* doc, const char* string) { /* parse the CSS */ while ((doc->offset < doc->length) && (doc->buffer[doc->offset])) { /* allocate a new node */ Node* node = CssAllocNode(doc); if (!doc->head) doc->head = node; if (!doc->tail) doc->tail = node; /* parse the next node out of the CSS */ if ((doc->buffer[doc->offset] == '/') && (doc->buffer[doc->offset+1] == '*')) _CssExtractBlockComment(doc, node); else if ((doc->buffer[doc->offset] == '"') || (doc->buffer[doc->offset] == '\'')) _CssExtractLiteral(doc, node); else if (charIsWhitespace(doc->buffer[doc->offset])) _CssExtractWhitespace(doc, node); else if (charIsIdentifier(doc->buffer[doc->offset])) _CssExtractIdentifier(doc, node); else _CssExtractSigil(doc, node); /* move ahead to the end of the parsed node */ doc->offset += node->length; /* add the node to our list of nodes */ if (node != doc->tail) CssAppendNode(doc->tail, node); doc->tail = node; } /* return the node list */ return doc->head; } /* **************************************************************************** * MINIFICATION FUNCTIONS * **************************************************************************** */ /* Skips over any "zero value" found in the provided string, returning a * pointer to the next character after those zeros (which may be the same * as the pointer to ther original string, if no zeros were found). */ const char* CssSkipZeroValue(const char* str) { /* Skip leading zeros */ while (*str == '0') { str ++; } const char* after_leading_zeros = str; /* Decimal point, followed by more zeros? */ if (*str == '.') { str ++; while (*str == '0') { str ++; } if (charIsNumeric(*str)) { /* ends in digit; significant at the decimal point */ return after_leading_zeros; } return str; } /* Done. */ return after_leading_zeros; } /* checks to see if the string contains a known CSS unit */ bool CssIsKnownUnit(const char* str) { /* If it ends with a known Unit, its a Zero Unit */ if (0 == strncmp(str, "em", 2)) { return 1; } if (0 == strncmp(str, "ex", 2)) { return 1; } if (0 == strncmp(str, "ch", 2)) { return 1; } if (0 == strncmp(str, "rem", 3)) { return 1; } if (0 == strncmp(str, "vw", 2)) { return 1; } if (0 == strncmp(str, "vh", 2)) { return 1; } if (0 == strncmp(str, "vmin", 3)) { return 1; } if (0 == strncmp(str, "vmax", 3)) { return 1; } if (0 == strncmp(str, "cm", 2)) { return 1; } if (0 == strncmp(str, "mm", 2)) { return 1; } if (0 == strncmp(str, "in", 2)) { return 1; } if (0 == strncmp(str, "px", 2)) { return 1; } if (0 == strncmp(str, "pt", 2)) { return 1; } if (0 == strncmp(str, "pc", 2)) { return 1; } if (0 == strncmp(str, "%", 1)) { return 1; } /* Nope */ return 0; } /* collapses all of the nodes to their shortest possible representation */ void CssCollapseNodes(Node* curr) { bool inMacIeCommentHack = 0; bool inFunction = 0; while (curr) { Node* next = curr->next; switch (curr->type) { case NODE_WHITESPACE: /* collapse to a single whitespace character */ curr->length = 1; break; case NODE_BLOCKCOMMENT: if (!inMacIeCommentHack && nodeIsMACIECOMMENTHACK(curr)) { /* START of mac/ie hack */ CssSetNodeContents(curr, start_ie_hack, strlen(start_ie_hack)); curr->can_prune = 0; inMacIeCommentHack = 1; } else if (inMacIeCommentHack && !nodeIsMACIECOMMENTHACK(curr)) { /* END of mac/ie hack */ CssSetNodeContents(curr, end_ie_hack, strlen(end_ie_hack)); curr->can_prune = 0; inMacIeCommentHack = 0; } break; case NODE_IDENTIFIER: { /* if the node doesn't begin with a "zero", nothing to collapse */ const char* ptr = curr->contents; if ( (*ptr != '0') && (*ptr != '.' )) { /* not "0" and not "point-something" */ break; } if ( (*ptr == '.') && (*(ptr+1) != '0') ) { /* "point-something", but not "point-zero" */ break; } /* skip all leading zeros */ ptr = CssSkipZeroValue(curr->contents); /* if we didn't skip anything, no Zeros to collapse */ if (ptr == curr->contents) { break; } /* did we skip the entire thing, and thus the Node is "all zeros"? */ size_t skipped = ptr - curr->contents; if (skipped == curr->length) { /* nothing but zeros, so truncate to "0" */ CssSetNodeContents(curr, "0", 1); break; } /* was it a zero percentage? */ if (*ptr == '%') { /* a zero percentage; truncate to "0%" */ CssSetNodeContents(curr, "0%", 2); break; } /* if all we're left with is a known CSS unit, and we're NOT in * a function (where we have to preserve units), just truncate * to "0" */ if (!inFunction && CssIsKnownUnit(ptr)) { /* not in a function, and is a zero unit; truncate to "0" */ CssSetNodeContents(curr, "0", 1); break; } /* otherwise, just skip leading zeros, and preserve any unit */ /* ... do we need to back up one char to find a significant zero? */ if (*ptr != '.') { ptr --; } /* ... if that's not the start of the buffer ... */ if (ptr != curr->contents) { /* set the buffer to "0 + units", blowing away the earlier bits */ size_t len = curr->length - (ptr - curr->contents); CssSetNodeContents(curr, ptr, len); } break; } case NODE_SIGIL: if (nodeIsCHAR(curr,'(')) { inFunction = 1; } if (nodeIsCHAR(curr,')')) { inFunction = 0; } break; default: break; } curr = next; } } /* checks to see whether we can prune the given node from the list. * * THIS is the function that controls the bulk of the minification process. */ enum { PRUNE_NO, PRUNE_PREVIOUS, PRUNE_CURRENT, PRUNE_NEXT }; int CssCanPrune(Node* node) { Node* prev = node->prev; Node* next = node->next; /* only if node is prunable */ if (!node->can_prune) return PRUNE_NO; switch (node->type) { case NODE_EMPTY: /* prune empty nodes */ return PRUNE_CURRENT; case NODE_WHITESPACE: /* remove whitespace before comment blocks */ if (next && nodeIsBLOCKCOMMENT(next)) return PRUNE_CURRENT; /* remove whitespace after comment blocks */ if (prev && nodeIsBLOCKCOMMENT(prev)) return PRUNE_CURRENT; /* remove whitespace before "!important" */ if (next && nodeStartsBANGIMPORTANT(next)) { return PRUNE_CURRENT; } /* leading whitespace gets pruned */ if (!prev) return PRUNE_CURRENT; /* trailing whitespace gets pruned */ if (!next) return PRUNE_CURRENT; /* keep all other whitespace */ return PRUNE_NO; case NODE_BLOCKCOMMENT: /* keep comments that contain the word "copyright" */ if (nodeContains(node,"copyright")) return PRUNE_NO; /* remove comment blocks */ return PRUNE_CURRENT; case NODE_IDENTIFIER: /* keep all identifiers */ return PRUNE_NO; case NODE_LITERAL: /* keep all literals */ return PRUNE_NO; case NODE_SIGIL: /* remove whitespace after "prefix" sigils */ if (nodeIsPREFIXSIGIL(node) && next && nodeIsWHITESPACE(next)) return PRUNE_NEXT; /* remove whitespace before "postfix" sigils */ if (nodeIsPOSTFIXSIGIL(node) && prev && nodeIsWHITESPACE(prev)) return PRUNE_PREVIOUS; /* remove ";" characters at end of selector groups */ if (nodeIsCHAR(node,';') && next && nodeIsSIGIL(next) && nodeIsCHAR(next,'}')) return PRUNE_CURRENT; /* keep all other sigils */ return PRUNE_NO; } /* keep anything else */ return PRUNE_NO; } /* prune nodes from the list */ Node* CssPruneNodes(Node *head) { Node* curr = head; while (curr) { /* see if/how we can prune this node */ int prune = CssCanPrune(curr); /* prune. each block is responsible for moving onto the next node */ Node* prev = curr->prev; Node* next = curr->next; switch (prune) { case PRUNE_PREVIOUS: /* discard previous node */ CssDiscardNode(prev); /* reset "head" if that's what got pruned */ if (prev == head) head = curr; break; case PRUNE_CURRENT: /* discard current node */ CssDiscardNode(curr); /* reset "head" if that's what got pruned */ if (curr == head) head = prev ? prev : next; /* backup and try again if possible */ curr = prev ? prev : next; break; case PRUNE_NEXT: /* discard next node */ CssDiscardNode(next); /* stay on current node, and try again */ break; default: /* move ahead to next node */ curr = next; break; } } /* return the (possibly new) head node back to the caller */ return head; } /* **************************************************************************** * Minifies the given CSS, returning a newly allocated string back to the * caller (YOU'RE responsible for freeing its memory). * **************************************************************************** */ char* CssMinify(const char* string) { char* results; CssDoc doc; /* initialize our CSS document object */ doc.head = NULL; doc.tail = NULL; doc.buffer = string; doc.length = strlen(string); doc.offset = 0; Newz(0, doc.head_set, 1, NodeSet); doc.tail_set = doc.head_set; /* PASS 1: tokenize CSS into a list of nodes */ Node* head = CssTokenizeString(&doc, string); if (!head) return NULL; /* PASS 2: collapse nodes */ CssCollapseNodes(head); /* PASS 3: prune nodes */ head = CssPruneNodes(head); if (!head) return NULL; /* PASS 4: re-assemble CSS into single string */ { Node* curr; char* ptr; /* allocate the result buffer to the same size as the original CSS; in * a worst case scenario that's how much memory we'll need for it. */ Newz(0, results, (strlen(string)+1), char); ptr = results; /* copy node contents into result buffer */ curr = head; while (curr) { memcpy(ptr, curr->contents, curr->length); ptr += curr->length; curr = curr->next; } *ptr = 0; } /* free memory used by the NodeSets */ { NodeSet* curr = doc.head_set; while (curr) { NodeSet* next = curr->next; Safefree(curr); curr = next; } } /* return resulting minified CSS back to caller */ return results; } MODULE = CSS::Minifier::XS PACKAGE = CSS::Minifier::XS PROTOTYPES: disable SV* minify(string) SV* string INIT: char* buffer = NULL; RETVAL = &PL_sv_undef; CODE: /* minify the CSS */ buffer = CssMinify( SvPVX(string) ); /* hand back the minified CSS (if we had any) */ if (buffer != NULL) { RETVAL = newSVpv(buffer, 0); Safefree( buffer ); } OUTPUT: RETVAL README100644001750001750 662714007640721 15013 0ustar00grahamgraham000000000000CSS-Minifier-XS-0.13NAME CSS::Minifier::XS - XS based CSS minifier SYNOPSIS use CSS::Minifier::XS qw(minify); my $css = '...'; my $minified = minify($css); DESCRIPTION CSS::Minifier::XS is a CSS "minifier"; its designed to remove unnecessary whitespace and comments from CSS files, while also not breaking the CSS. CSS::Minifier::XS is similar in function to CSS::Minifier, but is substantially faster as its written in XS and not just pure Perl. METHODS minify($css) Minifies the given $css, returning the minified CSS back to the caller. HOW IT WORKS CSS::Minifier::XS minifies the CSS by removing unnecessary whitespace from CSS documents. Comment blocks are also removed, except when (a) they contain the word "copyright" in them, or (b) they're needed to implement the "Mac/IE Comment Hack". Internally, the minification is done by taking multiple passes through the CSS document: Pass 1: Tokenize First, we go through and parse the CSS document into a series of tokens internally. The tokenizing process does not check to make sure that you've got syntactically valid CSS, it just breaks up the text into a stream of tokens suitable for processing by the subsequent stages. Pass 2: Collapse We then march through the token list and collapse certain tokens down to their smallest possible representation. If they're still included in the final results we only want to include them at their shortest. Whitespace Runs of multiple whitespace characters are reduced down to a single whitespace character. If the whitespace contains any "end of line" (EOL) characters, then the end result is the first EOL character encountered. Otherwise, the result is the first whitespace character in the run. Comments Comments implementing the "Mac/IE Comment Hack" are collapsed down to the smallest possible comment that would still implement the hack ("/*\*/" to start the hack, and "/**/" to end it). Zero Units Zero Units (e.g. 0px) are reduced down to just "0", as the CSS specification indicates that the unit is not required when its a zero value. Pass 3: Pruning We then go back through the token list and prune and remove unnecessary tokens. Whitespace Wherever possible, whitespace is removed; before+after comment blocks, and before+after various symbols/sigils. Comments Comments that either (a) are needed to implement the "Mac/IE Comment Hack", or that (b) contain the word "copyright" in them are preserved. All other comments are removed. Symbols/Sigils Semi-colons that are immediately followed by a closing brace (e.g. ";}") are removed; semi-colons are needed to separate multiple declarations, but aren't required at the end of a group. Everything else We keep everything else; identifiers, quoted literal strings, symbols/sigils, etc. Pass 4: Re-assembly Lastly, we go back through the token list and re-assemble it all back into a single CSS string, which is then returned back to the caller. AUTHOR Graham TerMarsch (cpan@howlingfrog.com) COPYRIGHT Copyright (C) 2007-, Graham TerMarsch. All Rights Reserved. This is free software; you can redistribute it and/or modify it under the same license as Perl itself. SEE ALSO CSS::Minifier. Changes100644001750001750 764314007640721 15425 0ustar00grahamgraham000000000000CSS-Minifier-XS-0.13Revision history for Perl extension CSS::Minifier::XS. 0.13 2021-02-06 17:26:39-08:00 America/Vancouver - Internals; avoid allocating memory for each node as we tokenize the document, and simply use pointers back into original string. - Dramatically improves performance; local testing shows boost from ~25/s to ~85MB/s - Improve zero value minification further - Simplified whitespace compaction 0.12 2021-01-30 21:46:07-08:00 America/Vancouver - rewrote test suite into a single ".t" test - GH #1 / RT #97574; whitespace before a ":" in a pseudo-selector is meaningful and needs to be preserved (e.g. "#link :visited") - Further reductions of "zero values", when possible - "00000px" and "0.0px" become "0px" - "000%" and "0.0%" become "0%" - units are preserved inside of functions, but eliminated otherwise, and percentages are always left as a percentages - Optimized whitespace collapsing - Optimized memory usage and string copying 0.11 2020-12-30 21:27:39-08:00 America/Vancouver - POD spelling fixes - Switch to DZil Author Bundle 0.10 2020-12-28 11:00:17-08:00 America/Vancouver - RT #90879; correct minification of %s in "hsl()" and "hsla()" functions Thanks to Philipp Soehnlein - RT #103231; don't remove units on zero values inside of functions. Thanks to Isaac Montoya, for an additional test case. - No long drop units on zero percentages, as those may be required for CSS animations. Thanks to Isaac Montoya for continuing to poke me on this. - Now prunes leading whitespace before "!important" e.g. "color: red !important" becomes "color:red!important" - Switch to Dist::Zilla 0.09 Wed Oct 31 13:00 PDT 2013 - RT #85350; retain WS around "+" and "-" operators, so that we don't break the "calc()" function by accident. Thanks to Philipp Soehnlein - RT #60239; remove WS around ">" and "~" selector combinators Thanks to Jacob R. - RT #60238; remove units on zero values (e.g. "0px" -> "0") Thanks to Jacob R. - RT #85896 and #79792; allow builds on Perls older than 5.8.8 Thanks to Michael Robinton 0.08 Tue Nov 2 22:25 PDT 2010 - Bump Perl requirement to 5.8.8; oldest release with Newxz() 0.07 Wed Jul 21 21:09 PDT 2010 - RT #39978; use 'Newxz/Safefree' instead of 'malloc/free' for memory allocation. Thanks to Kenichi Ishigaki. 0.06 Wed Jul 21 20:44 PDT 2010 - RT #59549; '(' should retain leading WS, as needed for @media queries. Thanks to Mike Cardwell. 0.05 Fri Apr 23 22:29 PDT 2010 - Switch to Git 0.04 Thu Aug 6 22:03 PDT 2009 - fix invalid "L" POD sequence 0.03 Wed Jul 16 23:22 PDT 2008 - RT #36557: Nasty segfault on minifying comment-only css input. Turned out to be *any* input that minified to "nothing" caused the segfault, and only in certain Perl versions. 0.02 Tue May 6 00:16 PDT 2008 - rebuild packages; EU::MM borked my META.yml 0.01 Mon May 5 15:15 PDT 2008 - no changes from 0.01_06, but is non-devel 0.01_06 Wed Oct 31 20:39 PDT 2007 - retain whitespace after ")" characters; MSIE needs the whitespace, even though the W3 validator considers the results valid CSS. 0.01_05 Sat Oct 20 22:52 PDT 2007 - don't use "strcasestr()"; not available on Solaris 0.01_04 Wed Oct 17 15:57 PDT 2007 - fix t/02-minify.t so it doesn't try to "use_ok()" before issuing a test plan 0.01_03 Tue Oct 16 19:45 PDT 2007 - don't use "strndup()"; not available on all systems - we require Perl 5.006; update Build.PL and XS.pm to denote this 0.01_02 Tue Oct 16 12:18 PDT 2007 - relocate the XS file so that its picked up properly by EU::MM when running "perl Makefile.PL" to do a build. 0.01_01 Mon Oct 15 22:11 PDT 2007 - initial public version LICENSE100644001750001750 4367114007640721 15160 0ustar00grahamgraham000000000000CSS-Minifier-XS-0.13This software is copyright (c) 2021 by Graham TerMarsch. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. Terms of the Perl programming language system itself a) the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version, or b) the "Artistic License" --- The GNU General Public License, Version 1, February 1989 --- This software is Copyright (c) 2021 by Graham TerMarsch. This is free software, licensed under: The GNU General Public License, Version 1, February 1989 GNU GENERAL PUBLIC LICENSE Version 1, February 1989 Copyright (C) 1989 Free Software Foundation, Inc. 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The license agreements of most software companies try to keep users at the mercy of those companies. By contrast, our General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. The General Public License applies to the Free Software Foundation's software and to any other program whose authors commit to using it. You can use it for your programs, too. When we speak of free software, we are referring to freedom, not price. Specifically, the General Public License is designed to make sure that you have the freedom to give away or sell copies of free software, that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of a such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must tell them their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License Agreement applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any work containing the Program or a portion of it, either verbatim or with modifications. Each licensee is addressed as "you". 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this General Public License and to the absence of any warranty; and give any other recipients of the Program a copy of this General Public License along with the Program. You may charge a fee for the physical act of transferring a copy. 2. You may modify your copy or copies of the Program or any portion of it, and copy and distribute such modifications under the terms of Paragraph 1 above, provided that you also do the following: a) cause the modified files to carry prominent notices stating that you changed the files and the date of any change; and b) cause the whole of any work that you distribute or publish, that in whole or in part contains the Program or any part thereof, either with or without modifications, to be licensed at no charge to all third parties under the terms of this General Public License (except that you may choose to grant warranty protection to some or all third parties, at your option). c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the simplest and most usual way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this General Public License. d) You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. Mere aggregation of another independent work with the Program (or its derivative) on a volume of a storage or distribution medium does not bring the other work under the scope of these terms. 3. You may copy and distribute the Program (or a portion or derivative of it, under Paragraph 2) in object code or executable form under the terms of Paragraphs 1 and 2 above provided that you also do one of the following: a) accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Paragraphs 1 and 2 above; or, b) accompany it with a written offer, valid for at least three years, to give any third party free (except for a nominal charge for the cost of distribution) a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Paragraphs 1 and 2 above; or, c) accompany it with the information you received as to where the corresponding source code may be obtained. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form alone.) Source code for a work means the preferred form of the work for making modifications to it. For an executable file, complete source code means all the source code for all modules it contains; but, as a special exception, it need not include source code for modules which are standard libraries that accompany the operating system on which the executable file runs, or for standard header files or definitions files that accompany that operating system. 4. You may not copy, modify, sublicense, distribute or transfer the Program except as expressly provided under this General Public License. Any attempt otherwise to copy, modify, sublicense, distribute or transfer the Program is void, and will automatically terminate your rights to use the Program under this License. However, parties who have received copies, or rights to use copies, from you under this General Public License will not have their licenses terminated so long as such parties remain in full compliance. 5. By copying, distributing or modifying the Program (or any work based on the Program) you indicate your acceptance of this license to do so, and all its terms and conditions. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. 7. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of the license which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the license, you may choose any version ever published by the Free Software Foundation. 8. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 9. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 10. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS Appendix: How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to humanity, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) 19yy This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) 19xx name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (a program to direct compilers to make passes at assemblers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice That's all there is to it! --- The Artistic License 1.0 --- This software is Copyright (c) 2021 by Graham TerMarsch. This is free software, licensed under: The Artistic License 1.0 The Artistic License Preamble The intent of this document is to state the conditions under which a Package may be copied, such that the Copyright Holder maintains some semblance of artistic control over the development of the package, while giving the users of the package the right to use and distribute the Package in a more-or-less customary fashion, plus the right to make reasonable modifications. Definitions: - "Package" refers to the collection of files distributed by the Copyright Holder, and derivatives of that collection of files created through textual modification. - "Standard Version" refers to such a Package if it has not been modified, or has been modified in accordance with the wishes of the Copyright Holder. - "Copyright Holder" is whoever is named in the copyright or copyrights for the package. - "You" is you, if you're thinking about copying or distributing this Package. - "Reasonable copying fee" is whatever you can justify on the basis of media cost, duplication charges, time of people involved, and so on. (You will not be required to justify it to the Copyright Holder, but only to the computing community at large as a market that must bear the fee.) - "Freely Available" means that no fee is charged for the item itself, though there may be fees involved in handling the item. It also means that recipients of the item may redistribute it under the same conditions they received it. 1. You may make and give away verbatim copies of the source form of the Standard Version of this Package without restriction, provided that you duplicate all of the original copyright notices and associated disclaimers. 2. You may apply bug fixes, portability fixes and other modifications derived from the Public Domain or from the Copyright Holder. A Package modified in such a way shall still be considered the Standard Version. 3. You may otherwise modify your copy of this Package in any way, provided that you insert a prominent notice in each changed file stating how and when you changed that file, and provided that you do at least ONE of the following: a) place your modifications in the Public Domain or otherwise make them Freely Available, such as by posting said modifications to Usenet or an equivalent medium, or placing the modifications on a major archive site such as ftp.uu.net, or by allowing the Copyright Holder to include your modifications in the Standard Version of the Package. b) use the modified Package only within your corporation or organization. c) rename any non-standard executables so the names do not conflict with standard executables, which must also be provided, and provide a separate manual page for each non-standard executable that clearly documents how it differs from the Standard Version. d) make other distribution arrangements with the Copyright Holder. 4. You may distribute the programs of this Package in object code or executable form, provided that you do at least ONE of the following: a) distribute a Standard Version of the executables and library files, together with instructions (in the manual page or equivalent) on where to get the Standard Version. b) accompany the distribution with the machine-readable source of the Package with your modifications. c) accompany any non-standard executables with their corresponding Standard Version executables, giving the non-standard executables non-standard names, and clearly documenting the differences in manual pages (or equivalent), together with instructions on where to get the Standard Version. d) make other distribution arrangements with the Copyright Holder. 5. You may charge a reasonable copying fee for any distribution of this Package. You may charge any fee you choose for support of this Package. You may not charge a fee for this Package itself. However, you may distribute this Package in aggregate with other (possibly commercial) programs as part of a larger (possibly commercial) software distribution provided that you do not advertise this Package as a product of your own. 6. The scripts and library files supplied as input to or produced as output from the programs of this Package do not automatically fall under the copyright of this Package, but belong to whomever generated them, and may be sold commercially, and may be aggregated with this Package. 7. C or perl subroutines supplied by you and linked into this Package shall not be considered part of this Package. 8. The name of the Copyright Holder may not be used to endorse or promote products derived from this software without specific prior written permission. 9. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. The End MANIFEST100644001750001750 110414007640721 15245 0ustar00grahamgraham000000000000CSS-Minifier-XS-0.13# This file was automatically generated by Dist::Zilla::Plugin::Manifest v6.017. Changes LICENSE MANIFEST META.json META.yml Makefile.PL README XS.xs lib/CSS/Minifier/XS.pm t/00-report-prereqs.dd t/00-report-prereqs.t t/01-compile.t t/minify.t xt/author/clean-namespaces.t xt/author/comparison.t xt/author/distmeta.t xt/author/eof.t xt/author/eol.t xt/author/leaks.t xt/author/minimum-version.t xt/author/no-breakpoints.t xt/author/no-tabs.t xt/author/pod-coverage.t xt/author/pod-spell.t xt/author/pod-syntax.t xt/author/synopsis.t xt/release/kwalitee.t xt/release/unused-vars.t META.yml100644001750001750 170614007640721 15375 0ustar00grahamgraham000000000000CSS-Minifier-XS-0.13--- abstract: 'XS based CSS minifier' author: - 'Graham TerMarsch ' build_requires: ExtUtils::MakeMaker: '0' File::Spec: '0' File::Temp: '0' IO::Handle: '0' IPC::Open3: '0' Test::DiagINC: '0.002' Test::More: '0.96' configure_requires: ExtUtils::MakeMaker: '0' dynamic_config: 0 generated_by: 'Dist::Zilla version 6.017, CPAN::Meta::Converter version 2.150010' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: '1.4' name: CSS-Minifier-XS no_index: directory: - t - xt requires: perl: '5.008001' resources: bugtracker: https://github.com/bleargh45/CSS-Minifier-XS/issues homepage: http://metacpan.org/release/CSS-Minifier-XS/ repository: git://github.com/bleargh45/CSS-Minifier-XS.git version: '0.13' x_generated_by_perl: v5.32.1 x_serialization_backend: 'YAML::Tiny version 1.73' x_spdx_expression: 'Artistic-1.0-Perl OR GPL-1.0-or-later' x_static_install: 0 META.json100644001750001750 516714007640721 15552 0ustar00grahamgraham000000000000CSS-Minifier-XS-0.13{ "abstract" : "XS based CSS minifier", "author" : [ "Graham TerMarsch " ], "dynamic_config" : 0, "generated_by" : "Dist::Zilla version 6.017, CPAN::Meta::Converter version 2.150010", "license" : [ "perl_5" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : 2 }, "name" : "CSS-Minifier-XS", "no_index" : { "directory" : [ "t", "xt" ] }, "prereqs" : { "configure" : { "requires" : { "ExtUtils::MakeMaker" : "0" } }, "develop" : { "requires" : { "CSS::Compressor" : "0", "CSS::Minifier" : "0", "Dist::Zilla" : "5", "Dist::Zilla::PluginBundle::Author::GTERMARS" : "0", "File::Which" : "0", "IPC::Run" : "0", "Number::Format" : "0", "Pod::Coverage::TrustPod" : "0", "Software::License::Perl_5" : "0", "Test::CPAN::Meta" : "0", "Test::CleanNamespaces" : "0.15", "Test::EOF" : "0", "Test::EOL" : "0", "Test::Kwalitee" : "1.21", "Test::LeakTrace" : "0", "Test::MinimumVersion" : "0", "Test::More" : "0.88", "Test::NoBreakpoints" : "0.15", "Test::NoTabs" : "0", "Test::Pod" : "1.41", "Test::Pod::Coverage" : "1.08", "Test::Spelling" : "0.12", "Test::Synopsis" : "0" } }, "runtime" : { "requires" : { "perl" : "5.008001" } }, "test" : { "recommends" : { "CPAN::Meta" : "2.120900" }, "requires" : { "ExtUtils::MakeMaker" : "0", "File::Spec" : "0", "File::Temp" : "0", "IO::Handle" : "0", "IPC::Open3" : "0", "Test::DiagINC" : "0.002", "Test::More" : "0.96" } } }, "release_status" : "stable", "resources" : { "bugtracker" : { "web" : "https://github.com/bleargh45/CSS-Minifier-XS/issues" }, "homepage" : "http://metacpan.org/release/CSS-Minifier-XS/", "repository" : { "type" : "git", "url" : "git://github.com/bleargh45/CSS-Minifier-XS.git", "web" : "https://github.com/bleargh45/CSS-Minifier-XS" } }, "version" : "0.13", "x_generated_by_perl" : "v5.32.1", "x_serialization_backend" : "Cpanel::JSON::XS version 4.24", "x_spdx_expression" : "Artistic-1.0-Perl OR GPL-1.0-or-later", "x_static_install" : 0 } t000755001750001750 014007640721 14223 5ustar00grahamgraham000000000000CSS-Minifier-XS-0.13minify.t100644001750001750 3740514007640721 16074 0ustar00grahamgraham000000000000CSS-Minifier-XS-0.13/t#!/usr/bin/perl use strict; use warnings; use if $ENV{AUTOMATED_TESTING}, 'Test::DiagINC'; use Test::More; use CSS::Minifier::XS qw(minify); ############################################################################### # Removal of leading whitespace, regardless of "space", "tab", "CR", "LF". subtest 'leading whitespace can be removed' => sub { my $given = "\n\n\r\t\n \n h1 { }"; my $expect = 'h1{}'; my $got = minify($given); is $got, $expect; }; ############################################################################### # Removal of trailing whitespace, regardless of "space", "tab", "CR", "LF". subtest 'trailing whitespace can be removed' => sub { my $given = "h1 { } \t\n\r\n"; my $expect = 'h1{}'; my $got = minify($given); is $got, $expect; }; ############################################################################### # Whitespace that trails a ")" may be preserved. subtest 'whitespace trailing a ")"' => sub { subtest 'end of statement; removed' => sub { my $given = 'div { background: url(foo.gif) ; }'; my $expect = 'div{background:url(foo.gif)}'; my $got = minify($given); is $got, $expect; }; subtest 'inside of statement; preserved' => sub { my $given = 'div { background: url(foo.gif) no-repeat; }'; my $expect = 'div{background:url(foo.gif) no-repeat}'; my $got = minify($given); is $got, $expect; }; }; ############################################################################### # Removal of trailing semi-colons, at end of a group. subtest 'trailing semi-colons' => sub { subtest 'at end of group; removed' => sub { my $given = 'h1 { font-weight: bold; }'; my $expect = 'h1{font-weight:bold}'; my $got = minify($given); is $got, $expect; }; subtest 'inside of group; preserved' => sub { my $given = 'h1 { font-weight: bold; color: red;}'; my $expect = 'h1{font-weight:bold;color:red}'; my $got = minify($given); is $got, $expect; }; }; ############################################################################### # Comments get minified and removed, UNLESS they contain the word # "copyright" in them. subtest 'comments' => sub { subtest 'block comment removed' => sub { my $given = '/* comment */ h1 { background: green }'; my $expect = 'h1{background:green}'; my $got = minify($given); is $got, $expect; }; subtest 'copyright comment remains' => sub { my $given = '/* comment with copyright */ h1 { color: red }'; my $expect = '/* comment with copyright */h1{color:red}'; my $got = minify($given); is $got, $expect; }; subtest 'copyright is case in-sensitive' => sub { my $given = '/* comment with CoPyRiGhT */ h1 { color: red }'; my $expect = '/* comment with CoPyRiGhT */h1{color:red}'; my $got = minify($given); is $got, $expect; }; }; ############################################################################### # Comments with the "Mac/IE Comment Hack" are preserved, but minified. subtest 'mac/ie comment hack' => sub { subtest 'comment hack is minified' => sub { my $given = '/* start \*/ ul { color: red } /* end */'; my $expect = '/*\*/ul{color:red}/**/'; my $got = minify($given); is $got, $expect; }; subtest 'inner hacks are removed' => sub { my $given = '/* start \*/ ul { /* inner \*/ color: red } /* end */'; my $expect = '/*\*/ul{color:red}/**/'; my $got = minify($given); is $got, $expect; }; }; ############################################################################### # "!important" declarations can have preceeding whitespace removed. subtest '!important' => sub { my $given = 'a { box-shadow: none !important; }'; my $expect = 'a{box-shadow:none!important}'; my $got = minify($given); is $got, $expect; }; ############################################################################### # CSS Selector Combinators get whitespace removed subtest 'css selector combinators' => sub { subtest 'child-of' => sub { my $given = 'h1 > p { background: green }'; my $expect = 'h1>p{background:green}'; my $got = minify($given); is $got, $expect; }; subtest 'adjacent-sibling' => sub { # adjacent siblings don't get whitespace removed, as the "+" could also # be seen as a mathematical operator my $given = 'h1 + p { background: green }'; my $expect = 'h1 + p{background:green}'; my $got = minify($given); is $got, $expect; }; subtest 'general-sibling' => sub { my $given = 'h1 ~ p { background: green }'; my $expect = 'h1~p{background:green}'; my $got = minify($given); is $got, $expect; }; }; ############################################################################### # CSS pseudo-selectors get leading whitespace preserved # - which (unfortunately) means that "whitespace before a ':' is significant", # as otherwise we need to truly understand the context that we are in when # we see a ":" to determine if we can eliminate the whitespace or not subtest 'pseudo-selectors' => sub { my $given = '#test :link { color: red; }'; my $expect = '#test :link{color:red}'; my $got = minify($given); is $got, $expect; }; ############################################################################### # Media selectors require "(" to retain leading whitespace, which is # minified. subtest 'media selectors' => sub { subtest 'whitespace is preserved' => sub { my $given = '@media all and (max-width: 100px) { }'; my $expect = '@media all and (max-width:100px){}'; my $got = minify($given); is $got, $expect; }; subtest 'whitespace is minified' => sub { my $given = '@media all and (max-width: 100px) { }'; my $expect = '@media all and (max-width:100px){}'; my $got = minify($given); is $got, $expect; }; }; ############################################################################### # "zero" can be expressed without a unit, in *MOST* cases. subtest 'zero without units' => sub { subtest 'px' => sub { my $given = 'p { width: 0px }'; my $expect = 'p{width:0}'; my $got = minify($given); is $got, $expect; }; subtest 'no units' => sub { my $given = 'p { width: 0 }'; my $expect = 'p{width:0}'; my $got = minify($given); is $got, $expect; }; # Percent is special, and may need to be preserved for CSS animations subtest 'percent' => sub { my $given = 'p { width: 0% }'; my $expect = 'p{width:0%}'; my $got = minify($given); is $got, $expect; }; # Inside of a function, units/zeros are preserved. subtest 'inside a function' => sub { my $given = 'p { width: calc(300px - 0px) }'; my $expect = 'p{width:calc(300px - 0px)}'; my $got = minify($given); is $got, $expect; }; }; ############################################################################### # "point zero" can be expressed without a unit, in *MOST* cases. subtest 'point-zero without units' => sub { subtest 'px' => sub { my $given = 'p { width: .0px }'; my $expect = 'p{width:0}'; my $got = minify($given); is $got, $expect; }; subtest 'no units' => sub { my $given = 'p { width: .0 }'; my $expect = 'p{width:0}'; my $got = minify($given); is $got, $expect; }; # Percent is special, and may need to be preserved for CSS animations, # but will be minified from "point-zero" to just "zero". subtest 'percent' => sub { my $given = 'p { width: .0% }'; my $expect = 'p{width:0%}'; my $got = minify($given); is $got, $expect; }; # Inside of a function, units/zeros are preserved. subtest 'inside a function' => sub { my $given = 'p { width: calc(300px - .0px) }'; my $expect = 'p{width:calc(300px - 0px)}'; my $got = minify($given); is $got, $expect; }; }; ############################################################################### # "point zero something" requires preservation subtest 'point-zero without units' => sub { subtest 'px' => sub { my $given = 'p { width: .001px }'; my $expect = 'p{width:.001px}'; my $got = minify($given); is $got, $expect; }; subtest 'no units' => sub { my $given = 'p { width: .001 }'; my $expect = 'p{width:.001}'; my $got = minify($given); is $got, $expect; }; # Percent is special, and may need to be preserved for CSS animations subtest 'percent' => sub { my $given = 'p { width: .001% }'; my $expect = 'p{width:.001%}'; my $got = minify($given); is $got, $expect; }; # Inside of a function, units/zeros are preserved. subtest 'inside a function' => sub { my $given = 'p { width: calc(300px - .001px) }'; my $expect = 'p{width:calc(300px - .001px)}'; my $got = minify($given); is $got, $expect; }; }; ############################################################################### # "something point zero" requires preservation of the unit subtest 'something-point-zero preserves units' => sub { subtest 'px' => sub { my $given = 'p { width: 1.0px }'; my $expect = 'p{width:1.0px}'; my $got = minify($given); is $got, $expect; }; # Percent is special, and may need to be preserved for CSS animations subtest 'percent' => sub { my $given = 'p { width: 1.0% }'; my $expect = 'p{width:1.0%}'; my $got = minify($given); is $got, $expect; }; # Inside of a function, units/zeros are preserved. subtest 'inside a function' => sub { my $given = 'p { width: calc(300px - 1.0px) }'; my $expect = 'p{width:calc(300px - 1.0px)}'; my $got = minify($given); is $got, $expect; }; }; ############################################################################### # "zero point zero" can be expressed without a unit, in *MOST* cases. subtest 'zero-point-zero without units' => sub { subtest 'px' => sub { my $given = 'p { width: 0.0px }'; my $expect = 'p{width:0}'; my $got = minify($given); is $got, $expect; }; subtest 'no units' => sub { my $given = 'p { width: 0.0 }'; my $expect = 'p{width:0}'; my $got = minify($given); is $got, $expect; }; # Percent is special, and may need to be preserved for CSS animations, # but will be minified from "zero-point-zero" to just "zero". subtest 'percent' => sub { my $given = 'p { width: 0.0% }'; my $expect = 'p{width:0%}'; my $got = minify($given); is $got, $expect; }; # Inside of a function, units/zeros are preserved. subtest 'inside a function' => sub { my $given = 'p { width: calc(300px - 0.0px) }'; my $expect = 'p{width:calc(300px - 0px)}'; my $got = minify($given); is $got, $expect; }; }; ############################################################################### # "zero point zero somethihg" requires preservation subtest 'zero-point-zero without units' => sub { subtest 'px' => sub { my $given = 'p { width: 0.001px }'; my $expect = 'p{width:.001px}'; my $got = minify($given); is $got, $expect; }; subtest 'no units' => sub { my $given = 'p { width: 0.001 }'; my $expect = 'p{width:.001}'; my $got = minify($given); is $got, $expect; }; # Percent is special, and may need to be preserved for CSS animations, # but will be minified from "zero-point-zero-something" to just # "point-zero-something" subtest 'percent' => sub { my $given = 'p { width: 0.001% }'; my $expect = 'p{width:.001%}'; my $got = minify($given); is $got, $expect; }; # Inside of a function, units/zeros are preserved. subtest 'inside a function' => sub { my $given = 'p { width: calc(300px - 0.001px) }'; my $expect = 'p{width:calc(300px - .001px)}'; my $got = minify($given); is $got, $expect; }; }; ############################################################################### # "zerooooooo" can be expressed without a unit, in *MOST* cases. subtest 'zerooooooo without units' => sub { subtest 'px' => sub { my $given = 'p { width: 0000px }'; my $expect = 'p{width:0}'; my $got = minify($given); is $got, $expect; }; subtest 'no units' => sub { my $given = 'p { width: 0000 }'; my $expect = 'p{width:0}'; my $got = minify($given); is $got, $expect; }; # Percent is special, and may need to be preserved for CSS animations, # but will be minified from "zerooooo" to just "zero". subtest 'percent' => sub { my $given = 'p { width: 000% }'; my $expect = 'p{width:0%}'; my $got = minify($given); is $got, $expect; }; # Inside of a function, units/zeros are preserved. subtest 'inside a function' => sub { my $given = 'p { width: calc(300px - 0000px) }'; my $expect = 'p{width:calc(300px - 0px)}'; my $got = minify($given); is $got, $expect; }; }; ############################################################################### # "zerooooo-point-zeroooo" can be expressed without a unit, in *MOST* cases. subtest 'zerooooo-point-zeroooo without units' => sub { subtest 'px' => sub { my $given = 'p { width: 000.0000px }'; my $expect = 'p{width:0}'; my $got = minify($given); is $got, $expect; }; # Percent is special, and may need to be preserved for CSS animations, # but will be minified from "zerooo-point-zerooo" to just "zero". subtest 'percent' => sub { my $given = 'p { width: 000.000% }'; my $expect = 'p{width:0%}'; my $got = minify($given); is $got, $expect; }; # Inside of a function, units/zeros are preserved. subtest 'inside a function' => sub { my $given = 'p { width: calc(300px - 000.0000px) }'; my $expect = 'p{width:calc(300px - 0px)}'; my $got = minify($given); is $got, $expect; }; }; ############################################################################### # Inside of "calc()", whitespace surrounding operators needs to be preserved. subtest 'calc() method' => sub { subtest 'plus' => sub { my $given = 'h1 { width: calc( 100px + 10% ) }'; my $expect = 'h1{width:calc(100px + 10%)}'; my $got = minify($given); is $got, $expect; }; subtest 'minus' => sub { my $given = 'h2 { width: calc(100% - 30px ) }'; my $expect = 'h2{width:calc(100% - 30px)}'; my $got = minify($given); is $got, $expect; }; subtest 'times' => sub { my $given = 'h3 { width: calc( 60px * 2 ) }'; my $expect = 'h3{width:calc(60px * 2)}'; my $got = minify($given); is $got, $expect; }; subtest 'divide' => sub { my $given = 'h4 { width: calc( 100% / 2 ) }'; my $expect = 'h4{width:calc(100% / 2)}'; my $got = minify($given); is $got, $expect; }; }; ############################################################################### # RT #36557: Nasty segfault on minifying comment-only css input # # Actually turns out to be that *anything* that minifies to "nothing" causes # a segfault in Perl-5.8. Perl-5.10 seems immune. subtest 'minifies to nothing' => sub { my $given = '/* */'; my $expect = undef; my $got = minify($given); is $got, $expect; }; ############################################################################### # General/broad test subtest 'general/broad test' => sub { my $given = "/* comment to be removed */\n" . "\@import url(more.css );\n\n" . "\tbody, td, th {\nfont-family: Verdana, 'Bitstream Vera Sans';}\n" . ".nav {\n\tmargin-left:20%\n}" . "#main-nav { background-color: red; border: 1px solid yellow; }\n" . "div#content h1 + p\t{padding-top: 000;}"; my $expect = "" . "\@import url(more.css);" . "body,td,th{font-family:Verdana,'Bitstream Vera Sans'}" . ".nav{margin-left:20%}" . "#main-nav{background-color:red;border:1px solid yellow}" . "div#content h1 + p{padding-top:0}"; my $got = minify($given); is $got, $expect; }; ############################################################################### done_testing(); Makefile.PL100644001750001750 243214007640721 16073 0ustar00grahamgraham000000000000CSS-Minifier-XS-0.13# This file was automatically generated by Dist::Zilla::Plugin::MakeMaker v6.017. use strict; use warnings; use 5.008001; use ExtUtils::MakeMaker; my %WriteMakefileArgs = ( "ABSTRACT" => "XS based CSS minifier", "AUTHOR" => "Graham TerMarsch ", "CONFIGURE_REQUIRES" => { "ExtUtils::MakeMaker" => 0 }, "DISTNAME" => "CSS-Minifier-XS", "LICENSE" => "perl", "MIN_PERL_VERSION" => "5.008001", "NAME" => "CSS::Minifier::XS", "PREREQ_PM" => {}, "TEST_REQUIRES" => { "ExtUtils::MakeMaker" => 0, "File::Spec" => 0, "File::Temp" => 0, "IO::Handle" => 0, "IPC::Open3" => 0, "Test::DiagINC" => "0.002", "Test::More" => "0.96" }, "VERSION" => "0.13", "test" => { "TESTS" => "t/*.t" } ); my %FallbackPrereqs = ( "ExtUtils::MakeMaker" => 0, "File::Spec" => 0, "File::Temp" => 0, "IO::Handle" => 0, "IPC::Open3" => 0, "Test::DiagINC" => "0.002", "Test::More" => "0.96" ); unless ( eval { ExtUtils::MakeMaker->VERSION(6.63_03) } ) { delete $WriteMakefileArgs{TEST_REQUIRES}; delete $WriteMakefileArgs{BUILD_REQUIRES}; $WriteMakefileArgs{PREREQ_PM} = \%FallbackPrereqs; } delete $WriteMakefileArgs{CONFIGURE_REQUIRES} unless eval { ExtUtils::MakeMaker->VERSION(6.52) }; WriteMakefile(%WriteMakefileArgs); 01-compile.t100644001750001750 303714007640721 16421 0ustar00grahamgraham000000000000CSS-Minifier-XS-0.13/tuse 5.006; use strict; use warnings; # this test was generated with Dist::Zilla::Plugin::Test::Compile 2.058 use if $ENV{AUTOMATED_TESTING}, 'Test::DiagINC'; use Test::More 0.94; plan tests => 1 + ($ENV{AUTHOR_TESTING} ? 1 : 0); my @module_files = ( 'CSS/Minifier/XS.pm' ); # fake home for cpan-testers use File::Temp; local $ENV{HOME} = File::Temp::tempdir( CLEANUP => 1 ); my @switches = ( -d 'blib' ? '-Mblib' : '-Ilib', ); use File::Spec; use IPC::Open3; use IO::Handle; open my $stdin, '<', File::Spec->devnull or die "can't open devnull: $!"; my @warnings; for my $lib (@module_files) { # see L my $stderr = IO::Handle->new; diag('Running: ', join(', ', map { my $str = $_; $str =~ s/'/\\'/g; q{'} . $str . q{'} } $^X, @switches, '-e', "require q[$lib]")) if $ENV{PERL_COMPILE_TEST_DEBUG}; my $pid = open3($stdin, '>&STDERR', $stderr, $^X, @switches, '-e', "require q[$lib]"); binmode $stderr, ':crlf' if $^O eq 'MSWin32'; my @_warnings = <$stderr>; waitpid($pid, 0); is($?, 0, "$lib loaded ok"); shift @_warnings if @_warnings and $_warnings[0] =~ /^Using .*\bblib/ and not eval { +require blib; blib->VERSION('1.01') }; if (@_warnings) { warn @_warnings; push @warnings, @_warnings; } } is(scalar(@warnings), 0, 'no warnings found') or diag 'got warnings: ', explain(\@warnings) if $ENV{AUTHOR_TESTING}; BAIL_OUT("Compilation problems") if !Test::More->builder->is_passing; author000755001750001750 014007640721 15715 5ustar00grahamgraham000000000000CSS-Minifier-XS-0.13/xteol.t100644001750001750 54214007640721 17002 0ustar00grahamgraham000000000000CSS-Minifier-XS-0.13/xt/authoruse strict; use warnings; # this test was generated with Dist::Zilla::Plugin::Test::EOL 0.19 use Test::More 0.88; use Test::EOL; my @files = ( 'lib/CSS/Minifier/XS.pm', 't/00-report-prereqs.dd', 't/00-report-prereqs.t', 't/01-compile.t', 't/minify.t' ); eol_unix_ok($_, { trailing_whitespace => 1 }) foreach @files; done_testing; eof.t100644001750001750 43714007640721 16777 0ustar00grahamgraham000000000000CSS-Minifier-XS-0.13/xt/authoruse strict; use warnings; use Test::More; # Generated by Dist::Zilla::Plugin::Test::EOF 0.0600 eval "use Test::EOF"; plan skip_all => 'Test::EOF required to test for correct end of file flag' if $@; all_perl_files_ok({ minimum_newlines => 1, maximum_newlines => 4 }); done_testing(); leaks.t100644001750001750 175214007640721 17346 0ustar00grahamgraham000000000000CSS-Minifier-XS-0.13/xt/author#!/usr/bin/perl use strict; use warnings; use Test::More; use File::Which qw(which); use CSS::Minifier::XS qw(minify); BEGIN { eval "use Test::LeakTrace"; plan skip_all => "Test::LeakTrace required for leak testing" if $@; } use Test::LeakTrace; ############################################################################### # What CSS docs do we want to try compressing? my $curl = which('curl'); my @libs = qw( https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.css https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.css ); ############################################################################### # Make sure we're not leaking memory when we minify foreach my $url (@libs) { subtest $url => sub { my $css = qx{$curl --silent $url}; ok $css, 'got some CSS to minify'; no_leaks_ok { minify($css) } "no leaks when minifying; $url"; }; } ############################################################################### done_testing(); no-tabs.t100644001750001750 51014007640721 17561 0ustar00grahamgraham000000000000CSS-Minifier-XS-0.13/xt/authoruse strict; use warnings; # this test was generated with Dist::Zilla::Plugin::Test::NoTabs 0.15 use Test::More 0.88; use Test::NoTabs; my @files = ( 'lib/CSS/Minifier/XS.pm', 't/00-report-prereqs.dd', 't/00-report-prereqs.t', 't/01-compile.t', 't/minify.t' ); notabs_ok($_) foreach @files; done_testing; distmeta.t100644001750001750 17214007640721 20034 0ustar00grahamgraham000000000000CSS-Minifier-XS-0.13/xt/author#!perl # This file was automatically generated by Dist::Zilla::Plugin::MetaTests. use Test::CPAN::Meta; meta_yaml_ok(); synopsis.t100644001750001750 6014007640721 20065 0ustar00grahamgraham000000000000CSS-Minifier-XS-0.13/xt/author#!perl use Test::Synopsis; all_synopsis_ok(); 00-report-prereqs.t100644001750001750 1353314007640721 20004 0ustar00grahamgraham000000000000CSS-Minifier-XS-0.13/t#!perl use strict; use warnings; # This test was generated by Dist::Zilla::Plugin::Test::ReportPrereqs 0.028 use if $ENV{AUTOMATED_TESTING}, 'Test::DiagINC'; use Test::More tests => 1; use ExtUtils::MakeMaker; use File::Spec; # from $version::LAX my $lax_version_re = qr/(?: undef | (?: (?:[0-9]+) (?: \. | (?:\.[0-9]+) (?:_[0-9]+)? )? | (?:\.[0-9]+) (?:_[0-9]+)? ) | (?: v (?:[0-9]+) (?: (?:\.[0-9]+)+ (?:_[0-9]+)? )? | (?:[0-9]+)? (?:\.[0-9]+){2,} (?:_[0-9]+)? ) )/x; # hide optional CPAN::Meta modules from prereq scanner # and check if they are available my $cpan_meta = "CPAN::Meta"; my $cpan_meta_pre = "CPAN::Meta::Prereqs"; my $HAS_CPAN_META = eval "require $cpan_meta; $cpan_meta->VERSION('2.120900')" && eval "require $cpan_meta_pre"; ## no critic # Verify requirements? my $DO_VERIFY_PREREQS = 1; sub _max { my $max = shift; $max = ( $_ > $max ) ? $_ : $max for @_; return $max; } sub _merge_prereqs { my ($collector, $prereqs) = @_; # CPAN::Meta::Prereqs object if (ref $collector eq $cpan_meta_pre) { return $collector->with_merged_prereqs( CPAN::Meta::Prereqs->new( $prereqs ) ); } # Raw hashrefs for my $phase ( keys %$prereqs ) { for my $type ( keys %{ $prereqs->{$phase} } ) { for my $module ( keys %{ $prereqs->{$phase}{$type} } ) { $collector->{$phase}{$type}{$module} = $prereqs->{$phase}{$type}{$module}; } } } return $collector; } my @include = qw( ); my @exclude = qw( ); # Add static prereqs to the included modules list my $static_prereqs = do './t/00-report-prereqs.dd'; # Merge all prereqs (either with ::Prereqs or a hashref) my $full_prereqs = _merge_prereqs( ( $HAS_CPAN_META ? $cpan_meta_pre->new : {} ), $static_prereqs ); # Add dynamic prereqs to the included modules list (if we can) my ($source) = grep { -f } 'MYMETA.json', 'MYMETA.yml'; my $cpan_meta_error; if ( $source && $HAS_CPAN_META && (my $meta = eval { CPAN::Meta->load_file($source) } ) ) { $full_prereqs = _merge_prereqs($full_prereqs, $meta->prereqs); } else { $cpan_meta_error = $@; # capture error from CPAN::Meta->load_file($source) $source = 'static metadata'; } my @full_reports; my @dep_errors; my $req_hash = $HAS_CPAN_META ? $full_prereqs->as_string_hash : $full_prereqs; # Add static includes into a fake section for my $mod (@include) { $req_hash->{other}{modules}{$mod} = 0; } for my $phase ( qw(configure build test runtime develop other) ) { next unless $req_hash->{$phase}; next if ($phase eq 'develop' and not $ENV{AUTHOR_TESTING}); for my $type ( qw(requires recommends suggests conflicts modules) ) { next unless $req_hash->{$phase}{$type}; my $title = ucfirst($phase).' '.ucfirst($type); my @reports = [qw/Module Want Have/]; for my $mod ( sort keys %{ $req_hash->{$phase}{$type} } ) { next if $mod eq 'perl'; next if grep { $_ eq $mod } @exclude; my $file = $mod; $file =~ s{::}{/}g; $file .= ".pm"; my ($prefix) = grep { -e File::Spec->catfile($_, $file) } @INC; my $want = $req_hash->{$phase}{$type}{$mod}; $want = "undef" unless defined $want; $want = "any" if !$want && $want == 0; my $req_string = $want eq 'any' ? 'any version required' : "version '$want' required"; if ($prefix) { my $have = MM->parse_version( File::Spec->catfile($prefix, $file) ); $have = "undef" unless defined $have; push @reports, [$mod, $want, $have]; if ( $DO_VERIFY_PREREQS && $HAS_CPAN_META && $type eq 'requires' ) { if ( $have !~ /\A$lax_version_re\z/ ) { push @dep_errors, "$mod version '$have' cannot be parsed ($req_string)"; } elsif ( ! $full_prereqs->requirements_for( $phase, $type )->accepts_module( $mod => $have ) ) { push @dep_errors, "$mod version '$have' is not in required range '$want'"; } } } else { push @reports, [$mod, $want, "missing"]; if ( $DO_VERIFY_PREREQS && $type eq 'requires' ) { push @dep_errors, "$mod is not installed ($req_string)"; } } } if ( @reports ) { push @full_reports, "=== $title ===\n\n"; my $ml = _max( map { length $_->[0] } @reports ); my $wl = _max( map { length $_->[1] } @reports ); my $hl = _max( map { length $_->[2] } @reports ); if ($type eq 'modules') { splice @reports, 1, 0, ["-" x $ml, "", "-" x $hl]; push @full_reports, map { sprintf(" %*s %*s\n", -$ml, $_->[0], $hl, $_->[2]) } @reports; } else { splice @reports, 1, 0, ["-" x $ml, "-" x $wl, "-" x $hl]; push @full_reports, map { sprintf(" %*s %*s %*s\n", -$ml, $_->[0], $wl, $_->[1], $hl, $_->[2]) } @reports; } push @full_reports, "\n"; } } } if ( @full_reports ) { diag "\nVersions for all modules listed in $source (including optional ones):\n\n", @full_reports; } if ( $cpan_meta_error || @dep_errors ) { diag "\n*** WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING ***\n"; } if ( $cpan_meta_error ) { my ($orig_source) = grep { -f } 'MYMETA.json', 'MYMETA.yml'; diag "\nCPAN::Meta->load_file('$orig_source') failed with: $cpan_meta_error\n"; } if ( @dep_errors ) { diag join("\n", "\nThe following REQUIRED prerequisites were not satisfied:\n", @dep_errors, "\n" ); } pass('Reported prereqs'); # vim: ts=4 sts=4 sw=4 et: pod-spell.t100644001750001750 41614007640721 20122 0ustar00grahamgraham000000000000CSS-Minifier-XS-0.13/xt/authoruse strict; use warnings; use Test::More; # generated by Dist::Zilla::Plugin::Test::PodSpelling 2.007005 use Test::Spelling 0.12; use Pod::Wordlist; add_stopwords(); all_pod_files_spelling_ok( qw( bin lib ) ); __DATA__ CSS Graham Minifier TerMarsch XS cpan lib release000755001750001750 014007640721 16033 5ustar00grahamgraham000000000000CSS-Minifier-XS-0.13/xtkwalitee.t100644001750001750 27514007640721 20151 0ustar00grahamgraham000000000000CSS-Minifier-XS-0.13/xt/release# this test was generated with Dist::Zilla::Plugin::Test::Kwalitee 2.12 use strict; use warnings; use Test::More 0.88; use Test::Kwalitee 1.21 'kwalitee_ok'; kwalitee_ok(); done_testing; Minifier000755001750001750 014007640721 16720 5ustar00grahamgraham000000000000CSS-Minifier-XS-0.13/lib/CSSXS.pm100644001750001750 716714007640721 17763 0ustar00grahamgraham000000000000CSS-Minifier-XS-0.13/lib/CSS/Minifierpackage CSS::Minifier::XS; use strict; use warnings; require Exporter; require DynaLoader; our @ISA = qw(Exporter DynaLoader); our @EXPORT_OK = qw(minify); our $VERSION = '0.13'; bootstrap CSS::Minifier::XS $VERSION; 1; =for stopwords minifier minifies minified minification eol tokenizing =head1 NAME CSS::Minifier::XS - XS based CSS minifier =head1 SYNOPSIS use CSS::Minifier::XS qw(minify); my $css = '...'; my $minified = minify($css); =head1 DESCRIPTION C is a CSS "minifier"; its designed to remove unnecessary whitespace and comments from CSS files, while also B breaking the CSS. C is similar in function to C, but is substantially faster as its written in XS and not just pure Perl. =head1 METHODS =over =item minify($css) Minifies the given C<$css>, returning the minified CSS back to the caller. =back =head1 HOW IT WORKS C minifies the CSS by removing unnecessary whitespace from CSS documents. Comment blocks are also removed, I when (a) they contain the word "copyright" in them, or (b) they're needed to implement the "Mac/IE Comment Hack". Internally, the minification is done by taking multiple passes through the CSS document: =head2 Pass 1: Tokenize First, we go through and parse the CSS document into a series of tokens internally. The tokenizing process B check to make sure that you've got syntactically valid CSS, it just breaks up the text into a stream of tokens suitable for processing by the subsequent stages. =head2 Pass 2: Collapse We then march through the token list and collapse certain tokens down to their smallest possible representation. I they're still included in the final results we only want to include them at their shortest. =over =item Whitespace Runs of multiple whitespace characters are reduced down to a single whitespace character. If the whitespace contains any "end of line" (EOL) characters, then the end result is the I EOL character encountered. Otherwise, the result is the first whitespace character in the run. =item Comments Comments implementing the "Mac/IE Comment Hack" are collapsed down to the smallest possible comment that would still implement the hack ("/*\*/" to start the hack, and "/**/" to end it). =item Zero Units Zero Units (e.g. C<0px>) are reduced down to just "0", as the CSS specification indicates that the unit is not required when its a zero value. =back =head2 Pass 3: Pruning We then go back through the token list and prune and remove unnecessary tokens. =over =item Whitespace Wherever possible, whitespace is removed; before+after comment blocks, and before+after various symbols/sigils. =item Comments Comments that either (a) are needed to implement the "Mac/IE Comment Hack", or that (b) contain the word "copyright" in them are preserved. B other comments are removed. =item Symbols/Sigils Semi-colons that are immediately followed by a closing brace (e.g. ";}") are removed; semi-colons are needed to separate multiple declarations, but aren't required at the end of a group. =item Everything else We keep everything else; identifiers, quoted literal strings, symbols/sigils, etc. =back =head2 Pass 4: Re-assembly Lastly, we go back through the token list and re-assemble it all back into a single CSS string, which is then returned back to the caller. =head1 AUTHOR Graham TerMarsch (cpan@howlingfrog.com) =head1 COPYRIGHT Copyright (C) 2007-, Graham TerMarsch. All Rights Reserved. This is free software; you can redistribute it and/or modify it under the same license as Perl itself. =head1 SEE ALSO C. =cut comparison.t100644001750001750 617614007640721 20426 0ustar00grahamgraham000000000000CSS-Minifier-XS-0.13/xt/author#!/usr/bin/perl use strict; use warnings; use Test::More; use CSS::Compressor; use CSS::Minifier qw(); use CSS::Minifier::XS qw(); use File::Which qw(which); use IO::File; use Number::Format qw(format_bytes); use Benchmark qw(countit); ############################################################################### # Only run Comparison if asked for. unless ($ENV{BENCHMARK}) { plan skip_all => 'Skipping Benchmark; use BENCHMARK=1 to run'; } ############################################################################### # How long are we allowing each compressor to run? my $time = 5; ############################################################################### # Find "curl" my $curl = which('curl'); unless ($curl) { plan skip_all => 'curl required for comparison'; } ############################################################################### # What CSS docs do we want to try compressing? my @libs = ( 'http://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.css', 'http://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.css', 'http://cdnjs.cloudflare.com/ajax/libs/hover.css/2.3.1/css/hover.css', 'http://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.2/css/fontawesome.css', ); ############################################################################### # Go grab the CSS documents, compress them, and spit out results to compare. foreach my $uri (@libs) { subtest $uri => sub { my $content = qx{$curl --silent $uri}; ok defined $content, 'fetched CSS'; BAIL_OUT("No CSS fetched!") unless (length($content)); # CSS::Compressor do_compress('CSS::Compressor', $content, sub { my $css = shift; my $small = CSS::Compressor::css_compress($css); return $small; } ); # CSS::Minifier do_compress('CSS::Minifier', $content, sub { my $css = shift; my $small = CSS::Minifier::minify(input => $css); return $small; } ); # CSS::Minifier::XS do_compress('CSS::Minifier::XS', $content, sub { my $css = shift; my $small = CSS::Minifier::XS::minify($css); return $small; } ); }; } ############################################################################### done_testing(); sub do_compress { my $name = shift; my $css = shift; my $cb = shift; # Compress the CSS my $small; my $count = countit($time, sub { $small = $cb->($css) } ); # Stuff the compressed CSS out to file for examination my $fname = lc($name); $fname =~ s{\W+}{-}g; my $fout = IO::File->new(">$fname.out"); $fout->print($small); # Calculate length, speed, and percent savings my $before = length($css); my $after = length($small); my $rate = sprintf('%ld', ($count->iters / $time) * $before); my $savings = sprintf('%0.2f%%', (($before - $after) / $before) * 100); my $results = sprintf("%20s before[%7d] after[%7d] savings[%6s] rate[%8s/sec]", $name, $before, $after, $savings, format_bytes($rate, unit => 'K', precision => 0), ); pass $results; } 00-report-prereqs.dd100644001750001750 521214007640721 20103 0ustar00grahamgraham000000000000CSS-Minifier-XS-0.13/tdo { my $x = { 'configure' => { 'requires' => { 'ExtUtils::MakeMaker' => '0' } }, 'develop' => { 'requires' => { 'CSS::Compressor' => '0', 'CSS::Minifier' => '0', 'Dist::Zilla' => '5', 'Dist::Zilla::PluginBundle::Author::GTERMARS' => '0', 'File::Which' => '0', 'IPC::Run' => '0', 'Number::Format' => '0', 'Pod::Coverage::TrustPod' => '0', 'Software::License::Perl_5' => '0', 'Test::CPAN::Meta' => '0', 'Test::CleanNamespaces' => '0.15', 'Test::EOF' => '0', 'Test::EOL' => '0', 'Test::Kwalitee' => '1.21', 'Test::LeakTrace' => '0', 'Test::MinimumVersion' => '0', 'Test::More' => '0.88', 'Test::NoBreakpoints' => '0.15', 'Test::NoTabs' => '0', 'Test::Pod' => '1.41', 'Test::Pod::Coverage' => '1.08', 'Test::Spelling' => '0.12', 'Test::Synopsis' => '0' } }, 'runtime' => { 'requires' => { 'perl' => '5.008001' } }, 'test' => { 'recommends' => { 'CPAN::Meta' => '2.120900' }, 'requires' => { 'ExtUtils::MakeMaker' => '0', 'File::Spec' => '0', 'File::Temp' => '0', 'IO::Handle' => '0', 'IPC::Open3' => '0', 'Test::DiagINC' => '0.002', 'Test::More' => '0.96' } } }; $x; }pod-syntax.t100644001750001750 25214007640721 20327 0ustar00grahamgraham000000000000CSS-Minifier-XS-0.13/xt/author#!perl # This file was automatically generated by Dist::Zilla::Plugin::PodSyntaxTests. use strict; use warnings; use Test::More; use Test::Pod 1.41; all_pod_files_ok(); pod-coverage.t100644001750001750 33414007640721 20575 0ustar00grahamgraham000000000000CSS-Minifier-XS-0.13/xt/author#!perl # This file was automatically generated by Dist::Zilla::Plugin::PodCoverageTests. use Test::Pod::Coverage 1.08; use Pod::Coverage::TrustPod; all_pod_coverage_ok({ coverage_class => 'Pod::Coverage::TrustPod' }); unused-vars.t100644001750001750 36214007640721 20615 0ustar00grahamgraham000000000000CSS-Minifier-XS-0.13/xt/release#!perl use Test::More 0.96 tests => 1; eval { require Test::Vars }; SKIP: { skip 1 => 'Test::Vars required for testing for unused vars' if $@; Test::Vars->import; subtest 'unused vars' => sub { all_vars_ok(); }; }; no-breakpoints.t100644001750001750 31414007640721 21153 0ustar00grahamgraham000000000000CSS-Minifier-XS-0.13/xt/authoruse strict; use warnings; # this test was generated with Dist::Zilla::Plugin::Test::NoBreakpoints 0.0.2 use Test::More 0.88; use Test::NoBreakpoints 0.15; all_files_no_breakpoints_ok(); done_testing; minimum-version.t100644001750001750 15414007640721 21360 0ustar00grahamgraham000000000000CSS-Minifier-XS-0.13/xt/authoruse strict; use warnings; use Test::More; use Test::MinimumVersion; all_minimum_version_from_metayml_ok(); clean-namespaces.t100644001750001750 36114007640721 21421 0ustar00grahamgraham000000000000CSS-Minifier-XS-0.13/xt/authoruse strict; use warnings; # this test was generated with Dist::Zilla::Plugin::Test::CleanNamespaces 0.006 use Test::More 0.94; use Test::CleanNamespaces 0.15; subtest all_namespaces_clean => sub { all_namespaces_clean() }; done_testing;