Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into dev-cssonly
Browse files Browse the repository at this point in the history
Conflicts:
	build/CommentCoreLibrary.min.js
  • Loading branch information
jabbany committed Oct 19, 2016
2 parents d61d9f5 + 684d54c commit db6bd13
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 19 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
@@ -1,9 +1,9 @@
language: node_js
sudo: false
node_js:
- "0.10"
- "6.7.0"
before_install:
- npm install npm@1.4 -g
- npm install npm -g
before_script:
- npm install -g grunt-cli
script:
Expand Down
8 changes: 6 additions & 2 deletions Gruntfile.coffee
Expand Up @@ -167,8 +167,10 @@ module.exports = (grunt) ->
specs: 'compiled_spec/*spec.js'
helpers: 'spec/*helper.js'
vendor: [
'node_modules/jasmine-jquery/vendor/jquery/jquery.js'
'node_modules/jquery/dist/jquery.js'
'node_modules/jasmine-jquery/lib/jasmine-jquery.js'
'node_modules/sinon/pkg/sinon.js'
'node_modules/jasmine-sinon/lib/jasmine-sinon.js'
]
template: require('grunt-template-jasmine-istanbul')
templateOptions:
Expand All @@ -180,8 +182,10 @@ module.exports = (grunt) ->
specs: 'compiled_spec/*spec.js'
helpers: 'spec/*helper.js'
vendor: [
'node_modules/jasmine-jquery/vendor/jquery/jquery.js'
'node_modules/jquery/dist/jquery.js'
'node_modules/jasmine-jquery/lib/jasmine-jquery.js'
'node_modules/sinon/pkg/sinon.js'
'node_modules/jasmine-sinon/lib/jasmine-sinon.js'
]
template: require('grunt-template-jasmine-istanbul')
templateOptions:
Expand Down
16 changes: 15 additions & 1 deletion build/CommentCoreLibrary.js
Expand Up @@ -1527,6 +1527,20 @@ function BilibiliParser(xmlDoc, text, warn){
return string.replace(/\t/,"\\t");
}

function formatmode7(text) {
if (text.charAt(0) == '[') switch (text.charAt(text.length - 1)) {
case ']':
return text;
case '"':
return text + ']';
case ',':
return text.substring(0, text.length - 1) + '"]';
default:
return formatmode7(text.substring(0, text.length - 1));
};
if (text.charAt(0) !== '[') return text;
};

