From d2a226852b459cf2290924915a4e23317cf2aa9d Mon Sep 17 00:00:00 2001 From: or-else Date: Fri, 15 Sep 2017 18:04:02 -0700 Subject: [PATCH] fixed drafty's handling of undefined values --- README.md | 2 +- drafty.js | 13 ++++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f80e731c..36e11a0f 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ and [icons](https://google.github.io/material-design-icons/#icon-font-for-the-we This is work in progress. Bugs are expected. The app was tested in the latest Chrome & Firefox only. -Try it (possibly older version) live at http://api.tinode.co/x/example-react-js/. Login as one of `alice`, `bob`, `carol`, `dave`, `frank`. Password is `123`, e.g. password for `alice` is `alice123`. You can discover other users by email or phone by prefixing them with `email:` or `tel:` respectively. Emails are `@example.com`, e.g. `alice@example.com`, phones are `17025550001` through `17025550009`. The demo is reset every night at 2:15 am Pacific time. +Try it (possibly newer or older version) live at https://api.tinode.co/x/example-react-js/. Login as one of `alice`, `bob`, `carol`, `dave`, `frank`. Password is `123`, e.g. password for `alice` is `alice123`. You can discover other users by email or phone by prefixing them with `email:` or `tel:` respectively. Emails are `@example.com`, e.g. `alice@example.com`, phones are `17025550001` through `17025550009`. The demo is reset every night at 2:15 am Pacific time. License: [Apache 2.0](http://www.apache.org/licenses/LICENSE-2.0) diff --git a/drafty.js b/drafty.js index eb304af4..b845c3ea 100644 --- a/drafty.js +++ b/drafty.js @@ -507,6 +507,12 @@ Sample JSON representation of the text above: var spans = [].concat(fmt); + // Zero values may have been stripped. Restore them. + spans.map(function(s) { + s.at = s.at || 0; + s.len = s.len || 0; + }); + // Soft spans first by start index (asc) then by length (desc). spans.sort(function(a, b) { if (a.at - b.at == 0) { @@ -515,18 +521,23 @@ Sample JSON representation of the text above: return a.at - b.at; }); + console.log(spans); + // Denormalize entities into spans. Create a copy of the objects to leave // original Drafty object unchanged. spans = spans.map(function(s) { var data; var tp = s.tp; - if (s.key + 1 > 0) { + if (!tp) { + s.key = s.key || 0; data = ent[s.key].data; tp = ent[s.key].tp; } return {tp: tp, data: data, at: s.at, len: s.len}; }); + console.log(spans); + return forEach(txt, 0, txt.length, spans, formatter, context); },