/*jslint indent: 2, maxlen: 80, node: true */
/* -*- tab-width: 2 -*- */
'use strict';
function throw2string(func) {
return function () {
try {
return func.apply(this, arguments);
} catch (err) {
return String(err);
}
};
}
/*** BEGIN readme ***/
var xmlAttrDict = require('xmlattrdict'), input, result,
assert = require('assert');
function expect(x) { assert.deepStrictEqual(result, x); }
//##########\\ tag -> dict //#############################################\\
result = xmlAttrDict('');
expect({ '': '?xml', version: '1.0' }); // final "?" is ignored
result = xmlAttrDict("");
expect({ '': '!--', I: true, ' ': "'m a comm& --" });
result = xmlAttrDict('
'); // v0.1.12 failed to detect tagname
expect({ '': 'hr' }); // if no attributes were given
result = xmlAttrDict('');
expect({ '': 'ubuntu', ver: '14.04', lts: true, name: 'trusty', '/': true });
result = xmlAttrDict('');
expect({ '': '/closing', tag: true, 'empty-attr': '' });
//===== repeating attribute names =====
input = '';
result = xmlAttrDict(input);
expect({ '': 'phones', line: [ '23', '42', 'hold' ] });
result = xmlAttrDict(input, { multi: true });
expect({ '': 'phones', line: [ '23', '42', 'hold' ] });
result = xmlAttrDict(input, { multi: false });
expect({ '': 'phones', line: '23' });
result = xmlAttrDict(input, { multi: '\n' });
expect({ '': 'phones', line: '23\n42\nhold' });
result = throw2string(xmlAttrDict)(input, { multi: 2 });
expect('Error: Unsupported merge strategy: 2');
function prepend(a, b) { return b + a; }
result = xmlAttrDict(input, { multi: prepend });
expect({ '': 'phones', line: 'hold4223' });
//===== capture attribute order =====
input = '';
result = xmlAttrDict(input, { attrOrder: '#' });
expect({ '': 'toast',
'#': ['bread', 'cheese', 'ham', 'salad', 'tomato', 'cheese', 'tomato',
'salad', 'bread'],
bread: [true, true], cheese: ['cheddar', 'gouda'], ham: true,
salad: [true, true], tomato: [true, true] });
result = ['add them here'];
xmlAttrDict(input, { attrOrder: result });
expect(['add them here', 'bread', 'cheese', 'ham', 'salad', 'tomato',
'cheese', 'tomato', 'salad', 'bread']);
//===== extra non-attribute text =====
result = xmlAttrDict('');
expect({ '': '/closing', withSlash: true, '/': true });
result = xmlAttrDict('');
expect({ '': '/closing', withSpaceSlash: true, '/': true });
//===== Tags with angle brackets inside them =====
input = (''
+ '\n ]>');
result = xmlAttrDict(input);
expect({ '': '!DOCTYPE',
'screw-basic-parsers': true,
'[]': [ '' ] });
//##########\\ dict -> tag //#############################################\\
result = xmlAttrDict({ '': '!DOCTYPE', ' ': 'html' });
expect('');
result = xmlAttrDict({ '': '!DOCTYPE', html: true });
expect('');
result = xmlAttrDict({ '': '!DOCTYPE', html: null });
expect('');
input = { '': 'order', a: [10, 1], z: 26, y: 25, c: 3, x: 24, b: 2 };
result = xmlAttrDict(input);
expect('');
result = xmlAttrDict(input, { attrOrder: false });
expect('');
result = xmlAttrDict(input, { attrOrder: ['c', 'a', 'z', 'A', 'c', 'a'] });
expect('');
result = xmlAttrDict({ '': '!--', thisIs: true, a: null, comment: true,
'>': ' --' });
expect('');
result = xmlAttrDict({ '': '!--', ' ': 'thisIs a comment', '>': ' --' });
expect('');
result = xmlAttrDict({ '': '!--', ' ': 'thisIs a comment --' });
expect('');
result = xmlAttrDict({ '': 'meta', 'http-equiv': 'Content-Type',
content: 'text/html' });
expect('');
result = xmlAttrDict({ '': 'msg', text: '&Hello s\nwman! ☃' });
expect('');
//===== additional verbatim text and tail =====
result = xmlAttrDict({ '': 'hr', size: 1 });
expect('
');
result = xmlAttrDict({ '': 'hr', size: 1, '>': '/' });
expect('
');
result = xmlAttrDict({ '': 'hr', size: 1, ' ': '/' });
expect('
'); // <-- space --^
result = xmlAttrDict({ '': 'hr', size: 1, '/': true });
expect('
');
result = xmlAttrDict({ '': 'hr', size: 1, ' ': 'verbatim' });
expect('
tim>');
result = xmlAttrDict({ '': 'hr', size: 1, '>': '&tail;' });
expect('
');
result = xmlAttrDict({ '': 'hr', size: 1, '>': '&tail', ' ': 'veratim' });
expect('
atim&tail>');
//===== invalid attribute names =====
input = { '': '\r', '\n': 'nl', '\t': 'tab', '=': 'eq', '?': 'qm', ' ': 'sp' };
result = throw2string(xmlAttrDict)(input);
expect('Error: bad keys: " ", "
", "=", "?"');
result = throw2string(xmlAttrDict)(input, { badKeys: 'error' });
expect('Error: bad keys: " ", "
", "=", "?"');
result = xmlAttrDict(input, { badKeys: 'accept' });
expect('<\r \t="tab" \n="nl" =="eq" ?="qm" sp>');
result = xmlAttrDict(input, { badKeys: 'comment' });
expect('<\r sp>');
//===== convenience attributes =====
// innerText (¶) and innerXML (|):
result = xmlAttrDict({ '': 'em', 'class': 'marked', '…': '[1]',
'¶': 'Typo: The "<" should have been a ">".',
'|': '' });
expect(''
+ 'Typo: The "<" should have been a ">".'
+ ''
+ '[1]');
//##########\\ document -> list //########################################\\
input = '
Back to
Homepage';
result = xmlAttrDict.splitXml(input);
expect([
{ '': 'li' },
{ '': 'a', href: '#' },
{ '': 'img', src: 'home.png' },
{ '…': ' Back to' },
{ '': 'br', '/': true },
{ '…': 'Homepage' },
{ '': '/a' },
{ '': '/li' },
]);
input = 'Be bold!'; // Document fragment can start with text.
result = xmlAttrDict.splitXml(input);
expect([{ '…': 'Be ' }, { '': 'b' }, { '…': 'bold!' }, { '': '/b' }]);
result = xmlAttrDict.splitXml(input, { wrapTexts: '=' });
expect([{ '=': 'Be ' }, { '': 'b' }, { '=': 'bold!' }, { '': '/b' }]);
result = xmlAttrDict.splitXml(input, { wrapTexts: '' }); // ambiguous!
expect([{ '': 'Be ' }, { '': 'b' }, { '': 'bold!' }, { '': '/b' }]);
result = xmlAttrDict.splitXml(input, { wrapTexts: false });
expect(['Be ', { '': 'b' }, 'bold!', { '': '/b' }]);
result = xmlAttrDict.splitXml(input, { textTagName: '=' });
expect([{ '': '=', '…': 'Be ' },
{ '': 'b' }, { '': '=', '…': 'bold!' }, { '': '/b' }]);
result = xmlAttrDict.splitXml(input, { wrapTexts: 'v', textTagName: 4 });
expect([{ '': 4, v: 'Be ' },
{ '': 'b' }, { '': 4, v: 'bold!' }, { '': '/b' }]);
//##########\\ list -> document //########################################\\
input = [
{ '': 'object', width: 50, height: 30 },
{ '': 'param', name: 'movie', value: 'main.swf' },
{ '': 'embed', src: 'main.swf', width: 50, height: 30 },
'Please install ',
{ '…': 'and enable ' },
{ '': '', '…': 'the flash player.' },
{ '': '/embed' },
{ '': '/object' },
];
result = xmlAttrDict.compileXml(input);
expect('');
/*** ENDOF readme ***/
console.log('+OK all tests passed');