Skip to content

Commit 1d38f68

Browse files
ptrdoptrdo
authored andcommitted
Incorporates default option to strip Byte Order Mark.
1 parent 7475548 commit 1d38f68

3 files changed

Lines changed: 59 additions & 21 deletions

File tree

README.md

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -260,27 +260,29 @@ 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.2"
263+
templater.getVerson(); // returns current version, e.g. "2.3.0"
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.2"
267+
templater.getVersion(); // returns current version, e.g. "2.3.0"
268268

269269
// Defaults are assumed, but configuration can be passed to init:
270270
templater.init({
271271
showHeritage: true, // bypass obj.hasOwnProperty() filtering.
272-
strictStandard: true // require Microdata attributes.
272+
strictStandard: true, // require Microdata attributes.
273+
stripByteOrderMark: false // leave HTML unperturbed.
273274
});
274275
```
275276

276-
| Method Name | Argument(s) | Description |
277-
|-------------|-------------|-------------|
277+
| Method Name | Argument(s) | Description |
278+
|-------------|-------------|---------------------------------------------------------------------------|
278279
| `init` | *Object (optional)* | Returns an instance of the microdata-template. Config object is optional. |
279-
| `render` | *element, data* | Populates the HTML element template with data. |
280-
| `clear` | *element, callback* | Removes dynamically populated content, retaining the original template. |
281-
| `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. |
284-
| `setTransformer` | *name, func* | Provides for a custom transformer. |
285-
| `getTransformers` | *none* | Returns default and custom transformers. |
286-
| `getVersion` | *none* | Returns the current version. |
280+
| `render` | *element, data* | Populates the HTML element template with data. |
281+
| `clear` | *element, callback* | Removes dynamically populated content, retaining the original template. |
282+
| `refresh` | *element, data* | Makes current a previously rendered template. |
283+
| `getSetShowHeritage` | *Boolean* | False by default. When true, bypasses obj.hasOwnProperty() filtering. |
284+
| `getSetStrictStandard` | *Boolean* | False by default. When true, Microdata attributes are always required. |
285+
| `getSetStripByteOrderMark` | *Boolean* | True by default. When true, strips Byte Order Mark from incoming HTML snippets. |
286+
| `setTransformer` | *name, func* | Provides for a custom transformer. |
287+
| `getTransformers` | *none* | Returns default and custom transformers. |
288+
| `getVersion` | *none* | Returns the current version. |

lib/microdata-template.js

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
* An implementation of HTML template by way of the microdata mechanism.
77
*
88
* @author Peter Sylwester
9-
* @copyright (c)2019 Peter Sylwester
9+
* @copyright (c)2022 Peter Sylwester
1010
* @license MIT
11-
* @version 2.2.2, 2019/09/16
11+
* @version 2.3.0, 2022/05/03
1212
* @requires "HTML5", "ECMA-262 Edition 5.1"
1313
*
1414
*
@@ -177,7 +177,7 @@
177177

178178
/**
179179
* TODO: Determine if this is only a dubious feature.
180-
* @type {Boolean} When true bypasses obj.hasOwnProperty() filtering.
180+
* @type {Boolean} When true, bypasses obj.hasOwnProperty() filtering.
181181
*/
182182
var showHeritage = false;
183183

@@ -187,8 +187,15 @@
187187
* @public via proxy
188188
* @type {Boolean} When true, requires Microdata attributes for template.
189189
*/
190-
var strictStandard = false;
191-
190+
var strictStandard = false;
191+
192+
/**
193+
* stripByteOrderMark removes the marking indicating UTF-8 encoding.
194+
*
195+
* @public via proxy
196+
* @type {Boolean} When true, strips Byte Order Mark from incoming HTML snippets.
197+
*/
198+
var stripByteOrderMark = true;
192199

193200
/* PRIVATE UTILITIES */
194201

@@ -231,8 +238,13 @@
231238
return clone;
232239
};
233240

234-
var findTemplate = function (node) {
235-
var candidate;
241+
var findTemplate = function (source) {
242+
var node, candidate;
243+
if (stripByteOrderMark) {
244+
node = stripBOM(source);
245+
} else {
246+
node = source;
247+
}
236248
if (!strictStandard) {
237249
return node;
238250
} else if (isTemplate(node)) {
@@ -269,6 +281,15 @@
269281
return obj === undefined ? "" : obj;
270282
};
271283

284+
var stripBOM = function(buf) {
285+
var str = buf.toString("utf-8");
286+
if(str.charCodeAt(0) === 0xFEFF) {
287+
return str.slice(1);
288+
} else {
289+
return str;
290+
}
291+
};
292+
272293
/* PRIVATE METHODS */
273294

274295
var clear = function (template, callback) {
@@ -645,6 +666,9 @@
645666
if ("showHeritage" in config) {
646667
strictStandard = !!config.showHeritage;
647668
}
669+
if ("stripByteOrderMark" in config) {
670+
stripByteOrderMark = !!config.stripByteOrderMark;
671+
}
648672
}
649673
return this;
650674
},
@@ -755,6 +779,18 @@
755779
return showHeritage;
756780
},
757781

782+
getSetStripByteOrderMark: function (boo) {
783+
/**
784+
* @param boo {Boolean} When true, strips Byte Order Mark from incoming HTML snippets.
785+
* @returns {Boolean} The current setting.
786+
*/
787+
if (arguments.length > 0) {
788+
stripByteOrderMark = !!boo;
789+
}
790+
791+
return showHeritage;
792+
},
793+
758794
getSetSource: function (obj) {
759795

760796
if (arguments.length > 0) {

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.2",
3+
"version": "2.3.0",
44
"description": "An implementation of HTML template by way of the microdata mechanism.",
55
"keywords": [
66
"javascript",

0 commit comments

Comments
 (0)