Welcome to mirror list, hosted at ThFree Co, Russian Federation.

text.js « vs « src - github.com/microsoft/vscode.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b363b799e4c9050c951dcc2b76e3e5239fd0d0b9 (plain)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
/// <reference path="declares.ts" />
/// <reference path="loader.ts" />
'use strict';
var TextLoaderPlugin;
(function (TextLoaderPlugin) {
    var BrowserTextLoader = (function () {
        function BrowserTextLoader() {
        }
        BrowserTextLoader.prototype.load = function (name, fileUrl, externalCallback, externalErrorback) {
            var req = new XMLHttpRequest();
            req.onreadystatechange = function () {
                if (req.readyState === 4) {
                    if ((req.status >= 200 && req.status < 300) || req.status === 1223 || (req.status === 0 && req.responseText && req.responseText.length > 0)) {
                        externalCallback(req.responseText);
                    }
                    else {
                        externalErrorback(req);
                    }
                    req.onreadystatechange = null;
                }
            };
            req.open('GET', fileUrl, true);
            req.responseType = '';
            req.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
            req.send(null);
        };
        return BrowserTextLoader;
    })();
    function readFileAndRemoveBOM(fs, path) {
        var BOM_CHAR_CODE = 65279;
        var contents = fs.readFileSync(path, 'utf8');
        // Remove BOM
        if (contents.charCodeAt(0) === BOM_CHAR_CODE) {
            contents = contents.substring(1);
        }
        return contents;
    }
    var NodeTextLoader = (function () {
        function NodeTextLoader() {
            this.fs = require.nodeRequire('fs');
        }
        NodeTextLoader.prototype.load = function (name, fileUrl, callback, errorback) {
            callback(readFileAndRemoveBOM(this.fs, fileUrl));
        };
        return NodeTextLoader;
    })();
    // ------------------------------ Finally, the plugin
    var TextPlugin = (function () {
        function TextPlugin(textLoader) {
            this.textLoader = textLoader;
        }
        TextPlugin.prototype.load = function (name, req, load, config) {
            config = config || {};
            var myConfig = config['vs/text'] || {};
            var myPaths = myConfig.paths || {};
            var redirectedName = name;
            for (var path in myPaths) {
                if (myPaths.hasOwnProperty(path)) {
                    if (name.indexOf(path) === 0) {
                        redirectedName = myPaths[path] + name.substr(path.length);
                    }
                }
            }
            var fileUrl = req.toUrl(redirectedName);
            this.textLoader.load(name, fileUrl, function (contents) {
                if (config.isBuild) {
                    TextPlugin.BUILD_MAP[name] = contents;
                }
                load(contents);
            }, function (err) {
                if (typeof load.error === 'function') {
                    load.error('Could not find ' + fileUrl);
                }
            });
        };
        TextPlugin.prototype.write = function (pluginName, moduleName, write) {
            if (TextPlugin.BUILD_MAP.hasOwnProperty(moduleName)) {
                var escapedText = Utilities.escapeText(TextPlugin.BUILD_MAP[moduleName]);
                write('define("' + pluginName + '!' + moduleName + '", function () { return "' + escapedText + '"; });');
            }
        };
        TextPlugin.BUILD_MAP = {};
        return TextPlugin;
    })();
    TextLoaderPlugin.TextPlugin = TextPlugin;
    var Utilities = (function () {
        function Utilities() {
        }
        /**
         * Escape text such that it can be used in a javascript string enclosed by double quotes (")
         */
        Utilities.escapeText = function (text) {
            // http://www.javascriptkit.com/jsref/escapesequence.shtml
            // \b	Backspace.
            // \f	Form feed.
            // \n	Newline.
            // \O	Nul character.
            // \r	Carriage return.
            // \t	Horizontal tab.
            // \v	Vertical tab.
            // \'	Single quote or apostrophe.
            // \"	Double quote.
            // \\	Backslash.
            // \ddd	The Latin-1 character specified by the three octal digits between 0 and 377. ie, copyright symbol is \251.
            // \xdd	The Latin-1 character specified by the two hexadecimal digits dd between 00 and FF.  ie, copyright symbol is \xA9.
            // \udddd	The Unicode character specified by the four hexadecimal digits dddd. ie, copyright symbol is \u00A9.
            var _backspace = '\b'.charCodeAt(0);
            var _formFeed = '\f'.charCodeAt(0);
            var _newLine = '\n'.charCodeAt(0);
            var _nullChar = 0;
            var _carriageReturn = '\r'.charCodeAt(0);
            var _tab = '\t'.charCodeAt(0);
            var _verticalTab = '\v'.charCodeAt(0);
            var _backslash = '\\'.charCodeAt(0);
            var _doubleQuote = '"'.charCodeAt(0);
            var startPos = 0, chrCode, replaceWith = null, resultPieces = [];
            for (var i = 0, len = text.length; i < len; i++) {
                chrCode = text.charCodeAt(i);
                switch (chrCode) {
                    case _backspace:
                        replaceWith = '\\b';
                        break;
                    case _formFeed:
                        replaceWith = '\\f';
                        break;
                    case _newLine:
                        replaceWith = '\\n';
                        break;
                    case _nullChar:
                        replaceWith = '\\0';
                        break;
                    case _carriageReturn:
                        replaceWith = '\\r';
                        break;
                    case _tab:
                        replaceWith = '\\t';
                        break;
                    case _verticalTab:
                        replaceWith = '\\v';
                        break;
                    case _backslash:
                        replaceWith = '\\\\';
                        break;
                    case _doubleQuote:
                        replaceWith = '\\"';
                        break;
                }
                if (replaceWith !== null) {
                    resultPieces.push(text.substring(startPos, i));
                    resultPieces.push(replaceWith);
                    startPos = i + 1;
                    replaceWith = null;
                }
            }
            resultPieces.push(text.substring(startPos, len));
            return resultPieces.join('');
        };
        return Utilities;
    })();
    TextLoaderPlugin.Utilities = Utilities;
    (function () {
        var textLoader = null;
        var isAtomShell = (typeof process !== 'undefined' && typeof process.versions !== 'undefined' && typeof process.versions['electron'] !== 'undefined');
        if (typeof process !== 'undefined' && process.versions && !!process.versions.node && !isAtomShell) {
            textLoader = new NodeTextLoader();
        }
        else {
            textLoader = new BrowserTextLoader();
        }
        define('vs/text', new TextPlugin(textLoader));
    })();
})(TextLoaderPlugin || (TextLoaderPlugin = {}));