Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Making Collection declarations more robust. Still got a concat bug on…

… 1.8.7, but this won't cause bugs in packed scripts.
  • Loading branch information...
commit 9ece59428bbd1bfd497b45a2ae4de2d163a24849 1 parent 63f30ce
@jcoglan authored
View
12 lib/packr.rb
@@ -8,8 +8,8 @@
'/packr/regexp_group',
'/packr/constants',
'/packr/encoder',
- '/packr/minifier',
'/packr/parser',
+ '/packr/minifier',
'/packr/privates',
'/packr/shrinker',
'/packr/words',
@@ -22,11 +22,11 @@ class Packr
VERSION = '3.1.0'
- DATA = Parser.new
- DATA.put("STRING1", IGNORE)
- DATA.put('STRING2', IGNORE)
- DATA.put("CONDITIONAL", IGNORE) # conditional comments
- DATA.put("(OPERATOR)\\s*(REGEXP)", "\\1\\2")
+ DATA = Parser.new.
+ put("STRING1", IGNORE).
+ put('STRING2', IGNORE).
+ put("CONDITIONAL", IGNORE). # conditional comments
+ put("(OPERATOR)\\s*(REGEXP)", "\\1\\2")
def self.encode62(c)
(c < 62 ? '' : encode62((c / 62.0).to_i)) +
View
14 lib/packr/base62.rb
@@ -17,7 +17,7 @@ def encode(script)
words = search(script)
words.sort!
- encoded = Collection.new({}) # a dictionary of base62 -> base10
+ encoded = Collection.new # a dictionary of base62 -> base10
words.size.times { |i| encoded.put(Packr.encode62(i), i) }
replacement = lambda { |word| words.get(word).replacement }
@@ -81,12 +81,12 @@ def get_count(words)
def get_decoder(words)
# returns a pattern used for fast decoding of the packed script
- trim = RegexpGroup.new({
- "(\\d)(\\|\\d)+\\|(\\d)" => "\\1-\\3",
- "([a-z])(\\|[a-z])+\\|([a-z])" => "\\1-\\3",
- "([A-Z])(\\|[A-Z])+\\|([A-Z])" => "\\1-\\3",
- "\\|" => ""
- })
+ trim = RegexpGroup.new.
+ put("(\\d)(\\|\\d)+\\|(\\d)", "\\1-\\3").
+ put("([a-z])(\\|[a-z])+\\|([a-z])", "\\1-\\3").
+ put("([A-Z])(\\|[A-Z])+\\|([A-Z])", "\\1-\\3").
+ put("\\|", "")
+
pattern = trim.exec(words.map { |word, key|
word.to_s.empty? ? "" : word.replacement
}[0...62].join("|"))
View
1  lib/packr/collection.rb
@@ -68,6 +68,7 @@ def put(key, item = nil)
begin; klass = self.class::Item; rescue; end
item = self.class.create(key, item) if klass and !item.is_a?(klass)
@values[key.to_s] = item
+ self
end
def put_at(index, item = nil)
View
60 lib/packr/minifier.rb
@@ -6,7 +6,7 @@ def self.conditional_comments
end
def initialize
- @concat = Parser.new(CONCAT).merge(DATA)
+ @concat = CONCAT.union(DATA)
def @concat.exec(script)
parsed = super(script)
@@ -17,9 +17,9 @@ def @concat.exec(script)
parsed
end
- @comments = DATA.union(Parser.new(COMMENTS))
- @clean = DATA.union(Parser.new(CLEAN))
- @whitespace = DATA.union(Parser.new(WHITESPACE))
+ @comments = DATA.union(COMMENTS)
+ @clean = DATA.union(CLEAN)
+ @whitespace = DATA.union(WHITESPACE)
@@conditional_comments = @comments.copy
@@conditional_comments.put_at(-1, " \\3")
@@ -40,16 +40,15 @@ def minify(script)
CONTINUE = /\\\r?\n/
- CLEAN = {
- "\\(\\s*([^;)]*)\\s*;\\s*([^;)]*)\\s*;\\s*([^;)]*)\\)" => "(\\1;\\2;\\3)", # for (;;) loops
- "throw[^};]+[};]" => IGNORE, # a safari 1.3 bug
- ";+\\s*([};])" => "\\1"
- }
+ CLEAN = Parser.new.
+ put("\\(\\s*([^;)]*)\\s*;\\s*([^;)]*)\\s*;\\s*([^;)]*)\\)", "(\\1;\\2;\\3)"). # for (;;) loops
+ put("throw[^};]+[};]", IGNORE). # a safari 1.3 bug
+ put(";+\\s*([};])", "\\1")
- COMMENTS = {
- ";;;[^\\n]*\\n" => REMOVE,
- "(COMMENT1)\\n\\s*(REGEXP)?" => "\n\\3",
- "(COMMENT2)\\s*(REGEXP)?" => lambda do |*args|
+ COMMENTS = Parser.new.
+ put(";;;[^\\n]*\\n", REMOVE).
+ put("(COMMENT1)\\n\\s*(REGEXP)?", "\n\\3").
+ put("(COMMENT2)\\s*(REGEXP)?", lambda do |*args|
match, comment, b, regexp = args[0..3]
if comment =~ /^\/\*@/ and comment =~ /@\*\/$/
# comments = Minifier.conditional_comments.exec(comment)
@@ -57,27 +56,24 @@ def minify(script)
comment = ""
end
comment + " " + (regexp || "")
- end
- }
+ end)
- CONCAT = {
- "(STRING1)\\+(STRING1)" => lambda { |*args| args[1][0...-1] + args[3][1..-1] },
- "(STRING2)\\+(STRING2)" => lambda { |*args| args[1][0...-1] + args[3][1..-1] }
- }
+ CONCAT = Parser.new.
+ put("(STRING1)\\+(STRING1)", lambda { |*args| args[1][0...-1] + args[3][1..-1] }).
+ put("(STRING2)\\+(STRING2)", lambda { |*args| args[1][0...-1] + args[3][1..-1] })
- WHITESPACE = {
- "/\\/\\/@[^\\n]*\\n" => IGNORE,
- "@\\s+\\b" => "@ ", # protect conditional comments
- "\\b\\s+@" => " @",
- "(\\d)\\s+(\\.\\s*[a-z\\$_\\[(])" => "\\1 \\2", # http://dean.edwards.name/weblog/2007/04/packer3/#comment84066
- "([+-])\\s+([+-])" => "\\1 \\2", # c = a++ +b;
- "\\b\\s+\\$\\s+\\b" => " $ ", # var $ in
- "\\$\\s+\\b" => "$ ", # object$ in
- "\\b\\s+\\$" => " $", # return $object
- # "\\b\\s+#" => " #", # CSS
- "\\b\\s+\\b" => SPACE,
- "\\s+" => REMOVE
- }
+ WHITESPACE = Parser.new.
+ put("/\\/\\/@[^\\n]*\\n", IGNORE).
+ put("@\\s+\\b", "@ "). # protect conditional comments
+ put("\\b\\s+@", " @").
+ put("(\\d)\\s+(\\.\\s*[a-z\\$_\\[(])", "\\1 \\2"). # http://dean.edwards.name/weblog/2007/04/packer3/#comment84066
+ put("([+-])\\s+([+-])", "\\1 \\2"). # c = a++ +b;
+ put("\\b\\s+\\$\\s+\\b", " $ "). # var $ in
+ put("\\$\\s+\\b", "$ "). # object$ in
+ put("\\b\\s+\\$", " $"). # return $object
+ # put("\\b\\s+#", " #"). # CSS
+ put("\\b\\s+\\b", SPACE).
+ put("\\s+", REMOVE)
end
end
View
17 lib/packr/parser.rb
@@ -7,15 +7,14 @@ def put(expression, replacement)
end
# STRING1 requires backslashes to fix concat bug
- DICTIONARY = RegexpGroup.new({
- :OPERATOR => /return|typeof|[\[(\^=,{}:;&|!*?]/.source,
- :CONDITIONAL => /\/\*@\w*|\w*@\*\/|\/\/@\w*|@\w+/.source,
- :COMMENT1 => /\/\/[^\n]*/.source,
- :COMMENT2 => /\/\*[^*]*\*+([^\/][^*]*\*+)*\//.source,
- :REGEXP => /\/(\\[\/\\]|[^*\/])(\\.|[^\/\n\\])*\/[gim]*/.source,
- :STRING1 => /\'(\\.|[^\'\\])*\'/.source,
- :STRING2 => /"(\\.|[^"\\])*"/.source
- })
+ DICTIONARY = RegexpGroup.new.
+ put(:OPERATOR, /return|typeof|[\[(\^=,{}:;&|!*?]/.source).
+ put(:CONDITIONAL, /\/\*@\w*|\w*@\*\/|\/\/@\w*|@\w+/.source).
+ put(:COMMENT1, /\/\/[^\n]*/.source).
+ put(:COMMENT2, /\/\*[^*]*\*+([^\/][^*]*\*+)*\//.source).
+ put(:REGEXP, /\/(\\[\/\\]|[^*\/])(\\.|[^\/\n\\])*\/[gim]*/.source).
+ put(:STRING1, /\'(\\.|[^\'\\])*\'/.source).
+ put(:STRING2, /"(\\.|[^"\\])*"/.source)
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.