forked from phcode-dev/staging.phcode.dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEncodingDetector.js
More file actions
1 lines (1 loc) · 2.62 KB
/
Copy pathEncodingDetector.js
File metadata and controls
1 lines (1 loc) · 2.62 KB
1
define(function(require,exports,module){const FileUtils=require("file/FileUtils"),LanguageManager=require("language/LanguageManager"),SNIFFABLE_EXTENSIONS=["html","htm","xhtml","shtml","php","xml"],SNIFF_BYTE_LIMIT=1024,META_CHARSET_RE=/<meta[^>]+charset\s*=\s*["']?\s*([a-zA-Z0-9_\-:.]+)/i,CHARSET_ALIASES={latin1:"windows1252",iso88591:"windows1252"};function _normalizeEncodingName(name){return name.toLowerCase().replace(/[^0-9a-z]/g,"")}const BOM_SIGNATURES=[{bytes:[239,187,191],encoding:"utf8"},{bytes:[255,254,0,0],encoding:"utf32le"},{bytes:[0,0,254,255],encoding:"utf32be"},{bytes:[255,254],encoding:"utf16le"},{bytes:[254,255],encoding:"utf16be"}];function _detectBOM(bytes){for(const sig of BOM_SIGNATURES)if(bytes.length>=sig.bytes.length&&sig.bytes.every(function(b,i){return bytes[i]===b}))return sig.encoding;return null}function _extractDeclaredCharset(bytes){const prefix=bytes.subarray(0,Math.min(bytes.length,SNIFF_BYTE_LIMIT));let asciiText="";for(let i=0;i<prefix.length;i++)asciiText+=String.fromCharCode(prefix[i]);const match=META_CHARSET_RE.exec(asciiText);return match?match[1].toLowerCase():null}function _isValidUTF8(bytes){try{return new TextDecoder("utf8",{fatal:!0}).decode(bytes),!0}catch(e){return!1}}function detectEncodingFromBytes(extension,bytes,supportedEncodings){if(!bytes||!bytes.length)return null;const bom=_detectBOM(bytes);if(bom)return"utf8"===bom?null:bom;if(-1===SNIFFABLE_EXTENSIONS.indexOf(extension))return null;if(_isValidUTF8(bytes))return null;const rawDeclared=_extractDeclaredCharset(bytes);if(!rawDeclared)return null;let declared=_normalizeEncodingName(rawDeclared);return"utf8"===(declared=CHARSET_ALIASES[declared]||declared)?null:(supportedEncodings=supportedEncodings||"undefined"!=typeof fs&&fs.SUPPORTED_ENCODINGS)&&-1===supportedEncodings.indexOf(declared)?null:declared}function detectFileEncoding(file){const result=new $.Deferred,language=LanguageManager.getLanguageForPath(file.fullPath);if(language.isBinary())return result.resolve(null),result.promise();const extension=FileUtils.getFileExtension(file.fullPath).toLowerCase();return file.read({encoding:window.fs.BYTE_ARRAY_ENCODING,doNotCache:!0},function(err,content){if(err||!content)return void result.resolve(null);const bytes=new Uint8Array(content);result.resolve(detectEncodingFromBytes(extension,bytes,window.fs.SUPPORTED_ENCODINGS))}),result.promise()}function isKnownTextEncoding(encoding){return!!encoding&&encoding!==window.fs.BYTE_ARRAY_ENCODING}exports.SNIFFABLE_EXTENSIONS=SNIFFABLE_EXTENSIONS,exports.detectEncodingFromBytes=detectEncodingFromBytes,exports.detectFileEncoding=detectFileEncoding,exports.isKnownTextEncoding=isKnownTextEncoding});