/** * Copyright JS Foundation and other contributors, http://js.foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. **/ RED.utils = (function() { function formatString(str) { return str.replace(/\r?\n/g,"↵").replace(/\t/g,"→"); } function sanitize(m) { return m.replace(/&/g,"&").replace(//g,">"); } function buildMessageSummaryValue(value) { var result; if (Array.isArray(value)) { result = $('').html('array['+value.length+']'); } else if (value === null) { result = $('null'); } else if (typeof value === 'object') { if (value.hasOwnProperty('type') && value.type === 'Buffer' && value.hasOwnProperty('data')) { result = $('').html('buffer['+value.data.length+']'); } else if (value.hasOwnProperty('type') && value.type === 'array' && value.hasOwnProperty('data')) { result = $('').html('array['+value.length+']'); } else { result = $('object'); } } else if (typeof value === 'string') { var subvalue; if (value.length > 30) { subvalue = sanitize(value.substring(0,30))+"…"; } else { subvalue = sanitize(value); } result = $('').html('"'+formatString(subvalue)+'"'); } else { result = $('').text(""+value); } return result; } function makeExpandable(el,onexpand) { el.addClass("debug-message-expandable"); el.click(function(e) { var parent = $(this).parent(); if (parent.hasClass('collapsed')) { if (onexpand && !parent.hasClass('built')) { onexpand(); parent.addClass('built'); } parent.removeClass('collapsed'); } else { parent.addClass('collapsed'); } e.preventDefault(); }); } function buildMessageElement(obj,key,typeHint,hideKey) { var i; var e; var entryObj; var header; var headerHead; var value; var element = $(''); if (!key) { element.addClass("debug-message-top-level"); } header = $('').appendTo(element); if (key && !hideKey) { $('').text(key).appendTo(header); $(': ').appendTo(header); } entryObj = $('').appendTo(header); var isArray = Array.isArray(obj); var isArrayObject = false; if (obj && typeof obj === 'object' && obj.hasOwnProperty('type') && obj.hasOwnProperty('data') && ((obj.__encoded__ && obj.type === 'array') || obj.type === 'Buffer')) { isArray = true; isArrayObject = true; } if (obj === null || obj === undefined) { $(''+obj+'').appendTo(entryObj); } else if (typeof obj === 'string') { if (/[\t\n\r]/.test(obj)) { element.addClass('collapsed'); $(' ').prependTo(header); makeExpandable(header, function() { $('').html(typeHint||'string').appendTo(header); var row = $('').appendTo(element); $('
').text(obj).appendTo(row);
                });
            }
            e = $('').html('"'+formatString(sanitize(obj))+'"').appendTo(entryObj);
            if (/^#[0-9a-f]{6}$/i.test(obj)) {
                $('').css('backgroundColor',obj).appendTo(e);
            }

        } else if (typeof obj === 'number') {
            e = $('').text(""+obj).appendTo(entryObj);
            if (Number.isInteger(obj) && (obj >= 0)) { // if it's a +ve integer
                e.addClass("debug-message-type-number-toggle");
                e.click(function(evt) {
                    var format = $(this).data('format') || "date";
                    if (format === 'dec') {
                        $(this).text(""+obj).data('format','date');
                    } else if ((format === 'date') && (obj.toString().length===13) && (obj<=2147483647000)) {
                        $(this).text((new Date(obj)).toISOString()).data('format','hex');
                    } else if ((format === 'date') && (obj.toString().length===10) && (obj<=2147483647)) {
                        $(this).text((new Date(obj*1000)).toISOString()).data('format','hex');
                    } else {
                        $(this).text("0x"+(obj).toString(16)).data('format','dec');
                    }
                    evt.preventDefault();
                });
            }
        } else if (isArray) {
            element.addClass('collapsed');

            var originalLength = obj.length;
            if (typeHint) {
                var m = /\[(\d+)\]/.exec(typeHint);
                if (m) {
                    originalLength = parseInt(m[1]);
                }
            }
            var data = obj;
            var type = 'array';
            if (isArrayObject) {
                data = obj.data;
                if (originalLength === undefined) {
                    originalLength = data.length;
                }
                if (data.__encoded__) {
                    data = data.data;
                }
                type = obj.type.toLowerCase();
            } else if (/buffer/.test(typeHint)) {
                type = 'buffer';
            }
            var fullLength = data.length;

            if (originalLength > 0) {
                $(' ').prependTo(header);
                var arrayRows = $('
').appendTo(element); element.addClass('debug-message-buffer-raw'); makeExpandable(header,function() { if (!key) { headerHead = $('').html(typeHint||(type+'['+originalLength+']')).appendTo(header); } if (type === 'buffer') { var stringRow = $('
').appendTo(element); var sr = $('').appendTo(stringRow); var stringEncoding = ""; try { stringEncoding = String.fromCharCode.apply(null, new Uint16Array(data)) } catch(err) { console.log(err); } $('
').text(stringEncoding).appendTo(sr);
                        var bufferOpts = $('').appendTo(headerHead);
                        $('').addClass('selected').html('raw').appendTo(bufferOpts).click(function(e) {
                            if ($(this).text() === 'raw') {
                                $(this).text('string');
                                element.addClass('debug-message-buffer-string').removeClass('debug-message-buffer-raw');
                            } else {
                                $(this).text('raw');
                                element.removeClass('debug-message-buffer-string').addClass('debug-message-buffer-raw');
                            }
                            e.preventDefault();
                            e.stopPropagation();
                        })
                    }
                    var row;
                    if (fullLength <= 10) {
                        for (i=0;i