/** * 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.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,expand) { 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(); }); if (expand) { el.click(); } } var pinnedPaths = {}; var formattedPaths = {}; function addMessageControls(obj,sourceId,key,msg,rootPath,strippedKey) { if (!pinnedPaths.hasOwnProperty(sourceId)) { pinnedPaths[sourceId] = {} } var tools = $('').appendTo(obj); var copyTools = $('').appendTo(tools); var copyPath = $('').appendTo(copyTools).click(function(e) { e.preventDefault(); e.stopPropagation(); RED.clipboard.copyText(key,copyPath,"clipboard.copyMessagePath"); }) var copyPayload = $('').appendTo(copyTools).click(function(e) { e.preventDefault(); e.stopPropagation(); RED.clipboard.copyText(msg,copyPayload,"clipboard.copyMessageValue"); }) if (strippedKey !== '') { var isPinned = pinnedPaths[sourceId].hasOwnProperty(strippedKey); var pinPath = $('').appendTo(tools).click(function(e) { e.preventDefault(); e.stopPropagation(); if (pinnedPaths[sourceId].hasOwnProperty(strippedKey)) { delete pinnedPaths[sourceId][strippedKey]; $(this).removeClass("selected"); obj.removeClass("debug-message-row-pinned"); } else { var rootedPath = "$"+(strippedKey[0] === '['?"":".")+strippedKey; pinnedPaths[sourceId][strippedKey] = normalisePropertyExpression(rootedPath); $(this).addClass("selected"); obj.addClass("debug-message-row-pinned"); } }).toggleClass("selected",isPinned); obj.toggleClass("debug-message-row-pinned",isPinned); } } function checkExpanded(strippedKey,expandPaths,minRange,maxRange) { if (expandPaths && expandPaths.length > 0) { if (strippedKey === '' && minRange === undefined) { return true; } for (var i=0;i'); header = $('').appendTo(element); if (sourceId) { addMessageControls(header,sourceId,path,obj,rootPath,strippedKey); } if (!key) { element.addClass("debug-message-top-level"); if (sourceId) { var pinned = pinnedPaths[sourceId]; expandPaths = []; if (pinned) { for (var pinnedPath in pinned) { if (pinned.hasOwnProperty(pinnedPath)) { try { var res = getMessageProperty({$:obj},pinned[pinnedPath]); if (res !== undefined) { expandPaths.push(pinnedPath); } } catch(err) { } } } expandPaths.sort(); } } } else { if (!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);
                },checkExpanded(strippedKey,expandPaths));
            }
            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 = $('').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) {
                    evt.preventDefault();
                    formatNumber($(this), obj, sourceId, path, true);
                });
            }
            formatNumber(e,obj,sourceId,path,false);

        } 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'); if (key) { headerHead = $('').html(typeHint||(type+'['+originalLength+']')).appendTo(entryObj); } else { headerHead = $('').appendTo(entryObj); $('[ ').appendTo(headerHead); var arrayLength = Math.min(originalLength,10); for (i=0;i, ').appendTo(headerHead); } } if (originalLength > arrayLength) { $('').appendTo(headerHead); } if (arrayLength === 0) { $('empty').appendTo(headerHead); } $(' ]').appendTo(headerHead); } 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);
                        var switchFormat = $('').addClass('selected').html('raw').appendTo(bufferOpts).click(function(e) {
                            e.preventDefault();
                            e.stopPropagation();
                            formatBuffer(element,$(this),sourceId,path,true);
                        });
                        formatBuffer(element,switchFormat,sourceId,path,false);

                    }
                    var row;
                    if (fullLength <= 10) {
                        for (i=0;i