-
Notifications
You must be signed in to change notification settings - Fork 9
/
Tag.cfc
243 lines (212 loc) · 7.57 KB
/
Tag.cfc
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
component extends="Statement" {
variables.endTagStartPosition = 0;
variables.startTagEndPosition = 0;
public boolean function isTag() {
return true;
}
public boolean function isFunction() {
return getName() == "cffunction";
}
public void function setEndTagStartPosition(position) {
variables.endTagStartPosition = arguments.position;
}
public void function setStartTagEndPosition(position) {
variables.startTagEndPosition = arguments.position;
}
public function getInnerContentStartPosition() {
return getStartTagEndPosition()+1;
}
public function getStartTagEndPosition() {
return variables.startTagEndPosition;
}
public function getEndTagStartPosition() {
return variables.endTagStartPosition;
}
public boolean function isCustomTag() {
return lCase(left(getName(), 3)) == "cf_";
}
public boolean function couldHaveInnerContent() {
if (isCustomTag()) {
//custom tag assume true
return true;
}
return listFindNoCase("cfoutput,cfmail,cfsavecontent,cfquery,cfdocument,cfpdf,cfhtmltopdf,cfhtmltopdfitem,cfscript,cfform,cfloop,cfif,cfelse,cfelseif,cftry,cfcatch,cffinally,cfstoredproc,cfswitch,cfcase,cfdefaultcase,cfcomponent,cffunction,cfchart,cfclient,cfdiv,cfdocumentitem,cfdocumentsection,cfformgroup,cfgrid,cfhttp,cfimap,cfinterface,cfinvoke,cflayout,cflock,cflogin,cfmap,cfmenu,cfmodule,cfpod,cfpresentation,cfthread,cfreport,cfsilent,cftable,cftextarea,cftimer,cftransaction,cftree,cfzip,cfwindow,cfxml", getName());
}
public string function getAttributeContent(stripTrailingSlash=false) {
if (!structKeyExists(variables, "attributeContent")) {
if (getStartTagEndPosition() == 0 || getStartPosition() == 0 || getStartPosition() >= getStartTagEndPosition()) {
throw(message="Unable to getAttributeContent for tag: #getName()# startPosition:#getStartPosition()# startTagEndPosition:#getStartTagEndPosition()#");
} else if (!hasAttributes()) {
//tag with no attributes determined by length, skip mid operation
variables.attributeContent = "";
} else {
variables.attributeContent = mid(getFile().getFileContent(), getStartPosition()+1, getStartTagEndPosition()-getStartPosition()-1);
try {
variables.attributeContent = reReplaceNoCase(variables.attributeContent, "^[[:space:]]*" & regexEscape(getName()), "");
} catch (any e) {
throw(message=e.message & "name:#getName()# ac:#variables.attributeContent#");
}
}
}
if (arguments.stripTrailingSlash) {
variables.attributeContent = reReplace(variables.attributeContent, "\/[[:space:]]*$", "", "ALL");
}
return variables.attributeContent;
}
public string function regexEscape(str) {
return reReplace(arguments.str, "([()?.\[\]*]+)", "\\\1", "ALL");
}
public boolean function hasAttributes() {
return getStartTagEndPosition()-getStartPosition() != len(getName()) + 1;
}
public boolean function hasInnerContent() {
return (getStartTagEndPosition()+1 < getEndTagStartPosition());
}
public boolean function isInnerContentEvaluated() {
return listFindNoCase("cfoutput,cfquery,cfmail", getName());
}
public string function getInnerContent() {
if (!hasInnerContent()) {
return "";
} else {
return mid(getFile().getFileContent(), getStartTagEndPosition()+1, getEndTagStartPosition()-getStartTagEndPosition()-1);
}
}
public array function getExpressions() {
var expr = "";
var e = "";
if ( structKeyExists(variables, "expressions") ) {
return variables.expressions;
} else {
variables.expressions = arrayNew(1);
}
if ( listFindNoCase("cfset,cfif,cfelseif,cfreturn", getName()) ) {
arrayAppend(variables.expressions, {"expression"=getAttributeContent(), "position"=getStartPosition()});
} else {
// attributes
if ( hasAttributes() && (NOT isInnerContentEvaluated() || !hasInnerContent()) ) {
getAttributes();
return variables.attributeExpressions;
} else if ( isInnerContentEvaluated() && hasInnerContent() ) {
if ( hasAttributes() ) {
getAttributes();
if ( arrayLen(variables.attributeExpressions) ) {
arrayAppend(variables.expressions, variables.attributeExpressions, true);
}
}
expr = getExpressionsFromString(getStrippedInnerContent(stripComments=true, stripCFMLTags=true));
if ( arrayLen(expr) ) {
for ( e in expr ) {
e.position = e.position + getInnerContentStartPosition() - 1;
arrayAppend(variables.expressions, e);
}
}
}
}
return variables.expressions;
}
string function getStrippedInnerContent(boolean stripComments="true", boolean stripCFMLTags="false") {
var l = StructNew();
var innerContent = getInnerContent();
var cacheKey = "strippedInnerContent" & ((arguments.stripComments) ? "Comments" : "") & ((arguments.stripCFMLTags) ? "Tags" : "");
if (structKeyExists(variables, cacheKey)) {
return variables[cacheKey];
}
if ( arguments.stripComments && hasInnerContent() ) {
if ( !StructKeyExists(variables, cacheKey) ) {
l.found = Find("<"&"!---", innerContent);
if ( l.found ) {
l.content = "";
l.inComment = 0;
l.lastCommentStart = 0;
for ( l.i=1 ; l.i<=Len(innerContent) ; l.i++ ) {
l.c = Mid(innerContent, l.i, 1);
if ( l.c == "<" ) {
if ( Mid(innerContent, l.i, 5) == "<!---" ) {
l.inComment = l.inComment + 1;
l.content = l.content & " ";
} else if ( l.inComment == 0 ) {
l.content = l.content & "<";
} else {
l.content = l.content & " ";
}
} else if ( l.c == ">" && l.inComment > 0 && l.i >= 4 ) {
if ( Mid(innerContent, l.i-3, 4) == "--->" ) {
l.inComment = l.inComment - 1;
l.content = l.content & " ";
} else if ( l.inComment == 0 ) {
l.content = l.content & ">";
} else {
l.content = l.content & " ";
}
} else if ( l.c == Chr(13) ) {
l.content = l.content & Chr(10);
} else if ( l.c == Chr(10) ) {
l.content = l.content & Chr(10);
} else if ( l.inComment > 0 ) {
l.content = l.content & " ";
} else {
// not in comment
l.content = l.content & l.c;
}
}
innerContent = l.content;
}
}
if ( arguments.stripCFMLTags ) {
l.stripResult = innerContent;
for ( l.match in reMatchNoCase("</?cf[^>]+>", l.stripResult) ) {
l.replace = repeatString(" ", len(l.match));
l.stripResult = replace(l.stripResult, l.match, l.replace, "all");
}
innerContent = l.stripResult;
}
}
variables[cacheKey] = innerContent;
return innerContent;
}
public array function getVariablesWritten() {
var vars = ArrayNew(1);
var attrs = getAttributes();
switch ( LCase(getName()) ) {
case "cfset":
if ( getAttributeContent() contains "=" ) {
ArrayAppend(vars, Trim(ListFirst(getAttributeContent(), "=")));
}
break;
case "cfquery":
if ( StructKeyExists(attrs, "name") ) {
ArrayAppend(vars, attrs.name);
}
if ( StructKeyExists(attrs, "result") ) {
ArrayAppend(vars, attrs.result);
}
break;
case "cfhttp":
if ( StructKeyExists(attrs, "result") ) {
ArrayAppend(vars, attrs.result);
} else {
ArrayAppend(vars, "cfhttp");
}
break;
case "cfprocparam":
if ( StructKeyExists(attrs, "variable") && StructKeyExists(attrs, "type") && attrs.type == "out" ) {
ArrayAppend(vars, attrs.variable);
}
break;
case "cfparam":
if ( StructKeyExists(attrs, "name") ) {
ArrayAppend(vars, attrs.name);
}
break;
}
return vars;
}
/* for debugging */
function getVariables() {
var rtn = super.getVariables();
rtn.attributes = getAttributes();
rtn.attributeContent = getAttributeContent();
return rtn;
}
}