Stop treating objects with a nodeType as if they are DOM Nodes

- Fixes #1638
This commit is contained in:
Gregg Van Hove
2018-12-17 17:10:53 -08:00
parent c67a5b830c
commit 37dfe50d99
3 changed files with 37 additions and 15 deletions
+27 -11
View File
@@ -5055,10 +5055,12 @@ getJasmineRequireObj().pp = function(j$) {
this.emitScalar(value.toString()); this.emitScalar(value.toString());
} else if (typeof value === 'function') { } else if (typeof value === 'function') {
this.emitScalar('Function'); this.emitScalar('Function');
} else if (value.nodeType === 1) { } else if (j$.isDomNode(value)) {
this.emitDomElement(value); if (value.tagName) {
} else if (typeof value.nodeType === 'number') { this.emitDomElement(value);
this.emitScalar('HTMLNode'); } else {
this.emitScalar('HTMLNode');
}
} else if (value instanceof Date) { } else if (value instanceof Date) {
this.emitScalar('Date(' + value + ')'); this.emitScalar('Date(' + value + ')');
} else if (j$.isSet(value)) { } else if (j$.isSet(value)) {
@@ -5249,15 +5251,29 @@ getJasmineRequireObj().pp = function(j$) {
}; };
PrettyPrinter.prototype.emitDomElement = function(el) { PrettyPrinter.prototype.emitDomElement = function(el) {
var closingTag = '</' + el.tagName.toLowerCase() + '>'; var tagName = el.tagName.toLowerCase(),
attrs = el.attributes,
i,
len = attrs.length,
out = '<' + tagName,
attr;
if (el.innerHTML === '') { for (i = 0; i < len; i++) {
this.append(el.outerHTML.replace(closingTag, '')); attr = attrs[i];
} else { out += ' ' + attr.name;
var tagEnd = el.outerHTML.indexOf('>');
this.append(el.outerHTML.substring(0, tagEnd + 1)); if (attr.value !== '') {
this.append('...' + closingTag); out += '="' + attr.value + '"';
}
} }
out += '>';
if (el.childElementCount !== 0 || el.textContent !== '') {
out += '...</' + tagName + '>';
}
this.append(out);
}; };
PrettyPrinter.prototype.formatProperty = function(obj, property, isGetter) { PrettyPrinter.prototype.formatProperty = function(obj, property, isGetter) {
+4
View File
@@ -132,6 +132,10 @@ describe("jasmineUnderTest.pp", function () {
}, bar: [1, 2, 3]})).toEqual("Object({ foo: Function, bar: [ 1, 2, 3 ] })"); }, bar: [1, 2, 3]})).toEqual("Object({ foo: Function, bar: [ 1, 2, 3 ] })");
}); });
it("should stringify objects that almost look like DOM nodes", function() {
expect(jasmineUnderTest.pp({nodeType: 1})).toEqual("Object({ nodeType: 1 })");
});
it("should truncate objects with too many keys", function () { it("should truncate objects with too many keys", function () {
var originalMaxLength = jasmineUnderTest.MAX_PRETTY_PRINT_ARRAY_LENGTH; var originalMaxLength = jasmineUnderTest.MAX_PRETTY_PRINT_ARRAY_LENGTH;
var long = {a: 1, b: 2, c: 3}; var long = {a: 1, b: 2, c: 3};
+6 -4
View File
@@ -34,10 +34,12 @@ getJasmineRequireObj().pp = function(j$) {
this.emitScalar(value.toString()); this.emitScalar(value.toString());
} else if (typeof value === 'function') { } else if (typeof value === 'function') {
this.emitScalar('Function'); this.emitScalar('Function');
} else if (value.nodeType === 1) { } else if (j$.isDomNode(value)) {
this.emitDomElement(value); if (value.tagName) {
} else if (typeof value.nodeType === 'number') { this.emitDomElement(value);
this.emitScalar('HTMLNode'); } else {
this.emitScalar('HTMLNode');
}
} else if (value instanceof Date) { } else if (value instanceof Date) {
this.emitScalar('Date(' + value + ')'); this.emitScalar('Date(' + value + ')');
} else if (j$.isSet(value)) { } else if (j$.isSet(value)) {