From 85ca7646d69c4a6017bcef3472fa0390d942c0e6 Mon Sep 17 00:00:00 2001 From: Richard Gibson Date: Mon, 22 Oct 2018 17:01:26 -0400 Subject: [PATCH 1/4] data: Reorganize for clarity --- entries/data.xml | 55 ++++++++++++++++++++++-------------------------- 1 file changed, 25 insertions(+), 30 deletions(-) diff --git a/entries/data.xml b/entries/data.xml index a8ff396c..30914f76 100644 --- a/entries/data.xml +++ b/entries/data.xml @@ -24,14 +24,13 @@

We can set several distinct values for a single element and retrieve them later:


 $( "body" ).data( "foo", 52 );
-$( "body" ).data( "bar", { myType: "test", count: 40 } );
+$( "body" ).data( "bar", { isManual: true } );
 $( "body" ).data( { baz: [ 1, 2, 3 ] } );
 $( "body" ).data( "foo" ); // 52
-$( "body" ).data(); // { foo: 52, bar: { myType: "test", count: 40 }, baz: [ 1, 2, 3 ] }
+$( "body" ).data(); // { foo: 52, bar: { isManual: true }, baz: [ 1, 2, 3 ] }
       
-

In jQuery 1.4.3 setting an element's data object with .data(obj) extends the data previously stored with that element.

-

Prior to jQuery 1.4.3 (starting in jQuery 1.4) the .data() 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.

-

jQuery 3 changes the behavior of this method to align it to the Dataset API specifications. 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 the algorithm of the Dataset API. Writing a statement like $( "body" ).data( { "my-name": "aValue" } ).data(); will return { myName: "aValue" }.

+

Prior to jQuery 1.4.3, .data( obj ) completely replaced all data. Since jQuery 1.4.3, data is instead extended by shallow merge.

+

Since jQuery 3, 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 HTML dataset API. A statement like $( "body" ).data( { "my-name": "aValue" } ).data(); will return { myName: "aValue" }.

Due to the way browsers interact with plugins and external code, the .data() method cannot be used on <object> (unless it's a Flash plugin), <applet> or <embed> elements.

@@ -76,46 +75,42 @@ $( "span:last" ).text( $( "div" ).data( "test" ).last ); 1.4 - 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. + Return arbitrary data associated with the first element in the jQuery collection, as set by data() or by an HTML5 data-* attribute. -

The .data() 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:

+

The .data() 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:


-alert( $( "body" ).data( "foo" ) );
-alert( $( "body" ).data() );
+var elem = document.createElement( "span" );
+$( elem ).data( "foo" ); // undefined
+$( elem ).data(); // {}
+
+$( elem ).data( "foo", 42 );
+$( elem ).data( "foo" ); // 42
+$( elem ).data(); // { foo: 42 }
       
-

The above lines alert the data values that were set on the body element. If no data at all was set on that element, undefined is returned.

+

Calling .data() with no parameters returns a JavaScript object containing each stored value as a property. Using the object directly to get or set values is faster than making individual calls to .data():


-alert( $( "body" ).data( "foo" ) ); // undefined
-$( "body" ).data( "bar", "foobar" );
-alert( $( "body" ).data( "bar" ) ); // foobar
-      
-

jQuery 3 changes the behavior of this method to align it to the Dataset API specifications. 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 the algorithm of the Dataset API. Writing a statement like $( "body" ).data( { "my-name": "aValue" } ).data(); will return { myName: "aValue" }.

+var mydata = $( "#mydiv" ).data(); +mydata.count = 43; +mydata.status = "embiggened"; +$( "#mydiv" ).data( "count" ); // 43 + +

Since jQuery 3, 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 HTML dataset API. A statement like $( "body" ).data( { "my-name": "aValue" } ).data(); will return { myName: "aValue" }.

HTML5 data-* Attributes

-

As of jQuery 1.4.3 HTML 5 data- attributes 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 W3C HTML5 specification.

+

Since jQuery 1.4.3, data-* attributes are used to initialize jQuery data. An element's data-* attributes are retrieved the first time the data() method is invoked upon it, and then are no longer accessed or mutated (all values are stored internally by jQuery).

+

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 jQuery.parseJSON is used to parse it; it must follow valid JSON syntax including quoted property names. A string not parseable as a JavaScript value is not converted.

+

To retrieve a data-* attribute value as an unconverted string, use the attr() method.

+

Since jQuery 1.6, dashes in data-* attribute names have been processed in alignment with the HTML dataset API.

For example, given the following HTML:

<div data-role="page" data-last-value="43" data-hidden="true" data-options='{"name":"John"}'></div>
-

All of the following jQuery code will work.

+

The following comparisons are all true:


 $( "div" ).data( "role" ) === "page";
 $( "div" ).data( "lastValue" ) === 43;
 $( "div" ).data( "hidden" ) === true;
 $( "div" ).data( "options" ).name === "John";
       
-

The second statement of the code above correctly refers to the data-last-value 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 data- to the result. So, the string lastValue is converted to data-last-value.

-

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.

-

When the data attribute is an object (starts with '{') or array (starts with '[') then jQuery.parseJSON is used to parse the string; it must follow valid JSON syntax including quoted property names. If the value isn't parseable as a JavaScript value, it is left as a string.

-

To retrieve the value's attribute as a string without any attempt to convert it, use the attr() method.

-

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).

-

Calling .data() 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 .data(obj). Using the object directly to get or set values is faster than making individual calls to .data() to get or set each value:

-

