mirror of
https://github.com/tenrok/vue-meta.git
synced 2026-06-19 19:50:34 +03:00
change to more versatile merge behaviour
This commit is contained in:
+15
-26
@@ -9,40 +9,29 @@ import getComponentOption from './getComponentOption'
|
||||
*/
|
||||
export default function getMetaInfo (component) {
|
||||
// collect & aggregate all metaInfo $options
|
||||
const info = getComponentOption({
|
||||
let info = getComponentOption({
|
||||
component,
|
||||
option: 'metaInfo',
|
||||
deep: true,
|
||||
// In order to prevent certain tags from being overwritten,
|
||||
// (like <meta name="description" ...> being overwritten by
|
||||
// <meta name="keywords" ...>), we need to specify a different
|
||||
// array merge strategy. This strategy exploits a trick
|
||||
// with associative arrays in JS using O(1) lookup
|
||||
|
||||
/* eslint-disable no-labels */
|
||||
arrayMerge (oldTags, newTags) {
|
||||
const updatedTags = []
|
||||
for (let oldTagIndex in oldTags) {
|
||||
const oldTag = oldTags[oldTagIndex]
|
||||
let sharedAttributes = false
|
||||
ifTagsHaveEqualSharedAttributeValues: for (let newTagIndex in newTags) {
|
||||
const newTag = newTags[newTagIndex]
|
||||
for (let attribute in newTag) {
|
||||
if (newTag.hasOwnProperty(attribute) && oldTag.hasOwnProperty(attribute)) {
|
||||
if (oldTag[attribute] === newTag[attribute]) {
|
||||
sharedAttributes = true
|
||||
break ifTagsHaveEqualSharedAttributeValues
|
||||
}
|
||||
}
|
||||
arrayMerge (target, source) {
|
||||
const destination = []
|
||||
for (let targetIndex in target) {
|
||||
const targetItem = target[targetIndex]
|
||||
let shared = false
|
||||
for (let sourceIndex in source) {
|
||||
const sourceItem = source[sourceIndex]
|
||||
if (targetItem.vmid === sourceItem.vmid) {
|
||||
shared = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if (!sharedAttributes) {
|
||||
updatedTags.push(oldTag)
|
||||
if (!shared) {
|
||||
destination.push(targetItem)
|
||||
}
|
||||
}
|
||||
return updatedTags.concat(newTags)
|
||||
|
||||
return destination.concat(source)
|
||||
}
|
||||
/* eslint-enable no-labels */
|
||||
})
|
||||
|
||||
// if any info options are a function, coerce them to the result of a call
|
||||
|
||||
Reference in New Issue
Block a user