| 1037 | * Convert a value to a nice, human-readable string |
| 1038 | */ |
| 1039 | function format_value(val, seen) |
| 1040 | { |
| 1041 | if (!seen) { |
| 1042 | seen = []; |
| 1043 | } |
| 1044 | if (typeof val === "object" && val !== null) { |
| 1045 | if (seen.indexOf(val) >= 0) { |
| 1046 | return "[...]"; |
| 1047 | } |
| 1048 | seen.push(val); |
| 1049 | } |
| 1050 | if (Array.isArray(val)) { |
| 1051 | let output = "["; |
| 1052 | if (val.beginEllipsis !== undefined) { |
| 1053 | output += "…, "; |
| 1054 | } |
| 1055 | output += val.map(function(x) {return format_value(x, seen);}).join(", "); |
| 1056 | if (val.endEllipsis !== undefined) { |
| 1057 | output += ", …"; |
| 1058 | } |
| 1059 | return output + "]"; |
| 1060 | } |
| 1061 | |
| 1062 | switch (typeof val) { |
| 1063 | case "string": |
| 1064 | val = val.replace(/\\/g, "\\\\"); |
| 1065 | for (var p in replacements) { |
| 1066 | var replace = "\\" + replacements[p]; |
| 1067 | val = val.replace(RegExp(String.fromCharCode(p), "g"), replace); |
| 1068 | } |
| 1069 | return '"' + val.replace(/"/g, '\\"') + '"'; |
| 1070 | case "boolean": |
| 1071 | case "undefined": |
| 1072 | return String(val); |
| 1073 | case "number": |
| 1074 | // In JavaScript, -0 === 0 and String(-0) == "0", so we have to |
| 1075 | // special-case. |
| 1076 | if (val === -0 && 1/val === -Infinity) { |
| 1077 | return "-0"; |
| 1078 | } |
| 1079 | return String(val); |
| 1080 | case "object": |
| 1081 | if (val === null) { |
| 1082 | return "null"; |
| 1083 | } |
| 1084 | |
| 1085 | // Special-case Node objects, since those come up a lot in my tests. I |
| 1086 | // ignore namespaces. |
| 1087 | if (is_node(val)) { |
| 1088 | switch (val.nodeType) { |
| 1089 | case Node.ELEMENT_NODE: |
| 1090 | var ret = "<" + val.localName; |
| 1091 | for (var i = 0; i < val.attributes.length; i++) { |
| 1092 | ret += " " + val.attributes[i].name + '="' + val.attributes[i].value + '"'; |
| 1093 | } |
| 1094 | ret += ">" + val.innerHTML + "</" + val.localName + ">"; |
| 1095 | return "Element node " + truncate(ret, 60); |
| 1096 | case Node.TEXT_NODE: |