Skip to content

Commit de349be

Browse files
authored
Merge pull request #609 from PolymerElements/unique-ids
2.x: Generate unique IDs for the input in the shadow root
2 parents cc19661 + 4865f5e commit de349be

File tree

3 files changed

+41
-10
lines changed

3 files changed

+41
-10
lines changed

Diff for: paper-input-behavior.html

+22-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
Polymer.PaperInputHelper = {};
2020
Polymer.PaperInputHelper.NextLabelID = 1;
2121
Polymer.PaperInputHelper.NextAddonID = 1;
22+
Polymer.PaperInputHelper.NextInputID = 1;
2223

2324
/**
2425
* Use `Polymer.PaperInputBehavior` to implement inputs with `<paper-input-container>`. This
@@ -361,8 +362,12 @@
361362
_ariaLabelledBy: {
362363
type: String,
363364
value: ''
364-
}
365+
},
365366

367+
_inputId: {
368+
type: String,
369+
value: ''
370+
}
366371
},
367372

368373
listeners: {
@@ -381,6 +386,16 @@
381386
* Returns a reference to the input element.
382387
*/
383388
get inputElement() {
389+
// Chrome generates audit errors if an <input type="password"> has a
390+
// duplicate ID, which is almost always true in Shady DOM. Generate
391+
// a unique ID instead.
392+
if (!this.$) {
393+
this.$ = {}
394+
}
395+
if (!this.$.input) {
396+
this._generateInputId();
397+
this.$.input = this.$$('#' + this._inputId);
398+
}
384399
return this.$.input;
385400
},
386401

@@ -516,6 +531,12 @@
516531
this._ariaLabelledBy = labelledBy;
517532
},
518533

534+
_generateInputId: function() {
535+
if (!this._inputId || this._inputId === '') {
536+
this._inputId = 'input-' + Polymer.PaperInputHelper.NextInputID++;
537+
}
538+
},
539+
519540
_onChange:function(event) {
520541
// In the Shadow DOM, the `change` event is not leaked into the
521542
// ancestor tree, so we must do this manually.

Diff for: paper-input.html

+13-4
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@
161161

162162
<slot name="prefix" slot="prefix"></slot>
163163

164-
<label hidden$="[[!label]]" aria-hidden="true" for="input" slot="label">[[label]]</label>
164+
<label hidden$="[[!label]]" aria-hidden="true" for$="[[_inputId]]" slot="label">[[label]]</label>
165165

166166
<span id="template-placeholder"></span>
167167

@@ -183,7 +183,9 @@
183183
Expect some conditional code (especially in the tests).
184184
-->
185185
<template id="v0">
186-
<input is="iron-input" id="input" slot="input"
186+
<input is="iron-input" slot="input"
187+
class="input-element"
188+
id$="[[_inputId]]"
187189
aria-labelledby$="[[_ariaLabelledBy]]"
188190
aria-describedby$="[[_ariaDescribedBy]]"
189191
disabled$="[[disabled]]"
@@ -221,12 +223,14 @@
221223

222224
<template id="v1">
223225
<!-- Need to bind maxlength so that the paper-input-char-counter works correctly -->
224-
<iron-input bind-value="{{value}}" id="input" slot="input"
226+
<iron-input bind-value="{{value}}" slot="input"
227+
class="input-element"
228+
id$="[[_inputId]]"
225229
maxlength$="[[maxlength]]"
226230
allowed-pattern="[[allowedPattern]]"
227231
invalid="{{invalid}}"
228232
validator="[[validator]]">
229-
<input id="nativeInput"
233+
<input
230234
aria-labelledby$="[[_ariaLabelledBy]]"
231235
aria-describedby$="[[_ariaDescribedBy]]"
232236
disabled$="[[disabled]]"
@@ -302,6 +306,11 @@
302306
},
303307

304308
_onIronInputReady: function() {
309+
// Even though this is only used in the next line, save this for
310+
// backwards compatibility, since the native input had this ID until 2.0.5.
311+
if (!this.$.nativeInput) {
312+
this.$.nativeInput = this.$$('input');
313+
}
305314
if (this.inputElement &&
306315
this._typesThatHaveText.indexOf(this.$.nativeInput.type) !== -1) {
307316
this.alwaysFloatLabel = true;

Diff for: paper-textarea.html

+6-5
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,10 @@
5555
disabled$="[[disabled]]"
5656
invalid="[[invalid]]">
5757

58-
<label hidden$="[[!label]]" aria-hidden="true" for="input" slot="label">[[label]]</label>
58+
<label hidden$="[[!label]]" aria-hidden="true" for$="[[_inputId]]" slot="label">[[label]]</label>
5959

60-
<iron-autogrow-textarea id="input" class="paper-input-input" slot="input"
60+
<iron-autogrow-textarea class="paper-input-input" slot="input"
61+
id$="[[_inputId]]"
6162
aria-labelledby$="[[_ariaLabelledBy]]"
6263
aria-describedby$="[[_ariaDescribedBy]]"
6364
bind-value="{{value}}"
@@ -151,15 +152,15 @@
151152
},
152153

153154
_ariaLabelledByChanged: function(ariaLabelledBy) {
154-
this.$.input.textarea.setAttribute('aria-labelledby', ariaLabelledBy);
155+
this._focusableElement.setAttribute('aria-labelledby', ariaLabelledBy);
155156
},
156157

157158
_ariaDescribedByChanged: function(ariaDescribedBy) {
158-
this.$.input.textarea.setAttribute('aria-describedby', ariaDescribedBy);
159+
this._focusableElement.setAttribute('aria-describedby', ariaDescribedBy);
159160
},
160161

161162
get _focusableElement() {
162-
return this.$.input.textarea;
163+
return this.inputElement.textarea;
163164
},
164165
});
165166
</script>

0 commit comments

Comments
 (0)