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

Commit

Permalink
Add temporary workaround for SimpleDOM
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
chancancode committed Oct 6, 2015
1 parent b0994e1 commit f8b221d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 34 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@
"emberjs-build": "0.2.1",
"git-repo-version": "^0.1.2",
"handlebars": "^3.0.2",
"morph-range": "0.2.5",
"morph-range": "0.3.0",
"qunit": "^0.7.2",
"rsvp": "~3.0.6"
"rsvp": "~3.0.6",
"simple-dom": "~0.2.4"
}
}
65 changes: 33 additions & 32 deletions packages/dom-helper/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -519,49 +519,50 @@ prototype.insertBoundary = function(fragment, index) {
this.insertBefore(fragment, this.createTextNode(''), child);
};

prototype.setMorphHTML = function(morph, html) {
morph.setHTML(html);
};

prototype.parseHTML = function(html, contextualElement) {
var childNodes;

if (interiorNamespace(contextualElement) === svgNamespace) {
childNodes = buildSVGDOM(html, this);
if (typeof this.document.createRawHTMLSection === 'function') {
// Temporary workaround to support SimpleDOM in node
return this.document.createRawHTMLSection(html);
} else {
var nodes = buildHTMLDOM(html, contextualElement, this);
if (detectOmittedStartTag(html, contextualElement)) {
var node = nodes[0];
while (node && node.nodeType !== 1) {
node = node.nextSibling;
}
childNodes = node.childNodes;
var childNodes;

if (interiorNamespace(contextualElement) === svgNamespace) {
childNodes = buildSVGDOM(html, this);
} else {
childNodes = nodes;
var nodes = buildHTMLDOM(html, contextualElement, this);
if (detectOmittedStartTag(html, contextualElement)) {
var node = nodes[0];
while (node && node.nodeType !== 1) {
node = node.nextSibling;
}
childNodes = node.childNodes;
} else {
childNodes = nodes;
}
}
}

// Copy node list to a fragment.
var fragment = this.document.createDocumentFragment();
// Copy node list to a fragment.
var fragment = this.document.createDocumentFragment();

if (childNodes && childNodes.length > 0) {
var currentNode = childNodes[0];
if (childNodes && childNodes.length > 0) {
var currentNode = childNodes[0];

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

while (currentNode) {
var tempNode = currentNode;
currentNode = currentNode.nextSibling;
while (currentNode) {
var tempNode = currentNode;
currentNode = currentNode.nextSibling;

fragment.appendChild(tempNode);
fragment.appendChild(tempNode);
}
}
}

return fragment;
return fragment;
}
};

var parsingNode;
Expand Down
28 changes: 28 additions & 0 deletions packages/dom-helper/tests/dom-helper-node-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
/*globals require*/

import DOMHelper from "../dom-helper";

var SimpleDOM = require('simple-dom');

var dom;

QUnit.module('DOM Helper (Node)', {
Expand Down Expand Up @@ -34,3 +38,27 @@ test('it instantiates with a stub document', function(){
var createdElement = dom.createElement('div');
equal(createdElement, element, 'dom helper calls passed stub');
});

QUnit.module('DOM Helper (Integration: SimpleDOM)', {
afterEach: function() {
dom = null;
}
});

test('it instantiates with a SimpleDOM document', function(){
var doc = new SimpleDOM.Document();
dom = new DOMHelper(doc);
ok(dom, 'dom helper can instantiate');
var createdElement = dom.createElement('div');
equal(createdElement.tagName, 'DIV', 'dom helper calls passed stub');
});

test('it does not parse HTML', function(){
var doc = new SimpleDOM.Document();
dom = new DOMHelper(doc);
ok(dom, 'dom helper can instantiate');
var parsed = dom.parseHTML('<div>Hello</div>');
equal(parsed.nodeType, -1, 'parseHTML creates a RawHTMLSection');
equal(parsed.nodeName, '#raw-html-section', 'parseHTML creates a RawHTMLSection');
equal(parsed.nodeValue, '<div>Hello</div>', 'parseHTML creates a RawHTMLSection');
});

0 comments on commit f8b221d

Please sign in to comment.