Skip to content

Commit e34ca55

Browse files
ptrdopsylwester
authored andcommitted
Strict Standard
1 parent 384189b commit e34ca55

3 files changed

Lines changed: 63 additions & 25 deletions

File tree

README.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
###### This is released software. Please **[log issues](https://github.com/ptrdo/microdata-template/issues)** found.
2-
# microdata-template `v2.2.1`
2+
# microdata-template `v2.2.2`
33
An implementation of HTML template by way of the microdata mechanism.
44
### The Gist
55
This JavaScript module should simplify adding dynamic content to HTML documents while staying true to the recommendations of web standards. There are no dependencies here except the JavaScript [ECMA5 standard](http://www.ecma-international.org/ecma-262/5.1/) which enjoys [nearly universal support](http://kangax.github.io/compat-table/es5/) in modern browsers. Also, since the HTML recommendations for integral technologies such as [template](https://www.w3.org/TR/html52/semantics-scripting.html#the-template-element) and [microdata](https://www.w3.org/TR/microdata/) are variably implemented by modern browsers, this module serves as a [polyfill](https://en.wikipedia.org/wiki/Polyfill) to assure reliable results. Best of all, this methodology encourages the writing of low-dependency JavaScript and perfectly valid HTML — even within fully-functional templated markup.
@@ -149,7 +149,7 @@ import templater from "./path/to/microdata-template.js";
149149
let itemToReplicate = document.querySelector("[itemprop][hidden]"); // the template
150150
let parentOfClones = document.getElementById("months"); // the parent of the template
151151

152-
templater.render(itemToReplicate||parentOfClones, myData); // either works!
152+
templater.render(itemToReplicate||parentOfClones, myData); // either works when strictStandards = true;
153153
```
154154

155155
>**NOTE:** The [microdata specification](https://www.w3.org/TR/microdata/) requires an element with an `itemscope` attribute to also have either an `itemref` or `itemtype` attribute, but this rule is not enforced here.
@@ -260,20 +260,27 @@ Once instantiated in the web client code, the microdata-template can be addresse
260260
```javascript
261261
// In pre-ES6 implementations, the code is exposed to the global namespace:
262262
var templater = window.MicrodataTemplate.init();
263-
templater.getVerson(); // returns current version, e.g. "2.2.1"
263+
templater.getVerson(); // returns current version, e.g. "2.2.2"
264264

265265
// In ES6 implementations, the import code does not require init()
266266
import templater from "./path/to/microdata-template.js";
267-
templater.getVersion(); // returns current version, e.g. "2.2.1"
267+
templater.getVersion(); // returns current version, e.g. "2.2.2"
268268

269+
// Defaults are assumed, but configuration can be passed to init:
270+
templater.init({
271+
showHeritage: true, // bypass obj.hasOwnProperty() filtering.
272+
strictStandard: true // require Microdata attributes.
273+
});
269274
```
270275

271276
| Method Name | Argument(s) | Description |
272277
|-------------|-------------|-------------|
273-
| `init` | *none* | Returns an instance of the microdata-template. |
278+
| `init` | *Object (optional)* | Returns an instance of the microdata-template. Config object is optional. |
274279
| `render` | *element, data* | Populates the HTML element template with data. |
275280
| `clear` | *element, callback* | Removes dynamically populated content, retaining the original template. |
276281
| `refresh` | *element, data* | Makes current a previously rendered template. |
282+
| `getSetShowHeritage` | *Boolean* | False by default. When true, bypasses obj.hasOwnProperty() filtering. |
283+
| `getSetStrictStandard` | *Boolean* | False by default. When true, Microdata attributes are always required. |
277284
| `setTransformer` | *name, func* | Provides for a custom transformer. |
278285
| `getTransformers` | *none* | Returns default and custom transformers. |
279286
| `getVersion` | *none* | Returns the current version. |

lib/microdata-template.js

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* @author Peter Sylwester
99
* @copyright (c)2019 Peter Sylwester
1010
* @license MIT
11-
* @version 2.2.0, 2019/09/13
11+
* @version 2.2.2, 2019/09/16
1212
* @requires "HTML5", "ECMA-262 Edition 5.1"
1313
*
1414
*
@@ -53,7 +53,7 @@
5353
}(this, (function () {
5454
"use strict";
5555

56-
var element, source, version = "2.2.1";
56+
var element, source, version = "2.2.2";
5757

5858
/**
5959
* Markers are the HTML node element attributes used to designate components of the templated markup.
@@ -181,6 +181,14 @@
181181
*/
182182
var showHeritage = false;
183183

184+
/**
185+
* strictStandard determines Microdata compliance.
186+
*
187+
* @public via proxy
188+
* @type {Boolean} When true, requires Microdata attributes for template.
189+
*/
190+
var strictStandard = false;
191+
184192

185193
/* PRIVATE UTILITIES */
186194

@@ -202,16 +210,18 @@
202210
};
203211

204212
var isTemplate = function (node) {
205-
return !!node && !!node.hasAttribute
206-
&& node.hasAttribute(MARKER.hidden)
207-
&& (node.hasAttribute(MARKER.repeat) || node.hasAttribute(MARKER.property) || node.hasAttribute(MARKER.binder));
213+
return !!node && !!node.hasAttribute
214+
&& node.hasAttribute(MARKER.hidden)
215+
&& (node.hasAttribute(MARKER.repeat)
216+
|| node.hasAttribute(MARKER.property)
217+
|| node.hasAttribute(MARKER.binder));
208218
};
209219

210220
var isNestedTemplate = function (node) {
211221
return !!node && !!node.hasAttribute
212-
&& node.hasAttribute(MARKER.hidden)
213-
&& node.hasAttribute(MARKER.repeat)
214-
&& node.hasAttribute(MARKER.source);
222+
&& node.hasAttribute(MARKER.hidden)
223+
&& node.hasAttribute(MARKER.repeat)
224+
&& node.hasAttribute(MARKER.source);
215225
};
216226

217227
var denude = function (clone) {
@@ -223,7 +233,9 @@
223233

224234
var findTemplate = function (node) {
225235
var candidate;
226-
if (isTemplate(node)) {
236+
if (!strictStandard) {
237+
return node;
238+
} else if (isTemplate(node)) {
227239
return node;
228240
} else if (!!node && !!node.querySelector) {
229241
candidate = node.querySelector("["+MARKER.hidden+"]");
@@ -346,7 +358,7 @@
346358
}
347359
if (!!proxy) {
348360
if (alternates.length > 1) {
349-
proxy = alternates.filter(function (alt, j, arr) {
361+
proxy = alternates.filter(function (alt, k, arr) {
350362
if (PATTERN.address.test(alt)) {
351363
return alt;
352364
} else if (alt in datum) {
@@ -360,7 +372,7 @@
360372
}
361373
})[0];
362374
}
363-
if (!!combo) {
375+
if (!!combo && Array.isArray(proxy)) {
364376
// this is a parenthetical, e/g combineString:(foo,bar)
365377
value = [];
366378
proxy.forEach(function (clause) {
@@ -442,7 +454,7 @@
442454
}
443455
})[0];
444456
}
445-
if (!!combo) {
457+
if (!!combo && Array.isArray(proxy)) {
446458
// this is a parenthetical, e/g combineString:(foo,bar)
447459
value = [];
448460
proxy.forEach(function (clause) {
@@ -625,7 +637,15 @@
625637

626638
/* PUBLIC API */
627639

628-
init: function() {
640+
init: function(config) {
641+
if (arguments.length > 0 && Object(config) === config) {
642+
if ("strictStandard" in config) {
643+
strictStandard = !!config.strictStandard;
644+
}
645+
if ("showHeritage" in config) {
646+
strictStandard = !!config.showHeritage;
647+
}
648+
}
629649
return this;
630650
},
631651

@@ -710,19 +730,30 @@
710730
return null;
711731
}
712732
},
713-
714-
/**
715-
* @param boo {Boolean} When true the obj.hasOwnProperty() is not enforced.
716-
* @returns {Boolean} The current setting.
717-
*/
733+
718734
getSetShowHeritage: function (boo) {
719-
735+
/**
736+
* @param boo {Boolean} When true the obj.hasOwnProperty() is not enforced.
737+
* @returns {Boolean} The current setting.
738+
*/
720739
if (arguments.length > 0) {
721740
showHeritage = !!boo;
722741
}
723742

724743
return showHeritage;
725744
},
745+
746+
getSetStrictStandard: function (boo) {
747+
/**
748+
* @param boo {Boolean} When true Microdata attributes are always required.
749+
* @returns {Boolean} The current setting.
750+
*/
751+
if (arguments.length > 0) {
752+
strictStandard = !!boo;
753+
}
754+
755+
return showHeritage;
756+
},
726757

727758
getSetSource: function (obj) {
728759

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "microdata-template",
3-
"version": "2.2.1",
3+
"version": "2.2.2",
44
"description": "An implementation of HTML template by way of the microdata mechanism.",
55
"keywords": [
66
"javascript",

0 commit comments

Comments
 (0)