You will need to use Object.defineProperty in HTMLElement.prototype to override setter and getter innerHTML with its own innerHTML which treats the elements you want as void . Take a look here for how innerHTML and the HTML parser are implemented by default.
Note that Firefox sucks in inheritance when it comes to defining material on HTMLElement.prototype, where it filters, for example, on HTMLDivElement. In Opera, everything should work well.
In other words, which elements are invalid depends on the HTML parser. The parser follows this list and innerHTML uses the same rules basically.
So, if you do not want to create your own implementation of innerHTML in JS, you should probably just forget about it.
You can use the live DOM viewer to show others how specific markup is parsed. Then you will probably notice that the same end tags implicitly close the open item.
I have an obsolete innerHTML getter (not setter though) code here that uses a list of void elements. This may give you some ideas. But writing a setter implementation can be harder.
On the other hand, if you use createElement () and appendChild (), etc. instead of innerHTML, you donβt have to worry about this, and the native innerHTML getter displays unknown elements with end tags.
Note that you can treat the unknown element as xml and use XMLSerializer () and DOMParser () to do the following:
var x = document.createElement("test"); var serializer = new XMLSerializer(); alert(serializer.serializeToString(x)); var parser = new DOMParser(); var doc = parser.parseFromString("<test/>", "application/xml"); var div = document.createElement("div"); div.appendChild(document.importNode(doc.documentElement, true)); alert(serializer.serializeToString(div));
This is not exactly what you want, but what you can play with. (Check that in Opera instead of Firefox, to see the difference with the xmlns attributes. Also note that Chrome does not work, like Opera and Firefox.)