I've the following string containing HMTL. What would be sample code in JavaScript to remove leading and trailing white spaces that would appear in the rendering of this string? In other words: how can I obtain a string that would not show any leading or training white spaces in the HTML rendering but otherwise be identical?
<p> </p>
<div> </div>
Trimming using JavaScript<br />
<br />
<br />
<br />
all leading and trailing white spaces
<p> </p>
<div> </div>
<br />
tags be preserved in the expected output, but not e.g.<div></div>
? – Noe<br />
tag is not removed when trimming because its not a leading whitespace nor a trailing whitespace. But, iIf this tag occurred after the last displayable character(s) then it would be a trailing whitespace and will need to be trimmed. Similarly, if this tag occurred before the first displayable character(s) then it would be a leading whitespace and will need to be trimmed. – Flower<p></p>
get removed (if leading or trailing)? What about tags not meant to hold text, like an<img>
? Should we only delete entire elements or should we e.g. trim<p> foo</p>
to<p>foo</p>
? What about literal newline and space characters between tags that with default CSS won't render but could withwhite-space: pre
? What about<pre>
elements? Probably there's more... – Absent