avalon icon indicating copy to clipboard operation
avalon copied to clipboard

examples中使用avalon.$events后存在问题的页面及说明

Open ShirlyFE opened this issue 12 years ago • 9 comments

原本有问题的页面 antixss.html(图片不显示) data.html(avalon(this)取不到) repeat:1(报错)

原本可能有问题的页面 tree.html(无限增加)

改avalon.$events.js之后有问题的页面 if0-if4(ms-if) repeat:0(ms-repeat)\5(测试作用域)\6(取不到)\8(shift多选)\12(报错)\13(上方文字不显示、上下不同步)\15(右下add按钮)\16(闪动编译错误) test3(warning:el.array编译出错)

ShirlyFE avatar Sep 29 '14 08:09 ShirlyFE

让玉皓找的问题,具体存在的bug未细看,国庆假期来之后在修

ShirlyFE avatar Sep 29 '14 08:09 ShirlyFE

<!DOCTYPE html>
<html>
    <head>
        <title>反XSS攻击</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" /> 
        <script src="avalon.$events.js"></script>
        <script>
            avalon.define("test", function(vm) {
                vm.aaa = "<img onclick=333 src='http://www.baidu.com/img/baidu_jgylogo3.gif'>" +
                        "<form onclick=333 action='javascript:alert(1)'></form>" +
                        "<p onclick='aaa' onclick2=\"ddd\" title=eee onpress=eee>onclick=eee<span onmouseup='ddd'>ddd</span></p><script>alert(1)<\/script>"
            })

        </script>
    </head>

    <body ms-controller="test">
        <div>{{aaa|sanitize|html}}</div>
    </body>
</html>

