UNPKG

1.05 kBJavaScriptView Raw
1'use strict';
2
3var node = ['tag', 'script', 'style'],
4 singular = [
5 'area', 'base', 'br', 'col', 'command', 'embed', 'hr',
6 'img', 'input', 'link', 'meta', 'param', 'source', 'wbr',
7 'track'
8 ];
9
10function attributes(element) {
11 var attr = element.attribs;
12
13 if (!attr || typeof attr !== 'object') {
14 return '';
15 }
16
17 return Object.keys(attr).reduce(function (result, key) {
18 return result + ' ' + key + '="' + attr[key] + '"';
19 }, '');
20}
21
22exports.tag = exports.script = exports.style = function tag(element) {
23 return '<' + element.name + attributes(element) + '>';
24};
25
26exports.text = function text(element) {
27 return element.data;
28};
29
30exports.comment = function comment(element) {
31 return '<!--' + element.data + '-->';
32};
33
34exports.directive = function directive(element) {
35 return '<' + element.data + '>';
36};
37
38exports.close = function close(element) {
39 return ~node.indexOf(element.type) && !~singular.indexOf(element.name)
40 ? '</' + element.name + '>'
41 : '';
42};