Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 81b86d9

Browse files
author
Max Schaefer
committed
JavaScript: Skip XML processing instructions in E4X content.
1 parent be67d51 commit 81b86d9

3 files changed

Lines changed: 63 additions & 32 deletions

File tree

javascript/extractor/src/com/semmle/jcorn/CustomParser.java

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -545,22 +545,52 @@ protected XMLAttributeSelector decoratorToAttributeSelector(Decorator d) {
545545
}
546546

547547
@Override
548-
protected Either<Integer, Token> jsx_readChunk(StringBuilder out, int chunkStart, int ch) {
549-
// skip HTML comments (which are allowed in E4X, but not in JSX)
550-
if (this.options.e4x() && ch == '<' && charAt(this.pos+1) == '!' &&
551-
charAt(this.pos+2) == '-' && charAt(this.pos+3) == '-') {
552-
out.append(inputSubstring(chunkStart, this.pos));
553-
this.pos += 4;
554-
while (this.pos+2 < this.input.length()) {
555-
if (charAt(this.pos) == '-' && charAt(this.pos+1) == '-' && charAt(this.pos+2) == '>') {
556-
this.pos += 3;
548+
protected Token readToken(int code) {
549+
// skip XML processing instructions (which are allowed in E4X, but not in JSX)
550+
if (this.options.e4x()) {
551+
while (code == '<') {
552+
if (charAt(this.pos+1) == '?') {
553+
this.pos += 2;
554+
jsx_readUntil("?>");
555+
} else {
557556
break;
558557
}
559-
++this.pos;
558+
this.skipSpace();
559+
code = this.fullCharCodeAtPos();
560+
}
561+
}
562+
return super.readToken(code);
563+
}
564+
565+
@Override
566+
protected Either<Integer, Token> jsx_readChunk(StringBuilder out, int chunkStart, int ch) {
567+
// skip XML comments and processing instructions (which are allowed in E4X, but not in JSX)
568+
if (this.options.e4x() && ch == '<') {
569+
if (inputSubstring(this.pos+1, this.pos+4).equals("!--")) {
570+
out.append(inputSubstring(chunkStart, this.pos));
571+
this.pos += 4;
572+
jsx_readUntil("-->");
573+
return Either.left(this.pos);
574+
} else if (charAt(this.pos+1) == '?') {
575+
out.append(inputSubstring(chunkStart, this.pos));
576+
this.pos += 2;
577+
jsx_readUntil("?>");
578+
return Either.left(this.pos);
560579
}
561-
return Either.left(this.pos);
562580
}
563581

564582
return super.jsx_readChunk(out, chunkStart, ch);
565583
}
584+
585+
private void jsx_readUntil(String terminator) {
586+
char fst = terminator.charAt(0);
587+
while (this.pos+terminator.length() <= this.input.length()) {
588+
if (charAt(this.pos) == fst &&
589+
inputSubstring(this.pos, this.pos+terminator.length()).equals(terminator)) {
590+
this.pos += terminator.length();
591+
break;
592+
}
593+
++this.pos;
594+
}
595+
}
566596
}

javascript/extractor/tests/e4x/input/tst.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ message.soap::Body;
77
items.@[f()];
88
message.soap::[g()];
99

10-
var e = <elt>
10+
var e = <?xml version="1.0" encoding="UTF-8"?> <elt>
1111
<!-- comment -->
12-
</elt>;
12+
</elt>;

javascript/extractor/tests/e4x/output/trap/tst.js.trap

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ lines(#20018,#20001,"","
6464
locations_default(#20019,#10000,9,1,9,0)
6565
hasLocation(#20018,#20019)
6666
#20020=*
67-
lines(#20020,#20001,"var e = <elt>","
67+
lines(#20020,#20001,"var e = <?xml version=""1.0"" encoding=""UTF-8""?> <elt>","
6868
")
69-
#20021=@"loc,{#10000},10,1,10,13"
70-
locations_default(#20021,#10000,10,1,10,13)
69+
#20021=@"loc,{#10000},10,1,10,52"
70+
locations_default(#20021,#10000,10,1,10,52)
7171
hasLocation(#20020,#20021)
7272
#20022=*
7373
lines(#20022,#20001," <!-- comment -->","
@@ -77,7 +77,8 @@ locations_default(#20023,#10000,11,1,11,18)
7777
hasLocation(#20022,#20023)
7878
indentation(#10000,11," ",2)
7979
#20024=*
80-
lines(#20024,#20001," </elt>;","")
80+
lines(#20024,#20001," </elt>;","
81+
")
8182
#20025=@"loc,{#10000},12,1,12,9"
8283
locations_default(#20025,#10000,12,1,12,9)
8384
hasLocation(#20024,#20025)
@@ -424,26 +425,26 @@ tokeninfo(#20160,8,#20001,67,"=")
424425
locations_default(#20161,#10000,10,7,10,7)
425426
hasLocation(#20160,#20161)
426427
#20162=*
427-
tokeninfo(#20162,8,#20001,68,"<")
428-
#20163=@"loc,{#10000},10,9,10,9"
429-
locations_default(#20163,#10000,10,9,10,9)
428+
tokeninfo(#20162,8,#20001,68,"<?xml version=""1.0"" encoding=""UTF-8""?> <")
429+
#20163=@"loc,{#10000},10,9,10,48"
430+
locations_default(#20163,#10000,10,9,10,48)
430431
hasLocation(#20162,#20163)
431432
#20164=*
432433
tokeninfo(#20164,6,#20001,69,"elt")
433-
#20165=@"loc,{#10000},10,10,10,12"
434-
locations_default(#20165,#10000,10,10,10,12)
434+
#20165=@"loc,{#10000},10,49,10,51"
435+
locations_default(#20165,#10000,10,49,10,51)
435436
hasLocation(#20164,#20165)
436437
#20166=*
437438
tokeninfo(#20166,8,#20001,70,">")
438-
#20167=@"loc,{#10000},10,13,10,13"
439-
locations_default(#20167,#10000,10,13,10,13)
439+
#20167=@"loc,{#10000},10,52,10,52"
440+
locations_default(#20167,#10000,10,52,10,52)
440441
hasLocation(#20166,#20167)
441442
#20168=*
442443
tokeninfo(#20168,4,#20001,71,"
443444
<!-- comment -->
444445
")
445-
#20169=@"loc,{#10000},10,14,12,2"
446-
locations_default(#20169,#10000,10,14,12,2)
446+
#20169=@"loc,{#10000},10,53,12,2"
447+
locations_default(#20169,#10000,10,53,12,2)
447448
hasLocation(#20168,#20169)
448449
#20170=*
449450
tokeninfo(#20170,8,#20001,72,"<")
@@ -472,12 +473,12 @@ locations_default(#20179,#10000,12,9,12,9)
472473
hasLocation(#20178,#20179)
473474
#20180=*
474475
tokeninfo(#20180,0,#20001,77,"")
475-
#20181=@"loc,{#10000},12,10,12,9"
476-
locations_default(#20181,#10000,12,10,12,9)
476+
#20181=@"loc,{#10000},13,1,13,0"
477+
locations_default(#20181,#10000,13,1,13,0)
477478
hasLocation(#20180,#20181)
478479
toplevels(#20001,0)
479-
#20182=@"loc,{#10000},1,1,12,9"
480-
locations_default(#20182,#10000,1,1,12,9)
480+
#20182=@"loc,{#10000},1,1,13,0"
481+
locations_default(#20182,#10000,1,1,13,0)
481482
hasLocation(#20001,#20182)
482483
#20183=@"var;{e};{#20000}"
483484
variables(#20183,"e",#20000)
@@ -855,7 +856,7 @@ locations_default(#20271,#10000,10,1,12,9)
855856
hasLocation(#20270,#20271)
856857
stmtContainers(#20270,#20001)
857858
#20272=*
858-
exprs(#20272,64,#20270,0,"e = <el ... </elt>")
859+
exprs(#20272,64,#20270,0,"e = <?x ... </elt>")
859860
#20273=@"loc,{#10000},10,5,12,8"
860861
locations_default(#20273,#10000,10,5,12,8)
861862
hasLocation(#20272,#20273)
@@ -869,7 +870,7 @@ exprContainers(#20274,#20001)
869870
literals("e","e",#20274)
870871
decl(#20274,#20183)
871872
#20275=*
872-
exprs(#20275,89,#20272,1,"<elt>\n ... </elt>")
873+
exprs(#20275,89,#20272,1,"<?xml v ... </elt>")
873874
#20276=@"loc,{#10000},10,9,12,8"
874875
locations_default(#20276,#10000,10,9,12,8)
875876
hasLocation(#20275,#20276)

0 commit comments

Comments
 (0)