Created
May 10, 2011 11:18
-
-
Save jblanche/964290 to your computer and use it in GitHub Desktop.
the awesome
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Parser | |
| constructor: (el) -> | |
| @html = el | |
| @tracks = [] | |
| @parse() | |
| toJSON: -> | |
| {"tracks": @tracks} | |
| parse: -> | |
| @html.select('.tracks > li').each (track) => | |
| title = track.readAttribute("data-title") | |
| type = track.readAttribute("data-type") | |
| urls = [] | |
| json_track = { | |
| "title": title, | |
| "type": type | |
| } | |
| json_track["urls"] = switch type | |
| when "video" | |
| @parseVideo(track) | |
| when "document" | |
| @parseDocument(track) | |
| when "photo" | |
| @parsePhoto(track) | |
| @tracks.push(json_track) | |
| parseVideo: (track) -> | |
| urls = {} | |
| track.select('a').each (url) => | |
| urls[url.readAttribute('data-type')] = url.readAttribute("href") | |
| urls | |
| parseDocument: (track) -> | |
| urls = [] | |
| track.select('a').each (url) => | |
| urls.push url.readAttribute("href") | |
| urls | |
| parsePhoto: (track) -> | |
| urls = [] | |
| track.select('a').each (url) => | |
| urls.push url.readAttribute("href") | |
| urls |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| describe 'Parser', -> | |
| html = ''' | |
| <ul class="tracks"> | |
| <li class="track" data-title="lorem" data-type="video"> | |
| <ul class="sources"> | |
| <li><a data-type="ogg" href="http://playerhtml5.dev/media/trailer.ogg">OGG</a></li> | |
| <li><a data-type="m4v" href="http://playerhtml5.dev/media/trailer_iphone.m4v">m4v</a></li> | |
| </ul> | |
| </li> | |
| <li class="track" data-title="ipsum" data-type="photo"> | |
| <a href="http://playerhtml5.dev/media/header1.jpg">ipsum</a> | |
| </li> | |
| <li class="track" data-title="dolor" data-type="document"> | |
| <ol class="sources"> | |
| <li><a href="http://playerhtml5.dev/media/previews/bbb-splash.thumbnail.png">1</a></li> | |
| <li><a href="http://playerhtml5.dev/media/previews/evil-frank.thumbnail.png">2</a></li> | |
| <li><a href="http://playerhtml5.dev/media/previews/rodents.thumbnail.png">3</a></li> | |
| </ol> | |
| </li> | |
| <li class="track" data-title="sit" data-type="video"> | |
| <ul class="sources"> | |
| <li><a data-type="ogg" href="sintel_trailer-480p.ogv">OGG</a></li> | |
| <li><a data-type="m4v" href="sintel_trailer-480p.mp4">m4v</a></li> | |
| </ul> | |
| </li> | |
| </ul> | |
| ''' | |
| content = new Element('html').update(html) | |
| parser = new Parser(content) | |
| it 'should parse json ', -> | |
| expect(parser.html.innerHTML).toMatch('tracks') | |
| it 'should have 4 tracks', -> | |
| expect(parser.toJSON().tracks.length).toEqual 4 | |
| describe 'a track', -> | |
| beforeEach -> | |
| result = parser.parse | |
| it 'should have a title', -> | |
| expect(parser.tracks.first().title).toEqual 'lorem' | |
| it 'should have a type', -> | |
| expect(parser.tracks.first().type).toEqual 'video' | |
| describe 'video object parsing', -> | |
| video = content.down('.tracks > li') | |
| it 'should have 2 src', -> | |
| expect(video.select('a').length).toEqual 2 | |
| it 'should have an ogg url', -> | |
| expect(parser.parseVideo(video)['ogg']).toEqual 'http://playerhtml5.dev/media/trailer.ogg' | |
| describe 'photo object parsing', -> | |
| photo = content.down('.tracks > li:nth-child(2)') | |
| it 'should have a photo url', -> | |
| expect(parser.parsePhoto(photo)[0]).toEqual 'http://playerhtml5.dev/media/header1.jpg' | |
| describe 'document object parsing', -> | |
| document = content.down('.tracks > li:nth-child(3)') | |
| it 'should have 3 element', -> | |
| expect(parser.parseDocument(document).length).toEqual 3 | |
| describe 'should keep the HTML order', -> | |
| it 'should have bbb as first element', -> | |
| expect(parser.parseDocument(document).first()).toEqual "http://playerhtml5.dev/media/previews/bbb-splash.thumbnail.png" | |
| it 'should have rodents as last element', -> | |
| expect(parser.parseDocument(document).last()).toEqual "http://playerhtml5.dev/media/previews/rodents.thumbnail.png" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment