From 535ad084389550874ffcee214fd7175146b550d9 Mon Sep 17 00:00:00 2001 From: Nicolas Constant Date: Mon, 29 Jul 2019 18:59:12 -0400 Subject: [PATCH] fix links count --- .../create-status.component.spec.ts | 28 +++++++++++++++++-- .../create-status/create-status.component.ts | 15 +++++++++- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/src/app/components/create-status/create-status.component.spec.ts b/src/app/components/create-status/create-status.component.spec.ts index 88a39783..4c79d7a5 100644 --- a/src/app/components/create-status/create-status.component.spec.ts +++ b/src/app/components/create-status/create-status.component.spec.ts @@ -72,13 +72,35 @@ describe('CreateStatusComponent', () => { expect((component).charCountLeft).toBe(466); }); + it('should not count https link more than the minimum', () => { + const status = "https://www.joelonsoftware.com/2003/10/08/the-absolute-minimum-every-software-developer-absolutely-positively-must-know-about-unicode-and-character-sets-no-excuses/"; + (component).maxCharLength = 500; + (component).countStatusChar(status); + expect((component).charCountLeft).toBe(477); + }); + + it('should not count http link more than the minimum', () => { + const status = "http://www.joelonsoftware.com/2003/10/08/the-absolute-minimum-every-software-developer-absolutely-positively-must-know-about-unicode-and-character-sets-no-excuses/"; + (component).maxCharLength = 500; + (component).countStatusChar(status); + expect((component).charCountLeft).toBe(477); + }); + + + it('should not count links more than the minimum', () => { + const status = "http://www.joelonsoftware.com/2003/10/08/the-absolute-minimum-every-software-developer-absolutely-positively-must-know-about-unicode-and-character-sets-no-excuses/ http://www.joelonsoftware.com/2003/10/08/the-absolute-minimum-every-software-developer-absolutely-positively-must-know-about-unicode-and-character-sets-no-excuses/ http://www.joelonsoftware.com/2003/10/08/the-absolute-minimum-every-software-developer-absolutely-positively-must-know-about-unicode-and-character-sets-no-excuses/"; + (component).maxCharLength = 500; + (component).countStatusChar(status); + expect((component).charCountLeft).toBe(429); + }); + it('should count correctly complex status', () => { - const status = 'dsqdqs @NicolasConstant@mastodon.partipirate.org dsqdqs👇😱 😶 status #Pleroma with 😱 😶 emojis 😏 👍 #Mastodon @ddqsdqs @dsqdsq@dqsdsqqdsq'; + const status = 'dsqdqs @NicolasConstant@mastodon.partipirate.org dsqdqs👇😱 😶 status https://www.joelonsoftware.com/2003/10/08/the-absolute-minimum-every-software-developer-absolutely-positively-must-know-about-unicode-and-character-sets-no-excuses/ #Pleroma with 😱 😶 emojis 😏 👍 #Mastodon @ddqsdqs @dsqdsq@dqsdsqqdsq'; (component).title = '🙂 test'; (component).maxCharLength = 500; (component).countStatusChar(status); - expect((component).charCountLeft).toBe(397); - }); + expect((component).charCountLeft).toBe(373); + }); it('should not parse small status', () => { const status = 'this is a cool status'; diff --git a/src/app/components/create-status/create-status.component.ts b/src/app/components/create-status/create-status.component.ts index b67178e1..a4a6837e 100644 --- a/src/app/components/create-status/create-status.component.ts +++ b/src/app/components/create-status/create-status.component.ts @@ -355,8 +355,9 @@ export class CreateStatusComponent implements OnInit, OnDestroy { const currentStatus = parseStatus[parseStatus.length - 1]; const statusExtraChars = this.getMentionExtraChars(status); + const linksExtraChars = this.getLinksExtraChars(status); - const statusLength = [...currentStatus].length - statusExtraChars; + const statusLength = [...currentStatus].length - statusExtraChars - linksExtraChars; this.charCountLeft = this.maxCharLength - statusLength - this.getCwLength(); this.postCounts = parseStatus.length; } @@ -506,6 +507,18 @@ export class CreateStatusComponent implements OnInit, OnDestroy { return results; } + private getLinksExtraChars(status: string): number { + let mentionExtraChars = 0; + let links = status.split(' ').filter(x => x.startsWith('http://') || x.startsWith('https://')); + for (let link of links) { + if(link.length > 23){ + mentionExtraChars += link.length - 23; + } + } + + return mentionExtraChars; + } + private getMentionExtraChars(status: string): number { let mentionExtraChars = 0; let mentions = this.getMentionsFromStatus(status);