Skip to content
This repository was archived by the owner on Apr 4, 2019. It is now read-only.

Commit 20a565a

Browse files
committed
Add temporary workaround for SimpleDOM
The current parseHTML implementation requires some DOM APIs not implemented in SimpleDOM. This commit adds a temporary workaround to support using SimpleDOM on the server. In the future, we will move towards using `insertAdjacentHTML` and `innerHTML=` as the main APIs and remove the need of `parseHTML` altogether. This commit also reverts a previous commit that introduced the `setMorphHTML` method, which does exactly what `Morph#setHTML` does.
1 parent b0994e1 commit 20a565a

File tree

3 files changed

+64
-34
lines changed

3 files changed

+64
-34
lines changed

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@
4444
"emberjs-build": "0.2.1",
4545
"git-repo-version": "^0.1.2",
4646
"handlebars": "^3.0.2",
47-
"morph-range": "0.2.5",
47+
"morph-range": "0.2.6",
4848
"qunit": "^0.7.2",
49-
"rsvp": "~3.0.6"
49+
"rsvp": "~3.0.6",
50+
"simple-dom": "~0.3.0"
5051
}
5152
}

packages/dom-helper/lib/main.js

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -519,49 +519,50 @@ prototype.insertBoundary = function(fragment, index) {
519519
this.insertBefore(fragment, this.createTextNode(''), child);
520520
};
521521

522-
prototype.setMorphHTML = function(morph, html) {
523-
morph.setHTML(html);
524-
};
525-
526522
prototype.parseHTML = function(html, contextualElement) {
527-
var childNodes;
528-
529-
if (interiorNamespace(contextualElement) === svgNamespace) {
530-
childNodes = buildSVGDOM(html, this);
523+
if (typeof this.document.createRawHTMLSection === 'function') {
524+
// Temporary workaround to support SimpleDOM in node
525+
return this.document.createRawHTMLSection(html);
531526
} else {
532-
var nodes = buildHTMLDOM(html, contextualElement, this);
533-
if (detectOmittedStartTag(html, contextualElement)) {
534-
var node = nodes[0];
535-
while (node && node.nodeType !== 1) {
536-
node = node.nextSibling;
537-
}
538-
childNodes = node.childNodes;
527+
var childNodes;
528+
529+
if (interiorNamespace(contextualElement) === svgNamespace) {
530+
childNodes = buildSVGDOM(html, this);
539531
} else {
540-
childNodes = nodes;
532+
var nodes = buildHTMLDOM(html, contextualElement, this);
533+
if (detectOmittedStartTag(html, contextualElement)) {
534+
var node = nodes[0];
535+
while (node && node.nodeType !== 1) {
536+
node = node.nextSibling;
537+
}
538+
childNodes = node.childNodes;
539+
} else {
540+
childNodes = nodes;
541+
}
541542
}
542-
}
543543

544-
// Copy node list to a fragment.
545-
var fragment = this.document.createDocumentFragment();
544+
// Copy node list to a fragment.
545+
var fragment = this.document.createDocumentFragment();
546546

547-
if (childNodes && childNodes.length > 0) {
548-
var currentNode = childNodes[0];
547+
if (childNodes && childNodes.length > 0) {
548+
var currentNode = childNodes[0];
549549

550-
// We prepend an <option> to <select> boxes to absorb any browser bugs
551-
// related to auto-select behavior. Skip past it.
552-
if (contextualElement.tagName === 'SELECT') {
553-
currentNode = currentNode.nextSibling;
554-
}
550+
// We prepend an <option> to <select> boxes to absorb any browser bugs
551+
// related to auto-select behavior. Skip past it.
552+
if (contextualElement.tagName === 'SELECT') {
553+
currentNode = currentNode.nextSibling;
554+
}
555555

556-
while (currentNode) {
557-
var tempNode = currentNode;
558-
currentNode = currentNode.nextSibling;
556+
while (currentNode) {
557+
var tempNode = currentNode;
558+
currentNode = currentNode.nextSibling;
559559

560-
fragment.appendChild(tempNode);
560+
fragment.appendChild(tempNode);
561+
}
561562
}
562-
}
563563

564-
return fragment;
564+
return fragment;
565+
}
565566
};
566567

567568
var parsingNode;

packages/dom-helper/tests/dom-helper-node-test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
/*globals require*/
2+
13
import DOMHelper from "../dom-helper";
24

5+
var SimpleDOM = require('simple-dom');
6+
37
var dom;
48

59
QUnit.module('DOM Helper (Node)', {
@@ -34,3 +38,27 @@ test('it instantiates with a stub document', function(){
3438
var createdElement = dom.createElement('div');
3539
equal(createdElement, element, 'dom helper calls passed stub');
3640
});
41+
42+
QUnit.module('DOM Helper (Integration: SimpleDOM)', {
43+
afterEach: function() {
44+
dom = null;
45+
}
46+
});
47+
48+
test('it instantiates with a SimpleDOM document', function(){
49+
var doc = new SimpleDOM.Document();
50+
dom = new DOMHelper(doc);
51+
ok(dom, 'dom helper can instantiate');
52+
var createdElement = dom.createElement('div');
53+
equal(createdElement.tagName, 'DIV', 'dom helper calls passed stub');
54+
});
55+
56+
test('it does not parse HTML', function(){
57+
var doc = new SimpleDOM.Document();
58+
dom = new DOMHelper(doc);
59+
ok(dom, 'dom helper can instantiate');
60+
var parsed = dom.parseHTML('<div>Hello</div>');
61+
equal(parsed.nodeType, -1, 'parseHTML creates a RawHTMLSection');
62+
equal(parsed.nodeName, '#raw-html-section', 'parseHTML creates a RawHTMLSection');
63+
equal(parsed.nodeValue, '<div>Hello</div>', 'parseHTML creates a RawHTMLSection');
64+
});

0 commit comments

Comments
 (0)