tags:

views:

80

answers:

3

I have this markup

<html>
... some HTML ...

And I need to wrap it with an element like this:

<html>
<div class="user-content">
   ... some HTML ...
</div>

The problem, is ... some HTML ... can be lots of different things from raw text to complicated markup.

<div>

If I use a <div>, then it adds a block-level break. Meaning if I had

Paul is cool

now I have

<div class="user-content">
   Paul is cool
</div>

which forces a line break.

<span>

If I use a <span> then weird things start happening when I have a <span> contain a <div>. For example, the width of the span is shown as 0px which makes javascript not too happy with that node.

Is there a better tag I can use?

Some context

It's a long story. I need the node to exist in the HTML since I'm running untrusted javascript and I don't want the javascript to be able to walk inside that node. To do that, we've sandboxed all the DOM functions, and at each DOM call, we'll check if we're operating on a "user-content" node and not allow access if we are walking there or to any of its children.

+8  A: 

If your only problem with using a div is the line break when why not just give it the style: display: inline;?

If you can be more specific about your problem... Why do you have to wrap it?

Basically, div is what you want, just style it appropriately.

thenduks
Perfect answer. To expand upon this just a little bit, you could also use other block level tags, just so long as you make sure to set a CSS rule of display: inline. So, if for example the header, footer, article, or section tags feel more semantically appropriate than div, feel free to use those.
Moses
so adding `display:inline` doesn't actually make it an inline element? (meaning it can still have block children)? Are there other CSS attributes I should use? I want it to render as if it doesn't really exist in the DOM.
Paul Tarjan
Well, Paul, I believe the rule is that inline elements can't have _block level children_ -- they can of course have _children_.
thenduks
As for rendering as if it wasn't in the DOM at all... Well, the only way to do that is to _not have it in the DOM at all_!Again, can you be more specific about what you need and why? Perhaps there's another way to do what you need... Jumping to conclusions often leads to pain (here yours is "I need to wrap this in an element").
thenduks
Lastly remember that you don't _have_ to make the div inline! If what you need is to avoid the line-break caused by the block level element, then perhaps float or an absolute position would work...? I promise this is the last time I'll say it: if you give more information, that would be helpful in giving you the best solution :)
thenduks
I edited the question. We don't want javascript to be able to walk inside these elements.
Paul Tarjan
So, I tried a `<div>` with `display:inline` and javascript had the same trouble. The scrollWidth was 0 and other weirdness, just like if I used a `<span>` :(
Paul Tarjan
`display: inline` is exactly what makes an element inline, so, yea, that's not what you're looking for. But based on your updated answer this isn't the right approach at all if you ask me. I think you'd probably have better luck putting untrusted user content inside an iframe or something.
thenduks
A: 

You said you have a span which contains a div. Block elements should not be contained within inline elements. You could, however, use the span element within a div.

mjw06d
A: 

<span> is the closest thing to such element, and the weird behavior you mentioned is normal, <span> is an inline element, while <div> is a block element. You can't put block elements inside inline ones. (display type (inline/block) is the only difference between a span and a div I guess)

aularon