Skip to content

+ [html5] support to register static css styles #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: html5-feature-vue-render
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions html5/render/vue/env/css.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import '../styles/reset.css'
import '../styles/components.css'

import { toCSSText } from '../utils'
import { validateStyles } from '../validator'

function getSelector (key, scopeId) {
if (scopeId === '@GLOBAL') {
return `.${key}`
}
return `.${key}[${scopeId}]`
}

/**
* Genearte <style>
* @param {String} scopeId
* @param {Array} cssMaps
*/
export function registerStaticStyles (scopeId, cssMaps) {
if (Array.isArray(cssMaps)) {
applyToDOM(cssMaps.map(css => {
const styles = []
for (const key in css) {
if (process.env.NODE_ENV === 'development') {
validateStyles(css[key])
}
styles.push(`${getSelector(key, scopeId)}{${toCSSText(css[key])}}`)
}
return styles.join('\n')
}))
}
}

function createStyleElement (cssText) {
const $css = document.createTextNode(cssText)
const $style = document.createElement('style')
$style.type = 'text/css'
$style.setAttribute('data-type', 'weex-styles')
$style.appendChild($css)
return $style
}

export function applyToDOM (styleList) {
if (styleList.length) {
const $head = document.head || document.getElementsByTagName('head')[0]
styleList.map(createStyleElement).forEach($style => {
$head.appendChild($style)
})
}
}
5 changes: 2 additions & 3 deletions html5/render/vue/env/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import '../styles/reset.css'
import '../styles/components.css'

// import 'lazyimg'
import '../../browser/render/gesture'

Expand All @@ -15,6 +12,7 @@ import 'core-js/modules/es6.promise'

import modules from '../modules'
import weex from './weex'
import { registerStaticStyles } from './css'

// register built-in modules.
weex.install(modules)
Expand All @@ -28,6 +26,7 @@ export function setVue (vue) {
}

window.weex = weex
window.__register_static_styles__ = registerStaticStyles
window.global = window

export default weex
36 changes: 33 additions & 3 deletions html5/render/vue/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export * from './lazyload'
export * from './style'
export * from './type'

import { normalize } from './flex'

/**
* Create a cached version of a pure function.
*/
Expand Down Expand Up @@ -71,11 +73,39 @@ export function nextFrame (callback) {
runner(callback)
}

export function toCSSText (object) {
/**
* add flex prefixes for compatibility conisderation.
*/
export function normalizeStyles (style) {
const styles = normalize(style)

const realStyle = {}
for (const key in styles) {
const name = hyphenate(key)
let value = styles[key]

// TODO: add more reliable check
if (typeof value === 'number' && value !== 'line-height') {
// TODO: support more units
value += 'px'
}

realStyle[name] = value
}
return realStyle
}

// TODO: prefix or hack
export function generateCSSText (key, value) {
return `${hyphenate(key)}:${value};`
}

export function toCSSText (object, rewriter = generateCSSText) {
let cssText = ''
if (object) {
for (const key in object) {
cssText += `${hyphenate(key)}:${object[key]};`
const styles = normalizeStyles(object)
for (const key in styles) {
cssText += rewriter(key, styles[key])
}
}
return cssText
Expand Down
9 changes: 0 additions & 9 deletions html5/render/vue/utils/style.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
import { normalize } from './flex'

/**
* remove comments from a cssText.
*/
export function trimComment (cssText) {
return cssText.replace(/(?:\/\*)[^*]*\*\//g, '')
}

/**
* add flex prefixes for compatibility conisderation.
*/
export function normalizeStyles (style) {
return normalize(style)
}

let support = null

export function supportSticky () {
Expand Down
5 changes: 4 additions & 1 deletion html5/render/vue/validator/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ export function fontStyle (value) {
}

export function fontWeight (value) {
return ['normal', 'bold', 'light', 'bolder', 'lighter'].indexOf(value) !== -1
return [
'100', '200', '300', '400', '500', '600', '700', '800', '900',
'normal', 'bold', 'light', 'bolder', 'lighter'
].indexOf(String(value)) !== -1
}

export function textDecoration (value) {
Expand Down