Search code examples
google-chromeencodingcharacter-encodingwebkitchromium

How does chrome establish the right character-encoding?


I've been working with a lot of charsets lately and I discovered a lot of issues when trying to establish the proper charset for a random web page. The charset can be set in the headers of the html document, or inside the <head> section, multiple times or sometimes the declaration is omitted. Despite these issues chrome dose a great job at setting the best charset every time.

I've tried searching the sources but didn't manage to find anything as I don't know where to look.

So my question is where could I find the algorithm?

Thanks


update:

problematic example:

HTTP header of a document (based on server configurations):
Content-type: text/html; charset=utf-8
and the document looks like:

<?xml version="1.0" encoding="ISO-8859-1"?>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="Content-type" content="text/html;charset=ISO-8859-1" />
</head>
<body>...</body>
</html>

Which encoding would be used to render the text?


Solution

  • Chrome uses https://github.com/google/compact_enc_det

    If you want to read the actual code that calls that project, the function is DetectTextEncoding in the file third_party/blink/renderer/platform/text/text_encoding_detector.cc