Repository: plone/Products.CMFPlone

Issue:
add data-base-url attribute to HTML body tag in Plone 4
Since plone 4.3.12, the `<base href` attribute in HTML generated by Plone no longer always points to the context URL as it used to prior to the change. This change broke Plone and some add-ons. More breakage may still surface. Fixes have varied because no alternative was provided when the change was made.

For a lengthy background, see the [discussion](https://community.plone.org/t/how-to-get-context-url-in-js-on-plone-4-3-12/4031). 

Rather than rolling back the change which was done to support some other things and would require reverting them, I suggest providing a future-proof alternative (thanks @rodfersou for suggesting using a new attribute):

Plone 5 has removed `<base href` completely. Instead Plone 5 has added a `data-base-url` attribute to the HTML `body` tag. Which points to the context URL.

So, I suggest same be done for Plone 4. That way, anything in Plone core and/or add-ons needing context URL in Javascript have a future-proof way of getting it from here on.


@@sharing is broken on Page objects in Plone 4.3.12 and 4.3.14
## BUG/PROBLEM REPORT (OR OTHER COMMON ISSUE)

### What I did:

1. Create vanilla Plone 4.3.14 site.
2. Add private Page with title Test at the top of the site
3. Navigate to sharing tab for that page
4. Type some characters into the Search box
5. Kaboom: exception

### What I expect to happen:

List of potential users as search results

### What actually happened:

Large python back trace because the search form AJAX accessed:

  http://localhost:8080/Plone2/test/@@sharing/@@updateSharingInfo

rather than the following used by Plone 4.3.11:

  http://localhost:8080/Plone2/test/@@updateSharingInfo
 
The root cause appears to be:

https://pypi.python.org/pypi/plone.app.layout/2.3.17

  2.3.15 (2016-06-28)
   Fixes:
   _Fix base tag differs from actual URL (fixes [86](https://github.com/plone/plone.app.layout/issues/86)). [rodfersou]_

which was actually made **after** plone.app.layout 2.3.15 was released, (December 2016): that comment is placed incorrectly in the README file. I'm happy to make a bug report there as well.

### What version of Plone/ Addons I am using:

Vanilla Plone 4.3.14, which uses  plone.app.layout 2.3.17. So does Plone 4.3.12, and I see exactly the same problem there.

(NB: Pinning plone.app.layout 2.3.15 in Plone 4.3.14 resolves the problem).

Update: appears to be the same issue discussed in: https://github.com/plone/Products.CMFPlone/issues/2051



Discussion:

@dpc22 the problem with changelog happen that the code review take too much time to merge the pull request, and one release was done in the middle time.

Of course I didn't realize it when I rebase the branch.. my mistake.

This change was merged at version `2.3.16`, not `2.3.15` and I'll fix the changelog in the sequence.
Jenkins job http://jenkins.plone.org/job/pull-request-4.3/13/console
Did some changes thinking better on how URL is formed.

New jenkins job http://jenkins.plone.org/job/pull-request-4.3/14/console
@hvelarde green \o/
This one can be closed after the merge

Resolving patch:
diff --git a/Products/CMFPlone/browser/jsvariables.py b/Products/CMFPlone/browser/jsvariables.py
--- a/Products/CMFPlone/browser/jsvariables.py
+++ b/Products/CMFPlone/browser/jsvariables.py
@@ -7,6 +7,7 @@
 
 TEMPLATE = """\
 var portal_url = '%(portal_url)s';
+var base_url = '%(base_url)s';
 var form_modified_message = '%(form_modified)s';
 var form_resubmit_message = '%(form_resubmit)s';
 var external_links_open_new_window = '%(open_links)s';
@@ -36,6 +37,7 @@ def __call__(self, *args, **kwargs):
 
         props = getToolByName(context, 'portal_properties').site_properties
         portal_url = getToolByName(context, 'portal_url')()
+        base_url = self.request['HTTP_REFERER']
 
         # the following are flags for mark_special_links.js
         # links get the target="_blank" attribute
@@ -54,6 +56,7 @@ def __call__(self, *args, **kwargs):
 
         return TEMPLATE % dict(
             portal_url=portal_url,
+            base_url=base_url,
             open_links=open_links,
             mark_links=mark_links,
             form_modified=form_modified,


Test patch:
diff --git a/Products/CMFPlone/tests/browser.txt b/Products/CMFPlone/tests/browser.txt
--- a/Products/CMFPlone/tests/browser.txt
+++ b/Products/CMFPlone/tests/browser.txt
@@ -13,10 +13,10 @@ Test for "template id" in <body> tag. See tickets #9111/#8777
 
     >>> browser.open('http://nohost/plone/front-page')
     >>> browser.contents
-    '...<body class="template-document_view portaltype-document site-plone section-front-page...'
+    '...<body data-portal-url="http://nohost/plone" class="template-document_view portaltype-document site-plone section-front-page...'
     >>> browser.open('http://nohost/plone/news')
     >>> browser.contents
-    '...<body class="template-folder_summary_view portaltype-collection site-plone section-news...'
+    '...<body data-portal-url="http://nohost/plone" class="template-folder_summary_view portaltype-collection site-plone section-news...'
 
 Login
 
@@ -29,4 +29,4 @@ Test explicit browser view
 
     >>> browser.open('http://nohost/plone/@@dashboard')
     >>> browser.contents
-    '...<body class="template-dashboard portaltype-plone-site site-plone...'
+    '...<body data-portal-url="http://nohost/plone" class="template-dashboard portaltype-plone-site site-plone...'