Skip to content
This repository was archived by the owner on Oct 8, 2021. It is now read-only.

Commit 516b791

Browse files
author
Gabriel Schulhof
committed
Textinput: Do not concatenate clearBtnText value into HTML snippet
clearBtnText must be assigned via .attr( "title" ) to the title and via .text() to the contents of the clear button anchor. Do not append clear button to textarea elements. (cherry picked from commit 5197c10) Closes gh-7604 Fixes gh-7603 Fixes gh-7605
1 parent b0b3495 commit 516b791

File tree

3 files changed

+36
-7
lines changed

3 files changed

+36
-7
lines changed

js/widgets/forms/clearButton.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,21 @@ define( [
2020
_create: function() {
2121
this._super();
2222

23-
if ( !!this.options.clearBtn || this.isSearch ) {
23+
if ( this.isSearch ) {
24+
this.options.clearBtn = true;
25+
}
26+
27+
if ( !!this.options.clearBtn && this.inputNeedsWrap ) {
2428
this._addClearBtn();
2529
}
2630
},
2731

2832
clearButton: function() {
29-
30-
return $( "<a href='#' class='ui-input-clear ui-btn ui-icon-delete ui-btn-icon-notext ui-corner-all" +
31-
"' title='" + this.options.clearBtnText + "'>" + this.options.clearBtnText + "</a>" );
32-
33+
return $( "<a href='#' " +
34+
"class='ui-input-clear ui-btn ui-icon-delete ui-btn-icon-notext ui-corner-all'>" +
35+
"</a>" )
36+
.attr( "title", this.options.clearBtnText )
37+
.text( this.options.clearBtnText );
3338
},
3439

3540
_clearBtnClick: function( event ) {

tests/unit/textinput/index.html

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<script src="../../../js/jquery.tag.inserter.js"></script>
1111
<script src="../../../external/qunit/qunit.js"></script>
1212
<script src="../../../tests/jquery.testHelper.js"></script>
13+
<script src="../../../tests/jquery.setNameSpace.js"></script>
1314
<script>
1415
$.testHelper.asyncLoad([
1516
[ "widgets/forms/textinput"],["widgets/forms/clearButton"],["widgets/forms/autogrow" ],
@@ -58,14 +59,17 @@
5859

5960
<input type="text" id="text-input">
6061

61-
<input type="text" data-clear-btn="true" id="text-input-clear-btn">
62+
<input type="text" data-nstest-clear-btn="true" id="text-input-clear-btn">
6263

63-
<textarea data-clear-btn="true" id="textarea-clear-btn"></textarea>
64+
<textarea data-nstest-clear-btn="true" id="textarea-clear-btn"></textarea>
6465

6566
<input id="test-clear-btn-option"></input>
6667

68+
<input id="slider-input" type="number" data-nstest-type="range" min="0" max="93" step="1" value="17" data-nstest-clear-btn="true">
69+
6770
<input type="text" id="focus-class-test-for-input"></input>
6871
<textarea id="focus-class-test-for-textarea"></textarea>
72+
<input id="injection-test" data-nstest-clear-btn="true" data-nstest-clear-btn-text="'>a<script>$.clearBtnTextScriptInjected = true;</script>bc</a><a'">
6973
</div>
7074
</body>
7175
</html>

tests/unit/textinput/textinput_core.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,21 @@
104104
ok( ! $( "#textarea-clear-btn" ).next().is( "a.ui-input-clear" ), "data-clear-btn does not add clear button to textarea" );
105105
});
106106

107+
test( "data-clear-btn does not add clear button to textarea", function() {
108+
deepEqual( $( "#textarea-clear-btn" ).children( "a" ).length, 0,
109+
"No anchors have been inserted as children of the data-clear-btn textarea element" );
110+
});
111+
112+
test( "data-clear-btn does not add clear button to slider input", function() {
113+
ok( ! $( "#slider-input" ).next().is( "a.ui-input-clear" ),
114+
"data-clear-btn does not add clear button to slider input" );
115+
});
116+
117+
test( "data-clear-btn does not add clear button to slider input", function() {
118+
deepEqual( $( "#slider-input" ).children( "a" ).length, 0,
119+
"No anchors have been inserted as children of the data-clear-btn input element" );
120+
});
121+
107122
test( "data-clear-btn does not add native clear button to input button (IE10)", function() {
108123
// Get an input element, initial height, and reserve d for height difference
109124
var e = $( "input[data-clear-btn='true']" ),
@@ -149,4 +164,9 @@
149164
"turning off clearBtn removes wrapper class 'ui-input-has-clear'" );
150165
});
151166

167+
test( "cannot inject script via clearBtnText option", function() {
168+
deepEqual( !!$.clearBtnTextScriptInjected, false,
169+
"no script was injected via clearBtnText option" );
170+
});
171+
152172
})(jQuery);

0 commit comments

Comments
 (0)