Add ability to load and save HTML fragments

These HTML fragments are presumed to have no body or head tags, just
basic HTML markup. It really should check for the presence of:

  * html
  * head
  * body

And adjust saving accordingly.
master
kts of kettek 2017-08-30 05:16:15 -07:00
parent 3ed7d531ef
commit 1c9d0ee138
1 changed files with 16 additions and 1 deletions

View File

@ -39,7 +39,18 @@ CatProject.prototype.setup = function() {
}
CatProject.prototype.save = function(path, cb) {
//fs.writeFile(path, (new XMLSerializer().serializeToString(this.iframe.contentDocument))+this.iframe.contentDocument.outerHTML, 'utf-8', (err) => {
fs.writeFile(path, this.view.getContents(), 'utf-8', (err) => {
let iframe = this.view.querySelector('iframe');
// NOTE: this is pretty hackish, but we basically remove the xmlns namespace for HTML fragments and parse each node individually.
// FIXME: This presumes that the HTML fragment has _no_ real head or body.
var serializedData = '';
if (this.isFragment) {
for (var i = 0; i < iframe.contentDocument.body.children.length; i++) {
serializedData += new XMLSerializer().serializeToString(iframe.contentDocument.body.children[i]).replace(new RegExp(' xmlns="[^"]+"', 'i'), '');
}
} else {
serializedData = this.view.getContents();
}
fs.writeFile(path, serializedData, 'utf-8', (err) => {
if (err) throw err;
this.emit('saved');
this.path = path;
@ -53,6 +64,10 @@ CatProject.prototype.load = function(path, cb) {
if (err) throw err;
this.originalHTML = data;
this.view.setContents(data);
// FIXME: we just parse this stupid-style to check if it is an html fragment (which are saved differently). Fragments are assumed to simply not have <html> tags, and as such, will strip out any that get added.
if (this.originalHTML.search(new RegExp('<html[^>]+>', 'i')) == -1) {
this.isFragment = true;
}
cb(err);
});
}