Comparing version 0.1.2 to 0.2.0
@@ -39,2 +39,4 @@ 'use strict'; | ||
this.json = Object.create(null); | ||
if (createdMinimal || this.isCreationConstructor(node)) | ||
@@ -56,11 +58,14 @@ { | ||
this.head = this.selector('> head'); | ||
this.head = this.assertSelector('> head'); | ||
this.baseWrapped = this.selector('> head > base'); | ||
this.titleWrapped = this.selector('> head > title'); | ||
this.body = this.selector('> body'); | ||
if (!this.head || !this.body) | ||
this.body = this.assertSelector('> body'); | ||
this.selectorAll('> head > script[type="application/json"]').forEach(function(wrapped) | ||
{ | ||
throw new domv.Exception(new Error('Invalid document passed to the domv.HtmlDocument constructor, it should contain at least a "head" and a "body" element')); | ||
} | ||
var identifier = wrapped.getAttr('data-identifier'); | ||
if (identifier) | ||
{ | ||
this.json[identifier] = wrapped; | ||
} | ||
}, this); | ||
} | ||
@@ -212,22 +217,39 @@ | ||
/** Expose JSON data to an interpreter of the HTML document using a script tag. | ||
* The data is set on whatever identifier you specify. | ||
* @param {!string} identifier An identifier that is used when assigning the JSON data. | ||
/** Expose JSON data to an interpreter of the HTML document using a script type="application/json" element. | ||
* The data can be retrieved using getJSONData with the same identifier; | ||
* @param {!string} identifier Must be unique to properly get your JSON data back | ||
* @param {!*} data Any array, object, boolean, integer, et cetera that is able to be serialized into JSON. | ||
* @returns {!module:domv/lib/Component} The newly created "script" node | ||
* @example myDoc.addJSONData('window.foo', {'abc': 'def'}); | ||
* // <script type="text/javascript">window.foo={"abc":"def"};</script> | ||
* @example myDoc.addJSONData('foo', {'abc': 'def'}); | ||
* // <script type="application/json" data-identifier="foo">{"abc":"def"};</script> | ||
*/ | ||
HtmlDocument.prototype.addJSONData = function(identifier, data) | ||
{ | ||
var str = global.JSON.stringify(data); | ||
var node; | ||
data = global.JSON.stringify(data); | ||
data = identifier + '=' + data+';'; | ||
this.head.appendChild(node = this.create('script', { | ||
'type': 'text/javascript' | ||
'type': 'application/json', | ||
'data-identifier': identifier | ||
})); | ||
node.textContent = data; | ||
node.textContent = str; | ||
this.json[identifier] = node; | ||
return node; | ||
}; | ||
/** Retrieve JSON data previously exposed by addJSONData | ||
* @param {!string} identifier Same identifier as was used in addJSONData | ||
* @returns {?*} parsed json data | ||
*/ | ||
HtmlDocument.prototype.getJSONData = function(identifier) | ||
{ | ||
var node = this.json[identifier]; | ||
if (!node) | ||
{ | ||
return null; | ||
} | ||
return JSON.parse(node.textContent); | ||
}; | ||
HtmlDocument.prototype.stringifyAsHtml = function() | ||
@@ -234,0 +256,0 @@ { |
{ | ||
"name": "domv", | ||
"version": "0.1.2", | ||
"version": "0.2.0", | ||
"author": "Joris van der Wel <joris@jorisvanderwel.com>", | ||
@@ -5,0 +5,0 @@ "description": "Create views as components using DOM. Run the same code on the browser and on the server.", |
@@ -209,12 +209,30 @@ 'use strict'; | ||
}, | ||
'addJSONData()': function(test) | ||
'addJSONData() & getJSONData()': function(test) | ||
{ | ||
var htmldoc, script; | ||
htmldoc = new HtmlDocument(this.document); | ||
script = htmldoc.addJSONData('window.foo', {'abc': 'def'}); | ||
script = htmldoc.addJSONData('foo', {'abc': 'def'}); | ||
test.ok(script.isNodeEqual(htmldoc.outerNodeWrapped.selector('script'))); | ||
test.strictEqual(script.textContent, 'window.foo={"abc":"def"};'); | ||
test.strictEqual(script.getAttr('data-identifier'), 'foo'); | ||
test.strictEqual(script.getAttr('type'), 'application/json'); | ||
test.strictEqual(script.textContent, '{"abc":"def"}'); | ||
test.deepEqual(htmldoc.getJSONData('foo'), {abc: 'def'}); | ||
test.strictEqual(htmldoc.getJSONData('bar'), null); | ||
test.done(); | ||
}, | ||
'addJSONData() & getJSONData() after wrapping': function(test) | ||
{ | ||
var htmldoc, script; | ||
htmldoc = new HtmlDocument(this.document); | ||
htmldoc.addJSONData('foo', {'abc': 'def'}); | ||
// missing identifier: | ||
htmldoc.head.appendChild(htmldoc.create('script', {type: 'application/json'})); | ||
htmldoc = new HtmlDocument(htmldoc.outerNode); | ||
test.deepEqual(htmldoc.getJSONData('foo'), {abc: 'def'}); | ||
test.strictEqual(htmldoc.getJSONData('bar'), null); | ||
test.strictEqual(htmldoc.getJSONData(''), null); | ||
test.done(); | ||
}, | ||
'stringifyAsHtml() (overriden)': function(test) | ||
@@ -221,0 +239,0 @@ { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
238318
5152