This repository has been archived by the owner on Aug 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathx-pretty-json.html
90 lines (74 loc) · 1.95 KB
/
x-pretty-json.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<link rel="import" href="../polymer/polymer.html">
<dom-module id="x-pretty-json">
<template>
<style>
@import 'https://fonts.googleapis.com/css?family=Source+Code+Pro';
:host {
display: block;
width: 100%;
}
pre {
background-color: #282c34;
border: 1px solid #181a1f;
border-radius: 3px;
padding: 10px 20px;
margin: 20px;
resize: both;
overflow: auto;
}
#code {
font-family: "Source Code Pro", monospace;
font-size: 14px;
color: #abb2bf;
white-space: pre-wrap;
word-wrap: break-word;
}
.json-key {
color: #e06c75;
}
.json-value {
color: #56b6c2;
}
.json-string {
color: #98c379;
}
</style>
<pre id="test"><code id="code"></code></pre>
</template>
<script>
Polymer({
is: 'x-pretty-json',
properties: {
object: {
type: Object,
observer: '_objChanged'
}
},
_objChanged: function(object) {
if (object) {
var jsonLine = /^( *)("[$\w]+": )?("[^"]*"|[\w.+-]*)?([,[{])?$/mg;
var markup = JSON.stringify(object, null, 3)
.replace(/&/g, '&')
.replace(/\\"/g, '"')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(jsonLine, this.replacer);
this.$.code.innerHTML = markup;
}
},
replacer: function(match, pIndent, pKey, pVal, pEnd) {
var key = '<span class=json-key>';
var val = '<span class=json-value>';
var str = '<span class=json-string>';
var r = pIndent || '';
if (pKey) {
r = r + key + pKey.replace(/[": ]/g, '') + '</span>: ';
}
if (pVal) {
r = r + (pVal[0] == '"' ? str : val) + pVal + '</span>';
}
return r + (pEnd || '');
}
})
</script>
</dom-module>