Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow relative urls #47

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions precise_bbcode/bbcode/defaults/placeholder.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@ class UrlBBCodePlaceholder(BBCodePlaceholder):
name = 'url'

def validate(self, content, extra_context=None):
v = URLValidator()
try:
v(content)
except ValidationError:
if content[:2] == '//':
return False
if '://' in content:
v = URLValidator()
try:
v(content)
except ValidationError:
return False
return True


Expand Down
29 changes: 16 additions & 13 deletions precise_bbcode/bbcode/defaults/tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,19 +111,22 @@ def render(self, value, option=None, parent=None):
href = href[1:-1]
href = replace(href, bbcode_settings.BBCODE_ESCAPE_HTML)
if '://' not in href and self._domain_re.match(href):
href = 'http://' + href
v = URLValidator()

# Validates and renders the considered URL.
try:
v(href)
except ValidationError:
rendered = '[url={}]{}[/url]'.format(href, value) if option else \
'[url]{}[/url]'.format(value)
Comment on lines -121 to -122
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why remove this logic? This is going to introduce a change in behavior and I don't think this is necessary in order to allow relative URLs.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

¯\_(ツ)_/¯
Just felt this way.
I believe you're right, let's not change it too too much.

else:
content = value if option else href
rendered = '<a href="{}">{}</a>'.format(href, content or href)

href = 'https://' + href

if href[:2] == '//':
# Protocolless absolute URLs are unsafe.
href = ''

if '://' in href:
# Validates the considered URL only if it is not relative.
v = URLValidator()
try:
v(href)
except ValidationError:
href = ''

content = value if option else href
rendered = '<a href="{}">{}</a>'.format(href, content or href)
return rendered


Expand Down
Loading