-var mydata = $( "#mydiv" ).data();
-if ( mydata.count < 9 ) {
-  mydata.count = 43;
-  mydata.status = "embiggened";
-}
-    
From 34b6d5247aa6e9e96c852e4a27b8c67de51e8fab Mon Sep 17 00:00:00 2001 From: Richard Gibson Date: Mon, 22 Oct 2018 17:10:55 -0400 Subject: [PATCH 2/4] data: Explicitly disavow writing data-* attributes Fixes gh-1023 --- entries/data.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/entries/data.xml b/entries/data.xml index 30914f76..af51b3ad 100644 --- a/entries/data.xml +++ b/entries/data.xml @@ -29,6 +29,7 @@ $( "body" ).data( { baz: [ 1, 2, 3 ] } ); $( "body" ).data( "foo" ); // 52 $( "body" ).data(); // { foo: 52, bar: { isManual: true }, baz: [ 1, 2, 3 ] } +

Using the data() method to update data does not affect attributes in the DOM. To set a data-* attribute value, use attr.

Prior to jQuery 1.4.3, .data( obj ) completely replaced all data. Since jQuery 1.4.3, data is instead extended by shallow merge.

Since jQuery 3, 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 HTML dataset API. A statement like $( "body" ).data( { "my-name": "aValue" } ).data(); will return { myName: "aValue" }.

Due to the way browsers interact with plugins and external code, the .data() method cannot be used on <object> (unless it's a Flash plugin), <applet> or <embed> elements.

From 8759444fd5b581f48f16ab49de259c9b5bb90f45 Mon Sep 17 00:00:00 2001 From: Richard Gibson Date: Sat, 27 Oct 2018 10:36:54 -0400 Subject: [PATCH 3/4] data: Don't promise mutation of objects returned by .data() --- entries/data.xml | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/entries/data.xml b/entries/data.xml index af51b3ad..414e4f1c 100644 --- a/entries/data.xml +++ b/entries/data.xml @@ -88,13 +88,7 @@ $( elem ).data( "foo", 42 ); $( elem ).data( "foo" ); // 42 $( elem ).data(); // { foo: 42 } -

Calling .data() with no parameters returns a JavaScript object containing each stored value as a property. Using the object directly to get or set values is faster than making individual calls to .data():

-

-var mydata = $( "#mydiv" ).data();
-mydata.count = 43;
-mydata.status = "embiggened";
-$( "#mydiv" ).data( "count" ); // 43
-    
+

Calling .data() 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).

Since jQuery 3, 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 HTML dataset API. A statement like $( "body" ).data( { "my-name": "aValue" } ).data(); will return { myName: "aValue" }.

HTML5 data-* Attributes From 53728b1b3abc71d5638e998337f27b3cec90d3a9 Mon Sep 17 00:00:00 2001 From: Richard Gibson Date: Sat, 27 Oct 2018 10:41:23 -0400 Subject: [PATCH 4/4] data: Wrap "data-*" attribute names in ... --- entries/data.xml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/entries/data.xml b/entries/data.xml index 414e4f1c..7e742da1 100644 --- a/entries/data.xml +++ b/entries/data.xml @@ -29,7 +29,7 @@ $( "body" ).data( { baz: [ 1, 2, 3 ] } ); $( "body" ).data( "foo" ); // 52 $( "body" ).data(); // { foo: 52, bar: { isManual: true }, baz: [ 1, 2, 3 ] } -

Using the data() method to update data does not affect attributes in the DOM. To set a data-* attribute value, use attr.

+

Using the data() method to update data does not affect attributes in the DOM. To set a data-* attribute value, use attr.

Prior to jQuery 1.4.3, .data( obj ) completely replaced all data. Since jQuery 1.4.3, data is instead extended by shallow merge.

Since jQuery 3, 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 HTML dataset API. A statement like $( "body" ).data( { "my-name": "aValue" } ).data(); will return { myName: "aValue" }.

Due to the way browsers interact with plugins and external code, the .data() method cannot be used on <object> (unless it's a Flash plugin), <applet> or <embed> elements.

@@ -76,7 +76,7 @@ $( "span:last" ).text( $( "div" ).data( "test" ).last ); 1.4 - Return arbitrary data associated with the first element in the jQuery collection, as set by data() or by an HTML5 data-* attribute. + Return arbitrary data associated with the first element in the jQuery collection, as set by data() or by an HTML5 data-* attribute.

The .data() 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:


@@ -91,12 +91,12 @@ $( elem ).data(); // { foo: 42 }
       

Calling .data() 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).

Since jQuery 3, 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 HTML dataset API. A statement like $( "body" ).data( { "my-name": "aValue" } ).data(); will return { myName: "aValue" }.

- HTML5 data-* Attributes + HTML5 data-* Attributes

-

Since jQuery 1.4.3, data-* attributes are used to initialize jQuery data. An element's data-* attributes are retrieved the first time the data() method is invoked upon it, and then are no longer accessed or mutated (all values are stored internally by jQuery).

+

Since jQuery 1.4.3, data-* attributes are used to initialize jQuery data. An element's data-* attributes are retrieved the first time the data() method is invoked upon it, and then are no longer accessed or mutated (all values are stored internally by jQuery).

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 jQuery.parseJSON is used to parse it; it must follow valid JSON syntax including quoted property names. A string not parseable as a JavaScript value is not converted.

-

To retrieve a data-* attribute value as an unconverted string, use the attr() method.

-

Since jQuery 1.6, dashes in data-* attribute names have been processed in alignment with the HTML dataset API.

+

To retrieve a data-* attribute value as an unconverted string, use the attr() method.

+

Since jQuery 1.6, dashes in data-* attribute names have been processed in alignment with the HTML dataset API.

For example, given the following HTML:

<div data-role="page" data-last-value="43" data-hidden="true" data-options='{"name":"John"}'></div>

The following comparisons are all true: