Skip to content

Commit db6bd13

Browse files
committed
Merge remote-tracking branch 'origin/master' into dev-cssonly
Conflicts: build/CommentCoreLibrary.min.js
2 parents d61d9f5 + 684d54c commit db6bd13

8 files changed

Lines changed: 194 additions & 19 deletions

File tree

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
language: node_js
22
sudo: false
33
node_js:
4-
- "0.10"
4+
- "6.7.0"
55
before_install:
6-
- npm install npm@1.4 -g
6+
- npm install npm -g
77
before_script:
88
- npm install -g grunt-cli
99
script:

Gruntfile.coffee

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,10 @@ module.exports = (grunt) ->
167167
specs: 'compiled_spec/*spec.js'
168168
helpers: 'spec/*helper.js'
169169
vendor: [
170-
'node_modules/jasmine-jquery/vendor/jquery/jquery.js'
170+
'node_modules/jquery/dist/jquery.js'
171171
'node_modules/jasmine-jquery/lib/jasmine-jquery.js'
172+
'node_modules/sinon/pkg/sinon.js'
173+
'node_modules/jasmine-sinon/lib/jasmine-sinon.js'
172174
]
173175
template: require('grunt-template-jasmine-istanbul')
174176
templateOptions:
@@ -180,8 +182,10 @@ module.exports = (grunt) ->
180182
specs: 'compiled_spec/*spec.js'
181183
helpers: 'spec/*helper.js'
182184
vendor: [
183-
'node_modules/jasmine-jquery/vendor/jquery/jquery.js'
185+
'node_modules/jquery/dist/jquery.js'
184186
'node_modules/jasmine-jquery/lib/jasmine-jquery.js'
187+
'node_modules/sinon/pkg/sinon.js'
188+
'node_modules/jasmine-sinon/lib/jasmine-sinon.js'
185189
]
186190
template: require('grunt-template-jasmine-istanbul')
187191
templateOptions:

build/CommentCoreLibrary.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1527,6 +1527,20 @@ function BilibiliParser(xmlDoc, text, warn){
15271527
return string.replace(/\t/,"\\t");
15281528
}
15291529

