1var assert = require('assert'), 2 path = require('path'), 3 SimpleApiParser = require('../../index').SimpleApiParser, 4 TestUtils = require('../test_utils'); 5 6function getFullTestName(test, idx) { 7 return ['SimpleApiParser - ', idx, '.', test.name].join(''); 8} 9 10function sanitizeForComparison(str) { 11 return TestUtils.removeNewLines(str) 12 .replace(/\s/g, '') 13 .replace(/'/g, '"') 14 .toLowerCase(); 15} 16 17 18function createTest(html, expected, options) { 19 return function () { 20 //NOTE: the idea of the test is to serialize back given HTML using SimpleApiParser handlers 21 var actual = '', 22 parser = new SimpleApiParser({ 23 doctype: function (name, publicId, systemId) { 24 actual += '<!DOCTYPE ' + name; 25 26 if (publicId !== null) 27 actual += ' PUBLIC "' + publicId + '"'; 28 29 else if (systemId !== null) 30 actual += ' SYSTEM'; 31 32 if (systemId !== null) 33 actual += ' "' + systemId + '"'; 34 35 36 actual += '>'; 37 }, 38 39 startTag: function (tagName, attrs, selfClosing) { 40 actual += '<' + tagName; 41 42 if (attrs.length) { 43 for (var i = 0; i < attrs.length; i++) 44 actual += ' ' + attrs[i].name + '="' + attrs[i].value + '"'; 45 } 46 47 actual += selfClosing ? '/>' : '>'; 48 }, 49 50 endTag: function (tagName) { 51 actual += '</' + tagName + '>'; 52 }, 53 54 text: function (text) { 55 actual += text; 56 }, 57 58 comment: function (text) { 59 actual += '<!--' + text + '-->'; 60 } 61 }, options); 62 63 parser.parse(html); 64 65 expected = sanitizeForComparison(expected); 66 actual = sanitizeForComparison(actual); 67 68 //NOTE: use ok assertion, so output will not be polluted by the whole content of the strings 69 assert.ok(actual === expected, TestUtils.getStringDiffMsg(actual, expected)); 70 }; 71} 72 73TestUtils.loadSerializationTestData(path.join(__dirname, '../data/simple_api_parsing')) 74 .concat([ 75 { 76 name: 'Options - decodeHtmlEntities (text)', 77 src: '<div>&©</div>', 78 expected: '<div>&©</div>', 79 options: { 80 decodeHtmlEntities: false 81 } 82 }, 83 84 { 85 name: 'Options - decodeHtmlEntities (attributes)', 86 src: '<a href = "&test<" ©>Yo</a>', 87 expected: '<a href = "&test<" ©="">Yo</a>', 88 options: { 89 decodeHtmlEntities: false 90 } 91 } 92 ]) 93 .forEach(function (test, idx) { 94 var testName = getFullTestName(test, idx); 95 96 exports[testName] = createTest(test.src, test.expected, test.options); 97 98 exports['Options - locationInfo - ' + testName] = function () { 99 //NOTE: we've already tested the correctness of the location info with the Tokenizer tests. 100 //So here we just check that SimpleApiParser provides this info in the handlers. 101 var handlers = ['doctype', 'startTag', 'endTag', 'text', 'comment'].reduce(function (handlers, key) { 102 handlers[key] = function () { 103 var locationInfo = arguments[arguments.length - 1]; 104 105 assert.strictEqual(typeof locationInfo.start, 'number'); 106 assert.strictEqual(typeof locationInfo.end, 'number'); 107 }; 108 return handlers; 109 }, {}), 110 parser = new SimpleApiParser(handlers, {locationInfo: true}); 111 112 parser.parse(test.src); 113 }; 114 }); 115 116 117 118