Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

Commit

Permalink
Decode IDNA in PreviewCard (mastodon#2781)
Browse files Browse the repository at this point in the history
  • Loading branch information
ykzts authored and Gargron committed May 4, 2017
1 parent e37e84d commit e95983f
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 7 deletions.
18 changes: 11 additions & 7 deletions app/javascript/mastodon/features/status/components/card.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import React from 'react';
import ImmutablePropTypes from 'react-immutable-proptypes';
import punycode from 'punycode'

const hostStyle = {
display: 'block',
marginTop: '5px',
fontSize: '13px'
};
const IDNA_PREFIX = 'xn--';

const decodeIDNA = domain => {
return domain
.split('.')
.map(part => part.indexOf(IDNA_PREFIX) === 0 ? punycode.decode(part.slice(IDNA_PREFIX.length)) : part)
.join('.');
}

const getHostname = url => {
const parser = document.createElement('a');
Expand All @@ -30,7 +34,7 @@ class Card extends React.PureComponent {
}

if (provider.length < 1) {
provider = getHostname(card.get('url'))
provider = decodeIDNA(getHostname(card.get('url')));
}

return (
Expand All @@ -40,7 +44,7 @@ class Card extends React.PureComponent {
<div className='status-card__content'>
<strong className='status-card__title' title={card.get('title')}>{card.get('title')}</strong>
<p className='status-card__description'>{(card.get('description') || '').substring(0, 50)}</p>
<span className='status-card__host' style={hostStyle}>{provider}</span>
<span className='status-card__host'>{provider}</span>
</div>
</a>
);
Expand Down
6 changes: 6 additions & 0 deletions app/javascript/styles/components.scss
Original file line number Diff line number Diff line change
Expand Up @@ -1818,6 +1818,12 @@ button.icon-button.active i.fa-retweet {
color: $color3;
}

.status-card__host {
display: block;
margin-top: 5px;
font-size: 13px;
}

.status-card__image {
flex: 0 0 100px;
background: lighten($color1, 8%);
Expand Down
1 change: 1 addition & 0 deletions app/services/fetch_link_card_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def call(status)

return if url.nil?

url = Addressable::URI.parse(url).normalize.to_s
card = PreviewCard.where(status: status).first_or_initialize(status: status, url: url)
attempt_opengraph(card, url) unless attempt_oembed(card, url)
end
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"postcss-smart-import": "^0.6.12",
"precss": "^1.4.0",
"prop-types": "^15.5.8",
"punycode": "^2.1.0",
"rails-erb-loader": "^5.0.0",
"react": "^15.5.4",
"react-addons-perf": "^15.4.2",
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5343,6 +5343,10 @@ punycode@^1.2.4, punycode@^1.4.1:
version "1.4.1"
resolved "https://registry.yarnpkg.com/punycode/-/punycode-1.4.1.tgz#c0d5a63b2718800ad8e1eb0fa5269c84dd41845e"

punycode@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.1.0.tgz#5f863edc89b96db09074bad7947bf09056ca4e7d"

q@^1.1.2:
version "1.4.1"
resolved "https://registry.yarnpkg.com/q/-/q-1.4.1.tgz#55705bcd93c5f3673530c2c2cbc0c2b3addc286e"
Expand Down

0 comments on commit e95983f

Please sign in to comment.