]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/javascript/yui3/build/text/text-wordbreak.js
Release 6.2.2
[Github/sugarcrm.git] / include / javascript / yui3 / build / text / text-wordbreak.js
1 /*
2  Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3  Code licensed under the BSD License:
4  http://developer.yahoo.com/yui/license.html
5  version: 3.3.0
6  build: 3167
7  */
8 YUI.add('text-wordbreak',function(Y){var Text=Y.Text,WBData=Text.Data.WordBreak,ALETTER=0,MIDNUMLET=1,MIDLETTER=2,MIDNUM=3,NUMERIC=4,CR=5,LF=6,NEWLINE=7,EXTEND=8,FORMAT=9,KATAKANA=10,EXTENDNUMLET=11,OTHER=12,SETS=[new RegExp(WBData.aletter),new RegExp(WBData.midnumlet),new RegExp(WBData.midletter),new RegExp(WBData.midnum),new RegExp(WBData.numeric),new RegExp(WBData.cr),new RegExp(WBData.lf),new RegExp(WBData.newline),new RegExp(WBData.extend),new RegExp(WBData.format),new RegExp(WBData.katakana),new RegExp(WBData.extendnumlet)],EMPTY_STRING='',PUNCTUATION=new RegExp('^'+WBData.punctuation+'$'),WHITESPACE=/\s/,WordBreak={getWords:function(string,options){var i=0,map=WordBreak._classify(string),len=map.length,word=[],words=[],chr,includePunctuation,includeWhitespace;if(!options){options={};}
9 if(options.ignoreCase){string=string.toLowerCase();}
10 includePunctuation=options.includePunctuation;includeWhitespace=options.includeWhitespace;for(;i<len;++i){chr=string.charAt(i);word.push(chr);if(WordBreak._isWordBoundary(map,i)){word=word.join(EMPTY_STRING);if(word&&(includeWhitespace||!WHITESPACE.test(word))&&(includePunctuation||!PUNCTUATION.test(word))){words.push(word);}
11 word=[];}}
12 return words;},getUniqueWords:function(string,options){return Y.Array.unique(WordBreak.getWords(string,options));},isWordBoundary:function(string,index){return WordBreak._isWordBoundary(WordBreak._classify(string),index);},_classify:function(string){var chr,map=[],i=0,j,set,stringLength=string.length,setsLength=SETS.length,type;for(;i<stringLength;++i){chr=string.charAt(i);type=OTHER;for(j=0;j<setsLength;++j){set=SETS[j];if(set&&set.test(chr)){type=j;break;}}
13 map.push(type);}
14 return map;},_isWordBoundary:function(map,index){var prevType,type=map[index],nextType=map[index+1],nextNextType;if(index<0||(index>map.length-1&&index!==0)){return false;}
15 if(type===ALETTER&&nextType===ALETTER){return false;}
16 nextNextType=map[index+2];if(type===ALETTER&&(nextType===MIDLETTER||nextType===MIDNUMLET)&&nextNextType===ALETTER){return false;}
17 prevType=map[index-1];if((type===MIDLETTER||type===MIDNUMLET)&&nextType===ALETTER&&prevType===ALETTER){return false;}
18 if((type===NUMERIC||type===ALETTER)&&(nextType===NUMERIC||nextType===ALETTER)){return false;}
19 if((type===MIDNUM||type===MIDNUMLET)&&nextType===NUMERIC&&prevType===NUMERIC){return false;}
20 if(type===NUMERIC&&(nextType===MIDNUM||nextType===MIDNUMLET)&&nextNextType===NUMERIC){return false;}
21 if(type===EXTEND||type===FORMAT||prevType===EXTEND||prevType===FORMAT||nextType===EXTEND||nextType===FORMAT){return false;}
22 if(type===CR&&nextType===LF){return false;}
23 if(type===NEWLINE||type===CR||type===LF){return true;}
24 if(nextType===NEWLINE||nextType===CR||nextType===LF){return true;}
25 if(type===KATAKANA&&nextType===KATAKANA){return false;}
26 if(nextType===EXTENDNUMLET&&(type===ALETTER||type===NUMERIC||type===KATAKANA||type===EXTENDNUMLET)){return false;}
27 if(type===EXTENDNUMLET&&(nextType===ALETTER||nextType===NUMERIC||nextType===KATAKANA)){return false;}
28 return true;}};Text.WordBreak=WordBreak;},'3.3.0',{requires:['array-extras','text-data-wordbreak']});