All files / src/meta table.spec.ts

100% Statements 82/82
100% Branches 1/1
100% Functions 24/24
100% Lines 82/82
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 1751x 1x 1x       10x 10x       1x   1x   1x       1x 2x 2x         1x 1x 1x 1x     1x 1x 1x         1x       1x   1x 2x 2x               1x 1x 1x 1x 1x     1x 1x 1x 1x 1x         1x   1x 2x 2x         1x 1x 1x 1x 1x     1x 1x 1x 1x 1x         1x   1x 6x 6x           1x 1x 1x 1x 1x     1x 1x 1x 1x 1x     1x 1x 1x 1x 1x     1x 1x 1x 1x 1x     1x 1x 1x 1x 1x     1x 1x 1x 1x 1x                   24x                                      
import { MetaTable, MetaElement } from 'meta';
import { Config } from '../config';
import { Parser } from '../parser';
 
class ConfigMock extends Config {
	constructor(metaTable: MetaTable){
		super();
		this.metaTable = metaTable;
	}
}
 
describe('MetaTable', function(){
 
	const expect = require('chai').expect;
 
	describe('getMetaFor', function(){
 
		let table: MetaTable;
 
		beforeEach(function(){
			table = new MetaTable();
			table.loadFromObject({
				foo: mockEntry('foo', {phrasing: true}),
			});
		});
 
		it('should be populated for known elements', function(){
			const meta = table.getMetaFor('foo');
			expect(meta).not.to.be.undefined;
			expect(meta.tagName).to.equal('foo');
		});
 
		it('should be null for unknown elements', function(){
			const meta = table.getMetaFor('bar');
			expect(meta).to.be.null;
		});
 
	});
 
	describe('expression', function(){
 
		let table: MetaTable;
 
		describe('isDescendant', function(){
 
			beforeEach(function(){
				table = new MetaTable();
				table.loadFromObject({
					foo: mockEntry('foo'),
					spam: mockEntry('spam'),
					ham: mockEntry('ham'),
					dynamic: mockEntry('dynamic', {interactive: ['isDescendant', 'ham'], void: true}),
				});
			});
 
			it('should be true if child is a descendant of given tagName', function(){
				const parser = new Parser(new ConfigMock(table));
				const dom = parser.parseHtml('<foo><ham><dynamic/></ham></foo>').root;
				const el = dom.getElementsByTagName('dynamic');
				expect(el[0].meta.interactive).to.be.true;
			});
 
			it('should be false if child is not a descendant of given tagName', function(){
				const parser = new Parser(new ConfigMock(table));
				const dom = parser.parseHtml('<foo><spam><dynamic/></spam></foo>').root;
				const el = dom.getElementsByTagName('dynamic');
				expect(el[0].meta.interactive).to.be.false;
			});
 
		});
 
		describe('hasAttribute', function(){
 
			beforeEach(function(){
				table = new MetaTable();
				table.loadFromObject({
					dynamic: mockEntry('dynamic', {interactive: ['hasAttribute', 'foo'], void: true}),
				});
			});
 
			it('should be true if element has given attribute', function(){
				const parser = new Parser(new ConfigMock(table));
				const dom = parser.parseHtml('<dynamic foo/>').root;
				const el = dom.getElementsByTagName('dynamic');
				expect(el[0].meta.interactive).to.be.true;
			});
 
			it('should be false if element does not have given attribute', function(){
				const parser = new Parser(new ConfigMock(table));
				const dom = parser.parseHtml('<dynamic bar/>').root;
				const el = dom.getElementsByTagName('dynamic');
				expect(el[0].meta.interactive).to.be.false;
			});
 
		});
 
		describe('matchAttribute', function(){
 
			beforeEach(function(){
				table = new MetaTable();
				table.loadFromObject({
					foo: mockEntry('dynamic', {interactive: ['matchAttribute', ['type', '=', 'hidden']], void: true}),
					bar: mockEntry('dynamic', {interactive: ['matchAttribute', ['type', '!=', 'hidden']], void: true}),
				});
			});
 
			it('should be true when "=" is used to match existing value', function(){
				const parser = new Parser(new ConfigMock(table));
				const dom = parser.parseHtml('<foo type="hidden"/>').root;
				const el = dom.getElementsByTagName('foo');
				expect(el[0].meta.interactive).to.be.true;
			});
 
			it('should be false when "=" is used to match other value', function(){
				const parser = new Parser(new ConfigMock(table));
				const dom = parser.parseHtml('<foo type="other"/>').root;
				const el = dom.getElementsByTagName('foo');
				expect(el[0].meta.interactive).to.be.false;
			});
 
			it('should be false when "=" is used to match missing value', function(){
				const parser = new Parser(new ConfigMock(table));
				const dom = parser.parseHtml('<foo/>').root;
				const el = dom.getElementsByTagName('foo');
				expect(el[0].meta.interactive).to.be.false;
			});
 
			it('should be false when "!=" is used to match existing value', function(){
				const parser = new Parser(new ConfigMock(table));
				const dom = parser.parseHtml('<bar type="hidden"/>').root;
				const el = dom.getElementsByTagName('bar');
				expect(el[0].meta.interactive).to.be.false;
			});
 
			it('should be true when "!=" is used to match other value', function(){
				const parser = new Parser(new ConfigMock(table));
				const dom = parser.parseHtml('<bar type="other"/>').root;
				const el = dom.getElementsByTagName('bar');
				expect(el[0].meta.interactive).to.be.true;
			});
 
			it('should be false when "!=" is used to match missing value', function(){
				const parser = new Parser(new ConfigMock(table));
				const dom = parser.parseHtml('<bar/>').root;
				const el = dom.getElementsByTagName('bar');
				expect(el[0].meta.interactive).to.be.true;
			});
 
		});
 
	});
 
});
 
function mockEntry(tagName: string, stub = {}): MetaElement {
	return Object.assign({
		tagName,
		metadata: false,
		flow: false,
		sectioning: false,
		heading: false,
		phrasing: false,
		embedded: false,
		interactive: false,
		deprecated: false,
		void: false,
		transparent: false,
		implicitClosed: [],
		deprecatedAttributes: [],
		permittedContent: [],
		permittedDescendants: [],
		permittedOrder: [],
	}, stub);
}