Skip to content

Commit f3e2b0b

Browse files
committed
JS: Avoid using non-existent attribute as parent
1 parent ac62379 commit f3e2b0b

File tree

2 files changed

+45
-41
lines changed

2 files changed

+45
-41
lines changed

javascript/extractor/src/com/semmle/js/extractor/HTMLExtractor.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,18 +89,23 @@ public void handleElement(Element elt, HtmlPopulator.Context context) {
8989
}
9090
} else {
9191
Attributes attributes = elt.getAttributes();
92+
boolean attributesAreExtracted = shouldExtractAttributes(elt);
9293
// attributes can be null for directives
9394
if (attributes != null)
9495
for (Attribute attr : attributes) {
9596
// ignore empty attributes
9697
if (attr.getValue() == null || attr.getValue().isEmpty()) continue;
9798

99+
// If attributes are not extracted we can't use the attribute as the parent node.
100+
// In this case, use the enclosing element as the node.
101+
Segment parentSegment = attributesAreExtracted ? attr : elt;
102+
98103
extractTemplateTags(
99104
textualExtractor,
100105
attr.getSource(),
101106
attr.getBegin(),
102107
attr.getEnd(),
103-
() -> context.getNodeLabel(attr));
108+
() -> context.getNodeLabel(parentSegment));
104109

105110
String source = attr.getValue();
106111
int valueStart = attr.getValueSegment().getBegin();
@@ -113,7 +118,7 @@ public void handleElement(Element elt, HtmlPopulator.Context context) {
113118
source,
114119
valueStart,
115120
false /* isTypeScript */,
116-
context.getNodeLabel(attr));
121+
context.getNodeLabel(parentSegment));
117122
} else if (isAngularTemplateAttributeName(attr.getName())) {
118123
// For an attribute *ngFor="let var of EXPR", start parsing at EXPR
119124
int offset = 0;
@@ -133,7 +138,7 @@ public void handleElement(Element elt, HtmlPopulator.Context context) {
133138
source,
134139
valueStart + offset,
135140
false /* isTypeScript */,
136-
context.getNodeLabel(attr));
141+
context.getNodeLabel(parentSegment));
137142
} else if (source.startsWith("javascript:")) {
138143
source = source.substring(11);
139144
extractSnippet(
@@ -144,7 +149,7 @@ public void handleElement(Element elt, HtmlPopulator.Context context) {
144149
source,
145150
valueStart + 11,
146151
false /* isTypeScript */,
147-
context.getNodeLabel(attr));
152+
context.getNodeLabel(parentSegment));
148153
}
149154
}
150155
}

javascript/extractor/tests/generatedcode/output/trap/attributes.html.trap

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -352,42 +352,41 @@ xmlChars(#20110,"
352352
#20111=@"loc,{#10000},8,47,9,2"
353353
locations_default(#20111,#10000,8,47,9,2)
354354
xmllocations(#20110,#20111)
355-
#20112=*
356-
xmlElements(#20112,"div",#20098,9,#10000)
357-
#20113=@"loc,{#10000},8,5,8,46"
358-
locations_default(#20113,#10000,8,5,8,46)
359-
xmllocations(#20112,#20113)
360-
#20114=*
361-
xmlElements(#20114,"div",#20098,7,#10000)
362-
#20115=@"loc,{#10000},7,5,7,35"
363-
locations_default(#20115,#10000,7,5,7,35)
364-
xmllocations(#20114,#20115)
365-
#20116=*
366-
xmlElements(#20116,"div",#20098,5,#10000)
367-
#20117=@"loc,{#10000},6,5,6,31"
368-
locations_default(#20117,#10000,6,5,6,31)
369-
xmllocations(#20116,#20117)
370-
xmlAttrs(#20042,#20116,"foo","{{#foo}}{{/foo}}/",0,#10000)
371-
#20118=@"loc,{#10000},6,10,6,30"
372-
locations_default(#20118,#10000,6,10,6,30)
373-
xmllocations(#20042,#20118)
374-
#20119=*
375-
xmlElements(#20119,"div",#20098,3,#10000)
376-
#20120=@"loc,{#10000},5,5,5,32"
377-
locations_default(#20120,#10000,5,5,5,32)
378-
xmllocations(#20119,#20120)
379-
xmlAttrs(#20018,#20119,"foo","{{{foo}}}{{/foo}}/",0,#10000)
380-
#20121=@"loc,{#10000},5,10,5,31"
381-
locations_default(#20121,#10000,5,10,5,31)
382-
xmllocations(#20018,#20121)
383-
#20122=*
384-
xmlElements(#20122,"div",#20098,1,#10000)
385-
#20123=@"loc,{#10000},4,5,4,27"
386-
locations_default(#20123,#10000,4,5,4,27)
387-
xmllocations(#20122,#20123)
388-
xmlAttrs(#20002,#20122,"foo","{{foo}}",0,#10000)
389-
#20124=@"loc,{#10000},4,10,4,20"
390-
locations_default(#20124,#10000,4,10,4,20)
391-
xmllocations(#20002,#20124)
355+
xmlElements(#20058,"div",#20098,9,#10000)
356+
#20112=@"loc,{#10000},8,5,8,46"
357+
locations_default(#20112,#10000,8,5,8,46)
358+
xmllocations(#20058,#20112)
359+
#20113=*
360+
xmlElements(#20113,"div",#20098,7,#10000)
361+
#20114=@"loc,{#10000},7,5,7,35"
362+
locations_default(#20114,#10000,7,5,7,35)
363+
xmllocations(#20113,#20114)
364+
#20115=*
365+
xmlElements(#20115,"div",#20098,5,#10000)
366+
#20116=@"loc,{#10000},6,5,6,31"
367+
locations_default(#20116,#10000,6,5,6,31)
368+
xmllocations(#20115,#20116)
369+
xmlAttrs(#20042,#20115,"foo","{{#foo}}{{/foo}}/",0,#10000)
370+
#20117=@"loc,{#10000},6,10,6,30"
371+
locations_default(#20117,#10000,6,10,6,30)
372+
xmllocations(#20042,#20117)
373+
#20118=*
374+
xmlElements(#20118,"div",#20098,3,#10000)
375+
#20119=@"loc,{#10000},5,5,5,32"
376+
locations_default(#20119,#10000,5,5,5,32)
377+
xmllocations(#20118,#20119)
378+
xmlAttrs(#20018,#20118,"foo","{{{foo}}}{{/foo}}/",0,#10000)
379+
#20120=@"loc,{#10000},5,10,5,31"
380+
locations_default(#20120,#10000,5,10,5,31)
381+
xmllocations(#20018,#20120)
382+
#20121=*
383+
xmlElements(#20121,"div",#20098,1,#10000)
384+
#20122=@"loc,{#10000},4,5,4,27"
385+
locations_default(#20122,#10000,4,5,4,27)
386+
xmllocations(#20121,#20122)
387+
xmlAttrs(#20002,#20121,"foo","{{foo}}",0,#10000)
388+
#20123=@"loc,{#10000},4,10,4,20"
389+
locations_default(#20123,#10000,4,10,4,20)
390+
xmllocations(#20002,#20123)
392391
numlines(#10000,10,3,0)
393392
filetype(#10000,"html")

0 commit comments

Comments
 (0)