Utils.ellipsify(): Now allows the ellipsis to be specified, has test, and (actually) truncates to the given length.

This commit is contained in:
satyr 2010-03-30 22:30:48 +09:00
Родитель 42f1bfcdbe
Коммит 524ef8ed14
2 изменённых файлов: 39 добавлений и 30 удалений

Просмотреть файл

@ -265,41 +265,35 @@ function encodeJson(object) Utils.json.encode(object);
// **//Deprecated.//** Use {{{JSON.parse()}}} instead.
function decodeJson(string) Utils.json.decode(string);
// === {{{Utils.ellipsify(node, characters)}}} ===
// === {{{Utils.ellipsify(node, characters, ellipsis = "\u2026")}}} ===
// Given a DOM {{{node}}} (or string) and a maximum number of {{{characters}}},
// returns a new DOM node or string that has the same contents truncated to
// that number of characters. If any truncation was performed, an ellipsis is
// placed at the end of the content.
// that number of characters. If any truncation was performed,
// an {{{ellipsis}}} is placed at the end of the content.
function ellipsify(node, chars) {
function ellipsify(node, chars, ellipsis) {
ellipsis = ellipsis || "\u2026";
var doc = node.ownerDocument;
if (!doc) {
var str = String(node);
return str.length >= chars ? str.slice(0, chars) + "\u2026" : str;
if (chars < 1) return "";
let str = String(node);
return str.length > chars ? str.slice(0, chars - 1) + ellipsis : str;
}
var copy = node.cloneNode(false);
if (node.hasChildNodes()) {
var children = node.childNodes;
for (var i = 0; i < children.length && chars > 0; i++) {
var childNode = children[i];
var childCopy;
if (childNode.nodeType === childNode.TEXT_NODE) {
var value = childNode.nodeValue;
if (value.length >= chars) {
childCopy = doc.createTextNode(value.slice(0, chars) + "\u2026");
chars = 0;
}
else {
childCopy = childNode.cloneNode(false);
chars -= value.length;
}
}
else if (childNode.nodeType === childNode.ELEMENT_NODE) {
childCopy = ellipsify(childNode, chars);
chars -= childCopy.textContent.length;
}
copy.appendChild(childCopy);
var {firstChild: childNode, TEXT_NODE, ELEMENT_NODE} = node;
var copy = node.cloneNode(false), childCopy;
for (; childNode && chars > 0; childNode = childNode.nextSibling) {
if (childNode.nodeType === TEXT_NODE) {
let value = childNode.nodeValue;
childCopy = (value.length > chars
? doc.createTextNode(value.slice(0, chars - 1) + ellipsis)
: childNode.cloneNode(false));
chars -= value.length;
}
else if (childNode.nodeType === ELEMENT_NODE) {
childCopy = ellipsify(childNode, chars, ellipsis);
chars -= childCopy.textContent.length;
}
copy.appendChild(childCopy);
}
return copy;
}

Просмотреть файл

@ -57,8 +57,6 @@ Cu.import("resource://ubiquity/tests/test_parser1.js");
Cu.import("resource://ubiquity/tests/test_parser2.js");
Cu.import("resource://ubiquity/tests/testing_stubs.js");
var NLParser = NLParserMaker(1);
var globalObj = this;
function testXhtmlCodeSourceWorks() {
@ -621,6 +619,23 @@ function testUtilsEscapeUnescapeHtml() {
unescapeHtml(unescapeHtml(escapeHtml(escapeHtml(html)))));
}
function testUtilsEllipsify() {
this.skipIfXPCShell();
this.assertEquals(Utils.ellipsify("12345", 4), "123\u2026");
this.assertEquals(Utils.ellipsify("just an ellipsis", 1, "..."), "...");
this.assertEquals(Utils.ellipsify("nothing to ellipsify", 0), "");
var doc = Utils.hiddenWindow.document;
var div = doc.createElementNS("http://www.w3.org/1999/xhtml", "div");
div.appendChild(doc.createTextNode("qwerty"));
for (let i = 3; i --> 0;) div.appendChild(div.cloneNode(true));
for (let i = div.textContent.length + 1; i --> 0; i -= 12) {
let {length} = Utils.ellipsify(div, i).textContent;
this.assert(length <= i, length + " <= " + i);
}
}
function testL10nUtilsPropertySelector() {
var ps = LocalizationUtils.propertySelector("data:," + encodeURI(<![CDATA[
foo=%S %S