Skip to content

Commit 1bdc2c9

Browse files
committed
v2.0
1 parent f3d3f70 commit 1bdc2c9

5 files changed

Lines changed: 499 additions & 1180 deletions

File tree

browser.js

Lines changed: 70 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
innerHTML: { value: innerHTMLBind, writable: true, configurable: false, enumerable: false },
2828
value: { value: valueBind, writable: true, configurable: false, enumerable: false },
29+
getvalue: { value: getValueBind, writable: true, configurable: false, enumerable: false },
30+
setvalue: { value: setValueBind, writable: true, configurable: false, enumerable: false },
2931
check: { value: checkBind, writable: true, configurable: false, enumerable: false },
3032
hidden: { value: hiddenBind, writable: true, configurable: false, enumerable: false },
3133
enabled: { value: enabledBind, writable: true, configurable: false, enumerable: false },
@@ -193,24 +195,42 @@
193195
var templateElement = null;
194196

195197
try { templateElement = element.querySelector(selectors) || document.querySelector(selectors); }
196-
catch {
198+
catch { templateElement = null; }
199+
200+
201+
if (!templateElement) { return fetchContent(); }
202+
203+
bindTemplate();
204+
205+
return;
206+
207+
function fetchContent() {
197208

198209
fetch(selectors).then(function (res) {
199210

200-
res.text().then(function (strHTML) {
211+
res.text().then(function (textContent) {
201212

202213
templateElement = document.createElement("template");
203-
templateElement.innerHTML = strHTML;
214+
selectors.endsWith('.html')
215+
? templateElement.innerHTML = textContent
216+
: templateElement.innerHTML = '<div>' + encodeHTML(textContent) + '</div>';
204217
bindTemplate();
205218
});
206219
});
207-
}
208-
209-
if (!templateElement) { return; }
210-
211-
bindTemplate();
212220

213-
return;
221+
function encodeHTML(str) {
222+
223+
return str.replace(/&/g, '&amp;')
224+
.replace(/</g, '&lt;')
225+
.replace(/>/g, '&gt;')
226+
.replace(/"/g, '&quot;')
227+
.replace(/'/g, '&#39;')
228+
.replace(/ /g, "&nbsp;")
229+
.replace(/\r\n/g, '<br>') // Windows-style newlines
230+
.replace(/\r/g, '<br>') // Old Mac-style newlines
231+
.replace(/\n/g, '<br>'); // Unix-style newlines
232+
}
233+
}
214234

215235
function bindTemplate() {
216236

@@ -796,6 +816,47 @@
796816
// I am alive!
797817
return true;
798818
}
819+
function getValueBind(event) {
820+
821+
if (event.eventName === "unbind") {
822+
823+
this.onchange = null;
824+
this.onkeydown = null;
825+
826+
this.value = '';
827+
828+
// I am dead!
829+
return false;
830+
}
831+
832+
this.value = event.newValue;
833+
834+
// I am alive!
835+
return true;
836+
}
837+
function setValueBind(event) {
838+
839+
if (event.eventName === "unbind") {
840+
841+
this.onchange = null;
842+
this.onkeydown = null;
843+
844+
this.value = '';
845+
846+
// I am dead!
847+
return false;
848+
}
849+
850+
// Init
851+
if (event.eventName === "bind") {
852+
853+
this.onchange = function () { this.contextValue(this.value); };
854+
this.onkeydown = function (ev) { if (ev.keyCode === 27) { this.value = his.contextValue(); } };
855+
}
856+
857+
// I am alive!
858+
return true;
859+
}
799860
function checkBind(event) {
800861

801862
if (event.eventName === "unbind") {

0 commit comments

Comments
 (0)