Fix username/mention regexes

* Allow underscore instead of hypen
* Fix domain part handling
* Add tests for remote mention
This commit is contained in:
unarist 2018-04-09 01:10:04 +09:00
parent 48023998a4
commit 9b58a541b1
6 changed files with 17 additions and 7 deletions

View file

@ -76,7 +76,7 @@ export default Vue.extend({
} }
const err = const err =
!this.username.match(/^[a-zA-Z0-9\-]+$/) ? 'invalid-format' : !this.username.match(/^[a-zA-Z0-9_]+$/) ? 'invalid-format' :
this.username.length < 3 ? 'min-range' : this.username.length < 3 ? 'min-range' :
this.username.length > 20 ? 'max-range' : this.username.length > 20 ? 'max-range' :
null; null;

View file

@ -65,7 +65,7 @@ export default Vue.extend({
} }
const err = const err =
!this.nid.match(/^[a-zA-Z0-9\-]+$/) ? 'invalid-format' : !this.nid.match(/^[a-zA-Z0-9_]+$/) ? 'invalid-format' :
this.nid.length < 3 ? 'min-range' : this.nid.length < 3 ? 'min-range' :
this.nid.length > 30 ? 'max-range' : this.nid.length > 30 ? 'max-range' :
null; null;

View file

@ -24,7 +24,7 @@ export type IApp = {
}; };
export function isValidNameId(nameId: string): boolean { export function isValidNameId(nameId: string): boolean {
return typeof nameId == 'string' && /^[a-zA-Z0-9\-]{3,30}$/.test(nameId); return typeof nameId == 'string' && /^[a-zA-Z0-9_]{3,30}$/.test(nameId);
} }
/** /**

View file

@ -89,7 +89,7 @@ export const isRemoteUser = (user: any): user is IRemoteUser =>
//#region Validators //#region Validators
export function validateUsername(username: string): boolean { export function validateUsername(username: string): boolean {
return typeof username == 'string' && /^[a-zA-Z0-9\-]{3,20}$/.test(username); return typeof username == 'string' && /^[a-zA-Z0-9_]{3,20}$/.test(username);
} }
export function validatePassword(password: string): boolean { export function validatePassword(password: string): boolean {

View file

@ -4,7 +4,7 @@
import parseAcct from '../../../acct/parse'; import parseAcct from '../../../acct/parse';
module.exports = text => { module.exports = text => {
const match = text.match(/^(?:@[a-zA-Z0-9\-]+){1,2}/); const match = text.match(/^@[a-z0-9_]+(?:@[a-z0-9\.\-]+[a-z0-9])?/i);
if (!match) return null; if (!match) return null;
const mention = match[0]; const mention = match[0];
const { username, host } = parseAcct(mention.substr(1)); const { username, host } = parseAcct(mention.substr(1));

View file

@ -9,9 +9,11 @@ const syntaxhighlighter = require('../built/text/parse/core/syntax-highlighter')
describe('Text', () => { describe('Text', () => {
it('can be analyzed', () => { it('can be analyzed', () => {
const tokens = analyze('@himawari お腹ペコい :cat: #yryr'); const tokens = analyze('@himawari @hima_sub@namori.net お腹ペコい :cat: #yryr');
assert.deepEqual([ assert.deepEqual([
{ type: 'mention', content: '@himawari', username: 'himawari', host: null }, { type: 'mention', content: '@himawari', username: 'himawari', host: null },
{ type: 'text', content: ' '},
{ type: 'mention', content: '@hima_sub@namori.net', username: 'hima_sub', host: 'namori.net' },
{ type: 'text', content: ' お腹ペコい ' }, { type: 'text', content: ' お腹ペコい ' },
{ type: 'emoji', content: ':cat:', emoji: 'cat'}, { type: 'emoji', content: ':cat:', emoji: 'cat'},
{ type: 'text', content: ' '}, { type: 'text', content: ' '},
@ -20,7 +22,7 @@ describe('Text', () => {
}); });
it('can be inverted', () => { it('can be inverted', () => {
const text = '@himawari お腹ペコい :cat: #yryr'; const text = '@himawari @hima_sub@namori.net お腹ペコい :cat: #yryr';
assert.equal(analyze(text).map(x => x.content).join(''), text); assert.equal(analyze(text).map(x => x.content).join(''), text);
}); });
@ -41,6 +43,14 @@ describe('Text', () => {
], tokens); ], tokens);
}); });
it('remote mention', () => {
const tokens = analyze('@hima_sub@namori.net お腹ペコい');
assert.deepEqual([
{ type: 'mention', content: '@hima_sub@namori.net', username: 'hima_sub', host: 'namori.net' },
{ type: 'text', content: ' お腹ペコい' }
], tokens);
});
it('hashtag', () => { it('hashtag', () => {
const tokens = analyze('Strawberry Pasta #alice'); const tokens = analyze('Strawberry Pasta #alice');
assert.deepEqual([ assert.deepEqual([