1 | 'use strict';
|
2 |
|
3 | var Attrs = require('./attrs');
|
4 | var Block = require('./block');
|
5 | var inlineTags = require('../inline-tags');
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | var Tag = module.exports = function Tag(name, block) {
|
16 | Attrs.call(this);
|
17 | this.name = name;
|
18 | this.block = block || new Block;
|
19 | };
|
20 |
|
21 |
|
22 | Tag.prototype = Object.create(Attrs.prototype);
|
23 | Tag.prototype.constructor = Tag;
|
24 |
|
25 | Tag.prototype.type = 'Tag';
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | Tag.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 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | Tag.prototype.isInline = function(){
|
53 | return ~inlineTags.indexOf(this.name);
|
54 | };
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 | Tag.prototype.canInline = function(){
|
64 | var nodes = this.block.nodes;
|
65 |
|
66 | function isInline(node){
|
67 |
|
68 | if (node.isBlock) return node.nodes.every(isInline);
|
69 | return node.isText || (node.isInline && node.isInline());
|
70 | }
|
71 |
|
72 |
|
73 | if (!nodes.length) return true;
|
74 |
|
75 |
|
76 | if (1 == nodes.length) return isInline(nodes[0]);
|
77 |
|
78 |
|
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 |
|
88 | return false;
|
89 | };
|