Skip to content

Commit 9b8bf96

Browse files
authored
Merge pull request #718 from xiemaisi/js/ambiguous-id-attr-alert-loc
Approved by asger-semmle
2 parents 4348de3 + 0a2df6c commit 9b8bf96

File tree

4 files changed

+15
-11
lines changed

4 files changed

+15
-11
lines changed

javascript/ql/src/DOM/AmbiguousIdAttribute.ql

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
import javascript
1515

1616
/**
17-
* Holds if `elt` defines a DOM element with the given `id`
17+
* Holds if `attr` is an id attribute with value `id` of a DOM element
1818
* under document `root` at the given `line` and `column`.
1919
*
2020
* Furthermore, the id is required to be valid, and not look like a template.
2121
*/
22-
predicate elementAt(DOM::ElementDefinition elt, string id, DOM::ElementDefinition root, int line, int column) {
23-
exists (DOM::AttributeDefinition attr |
22+
predicate idAt(DOM::AttributeDefinition attr, string id, DOM::ElementDefinition root, int line, int column) {
23+
exists (DOM::ElementDefinition elt |
2424
attr = elt.getAttributeByName("id") |
2525
id = attr.getStringValue() and
2626
root = elt.getRoot() and
@@ -35,17 +35,17 @@ predicate elementAt(DOM::ElementDefinition elt, string id, DOM::ElementDefinitio
3535
}
3636

3737
/**
38-
* Holds if elements `earlier` and `later` have the same id and belong
39-
* to the same document, and `earlier` appears textually before `later`.
38+
* Holds if attributes `earlier` and `later` are id attributes with the same value in
39+
* the same document, and `earlier` appears textually before `later`.
4040
*/
41-
predicate sameId(DOM::ElementDefinition earlier, DOM::ElementDefinition later) {
41+
predicate sameId(DOM::AttributeDefinition earlier, DOM::AttributeDefinition later) {
4242
exists (string id, DOM::ElementDefinition root, int l1, int c1, int l2, int c2 |
43-
elementAt(earlier, id, root, l1, c1) and elementAt(later, id, root, l2, c2) |
43+
idAt(earlier, id, root, l1, c1) and idAt(later, id, root, l2, c2) |
4444
l1 < l2 or
4545
l1 = l2 and c1 < c2
4646
)
4747
}
4848

49-
from DOM::ElementDefinition earlier, DOM::ElementDefinition later
49+
from DOM::AttributeDefinition earlier, DOM::AttributeDefinition later
5050
where sameId(earlier, later) and not sameId(_, earlier)
5151
select earlier, "This element has the same id as $@.", later, "another element"
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
| AmbiguousIdAttribute.html:4:1:4:29 | <li>...</> | This element has the same id as $@. | AmbiguousIdAttribute.html:5:1:5:30 | <li>...</> | another element |
2-
| AmbiguousIdAttribute_fragment.html:2:3:3:2 | <li>...</> | This element has the same id as $@. | AmbiguousIdAttribute_fragment.html:3:3:3:32 | <li>...</> | another element |
3-
| tst.js:22:17:22:40 | <div id ... ></div> | This element has the same id as $@. | tst.js:22:41:22:64 | <div id ... ></div> | another element |
1+
| AmbiguousIdAttribute.html:4:5:4:14 | id=first | This element has the same id as $@. | AmbiguousIdAttribute.html:5:5:5:14 | id=first | another element |
2+
| AmbiguousIdAttribute_fragment.html:2:7:2:16 | id=first | This element has the same id as $@. | AmbiguousIdAttribute_fragment.html:3:7:3:16 | id=first | another element |
3+
| tst.js:22:22:22:33 | id="theDiff" | This element has the same id as $@. | tst.js:22:46:22:57 | id="theDiff" | another element |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
| DuplicateAttributes.html:1:4:1:28 | href=https://semmle.com | This attribute is duplicated $@. | DuplicateAttributes.html:1:30:1:54 | href=https://semmle.com | here |
22
| tst.js:9:4:9:28 | href="h ... le.com" | This attribute is duplicated $@. | tst.js:9:30:9:54 | href="h ... le.com" | here |
3+
| tst.js:25:17:25:28 | id="theDiff" | This attribute is duplicated $@. | tst.js:25:30:25:41 | id="theDiff" | here |

javascript/ql/test/query-tests/DOM/HTML/tst.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,6 @@ var div2 = <div id="theDiff"></div>;
2020

2121
// not OK
2222
var div3 = <div><div id="theDiff"></div><div id="theDiff"></div></div>;
23+
24+
// not OK
25+
var div4 = <div id="theDiff" id="theDiff"></div>;

0 commit comments

Comments
 (0)