1 | "use strict";
|
2 | var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
3 | var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
4 | if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
5 | else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
6 | return c > 3 && r && Object.defineProperty(target, key, r), r;
|
7 | };
|
8 | var PrettyPrintPlugin_1;
|
9 | Object.defineProperty(exports, "__esModule", { value: true });
|
10 | const components_1 = require("../components");
|
11 | const events_1 = require("../events");
|
12 | var PrettyPrintState;
|
13 | (function (PrettyPrintState) {
|
14 | PrettyPrintState[PrettyPrintState["Default"] = 0] = "Default";
|
15 | PrettyPrintState[PrettyPrintState["Comment"] = 1] = "Comment";
|
16 | PrettyPrintState[PrettyPrintState["Pre"] = 2] = "Pre";
|
17 | })(PrettyPrintState || (PrettyPrintState = {}));
|
18 | let PrettyPrintPlugin = PrettyPrintPlugin_1 = class PrettyPrintPlugin extends components_1.RendererComponent {
|
19 | initialize() {
|
20 | this.listenTo(this.owner, events_1.PageEvent.END, this.onRendererEndPage, -1024);
|
21 | }
|
22 | onRendererEndPage(event) {
|
23 | let match;
|
24 | let line;
|
25 | let lineState;
|
26 | let lineDepth;
|
27 | let tagName;
|
28 | let preName;
|
29 | let tagExp = /<\s*(\w+)[^>]*>|<\/\s*(\w+)[^>]*>|<!--|-->/g;
|
30 | let emptyLineExp = /^[\s]*$/;
|
31 | let minLineDepth = 1;
|
32 | let state = PrettyPrintState.Default;
|
33 | const stack = [];
|
34 | const lines = (event.contents || '').split(/\r\n?|\n/);
|
35 | let index = 0;
|
36 | let count = lines.length;
|
37 | while (index < count) {
|
38 | line = lines[index];
|
39 | if (emptyLineExp.test(line)) {
|
40 | if (state === PrettyPrintState.Default) {
|
41 | lines.splice(index, 1);
|
42 | count -= 1;
|
43 | continue;
|
44 | }
|
45 | }
|
46 | else {
|
47 | lineState = state;
|
48 | lineDepth = stack.length;
|
49 | while (match = tagExp.exec(line)) {
|
50 | if (state === PrettyPrintState.Comment) {
|
51 | if (match[0] === '-->') {
|
52 | state = PrettyPrintState.Default;
|
53 | }
|
54 | }
|
55 | else if (state === PrettyPrintState.Pre) {
|
56 | if (match[2] && match[2].toLowerCase() === preName) {
|
57 | state = PrettyPrintState.Default;
|
58 | }
|
59 | }
|
60 | else {
|
61 | if (match[0] === '<!--') {
|
62 | state = PrettyPrintState.Comment;
|
63 | }
|
64 | else if (match[1]) {
|
65 | tagName = match[1].toLowerCase();
|
66 | if (tagName in PrettyPrintPlugin_1.IGNORED_TAGS) {
|
67 | continue;
|
68 | }
|
69 | if (tagName in PrettyPrintPlugin_1.PRE_TAGS) {
|
70 | state = PrettyPrintState.Pre;
|
71 | preName = tagName;
|
72 | }
|
73 | else {
|
74 | if (tagName === 'body') {
|
75 | minLineDepth = 2;
|
76 | }
|
77 | stack.push(tagName);
|
78 | }
|
79 | }
|
80 | else if (match[2]) {
|
81 | tagName = match[2].toLowerCase();
|
82 | if (tagName in PrettyPrintPlugin_1.IGNORED_TAGS) {
|
83 | continue;
|
84 | }
|
85 | const n = stack.lastIndexOf(tagName);
|
86 | if (n !== -1) {
|
87 | stack.length = n;
|
88 | }
|
89 | }
|
90 | }
|
91 | }
|
92 | if (lineState === PrettyPrintState.Default) {
|
93 | lineDepth = Math.min(lineDepth, stack.length);
|
94 | line = line.replace(/^\s+/, '').replace(/\s+$/, '');
|
95 | if (lineDepth > minLineDepth) {
|
96 | line = Array(lineDepth - minLineDepth + 1).join('\t') + line;
|
97 | }
|
98 | lines[index] = line;
|
99 | }
|
100 | }
|
101 | index++;
|
102 | }
|
103 | event.contents = lines.join('\n');
|
104 | }
|
105 | };
|
106 | PrettyPrintPlugin.IGNORED_TAGS = {
|
107 | area: true,
|
108 | base: true,
|
109 | br: true,
|
110 | wbr: true,
|
111 | col: true,
|
112 | command: true,
|
113 | embed: true,
|
114 | hr: true,
|
115 | img: true,
|
116 | input: true,
|
117 | link: true,
|
118 | meta: true,
|
119 | param: true,
|
120 | source: true
|
121 | };
|
122 | PrettyPrintPlugin.PRE_TAGS = {
|
123 | pre: true,
|
124 | code: true,
|
125 | textarea: true,
|
126 | script: true,
|
127 | style: true
|
128 | };
|
129 | PrettyPrintPlugin = PrettyPrintPlugin_1 = __decorate([
|
130 | components_1.Component({ name: 'pretty-print' })
|
131 | ], PrettyPrintPlugin);
|
132 | exports.PrettyPrintPlugin = PrettyPrintPlugin;
|
133 |
|
\ | No newline at end of file |