@@ -13,7 +13,9 @@ import { queryElements, getElementsKey } from '../../utils/elements.js'
13
13
export default function updateTag ( appId , options = { } , type , tags , head , body ) {
14
14
const { attribute, tagIDKeyName } = options
15
15
16
- const dataAttributes = [ tagIDKeyName , ...commonDataAttributes ]
16
+ const dataAttributes = commonDataAttributes . slice ( )
17
+ dataAttributes . push ( tagIDKeyName )
18
+
17
19
const newElements = [ ]
18
20
19
21
const queryOptions = { appId, attribute, type, tagIDKeyName }
@@ -36,103 +38,98 @@ export default function updateTag (appId, options = {}, type, tags, head, body)
36
38
} )
37
39
}
38
40
39
- if ( tags . length ) {
40
- for ( const tag of tags ) {
41
- if ( tag . skip ) {
42
- continue
43
- }
44
-
45
- const newElement = document . createElement ( type )
46
- newElement . setAttribute ( attribute , appId )
41
+ tags . forEach ( ( tag ) => {
42
+ if ( tag . skip ) {
43
+ return
44
+ }
47
45
48
- for ( const attr in tag ) {
49
- /* istanbul ignore next */
50
- if ( ! tag . hasOwnProperty ( attr ) ) {
51
- continue
52
- }
46
+ const newElement = document . createElement ( type )
47
+ newElement . setAttribute ( attribute , appId )
53
48
54
- if ( attr === 'innerHTML' ) {
55
- newElement . innerHTML = tag . innerHTML
56
- continue
57
- }
49
+ for ( const attr in tag ) {
50
+ /* istanbul ignore next */
51
+ if ( ! tag . hasOwnProperty ( attr ) ) {
52
+ continue
53
+ }
58
54
59
- if ( attr === 'json ' ) {
60
- newElement . innerHTML = JSON . stringify ( tag . json )
61
- continue
62
- }
55
+ if ( attr === 'innerHTML ' ) {
56
+ newElement . innerHTML = tag . innerHTML
57
+ continue
58
+ }
63
59
64
- if ( attr === 'cssText' ) {
65
- if ( newElement . styleSheet ) {
66
- /* istanbul ignore next */
67
- newElement . styleSheet . cssText = tag . cssText
68
- } else {
69
- newElement . appendChild ( document . createTextNode ( tag . cssText ) )
70
- }
71
- continue
72
- }
60
+ if ( attr === 'json' ) {
61
+ newElement . innerHTML = JSON . stringify ( tag . json )
62
+ continue
63
+ }
73
64
74
- if ( attr === 'callback' ) {
75
- newElement . onload = ( ) => tag [ attr ] ( newElement )
76
- continue
65
+ if ( attr === 'cssText' ) {
66
+ if ( newElement . styleSheet ) {
67
+ /* istanbul ignore next */
68
+ newElement . styleSheet . cssText = tag . cssText
69
+ } else {
70
+ newElement . appendChild ( document . createTextNode ( tag . cssText ) )
77
71
}
72
+ continue
73
+ }
78
74
79
- const _attr = includes ( dataAttributes , attr )
80
- ? `data-${ attr } `
81
- : attr
75
+ if ( attr === 'callback' ) {
76
+ newElement . onload = ( ) => tag [ attr ] ( newElement )
77
+ continue
78
+ }
82
79
83
- const isBooleanAttribute = includes ( booleanHtmlAttributes , attr )
84
- if ( isBooleanAttribute && ! tag [ attr ] ) {
85
- continue
86
- }
80
+ const _attr = includes ( dataAttributes , attr )
81
+ ? `data-${ attr } `
82
+ : attr
87
83
88
- const value = isBooleanAttribute ? '' : tag [ attr ]
89
- newElement . setAttribute ( _attr , value )
84
+ const isBooleanAttribute = includes ( booleanHtmlAttributes , attr )
85
+ if ( isBooleanAttribute && ! tag [ attr ] ) {
86
+ continue
90
87
}
91
88
92
- const oldElements = currentElements [ getElementsKey ( tag ) ]
89
+ const value = isBooleanAttribute ? '' : tag [ attr ]
90
+ newElement . setAttribute ( _attr , value )
91
+ }
93
92
94
- // Remove a duplicate tag from domTagstoRemove, so it isn't cleared.
95
- let indexToDelete
96
- const hasEqualElement = oldElements . some ( ( existingTag , index ) => {
97
- indexToDelete = index
98
- return newElement . isEqualNode ( existingTag )
99
- } )
93
+ const oldElements = currentElements [ getElementsKey ( tag ) ]
100
94
101
- if ( hasEqualElement && ( indexToDelete || indexToDelete === 0 ) ) {
102
- oldElements . splice ( indexToDelete , 1 )
103
- } else {
104
- newElements . push ( newElement )
105
- }
95
+ // Remove a duplicate tag from domTagstoRemove, so it isn't cleared.
96
+ let indexToDelete
97
+ const hasEqualElement = oldElements . some ( ( existingTag , index ) => {
98
+ indexToDelete = index
99
+ return newElement . isEqualNode ( existingTag )
100
+ } )
101
+
102
+ if ( hasEqualElement && ( indexToDelete || indexToDelete === 0 ) ) {
103
+ oldElements . splice ( indexToDelete , 1 )
104
+ } else {
105
+ newElements . push ( newElement )
106
106
}
107
- }
107
+ } )
108
108
109
- let oldElements = [ ]
110
- for ( const current of Object . values ( currentElements ) ) {
111
- oldElements = [
112
- ...oldElements ,
113
- ...current
114
- ]
109
+ const oldElements = [ ]
110
+ for ( const type in currentElements ) {
111
+ Array . prototype . push . apply ( oldElements , currentElements [ type ] )
115
112
}
116
113
117
114
// remove old elements
118
- for ( const element of oldElements ) {
115
+ oldElements . forEach ( ( element ) => {
119
116
element . parentNode . removeChild ( element )
120
- }
117
+ } )
121
118
122
119
// insert new elements
123
- for ( const element of newElements ) {
120
+ newElements . forEach ( ( element ) => {
124
121
if ( element . hasAttribute ( 'data-body' ) ) {
125
122
body . appendChild ( element )
126
- continue
123
+ return
127
124
}
128
125
129
126
if ( element . hasAttribute ( 'data-pbody' ) ) {
130
127
body . insertBefore ( element , body . firstChild )
131
- continue
128
+ return
132
129
}
133
130
134
131
head . appendChild ( element )
135
- }
132
+ } )
136
133
137
134
return {
138
135
oldTags : oldElements ,
0 commit comments