From e2fa7ade5a540d8d53f27038278affbc5ef17336 Mon Sep 17 00:00:00 2001 From: datakurre Date: Wed, 11 Apr 2012 18:32:17 +0300 Subject: [PATCH 1/2] Fixed to not apply absolute prefix for relative urls starting with '#'. --- docs/HISTORY.txt | 3 +++ lib/diazo/rules.py | 9 ++++++++- lib/diazo/tests/absolute-prefix/output.html | 1 + lib/diazo/tests/absolute-prefix/theme.html | 1 + 4 files changed, 13 insertions(+), 1 deletion(-) diff --git a/docs/HISTORY.txt b/docs/HISTORY.txt index 9b22d07..7366f2e 100644 --- a/docs/HISTORY.txt +++ b/docs/HISTORY.txt @@ -1,6 +1,9 @@ History ======= +* Fixed to not apply absolute prefix for relative urls starting with '#'. + [datakurre] + 0.3 - 2010-05-29 ---------------- diff --git a/lib/diazo/rules.py b/lib/diazo/rules.py index c41537e..d603a62 100644 --- a/lib/diazo/rules.py +++ b/lib/diazo/rules.py @@ -11,7 +11,7 @@ from optparse import OptionParser from lxml import etree -from urlparse import urljoin +from urlparse import urljoin as urlparse_urljoin from diazo.cssrules import convert_css_selectors from diazo.utils import namespaces, fullname, AC_READ_NET, AC_READ_FILE, pkg_xsl, _createOptionParser @@ -31,6 +31,13 @@ apply_rules = pkg_xsl('apply-rules.xsl') fixup_themes = pkg_xsl('fixup-themes.xsl') +def urljoin(base, url, allow_fragments=True): + """Join the base with the url only when the url doesn't start with '#'""" + if url.startswith('#'): + return url + else: + return urlparse_urljoin(base, url, allow_fragments) + def add_identifiers(rules_doc): """Add identifiers to the rules for debugging""" for i, elem in enumerate(rules_doc.xpath( diff --git a/lib/diazo/tests/absolute-prefix/output.html b/lib/diazo/tests/absolute-prefix/output.html index 9462722..cac6a09 100644 --- a/lib/diazo/tests/absolute-prefix/output.html +++ b/lib/diazo/tests/absolute-prefix/output.html @@ -34,5 +34,6 @@ Link + Anchor diff --git a/lib/diazo/tests/absolute-prefix/theme.html b/lib/diazo/tests/absolute-prefix/theme.html index 264e65f..9139aba 100644 --- a/lib/diazo/tests/absolute-prefix/theme.html +++ b/lib/diazo/tests/absolute-prefix/theme.html @@ -34,5 +34,6 @@ Link + Anchor From 62cdcb7bb0c77bd3f4f63f9411706a899e7273e2 Mon Sep 17 00:00:00 2001 From: Asko Soukka Date: Wed, 11 Apr 2012 20:30:20 +0300 Subject: [PATCH 2/2] Refactored to not mix anchor_safe_urljoin with regular urljoin and use it only when necessary. --- lib/diazo/rules.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/diazo/rules.py b/lib/diazo/rules.py index d603a62..e2a3640 100644 --- a/lib/diazo/rules.py +++ b/lib/diazo/rules.py @@ -11,7 +11,7 @@ from optparse import OptionParser from lxml import etree -from urlparse import urljoin as urlparse_urljoin +from urlparse import urljoin from diazo.cssrules import convert_css_selectors from diazo.utils import namespaces, fullname, AC_READ_NET, AC_READ_FILE, pkg_xsl, _createOptionParser @@ -31,12 +31,12 @@ apply_rules = pkg_xsl('apply-rules.xsl') fixup_themes = pkg_xsl('fixup-themes.xsl') -def urljoin(base, url, allow_fragments=True): +def anchor_safe_urljoin(base, url): """Join the base with the url only when the url doesn't start with '#'""" if url.startswith('#'): return url else: - return urlparse_urljoin(base, url, allow_fragments) + return urljoin(base, url) def add_identifiers(rules_doc): """Add identifiers to the rules for debugging""" @@ -110,7 +110,7 @@ def apply_absolute_prefix(theme_doc, absolute_prefix): url = urljoin(absolute_prefix, node.get('src')) node.set('src', url) for node in theme_doc.xpath('//*[@href]'): - url = urljoin(absolute_prefix, node.get('href')) + url = anchor_safe_urljoin(absolute_prefix, node.get('href')) node.set('href', url) for node in theme_doc.xpath('//style'): node.text = IMPORT_STYLESHEET.sub(