1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- '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) {
- Attrs.call(this);
- this.name = name;
- 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 err = new Error('tag.clone is deprecated and will be removed in v2.0.0');
- console.warn(err.stack);
-
- 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;
- };
|