7
<p style="width:60px"> I am some random text. I am Some text.blabla</p>

The rendered HTML result of above might be,

I am some ra
ndom text. I
am Some text
.blabla

I would like to split the above paragraph into separate paragraphs row by row. So what the expected result is like,

<p style="width:60px">I am some ra</p>
<p style="width:60px">ndom text. I</p>
<p style="width:60px">am Some text</p>
<p style="width:60px">.blabla</p>

Is it possible to implement it in Javascript?

P.S. Why do I have to split it? Because I would like to get my own pagination system for a long HTML, where all the paragraphs and splited paragraphs within a height range would be put in to a same <div class="page"></div>. Finally, the whole HTML would be organized as

<div id="page1" class="page">
   <p> complete content </p>
   <p> upper part of XXX </p>
</div>

<div id="page2" class="page">
   <p> bottom part of XXX </p>
   <p>...</p><p>...</p>
</div>
Mostafa Talebi
  • 8,825
  • 16
  • 61
  • 105
ppn029012
  • 580
  • 1
  • 6
  • 20

2 Answers2

1

Well, assume that your text is in a <p id='wholeText'> :

var element = document.getElementById("wholeText");

var wholeText = element.innerHTML;

var splitText = wholeText.split("\r\n");

var newHtml = null;

for(var i = 0; i < splitText.length; i++)
{
   newHtml += "<p class='each-line'>"+splitText[i]+'</p>';
}

Then you can use newHtml to write in a DOM element. For instance, if you like to add it to a the same <p> which you got text from, you do:

element.innerHTML = newHtml; // remember element refers to the <p> with the ID of wholeText

Also put all of the above codes into a function and call the function after the windows has been fully loaded.

window.addEventListener("load", function(){

// put the code here
}, false);

If your line does not contain break characters, then please refer to this plugin : http://jsfiddle.net/nathan/qkmse/

The above link is suggested in this SO's question:

detecting line-breaks with jQuery?

Manngo
  • 14,066
  • 10
  • 88
  • 110
Mostafa Talebi
  • 8,825
  • 16
  • 61
  • 105
  • 1
    Thanks for your answer. The original text does not contain any '\r' or '\n'. The word breaking is only controlled by CSS. – ppn029012 Jul 27 '14 at 10:40
  • @ppn029012 I searched many places and found a solution, look at the updated answer's bottom-section. If the answer solves your problem, then I appreciate if you accept my answer. – Mostafa Talebi Jul 27 '14 at 11:15
  • Yes. It is one of the solution, but it works only with the words separated by blank. In my case, I have to modify it to adjust the text without any blank between the words. Thx. – ppn029012 Jul 27 '14 at 17:19
  • 2
    This answer starts by presenting a solution to a problem that was apparently not what was asked. Later two links were added, suggested as addressing the question asked. Link-only answers are generally not regarded as real answers at SO. – Jukka K. Korpela Jul 28 '14 at 06:30
1

If you really want to format the text as if word-wrap:break-word; word-break:break-all; were set on the p element (as you say in a comment) and each of the resulting lines is turned to a paragraph, you need code like the following: Process the p element character by character, putting as many characters as fit on one with the given width. When it is exceeded, construct a new p element and proceed with the rest of the text. You can do this by writing the characters to an invisible inline block and checking its rendered width.

Example (note that this really works only when the paragraph contains just text, no markup):

<p style="width:60px" id=p> I am some random text. I am Some text.blabla</p>
<script>
var width = 60;
var p = document.getElementById('p');
var parent = p.parentNode;
var line = document.createElement('span');
line.style.display = 'inline-block';
line.style.visibility = 'hidden';
var content = p.innerHTML;
document.body.appendChild(line);
var start = 0;
var i = 1;
while(i < content.length) {
  line.innerHTML = content.substring(start, i);
  console.log(i + ' ' + content.length);
  if(line.clientWidth > width) {
     var newp = document.createElement('p');
     newp.style.width = width + 'px';
     newp.innerHTML = content.substring(start, i - 1);
     parent.insertBefore(newp, p);
     start = i - 1;
     i = start + 1;
   }
   else {
     i++;
   }
}
newp = document.createElement('p');
newp.style.width = width + 'px';
newp.innerHTML = content.substring(start);
parent.insertBefore(newp, p);
parent.removeChild(p);
</script>

I’m pretty sure you should have a completely different approach, but I cannot help with the ultimate problem you are trying to solve, as it was not described.

Jukka K. Korpela
  • 195,524
  • 37
  • 270
  • 390
  • Your solution is similar with the other answer. Actually, the final goal is, transforming a long HTML to pages of PDF. The header of each page is based on the content of the first row of the rendered PDF(running header). But I can't predict what it would be in the first, so I try to implement my own pagination and add `page-break-before` for each page div. – ppn029012 Jul 28 '14 at 02:20
  • 1
    The key difference is that this solution does not expect to find line breaks in the source (in the sample in the question, there are none) and breaks according to the width requirement, not by the source line breaks. And I still don’t see how the question relates to pagination. If you think you need to split one logical paragraph into several `p` elements, there has probably been a mistake in some previous steps in addressing the original problem. – Jukka K. Korpela Jul 28 '14 at 05:49
  • Yes. The other answer discussed about the source line break first, then they changed it after I commented it. ok. When we split a long html into pages, we need to arrange the paragraphs into proper pages. But some paragraph near the edge need to be cut into two parts. e.g. 3 rows of 1 paragraph should be in Page n and the rest of 4 rows of that paragraph should be arranged to the next. So we need to split the one p into two. – ppn029012 Jul 28 '14 at 06:11