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 | package Torello.HTML;
import Torello.HTML.helper.AttrRegEx;
import Torello.Java.Additional.ByRef;
import java.util.function.Predicate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;
class RemoveAttributes
{
static TagNode removeAttributes(
final TagNode tn,
final Predicate<String> attrNameTest
)
{
ClosingTagNodeException.check(tn);
if (tn.str.length() == (tn.tok.length() + 2)) return tn;
StringBuilder sb = new StringBuilder();
sb.append(tn.str.substring(0, tn.tok.length() + 1));
String attribsStr = tn.str.substring(tn.tok.length() + 1);
Matcher m = AttrRegEx.KEY_VALUE_REGEX.matcher(attribsStr);
int pos = 0;
ByRef<Boolean> alreadySpaced = new ByRef<>(false);
// Here, the Key-Value (Attribue-Name & Attribute-Value) pairs are iterated. Care is
// taken to ensure that only the names (not the values) are modified.
/*
System.out.println(
"tn.str: " + BCYAN + tn.str + RESET + '\n' +
"RemoveAttributes-Start, attribsStr=" + BYELLOW + attribsStr + RESET
);
*/
while (m.find())
{
// System.out.println("sb: [" + sb.toString() + "]");
//
// Append the Attribute-Name
final String attrName = m.group(2);
booleanAttributeCheckAndAdd
(attribsStr.substring(pos, m.start(2)), attrNameTest, sb, alreadySpaced, false);
if (attrNameTest.test(attrName))
{
sb
.append(attrName)
.append('=')
.append(m.group(3));
alreadySpaced.f = false;
}
pos = m.end();
}
// The "excess" (The stuff at the end of the very-last-match)
booleanAttributeCheckAndAdd
(attribsStr.substring(pos), attrNameTest, sb, alreadySpaced, true);
// Return the new TagNode
// return new TagNode(sb.toString());
return new TagNode(tn.tok, sb.toString());
}
private static final Pattern WS_REGEX = Pattern.compile("\\s+");
private static void booleanAttributeCheckAndAdd(
String s,
final Predicate<String> attrNameTest,
final StringBuilder sb,
final ByRef<Boolean> alreadySpaced,
final boolean atEnd
)
{
/*
System.out.println(
" bACA s=[" + BCYAN + s + RESET + "], " +
"atEnd=" + atEnd + ", " +
"alreadySpaced.f=" + alreadySpaced.f
);
*/
final String end;
if (atEnd)
{
if (s.endsWith("/>"))
{
end = " />";
s = s.substring(0, s.length() - 2);
}
else // s.endsWith(">")
{
end = ">";
s = s.substring(0, s.length() - 1);
}
}
else end = null;
Matcher m = WS_REGEX.matcher(s);
int pos = 0;
while (m.find())
{
String nonWS = s.substring(pos, m.start());
String ws = m.group();
/*
System.out.println(
" #1) sb-INNER: [" + sb.toString() + "], " +
"nonWS=[" + nonWS + "], " +
"ws=[" + ws + "], " +
"alreadySpaced.f=" + alreadySpaced.f
);
*/
if ((nonWS.length() > 0) && attrNameTest.test(nonWS))
{
sb.append(nonWS);
alreadySpaced.f = false;
}
if (! alreadySpaced.f)
{
sb.append(' ');
alreadySpaced.f = true;
}
pos = m.end();
}
String last = s.substring(pos);
/*
System.out.println(
" #2) sb-INNER: [" + sb.toString() + "], " +
"s.substring(pos): [" + BCYAN + last + RESET + "], " +
"alreadySpaced.f: " + alreadySpaced.f
);
*/
if ((last.length() > 0) && attrNameTest.test(last))
{
sb.append(last);
alreadySpaced.f = false;
}
if (atEnd)
{
if (alreadySpaced.f) sb.deleteCharAt(sb.length() - 1);
sb.append(end);
}
}
}
|