views:

1108

answers:

3

I'm trying to fix a bug in a rich text editor I'm using, which causes <embed> tags to be inserted without their closing tag (which screws the output up completely). I've isolated the problem to this operation:

// body is a <body> tag
body.innerHTML = '<embed src="http://example.com/whatever"&gt;&lt;/embed&gt;';

No fancy code, just Firefox's innerHTML assignment. You should be able to duplicate the error in Firebug like so:

>>> document.body.innerHTML = "<embed></embed>"
"<embed></embed>"
>>> document.body.innerHTML
"<embed>"

Is there a workaround for this? I need the tag, but I can't justify rebuilding/replacing the entire rich text editor because of one crappy edge case.

I can't convert this to something like document.createElement('embed'), because real-world input to this editor can easily include a few paragraphs of text wrapped around the <embed>; innerHTML is, on paper, perfect for this use case, I just can't get it to work with an <embed>.

+4  A: 

This might not be an answer to your problem, but <embed> has never been part of any standardized version of HTML, says W3C.

some
Very true, so +1. But unfortunately, people use it, and it's the source of a nasty bug that, in the right circumstances, cuts off all content after the unclosed <embed>. ;)
ojrac
Not totally true anymore; `<embed>` has been added to the HTML5 spec. http://dev.w3.org/html5/spec/Overview.html#the-embed-element
yc
Wow, one should never say never...
some
A: 

Although <embed> is not a part of a standard html, it has been widely implemented and is, therefore, a de-facto standard.

That said, I'm running Firefox 3 (MineField v3.0.0.5) and am getting slightly unexpected, but altogether correct functionality. I was expecting the exact HTML to be put into the element I selected, but instead I got clean html.

Since <embed is an empty tag (that is, it cannot have contents, only attributes), Firefox turns this

<embed attribute="value"></embed>

into

<embed  attribute="value"/>

, which is a prefectly valid closed tag. It does the same on other empty tags, such as <input /> and <image>

At the this page you can see this in action. The scriptless version of the page has an embedded video (youtube) inside of a bright pink div. The script steals its contents and places it into the body as innerHTML.

yaauie
I was under the impression that a de facto standard was a standard that was nearly universal. However, given that object (the standard version) is just as widely supported and used quite a bit, I wouldn't consider embed is a de facto standard.
R. Bemrose
+1  A: 
Alan Storm
The problem you are referring to is that script-tags can't contain any tags and that the parser sees the start of the end tag: "</". If you had used the format "<element><\/element>" in your document.write it had worked.
some
As I understand it, that'd just evaluate to "<embed></embed>" before assignment, the same thing that fails.I've also tried stuff like innerHTML += "</embed>".Trying to break the assignment into multiple lines (like `innerHTML += "</embe"`) just throws errors.
ojrac
Weird, sorry about that. I thought I'd run though a bunch of tests, but it's not working now. I'm fired.
Alan Storm
You gave me another idea to test, though. Those are always good.We could always put in a feature request for a "Mail Goggles" style SO feature: Solve some addition/programming puzzles in under 60 seconds to post! ;)
ojrac