Given a
<div
lang="en"
style="
overflow-wrap: word-break;
hyphens: auto;
width: 8.6em;
background-color: highlight;
color: highlightText;
font-size: 20px;
">
This is a text that is long enough
to cause a line break if set so.
By using words like
"incomprehensibilities",
we can demonstrate word breaks.
</div>
I would like to access the formatted text with hyphenations with JavaScript to get a string similar to:
"This is a text that is long enough to cause a line break if set so. By using words like "incomprehensibilit-ies", we can demon-strate word breaks."
.
N.B. hyphens in incomprehensibilit-ies
and demon-strate
.
Is this possible?
The use case is that I need a way to break and hyphenate text in a <text>
element of an SVG by only using SVG elements.
2
Answers
Perhaps try with this
Far from perfect. This one:
<span>
offsetHeight
if a SPAN spans multiple lines<span>
to find when the word wrapped to a new line
The error is "demonstrate" fits when shortened to "demonstr" – "ate"
Needs some more JS voodoo