From eb3d4f82fc9f60a4f5993b85bd1580efd81d1b0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=87=E5=A4=A9?= Date: Thu, 13 Aug 2020 18:43:04 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=BB=E9=99=A4=E9=9D=9E=E6=B5=8F=E8=A7=88?= =?UTF-8?q?=E5=99=A8=E7=8E=AF=E5=A2=83=E7=9A=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rollup.config.dev.js | 20 +++---- src/anot.js | 2 +- src/anot.touch.js | 2 +- .../duplex/updateDataEvents.modern.js | 11 +--- src/directives/important.js | 1 - src/directives/on.js | 2 +- src/directives/text.js | 10 ++-- src/dom/attr/modern.js | 2 +- src/dom/css/share.js | 2 +- src/dom/event/modern.js | 2 +- src/dom/event/share.js | 25 +++----- src/dom/html/index.js | 2 +- src/dom/ready/modern.js | 6 +- src/dom/shim/modern.js | 60 +++++++++---------- src/effect/detect.js | 1 - src/effect/index.js | 2 +- src/filters/date.js | 42 ++----------- src/parser/index.js | 14 +---- src/renders/Directive.js | 14 ++--- src/renders/domRender.js | 34 ++++------- src/seed/browser.js | 32 ---------- src/seed/core.js | 19 +----- src/seed/{lang.modern.js => lang.js} | 0 src/vdom/VComment.js | 2 - src/vdom/VElement.modern.js | 8 +-- src/vdom/VText.js | 2 +- src/vmodel/ProxyArray.js | 8 +-- src/vmodel/modern.js | 2 +- src/vmodel/proxy.js | 2 +- src/vmodel/share.js | 2 +- 30 files changed, 103 insertions(+), 228 deletions(-) delete mode 100644 src/seed/browser.js rename src/seed/{lang.modern.js => lang.js} (100%) diff --git a/rollup.config.dev.js b/rollup.config.dev.js index 94fe208..46415cc 100644 --- a/rollup.config.dev.js +++ b/rollup.config.dev.js @@ -16,15 +16,15 @@ export default [ name: '_Anot' }, plugins: [esm()] - }, - { - input: 'src/anot.touch.js', - output: { - file: 'dist/anot.touch.js', - format: 'iife', - sourcemap: true, - name: '_Anot' - }, - plugins: [esm()] } + // { + // input: 'src/anot.touch.js', + // output: { + // file: 'dist/anot.touch.js', + // format: 'iife', + // sourcemap: true, + // name: '_Anot' + // }, + // plugins: [esm()] + // } ] diff --git a/src/anot.js b/src/anot.js index 213dc8c..2a6d829 100644 --- a/src/anot.js +++ b/src/anot.js @@ -1,5 +1,5 @@ import { Anot } from './seed/core' -import './seed/lang.modern' +import './seed/lang' import './filters/index' import './dom/modern' diff --git a/src/anot.touch.js b/src/anot.touch.js index 9b896e3..ef11796 100644 --- a/src/anot.touch.js +++ b/src/anot.touch.js @@ -1,5 +1,5 @@ import { Anot } from './seed/core' -import './seed/lang.modern' +import './seed/lang' import './filters/index' import './dom/modern' diff --git a/src/directives/duplex/updateDataEvents.modern.js b/src/directives/duplex/updateDataEvents.modern.js index 591b556..0d77e6d 100644 --- a/src/directives/duplex/updateDataEvents.modern.js +++ b/src/directives/duplex/updateDataEvents.modern.js @@ -5,13 +5,7 @@ * 2. value属性重写 * 3. 定时器轮询 */ -import { - Anot, - getShortID as markID, - window, - document, - msie -} from '../../seed/core' +import { Anot, getShortID as markID } from '../../seed/core' import { updateModel } from './updateDataHandle' export function updateDataEvents(dom, data) { @@ -48,9 +42,6 @@ export function updateDataEvents(dom, data) { //https://github.com/RubyLouvre/Anot/issues/1368#issuecomment-220503284 events.compositionstart = openComposition events.compositionend = closeComposition - if (Anot.msie) { - events.keyup = updateModelKeyDown - } } break } diff --git a/src/directives/important.js b/src/directives/important.js index c1a55b6..b51adc6 100644 --- a/src/directives/important.js +++ b/src/directives/important.js @@ -8,7 +8,6 @@ var impDir = Anot.directive('important', { throw 'error! no vmodel called ' + name }, update: function(node, attrName, $id) { - if (!Anot.inBrowser) return var dom = Anot.vdom(node, 'toDOM') if (dom.nodeType === 1) { dom.removeAttribute(attrName) diff --git a/src/directives/on.js b/src/directives/on.js index 86cfc46..5140ddf 100644 --- a/src/directives/on.js +++ b/src/directives/on.js @@ -1,4 +1,4 @@ -import { Anot, inBrowser } from '../seed/core' +import { Anot } from '../seed/core' import { addScope, makeHandle } from '../parser/index' diff --git a/src/directives/text.js b/src/directives/text.js index 4ec13a0..9001f32 100644 --- a/src/directives/text.js +++ b/src/directives/text.js @@ -1,4 +1,4 @@ -import { Anot, inBrowser } from '../seed/core' +import { Anot } from '../seed/core' Anot.directive('text', { delay: true, @@ -9,10 +9,10 @@ Anot.directive('text', { } var child = { nodeName: '#text', nodeValue: this.getValue() } node.children.splice(0, node.children.length, child) - if (inBrowser) { - Anot.clearHTML(node.dom) - node.dom.appendChild(Anot.vdom(child, 'toDOM')) - } + + Anot.clearHTML(node.dom) + node.dom.appendChild(Anot.vdom(child, 'toDOM')) + this.node = child var type = 'expr' this.type = this.name = type diff --git a/src/dom/attr/modern.js b/src/dom/attr/modern.js index 46b6db7..a5874bf 100644 --- a/src/dom/attr/modern.js +++ b/src/dom/attr/modern.js @@ -1,4 +1,4 @@ -import { Anot, window, document } from '../../seed/core' +import { Anot } from '../../seed/core' import { propMap } from './propMap' var rsvg = /^\[object SVG\w*Element\]$/ diff --git a/src/dom/css/share.js b/src/dom/css/share.js index 91acaf1..db953f7 100644 --- a/src/dom/css/share.js +++ b/src/dom/css/share.js @@ -1,4 +1,4 @@ -import { Anot, oneObject, cssHooks, window } from '../../seed/core' +import { Anot, oneObject, cssHooks } from '../../seed/core' var cssMap = oneObject('float', 'cssFloat') export { cssMap, cssHooks } diff --git a/src/dom/event/modern.js b/src/dom/event/modern.js index 2424305..a7cc840 100644 --- a/src/dom/event/modern.js +++ b/src/dom/event/modern.js @@ -1,4 +1,4 @@ -import { Anot, document } from '../../seed/core' +import { Anot } from '../../seed/core' import { avEvent } from './share' export { avEvent } diff --git a/src/dom/event/share.js b/src/dom/event/share.js index 734c62c..8b9b9aa 100644 --- a/src/dom/event/share.js +++ b/src/dom/event/share.js @@ -1,16 +1,7 @@ -import { - Anot, - _slice, - eventHooks, - modern, - window, - document, - root, - getShortID -} from '../../seed/core' +import { Anot, _slice, eventHooks, root, getShortID } from '../../seed/core' import { canBubbleUp } from './canBubbleUp' /* istanbul ignore if */ -var hackSafari = Anot.modern && document.ontouchstart +var hackSafari = document.ontouchstart //添加fn.bind, fn.unbind, bind, unbind Anot.fn.bind = function(type, fn, phase) { @@ -54,7 +45,7 @@ Anot.bind = function(elem, type, fn) { /* istanbul ignore if */ if (value.indexOf(type + ':') === -1) { //同一种事件只绑定一次 - if (canBubbleUp[type] || (Anot.modern && focusBlur[type])) { + if (canBubbleUp[type] || focusBlur[type]) { delegateEvent(type) } else { Anot._nativeBind(elem, type, dispatch) @@ -206,16 +197,14 @@ var eventProto = { preventDefault: function() { var e = this.originalEvent || {} e.returnValue = this.returnValue = false - if (modern && e.preventDefault) { - e.preventDefault() - } + + e.preventDefault() }, stopPropagation: function() { var e = this.originalEvent || {} e.cancelBubble = this.cancelBubble = true - if (modern && e.stopPropagation) { - e.stopPropagation() - } + + e.stopPropagation() }, stopImmediatePropagation: function() { this.stopPropagation() diff --git a/src/dom/html/index.js b/src/dom/html/index.js index a9bc632..f51a627 100644 --- a/src/dom/html/index.js +++ b/src/dom/html/index.js @@ -1,4 +1,4 @@ -import { Anot, Cache, document, createFragment } from '../../seed/core' +import { Anot, Cache, createFragment } from '../../seed/core' import { fromString } from '../../vtree/fromString' export { Anot } diff --git a/src/dom/ready/modern.js b/src/dom/ready/modern.js index 009c7d8..d19f292 100644 --- a/src/dom/ready/modern.js +++ b/src/dom/ready/modern.js @@ -1,4 +1,4 @@ -import { Anot, window, document, root, inBrowser } from '../../seed/core' +import { Anot, root } from '../../seed/core' var readyList = [] @@ -33,6 +33,4 @@ function bootstrap() { Anot.bind(window, 'load', fireReady) } -if (inBrowser) { - bootstrap() -} +bootstrap() diff --git a/src/dom/shim/modern.js b/src/dom/shim/modern.js index 1102270..e26ed6c 100644 --- a/src/dom/shim/modern.js +++ b/src/dom/shim/modern.js @@ -1,5 +1,5 @@ //safari5+是把contains方法放在Element.prototype上而不是Node.prototype -import { Anot, document, window, root } from '../../seed/core' +import { Anot, root } from '../../seed/core' import { fixContains } from './fixContains' export { Anot } @@ -9,35 +9,33 @@ Anot.cloneNode = function(a) { return a.cloneNode(true) } -if (Anot.modern) { - if (!document.contains) { - Node.prototype.contains = function(child) { - //IE6-8没有Node对象 - return fixContains(this, child) - } +if (!document.contains) { + Node.prototype.contains = function(child) { + //IE6-8没有Node对象 + return fixContains(this, child) } - function fixFF(prop, cb) { - //firefox12 http://caniuse.com/#search=outerHTML - if (!(prop in root)) { - HTMLElement.prototype.__defineGetter__(prop, cb) - } - } - fixFF('outerHTML', function() { - //https://developer.mozilla.org/en-US/docs/Web/API/ParentNode/children - var div = document.createElement('div') - div.appendChild(this) - return div.innerHTML - }) - fixFF('children', function() { - var children = [] - for (var i = 0, el; (el = this.childNodes[i++]); ) { - if (el.nodeType === 1) { - children.push(el) - } - } - return children - }) - fixFF('innerText', function() { - return this.textContent - }) } +function fixFF(prop, cb) { + //firefox12 http://caniuse.com/#search=outerHTML + if (!(prop in root)) { + HTMLElement.prototype.__defineGetter__(prop, cb) + } +} +fixFF('outerHTML', function() { + //https://developer.mozilla.org/en-US/docs/Web/API/ParentNode/children + var div = document.createElement('div') + div.appendChild(this) + return div.innerHTML +}) +fixFF('children', function() { + var children = [] + for (var i = 0, el; (el = this.childNodes[i++]); ) { + if (el.nodeType === 1) { + children.push(el) + } + } + return children +}) +fixFF('innerText', function() { + return this.textContent +}) diff --git a/src/effect/detect.js b/src/effect/detect.js index 9f2f320..8a773f3 100644 --- a/src/effect/detect.js +++ b/src/effect/detect.js @@ -4,7 +4,6 @@ * ------------------------------------------------------------ */ -import { window } from '../seed/core' let checker = { TransitionEvent: 'transitionend', WebKitTransitionEvent: 'webkitTransitionEnd', diff --git a/src/effect/index.js b/src/effect/index.js index 5225291..c336dca 100644 --- a/src/effect/index.js +++ b/src/effect/index.js @@ -1,4 +1,4 @@ -import { Anot, window, Cache } from '../seed/core' +import { Anot, Cache } from '../seed/core' import { cssDiff } from '../directives/css' import { css3, diff --git a/src/filters/date.js b/src/filters/date.js index 8163196..e62fa9a 100644 --- a/src/filters/date.js +++ b/src/filters/date.js @@ -1,37 +1,3 @@ -/* - 'yyyy': 4 digit representation of year (e.g. AD 1 => 0001, AD 2010 => 2010) - 'yy': 2 digit representation of year, padded (00-99). (e.g. AD 2001 => 01, AD 2010 => 10) - 'y': 1 digit representation of year, e.g. (AD 1 => 1, AD 199 => 199) - 'MMMM': Month in year (January-December) - 'MMM': Month in year (Jan-Dec) - 'MM': Month in year, padded (01-12) - 'M': Month in year (1-12) - 'dd': Day in month, padded (01-31) - 'd': Day in month (1-31) - 'EEEE': Day in Week,(Sunday-Saturday) - 'EEE': Day in Week, (Sun-Sat) - 'HH': Hour in day, padded (00-23) - 'H': Hour in day (0-23) - 'hh': Hour in am/pm, padded (01-12) - 'h': Hour in am/pm, (1-12) - 'mm': Minute in hour, padded (00-59) - 'm': Minute in hour (0-59) - 'ss': Second in minute, padded (00-59) - 's': Second in minute (0-59) - 'a': am/pm marker - 'Z': 4 digit (+sign) representation of the timezone offset (-1200-+1200) - format string can also be one of the following predefined localizable formats: - - 'medium': equivalent to 'MMM d, y h:mm:ss a' for en_US locale (e.g. Sep 3, 2010 12:05:08 pm) - 'short': equivalent to 'M/d/yy h:mm a' for en_US locale (e.g. 9/3/10 12:05 pm) - 'fullDate': equivalent to 'EEEE, MMMM d,y' for en_US locale (e.g. Friday, September 3, 2010) - 'longDate': equivalent to 'MMMM d, y' for en_US locale (e.g. September 3, 2010 - 'mediumDate': equivalent to 'MMM d, y' for en_US locale (e.g. Sep 3, 2010) - 'shortDate': equivalent to 'M/d/yy' for en_US locale (e.g. 9/3/10) - 'mediumTime': equivalent to 'h:mm:ss a' for en_US locale (e.g. 12:05:08 pm) - 'shortTime': equivalent to 'h:mm a' for en_US locale (e.g. 12:05 pm) - */ - function toInt(str) { return parseInt(str, 10) || 0 } @@ -44,8 +10,12 @@ function padNumber(num, digits, trim) { num = -num } num = '' + num - while (num.length < digits) num = '0' + num - if (trim) num = num.substr(num.length - digits) + while (num.length < digits) { + num = '0' + num + } + if (trim) { + num = num.substr(num.length - digits) + } return neg + num } diff --git a/src/parser/index.js b/src/parser/index.js index 779c099..c58e9ac 100644 --- a/src/parser/index.js +++ b/src/parser/index.js @@ -1,4 +1,4 @@ -import { Anot, msie, Cache } from '../seed/core' +import { Anot, Cache } from '../seed/core' import { clearString, stringPool, fill, rfill, dig } from '../vtree/clearString' var keyMap = Anot.oneObject( @@ -105,18 +105,6 @@ export function makeHandle(body) { if (rhandleName.test(body)) { body = body + '($event)' } - /* istanbul ignore if */ - if (msie < 9) { - body = body.replace(rfixIE678, function(a, b, c) { - return ( - '__vmodel__.' + - b + - '.call(__vmodel__' + - (/\S/.test(c) ? ',' + c : '') + - ')' - ) - }) - } return body } export function createGetter(expr, type) { diff --git a/src/renders/Directive.js b/src/renders/Directive.js index 75654d0..c123ad9 100644 --- a/src/renders/Directive.js +++ b/src/renders/Directive.js @@ -1,4 +1,4 @@ -import { Anot, inBrowser } from '../seed/core' +import { Anot } from '../seed/core' import { Action, protectedMenbers } from '../vmodel/Action' @@ -11,13 +11,13 @@ import { Action, protectedMenbers } from '../vmodel/Action' export function Directive(vm, binding, vdom, render) { var type = binding.type var decorator = Anot.directives[type] - if (inBrowser) { - var dom = Anot.vdom(vdom, 'toDOM') - if (dom.nodeType === 1) { - dom.removeAttribute(binding.attrName) - } - vdom.dom = dom + + var dom = Anot.vdom(vdom, 'toDOM') + if (dom.nodeType === 1) { + dom.removeAttribute(binding.attrName) } + vdom.dom = dom + var callback = decorator.update ? function(value) { if (!render.mount && /css|visible|duplex/.test(type)) { diff --git a/src/renders/domRender.js b/src/renders/domRender.js index dddff76..0c1e0d1 100644 --- a/src/renders/domRender.js +++ b/src/renders/domRender.js @@ -1,10 +1,4 @@ -import { - Anot, - config, - inBrowser, - delayCompileNodes, - directives -} from '../seed/core' +import { Anot, config, delayCompileNodes, directives } from '../seed/core' import { fromDOM } from '../vtree/fromDOM' import { fromString } from '../vtree/fromString' @@ -173,11 +167,11 @@ Render.prototype = { //推算出指令类型 var type = dirs['ms-important'] === $id ? 'important' : 'controller' //推算出用户定义时属性名,是使用ms-属性还是:属性 - var attrName = 'ms-' + type in attrs ? 'ms-' + type : ':' + type + var _tmp = 'ms-' + type + var attrName = _tmp in attrs ? _tmp : ':' + type + + delete attrs[attrName] - if (inBrowser) { - delete attrs[attrName] - } var dir = directives[type] scope = dir.getScope.call(this, $id, scope) if (!scope) { @@ -237,13 +231,11 @@ Render.prototype = { complete() { this.yieldDirectives() this.beforeReady() - if (inBrowser) { - var root = this.root - if (inBrowser) { - var rootDom = Anot.vdom(root, 'toDOM') - groupTree(rootDom, root.children) - } - } + + var root = this.root + + var rootDom = Anot.vdom(root, 'toDOM') + groupTree(rootDom, root.children) this.mount = true var fn @@ -271,9 +263,9 @@ Render.prototype = { } for (var i = 0, binding; (binding = bindings[i++]); ) { var dir = directives[binding.type] - if (!inBrowser && /on|duplex|active|hover/.test(binding.type)) { - continue - } + // if (!inBrowser && /on|duplex|active|hover/.test(binding.type)) { + // continue + // } if (dir.beforeInit) { dir.beforeInit.call(binding) } diff --git a/src/seed/browser.js b/src/seed/browser.js deleted file mode 100644 index 1de9f56..0000000 --- a/src/seed/browser.js +++ /dev/null @@ -1,32 +0,0 @@ -export let win = - typeof window === 'object' ? window : typeof global === 'object' ? global : {} - -export let inBrowser = !!win.location && win.navigator -/* istanbul ignore if */ - -export let document = inBrowser - ? win.document - : { - createElement: Object, - createElementNS: Object, - documentElement: 'xx', - contains: Boolean - } -export var root = inBrowser - ? document.documentElement - : { - outerHTML: 'x' - } - -let versions = { - objectobject: 7, //IE7-8 - objectundefined: 6, //IE6 - undefinedfunction: NaN, // other modern browsers - undefinedobject: NaN //Mobile Safari 8.0.0 (iOS 8.4.0) - //objectfunction chrome 47 -} -/* istanbul ignore next */ -export var msie = - document.documentMode || versions[typeof document.all + typeof XMLHttpRequest] - -export var modern = /NaN|undefined/.test(msie) || msie > 8 diff --git a/src/seed/core.js b/src/seed/core.js index 32a0000..ebfa712 100644 --- a/src/seed/core.js +++ b/src/seed/core.js @@ -1,8 +1,8 @@ -import { win, document, msie, inBrowser, root, modern } from './browser' import { Cache } from './cache' import { directive, directives, delayCompileNodes } from './directive' -export var window = win +const root = document.documentElement + export function Anot(el) { return new Anot.init(el) } @@ -53,17 +53,7 @@ export function log() { Function.apply.call(console.log, console, arguments) } } -export { - Cache, - directive, - directives, - delayCompileNodes, - document, - root, - msie, - modern, - inBrowser -} +export { Cache, directive, directives, delayCompileNodes, root } export function warn() { if (hasConsole && Anot.config.debug) { var method = console.warn || console.log @@ -274,12 +264,9 @@ shadowCopy(Anot, { error, config, - modern, - msie, root, document, window, - inBrowser, isObject, range, diff --git a/src/seed/lang.modern.js b/src/seed/lang.js similarity index 100% rename from src/seed/lang.modern.js rename to src/seed/lang.js diff --git a/src/vdom/VComment.js b/src/vdom/VComment.js index 988a013..f9a796d 100644 --- a/src/vdom/VComment.js +++ b/src/vdom/VComment.js @@ -1,5 +1,3 @@ -import { document } from '../seed/core' - export function VComment(text) { this.nodeName = '#comment' this.nodeValue = text diff --git a/src/vdom/VElement.modern.js b/src/vdom/VElement.modern.js index 4d7822b..0e6841d 100644 --- a/src/vdom/VElement.modern.js +++ b/src/vdom/VElement.modern.js @@ -1,4 +1,4 @@ -import { Anot, document } from '../seed/core' +import { Anot } from '../seed/core' export function VElement(type, props, children, isVoidTag) { this.nodeName = type @@ -12,7 +12,7 @@ VElement.prototype = { if (this.dom) return this.dom var dom, tagName = this.nodeName - if (Anot.modern && svgTags[tagName]) { + if (svgTags[tagName]) { dom = createSVG(tagName) } else { dom = document.createElement(tagName) @@ -89,6 +89,4 @@ var svgTags = Anot.oneObject( 'path,polygon,polyline,rect,symbol,text,use,g,svg' ) -if (Anot.inBrowser) { - var supportTemplate = 'content' in document.createElement('template') -} +var supportTemplate = 'content' in document.createElement('template') diff --git a/src/vdom/VText.js b/src/vdom/VText.js index 19ab661..cdfed6f 100644 --- a/src/vdom/VText.js +++ b/src/vdom/VText.js @@ -1,4 +1,4 @@ -import { Anot, document } from '../seed/core' +import { Anot } from '../seed/core' export function VText(text) { this.nodeName = '#text' diff --git a/src/vmodel/ProxyArray.js b/src/vmodel/ProxyArray.js index 2f1daff..03a4109 100644 --- a/src/vmodel/ProxyArray.js +++ b/src/vmodel/ProxyArray.js @@ -1,4 +1,4 @@ -import { Anot, ap, platform, modern, isObject } from '../seed/core' +import { Anot, ap, platform, isObject } from '../seed/core' import { Mutation } from './Mutation' var _splice = ap.splice @@ -102,9 +102,9 @@ __method__.forEach(function(method) { export function listFactory(array, stop, dd) { if (!stop) { hijackMethods(array) - if (modern) { - Object.defineProperty(array, '$model', platform.modelAccessor) - } + + Object.defineProperty(array, '$model', platform.modelAccessor) + platform.hideProperty(array, '$hashcode', Anot.makeHashCode('$')) platform.hideProperty(array, '$events', { __dep__: dd || new Mutation() }) } diff --git a/src/vmodel/modern.js b/src/vmodel/modern.js index 1cc1544..3cade54 100644 --- a/src/vmodel/modern.js +++ b/src/vmodel/modern.js @@ -1,4 +1,4 @@ -import { Anot, platform, modern } from '../seed/core' +import { Anot, platform } from '../seed/core' import { $$skipArray } from './reserved' import { Action } from './Action' import './share' diff --git a/src/vmodel/proxy.js b/src/vmodel/proxy.js index 3091c35..21cde20 100644 --- a/src/vmodel/proxy.js +++ b/src/vmodel/proxy.js @@ -1,4 +1,4 @@ -import { Anot, platform, isObject, modern } from '../seed/core' +import { Anot, platform, isObject } from '../seed/core' import { $$skipArray } from './reserved' import { Mutation } from './Mutation' import { Computed } from './Computed' diff --git a/src/vmodel/share.js b/src/vmodel/share.js index 363d3b4..9ec06f4 100644 --- a/src/vmodel/share.js +++ b/src/vmodel/share.js @@ -1,4 +1,4 @@ -import { Anot, platform, isObject, modern } from '../seed/core' +import { Anot, platform, isObject } from '../seed/core' import { $$skipArray } from './reserved' import { Mutation } from './Mutation' import { Computed } from './Computed'