From 1c9d0ee1386b8beec742dcebaf51babdf977dc88 Mon Sep 17 00:00:00 2001 From: kts of kettek Date: Wed, 30 Aug 2017 05:16:15 -0700 Subject: [PATCH] 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. --- classes/CatProject.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/classes/CatProject.js b/classes/CatProject.js index dbd6770..e59673d 100644 --- a/classes/CatProject.js +++ b/classes/CatProject.js @@ -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 tags, and as such, will strip out any that get added. + if (this.originalHTML.search(new RegExp(']+>', 'i')) == -1) { + this.isFragment = true; + } cb(err); }); }