Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2b624ed

Browse files
Caro Medeliusjenkins-bot
Caro Medelius
authored and
jenkins-bot
committed
ve.dm.Document: use offset map in findText
Use offset map in findText's set conditionto get correct ranges when case-folding has been usedBug: T407928Change-Id: I342b601c939c3d8a3b5ee40c04076972be327a6c
1 parenteb4f945 commit2b624ed

File tree

2 files changed

+53
-9
lines changed

2 files changed

+53
-9
lines changed

‎src/dm/ve.dm.Document.js‎

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1702,28 +1702,73 @@ ve.dm.Document.prototype.findText = function ( query, options = {} ) {
17021702
return[];
17031703
}
17041704

1705+
letnormalizedQuery=query;
17051706
if(!options.caseSensitiveString){
1706-
query=newSet(Array.from(query).map((s)=>s.toLocaleLowerCase(this.lang)));
1707+
normalizedQuery=newSet(Array.from(query).map((s)=>s.toLocaleLowerCase(this.lang)));
17071708
}
17081709

17091710
letminLen=Infinity,
17101711
maxLen=0;
1711-
query.forEach((s)=>{
1712+
normalizedQuery.forEach((s)=>{
17121713
minLen=Math.min(minLen,s.length);
17131714
maxLen=Math.max(maxLen,s.length);
17141715
});
17151716

1717+
/**
1718+
* Map from offset in case-folded string to offset in original string
1719+
* In some cases, case-folding can change string length
1720+
* For example, if s = '\u0130', then s.length === 1 but s.toLocaleLowerCase( 'en' ).length === 2
1721+
*
1722+
*@param {string} s
1723+
*@param {number} offsetLower in lowercased string
1724+
*@return {number} corresponding offset in original string
1725+
*/
1726+
constfixOffset=function(s,offsetLower){
1727+
// Start by guessing that lowercasing didn't change the offset,
1728+
// except when the offset is out of bounds in the original string
1729+
letguess=Math.min(offsetLower,s.length);
1730+
1731+
letdiff=s.slice(0,guess).toLocaleLowerCase(this.lang).length-offsetLower;
1732+
if(diff===0){
1733+
// Optimization note: this will almost always be true
1734+
// Only rare characters change length of substr when case folding
1735+
returnguess;
1736+
}
1737+
1738+
while(diff>0){
1739+
// The lowercase substr is longer than original
1740+
guess--;
1741+
diff=s.slice(0,guess).toLocaleLowerCase(this.lang).length-offsetLower;
1742+
}
1743+
1744+
while(diff<0){
1745+
// The lowercase substr is shorter than original
1746+
guess++;
1747+
diff=s.slice(0,guess).toLocaleLowerCase(this.lang).length-offsetLower;
1748+
}
1749+
// In some rare situations the diff might be positive now
1750+
// (which would correspond to no offset in the original string mapping to the desired offset)
1751+
returnguess;
1752+
};
1753+
17161754
data.forEachRunOfContent(searchRange,(off,line)=>{
1755+
letnormalizedLine=line;
17171756
if(!options.caseSensitiveString){
1718-
line=line.toLocaleLowerCase(this.lang);
1757+
normalizedLine=line.toLocaleLowerCase(this.lang);
17191758
}
17201759

17211760
// For each possible length, do a sliding window search on the normalized line
17221761
for(letlen=minLen;len<=maxLen;len++){
1723-
for(leti=0;i<=line.length-len;i++){
1724-
constsubstr=line.slice(i,i+len);
1725-
if(query.has(substr)){
1726-
ranges.push(newve.Range(off+i,off+i+len));
1762+
for(leti=0;i<=normalizedLine.length-len;i++){
1763+
constsubstr=normalizedLine.slice(i,i+len);
1764+
if(normalizedQuery.has(substr)){
1765+
letstart=i;
1766+
letend=i+len;
1767+
if(!options.caseSensitiveString){
1768+
start=fixOffset(line,start);
1769+
end=fixOffset(line,end);
1770+
}
1771+
ranges.push(newve.Range(off+start,off+end));
17271772
if(options.noOverlaps){
17281773
i+=len-1;
17291774
}

‎tests/dm/ve.dm.Document.test.js‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,8 +1171,7 @@ QUnit.test( 'findText (plain text)', ( assert ) => {
11711171
options:{},
11721172
ranges:[
11731173
newve.Range(78,82)
1174-
],
1175-
expectFail:true
1174+
]
11761175
},
11771176
{
11781177
msg:'Diacritic insensitive & case sensitive match',

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp