Răsfoiți Sursa

decouple createElement from DOM

Evan You 10 ani în urmă
părinte
comite
44f9ae68c0
2 a modificat fișierele cu 28 adăugiri și 25 ștergeri
  1. 27 1
      src/runtime/util/dom.js
  2. 1 24
      src/runtime/vdom/create-element.js

+ 27 - 1
src/runtime/util/dom.js

@@ -1,4 +1,4 @@
-import { isIE9 } from './env'
+import { inBrowser, isIE9 } from './env'
 import { warn } from './debug'
 import { makeMap } from '../../shared/util'
 
@@ -17,6 +17,32 @@ export const isReservedTag = makeMap(
   'content,element,shadow,template'
 )
 
+const unknownElementCache = Object.create(null)
+export function isUnknownElement (tag) {
+  if (!inBrowser) {
+    return true
+  }
+  tag = tag.toLowerCase()
+  if (unknownElementCache[tag] != null) {
+    return unknownElementCache[tag]
+  }
+  const el = document.createElement(tag)
+  if (tag.indexOf('-') > -1) {
+    // http://stackoverflow.com/a/28210364/1070244
+    return (unknownElementCache[tag] = (
+      el.constructor === window.HTMLUnknownElement ||
+      el.constructor === window.HTMLElement
+    ))
+  } else {
+    return (unknownElementCache[tag] = (
+      /HTMLUnknownElement/.test(el.toString()) &&
+      // Chrome returns unknown for several HTML5 elements.
+      // https://code.google.com/p/chromium/issues/detail?id=540526
+      !/^(data|time|rtc|rb)$/.test(tag)
+    ))
+  }
+}
+
 /**
  * Query an element selector if it's not an element already.
  *

+ 1 - 24
src/runtime/vdom/create-element.js

@@ -6,6 +6,7 @@ import {
   isPrimitive,
   isArray,
   isReservedTag,
+  isUnknownElement,
   resolveAsset
 } from '../util/index'
 
@@ -56,27 +57,3 @@ export function flatten (children) {
     return children
   }
 }
-
-const unknownElementCache = Object.create(null)
-
-function isUnknownElement (tag) {
-  tag = tag.toLowerCase()
-  if (unknownElementCache[tag] != null) {
-    return unknownElementCache[tag]
-  }
-  const el = document.createElement(tag)
-  if (tag.indexOf('-') > -1) {
-    // http://stackoverflow.com/a/28210364/1070244
-    return (unknownElementCache[tag] = (
-      el.constructor === window.HTMLUnknownElement ||
-      el.constructor === window.HTMLElement
-    ))
-  } else {
-    return (unknownElementCache[tag] = (
-      /HTMLUnknownElement/.test(el.toString()) &&
-      // Chrome returns unknown for several HTML5 elements.
-      // https://code.google.com/p/chromium/issues/detail?id=540526
-      !/^(data|time|rtc|rb)$/.test(tag)
-    ))
-  }
-}