0

I know HTML is experimental in Semgrep, but I expect it to work on very simple matches (Bash is experimental too, and it works well enough already).

How can I match any two elements with the same 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>

Attempt

Try on playground

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">

Error

Attempt gives only one match, but expected 4. Why does this happen?

Other patterns

  • Just <$TAGA id="$X"></$TAGA> matches any tag with ID as expected
  • <$TAGA id="$X"></$TAGA><$TAGB id="$X"></$TAGB> matches nothing (unexpected, should match anything with two consecutive same IDs)
    • Even if ... is used between
fejyesynb
  • 172
  • 1
  • 5

0 Answers0