From ccfd33529c9fea18fb293b75541d87444bcc987f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=87=E5=A4=A9?= Date: Mon, 6 Jul 2020 14:38:22 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dsvg=E7=9A=84=E6=94=AF?= =?UTF-8?q?=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- src/13-scan.js | 6 +++-- src/16-:attr.js | 67 +++++++++++++++++++++++-------------------------- 3 files changed, 37 insertions(+), 38 deletions(-) diff --git a/package.json b/package.json index 3143b83..c27580b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "anot", - "version": "2.2.1", + "version": "2.2.2", "description": "Anot - 迷你mvvm框架", "main": "dist/anot.js", "files": ["dist"], diff --git a/src/13-scan.js b/src/13-scan.js index 61aee47..f02d81b 100644 --- a/src/13-scan.js +++ b/src/13-scan.js @@ -50,7 +50,7 @@ function executeBindings(bindings, vmodels) { } var roneTime = /^\s*::/ -var rmsAttr = /:(\w+)-?(.*)|@(.*)/ +var rmsAttr = /^:(\w+)-?(.*)|@(.*)/ var events = oneObject( 'animationend,blur,change,input,click,dblclick,focus,keydown,keypress,keyup,mousedown,mouseenter,mouseleave,mousemove,mouseout,mouseover,mouseup,scan,scroll,submit' @@ -372,6 +372,8 @@ function scanText(textNode, vmodels, index) { anotFragment.appendChild(node) } textNode.parentNode.replaceChild(anotFragment, textNode) - if (bindings.length) executeBindings(bindings, vmodels) + if (bindings.length) { + executeBindings(bindings, vmodels) + } } } diff --git a/src/16-:attr.js b/src/16-:attr.js index 33a1a1a..026bf36 100644 --- a/src/16-:attr.js +++ b/src/16-:attr.js @@ -48,6 +48,7 @@ var attrDir = Anot.directive('attr', { update: function(val) { var elem = this.element var obj = {} + var isSVG = rsvg.test(elem) val = toJson(val) @@ -83,44 +84,40 @@ var attrDir = Anot.directive('attr', { elem.style.cssText = obj[i] continue } - if (i === 'href' || i === 'src') { + // 修正这些值的显示 + if (obj[i] === false || obj[i] === null || obj[i] === undefined) { + obj[i] = '' + } + + if ( + typeof elem[i] === 'boolean' || + typeof elem[boolMap[i]] === 'boolean' + ) { + var k = i + if (boolMap[i] && k !== boolMap[i]) { + k = boolMap[i] + } + //布尔属性必须使用el.xxx = true|false方式设值 + obj[i] = !!obj[i] + elem[k] = obj[i] + + if (!obj[i]) { + elem.removeAttribute(k) + continue + } + } + + //SVG只能使用setAttribute(xxx, yyy), HTML的固有属性必须elem.xxx = yyy + var isInnate = isSVG ? false : i in elem.cloneNode(false) + if (isInnate) { elem[i] = obj[i] } else { - // 修正这些值的显示 - if (obj[i] === false || obj[i] === null || obj[i] === undefined) { - obj[i] = '' - } - - if ( - typeof elem[i] === 'boolean' || - typeof elem[boolMap[i]] === 'boolean' - ) { - var k = i - if (boolMap[i] && k !== boolMap[i]) { - k = boolMap[i] - } - //布尔属性必须使用el.xxx = true|false方式设值 - obj[i] = !!obj[i] - elem[k] = obj[i] - - if (!obj[i]) { - elem.removeAttribute(k) - continue - } - } - - //SVG只能使用setAttribute(xxx, yyy), HTML的固有属性必须elem.xxx = yyy - var isInnate = rsvg.test(elem) ? false : i in elem.cloneNode(false) - if (isInnate) { - elem[i] = obj[i] - } else { - if (typeof obj[i] === 'object') { - obj[i] = Date.isDate(obj[i]) - ? obj[i].toISOString() - : JSON.stringify(obj[i]) - } - elem.setAttribute(i, obj[i]) + if (typeof obj[i] === 'object') { + obj[i] = Date.isDate(obj[i]) + ? obj[i].toISOString() + : JSON.stringify(obj[i]) } + elem.setAttribute(i, obj[i]) } } }