What characters will a TextArea wrap at, other than spaces?

 ̄綄美尐妖づ 提交于 2019-12-10 14:27:43

问题


I'm working on the latest version of my plugin, Textarea Line Counter (http://mostthingsweb.com/?p=84). To make it even more accurate, I want to identify regions of text that wrap because they are too large to fit on the line (like a sequence of repeated characters).

I'm assuming that browsers only wrap text at spaces. Are there any other characters that lines can be wrapped at? Thank you,


回答1:


Looks like it depends on the browser, my Opera wraps also on e.g. + % ? $ / ( [ { } \ ° ! ¿
Safari/Chrome on ¿ ? too

(guess there are lots more)




回答2:


Nice idea for a plugin. Fighting the accuracy issues is going to be a challenge.

There's not a universal catch all for the way textarea is going to handle a string (other than line breaks at spaces), or using word-wrap.

IE produced a break with . , () {} ?, but not with / * = +

In this example, textarea seems to have that "special" feeling like a td




回答3:


Based on all your advice, I have created a solution. It is rather large, and in fact I think I will make it into a separate plugin, as well as including it in my Textarea Line Counter. It works like this:

  1. Create a div to act as a container, and set the font to something monospaced (i.e. every character is the same width)
  2. Create a span within the container, and place a single letter.
  3. Take the width measurement of the span (which will be the width of the letter, once margins, padding, and some other CSS attributes are cloned)
  4. Create another div within the container and clone its CSS attributes. Set it's width to be two times the width of the letter found in step 3, and record its height.
  5. To test if a character will cause a wrap, set the text of the div to: A[some character]A. [some character] is a character you are trying to test.
  6. Test the height of the div. If it is larger than the height found in step 4, the text has wrapped.

I'm looking forward to releasing this plugin. Thank you again for all your advice.




回答4:


some browsers will break inside words if the word is longer than the col width, otherwise they break on spaces.

I notice some browsers set this by default- you can, too in most bowsers with:

textarea{word-wrap: break-word}

you can be sure it is not set by using textarea{word-wrap: normal}



来源:https://stackoverflow.com/questions/5011756/what-characters-will-a-textarea-wrap-at-other-than-spaces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!