Skip to content

Commit e658835

Browse files
committed
Fix the sample's replace logic and add a new replaceElWithHTML method to dom
1 parent 2a4fca9 commit e658835

File tree

7 files changed

+65
-17
lines changed

7 files changed

+65
-17
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ modal_form.insertForm = function(parsed_content, response, form){
269269
if( form ){
270270
271271
//replace and reassign
272-
form = form.outerHTML = parsed_content.html;
272+
form = dom.replaceElWithHTML(form, parsed_content.html);
273273
274274
//attach submit handler
275275
self.attachSubmitHandler(form);

dist/jpack.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@htmlguyllc/jpack",
3-
"version": "0.5.23",
3+
"version": "0.5.24",
44
"description": "Core Javascript Library of Everyday Objects, Events, and Utilities",
55
"keywords": [
66
"javascript",

src/components/navigation/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,11 +381,11 @@ export const navigation = {
381381
navigation.setTitle(parsed.title);
382382

383383
//replace content on the page
384-
dom.getElement(replace_el).outerHTML = parsed.html;
384+
const new_content = dom.replaceElWithHTML(replace_el, parsed.html);
385385

386386
//trigger nav complete event
387387
//get replace_el again because it was replaced
388-
navigation.triggerOnLoad(dom.getElement(incoming_el), incoming_el, parsed.route);
388+
navigation.triggerOnLoad(new_content, incoming_el, parsed.route);
389389
}
390390
}, 100);
391391

src/jpack.compiled.js

Lines changed: 31 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/utilities/dom/index.js

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@ export const dom = {
1515
getElement: function(el, error_on_none, error_on_multiple){
1616
el = this.getElements(el, error_on_none);
1717

18-
if( el.length > 1 ){
19-
if( error_on_multiple ) throw "Too many DOM elements found in getElement for "+JSON.stringify(el);
20-
return null;
21-
}
18+
if( el.length > 1 && error_on_multiple ) throw "Too many DOM elements found in getElement for "+JSON.stringify(el);
2219

2320
return el[0];
2421
},
@@ -87,6 +84,33 @@ export const dom = {
8784
return this;
8885
},
8986

87+
/**
88+
* Replaces a dom element with HTML
89+
*
90+
* uses .getElement() so el can be just about anything
91+
*
92+
* @param el
93+
* @param html
94+
* @returns {ChildNode}
95+
*/
96+
replaceElWithHTML: function(el, html){
97+
if( typeof html !== 'string' ) throw `${html} is not a string`;
98+
99+
el = this.getElement(el);
100+
101+
//create element from HTML
102+
let new_el = (new DOMParser()).parseFromString(html, "text/html");
103+
104+
//insert the new element before the current
105+
new_el = el.parentNode.insertBefore(new_el.documentElement.querySelector('body').childNodes[0], el);
106+
107+
//remove original element
108+
el.remove();
109+
110+
//return the new one
111+
return new_el;
112+
},
113+
90114
/**
91115
* Returns true if the provided element exists
92116
*

0 commit comments

Comments
 (0)