From 222821804e0b0d9f3a54fb15c1f856f25b51c579 Mon Sep 17 00:00:00 2001 From: ento Date: Thu, 30 Jul 2020 23:25:15 -0800 Subject: [PATCH] Fix Entity widget not rendering its content without a refresh (#4776) * Expose TEXT_NODE and ELEMENT_NODE constants * Add failing test for initial rendering of entity widget * Compute attributes when rendering --- core/modules/utils/fakedom.js | 24 +++++++++++++++++++-- core/modules/widgets/entity.js | 1 + editions/test/tiddlers/tests/test-widget.js | 18 ++++++++++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/core/modules/utils/fakedom.js b/core/modules/utils/fakedom.js index 3100e731d..ed371100e 100755 --- a/core/modules/utils/fakedom.js +++ b/core/modules/utils/fakedom.js @@ -21,14 +21,32 @@ var bumpSequenceNumber = function(object) { } }; +var TW_Node = function (){ + throw TypeError("Illegal constructor"); +}; + +Object.defineProperty(TW_Node.prototype, 'ELEMENT_NODE', { + get: function() { + return 1; + } +}); + +Object.defineProperty(TW_Node.prototype, 'TEXT_NODE', { + get: function() { + return 3; + } +}); + var TW_TextNode = function(text) { bumpSequenceNumber(this); this.textContent = text + ""; }; +TW_TextNode.prototype = Object.create(TW_Node.prototype); + Object.defineProperty(TW_TextNode.prototype, "nodeType", { get: function() { - return 3; + return this.TEXT_NODE; } }); @@ -49,6 +67,8 @@ var TW_Element = function(tag,namespace) { this.namespaceURI = namespace || "http://www.w3.org/1999/xhtml"; }; +TW_Element.prototype = Object.create(TW_Node.prototype); + Object.defineProperty(TW_Element.prototype, "style", { get: function() { return this._style; @@ -69,7 +89,7 @@ Object.defineProperty(TW_Element.prototype, "style", { Object.defineProperty(TW_Element.prototype, "nodeType", { get: function() { - return 1; + return this.ELEMENT_NODE; } }); diff --git a/core/modules/widgets/entity.js b/core/modules/widgets/entity.js index 5c885baea..0b5e68375 100755 --- a/core/modules/widgets/entity.js +++ b/core/modules/widgets/entity.js @@ -28,6 +28,7 @@ Render this widget into the DOM */ EntityWidget.prototype.render = function(parent,nextSibling) { this.parentDomNode = parent; + this.computeAttributes(); this.execute(); var entityString = this.getAttribute("entity",this.parseTreeNode.entity || ""), textNode = this.document.createTextNode($tw.utils.entityDecode(entityString)); diff --git a/editions/test/tiddlers/tests/test-widget.js b/editions/test/tiddlers/tests/test-widget.js index 0efc8b126..f3d500489 100755 --- a/editions/test/tiddlers/tests/test-widget.js +++ b/editions/test/tiddlers/tests/test-widget.js @@ -273,6 +273,24 @@ describe("Widget module", function() { expect(wrapper.innerHTML).toBe("

My linky link

"); }); + /* This test reproduces issue #4693. */ + it("should render the entity widget", function() { + var wiki = new $tw.Wiki(); + // Construct the widget node + var text = "\n\n<$entity entity=' ' />\n\n<$entity entity='✓' />\n"; + var widgetNode = createWidgetNode(parseText(text,wiki),wiki); + // Render the widget node to the DOM + var wrapper = renderWidgetNode(widgetNode); + // Test the rendering + expect(wrapper.innerHTML).toBe(" ✓"); + // Test the sequence numbers in the DOM + expect(wrapper.sequenceNumber).toBe(0); + expect(wrapper.children[0].sequenceNumber).toBe(1); + expect(wrapper.children[0].nodeType).toBe(wrapper.children[0].TEXT_NODE); + expect(wrapper.children[1].sequenceNumber).toBe(2); + expect(wrapper.children[1].nodeType).toBe(wrapper.children[1].TEXT_NODE); + }); + it("should deal with the list widget", function() { var wiki = new $tw.Wiki(); // Add some tiddlers