UNPKG

1.86 kBJavaScriptView Raw
1'use strict';
2
3var Attrs = require('./attrs');
4var Block = require('./block');
5var inlineTags = require('../inline-tags');
6
7/**
8 * Initialize a `Tag` node with the given tag `name` and optional `block`.
9 *
10 * @param {String} name
11 * @param {Block} block
12 * @api public
13 */
14
15var Tag = module.exports = function Tag(name, block) {
16 Attrs.call(this);
17 this.name = name;
18 this.block = block || new Block;
19};
20
21// Inherit from `Attrs`.
22Tag.prototype = Object.create(Attrs.prototype);
23Tag.prototype.constructor = Tag;
24
25Tag.prototype.type = 'Tag';
26
27/**
28 * Clone this tag.
29 *
30 * @return {Tag}
31 * @api private
32 */
33
34Tag.prototype.clone = function(){
35 var err = new Error('tag.clone is deprecated and will be removed in v2.0.0');
36 console.warn(err.stack);
37
38 var clone = new Tag(this.name, this.block.clone());
39 clone.line = this.line;
40 clone.attrs = this.attrs;
41 clone.textOnly = this.textOnly;
42 return clone;
43};
44
45/**
46 * Check if this tag is an inline tag.
47 *
48 * @return {Boolean}
49 * @api private
50 */
51
52Tag.prototype.isInline = function(){
53 return ~inlineTags.indexOf(this.name);
54};
55
56/**
57 * Check if this tag's contents can be inlined. Used for pretty printing.
58 *
59 * @return {Boolean}
60 * @api private
61 */
62
63Tag.prototype.canInline = function(){
64 var nodes = this.block.nodes;
65
66 function isInline(node){
67 // Recurse if the node is a block
68 if (node.isBlock) return node.nodes.every(isInline);
69 return node.isText || (node.isInline && node.isInline());
70 }
71
72 // Empty tag
73 if (!nodes.length) return true;
74
75 // Text-only or inline-only tag
76 if (1 == nodes.length) return isInline(nodes[0]);
77
78 // Multi-line inline-only tag
79 if (this.block.nodes.every(isInline)) {
80 for (var i = 1, len = nodes.length; i < len; ++i) {
81 if (nodes[i-1].isText && nodes[i].isText)
82 return false;
83 }
84 return true;
85 }
86
87 // Mixed tag
88 return false;
89};