forked from atom/atom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstyles-element.coffee
117 lines (85 loc) · 4.21 KB
/
styles-element.coffee
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
{Emitter, CompositeDisposable} = require 'event-kit'
class StylesElement extends HTMLElement
subscriptions: null
context: null
onDidAddStyleElement: (callback) ->
@emitter.on 'did-add-style-element', callback
onDidRemoveStyleElement: (callback) ->
@emitter.on 'did-remove-style-element', callback
onDidUpdateStyleElement: (callback) ->
@emitter.on 'did-update-style-element', callback
createdCallback: ->
@subscriptions = new CompositeDisposable
@emitter = new Emitter
@styleElementClonesByOriginalElement = new WeakMap
attachedCallback: ->
if @context is 'atom-text-editor'
for styleElement in @children
@upgradeDeprecatedSelectors(styleElement)
@context = @getAttribute('context') ? undefined
detachedCallback: ->
@subscriptions.dispose()
@subscriptions = new CompositeDisposable
attributeChangedCallback: (attrName, oldVal, newVal) ->
@contextChanged() if attrName is 'context'
initialize: (@styleManager) ->
throw new Error("Must pass a styleManager parameter when initializing a StylesElement") unless @styleManager?
@subscriptions.add @styleManager.observeStyleElements(@styleElementAdded.bind(this))
@subscriptions.add @styleManager.onDidRemoveStyleElement(@styleElementRemoved.bind(this))
@subscriptions.add @styleManager.onDidUpdateStyleElement(@styleElementUpdated.bind(this))
contextChanged: ->
return unless @subscriptions?
@styleElementRemoved(child) for child in Array::slice.call(@children)
@context = @getAttribute('context')
@styleElementAdded(styleElement) for styleElement in @styleManager.getStyleElements()
return
styleElementAdded: (styleElement) ->
return unless @styleElementMatchesContext(styleElement)
styleElementClone = styleElement.cloneNode(true)
styleElementClone.sourcePath = styleElement.sourcePath
styleElementClone.context = styleElement.context
styleElementClone.priority = styleElement.priority
@styleElementClonesByOriginalElement.set(styleElement, styleElementClone)
priority = styleElement.priority
if priority?
for child in @children
if child.priority > priority
insertBefore = child
break
@insertBefore(styleElementClone, insertBefore)
if @context is 'atom-text-editor'
@upgradeDeprecatedSelectors(styleElementClone)
@emitter.emit 'did-add-style-element', styleElementClone
styleElementRemoved: (styleElement) ->
return unless @styleElementMatchesContext(styleElement)
styleElementClone = @styleElementClonesByOriginalElement.get(styleElement) ? styleElement
styleElementClone.remove()
@emitter.emit 'did-remove-style-element', styleElementClone
styleElementUpdated: (styleElement) ->
return unless @styleElementMatchesContext(styleElement)
styleElementClone = @styleElementClonesByOriginalElement.get(styleElement)
styleElementClone.textContent = styleElement.textContent
@emitter.emit 'did-update-style-element', styleElementClone
styleElementMatchesContext: (styleElement) ->
not @context? or styleElement.context is @context
upgradeDeprecatedSelectors: (styleElement) ->
return unless styleElement.sheet?
upgradedSelectors = []
for rule in styleElement.sheet.cssRules
continue unless rule.selectorText?
continue if /\:host/.test(rule.selectorText)
inputSelector = rule.selectorText
outputSelector = rule.selectorText
.replace(/\.editor-colors($|[ >])/g, ':host$1')
.replace(/\.editor([:.][^ ,>]+)/g, ':host($1)')
.replace(/\.editor($|[ ,>])/g, ':host$1')
unless inputSelector is outputSelector
rule.selectorText = outputSelector
upgradedSelectors.push({inputSelector, outputSelector})
if upgradedSelectors.length > 0
warning = "Upgraded the following syntax theme selectors in `#{styleElement.sourcePath}` for shadow DOM compatibility:\n\n"
for {inputSelector, outputSelector} in upgradedSelectors
warning += "`#{inputSelector}` => `#{outputSelector}`\n"
warning += "\nSee the upgrade guide for information on removing this warning."
console.warn(warning)
module.exports = StylesElement = document.registerElement 'atom-styles', prototype: StylesElement.prototype