Home  >  Article  >  Web Front-end  >  Some research materials on intuitive line wrapping of textarea_javascript skills

Some research materials on intuitive line wrapping of textarea_javascript skills

WBOY
WBOYOriginal
2016-05-16 19:25:421338browse

Two days ago, I studied the line wrapping rules of textarea's intuitive rows. It's quite complicated:

How to choose the intuitive rows depends not only on the cols size, but also on the web page encoding method. The textarea of ​​
cols="30" can actually accommodate 29 half-width characters per line. The number of full-width characters is not necessarily determined, and it is related to the encoding method of the web page.
In the actual measurement of IE6.0, the visual line break occurs at the red text (the red text is folded to the next line).

dddddddddddddddddddddddddddddddd //The continuous half-width content after the full-width is treated as a complete word. If the remaining position cannot accommodate the length of the word, it must be wrapped. This is a special rule A

The general rule when web pages are encoded in Unicode (the pop-up page of "Run Code" is encoded in this way, and the pages generated by JS are encoded in this way) is

("full" means that no matter what is followed by the full-width , half-width characters, all have to be wrapped)

123456789012345678901234567890 //29 half-width is full
12345678911234567890 //17 full-width is full
One two three four five six seven eight ninety one two three four five six seven eight ninety
One two three four five six seven eight ninety one two three four five six seven eight ninety
0 One Two Three Four Five Six Seven Eight Nine One Two Three Four Five Six One Seven Eight Ninety //16 full-width, 1 half-width occupied
0 One Two Three Four Five Six Seven Eight Nine Eleven Two Three Four Five six seven eight ninety
0 one two three four five six seven eight ninety one two three four five one six seven eight ninety //15 full-width, full-width is not allowed after 2 half-widths
01 one two three four Five six seven eight ninety one two three four five six seven eight ninety
01 one two three four five six seven eight ninety one two three four five two six seven eight ninety // 15 full-width, 2 half-width can be used Allow another half-width (if more than one half-width, special rule A is followed)
012 one, two, three, four, five, six, seven, eight, ninety, one, two, three, four, five, six, seven, eight, ninety // 15 full-widths, 3 half-widths are used
012 one two three four five six seven eight ninety one two three four five three six seven eight ninety
0123 one two three four five six seven eight ninety eleven two three four five six seven eight ninety //14 Full-width, after 4 half-width, full-width is not allowed
0123 One, two, three, four, five-six, seven-eight, ninety-one, two, three, four, five-six, seven-eight-ninety // 14 full-width, after 4 half-width, 1 half-width can be accommodated (more than 1 According to special rules for half-width A)
01234 one, two, three, four, five, six, seven, eight, ninety, one, two, three, four, five, six, seven, eight, ninety // 14 full-width, 5 half-width occupied
01234, one-two-three-four-five-six Seven eight ninety one two three four five five six seven eight ninety
012345 one two three four five six seven eight ninety one two three four five six seven eight ninety //13 full-width, full-width cannot be allowed after 6 half-width
012345 123456789112364567890 //13 full-width, after 6 half-width, 1 half-width can be accommodated (if more than 1 half-width, special rule A will apply)
0123456 One Two Three Four Five Six Seven Eight Nine One Two Three Four Five Six Seven Eight Ninety //13 full-width, 7 half-width occupied
0123456 One Two Three Four Five Six Seven Eight Nine Eleven Two Three Seven Four Five six seven eight ninety
01234567 one two three four five six seven eight ninety one two three four five six seven eight ninety // 12 full-width, 8 half-width occupied
01234567 one two three four five sixty-seven 8912834567890
0123456781234567891234567890//11 full-width, 9 half-width cannot be followed by full-width
012345678 one two three four five six seven eight nine one nine two three four five six seven eight nine ten //11 full-width, after 9 half-widths, 1 half-width can be accommodated (if more than 1 half-width, special rule A will apply)
0123456789 one two three four five six seven eight ninety one two three four five six seven eight ninety //11 full-width, 10 half-width occupied

......

0 one two three four 56789123451267890 //15 full-width, after 1 half-width, 2 half-width consecutive characters can be accommodated (special rule A applies when more than 2 half-widths are used), this item can be changed from 15 full-width, 3 half-widths are filled up to get

