rangy icon indicating copy to clipboard operation
rangy copied to clipboard

appendChild not aware of void elements

Open Rob-pw opened this issue 10 years ago • 2 comments

At present, calling insertNodeAtPosition where n is a void element (e.g., <br>) causes the element to be appended as a child element, which results (potentially) in invalid nested elements.

I have added an array containing all the void elements as per http://www.w3.org/TR/html-markup/syntax.html#void-elements, and a function canAppendChild(element) to avoid this issue.

Rob-pw avatar Jun 12 '15 15:06 Rob-pw

I like this conceptually, but I'm not sure whether it is the correct thing to do. Current versions of all the major browsers allow you to add child nodes to a <br> element via JavaScript, both directly and using Range.insertNode. If you do so, the content shows up in the childNodes property of the <br> element but is not rendered and does not show up in innerHTML. I would guess this behaviour is specified somewhere since all the browsers do it. I'll try to find out.

timdown avatar Jun 13 '15 15:06 timdown

You are correct, elements can be appended to void elements, however they are not rendered. If changing existing functionality is undesirable, maybe just expose a method for checking whether an element can be appended as a child, that would satisfy my requirements also.

Rob-pw avatar Jun 16 '15 07:06 Rob-pw