Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for raw HTML nodes. #11

Merged
merged 2 commits into from
Jun 17, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/simple-dom/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Node from './document/node';
import Element from './document/element';
import Text from './document/text';
import Comment from './document/comment';
import RawHTMLSection from './document/raw-html-section';
import DocumentFragment from './document/document-fragment';

function Document() {
Expand All @@ -28,6 +29,10 @@ Document.prototype.createComment = function(text) {
return new Comment(text);
};

Document.prototype.createRawHTMLSection = function(text) {
return new RawHTMLSection(text);
};

Document.prototype.createDocumentFragment = function() {
return new DocumentFragment();
};
Expand Down
11 changes: 11 additions & 0 deletions lib/simple-dom/document/raw-html-section.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Node from './node';

function RawHTMLSection(text) {
this.nodeConstructor(-1, "#raw-html-section", text);
}

RawHTMLSection.prototype = Object.create(Node.prototype);
RawHTMLSection.prototype.constructor = RawHTMLSection;
RawHTMLSection.prototype.nodeConstructor = Node;

export default RawHTMLSection;
7 changes: 7 additions & 0 deletions lib/simple-dom/html-serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ HTMLSerializer.prototype.text = function(text) {
return this.escapeText(text.nodeValue);
};

HTMLSerializer.prototype.rawHTMLSection = function(text) {
return text.nodeValue;
};

HTMLSerializer.prototype.comment = function(comment) {
return '<!--'+comment.nodeValue+'-->';
};
Expand All @@ -76,6 +80,9 @@ HTMLSerializer.prototype.serialize = function(node) {
case 3:
buffer += this.text(node);
break;
case -1:
buffer += this.rawHTMLSection(node);
break;
case 8:
buffer += this.comment(node);
break;
Expand Down
33 changes: 31 additions & 2 deletions test/serializer-test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { element, fragment, text } from './support';
/*globals window*/

import { element, fragment, text, html } from './support';
import Serializer from 'simple-dom/html-serializer';
import voidMap from 'simple-dom/void-map';

Expand All @@ -17,4 +19,31 @@ QUnit.test('serializes correctly', function (assert) {
)
));
assert.equal(actual, '<div id="foo"><b>Foo &amp; Bar</b></div>');
});
});

// SimpleDOM supports an extension of the DOM API that allows inserting strings of
// unparsed, raw HTML into the document. When the document is subsequently serialized,
// the raw text of the HTML nodes is inserted into the HTML.
//
// This performance optimization allows users of SimpleDOM (like Ember's FastBoot) to insert
// raw HTML snippets into the final serialized output without requiring a parsing and
// reserialization round-trip.
if (typeof window === 'undefined') {
QUnit.test('serializes raw HTML', function(assert) {
var actual = this.serializer.serialize(fragment(
element('div', { id: 'foo' },
text('<p></p>')
)
));

assert.equal(actual, '<div id="foo">&lt;p&gt;&lt;/p&gt;</div>');

actual = this.serializer.serialize(fragment(
element('div', { id: 'foo' },
html('<p></p>')
)
));

assert.equal(actual, '<div id="foo"><p></p></div>');
});
}
6 changes: 5 additions & 1 deletion test/support.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@ export function text(s) {

export function comment(s) {
return document.createComment(s);
}
}

export function html(s) {
return document.createRawHTMLSection(s);
}