Skip to content

Commit

Permalink
Merge pull request #2 from ZsharE/master
Browse files Browse the repository at this point in the history
Converted library to vanilla javascript
  • Loading branch information
ZsharE authored Aug 27, 2024
2 parents a803478 + 9fac2e4 commit 93c9a90
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 88 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
[![npm](https://img.shields.io/npm/v/jsstack.js.svg)](https://www.npmjs.com/package/jsstack.js)
![license](https://img.shields.io/hexpm/l/plug.svg?style=flat-square)

A simple and easy jQuery plugin for highlighting JavaScript stack traces
A simple and easy library for highlighting JavaScript stack traces

#### Demo
<img src="example.png" alt="jsStack.js - demo" width="700" />

#### Initialization
```
$('.stacktrace').jsStack();
```javascript
jsStack('.stacktrace');
```

#### Default values for classes
```
$('.stacktrace').jsStack({
```javascript
jsStack('.stacktrace', {
method: 'st-methodName',
file: 'st-fileName',
line: 'st-lineNumber',
Expand All @@ -23,7 +23,7 @@ $('.stacktrace').jsStack({
```

#### Ready to go css
```
```css
pre {padding: 20px 10px;}
pre, code {background-color: #333;color: #ffffff;}
.st-methodName {color: #70c9ba;font-weight: bolder;}
Expand Down
182 changes: 102 additions & 80 deletions jsStack.js
Original file line number Diff line number Diff line change
@@ -1,101 +1,123 @@
/*!
* jsStack v1.0.1
* A simple and easy jQuery plugin for highlighting JavaScript stack traces
* License : Apache 2
* Author : Stanescu Eduard-Dan (https://elmah.io)
* jsStack v2.0.0
* A simple and easy library for highlighting JavaScript stack traces
* License: Apache 2
* Author: https://elmah.io
*/
(function($) {
'use strict';

$.fn.jsStack = function(options) {

var settings = $.extend({
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD
define([], factory);
} else if (typeof module === 'object' && module.exports) {
// Node, CommonJS-like
module.exports = factory();
} else {
// Browser globals (root is window)
root.jsStack = factory();
}
}(typeof self !== 'undefined' ? self : this, function () {
'use strict';

// Default values for classes
function jsStack(element, options) {
// Default settings
var settings = Object.assign({
method: 'st-methodName',
file: 'st-fileName',
line: 'st-lineNumber',
column: 'st-column'

}, options);

return this.each(function() {
var UNKNOWN_FUNCTION = '<unknown>';
var chrome = /^\s*at (?:(?:(?:Anonymous function)?|((?:\[object object\])?\S+(?: \[as \S+\])?)) )?\(?((?:file|http|https):.*?):(\d+)(?::(\d+))?\)?\s*$/i,
gecko = /^(?:\s*([^@]*)(?:\((.*?)\))?@)?(\S.*?):(\d+)(?::(\d+))?\s*$/i,
node = /^\s*at (?:((?:\[object object\])?\S+(?: \[as \S+\])?) )?\(?(.*?):(\d+)(?::(\d+))?\)?\s*$/i,
other = /^\s*at (?:(?:(?:Anonymous function)?|((?:\[object object\])?\S+(?: \[as \S+\])?)) )\(?((?:).*?)?\)?\s*$/i;

var UNKNOWN_FUNCTION = '<unknown>';
var chrome = /^\s*at (?:(?:(?:Anonymous function)?|((?:\[object object\])?\S+(?: \[as \S+\])?)) )?\(?((?:file|http|https):.*?):(\d+)(?::(\d+))?\)?\s*$/i,
gecko = /^(?:\s*([^@]*)(?:\((.*?)\))?@)?(\S.*?):(\d+)(?::(\d+))?\s*$/i,
node = /^\s*at (?:((?:\[object object\])?\S+(?: \[as \S+\])?) )?\(?(.*?):(\d+)(?::(\d+))?\)?\s*$/i,
other = /^\s*at (?:(?:(?:Anonymous function)?|((?:\[object object\])?\S+(?: \[as \S+\])?)) )\(?((?:).*?)?\)?\s*$/i;
var stacktrace = escapeHtml($(this).text()),
lines = stacktrace.split('\n'),
stack = '',
parts,
element;
var stacktrace = escapeHtml(element.textContent),
lines = stacktrace.split('\n'),
stack = '',
parts,
elementObj;

function template_line(line, element) {
line = line.replace(element.file, '<span class="'+ settings.file +'">' + element.file + '</span>')
.replace(element.methodName + ' (', '<span class="'+ settings.method +'">' + element.methodName + '</span> (')
.replace(':' + element.lineNumber + ':' + element.column, ':<span class="'+ settings.line +'">' + element.lineNumber + '</span>:<span class="'+ settings.column +'">' + element.column + '</span>');
line = line.replace(/&lt;/g, '<span>&lt;</span>').replace(/&gt;/g, '<span>&gt;</span>');
function template_line(line, element) {
line = line.replace(element.file, '<span class="'+ settings.file +'">' + element.file + '</span>')
.replace(element.methodName + ' (', '<span class="'+ settings.method +'">' + element.methodName + '</span> (')
.replace(':' + element.lineNumber + ':' + element.column, ':<span class="'+ settings.line +'">' + element.lineNumber + '</span>:<span class="'+ settings.column +'">' + element.column + '</span>');
line = line.replace(/&lt;/g, '<span>&lt;</span>').replace(/&gt;/g, '<span>&gt;</span>');

return line;
}
return line;
}

function escapeHtml(unsafe) {
return unsafe
.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;")
.replace(/'/g, "&#039;");
}
function escapeHtml(unsafe) {
return unsafe
.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;")
.replace(/'/g, "&#039;");
}

for (var i = 0, j = lines.length; i < j; ++i) {
var line = '';
for (var i = 0, j = lines.length; i < j; ++i) {
var line = '';

if ((parts = gecko.exec(lines[i]))) {
element = {
'file': parts[3],
'methodName': parts[1] || UNKNOWN_FUNCTION,
'lineNumber': +parts[4],
'column': parts[5] ? +parts[5] : null
};
line = template_line(lines[i], element);
} else if ((parts = chrome.exec(lines[i]))) {
element = {
'file': parts[2],
'methodName': parts[1] || UNKNOWN_FUNCTION,
'lineNumber': +parts[3],
'column': parts[4] ? +parts[4] : null
};
line = template_line(lines[i], element);
} else if ((parts = node.exec(lines[i]))) {
element = {
'file': parts[2],
'methodName': parts[1] || UNKNOWN_FUNCTION,
'lineNumber': +parts[3],
'column': parts[4] ? +parts[4] : null
};
line = template_line(lines[i], element);
} else if ((parts = other.exec(lines[i]))) {
element = {
'file': parts[2],
'methodName': parts[1] || UNKNOWN_FUNCTION
};
line = template_line(lines[i], element);
} else {
line = lines[i].replace(/&lt;/g, '<span>&lt;</span>').replace(/&gt;/g, '<span>&gt;</span>');
}
if ((parts = gecko.exec(lines[i]))) {
elementObj = {
'file': parts[3],
'methodName': parts[1] || UNKNOWN_FUNCTION,
'lineNumber': +parts[4],
'column': parts[5] ? +parts[5] : null
};
line = template_line(lines[i], elementObj);
} else if ((parts = chrome.exec(lines[i]))) {
elementObj = {
'file': parts[2],
'methodName': parts[1] || UNKNOWN_FUNCTION,
'lineNumber': +parts[3],
'column': parts[4] ? +parts[4] : null
};
line = template_line(lines[i], elementObj);
} else if ((parts = node.exec(lines[i]))) {
elementObj = {
'file': parts[2],
'methodName': parts[1] || UNKNOWN_FUNCTION,
'lineNumber': +parts[3],
'column': parts[4] ? +parts[4] : null
};
line = template_line(lines[i], elementObj);
} else if ((parts = other.exec(lines[i]))) {
elementObj = {
'file': parts[2],
'methodName': parts[1] || UNKNOWN_FUNCTION
};
line = template_line(lines[i], elementObj);
} else {
line = lines[i].replace(/&lt;/g, '<span>&lt;</span>').replace(/&gt;/g, '<span>&gt;</span>');
}

if (lines.length - 1 == i) {
stack += line;
} else {
stack += line + '\n';
}
if (lines.length - 1 == i) {
stack += line;
} else {
stack += line + '\n';
}
}

element.innerHTML = stack;
}

return $(this).html(stack);
// Function to initialize the plugin on elements
function initJsStack(elements, options) {
elements.forEach(function(element) {
jsStack(element, options);
});
};
}

// Expose the plugin globally
function jsStackLibrary(selector, options) {
var elements = document.querySelectorAll(selector);
initJsStack(Array.from(elements), options);
}

return jsStackLibrary;

}(jQuery));
}));
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "jsstack.js",
"version": "1.0.1",
"description": "A simple and easy jQuery plugin for highlighting JavaScript stack traces",
"version": "2.0.0",
"description": "A simple and easy library for highlighting JavaScript stack traces",
"main": "jsStack.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
Expand Down

0 comments on commit 93c9a90

Please sign in to comment.