Menu

#166 Bug with duplicate attribute

closed
None
2019-04-15
2019-02-21
z1ad
No

Some unexpected behaviour occurs if an attribute is specified more than once.

$html = str_get_html('<div id="test" style="display:none;" style="margin-top:5px;"></div>');
print_r($html->find("#test")[0]->getAllAttributes());

Array
(
    [id] => test
    [style] => display:none;
    ["margin-top:5px;"] => 1
)

Its even more unexpected if there is a space after the colon inside the style tag.

$html = str_get_html('<div id="test" style="display:none;" style="margin-top: 5px;"></div>');
print_r($html->find("#test")[0]->getAllAttributes());

Array
(
    [id] => test
    [style] => display:none;
    ["margin-top:] => 1
    [5px;"] => 1
)

Some info: https://stackoverflow.com/questions/26341507/can-an-html-element-have-the-same-attribute-twice
It seems browser usually drop the duplicate attributes so it might be worth doing the same here?

Discussion

  • z1ad

    z1ad - 2019-02-21

    Edit: I am using latest version 1.8.1

     
  • z1ad

    z1ad - 2019-02-21

    Another scenario where it can overwrite an unrelated attribute

    $html = str_get_html('<div id="test" duplicate="1" tag="original" duplicate=tag></div>');
    print_r($html->find("#test")[0]->getAllAttributes());
    
    Array
    (
        [id] => test
        [duplicate] => 1
        [tag] => 1 // should be "original"
    )
    
     
  • LogMANOriginal

    LogMANOriginal - 2019-03-09

    Thanks for reporting this issue. You are right, duplicate attributes are only partially skipped (based on the attribute name) which causes the next character being parsed as if it was a new attribute. I've just pushed a fix in [a4b54c] which parses the entire attribute before dropping it. Let me know if it works for you.

     
    👍
    1

    Related

    Commit: [a4b54c]

  • LogMANOriginal

    LogMANOriginal - 2019-03-09
    • assigned_to: LogMANOriginal
     
  • z1ad

    z1ad - 2019-04-14

    Hello, just wanted to confirm the fix has resolved the issue. Thank you!

     
    • LogMANOriginal

      LogMANOriginal - 2019-04-15

      Thanks for the feedback!

       
  • LogMANOriginal

    LogMANOriginal - 2019-04-15
    • status: open --> closed
     

Log in to post a comment.