From b1ecf81b0c0727db2245cfeba78cc46a33c3a814 Mon Sep 17 00:00:00 2001 From: Jermolene Date: Tue, 21 Feb 2017 13:09:32 +0000 Subject: [PATCH] Tentative improvements to highlight plugin problems We now use highlight.js in raw HTML mode on the server, rather than trying to use it with the fakedom. This causes problems with fakedoms inability to get textContent for a node that has been created by assigning innerHTML. So we extend the fakedom to allow the original text content to be saved. See #2778 for discussion. --- core/modules/utils/fakedom.js | 18 +++++++++++++++++- plugins/tiddlywiki/highlight/highlightblock.js | 16 ++++++++++------ 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/core/modules/utils/fakedom.js b/core/modules/utils/fakedom.js index 5175cebc5..05a500fa3 100755 --- a/core/modules/utils/fakedom.js +++ b/core/modules/utils/fakedom.js @@ -206,13 +206,29 @@ Object.defineProperty(TW_Element.prototype, "innerHTML", { set: function(value) { this.isRaw = true; this.rawHTML = value; + this.rawTextContent = null; + } +}); + +Object.defineProperty(TW_Element.prototype, "textInnerHTML", { + set: function(value) { + if(this.isRaw) { + this.rawTextContent = value; + } else { + throw "Cannot set textInnerHTML of a non-raw TW_Element"; + } } }); Object.defineProperty(TW_Element.prototype, "textContent", { get: function() { if(this.isRaw) { - throw "Cannot get textContent on a raw TW_Element"; + if(this.rawTextContent === null) { + console.log(booboo) + throw "Cannot get textContent on a raw TW_Element"; + } else { + return this.rawTextContent; + } } else { var b = []; $tw.utils.each(this.children,function(node) { diff --git a/plugins/tiddlywiki/highlight/highlightblock.js b/plugins/tiddlywiki/highlight/highlightblock.js index 80bd93fbb..e5049936b 100644 --- a/plugins/tiddlywiki/highlight/highlightblock.js +++ b/plugins/tiddlywiki/highlight/highlightblock.js @@ -28,12 +28,16 @@ CodeBlockWidget.prototype.postRender = function() { language = tiddler.fields.text || ""; } if(language) { - try { - domNode.className = language.toLowerCase() + " hljs"; - hljs.highlightBlock(domNode); - } catch(err) { - // Can't easily tell if a language is registered or not in the packed version of hightlight.js, - // so we silently fail and the codeblock remains unchanged + domNode.className = language.toLowerCase() + " hljs"; + if($tw.browser && !domNode.isTiddlyWikiFakeDom) { + hljs.highlightBlock(domNode); + } else { + var text = domNode.textContent; + domNode.children[0].innerHTML = hljs.fixMarkup(hljs.highlight(language,text).value); + // If we're using the fakedom then specially save the original raw text + if(domNode.isTiddlyWikiFakeDom) { + domNode.children[0].textInnerHTML = text; + } } } };