Skip to content

Commit 3d88fc6

Browse files
gibson042mgol
authored andcommitted
data: Explicitly disavow writing data-* attributes (jquery#1112)
Also includes general cleanup. Fixes jquerygh-1023 Closes jquerygh-1112
1 parent ed49708 commit 3d88fc6

File tree

1 file changed

+22
-32
lines changed

1 file changed

+22
-32
lines changed

entries/data.xml

+22-32
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@
2424
<p>We can set several distinct values for a single element and retrieve them later:</p>
2525
<pre><code>
2626
$( "body" ).data( "foo", 52 );
27-
$( "body" ).data( "bar", { myType: "test", count: 40 } );
27+
$( "body" ).data( "bar", { isManual: true } );
2828
$( "body" ).data( { baz: [ 1, 2, 3 ] } );
2929
$( "body" ).data( "foo" ); // 52
30-
$( "body" ).data(); // { foo: 52, bar: { myType: "test", count: 40 }, baz: [ 1, 2, 3 ] }
30+
$( "body" ).data(); // { foo: 52, bar: { isManual: true }, baz: [ 1, 2, 3 ] }
3131
</code></pre>
32-
<p>In jQuery 1.4.3 setting an element's data object with <code>.data(obj)</code> extends the data previously stored with that element.</p>
33-
<p>Prior to jQuery 1.4.3 (starting in jQuery 1.4) the <code>.data()</code> method completely replaced all data, instead of just extending the data object. If you are using third-party plugins it may not be advisable to completely replace the element's data object, since plugins may have also set data.</p>
34-
<p><strong>jQuery 3</strong> changes the behavior of this method to align it to the <a href="https://www.w3.org/TR/html5/dom.html#dom-dataset">Dataset API specifications</a>. Specifically, jQuery 3 transforms every two-character sequence of "-" (U+002D) followed by a lowercase ASCII letter by the uppercase version of the letter as per definition of <a href="https://www.w3.org/TR/html5/dom.html#dom-dataset">the algorithm of the Dataset API</a>. Writing a statement like <code>$( "body" ).data( { "my-name": "aValue" } ).data();</code> will return <code>{ myName: "aValue" }</code>.</p>
32+
<p>Using the <code>data()</code> method to update data does not affect attributes in the DOM. To set a <code>data-*</code> attribute value, use <code><a href="/attr/">attr</a></code>.</p>
33+
<p>Prior to jQuery 1.4.3, <code>.data( obj )</code> completely replaced all data. Since jQuery 1.4.3, data is instead extended by shallow merge.</p>
34+
<p>Since <strong>jQuery 3</strong>, every two-character sequence of "-" (U+002D) followed by a lowercase ASCII letter in a key is replaced by the uppercase version of the letter, in alignment with the <a href="https://html.spec.whatwg.org/multipage/dom.html#dom-dataset">HTML dataset API</a>. A statement like <code>$( "body" ).data( { "my-name": "aValue" } ).data();</code> will return <code>{ myName: "aValue" }</code>.</p>
3535
<p>Due to the way browsers interact with plugins and external code, the <code>.data()</code> method cannot be used on <code>&lt;object&gt;</code> (unless it's a Flash plugin), <code>&lt;applet&gt;</code> or <code>&lt;embed&gt;</code> elements.</p>
3636
</longdesc>
3737
<note id="no-data-on-xml" type="additional"/>
@@ -76,46 +76,36 @@ $( "span:last" ).text( $( "div" ).data( "test" ).last );
7676
<signature>
7777
<added>1.4</added>
7878
</signature>
79-
<desc>Return the value at the named data store for the first element in the jQuery collection, as set by data(name, value) or by an HTML5 data-* attribute.</desc>
79+
<desc>Return arbitrary data associated with the first element in the jQuery collection, as set by data() or by an HTML5 <code>data-*</code> attribute.</desc>
8080
<longdesc>
81-
<p>The <code>.data()</code> method allows us to attach data of any type to DOM elements in a way that is safe from circular references and therefore from memory leaks. We can retrieve several distinct values for a single element one at a time, or as a set:</p>
81+
<p>The <code>.data()</code> method allows us to read data previously associated with DOM elements. We can retrieve several distinct values for a single element one at a time, or as a set:</p>
8282
<pre><code>
83-
alert( $( "body" ).data( "foo" ) );
84-
alert( $( "body" ).data() );
85-
</code></pre>
86-
<p>The above lines alert the data values that were set on the <code>body</code> element. If no data at all was set on that element, <code>undefined</code> is returned.</p>
87-
<pre><code>
88-
alert( $( "body" ).data( "foo" ) ); // undefined
89-
$( "body" ).data( "bar", "foobar" );
90-
alert( $( "body" ).data( "bar" ) ); // foobar
83+
var elem = document.createElement( "span" );
84+
$( elem ).data( "foo" ); // undefined
85+
$( elem ).data(); // {}
86+
87+
$( elem ).data( "foo", 42 );
88+
$( elem ).data( "foo" ); // 42
89+
$( elem ).data(); // { foo: 42 }
9190
</code></pre>
92-
<p><strong>jQuery 3</strong> changes the behavior of this method to align it to the <a href="https://www.w3.org/TR/html5/dom.html#dom-dataset">Dataset API specifications</a>. Specifically, jQuery 3 transforms every two-character sequence of "-" (U+002D) followed by a lowercase ASCII letter by the uppercase version of the letter as per definition of <a href="https://www.w3.org/TR/html5/dom.html#dom-dataset">the algorithm of the Dataset API</a>. Writing a statement like <code>$( "body" ).data( { "my-name": "aValue" } ).data();</code> will return <code>{ myName: "aValue" }</code>.</p>
91+
<p>Calling <code>.data()</code> with no parameters returns a JavaScript object containing each stored value as a property. The object can be used directly to get data values (but note that property names originally containing dashes will have been modified as described below).</p>
92+
<p>Since <strong>jQuery 3</strong>, every two-character sequence of "-" (U+002D) followed by a lowercase ASCII letter in a key is replaced by the uppercase version of the letter, in alignment with the <a href="https://html.spec.whatwg.org/multipage/dom.html#dom-dataset">HTML dataset API</a>. A statement like <code>$( "body" ).data( { "my-name": "aValue" } ).data();</code> will return <code>{ myName: "aValue" }</code>.</p>
9393
<h4 id="data-html5">
94-
<a href="#data-html5">HTML5 data-* Attributes</a>
94+
<a href="#data-html5">HTML5 <code>data-*</code> Attributes</a>
9595
</h4>
96-
<p>As of jQuery 1.4.3 <a href="https://johnresig.com/blog/html-5-data-attributes/">HTML 5 data- attributes</a> will be automatically pulled in to jQuery's data object. The treatment of attributes with embedded dashes was changed in jQuery 1.6 to conform to the <a href="https://www.w3.org/TR/html5/dom.html#embedding-custom-non-visible-data-with-the-data-*-attributes">W3C HTML5 specification</a>.</p>
96+
<p>Since jQuery 1.4.3, <a href="https://html.spec.whatwg.org/multipage/dom.html#embedding-custom-non-visible-data-with-the-data-*-attributes"><code>data-*</code> attributes</a> are used to initialize jQuery data. An element's <code>data-*</code> attributes are retrieved the first time the <code>data()</code> method is invoked upon it, and then are no longer accessed or mutated (all values are stored internally by jQuery).</p>
97+
<p>Every attempt is made to convert the attribute's string value to a JavaScript value (this includes booleans, numbers, objects, arrays, and null). A string is only converted to a number if doing so doesn't change its representation (for example, the string "100" is converted to the number 100, but "1E02" and "100.000" are left as strings because their numeric value of 100 serializes to "100"). When a string starts with '{' or '[', then <code>jQuery.parseJSON</code> is used to parse it; it must follow <a href="https://en.wikipedia.org/wiki/JSON#Data_types.2C_syntax_and_example">valid JSON syntax</a> <em>including quoted property names</em>. A string not parseable as a JavaScript value is not converted.</p>
98+
<p>To retrieve a <code>data-*</code> attribute value as an unconverted string, use the <code><a href="/attr/">attr()</a></code> method.</p>
99+
<p>Since jQuery 1.6, dashes in <code>data-*</code> attribute names have been processed in alignment with the <a href="https://html.spec.whatwg.org/multipage/dom.html#dom-dataset">HTML dataset API</a>.</p>
97100
<p>For example, given the following HTML:</p>
98101
<pre><code>&lt;div data-role="page" data-last-value="43" data-hidden="true" data-options='{"name":"John"}'&gt;&lt;/div&gt;</code></pre>
99-
<p>All of the following jQuery code will work.</p>
102+
<p>The following comparisons are all true:</p>
100103
<pre><code>
101104
$( "div" ).data( "role" ) === "page";
102105
$( "div" ).data( "lastValue" ) === 43;
103106
$( "div" ).data( "hidden" ) === true;
104107
$( "div" ).data( "options" ).name === "John";
105108
</code></pre>
106-
<p>The second statement of the code above correctly refers to the <code>data-last-value</code> attribute of the element. In case no data is stored with the passed key, jQuery searches among the attributes of the element, converting a camel-cased string into a dashed string and then prepending <code>data-</code> to the result. So, the string <code>lastValue</code> is converted to <code>data-last-value</code>.</p>
107-
<p>Every attempt is made to convert the string to a JavaScript value (this includes booleans, numbers, objects, arrays, and null). A value is only converted to a number if doing so doesn't change the value's representation. For example, "1E02" and "100.000" are equivalent as numbers (numeric value 100) but converting them would alter their representation so they are left as strings. The string value "100" is converted to the number 100.</p>
108-
<p>When the data attribute is an object (starts with '{') or array (starts with '[') then <code>jQuery.parseJSON</code> is used to parse the string; it must follow <a href="https://en.wikipedia.org/wiki/JSON#Data_types.2C_syntax_and_example">valid JSON syntax</a> <em>including quoted property names</em>. If the value isn't parseable as a JavaScript value, it is left as a string.</p>
109-
<p>To retrieve the value's attribute as a string without any attempt to convert it, use the <code><a href="/attr/">attr()</a></code> method.</p>
110-
<p>The data- attributes are pulled in the first time the data property is accessed and then are no longer accessed or mutated (all data values are then stored internally in jQuery).</p>
111-
<p>Calling <code>.data()</code> with no parameters retrieves all of the values as a JavaScript object. This object can be safely cached in a variable as long as a new object is not set with <code>.data(obj)</code>. Using the object directly to get or set values is faster than making individual calls to <code>.data()</code> to get or set each value:</p>
112-
<pre><code>
113-
var mydata = $( "#mydiv" ).data();
114-
if ( mydata.count &lt; 9 ) {
115-
mydata.count = 43;
116-
mydata.status = "embiggened";
117-
}
118-
</code></pre>
119109
</longdesc>
120110
<note id="no-data-on-xml" type="additional"/>
121111
<example>

0 commit comments

Comments
 (0)