...

0 one, two, three, four, five, six, seven, eight, ninety, one, two, three, four, 1234, five, six, seven, eight, ninety // 14 full-width, 1 half-width It can accommodate 4 consecutive half-width characters (if there are more than 4 half-widths, special rule A is followed). This can be obtained from 14 full-widths and 5 half-widths.

...

01 one two three four 567891234234567890 //14 full-width, after 2 half-widths, 3 half-width consecutive characters can be accommodated (special rule A applies when more than 3 half-widths), this item can be composed of 14 full-width, 5 half-width is filled up to get

...
The line break position is not only related to the entire line of text before the line break position, but also to the red text (often the full-width text is folded because it cannot be squeezed down at the end of the upper line) of the next line).

To summarize:


CODE:[Copy to clipboard]x full width, y half width occupied = x full width, y-1 half width cannot be accommodated after full width = x full width, After y-n half-width characters, n half-width consecutive characters can be accommodated (if there are more than n half-width characters, special rules A apply).
So just do experiments to find out all the "full" situations.

Now list cols=30 and the "full" situation when the webpage is encoded in Unicode:

17 full-width
16 full-width, 1 half-width
15 full-width, 3 half-width
14 full-width, 5 half-width
13 full-width, 7 half-width
12 full-width, 8 half-width
11 full-width, 10 half-width
10 full-width, 12 half-width
9 full-width, 13 half-width
8 full-width, 15 half-width
7 full-width, 17 half-width
6 full-width, 19 half-width
5 full-width, 21 half-width
4 full-width, 22 half-width
3 full-width, 24 half-width
2 Full-width, 26 half-width
1 full-width, 28 half-width
29 half-width

Using the situation where a line break is on the verge of occurring according to the special rule A, it is easy to detect all "full" situations:
For example , one two three four five six seven eight nine ten 012345678912 one two three four five six seven eight nine ten //10 full-width, 12 half-width occupied
one two three four five six seven eight nine ten 0123456789123 one two three four five Sixty-seven, eighty-ninety // The red words are line-wrapped according to the special rule A, the blue words are full-width 9, and 13 half-width are used to wrap the line

cols=30. When the web page is encoded according to GB2312, the "full-width" rule is different. :

14 full-width, 1 half-width
13 full-width, 3 half-width
12 full-width, 5 half-width
11 full-width, 7 half-width
10 full-width, 9 half-width
9 full-width, 11 half-width
8 full-width, 13 half-width
7 full-width, 15 half-width
6 full-width, 17 half-width
5 full-width, 19 half-width
4 full-width, 21 half-width
3 full-width, 23 half-width
2 full-width, 25 half-width
1 full-width, 27 half-width
29 half-width

In this case, the "full" rule can only be measured through experiments based on the encoding method and cols of the web page. .

Count the number of full-width and half-width characters from the beginning of the textarea content, determine the position of the first visual line break according to the "full" rule and special rule A, and then start from the second line (including physical lines and visual lines) Line), count the number of full-width and half-width characters, and determine the position of the second intuitive line break according to the "full" rule and special rule A...and continue in this way until the end of the textarea content. In this way, all line break positions can be obtained.

Of course, it is not necessary or possible to do this experiment in actual applications. For example, select rows X to Y of a textarea. I haven’t learned the official method yet. Here’s what I did:


[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh to execute
]

What I wrote in early 2004 should be the most comprehensive research at present, right? ?
  Current Position : Physical Row: Physical Column: Visible Row: Visible Column: Error Message:
Current Position: Physical Row: Physical Column: Visible Row: Visible Column: Error Message:
CreateRange CreateTextRange
CreateRange.getClientRects() CreateTextRange.getClientRects()
CreateRange.getBoundingClientRect() CreateTextRange.getBoundingClientRect()

[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]

根据被某网站匿名转载的页面( http://dl.pconline.com.cn/html_2/4/545/id=38457&pn=0.html )上的网友留言,上面的《JS实现文本域的任意行选定》示例代码有一处bug:即在滚动条不在顶端的情况下运行示例代码会出错。现已修正:只增加了一行代码


CODE:
src.scrollTop=0 //确保开始数行数时滚动条位于顶部

[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn