1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.velocity.tools; |
21 |
|
|
22 |
|
import org.apache.commons.lang3.StringUtils; |
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
@version |
31 |
|
@since |
32 |
|
@since |
33 |
|
@since |
34 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (52) |
Complexity: 18 |
Complexity Density: 0.62 |
|
35 |
|
public class CSSIdentifierSerializer |
36 |
|
{ |
37 |
|
private static final int[][] NO_ESCAPE_RANGES = new int[][] {new int[] {0x0030, 0x0039}, |
38 |
|
new int[] {0x0041, 0x005A}, new int[] {0x0061, 0x007A}}; |
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
@param |
50 |
|
@return |
51 |
|
@throws |
52 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (30) |
Complexity: 9 |
Complexity Density: 0.5 |
|
53 |
54 |
public String serialize(String identifier)... |
54 |
|
{ |
55 |
54 |
if (StringUtils.isEmpty(identifier)) { |
56 |
2 |
return identifier; |
57 |
|
} |
58 |
|
|
59 |
52 |
int length = identifier.length(); |
60 |
52 |
int index = -1; |
61 |
52 |
StringBuilder result = new StringBuilder(); |
62 |
52 |
int firstCodeUnit = identifier.charAt(0); |
63 |
|
|
64 |
301 |
while (++index < length) { |
65 |
254 |
int codeUnit = identifier.charAt(index); |
66 |
|
|
67 |
|
|
68 |
|
|
69 |
254 |
if (codeUnit == 0x0000) { |
70 |
5 |
throw new IllegalArgumentException("Invalid character: the input contains U+0000."); |
71 |
|
|
72 |
249 |
} else if (shouldEscapeAsCodePoint(codeUnit, index, firstCodeUnit)) { |
73 |
|
|
74 |
25 |
result.append('\\').append(Integer.toHexString(codeUnit)).append(' '); |
75 |
|
|
76 |
224 |
} else if (index == 0 && length == 1 && codeUnit == 0x002D) { |
77 |
|
|
78 |
|
|
79 |
1 |
result.append('\\').append(identifier.charAt(index)); |
80 |
|
|
81 |
223 |
} else if (shouldNotEscape(codeUnit)) { |
82 |
|
|
83 |
201 |
result.append(identifier.charAt(index)); |
84 |
|
|
85 |
|
} else { |
86 |
|
|
87 |
|
|
88 |
22 |
result.append('\\').append(identifier.charAt(index)); |
89 |
|
} |
90 |
|
} |
91 |
|
|
92 |
47 |
return result.toString(); |
93 |
|
} |
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 5 |
Complexity Density: 1 |
|
98 |
249 |
private boolean shouldEscapeAsCodePoint(int codeUnit, int index, int firstCodeUnit)... |
99 |
|
{ |
100 |
|
|
101 |
249 |
if (isInRange(codeUnit, 0x0001, 0x001F) || codeUnit == 0x007F) { |
102 |
5 |
return true; |
103 |
|
|
104 |
244 |
} else if (index == 0 && isInRange(codeUnit, 0x0030, 0x0039)) { |
105 |
|
|
106 |
10 |
return true; |
107 |
|
|
108 |
|
} else { |
109 |
|
|
110 |
|
|
111 |
234 |
return index == 1 && isInRange(codeUnit, 0x0030, 0x0039) && firstCodeUnit == 0x002D; |
112 |
|
} |
113 |
|
} |
114 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
115 |
223 |
private boolean shouldNotEscape(int codeUnit)... |
116 |
|
{ |
117 |
|
|
118 |
|
|
119 |
|
|
120 |
223 |
return codeUnit >= 0x0080 || codeUnit == 0x002D || codeUnit == 0x005F || isInRanges(codeUnit, NO_ESCAPE_RANGES); |
121 |
|
} |
122 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
123 |
769 |
private boolean isInRange(int codeUnit, int start, int end)... |
124 |
|
{ |
125 |
769 |
return codeUnit >= start && codeUnit <= end; |
126 |
|
} |
127 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
128 |
165 |
private boolean isInRanges(int codeUnit, int[][] ranges)... |
129 |
|
{ |
130 |
165 |
for (int[] range : ranges) { |
131 |
429 |
if (isInRange(codeUnit, range[0], range[1])) { |
132 |
143 |
return true; |
133 |
|
} |
134 |
|
} |
135 |
22 |
return false; |
136 |
|
} |
137 |
|
} |