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

Skip to content

Commit f24af58

Browse files
committed
JS: Extract mapping from HTML node to parent Expression
1 parent 3b666a5 commit f24af58

3 files changed

Lines changed: 23 additions & 8 deletions

File tree

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,15 +1143,15 @@ public Label visit(Property nd, Context c) {
11431143
locationManager.emitNodeLocation(nd, propkey);
11441144
visitAll(nd.getDecorators(), propkey, IdContext.varBind, -1, -1);
11451145
visit(nd.getKey(), propkey, 0, nd.isComputed() ? IdContext.varBind : IdContext.label);
1146-
visit(nd.getValue(), propkey, 1, c.idcontext);
1146+
Label valueLabel = visit(nd.getValue(), propkey, 1, c.idcontext);
11471147
visit(nd.getDefaultValue(), propkey, 2, IdContext.varBind);
11481148
if (nd.isComputed()) trapwriter.addTuple("is_computed", propkey);
11491149
if (nd.isMethod()) trapwriter.addTuple("is_method", propkey);
11501150

11511151
// Extract the value of a property named `template` as HTML, in order to support
11521152
// Angular2 components with an inline template.
11531153
if (!nd.isComputed() && "template".equals(tryGetIdentifierName(nd.getKey()))) {
1154-
extractStringValueAsHtml(nd.getValue());
1154+
extractStringValueAsHtml(nd.getValue(), valueLabel);
11551155
}
11561156

11571157
return propkey;
@@ -1160,7 +1160,7 @@ public Label visit(Property nd, Context c) {
11601160
/**
11611161
* Extracts the string value of <code>expr</code> as an HTML snippet.
11621162
*/
1163-
private void extractStringValueAsHtml(Expression expr) {
1163+
private void extractStringValueAsHtml(Expression expr, Label exprLabel) {
11641164
TextualExtractor textualExtractor = lexicalExtractor.getTextualExtractor();
11651165
if (textualExtractor.isSnippet()) {
11661166
return; // do not create nested snippets
@@ -1185,7 +1185,11 @@ private void extractStringValueAsHtml(Expression expr) {
11851185
getMetrics(),
11861186
vfile.toFile());
11871187
HTMLExtractor html = HTMLExtractor.forEmbeddedHtml(config);
1188-
html.extract(innerTextualExtractor);
1188+
List<Label> rootNodes = html.extractEx(innerTextualExtractor).fst();
1189+
int rootNodeIndex = 0;
1190+
for (Label rootNode : rootNodes) {
1191+
trapwriter.addTuple("xml_element_parent_expression", rootNode, exprLabel, rootNodeIndex++);
1192+
}
11891193
}
11901194

11911195
private String tryGetIdentifierName(Expression e) {

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.semmle.js.extractor;
22

33
import java.io.File;
4+
import java.io.IOException;
45
import java.nio.file.Path;
6+
import java.util.List;
57
import java.util.regex.Matcher;
68
import java.util.regex.Pattern;
79

@@ -180,9 +182,13 @@ public HTMLExtractor(ExtractorConfig config, ExtractorState state) {
180182
public static HTMLExtractor forEmbeddedHtml(ExtractorConfig config) {
181183
return new HTMLExtractor(config, null, true);
182184
}
183-
185+
184186
@Override
185-
public LoCInfo extract(TextualExtractor textualExtractor) {
187+
public LoCInfo extract(TextualExtractor textualExtractor) throws IOException {
188+
return extractEx(textualExtractor).snd();
189+
}
190+
191+
public Pair<List<Label>, LoCInfo> extractEx(TextualExtractor textualExtractor) {
186192
// Angular templates contain attribute names that are not valid HTML/XML, such as [foo], (foo), [(foo)], and *foo.
187193
// Allow a large number of errors in attribute names, so the Jericho parser does not give up.
188194
Attributes.setDefaultMaxErrorCount(100);
@@ -198,9 +204,9 @@ public LoCInfo extract(TextualExtractor textualExtractor) {
198204

199205
extractor.setStartOffset(locationManager.getStartLine() - 1, locationManager.getStartColumn() - 1);
200206

201-
extractor.doit(Option.some(eltHandler));
207+
List<Label> rootNodes = extractor.doit(Option.some(eltHandler));
202208

203-
return locInfo;
209+
return Pair.make(rootNodes, locInfo);
204210
}
205211

206212
/**

javascript/ql/src/semmlecode.javascript.dbscheme

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ toplevel_parent_xml_node(
136136
unique int toplevel: @toplevel ref,
137137
int xmlnode: @xml_node_with_code ref);
138138

139+
xml_element_parent_expression(
140+
unique int xmlnode: @xmlelement ref,
141+
int expression: @expr ref,
142+
int index: int ref);
143+
139144
// statements
140145
#keyset[parent, idx]
141146
stmts (unique int id: @stmt,

0 commit comments

Comments
 (0)