/* Unit test for get */ assert = chai.assert; describe('Microformat.get', function() { var expected = { 'items': [{ 'type': ['h-card'], 'properties': { 'name': ['Glenn Jones'], 'url': ['http://glennjones.net'] } }], 'rels': { 'bookmark': ['http://glennjones.net'], 'alternate': ['http://example.com/fr'], 'home': ['http://example.com/fr'] }, 'rel-urls': { 'http://glennjones.net': { 'text': 'Glenn Jones', 'rels': ['bookmark'] }, 'http://example.com/fr': { 'media': 'handheld', 'hreflang': 'fr', 'text': 'French mobile homepage', 'rels': ['alternate', 'home'] } } }, html = '
Glenn Jones
French mobile homepage'; it('get - no options.node parse this document', function(){ var result; result = Microformats.get({}); assert.deepEqual( result.items, [] ); }); it('get - standard', function(){ var doc, node, options, result; doc = document.implementation.createHTMLDocument('New Document'); node = document.createElement('div'); node.innerHTML = html; doc.body.appendChild(node); options ={ 'node': node, 'baseUrl': 'http://example.com', 'dateFormat': 'html5' }; result = Microformats.get(options); assert.deepEqual( result, expected ); }); it('get - doc pass to node', function(){ var doc, node, options, result; doc = document.implementation.createHTMLDocument('New Document'); node = document.createElement('div'); node.innerHTML = html; doc.body.appendChild(node); options ={ 'node': doc, 'baseUrl': 'http://example.com', 'dateFormat': 'html5' }; result = Microformats.get(options); assert.deepEqual( result, expected ); }); it('get - pass base tag', function(){ var doc, node, options, result; doc = document.implementation.createHTMLDocument('New Document'); node = document.createElement('div'); node.innerHTML = html + ''; doc.body.appendChild(node); options ={ 'node': node, 'dateFormat': 'html5' }; result = Microformats.get(options); assert.deepEqual( result, expected ); }); it('get - pass no document', function(){ var doc, node, options, parser, result; doc = document.implementation.createHTMLDocument('New Document'); node = document.createElement('div'); node.innerHTML = html + ''; doc.body.appendChild(node); options ={ 'node': doc, 'dateFormat': 'html5' }; result = Microformats.get(options); assert.deepEqual( result, expected ); }); it('get - textFormat: normalised', function(){ var doc, node, options, result; var altHTML = '\n'; altHTML += ' Glenn\n'; altHTML += ' Jones\n'; altHTML += '\n'; doc = document.implementation.createHTMLDocument('New Document'); node = document.createElement('div'); node.innerHTML = altHTML; doc.body.appendChild(node); options ={ 'node': node, 'textFormat': 'normalised', 'dateFormat': 'html5' }; result = Microformats.get(options); assert.equal( result.items[0].properties.name[0], 'Glenn Jones' ); }); it('get - textFormat: whitespace', function(){ var doc, node, options, parser, result; var altHTML = '\n'; altHTML += ' Glenn\n'; altHTML += ' Jones\n'; altHTML += '\n'; doc = document.implementation.createHTMLDocument('New Document'); node = document.createElement('div'); node.innerHTML = altHTML; doc.body.appendChild(node); options ={ 'node': node, 'textFormat': 'whitespace', 'dateFormat': 'html5' }; result = Microformats.get(options); assert.equal( result.items[0].properties.name[0], '\n Glenn\n Jones\n' ); }); it('get - textFormat: whitespacetrimmed', function(){ var doc, node, options, result; var altHTML = '\n'; altHTML += ' Glenn\n'; altHTML += ' Jones\n'; altHTML += '\n'; doc = document.implementation.createHTMLDocument('New Document'); node = document.createElement('div'); node.innerHTML = altHTML; doc.body.appendChild(node); options ={ 'node': node, 'textFormat': 'whitespacetrimmed', 'dateFormat': 'html5' }; result = Microformats.get(options); assert.equal( result.items[0].properties.name[0], 'Glenn\n Jones' ); }); it('get - dateFormat: auto', function(){ var doc, node, options, result; var altHTML = '
Pub2015-07-01t17:30z
'; doc = document.implementation.createHTMLDocument('New Document'); node = document.createElement('div'); node.innerHTML = altHTML; doc.body.appendChild(node); options ={ 'node': node, 'dateFormat': 'auto' }; result = Microformats.get(options); assert.equal( result.items[0].properties.start[0], '2015-07-01t17:30z' ); }); it('get - dateFormat: w3c', function(){ var doc, node, options, result; var altHTML = '
Pub2015-07-01t17:30z
'; doc = document.implementation.createHTMLDocument('New Document'); node = document.createElement('div'); node.innerHTML = altHTML; doc.body.appendChild(node); options ={ 'node': node, 'dateFormat': 'w3c' }; result = Microformats.get(options); assert.equal( result.items[0].properties.start[0], '2015-07-01T17:30Z' ); }); it('get - dateFormat: html5', function(){ var doc, node, options, result; var altHTML = '
Pub2015-07-01t17:30z
'; doc = document.implementation.createHTMLDocument('New Document'); node = document.createElement('div'); node.innerHTML = altHTML; doc.body.appendChild(node); options ={ 'node': node, 'dateFormat': 'html5' }; result = Microformats.get(options); assert.equal( result.items[0].properties.start[0], '2015-07-01 17:30Z' ); }); it('get - dateFormat: rfc3339', function(){ var doc, node, options, result; var altHTML = '
Pub2015-07-01t17:30z
'; doc = document.implementation.createHTMLDocument('New Document'); node = document.createElement('div'); node.innerHTML = altHTML; doc.body.appendChild(node); options ={ 'node': node, 'dateFormat': 'rfc3339' }; result = Microformats.get(options); assert.equal( result.items[0].properties.start[0], '20150701T1730Z' ); }); it('get - filters h-card', function(){ var doc, node, options, parser, result; var altHTML = '
Pub2015-07-01t17:30z
Glenn Jones'; var altExpected = { 'items': [{ 'type': ['h-card'], 'properties': { 'name': ['Glenn Jones'], 'url': ['http://glennjones.net'] } }], 'rels': {}, 'rel-urls': {} } doc = document.implementation.createHTMLDocument('New Document'); node = document.createElement('div'); node.innerHTML = altHTML; doc.body.appendChild(node); // filter as an array options ={ 'node': node, 'filters': ['h-card'], 'dateFormat': 'html5' }; result = Microformats.get(options); assert.deepEqual( result, altExpected ); // filter as an string options ={ 'node': node, 'filters': 'h-card', 'dateFormat': 'html5' }; result = Microformats.get(options); assert.deepEqual( result, altExpected ); }); it('get - filters h-event', function(){ var doc, node, options, result; var altHTML = '
Pub2015-07-01t17:30z
Glenn Jones'; var altExpected = { 'items': [{ 'type': ['h-event'], 'properties': { 'name': ['Pub'], 'start': ['2015-07-01 17:30Z'] } }], 'rels': {}, 'rel-urls': {} } doc = document.implementation.createHTMLDocument('New Document'); node = document.createElement('div'); node.innerHTML = altHTML; doc.body.appendChild(node); options ={ 'node': node, 'filters': ['h-event'], 'dateFormat': 'html5' }; result = Microformats.get(options); assert.deepEqual( result, altExpected ); }); it('get - filters h-card and h-event', function(){ var doc, node, options, result; var altHTML = '
Pub2015-07-01t17:30z
Glenn Jones'; var altExpected = { 'items': [{ 'type': ['h-event'], 'properties': { 'name': ['Pub'], 'start': ['2015-07-01 17:30Z'] } }, { 'type': ['h-card'], 'properties': { 'name': ['Glenn Jones'], 'url': ['http://glennjones.net'] } }], 'rels': {}, 'rel-urls': {} } doc = document.implementation.createHTMLDocument('New Document'); node = document.createElement('div'); node.innerHTML = altHTML; doc.body.appendChild(node); options ={ 'node': node, 'filter': ['h-event'], 'dateFormat': 'html5' }; result = Microformats.get(options); assert.deepEqual( result, altExpected ); }); it('get - filters h-card no result', function(){ var doc, node, options, result; var altHTML = '
Pub2015-07-01t17:30z
'; var altExpected = { 'items': [], 'rels': {}, 'rel-urls': {} } doc = document.implementation.createHTMLDocument('New Document'); node = document.createElement('div'); node.innerHTML = altHTML; doc.body.appendChild(node); options ={ 'node': node, 'filters': ['h-card'], 'dateFormat': 'html5' }; result = Microformats.get(options); assert.deepEqual( result, altExpected ); }); it('get - filters h-card match v1 format', function(){ var doc, node, options, parser, result; var altHTML = 'Glenn Jones'; var altExpected = { 'items': [{ 'type': ['h-card'], 'properties': { 'name': ['Glenn Jones'] } }], 'rels': {}, 'rel-urls': {} } doc = document.implementation.createHTMLDocument('New Document'); node = document.createElement('div'); node.innerHTML = altHTML; doc.body.appendChild(node); options ={ 'node': node, 'filter': ['h-card'], 'dateFormat': 'html5' }; result = Microformats.get(options); assert.deepEqual( result, altExpected ); }); it('get - add new v1 format through options', function(){ var doc, node, options, result; var altHTML = '
£36.78
'; var altExpected = { 'items': [{ 'type': ['h-payment'], 'properties': { 'amount': ['36.78'] } }], 'rels': {}, 'rel-urls': {} }; var v1Definition = { root: 'hpayment', name: 'h-payment', properties: { 'amount': {} } }; doc = document.implementation.createHTMLDocument('New Document'); node = document.createElement('div'); node.innerHTML = altHTML; doc.body.appendChild(node); options ={ 'node': node, 'maps': v1Definition, 'dateFormat': 'html5' }; result = Microformats.get(options); assert.deepEqual( result, altExpected ); }); it('get - options.html', function(){ var options, result; options ={ 'html': html, 'baseUrl': 'http://example.com', 'dateFormat': 'html5' }; result = Microformats.get(options); assert.deepEqual( result, expected ); }); });