if(xmlDoc !== null){
var elems = xmlDoc.getElementsByTagName('d');
}else{
Expand Down Expand Up @@ -1574,7 +1588,7 @@ function BilibiliParser(xmlDoc, text, warn){
}else{
if(obj.mode == 7){
try{
adv = JSON.parse(format(text));
adv = JSON.parse(format(formatmode7(text)));
obj.shadow = true;
obj.x = parseFloat(adv[0]);
obj.y = parseFloat(adv[1]);
Expand Down
2 changes: 1 addition & 1 deletion build/CommentCoreLibrary.min.js

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions package.json
Expand Up @@ -34,7 +34,10 @@
"grunt-contrib-watch": "^1.0.0",
"grunt-template-jasmine-istanbul": "^0.5.0",
"grunt-ts": "^5.5.1",
"jquery": "^3.1.1",
"jasmine-jquery": "^2.0.0",
"sinon": "^1.17.6",
"jasmine-sinon": "^0.4.0",
"load-grunt-tasks": "^3.5.2"
}
}
48 changes: 39 additions & 9 deletions spec/CommentCoreLibrary_spec.coffee
@@ -1,4 +1,5 @@
'use strict'

describe 'CommentManager', ->
manager = stage = cmt = c1 = c2 = c3 = c4 = c5 = null

Expand Down Expand Up @@ -66,15 +67,15 @@ describe 'CommentManager', ->

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

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

describe '.clear', ->
it 'clears', ->
Expand All @@ -89,14 +90,37 @@ describe 'CommentManager', ->
expect(manager.timeline).toEqual [c3, c5]
manager.insert c4
expect(manager.timeline).toEqual [c3, c4 , c5]


describe '.setBounds', ->
beforeEach ->
manager.stage.style.width = '640px'
manager.stage.style.width = '480px'

it 'updates width and height', ->
manager.setBounds()
expect(manager.width).toEqual stage.offsetWidth
expect(manager.height).toEqual stage.offsetHeight

it 'dispatches resize event', ->
callback = sinon.spy()
manager.addEventListener 'resize', callback
manager.setBounds()
expect(callback).toHaveBeenCalled true

it 'sets bounds on comment space allocators', ->
spies = {}
for allocatorName, allocator of manager.csa
spies[allocatorName] = sinon.spy allocator, 'setBounds'
manager.setBounds()
for allocatorName, spy of spies
expect(spy).toHaveBeenCalledWith stage.offsetWidth, stage.offsetHeight

describe '.addEventListener .dispatchEvent', ->
it 'add one event listener', ->
hasDispatchedEvent = false
manager.addEventListener 'myCustomEvent', ->
hasDispatchedEvent = true
callback = sinon.spy()
manager.addEventListener 'myCustomEvent', callback
manager.dispatchEvent 'myCustomEvent'
expect(hasDispatchedEvent).toBe true
expect(callback).toHaveBeenCalled true

it 'add multiple event listeners', ->
dispatchedEventId = 0
Expand All @@ -106,3 +130,9 @@ describe 'CommentManager', ->
dispatchedEventId = 2
manager.dispatchEvent 'myCustomEvent'
expect(dispatchedEventId).toBe 2

it 'dispatch event works with data', ->
callback = sinon.spy()
manager.addEventListener 'myCustomEvent', callback
manager.dispatchEvent 'myCustomEvent', 'foo'
expect(callback).toHaveBeenCalledWith 'foo'
116 changes: 113 additions & 3 deletions spec/core/Comment_spec.coffee
@@ -1,7 +1,117 @@
'use strict'

describe 'CoreComment', ->
xit('TODO: add some examples to (or delete) it')
manager = null

beforeEach ->
manager = new CommentManager(document.createElement 'div')

it 'cannot initialize without parent', ->
expect(CoreComment).toThrow()

it 'initializes defaults from empty IComment', ->
comment = new CoreComment(manager)
expect(comment.mode).toBe 1
expect(comment.text).toBe ''
expect(comment.ttl).toBe 4000
expect(comment.dur).toBe 4000
expect(comment.movable).toBe true
expect(comment.color).toBe 0xffffff
expect(comment.size).toBe 25
expect(comment.alpha).toBe 1
expect(comment.border).toBe false
expect(comment.align).toBe 0
expect(comment.parent).toBe manager

it 'initializes from parameterized IComment', ->
config =
stime: 100
mode: 2
dur: 5000
text: 'FooBar'
color: 0xf0f0f0
size: 24
border: true
opacity: 0.5
font: 'SimSun'
comment = new CoreComment(manager, config)

'stime mode dur text color size border'.split(' ').forEach (property) ->
expect(comment[property]).toBe config[property]

expect(comment.alpha).toBe config.opacity

'time update finish'.split(' ').forEach (method) ->
it "has #{method}", ->
comment = new CoreComment(manager)
expect(typeof comment[method]).toBe 'function'

describe '.time', ->
comment = null

beforeEach ->
config =
dur: 1000
comment = new CoreComment(manager, config)

it 'ages comment', ->
comment.time 100
expect(comment.ttl).toBe (comment.dur - 100)

it 'calls update when movable', ->
spy = sinon.spy comment, 'update'
comment.time 100
expect(spy).toHaveBeenCalled true

it 'calls finish if expired', ->
spy = sinon.spy comment, 'finish'

# Create the DOM and add it to the manager
comment.init()
comment.parent.stage.appendChild comment.dom

comment.time 2000
expect(spy).toHaveBeenCalled true

describe '.invalidate', ->
comment = null

beforeEach ->
config =
dur: 1000
comment = new CoreComment(manager, config)

it 'invalidates comment cache data', ->
comment.invalidate()
expect(comment._x).toBe null
expect(comment._y).toBe null
expect(comment._width).toBe null
expect(comment._height).toBe null

describe '.update', ->
it 'calls animate', ->
comment = new CoreComment(manager)
spy = sinon.spy comment, 'animate'
comment.update()
expect(spy).toHaveBeenCalled true

describe '.LINEAR', ->
it 'does linear interpolation', ->
v = CoreComment.LINEAR(400, 1, 1, 4000)
expect(v).toBe 1.1

describe 'ScrollComment', ->
xit('TODO: add some examples to (or delete) it')
manager = null

beforeEach ->
manager = new CommentManager(document.createElement 'div')

it 'cannot initialize without parent', ->
expect(ScrollComment).toThrow()

it 'applies scaling for scroll comments', ->
manager.options.scroll.scale = 10
config =
dur: 4000
comment = new ScrollComment(manager, config)
expect(comment.dur).toBe 40000

16 changes: 15 additions & 1 deletion src/parsers/BilibiliFormat.js
Expand Up @@ -9,6 +9,20 @@ function BilibiliParser(xmlDoc, text, warn){
return string.replace(/\t/,"\\t");
}

function formatmode7(text) {
if (text.charAt(0) == '[') switch (text.charAt(text.length - 1)) {
case ']':
return text;
case '"':
return text + ']';
case ',':
return text.substring(0, text.length - 1) + '"]';
default:
return formatmode7(text.substring(0, text.length - 1));
};
if (text.charAt(0) !== '[') return text;
};

if(xmlDoc !== null){
var elems = xmlDoc.getElementsByTagName('d');
}else{
Expand Down Expand Up @@ -56,7 +70,7 @@ function BilibiliParser(xmlDoc, text, warn){
}else{
if(obj.mode == 7){
try{
adv = JSON.parse(format(text));
adv = JSON.parse(format(formatmode7(text)));
obj.shadow = true;
obj.x = parseFloat(adv[0]);
obj.y = parseFloat(adv[1]);
Expand Down

0 comments on commit db6bd13

Please sign in to comment.