我知道 HTML 在 Semgrep 中是实验性的,但我希望它能在非常简单的匹配上工作(Bash 也是实验性的,它已经足够好了)。
如何匹配具有相同 ID 的任意两个元素?
<!-- this should match since both have same IDs -->
<div id="a"></div>
<div id="a"></div>
<!-- this should match since both have same IDs, even though spacing is different (valid HTML) -->
<div id="b"></div>
<div id = "b"></div>
<!-- this should match since both have same IDs, even though different tags -->
<div id="c"></div>
<div>Something in between should still work</div>
<span id="c"></div>
<!-- self-closing tags should match too -->
<link id="d" rel="stylesheet" href="styles.css">
<span id="d"></span>
rules:
- id: dupe_id
message: duplicate IDs not allowed
languages:
- html
severity: ERROR
patterns:
- pattern-either:
# Both not self-closing
- pattern: <$TAGA id="$X"></$TAGA><$TAGB id="$X"></$TAGB>
# First self-closing
- pattern: <$TAGA id="$X"><$TAGB id="$X"></$TAGB>
# Second self-closing
- pattern: <$TAGB id="$X"></$TAGB><$TAGA id="$X">
尝试只给出了一场比赛,但预期是 4.
<$TAGA id="$X"></$TAGA>
与预期的 ID 匹配任何标签<$TAGA id="$X"></$TAGA><$TAGB id="$X"></$TAGB>
什么都不匹配(意外,应该匹配任何具有两个连续相同 ID 的东西)
...
用在