fixed drafty's handling of undefined values

This commit is contained in:
or-else
2017-09-15 18:04:02 -07:00
parent 9c7a05f616
commit d2a226852b
2 changed files with 13 additions and 2 deletions
+1 -1
View File
@@ -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. 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 `<login>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 `<login>@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 `<login>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 `<login>@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) License: [Apache 2.0](http://www.apache.org/licenses/LICENSE-2.0)
+12 -1
View File
@@ -507,6 +507,12 @@ Sample JSON representation of the text above:
var spans = [].concat(fmt); 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). // Soft spans first by start index (asc) then by length (desc).
spans.sort(function(a, b) { spans.sort(function(a, b) {
if (a.at - b.at == 0) { if (a.at - b.at == 0) {
@@ -515,18 +521,23 @@ Sample JSON representation of the text above:
return a.at - b.at; return a.at - b.at;
}); });
console.log(spans);
// Denormalize entities into spans. Create a copy of the objects to leave // Denormalize entities into spans. Create a copy of the objects to leave
// original Drafty object unchanged. // original Drafty object unchanged.
spans = spans.map(function(s) { spans = spans.map(function(s) {
var data; var data;
var tp = s.tp; var tp = s.tp;
if (s.key + 1 > 0) { if (!tp) {
s.key = s.key || 0;
data = ent[s.key].data; data = ent[s.key].data;
tp = ent[s.key].tp; tp = ent[s.key].tp;
} }
return {tp: tp, data: data, at: s.at, len: s.len}; return {tp: tp, data: data, at: s.at, len: s.len};
}); });
console.log(spans);
return forEach(txt, 0, txt.length, spans, formatter, context); return forEach(txt, 0, txt.length, spans, formatter, context);
}, },