WICKET-6557 Allow meta tags to be contributed during AJAX request

this closes #279


Project: http://git-wip-us.apache.org/repos/asf/wicket/repo
Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/c4d2cb35
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/c4d2cb35
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/c4d2cb35

Branch: refs/heads/master
Commit: c4d2cb35bb3e4d5dcdcb130fcb6839f6b88501f0
Parents: c3f2da7
Author: Dennis Hoersch <dennishoer...@web.de>
Authored: Mon May 28 13:16:44 2018 +0200
Committer: Sven Meier <svenme...@apache.org>
Committed: Tue May 29 17:12:52 2018 +0200

----------------------------------------------------------------------
 .../wicket/ajax/res/js/wicket-ajax-jquery.js    | 22 +++++++++++
 wicket-core/src/test/js/head.js                 | 39 ++++++++++++++++++++
 2 files changed, 61 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/c4d2cb35/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 eea2c18..6a3a45b 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
@@ -1893,6 +1893,8 @@
                                                                
this.processScript(context, node);
                                                        } else if (name === 
"style") {
                                                                
this.processStyle(context, node);
+                                                       } else if (name === 
"meta") {
+                                                               
this.processMeta(context, node);
                                                        }
                                                } else if (node.nodeType === 8) 
{ // comment type
                                                        
this.processComment(context, node);
@@ -2082,6 +2084,26 @@
                                        });
                                },
 
+                               processMeta: function (context, node) {
+                                       context.steps.push(function (notify) {
+                                               var meta = 
Wicket.Head.createElement("meta"),
+                                                       $meta = jQuery(meta),
+                                                       attrs = 
jQuery(node).prop("attributes"),
+                                                       name = 
node.getAttribute("name");
+
+                                               if(name) {
+                                                       jQuery('meta[name="' + 
name + '"]').remove();
+                                               }
+                                               jQuery.each(attrs, function() {
+                                                       $meta.attr(this.name, 
this.value);
+                                               });
+
+                                               Wicket.Head.addElement(meta);
+
+                                               return FunctionsExecuter.DONE;
+                                       });
+                               },
+
                                // process (conditional) comments
                                processComment: function (context, node) {
                                        context.steps.push(function (notify) {

http://git-wip-us.apache.org/repos/asf/wicket/blob/c4d2cb35/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 9966749..71710db 100644
--- a/wicket-core/src/test/js/head.js
+++ b/wicket-core/src/test/js/head.js
@@ -267,6 +267,45 @@ jQuery(document).ready(function() {
                equal(baseUrlText, '/**/Wicket.Ajax.baseUrl=\"clock\";/**/', 
"Wicket.Ajax.baseUrl must be the third item's content");
        });
 
+       module('Wicket.Head.Contributor.processContribution');
+
+       test('Wicket.Head.Contributor.processContribution - can add meta tags', 
function() {
+               var process = function(contribution) {
+                       var doc = 
Wicket.Xml.parse('<header-contribution><![CDATA[<head>' + contribution + 
'</head>]]></header-contribution>').documentElement,
+                               context = {
+                                       steps: []
+                               };
+                               
Wicket.Head.Contributor.processContribution(context, doc);
+                               return context;
+                       },
+                       metaTags = function(name) {
+                               return jQuery('head meta[name=' + name + ']');
+                       };
+
+               var context1 = process('<meta name="m1" content="c1" />');
+               equal(context1.steps.length, 1, "There must be 1 steps to be 
executed.");
+               equal(metaTags("m1").length, 0, "There must be no meta tag 
before the first contribution.");
+               context1.steps[0]();
+               equal(metaTags("m1").length, 1, "There must be one meta tag 
after the first contribution.");
+               equal(metaTags("m1").attr("content"), "c1", "The meta tag must 
have the content as requested.");
+
+               var context2 = process('<meta name="m1" content="c1_1" />');
+               equal(context2.steps.length, 1, "There must be 1 steps to be 
executed.");
+               equal(metaTags("m1").length, 1, "There must be one old meta tag 
before the second contribution.");
+               context2.steps[0]();
+               equal(metaTags("m1").length, 1, "There must be one meta tag 
after the second contribution.");
+               equal(metaTags("m1").attr("content"), "c1_1", "The meta tag 
must have the content as requested.");
+
+               var context3 = process('<meta name="m2" content="c2" />');
+               equal(context3.steps.length, 1, "There must be 1 steps to be 
executed.");
+               equal(metaTags("m2").length, 0, "There must be no meta tag 
before the third contribution.");
+               context3.steps[0]();
+               equal(metaTags("m2").length, 1, "There must be one new meta tag 
after the third contribution.");
+               equal(metaTags("m2").attr("content"), "c2", "The meta tag must 
have the content as requested.");
+               equal(metaTags("m1").length, 1, "There must be still the old 
meta tag after the third contribution.");
+               equal(metaTags("m1").attr("content"), "c1_1", "The meta tag 
must still have the content as requested.");
+       });
+
        /**
         * Wicket.Head.Contributor.processXYZ method will be tested in ajax.js
         */

Reply via email to