aboutsummaryrefslogtreecommitdiffstats
path: root/src/elements/Style.js
blob: 643f3560250d82921fd5d790f7912dc1c1f43b87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { nodeOrNew, register, wrapWithAttrCheck } from '../utils/adopter.js'
import { registerMethods } from '../utils/methods.js'
import { unCamelCase } from '../utils/utils.js'
import Element from './Element.js'

function cssRule ( selector, rule ) {

  if ( !selector ) return ''
  if ( !rule ) return selector

  var ret = selector + '{'

  for ( var i in rule ) {

    ret += unCamelCase( i ) + ':' + rule[i] + ';'

  }

  ret += '}'

  return ret

}

export default class Style extends Element {

  constructor ( node ) {

    super( nodeOrNew( 'style', node ), node )

  }

  words ( w ) {

    this.node.textContent += ( w || '' )
    return this

  }

  font ( name, src, params = {} ) {

    return this.rule( '@font-face', {
      fontFamily: name,
      src: src,
      ...params
    } )

  }

  rule ( selector, obj ) {

    return this.words( cssRule( selector, obj ) )

  }

}

registerMethods( 'Dom', {
  style: wrapWithAttrCheck( function ( selector, obj ) {

    return this.put( new Style() ).rule( selector, obj )

  } ),
  fontface: wrapWithAttrCheck( function ( name, src, params ) {

    return this.put( new Style() ).font( name, src, params )

  } )
} )

register( Style )