@dooboostore/dom-render
Version:
html view template engine
183 lines • 4.93 kB
JavaScript
import { Compiler as BaseCompiler } from './compiler';
/**
* Initialize a new `Compiler`.
*/
export class IdentityCompiler extends BaseCompiler {
constructor(options = {}) {
super(options);
this.indentation = typeof options.indent === 'string' ? options.indent : ' ';
this.level = 1;
}
/**
* Compile `node`.
*/
compile(node) {
return this.stylesheet(node);
}
/**
* Visit stylesheet node.
*/
stylesheet(node) {
return this.mapVisit(node.stylesheet.rules, '\n\n');
}
/**
* Visit comment node.
*/
comment(node) {
return this.emit(this.indent() + '/*' + node.comment + '*/');
}
/**
* Visit import node.
*/
import(node) {
return this.emit('@import ' + node.import + ';');
}
/**
* Visit media node.
*/
media(node) {
return this.emit('@media ' + node.media)
+ this.emit(' {\n'
+ this.indent(1))
+ this.mapVisit(node.rules, '\n\n')
+ this.emit(this.indent(-1)
+ '\n}');
}
/**
* Visit document node.
*/
document(node) {
const doc = '@' + (node.vendor || '') + 'document ' + node.document;
return this.emit(doc)
+ this.emit(' '
+ ' {\n'
+ this.indent(1))
+ this.mapVisit(node.rules, '\n\n')
+ this.emit(this.indent(-1)
+ '\n}');
}
/**
* Visit charset node.
*/
charset(node) {
return this.emit('@charset ' + node.charset + ';');
}
/**
* Visit namespace node.
*/
namespace(node) {
return this.emit('@namespace ' + node.namespace + ';');
}
/**
* Visit supports node.
*/
supports(node) {
return this.emit('@supports ' + node.supports)
+ this.emit(' {\n'
+ this.indent(1))
+ this.mapVisit(node.rules, '\n\n')
+ this.emit(this.indent(-1)
+ '\n}');
}
/**
* Visit keyframes node.
*/
keyframes(node) {
return this.emit('@' + (node.vendor || '') + 'keyframes ' + node.name)
+ this.emit(' {\n'
+ this.indent(1))
+ this.mapVisit(node.keyframes, '\n')
+ this.emit(this.indent(-1)
+ '}');
}
/**
* Visit keyframe node.
*/
keyframe(node) {
const decls = node.declarations;
return this.emit(this.indent())
+ this.emit(node.values.join(', '))
+ this.emit(' {\n'
+ this.indent(1))
+ this.mapVisit(decls, '\n')
+ this.emit(this.indent(-1)
+ '\n'
+ this.indent() + '}\n');
}
/**
* Visit page node.
*/
page(node) {
const sel = node.selectors.length
? node.selectors.join(', ') + ' '
: '';
return this.emit('@page ' + sel)
+ this.emit('{\n')
+ this.emit(this.indent(1))
+ this.mapVisit(node.declarations, '\n')
+ this.emit(this.indent(-1))
+ this.emit('\n}');
}
/**
* Visit font-face node.
*/
'font-face'(node) {
return this.emit('@font-face ')
+ this.emit('{\n')
+ this.emit(this.indent(1))
+ this.mapVisit(node.declarations, '\n')
+ this.emit(this.indent(-1))
+ this.emit('\n}');
}
/**
* Visit host node.
*/
host(node) {
return this.emit('@host')
+ this.emit(' {\n'
+ this.indent(1))
+ this.mapVisit(node.rules, '\n\n')
+ this.emit(this.indent(-1)
+ '\n}');
}
/**
* Visit custom-media node.
*/
'custom-media'(node) {
return this.emit('@custom-media ' + node.name + ' ' + node.media + ';');
}
/**
* Visit rule node.
*/
rule(node) {
const indent = this.indent();
const decls = node.declarations;
if (!decls.length)
return '';
return this.emit(node.selectors.map(s => indent + s).join(',\n'))
+ this.emit(' {\n')
+ this.emit(this.indent(1))
+ this.mapVisit(decls, '\n')
+ this.emit(this.indent(-1))
+ this.emit('\n' + this.indent() + '}');
}
/**
* Visit declaration node.
*/
declaration(node) {
return this.emit(this.indent())
+ this.emit(node.property + ': ' + node.value)
+ this.emit(';');
}
/**
* Increase, decrease or return current indentation.
*/
indent(level) {
if (level != null) {
this.level += level;
return '';
}
return Array(this.level).join(this.indentation);
}
}
//# sourceMappingURL=identity.js.map