Let's assume the following element (look for the trailing and leading spaces):
<p>
<span class="item">Lorem Ipsum is simply dummy text </span><span class="item">of the printing and typesetting</span><span class="item"> industry.</span>
</p>
I want to replace all spaces with
, due to display: inline-block
weird behavior shown here: http://jsfiddle.net/SQuUZ/ (I don't know about all browsers, but latest Chrome and Firefox both act the same).
Now, since javascript is an option here, so is jQuery, I could:
$('p').text($('p').text().replace(/ /g, ' '));
But it escapes the
and turns out into a mess of entities
.
Obviously, for such purposes we could use $('p').html()
:
$('p').html($('p').html().replace(/ /g, ' '));
But this one is even worse, because, it also adds
within the tags themselves:
<p>
<span class="item">Lorem Ipsum is simply dummy text </span><span class="item">of the printing and typesetting</span><span class="item"> industry.</span>
</p>
<!-- TL;DR -->
<span class="item"></span> <!-- is actually invalid... -->
And it breaks everything...
<span>
elements with class item
inside the container (that may also not always be <p>
).What options do I have here?
In fact, could anyone explain why there is such a bug with that multi-line / single-line display: inline-block;
? (See fiddle link above, and examine...)
Question migrated to display: inline-block; weird spacing behavior
Use the String. replaceAll method to replace all spaces with underscores in a JavaScript string, e.g. string. replaceAll(' ', '_') . The replaceAll method returns a new string with all whitespace characters replaced by underscores.
In Java, we can use regex \\s+ to match whitespace characters, and replaceAll("\\s+", " ") to replace them with a single space.
$('p').contents().filter(function(){
return this.nodeType == 3 // Text node
}).each(function(){
this.data = this.data.replace(/ /g, '\u00a0');
});
DEMO
Even tho jQuery is really great and does all things, CSS could also work in some cases:
white-space: pre-wrap;
Demo.
CSS3 related: text-space-collapse
could anyone explain why there is such a bug with that multi-line / single-line display: inline-block;? (See fiddle link above, and examine...)
Consider:
<p><span style="display:inline-block">lorem </span><span>ipsum</span></p>
The space character is inside the line box container created by display:inline-block
. CSS 2.1 16.6.1 describes how spaces in a line box must be processed:
As each line is laid out … [i]f a space (U+0020) at the end of a line has 'white-space' set to 'normal', 'nowrap', or 'pre-line', it is … removed.
As the space is at the end of the line inside the inline block, it is removed.
Contrast:
<p><span style="display:inline-block">lorem</span> <span>ipsum</span></p>
In this case, the space is not removed, because it is between two inline-level elements that make up a single line box.
you can use the <pre> </pre>
element to make spaces visible. Its a fast solution if you want to visually display, lets say, ascii art.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With