This repository has been archived by the owner on Dec 12, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
abbreviation.js
119 lines (106 loc) · 3.09 KB
/
abbreviation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
* Utility functions to work with <code>AbbreviationNode</code> as HTML element
* @param {Function} require
* @param {Underscore} _
*/
if (typeof module === 'object' && typeof define !== 'function') {
var define = function (factory) {
module.exports = factory(require, exports, module);
};
}
define(function(require, exports, module) {
var elements = require('../assets/elements');
var tabStops = require('../assets/tabStops');
var utils = require('../utils/common');
var tagName = require('../resolver/tagName');
return {
/**
* Test if passed node is unary (no closing tag)
* @param {AbbreviationNode} node
* @return {Boolean}
*/
isUnary: function(node) {
if (node.children.length || node._text || this.isSnippet(node)) {
return false;
}
var r = node.data('resource');
return r && r.is_empty;
},
/**
* Test if passed node is inline-level (like <strong>, <img>)
* @param {AbbreviationNode} node
* @return {Boolean}
*/
isInline: function(node) {
return node.isTextNode()
|| !node.name()
|| tagName.isInlineLevel(node.name());
},
/**
* Test if passed node is block-level
* @param {AbbreviationNode} node
* @return {Boolean}
*/
isBlock: function(node) {
return this.isSnippet(node) || !this.isInline(node);
},
/**
* Test if given node is a snippet
* @param {AbbreviationNode} node
* @return {Boolean}
*/
isSnippet: function(node) {
return elements.is(node.data('resource'), 'snippet');
},
/**
* This function tests if passed node content contains HTML tags.
* This function is mostly used for output formatting
* @param {AbbreviationNode} node
* @returns {Boolean}
*/
hasTagsInContent: function(node) {
return utils.matchesTag(node.content);
},
/**
* Test if current element contains block-level children
* @param {AbbreviationNode} node
* @return {Boolean}
*/
hasBlockChildren: function(node) {
return (this.hasTagsInContent(node) && this.isBlock(node))
|| node.children.some(function(child) {
return this.isBlock(child);
}, this);
},
/**
* Utility function that inserts content instead of <code>${child}</code>
* variables on <code>text</code>
* @param {String} text Text where child content should be inserted
* @param {String} childContent Content to insert
* @param {Object} options
* @returns {String
*/
insertChildContent: function(text, childContent, options) {
options = utils.extend({
keepVariable: true,
appendIfNoChild: true
}, options || {});
var childVariableReplaced = false;
text = tabStops.replaceVariables(text, function(variable, name, data) {
var output = variable;
if (name == 'child') {
// add correct indentation
output = utils.padString(childContent, utils.getLinePaddingFromPosition(text, data.start));
childVariableReplaced = true;
if (options.keepVariable)
output += variable;
}
return output;
});
if (!childVariableReplaced && options.appendIfNoChild) {
text += childContent;
}
return text;
}
};
});