1
0
mirror of https://github.com/django/django.git synced 2024-12-22 17:16:24 +00:00

Fixed #28688 -- Made admin's URLify.js skip removal of English words if non-ASCII chars are present.

This commit is contained in:
Sævar Öfjörð Magnússon 2017-10-09 10:40:32 +00:00 committed by Tim Graham
parent e8649ae368
commit f90be0a83e
2 changed files with 18 additions and 7 deletions

View File

@ -155,13 +155,19 @@
if (!allowUnicode) { if (!allowUnicode) {
s = downcode(s); s = downcode(s);
} }
var removelist = [ var hasUnicodeChars = /[^\u0000-\u007f]/.test(s);
"a", "an", "as", "at", "before", "but", "by", "for", "from", "is", // Remove English words only if the string contains ASCII (English)
"in", "into", "like", "of", "off", "on", "onto", "per", "since", // characters.
"than", "the", "this", "that", "to", "up", "via", "with" if (!hasUnicodeChars) {
]; var removeList = [
var r = new RegExp('\\b(' + removelist.join('|') + ')\\b', 'gi'); "a", "an", "as", "at", "before", "but", "by", "for", "from",
s = s.replace(r, ''); "is", "in", "into", "like", "of", "off", "on", "onto", "per",
"since", "than", "the", "this", "that", "to", "up", "via",
"with"
];
var r = new RegExp('\\b(' + removeList.join('|') + ')\\b', 'gi');
s = s.replace(r, '');
}
// if downcode doesn't hit, the char will be stripped here // if downcode doesn't hit, the char will be stripped here
if (allowUnicode) { if (allowUnicode) {
// Keep Unicode letters including both lowercase and uppercase // Keep Unicode letters including both lowercase and uppercase

View File

@ -23,3 +23,8 @@ QUnit.test('merge adjacent whitespace', function(assert) {
QUnit.test('trim trailing hyphens', function(assert) { QUnit.test('trim trailing hyphens', function(assert) {
assert.strictEqual(URLify('D silent always', 9, true), 'd-silent'); assert.strictEqual(URLify('D silent always', 9, true), 'd-silent');
}); });
QUnit.test('do not remove English words if the string contains non-ASCII', function(assert) {
// If removing English words wasn't skipped, the last 'a' would be removed.
assert.strictEqual(URLify('Kaupa-miða', 255, true), 'kaupa-miða');
});