'use strict'; var Node = require('./Node'); var Literal = require('./Literal'); var escapeXmlAttr = require('raptor-util/escapeXml').attr; var HtmlAttributeCollection = require('./HtmlAttributeCollection'); class HtmlElement extends Node { constructor(def) { super('HtmlElement'); var tagName = def.tagName; this.tagName = null; this.dynamicTagName = null; if (tagName instanceof Node) { if (tagName instanceof Literal) { this.tagName = tagName.value; } else { this.dynamicTagName = tagName; } } else if (typeof tagName === 'string'){ this.tagName = tagName; } this._attributes = def.attributes; if (!(this._attributes instanceof HtmlAttributeCollection)) { this._attributes = new HtmlAttributeCollection(this._attributes); } this.body = this.makeContainer(def.body); this.argument = def.argument; this.allowSelfClosing = false; this.startTagOnly = false; this._dynamicAttributesExpressionArray = undefined; } generateHtmlCode(generator) { var tagName = this.tagName; // Convert the tag name into a Node so that we generate the code correctly if (tagName) { tagName = generator.builder.literal(tagName); } else { tagName = this.dynamicTagName; } var body = this.body; var startTagOnly = this.startTagOnly; var allowSelfClosing = this.allowSelfClosing; var hasBody = body && body.length; var builder = generator.builder; // Starting tag generator.addWriteLiteral('<'); generator.addWrite(tagName); var attributes = this._attributes && this._attributes.all; if (attributes) { for (let i=0; i'); generator.generateStatements(body); generator.addWriteLiteral(''); } else { if (startTagOnly) { generator.addWriteLiteral('>'); } else if (allowSelfClosing) { generator.addWriteLiteral('/>'); } else { generator.addWriteLiteral('>'); } } } addDynamicAttributes(expression) { if (!this._dynamicAttributesExpressionArray) { this._dynamicAttributesExpressionArray = []; } this._dynamicAttributesExpressionArray.push(expression); } getAttribute(name) { return this._attributes != null && this._attributes.getAttribute(name); } getAttributeValue(name) { var attr = this._attributes != null && this._attributes.getAttribute(name); if (attr) { return attr.value; } } setAttributeValue(name, value) { this._attributes.setAttributeValue(name, value); } removeAttribute(name) { if (this._attributes) { this._attributes.removeAttribute(name); } } hasAttribute(name) { return this._attributes != null && this._attributes.hasAttribute(name); } getAttributes() { return this._attributes.all; } get attributes() { return this._attributes.all; } forEachAttribute(callback, thisObj) { var attributes = this._attributes.all.concat([]); for (let i=0, len=attributes.length; i'; } toJSON() { return { type: this.type, tagName: this.tagName, attributes: this._attributes, argument: this.argument, body: this.body }; } } module.exports = HtmlElement;