Проблема с кодировкой getHtml () в officejs - PullRequest
0 голосов
/ 25 апреля 2018

У меня возникла проблема с методом getHtml() в officejs (Javascript API для Word).

У меня есть Word Addin, работающий в Word 2016 (32-разрядная версия, для настольных компьютеров), который использует следующий код для извлечения html тела документа:

  Word.run(function (context) {

        // Queue a command to get the current selection and then
        // create a proxy range object with the results.
        var range = context.document.body;

        var bodyHTML = range.getHtml();
        // variable for keeping the search results for the longest word.
        var searchResults;

        // Queue a command to load the range selection result.
        context.load(range, 'text');

        // Synchronize the document state by executing the queued commands
        // and return a promise to indicate task completion.
        return context.sync()
            .then(function () {
               let html = bodyHTML.value; 
            });
    })
    .catch(errorHandler);

Однако возвращенный HTML содержит неправильные символы для кодировки в метатеге <meta http-equiv=Content-Type content="text/html; charset=utf-8">

Например, в этом документе: example document containing some double quotes and ellipsis

Я получаю следующее: debug view of html produced by API

Как получить HTML в правильной кодировке?

Обновление

Чтобы доказать, что это не проблема визуализатора, вот скриншот html визуализатора тоже:

HTML visualizer

Еще одно обновление ...

Вот мой временный обходной путь ( Примечание: не решение этой проблемы, это просто временное исправление, которое я использую для продолжения разработки, надеясь, что API исправлен ):

 function fixAnsiUtf8Issue(str) {
        var repl =
            [
            { "from": "€", "to": "€" },
            { "from": "‚", "to": "‚" },
            { "from": "„", "to": "„" },
            { "from": "…", "to": "…" },
            { "from": "‡", "to": "‡" },
            { "from": "‰", "to": "‰" },
            { "from": "‹", "to": "‹" },
            { "from": "‘", "to": "‘" },
            { "from": "’", "to": "’" },
            { "from": "“", "to": "“" },
            { "from": "•", "to": "•" },
            { "from": "–", "to": "–" },
            { "from": "—", "to": "—" },
            { "from": "â„¢", "to": "™" },
            { "from": "›", "to": "›" },
            // worried that this might interfere with the entry below - which is much more common in our context (hence the commenting out)
            //{ "from": "†", "to": "†" },
            { "from": "â€", "to": "”" },
            { "from": "Æ’", "to": "ƒ" },
            { "from": "ˆ", "to": "ˆ" },
            { "from": "Å’", "to": "Œ" },
            { "from": "Ž", "to": "Ž" },
            { "from": "Ëœ", "to": "˜" },
            { "from": "Å¡", "to": "š" },
            { "from": "Å“", "to": "œ" },
            { "from": "ž", "to": "ž" },
            { "from": "Ÿ", "to": "Ÿ" },
            { "from": "¡", "to": "¡" },
            { "from": "¢", "to": "¢" },
            { "from": "£", "to": "£" },
            { "from": "¤", "to": "¤" },
            { "from": "Â¥", "to": "¥" },
            { "from": "¦", "to": "¦" },
            { "from": "§", "to": "§" },
            { "from": "¨", "to": "¨" },
            { "from": "©", "to": "©" },
            { "from": "ª", "to": "ª" },
            { "from": "«", "to": "«" },
            { "from": "¬", "to": "¬" },
            { "from": "®", "to": "®" },
            { "from": "¯", "to": "¯" },
            { "from": "°", "to": "°" },
            { "from": "±", "to": "±" },
            { "from": "²", "to": "²" },
            { "from": "³", "to": "³" },
            { "from": "´", "to": "´" },
            { "from": "µ", "to": "µ" },
            { "from": "¶", "to": "¶" },
            { "from": "·", "to": "·" },
            { "from": "¸", "to": "¸" },
            { "from": "¹", "to": "¹" },
            { "from": "º", "to": "º" },
            { "from": "»", "to": "»" },
            { "from": "¼", "to": "¼" },
            { "from": "½", "to": "½" },
            { "from": "¾", "to": "¾" },
            { "from": "¿", "to": "¿" },
            { "from": "À", "to": "À" },
            { "from": "Â", "to": "" },
            { "from": "Â", "to": "Â" },
            { "from": "Ã", "to": "Ã" },
            { "from": "Ä", "to": "Ä" },
            { "from": "Ã…", "to": "Å" },
            { "from": "Æ", "to": "Æ" },
            { "from": "Ç", "to": "Ç" },
            { "from": "È", "to": "È" },
            { "from": "É", "to": "É" },
            { "from": "Ê", "to": "Ê" },
            { "from": "Ë", "to": "Ë" },
            { "from": "ÃŒ", "to": "Ì" },
            { "from": "ÃŽ", "to": "Î" },
            { "from": "Ñ", "to": "Ñ" },
            { "from": "Ã’", "to": "Ò" },
            { "from": "Ó", "to": "Ó" },
            { "from": "Ô", "to": "Ô" },
            { "from": "Õ", "to": "Õ" },
            { "from": "Ö", "to": "Ö" },
            { "from": "×", "to": "×" },
            { "from": "Ø", "to": "Ø" },
            { "from": "Ù", "to": "Ù" },
            { "from": "Ú", "to": "Ú" },
            { "from": "Û", "to": "Û" },
            { "from": "Ãœ", "to": "Ü" },
            { "from": "Þ", "to": "Þ" },
            { "from": "ß", "to": "ß" },
            { "from": "á", "to": "á" },
            { "from": "â", "to": "â" },
            { "from": "ã", "to": "ã" },
            { "from": "ä", "to": "ä" },
            { "from": "Ã¥", "to": "å" },
            { "from": "æ", "to": "æ" },
            { "from": "ç", "to": "ç" },
            { "from": "è", "to": "è" },
            { "from": "é", "to": "é" },
            { "from": "ê", "to": "ê" },
            { "from": "ë", "to": "ë" },
            { "from": "ì", "to": "ì" },
            { "from": "í", "to": "í" },
            { "from": "î", "to": "î" },
            { "from": "ï", "to": "ï" },
            { "from": "ð", "to": "ð" },
            { "from": "ñ", "to": "ñ" },
            { "from": "ò", "to": "ò" },
            { "from": "ó", "to": "ó" },
            { "from": "ô", "to": "ô" },
            { "from": "õ", "to": "õ" },
            { "from": "ö", "to": "ö" },
            { "from": "÷", "to": "÷" },
            { "from": "ø", "to": "ø" },
            { "from": "ù", "to": "ù" },
            { "from": "ú", "to": "ú" },
            { "from": "û", "to": "û" },
            { "from": "ü", "to": "ü" },
            { "from": "ý", "to": "ý" },
            { "from": "þ", "to": "þ" },
            { "from": "ÿ", "to": "ÿ" },
            { "from": "Ã ", "to": "à" },
            { "from": "Å ", "to": "Š" },
            { "from": "Ã", "to": "Á" },
            { "from": "Ã", "to": "Í" },
            { "from": "Ã", "to": "Ï" },
            { "from": "Ã", "to": "Ð" },
            { "from": "Ã", "to": "Ý" },
            ];

      for (let i = 0, replLength = repl.length; i < replLength; i++) {
          const o = repl[i];
          var re = new RegExp(o.from, 'g');
          str = str.replace(re, o.to);
      }

      return str;
    }
...