第一个问题,升级正则

  var rscripts = /<script[^>]*>([\S\s]*?)<\/script\s*>/gim
    var ron = /\s+(on[^=\s]+)(?:=("[^"]*"|'[^']*'|[^\s>]+))?/g
    var ropen = /<\w+\b(?:(["'])[^"]*?(\1)|[^>])*>/ig
    var rsanitize = {
        a: /\b(href)\=("javascript[^"]*"|'javascript[^']*')/ig,
        img: /\b(src)\=("javascript[^"]*"|'javascript[^']*')/ig,
        form: /\b(action)\=("javascript[^"]*"|'javascript[^']*')/ig
    }


        sanitize: function(str) {
            return str.replace(rscripts, "").replace(ropen, function(a, b) {
                var match = a.toLowerCase().match(/<(\w+)\s/)
                if (match) {//处理a标签的href属性,img标签的src属性,form标签的action属性
                    var reg = rsanitize[match[1]]
                    if (reg) {
                        a = a.replace(reg, function(s, name, value) {
                            var quote = value.charAt(0)
                            return  name + "=" + quote + "javascript:void(0)" + quote
                        })
                    }
                }
                return a.replace(ron, " ").replace(/\s+/g, " ")//移除onXXX事件
            })
        },

RubyLouvre avatar Sep 29 '14 14:09 RubyLouvre

data.html(avalon(this)取不到)没发现问题 if0-if4(ms-if)已修

RubyLouvre avatar Sep 29 '14 15:09 RubyLouvre

repeat:0 报错 "Uncaught TypeError: undefined is not a function "错误

将data-rollback的实现改成这样就好了

            data.rollback = function() {//只用于list为对象的情况
                bindingExecutors.repeat.call(data, "clear")
                var elem = data.element
                var parentNode = elem.parentNode
                var content = avalon.parseHTML(data.template).firstChild
                parentNode.replaceChild(content, elem)
                var target = data.element = data.type === "repeat" ? content : parentNode
                target.setAttribute(data.name, data.value)
            }

RubyLouvre avatar Sep 29 '14 15:09 RubyLouvre

getLocatedNode与removeView方法更名为locateFragment、removeFragment:

    function locateFragment(data, pos) {
        if (data.type == "repeat") {//ms-repeat,data.group为1
            var node = data.element.nextSibling
            for (var i = 0, n = pos; i < n; i++) {
                if (node) {
                    node = node.nextSibling
                } else {
                    break
                }
            }
        } else {
            var nodes = avalon.slice(data.element.parentNode.childNodes, 1)
            data.group = nodes.length / data.proxies.length
            node = nodes[data.group * pos]
        }
        return node || null
    }

    function removeFragment(node, group, pos) {
        var n = group * (pos || 1)
        var nodes = [node], i = 1
        var view = hyperspace
        while (i < n) {
            node = node.nextSibling
            if (node) {
                nodes[i++] = node
            }
        }
        for (var i = 0; node = nodes[i++]; ) {
            view.appendChild(node)
        }
        return view
    }

RubyLouvre avatar Oct 01 '14 10:10 RubyLouvre

现在只剩下 tree.html(无限增加)的问题

RubyLouvre avatar Oct 01 '14 10:10 RubyLouvre

修正已知所有BUG

RubyLouvre avatar Oct 01 '14 13:10 RubyLouvre

<!DOCTYPE HTML>
<html>
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>ms-each</title>
        <script src="avalon.$events.js" type="text/javascript"></script>

        <script>
            avalon.ready(function() {
                function expect(a) {
                    return {
                        to: {
                            be: function(b) {
                                if (a === b) {
                                    console.log(a === b)
                                } else {
                                    console.log(a + " !== " + b)
                                }
                            }
                        }
                    }
                }
                var model = avalon.define("test", function(vm) {
                    vm.array = [1, 2, 3, 4]
                    vm.object = {
                        a: 1,
                        b: 2,
                        c: 3
                    }
                    vm.sort = function() {
                        return ["b", "a", "c"]
                    }
                    vm.callback = function(a) {
                        expect(a).to.be("add")
                        expect(this.tagName.toLowerCase()).to.be("ul")
                        //   end()
                    }
                    vm.callback2 = function(a) {
                        expect(a).to.be("add")
                        expect(this.tagName.toLowerCase()).to.be("ol")
                        // end()
                    }
                    vm.callback3 = function(a) {
                        expect(a).to.be("append")
                        expect(this.tagName.toLowerCase()).to.be("tr")
                        var cells = this.cells
                        expect(cells[0].innerHTML).to.be("b:2")
                        expect(cells[1].innerHTML).to.be("a:1")
                        expect(cells[2].innerHTML).to.be("c:3")
                        //   end()
                    }
                })
                var body = document.body
                var div = document.createElement("div")
                div.innerHTML = "<div ms-controller=\"test\"><ul ms-each=\"array\" data-each-rendered=\"callback\"><li>{{el}}</li></ul><ol><li ms-repeat=\"array\" data-repeat-rendered=\"callback2\">{{el}}</li></ol>\n            <table border=\"1\"><tbody><tr ms-with=\"object\" data-with-sorted=\"sort\" data-with-rendered=\"callback3\"><td>{{$key}}:{{$val}}</td></tr></tbody></table></div>"
                body.appendChild(div)
                avalon.scan(div, model)
                var endIndex = 0
                function end() {
                    endIndex++
                    if (endIndex == 3) {
                        body.removeChild(div)
                        // done()
                    }
                }
            })
        </script>
    </head>
    <body>

    </body>
</html>

RubyLouvre avatar Oct 03 '14 08:10 RubyLouvre

对监控函数的依赖实现

早期是这样做的

        functionProperties.forEach(function(fn) {
            var name = fn.avalonName
            if (name === "hasOwnProperty")
                return
            var data = {
                evaluator: function() {
                    notifySubscribers($vmodel.$events[fn.avalonName])
                },
                type: "function::" + name,
                element: head,
                handler: noop,
                args: []
            }
            var vars = getVariables(fn.toString().replace(rvariable, "")).concat()
            if (vars.length) {//计算依赖
                vars = vars.filter(function(name) {
                    var test = name.split(".")[0]
                    if ($vmodel.hasOwnProperty(test)) {
                        return  (typeof $vmodel[test] != "object") && !isFunction($vmodel[test])
                    }
                })
                addAssign(vars, $vmodel, name, data)
            }
        })
   var rvariable = /[$\w][$\w]*\./g
    function isObservable(name, value, $skipArray, functionProperties) {
        if (value && value.nodeType) {
            return false
        }
        if ($skipArray.indexOf(name) !== -1) {
            return false
        }
        if ($$skipArray.indexOf(name) !== -1) {
            return false
        }
        var $special = $skipArray.$special
        if (name && name.charAt(0) === "$" && !$special[name]) {
            return false
        }
        if (isFunction(value)) {
            value.avalonName = name
            functionProperties.push(value)
            return false
        }
        return true
    }

RubyLouvre avatar Oct 05 '14 15:10 RubyLouvre