Skip to content

Commit 6268c57

Browse files
committed
REVIEW ME: lots of changes.
1 parent db00ed0 commit 6268c57

File tree

1 file changed

+35
-26
lines changed

1 file changed

+35
-26
lines changed

JSLint.coffee

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ JSLINT = ctx.JSLINT
1010
try
1111
if process.argv.length < 3
1212
throw new Error "No file specified"
13-
script = fs.readFileSync process.argv[2], 'utf8'
13+
filename = process.argv[2]
14+
script = fs.readFileSync filename, 'utf8'
1415
catch e
1516
console.log "File could not be read"
1617
process.exit 1
@@ -27,11 +28,15 @@ unless details?
2728
console.error "There are no details!"
2829
process.exit 1
2930

30-
globals = ['document','twttr']
31-
if details.access? and Array.isArray details.access
32-
for a in details.access
33-
if typeof a is 'string' and a.match /^[a-zA-Z]+$/
34-
globals.push a
31+
details.id = 2
32+
details.revision = 1
33+
34+
# Alias dependencies.
35+
accessList = details.access ? []
36+
accessList.unshift "app"
37+
accessList.unshift "plugin"
38+
for access in accessList
39+
script = " var #{access} = lib.#{access}();\n#{script}"
3540

3641
walk = (ast) ->
3742
if Array.isArray(ast) and ast.length > 0
@@ -55,16 +60,6 @@ walk = (ast) ->
5560
else if type is 'unary-prefix'
5661
ast[2] = walk ast[2]
5762
else if type is 'name'
58-
if globals.indexOf(ast[1]) isnt -1
59-
ast = [
60-
'call'
61-
[
62-
'dot'
63-
['name','lib']
64-
'global'
65-
]
66-
[['string',ast[1]]]
67-
]
6863
#skip
6964
else if type is 'sub'
7065
ast[1] = walk ast[1]
@@ -123,10 +118,6 @@ walk = (ast) ->
123118
]
124119
]
125120
ast[2][1].unshift [
126-
'stat'
127-
ast[1]
128-
]
129-
ast[2][1].push [
130121
'if'
131122
[
132123
'unary-prefix'
@@ -144,6 +135,10 @@ walk = (ast) ->
144135
]
145136
undefined
146137
]
138+
ast[2][1].unshift [
139+
'stat'
140+
ast[1]
141+
]
147142
ast = [
148143
'for'
149144
null
@@ -216,14 +211,27 @@ ast = uglify.ast_mangle ast, {
216211
defines: {'this':['name', "null"]}
217212
}
218213
#ast = uglify.ast_squeeze ast
219-
script = uglify.gen_code ast, beautify: true, indent_start:8, indent_level:2, quote_keys: true
214+
script = uglify.gen_code ast, beautify: true, indent_start:4, indent_level:2, quote_keys: true
220215
console.log script
221216

222217
script = script.replace /(\n|^)\/\/.*(\n|$)/g, "$2"
223-
adsafeId = "APPAAAAFF_"
224-
script = "<div id=\"#{adsafeId}\">\n <script>\n ADSAFE.go(\"#{adsafeId}\", function (dom, lib) {\n \"use strict\";\n#{script}}\n );\n </script>\n</div>"
218+
num2alphabet = (num) ->
219+
chars = "ABCDEFGHIJ".split("")
220+
num = parseInt num
221+
if isNaN(num) or !isFinite(num)
222+
throw new Error "Invalid number"
223+
num = ""+num
224+
out = ""
225+
for i in [0...num.length]
226+
out += chars[parseInt num.charAt(i)]
227+
return out
228+
id = details.id
229+
adsafeId = "ZZZ#{num2alphabet id}_"
230+
head = "<div id=\"#{adsafeId}\"><script>\n"
231+
foot = "</script>\n</div>"
232+
script = "#{head} ADSAFE.go(\"#{adsafeId}\", function (dom, lib) {\n \"use strict\";\n#{script}\n });#{foot}"
225233
ok = JSLINT script, {
226-
adsafe: true, fragment: true, predef: globals, browser: true, safe: true, bitwise: true, continue: true, eqeq: true, es5: true, evil: false, forin: true, newcap: true, nomen: true, plusplus: true, regexp: true, undef: true, unparam: true, sloppy: true, stupid: true, sub: true, vars: true, white: true, css: true
234+
adsafe: true, fragment: true, predef: [], browser: true, safe: true, bitwise: true, continue: true, eqeq: true, es5: true, evil: false, forin: true, newcap: true, nomen: true, plusplus: true, regexp: true, undef: true, unparam: true, sloppy: true, stupid: true, sub: true, vars: true, white: true, css: true
227235
}, {
228236
plugin: false
229237
}
@@ -232,6 +240,7 @@ unless ok
232240
result = JSLINT.data()
233241
console.log util.inspect result, false, 3, true
234242
else
235-
script = script.replace "<script>","<script>\n (function(){\n var ADSAFE = new ADSAFE_APP(\"#{adsafeId}\",#{JSON.stringify(details)});"
236-
script = script.replace "</script>"," }());\n </script>"
243+
script = script.substr(head.length, script.length - (head.length+foot.length))
244+
script = "new ADSAFE_APP(#{id}, \"#{adsafeId}\", #{JSON.stringify(details)}, function(ADSAFE){\n#{script}\n});"
245+
fs.writeFileSync "#{filename}.adsafe.js", script
237246
console.log script

0 commit comments

Comments
 (0)