You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
88 lines
1.7 KiB
JavaScript
88 lines
1.7 KiB
JavaScript
'use strict';
|
|
|
|
var Attrs = require('./attrs');
|
|
var Block = require('./block');
|
|
var inlineTags = require('../inline-tags');
|
|
|
|
/**
|
|
* Initialize a `Tag` node with the given tag `name` and optional `block`.
|
|
*
|
|
* @param {String} name
|
|
* @param {Block} block
|
|
* @api public
|
|
*/
|
|
|
|
var Tag = module.exports = function Tag(name, block) {
|
|
this.name = name;
|
|
this.attrs = [];
|
|
this.attributeBlocks = [];
|
|
this.block = block || new Block;
|
|
};
|
|
|
|
// Inherit from `Attrs`.
|
|
Tag.prototype = Object.create(Attrs.prototype);
|
|
Tag.prototype.constructor = Tag;
|
|
|
|
Tag.prototype.type = 'Tag';
|
|
|
|
/**
|
|
* Clone this tag.
|
|
*
|
|
* @return {Tag}
|
|
* @api private
|
|
*/
|
|
|
|
Tag.prototype.clone = function(){
|
|
var clone = new Tag(this.name, this.block.clone());
|
|
clone.line = this.line;
|
|
clone.attrs = this.attrs;
|
|
clone.textOnly = this.textOnly;
|
|
return clone;
|
|
};
|
|
|
|
/**
|
|
* Check if this tag is an inline tag.
|
|
*
|
|
* @return {Boolean}
|
|
* @api private
|
|
*/
|
|
|
|
Tag.prototype.isInline = function(){
|
|
return ~inlineTags.indexOf(this.name);
|
|
};
|
|
|
|
/**
|
|
* Check if this tag's contents can be inlined. Used for pretty printing.
|
|
*
|
|
* @return {Boolean}
|
|
* @api private
|
|
*/
|
|
|
|
Tag.prototype.canInline = function(){
|
|
var nodes = this.block.nodes;
|
|
|
|
function isInline(node){
|
|
// Recurse if the node is a block
|
|
if (node.isBlock) return node.nodes.every(isInline);
|
|
return node.isText || (node.isInline && node.isInline());
|
|
}
|
|
|
|
// Empty tag
|
|
if (!nodes.length) return true;
|
|
|
|
// Text-only or inline-only tag
|
|
if (1 == nodes.length) return isInline(nodes[0]);
|
|
|
|
// Multi-line inline-only tag
|
|
if (this.block.nodes.every(isInline)) {
|
|
for (var i = 1, len = nodes.length; i < len; ++i) {
|
|
if (nodes[i-1].isText && nodes[i].isText)
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
// Mixed tag
|
|
return false;
|
|
};
|