You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@weex.apache.org by fk...@apache.org on 2017/04/12 08:27:52 UTC

[09/17] incubator-weex git commit: * [html5] add tests for removing scoped stylesheet nodes.

* [html5] add tests for removing scoped stylesheet nodes.


Project: http://git-wip-us.apache.org/repos/asf/incubator-weex/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-weex/commit/501c85fa
Tree: http://git-wip-us.apache.org/repos/asf/incubator-weex/tree/501c85fa
Diff: http://git-wip-us.apache.org/repos/asf/incubator-weex/diff/501c85fa

Branch: refs/heads/0.11-dev
Commit: 501c85fa864eeb575872f46204a6f2e4d8a61eb1
Parents: e8ab269
Author: MrRaindrop <te...@gmail.com>
Authored: Thu Apr 6 23:05:44 2017 +0800
Committer: MrRaindrop <te...@gmail.com>
Committed: Thu Apr 6 23:05:44 2017 +0800

----------------------------------------------------------------------
 html5/render/vue/core/style.js                | 25 ++++++--
 html5/test/render/vue/components/switch.js    |  4 --
 html5/test/render/vue/components/text.js      |  4 --
 html5/test/render/vue/components/web.js       |  4 --
 html5/test/render/vue/core/scope-style-map.js | 70 ++++++++++++++++++++--
 html5/test/render/vue/core/style.js           | 16 ++++-
 html5/test/render/vue/helper/index.js         | 21 +++----
 html5/test/render/vue/helper/runtime.js       | 18 +++---
 package.json                                  |  2 +-
 packages/weex-vue-render/package.json         |  2 +-
 10 files changed, 120 insertions(+), 46 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/501c85fa/html5/render/vue/core/style.js
