You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@weex.apache.org by ta...@apache.org on 2017/05/25 02:45:53 UTC

[11/50] incubator-weex git commit: * [html5] fix style regexp.

* [html5] fix style regexp.


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

Branch: refs/heads/0.13-dev
Commit: e30b1a543f5a6a896e763f1135eecb6bb3dd8c68
Parents: f8b7759
Author: MrRaindrop <te...@gmail.com>
Authored: Tue May 9 11:45:34 2017 +0800
Committer: MrRaindrop <te...@gmail.com>
Committed: Tue May 9 11:45:34 2017 +0800

----------------------------------------------------------------------
 html5/render/vue/env/weex.js    |  8 +++++-
 html5/render/vue/utils/style.js | 51 ++++++++++++++++++------------------
 2 files changed, 33 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/e30b1a54/html5/render/vue/env/weex.js
----------------------------------------------------------------------
diff --git a/html5/render/vue/env/weex.js b/html5/render/vue/env/weex.js
index dba6d3d..e5ae94b 100644
--- a/html5/render/vue/env/weex.js
+++ b/html5/render/vue/env/weex.js
@@ -33,7 +33,9 @@ const weex = {
     bundleUrl: location.href
   },
 
-  document: { body: {} },
+  document: {
+    body: {}
+  },
 
   requireModule (moduleName) {
     return weexModules[moduleName]
@@ -43,6 +45,10 @@ const weex = {
     return this.registerApiModule(...args)
   },
 
+  /**
+   * Register a new vue instance in this weex instance. Put its root element into weex.document.body.children, so
+   * that user can use weex.document.body to walk through all dom structures in all vue instances in the page.
+   */
   registerVueInstance (instance) {
     if (!instance instanceof Vue) {
       if (process.env.NODE_ENV === 'development') {

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/e30b1a54/html5/render/vue/utils/style.js
----------------------------------------------------------------------
diff --git a/html5/render/vue/utils/style.js b/html5/render/vue/utils/style.js
index 535afed..aca1fc7 100644
--- a/html5/render/vue/utils/style.js
+++ b/html5/render/vue/utils/style.js
@@ -66,13 +66,18 @@ export function normalizeUnitsNum (val: string): string {
   return parseScale(parseFloat(match[1]), unit)
 }
 
-function parseScale (val: number, unit: string): string {
+function getUnitScaleMap () {
   const { scale, dpr } = getViewportInfo()
-  const unitScaleMap = {
+  return {
     px: scale,
     wx: scale * dpr
   }
-  return val * unitScaleMap[unit] + 'px'
+}
+
+function parseScale (val: number, unit: string): string {
+  const unitScaleMap = getUnitScaleMap()
+  const res = val  * unitScaleMap[unit]
+  return (res === 0 ? 0 : res > 1 ? res : 1) + 'px'
 }
 
 export function normalizeString (styleKey: string, styleVal: string): string {
@@ -80,31 +85,27 @@ export function normalizeString (styleKey: string, styleVal: string): string {
     return styleVal
   }
 
-  // 1. test if is a regular scale css. e.g. `width: 100px;`
+  /**
+   * 1. test if is a regular scale css. e.g. `width: 100px;`
+   *  this should be a standalone number value with or without unit, otherwise
+   *  it shouldn't be changed.
+   */
   const unitsNum = normalizeUnitsNum(styleVal)
   if (unitsNum) { return unitsNum }
 
-  // 2. test if is a translate scale. e.g. `transform: translate2d(1px, 2px);`
-  const regTranslateString = /translate[^(]*\([+-\d ,.pwx]+\)/i // unit support: wx, px.
-  if (styleKey.match(/transform/i) && regTranslateString.test(styleVal)) {
-    const val = styleVal.replace(regTranslateString, function (translate) {
-      const reg = /([+-]?\d+(?:\.\d+)?)([p,w]x)?(?![dD])/g
-      return translate.replace(reg, function (m, $1, $2) {
-        const unit = $2 || 'px'
-        return parseScale($1, unit)
-      })
-    })
-    return val
-  }
-
-  // 3. test if is a border style. e.g. `border: 1px solid red;`
-  const regBorderKey = /^border(?:-(?:top|bottom|left|right))?$/
-  const regBorderVal = /^([+-]?\d+(?:\.\d+)?)([p ,w]x)?\s+/
-  if (regBorderKey.test(styleKey) && regBorderVal.test(styleVal)) {
-    const reg = /^([+-]?\d+(?:\.\d+)?)([p,w]x)?/
-    const val = styleVal.replace(reg, function (m, $1, $2) {
-      const unit = $2 || 'px'
-      return parseScale($1, unit)
+  /**
+   * 2. if a string contains multiple px values, than they should be all normalized.
+   *  values should have wx or px units, otherwise they should be left unchanged.
+   *  e.g.
+   *    transform: translate(10px, 6px, 0)
+   *    border: 2px solid red
+   */
+  const numReg = /([+-]?[\d.]+)([p,w]x)/ig
+  if (numReg.test(styleVal)) {
+    const unitScaleMap = getUnitScaleMap()
+    const val = styleVal.replace(numReg, function (m, $0, $1) {
+      const res = parseFloat($0) * unitScaleMap[$1]
+      return (res === 0 ? 0 : res > 1 ? res : 1) + 'px'
     })
     return val
   }