You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by mg...@apache.org on 2013/02/21 12:36:14 UTC
[2/2] git commit: WICKET-5047 Wicket Ajax: Inline script header
contribution issue
Updated Branches:
refs/heads/master e8fca2405 -> dfcafea9f
WICKET-5047 Wicket Ajax: Inline script header contribution issue
Project: http://git-wip-us.apache.org/repos/asf/wicket/repo
Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/dfcafea9
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/dfcafea9
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/dfcafea9
Branch: refs/heads/master
Commit: dfcafea9fa30c6c039f2d5802363830ed9576e2b
Parents: 1eb424f
Author: Martin Tzvetanov Grigorov <mg...@apache.org>
Authored: Thu Feb 21 13:35:50 2013 +0200
Committer: Martin Tzvetanov Grigorov <mg...@apache.org>
Committed: Thu Feb 21 13:35:50 2013 +0200
----------------------------------------------------------------------
.../wicket/ajax/res/js/wicket-ajax-jquery.js | 15 +++++-
wicket-core/src/test/js/ajax.js | 36 +++++++++++++++
.../src/test/js/data/ajax/javaScriptTemplate.xml | 18 +++++++
wicket-core/src/test/js/head.js | 9 ++--
4 files changed, 71 insertions(+), 7 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/wicket/blob/dfcafea9/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/wicket-ajax-jquery.js
----------------------------------------------------------------------
diff --git a/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/wicket-ajax-jquery.js b/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/wicket-ajax-jquery.js
index df32e12..84b9f96 100644
--- a/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/wicket-ajax-jquery.js
+++ b/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/wicket-ajax-jquery.js
@@ -1979,6 +1979,11 @@
var id = node.getAttribute("id");
+ var type = node.getAttribute("type");
+ if (!type || type.toLowerCase() === "text/javascript") {
+ text = 'try{'+text+'}catch(e){Wicket.Log.error(e);}';
+ }
+
if (typeof(id) === "string" && id.length > 0) {
// add javascript to document head
Wicket.Head.addJavascript(text, id);
@@ -2070,7 +2075,6 @@
// also a src value. Therefore we put the url to the src_ (notice the underscore) attribute.
// Wicket.Head.containsElement is aware of that and takes also the underscored attributes into account.
addJavascript: function (content, id, fakeSrc) {
- content = 'try{'+content+'}catch(e){Wicket.Log.error(e);}';
var script = Wicket.Head.createElement("script");
if (id) {
script.id = id;
@@ -2092,11 +2096,14 @@
addJavascripts: function (element, contentFilter) {
function add(element) {
var src = element.getAttribute("src");
+ var type = element.getAttribute("type");
// if it is a reference, just add it to head
if (src !== null && src.length > 0) {
var e = document.createElement("script");
- e.setAttribute("type","text/javascript");
+ if (type) {
+ e.setAttribute("type",type);
+ }
e.setAttribute("src", src);
Wicket.Head.addElement(e);
} else {
@@ -2109,6 +2116,10 @@
content = contentFilter(content);
}
+ if (!type || type.toLowerCase() === "text/javascript") {
+ content = 'try{'+content+'}catch(e){Wicket.Log.error(e);}';
+ }
+
Wicket.Head.addJavascript(content, element.id);
}
}
http://git-wip-us.apache.org/repos/asf/wicket/blob/dfcafea9/wicket-core/src/test/js/ajax.js
----------------------------------------------------------------------
diff --git a/wicket-core/src/test/js/ajax.js b/wicket-core/src/test/js/ajax.js
index f603abc..e49b5d6 100644
--- a/wicket-core/src/test/js/ajax.js
+++ b/wicket-core/src/test/js/ajax.js
@@ -1113,5 +1113,41 @@ jQuery(document).ready(function() {
jQuery('#usedAsContextWicket5025').triggerHandler("asContextFailure");
});
+
+ /**
+ * https://issues.apache.org/jira/browse/WICKET-5047
+ */
+ asyncTest('try/catch only the content of \'script type="text/javascript"\'.', function () {
+
+ // manually call HTMLScriptElement.onload() to let
+ // FunctionsExecutor finish its work
+ var oldAddElement = Wicket.Head.addElement;
+ Wicket.Head.addElement = function(element) {
+ oldAddElement(element);
+ if (element.onload) {
+ element.onload();
+ }
+ };
+
+ expect(2);
+
+ var attrs = {
+ u: 'data/ajax/javaScriptTemplate.xml',
+ coh: [
+ function() {
+ start();
+
+ var jsTemplateText = jQuery('#jsTemplate').text();
+ equal(jsTemplateText, 'var data = 123;', 'JavaScript template is *not* try/catched');
+
+ var jsNonTemplateText = jQuery('#jsNonTemplate').text();
+ equal(jsNonTemplateText, 'try{var data = 456;}catch(e){Wicket.Log.error(e);}', 'JavaScript non template *is* try/catched');
+
+ }
+ ]
+ };
+
+ Wicket.Ajax.ajax(attrs);
+ });
}
});
http://git-wip-us.apache.org/repos/asf/wicket/blob/dfcafea9/wicket-core/src/test/js/data/ajax/javaScriptTemplate.xml
----------------------------------------------------------------------
diff --git a/wicket-core/src/test/js/data/ajax/javaScriptTemplate.xml b/wicket-core/src/test/js/data/ajax/javaScriptTemplate.xml
new file mode 100644
index 0000000..0865ead
--- /dev/null
+++ b/wicket-core/src/test/js/data/ajax/javaScriptTemplate.xml
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<ajax-response><header-contribution encoding="wicket1"><![CDATA[<head><script type="text/x-jquery-tmpl" id="jsTemplate">var data = 123;</script><script type="text/javascript" id="jsNonTemplate">var data = 456;</script></head>]]></header-contribution></ajax-response>
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/wicket/blob/dfcafea9/wicket-core/src/test/js/head.js
----------------------------------------------------------------------
diff --git a/wicket-core/src/test/js/head.js b/wicket-core/src/test/js/head.js
index 7a2a711..1cfe303 100644
--- a/wicket-core/src/test/js/head.js
+++ b/wicket-core/src/test/js/head.js
@@ -146,20 +146,19 @@ jQuery(document).ready(function() {
test('Wicket.Head.addJavascripts - no script tags', function() {
var $element = jQuery('<div>DIV TEXT<span>SPAN TEXT<a href="#anchor">ANCHOR</a></span></div>'),
- initialHeadElementsNumber = jQuery('head').children().length;
-
+ initialHeadElementsNumber = jQuery('head').children().length;
+
Wicket.Head.addJavascripts($element[0]);
equal(initialHeadElementsNumber, jQuery('head').children().length, 'No script elements in the added element, so nothing is added');
});
-
test('Wicket.Head.addJavascripts - direct script tag', function() {
expect(2);
var $element = jQuery('<script>ok(true);</script>'),
- initialHeadElementsNumber = jQuery('head').children().length;
-
+ initialHeadElementsNumber = jQuery('head').children().length;
+
Wicket.Head.addJavascripts($element[0]);
equal(jQuery('head').children().length, initialHeadElementsNumber + 1, 'A script element must be added');