----------------------------------------------------------------------
diff --git a/html5/render/vue/core/style.js b/html5/render/vue/core/style.js
index 09357cf..d5b5d83 100644
--- a/html5/render/vue/core/style.js
+++ b/html5/render/vue/core/style.js
@@ -19,9 +19,17 @@ export function getHeadStyleMap () {
   const needToRemoveStyleSheetNodes = []
   const res = Array.from(document.styleSheets || [])
     .reduce((pre, styleSheet) => {
-      // why not using styleSheet.rules || styleSheet.cssRules to get css rules ?
-      // because weex's components defined non-standard style attributes, which is
-      // auto ignored when access rule.cssText.
+      /**
+       * why not using styleSheet.rules || styleSheet.cssRules to get css rules ?
+       * because weex's components defined non-standard style attributes, which is
+       * auto ignored when access rule.cssText.
+       */
+      if (!styleSheet.cssRules) {
+        /**
+         * no rules. just ignore this. probably a link stylesheet.
+         */
+        return pre
+      }
       const strArr = trimComment(styleSheet.ownerNode.textContent.trim()).split(/}/)
       const len = strArr.length
       const rules = []
@@ -77,9 +85,14 @@ export function getHeadStyleMap () {
       needToRemoveStyleSheetNodes.push(styleSheet.ownerNode)
       return pre
     }, {})
-  needToRemoveStyleSheetNodes.forEach(function (node) {
-    node.parentNode.removeChild(node)
-  })
+  if (!window._no_remove_style_sheets) {
+    needToRemoveStyleSheetNodes.forEach(function (node) {
+      node.parentNode.removeChild(node)
+    })
+  }
+  else if (process.env.NODE_ENV === 'development') {
+    console.warn(`[vue-render] you've defined '_no_remove_style_sheets' and the v-data-xx stylesheets will not be removed.`)
+  }
   if (process.env.NODE_ENV === 'development') {
     tagEnd('getHeadStyleMap')
   }

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/501c85fa/html5/test/render/vue/components/switch.js
----------------------------------------------------------------------
diff --git a/html5/test/render/vue/components/switch.js b/html5/test/render/vue/components/switch.js
index 2c892f3..36d8d1c 100644
--- a/html5/test/render/vue/components/switch.js
+++ b/html5/test/render/vue/components/switch.js
@@ -8,10 +8,6 @@ init('<switch> component', (Vue, helper) => {
     helper.register('switch', _switch)
   })
 
-  after(() => {
-    helper.reset()
-  })
-
   it('simple <switch> component', () => {
     const vm = compile(`<switch></switch>`)
 

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/501c85fa/html5/test/render/vue/components/text.js
----------------------------------------------------------------------
diff --git a/html5/test/render/vue/components/text.js b/html5/test/render/vue/components/text.js
index 4f5ff94..13e661f 100644
--- a/html5/test/render/vue/components/text.js
+++ b/html5/test/render/vue/components/text.js
@@ -9,10 +9,6 @@ init('<text> component', (Vue, helper) => {
     helper.register('text', text)
   })
 
-  after(() => {
-    helper.reset()
-  })
-
   it('create simple text component', () => {
     const vm = compile(`<text>abc</text>`)
 

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/501c85fa/html5/test/render/vue/components/web.js
----------------------------------------------------------------------
diff --git a/html5/test/render/vue/components/web.js b/html5/test/render/vue/components/web.js
index 642bd4a..ca61f0a 100644
--- a/html5/test/render/vue/components/web.js
+++ b/html5/test/render/vue/components/web.js
@@ -7,10 +7,6 @@ init('<web> component', (Vue, helper) => {
     helper.register('web', web)
   })
 
-  after(() => {
-    helper.reset()
-  })
-
   it('simple <web> component', () => {
     const vm = compile(`<web></web>`)
 

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/501c85fa/html5/test/render/vue/core/scope-style-map.js
----------------------------------------------------------------------
diff --git a/html5/test/render/vue/core/scope-style-map.js b/html5/test/render/vue/core/scope-style-map.js
index 2ead7e8..8968687 100644
--- a/html5/test/render/vue/core/scope-style-map.js
+++ b/html5/test/render/vue/core/scope-style-map.js
@@ -1,16 +1,78 @@
-import {
-  getHeadStyleMap
-} from '../../../../render/vue/core/style'
+import { getHeadStyleMap } from '../../../../render/vue/core/style'
 import '../data/head-css'
 import headMap from '../data/head-map'
 
-describe('style map', (Vue, helper) => {
+function toArray (arr) {
+  return Array.prototype.slice.call(arr)
+}
+
+function getVStyleSheetNodes () {
+  const regVStyleSheets = /((?:,?\s*\.[\w-]+\[data-v-\w+\](?::\w+)?)+)\s*({[^}]+)/
+  const nodes = toArray(document.styleSheets)
+    .filter(function (styleSheet) {
+      return regVStyleSheets.test(styleSheet.ownerNode.textContent)
+    }).map(function (styleSheet) {
+      return styleSheet.ownerNode
+    })
+  return nodes
+}
+
+function getStyleSheetNodes () {
+  return toArray(document.styleSheets)
+    .map(function (styleSheet) {
+      return styleSheet.ownerNode
+    })
+}
+
+describe('style map', () => {
+
+  let styleSheetNodes
+
+  before(function () {
+    styleSheetNodes = getStyleSheetNodes()
+  })
+
   it('should get head style map.', () => {
+    /**
+     * before get style map.
+     */
+    const vStyleSheetNodes = getVStyleSheetNodes()
+    const allNodes = getStyleSheetNodes()
+    const vNodesExisted = vStyleSheetNodes.filter(function (node) {
+      return allNodes.indexOf(node) > -1
+    })
+    expect(vNodesExisted).to.have.members(vStyleSheetNodes)
+
+    /**
+     * get style map.
+     */
+    window._no_remove_style_sheets = false
     const styleMap = getHeadStyleMap()
+    window._no_remove_style_sheets = true
+
+    /**
+     * after get style map.
+     */
     for (const key in headMap) {
       const expected = headMap[key]
       const result = styleMap[key]
+      debugger;
       expect(expected).be.deep.equal(result)
     }
+
+    const nowStyleSheetNodes = getStyleSheetNodes()
+    const vNodesLeft = vStyleSheetNodes.filter(function (node) {
+      return nowStyleSheetNodes.indexOf(node) > -1
+    })
+    expect(vNodesLeft).to.have.lengthOf(0)
+  })
+
+  after(function () {
+    const nodes = getStyleSheetNodes()
+    styleSheetNodes.forEach(function (node) {
+      if (nodes.indexOf(node) === -1) {
+        document.head.appendChild(node)
+      }
+    })
   })
 })

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/501c85fa/html5/test/render/vue/core/style.js
----------------------------------------------------------------------
diff --git a/html5/test/render/vue/core/style.js b/html5/test/render/vue/core/style.js
index 4b6b88f..cacdd57 100644
--- a/html5/test/render/vue/core/style.js
+++ b/html5/test/render/vue/core/style.js
@@ -11,10 +11,24 @@ import { init as initViewport, resetViewport } from '../../../../render/vue/env/
 
 import scopedStyleBundle from '../data/build/dotvue/scoped-style'
 
+function toArray (arr) {
+  return Array.prototype.slice.call(arr)
+}
+
+function getVStyleSheetNodes () {
+  const regVStyleSheets = /((?:,?\s*\.[\w-]+\[data-v-\w+\](?::\w+)?)+)\s*({[^}]+)/
+  const nodes = toArray(document.styleSheets)
+    .filter(function (styleSheet) {
+      return regVStyleSheets.test(styleSheet.ownerNode.textContent)
+    }).map(function (styleSheet) {
+      return styleSheet.ownerNode
+    })
+  return nodes
+}
+
 init('core style', (Vue, helper) => {
   const { compile, utils } = helper
   const { scale } = initViewport()
-
   before(() => {
     helper.register('div', div)
     helper.register('image', image)

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/501c85fa/html5/test/render/vue/helper/index.js
----------------------------------------------------------------------
diff --git a/html5/test/render/vue/helper/index.js b/html5/test/render/vue/helper/index.js
index 425aa5c..e89ac86 100644
--- a/html5/test/render/vue/helper/index.js
+++ b/html5/test/render/vue/helper/index.js
@@ -5,6 +5,7 @@ import Vue from 'vue/dist/vue.esm.js'
 // import { base, scrollable, style, inputCommon } from '../../../render/vue/mixins'
 import { base, style } from '../../../../render/vue/mixins'
 import weex from '../../../../render/vue/env/weex'
+import { setVue } from '../../../../render/vue/env'
 
 import * as utils from './utils'
 
@@ -23,8 +24,12 @@ export function init (title, fn) {
       Vue.mixin(base)
       Vue.mixin(style)
 
+      setVue(Vue)
+
       window.global = window
       global.weex = weex
+
+      window._no_remove_style_sheets = true
     })
 
     const helper = {
@@ -36,14 +41,8 @@ export function init (title, fn) {
        * @param  {object} component.
        */
       register (name, component) {
-        components[name] = component
-      },
-
-      /**
-       * reset registered components with empty object.
-       */
-      reset () {
-        components = {}
+        global.weex.registerComponent(name, component)
+        // components[name] = component
       },
 
       /**
@@ -52,7 +51,7 @@ export function init (title, fn) {
        * @return {Vue} vue instance.
        */
       createVm (options = {}) {
-        options.components = components
+        // options.components = components
         return new Vue(options).$mount()
       },
 
@@ -70,10 +69,6 @@ export function init (title, fn) {
      * describe a vue-render test for certain vue verson.
      */
     describe(`Vue ${Vue.version}`, () => {
-      after(function () {
-        helper.reset()
-      })
-
       fn(Vue, helper)
     })
   })

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/501c85fa/html5/test/render/vue/helper/runtime.js
----------------------------------------------------------------------
diff --git a/html5/test/render/vue/helper/runtime.js b/html5/test/render/vue/helper/runtime.js
index 6f5ea17..319fd20 100644
--- a/html5/test/render/vue/helper/runtime.js
+++ b/html5/test/render/vue/helper/runtime.js
@@ -5,6 +5,7 @@ import Vue from 'vue/dist/vue.runtime.esm.js'
 // import { base, scrollable, style, inputCommon } from '../../../render/vue/mixins'
 import { base, style } from '../../../../render/vue/mixins'
 import weex from '../../../../render/vue/env/weex'
+import { setVue } from '../../../../render/vue/env'
 
 import * as utils from './utils'
 
@@ -23,8 +24,12 @@ export function init (title, fn) {
       Vue.mixin(base)
       Vue.mixin(style)
 
+      setVue(Vue)
+
       window.global = window
       global.weex = weex
+
+      window._no_remove_style_sheets = true
     })
 
     const helper = {
@@ -36,15 +41,16 @@ export function init (title, fn) {
        * @param  {object} component.
        */
       register (name, component) {
+        global.weex.registerComponent(name, component)
         components[name] = component
       },
 
       /**
        * reset registered components with empty object.
        */
-      reset () {
-        components = {}
-      },
+      // reset () {
+      //   components = {}
+      // },
 
       /**
        * create a vm instance of Vue.
@@ -52,7 +58,7 @@ export function init (title, fn) {
        * @return {Vue} vue instance.
        */
       createVm (options = {}) {
-        options.components = components
+        // options.components = components
         return new Vue(options).$mount()
       },
 
@@ -70,10 +76,6 @@ export function init (title, fn) {
      * describe a vue-render test for certain vue verson.
      */
     describe(`Vue ${Vue.version}`, () => {
-      after(function () {
-        helper.reset()
-      })
-
       fn(Vue, helper)
     })
   })

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/501c85fa/package.json
----------------------------------------------------------------------
diff --git a/package.json b/package.json
index d5736c7..94d1027 100644
--- a/package.json
+++ b/package.json
@@ -4,7 +4,7 @@
   "subversion": {
     "browser": "0.5.0",
     "framework": "0.19.17",
-    "vue-render": "0.11.4",
+    "vue-render": "0.11.5",
     "transformer": ">=0.1.5 <0.5"
   },
   "description": "A framework for building Mobile cross-platform UI",

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/501c85fa/packages/weex-vue-render/package.json
----------------------------------------------------------------------
diff --git a/packages/weex-vue-render/package.json b/packages/weex-vue-render/package.json
index 81ddff0..9e28dc2 100644
--- a/packages/weex-vue-render/package.json
+++ b/packages/weex-vue-render/package.json
@@ -1,6 +1,6 @@
 {
   "name": "weex-vue-render",
-  "version": "0.11.4",
+  "version": "0.11.5",
   "description": "Weex built-in components for Vue 2.x.",
   "license": "Apache-2.0",
   "main": "src/render/vue/index.js",