Closed Bug 1757764 Opened 3 years ago Closed 3 years ago

innerHTML assigns a <p>with inner tags incorrectly

Categories

(Core :: DOM: Core & HTML, defect)

Firefox 97
defect

Tracking

()

RESOLVED INVALID

People

(Reporter: g.maslov, Unassigned)

Details

User Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:97.0) Gecko/20100101 Firefox/97.0

Steps to reproduce:

In the firefox developer console

let myshit=document.createElement('p')

let somestr="<p> <span class="aaa"></span> <span class="bbb"><div>uuuu</div><div>iiiiii<br></div></span> <span class="ccc"></span> </p>"

myshit.innerHTML=somestr
"<p> <span class="aaa"></span> <span class="bbb"><div>uuuu</div><div>iiiiii<br></div></span> <span class="ccc"></span> </p>"

myshit.innerHTML
"<p> <span class="aaa"></span> <span class="bbb"></span></p><div>uuuu</div><div>iiiiii<br></div> <span class="ccc"></span> <p></p>"

Actual results:

Somehow <p> is screwed up with innerHTML

"<p> <span class="aaa"></span> <span class="bbb"></span></p><div>uuuu</div><div>iiiiii<br></div> <span class="ccc"></span> <p></p>"

Expected results:

"<p> <span class="aaa"></span> <span class="bbb"><div>uuuu</div><div>iiiiii<br></div></span> <span class="ccc"></span> </p>"

Component: Untriaged → DOM: Core & HTML
Product: Firefox → Core

Could you please provide a test case in order to reproduce the issue? I'm not sure how could I use the one from the description in the developer console.
Thanks.

Flags: needinfo?(g.maslov)

HI, it's pretty basic

  1. Use the provided test string in a javascript or in the console.

teststring = '<p> <span class="aaa"></span> <span class="bbb"><div>uuuu</div><div>iiiiii<br></div></span> <span class="ccc"></span> </p>'

  1. the following code and the assertion fails

    myElement = document.createElement('p');
    myElement.innerHtml = teststing;
    console.assert(myElement.innferHTML == teststring, "found a bug:"+myElement.innerHtml);

In particular you can notice that the <p> tag is inserted incorrectly twice

The same example with a div tag works

teststring = '<div> <span class="aaa"></span> <span class="bbb"><div>uuuu</div><div>iiiiii<br></div></span> <span class="ccc"></span> </div>'

Flags: needinfo?(g.maslov)
Flags: needinfo?(hsivonen)

This also happens in Chromium, it's how the html parser works, it closes <p>s when opening other blocks like <div>. I think this is invalid.

This is from the <p> spec

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/p

The start tag is required. The end tag may be omitted if the <p> element is immediately followed by an <address>, <article>, <aside>, <blockquote>, <div>, <dl>, <fieldset>, <footer>, <form>, <h1>, <h2>, <h3>, <h4>, <h5>, <h6>, <header>, <hr>, <menu>, <nav>, <ol>, <pre>, <section>, <table>, <ul> or another <p> element, or if there is no more content in the parent element and the parent element is not an <a> element.

<span> is not in the list, or am I missing something?

Severity: -- → S3
Status: UNCONFIRMED → RESOLVED
Closed: 3 years ago
Resolution: --- → INVALID
Flags: needinfo?(hsivonen)
You need to log in before you can comment on or make changes to this bug.