Skip to content

Commit 278b3c2

Browse files
committed
Avoid StringIndexOutOfBoundsException for angular expression. See
#129
1 parent b3b8700 commit 278b3c2

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

org.eclipse.angularjs.core.tests/src/org/eclipse/angularjs/core/utils/AngularRegionUtilsTest.java

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,51 @@
1111
package org.eclipse.angularjs.core.utils;
1212

1313
import org.eclipse.wst.xml.core.internal.regions.DOMRegionContext;
14+
import org.junit.Assert;
1415
import org.junit.Test;
1516

1617
public class AngularRegionUtilsTest {
1718

1819
@Test
19-
public void test() {
20+
public void offsetBeforeStartEL() {
21+
AngularELRegion region = AngularRegionUtils.getAngularELRegion(
22+
DOMRegionContext.XML_CONTENT, "{{todo.text + to}}", 588, 588,
23+
"{{", "}}");
24+
Assert.assertNull(region);
25+
}
26+
27+
@Test
28+
public void offsetAfterStartEL() {
29+
AngularELRegion region = AngularRegionUtils.getAngularELRegion(
30+
DOMRegionContext.XML_CONTENT, "{{todo.text + to}}", 588, 590,
31+
"{{", "}}");
32+
Assert.assertNotNull(region);
33+
Assert.assertEquals("todo.text + to", region.getExpression());
34+
Assert.assertEquals(0, region.getExpressionOffset());
35+
}
36+
37+
@Test
38+
public void offsetBeforeEndEL() {
39+
AngularELRegion region = AngularRegionUtils.getAngularELRegion(
40+
DOMRegionContext.XML_CONTENT, "{{todo.text + to}}", 588, 603,
41+
"{{", "}}");
42+
Assert.assertNull(region);
43+
}
44+
45+
@Test
46+
public void offsetAfterEndEL() {
2047
AngularELRegion region = AngularRegionUtils.getAngularELRegion(
2148
DOMRegionContext.XML_CONTENT, "{{todo.text + to}}", 588, 604,
2249
"{{", "}}");
50+
Assert.assertNotNull(region);
51+
Assert.assertEquals("todo.text + to", region.getExpression());
52+
Assert.assertEquals(14, region.getExpressionOffset());
53+
}
54+
55+
@Test
56+
public void test2() {
57+
AngularELRegion region = AngularRegionUtils.getAngularELRegion(
58+
DOMRegionContext.XML_CONTENT, "{{", 588, 604, "{{", "}}");
59+
Assert.assertNull(region);
2360
}
2461
}

org.eclipse.angularjs.core/src/org/eclipse/angularjs/core/utils/AngularRegionUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public static AngularELRegion getAngularELRegion(String regionType,
5757
String regionText, int regionStartOffset, int documentPosition,
5858
String startSymbol, String endSymbol) {
5959
int startOffset = documentPosition - regionStartOffset;
60-
if (startOffset < 0) {
60+
if (startOffset < 0 || startOffset > regionText.length()) {
6161
return null;
6262
}
6363
if (regionType == DOMRegionContext.XML_CONTENT

0 commit comments

Comments
 (0)