1530+
function formatmode7(text) {
1531+
if (text.charAt(0) == '[') switch (text.charAt(text.length - 1)) {
1532+
case ']':
1533+
return text;
1534+
case '"':
1535+
return text + ']';
1536+
case ',':
1537+
return text.substring(0, text.length - 1) + '"]';
1538+
default:
1539+
return formatmode7(text.substring(0, text.length - 1));
1540+
};
1541+
if (text.charAt(0) !== '[') return text;
1542+
};
1543+
15301544
if(xmlDoc !== null){
15311545
var elems = xmlDoc.getElementsByTagName('d');
15321546
}else{
@@ -1574,7 +1588,7 @@ function BilibiliParser(xmlDoc, text, warn){
15741588
}else{
15751589
if(obj.mode == 7){
15761590
try{
1577-
adv = JSON.parse(format(text));
1591+
adv = JSON.parse(format(formatmode7(text)));
15781592
obj.shadow = true;
15791593
obj.x = parseFloat(adv[0]);
15801594
obj.y = parseFloat(adv[1]);

build/CommentCoreLibrary.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@
3434
"grunt-contrib-watch": "^1.0.0",
3535
"grunt-template-jasmine-istanbul": "^0.5.0",
3636
"grunt-ts": "^5.5.1",
37+
"jquery": "^3.1.1",
3738
"jasmine-jquery": "^2.0.0",
39+
"sinon": "^1.17.6",
40+
"jasmine-sinon": "^0.4.0",
3841
"load-grunt-tasks": "^3.5.2"
3942
}
4043
}

spec/CommentCoreLibrary_spec.coffee

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
'use strict'
2+
23
describe 'CommentManager', ->
34
manager = stage = cmt = c1 = c2 = c3 = c4 = c5 = null
45

@@ -66,15 +67,15 @@ describe 'CommentManager', ->
6667

6768
describe '.start', ->
6869
it 'starts the timer', ->
70+
spy = sinon.spy window, 'setInterval'
6971
manager.start()
70-
# TODO: figure out how to test the timer
71-
# maybe just add spy on window.setInterval
72+
expect(spy).toHaveBeenCalled true
7273

7374
describe '.stop', ->
7475
it 'stops the timer', ->
76+
spy = sinon.spy window, 'clearInterval'
7577
manager.stop()
76-
# TODO: figure out how to test the timer
77-
# maybe just add spy on window.clearInterval
78+
expect(spy).toHaveBeenCalled true
7879

7980
describe '.clear', ->
8081
it 'clears', ->
@@ -89,14 +90,37 @@ describe 'CommentManager', ->
8990
expect(manager.timeline).toEqual [c3, c5]
9091
manager.insert c4
9192
expect(manager.timeline).toEqual [c3, c4 , c5]
92-
93+
94+
describe '.setBounds', ->
95+
beforeEach ->
96+
manager.stage.style.width = '640px'
97+
manager.stage.style.width = '480px'
98+
99+
it 'updates width and height', ->
100+
manager.setBounds()
101+
expect(manager.width).toEqual stage.offsetWidth
102+
expect(manager.height).toEqual stage.offsetHeight
103+
104+
it 'dispatches resize event', ->
105+
callback = sinon.spy()
106+
manager.addEventListener 'resize', callback
107+
manager.setBounds()
108+
expect(callback).toHaveBeenCalled true
109+
110+
it 'sets bounds on comment space allocators', ->
111+
spies = {}
112+
for allocatorName, allocator of manager.csa
113+
spies[allocatorName] = sinon.spy allocator, 'setBounds'
114+
manager.setBounds()
115+
for allocatorName, spy of spies
116+
expect(spy).toHaveBeenCalledWith stage.offsetWidth, stage.offsetHeight
117+
93118
describe '.addEventListener .dispatchEvent', ->
94119
it 'add one event listener', ->
95-
hasDispatchedEvent = false
96-
manager.addEventListener 'myCustomEvent', ->
97-
hasDispatchedEvent = true
120+
callback = sinon.spy()
121+
manager.addEventListener 'myCustomEvent', callback
98122
manager.dispatchEvent 'myCustomEvent'
99-
expect(hasDispatchedEvent).toBe true
123+
expect(callback).toHaveBeenCalled true
100124

101125
it 'add multiple event listeners', ->
102126
dispatchedEventId = 0
@@ -106,3 +130,9 @@ describe 'CommentManager', ->
106130
dispatchedEventId = 2
107131
manager.dispatchEvent 'myCustomEvent'
108132
expect(dispatchedEventId).toBe 2
133+
134+
it 'dispatch event works with data', ->
135+
callback = sinon.spy()
136+
manager.addEventListener 'myCustomEvent', callback
137+
manager.dispatchEvent 'myCustomEvent', 'foo'
138+
expect(callback).toHaveBeenCalledWith 'foo'

spec/core/Comment_spec.coffee

Lines changed: 113 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,117 @@
11
'use strict'
2-
32
describe 'CoreComment', ->
4-
xit('TODO: add some examples to (or delete) it')
3+
manager = null
4+
5+
beforeEach ->
6+
manager = new CommentManager(document.createElement 'div')
7+
8+
it 'cannot initialize without parent', ->
9+
expect(CoreComment).toThrow()
10+
11+
it 'initializes defaults from empty IComment', ->
12+
comment = new CoreComment(manager)
13+
expect(comment.mode).toBe 1
14+
expect(comment.text).toBe ''
15+
expect(comment.ttl).toBe 4000
16+
expect(comment.dur).toBe 4000
17+
expect(comment.movable).toBe true
18+
expect(comment.color).toBe 0xffffff
19+
expect(comment.size).toBe 25
20+
expect(comment.alpha).toBe 1
21+
expect(comment.border).toBe false
22+
expect(comment.align).toBe 0
23+
expect(comment.parent).toBe manager
24+
25+
it 'initializes from parameterized IComment', ->
26+
config =
27+
stime: 100
28+
mode: 2
29+
dur: 5000
30+
text: 'FooBar'
31+
color: 0xf0f0f0
32+
size: 24
33+
border: true
34+
opacity: 0.5
35+
font: 'SimSun'
36+
comment = new CoreComment(manager, config)
37+
38+
'stime mode dur text color size border'.split(' ').forEach (property) ->
39+
expect(comment[property]).toBe config[property]
40+
41+
expect(comment.alpha).toBe config.opacity
42+
43+
'time update finish'.split(' ').forEach (method) ->
44+
it "has #{method}", ->
45+
comment = new CoreComment(manager)
46+
expect(typeof comment[method]).toBe 'function'
47+
48+
describe '.time', ->
49+
comment = null
50+
51+
beforeEach ->
52+
config =
53+
dur: 1000
54+
comment = new CoreComment(manager, config)
55+
56+
it 'ages comment', ->
57+
comment.time 100
58+
expect(comment.ttl).toBe (comment.dur - 100)
59+
60+
it 'calls update when movable', ->
61+
spy = sinon.spy comment, 'update'
62+
comment.time 100
63+
expect(spy).toHaveBeenCalled true
64+
65+
it 'calls finish if expired', ->
66+
spy = sinon.spy comment, 'finish'
67+
68+
# Create the DOM and add it to the manager
69+
comment.init()
70+
comment.parent.stage.appendChild comment.dom
71+
72+
comment.time 2000
73+
expect(spy).toHaveBeenCalled true
74+
75+
describe '.invalidate', ->
76+
comment = null
77+
78+
beforeEach ->
79+
config =
80+
dur: 1000
81+
comment = new CoreComment(manager, config)
82+
83+
it 'invalidates comment cache data', ->
84+
comment.invalidate()
85+
expect(comment._x).toBe null
86+
expect(comment._y).toBe null
87+
expect(comment._width).toBe null
88+
expect(comment._height).toBe null
89+
90+
describe '.update', ->
91+
it 'calls animate', ->
92+
comment = new CoreComment(manager)
93+
spy = sinon.spy comment, 'animate'
94+
comment.update()
95+
expect(spy).toHaveBeenCalled true
96+
97+
describe '.LINEAR', ->
98+
it 'does linear interpolation', ->
99+
v = CoreComment.LINEAR(400, 1, 1, 4000)
100+
expect(v).toBe 1.1
5101

6102
describe 'ScrollComment', ->
7-
xit('TODO: add some examples to (or delete) it')
103+
manager = null
104+
105+
beforeEach ->
106+
manager = new CommentManager(document.createElement 'div')
107+
108+
it 'cannot initialize without parent', ->
109+
expect(ScrollComment).toThrow()
110+
111+
it 'applies scaling for scroll comments', ->
112+
manager.options.scroll.scale = 10
113+
config =
114+
dur: 4000
115+
comment = new ScrollComment(manager, config)
116+
expect(comment.dur).toBe 40000
117+

src/parsers/BilibiliFormat.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,20 @@ function BilibiliParser(xmlDoc, text, warn){
99
return string.replace(/\t/,"\\t");
1010
}
1111

12+
function formatmode7(text) {
13+
if (text.charAt(0) == '[') switch (text.charAt(text.length - 1)) {
14+
case ']':
15+
return text;
16+
case '"':
17+
return text + ']';
18+
case ',':
19+
return text.substring(0, text.length - 1) + '"]';
20+
default:
21+
return formatmode7(text.substring(0, text.length - 1));
22+
};
23+
if (text.charAt(0) !== '[') return text;
24+
};
25+
1226
if(xmlDoc !== null){
1327
var elems = xmlDoc.getElementsByTagName('d');
1428
}else{
@@ -56,7 +70,7 @@ function BilibiliParser(xmlDoc, text, warn){
5670
}else{
5771
if(obj.mode == 7){
5872
try{
59-
adv = JSON.parse(format(text));
73+
adv = JSON.parse(format(formatmode7(text)));
6074
obj.shadow = true;
6175
obj.x = parseFloat(adv[0]);
6276
obj.y = parseFloat(adv[1]);

0 commit comments

Comments
 (0)