html-element
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -6,3 +6,4 @@ require('./index.js') | ||
h1.textContent = 'w0f w0f'; | ||
h1.classList.add('cls'); | ||
console.log(h1.toString()); | ||
h1.src ="bla" | ||
h1.className="cls"; | ||
console.log(h1.outerHTML); |
77
index.js
@@ -108,7 +108,23 @@ global.Document = Document | ||
Element.prototype.toString = function () { | ||
Element.prototype.__defineGetter__('innerHTML', function () { | ||
var s = '' | ||
this.childNodes.forEach(function (e) { | ||
s += (e.outerHTML || e.textContent) | ||
}) | ||
return s | ||
}) | ||
Element.prototype.__defineSetter__('innerHTML', function (v) { | ||
//only handle this simple case that doesn't need parsing | ||
//this case is useful... parsing is hard and will need added deps! | ||
if(v == '') | ||
this.childNodes.length = 0 | ||
}) | ||
Element.prototype.__defineGetter__('outerHTML', function () { | ||
var a = [], self = this; | ||
function _stringify(arr, d) { | ||
var attr = [], value; | ||
function _stringify(arr) { | ||
var attr = [], value; | ||
arr.forEach(function(a){ | ||
@@ -129,12 +145,41 @@ value = ('style' != a.name) ? a.value : _stylify(self.style.styles); | ||
a.push('<'+this.nodeName + _stringify(this.attributes)+'>') | ||
this.textContent && a.push(this.textContent); | ||
this.childNodes.forEach(function (e) { | ||
a.push(e.toString()) | ||
}) | ||
function _propertify() { | ||
var props = []; | ||
for (var key in self) { | ||
_isProperty(key) && props.push({name: key, value:self[key]}); | ||
} | ||
// special className case, if className property is define while 'class' attribute is not then | ||
// include class attribute in output | ||
self.className && !self.getAttribute('class') && props.push({name:'class', value: self.className}) | ||
return props ? _stringify(props) : ''; | ||
} | ||
function _isProperty(key) { | ||
var types = ['string','boolean','number'] | ||
for (var i=0; i<=types.length;i++) { | ||
if (self.hasOwnProperty(key) && | ||
types[i] === typeof self[key] && | ||
key !== 'nodeName' && | ||
key !== 'className' | ||
) return true; | ||
} | ||
} | ||
a.push('<'+this.nodeName + _propertify() + _stringify(this.attributes)+'>') | ||
a.push(this.innerHTML) | ||
a.push('</'+this.nodeName+'>') | ||
return a.join('\n') | ||
} | ||
}) | ||
Element.prototype.__defineGetter__('textContent', function () { | ||
var s = '' | ||
this.childNodes.forEach(function (e) { | ||
s += e.textContent | ||
}) | ||
return s | ||
}) | ||
Element.prototype.addEventListener = function(t, l) {} | ||
@@ -151,7 +196,11 @@ | ||
function Text(){ | ||
} | ||
function Text(){} | ||
Text.prototype.toString = function() { | ||
return escapeHTML(this.value); | ||
} | ||
Text.prototype.__defineGetter__('textContent', function() { | ||
return escapeHTML(this.value || ''); | ||
}) | ||
Text.prototype.__defineSetter__('textContent', function(v) { | ||
this.value = v | ||
}) | ||
{ | ||
"name": "html-element", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"homepage": "https://github.com/1N50MN14/html-element", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -30,6 +30,4 @@ # html-element | ||
## TODO / Missing features | ||
- innerHTML | ||
- title (?) | ||
- Your PR to make this a more accurate implementation | ||
Please make a PR to help making this this a more accurate implementation. | ||
@@ -36,0 +34,0 @@ ## License |
@@ -5,7 +5,39 @@ require('../'); | ||
//remove white space so it's easier to test | ||
function clean(e) { | ||
return e.replace(/\n/g, '') | ||
.replace(/\s+/, ' ') | ||
} | ||
test('create a Text node', function(t){ | ||
var h1 = document.createElement('h1') | ||
h1.setAttribute('class', 'myclass'); | ||
t.type(h1.toString(), "string", "type of h1 should be string") | ||
t.end(); | ||
var h1 = document.createElement('h1') | ||
t.equal(clean(h1.outerHTML), "<h1></h1>") | ||
t.equal(clean(h1.textContent), "") | ||
t.equal(clean(h1.innerHTML), "") | ||
h1.setAttribute('class', 'myclass'); | ||
t.type(h1.toString(), "string", "type of h1 should be string") | ||
t.equal(clean(h1.outerHTML), '<h1 class="myclass"></h1>') | ||
t.equal(clean(h1.textContent), "") | ||
h1.appendChild(document.createTextNode('hello')) | ||
t.equal(clean(h1.outerHTML), '<h1 class="myclass">hello</h1>') | ||
t.equal(clean(h1.textContent), "hello") | ||
t.equal(clean(h1.innerHTML), "hello") | ||
h1.appendChild(document.createElement('h2')) | ||
t.equal(clean(h1.outerHTML), '<h1 class="myclass">hello<h2></h2></h1>') | ||
t.equal(clean(h1.innerHTML), "hello<h2></h2>") | ||
h1.innerHTML = '' | ||
t.equal(clean(h1.innerHTML), "") | ||
t.equal(clean(h1.outerHTML), '<h1 class="myclass"></h1>') | ||
t.equal(h1.childNodes.length, 0) | ||
t.end(); | ||
}) | ||
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
8790
199
36