Bug 991427 - Check if the last character of the string is part of a surrogate pair when truncating text for display in the context menu, to avoid splitting surrogate pairs, r=gavin

This commit is contained in:
Andrea 2014-08-16 17:06:10 -07:00
parent e19d99af2b
commit cf8d1dff40
3 changed files with 31 additions and 13 deletions

View File

@ -1677,8 +1677,15 @@ nsContextMenu.prototype = {
// Store searchTerms in context menu item so we know what to search onclick
menuItem.searchTerms = selectedText;
if (selectedText.length > 15)
selectedText = selectedText.substr(0,15) + this.ellipsis;
// If the JS character after our truncation point is a trail surrogate,
// include it in the truncated string to avoid splitting a surrogate pair.
if (selectedText.length > 15) {
let truncLength = 15;
let truncChar = selectedText[15].charCodeAt(0);
if (truncChar >= 0xDC00 && truncChar <= 0xDFFF)
truncLength++;
selectedText = selectedText.substr(0,truncLength) + this.ellipsis;
}
// Use the current engine if the search bar is visible, the default
// engine otherwise.

View File

@ -108,6 +108,13 @@ function test() {
expectedLabelContents: "A partial link " + ellipsis,
});
testElement({
id: "surrogatePair",
isSelected: true,
shouldBeShown: true,
expectedLabelContents: "This character\uD83D\uDD25" + ellipsis,
});
// cleanup
document.popupNode = null;
gBrowser.removeCurrentTab();

View File

@ -1,16 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<a href="http://mozilla.org" id="link">I'm a link!</a>
<a href="http://mozilla.org" id="longLink">I'm a really long link and I should be truncated.</a>
<body>
<a href="http://mozilla.org" id="link">I'm a link!</a>
<a href="http://mozilla.org" id="longLink">I'm a really long link and I should be truncated.</a>
<span id="plainText">
Right clicking me when I'm selected should show the menu item.
</span>
<span id="mixedContent">
I'm some text, and <a href="http://mozilla.org">I'm a link!</a>
</span>
<span id="plainText">
Right clicking me when I'm selected should show the menu item.
</span>
<span id="mixedContent">
I'm some text, and <a href="http://mozilla.org">I'm a link!</a>
</span>
<a href="http://mozilla.org">A partial <span id="partialLink">link selection</span></a>
</body>
<a href="http://mozilla.org">A partial <span id="partialLink">link selection</span></a>
<span id="surrogatePair">
This character🔥 shouldn't be truncated.
</span>
</body>
</html>