-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathAmbiguousIdAttribute.ql
More file actions
51 lines (47 loc) · 1.78 KB
/
AmbiguousIdAttribute.ql
File metadata and controls
51 lines (47 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
* @name Ambiguous HTML id attribute
* @description If an HTML document contains two elements with the
* same id attribute, it may be interpreted differently
* by different browsers.
* @kind problem
* @problem.severity warning
* @id js/duplicate-html-id
* @tags maintainability
* correctness
* @precision very-high
*/
import javascript
/**
* Holds if `attr` is an id attribute with value `id` of a DOM element
* under document `root` at the given `line` and `column`.
*
* Furthermore, the id is required to be valid, and not look like a template.
*/
predicate idAt(DOM::AttributeDefinition attr, string id, DOM::ElementDefinition root, int line, int column) {
exists (DOM::ElementDefinition elt |
attr = elt.getAttributeByName("id") |
id = attr.getStringValue() and
root = elt.getRoot() and
elt.getLocation().hasLocationInfo(_, line, column, _, _) and
not (
// exclude invalid ids (reported by another query)
DOM::isInvalidHtmlIdAttributeValue(attr, _) or
// exclude attribute values that look like they might be templated
attr.mayHaveTemplateValue()
)
)
}
/**
* Holds if attributes `earlier` and `later` are id attributes with the same value in
* the same document, and `earlier` appears textually before `later`.
*/
predicate sameId(DOM::AttributeDefinition earlier, DOM::AttributeDefinition later) {
exists (string id, DOM::ElementDefinition root, int l1, int c1, int l2, int c2 |
idAt(earlier, id, root, l1, c1) and idAt(later, id, root, l2, c2) |
l1 < l2 or
l1 = l2 and c1 < c2
)
}
from DOM::AttributeDefinition earlier, DOM::AttributeDefinition later
where sameId(earlier, later) and not sameId(_, earlier)
select earlier, "This element has the same id as $@.", later, "another element"