-
Notifications
You must be signed in to change notification settings - Fork 922
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Co-authored-by: meteorlxy <[email protected]>
- Loading branch information
1 parent
2fe35bb
commit bfbab28
Showing
6 changed files
with
224 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
describe('updateHead', () => { | ||
it('should update head correctly', () => { | ||
// en-US | ||
cy.visit('/') | ||
|
||
// lang | ||
cy.get('html').should('have.attr', 'lang', 'en-US') | ||
// title | ||
cy.title().should('eq', 'VuePress E2E') | ||
cy.get('head title') | ||
.should('have.length', 1) | ||
.should('have.text', 'VuePress E2E') | ||
// description | ||
cy.get('head meta[name="description"]') | ||
.should('have.length', 1) | ||
.should('have.attr', 'content', 'VuePress E2E Test Site') | ||
// head | ||
cy.get('head meta[name="foo"]') | ||
.should('have.length', 1) | ||
.should('have.attr', 'content', 'foo') | ||
cy.get('head meta[name="bar"]') | ||
.should('have.length', 1) | ||
.should('have.attr', 'content', 'foobar') | ||
cy.get('head meta[name="baz"]') | ||
.should('have.length', 1) | ||
.should('have.attr', 'content', 'foobar baz') | ||
cy.get('head meta[name="foo-en"]') | ||
.should('have.length', 1) | ||
.should('have.attr', 'content', 'foo-en') | ||
|
||
// navigate to zh-CN | ||
cy.get('.e2e-theme-nav a').contains('zh-CN').click() | ||
|
||
// lang | ||
cy.get('html').should('have.attr', 'lang', 'zh-CN') | ||
// title | ||
cy.title().should('eq', 'VuePress E2E') | ||
cy.get('head title') | ||
.should('have.length', 1) | ||
.should('have.text', 'VuePress E2E') | ||
// description | ||
cy.get('head meta[name="description"]') | ||
.should('have.length', 1) | ||
.should('have.attr', 'content', 'VuePress E2E 测试站点') | ||
// head | ||
cy.get('head meta[name="foo"]') | ||
.should('have.length', 1) | ||
.should('have.attr', 'content', 'foo') | ||
cy.get('head meta[name="bar"]') | ||
.should('have.length', 1) | ||
.should('have.attr', 'content', 'foobar zh') | ||
cy.get('head meta[name="baz"]') | ||
.should('have.length', 1) | ||
.should('have.attr', 'content', 'baz') | ||
cy.get('head meta[name="foo-zh"]') | ||
.should('have.length', 1) | ||
.should('have.attr', 'content', 'foo-zh') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,46 @@ | ||
import type { HeadConfig } from '../types/index.js' | ||
|
||
const TAGS_ALLOWED = ['link', 'meta', 'script', 'style', 'noscript', 'template'] | ||
const TAGS_UNIQUE = ['title', 'base'] | ||
|
||
/** | ||
* Resolve identifier of a tag, to avoid duplicated tags in `<head>` | ||
*/ | ||
export const resolveHeadIdentifier = ([ | ||
tag, | ||
attrs, | ||
content, | ||
]: HeadConfig): string => { | ||
export const resolveHeadIdentifier = ([tag, attrs, content]: HeadConfig): | ||
| string | ||
| null => { | ||
// avoid duplicated unique tags | ||
if (TAGS_UNIQUE.includes(tag)) { | ||
return tag | ||
} | ||
|
||
// avoid disallowed tags | ||
if (!TAGS_ALLOWED.includes(tag)) { | ||
return null | ||
} | ||
|
||
// avoid duplicated `<meta>` with same `name` | ||
if (tag === 'meta' && attrs.name) { | ||
return `${tag}.${attrs.name}` | ||
} | ||
|
||
// there should be only one `<title>` or `<base>` | ||
if (['title', 'base'].includes(tag)) { | ||
return tag | ||
} | ||
|
||
// avoid duplicated `<template>` with same `id` | ||
if (tag === 'template' && attrs.id) { | ||
return `${tag}.${attrs.id}` | ||
} | ||
|
||
return JSON.stringify([tag, attrs, content]) | ||
return JSON.stringify([ | ||
tag, | ||
Object.entries(attrs) | ||
.map(([key, value]) => { | ||
// normalize boolean attributes | ||
if (typeof value === 'boolean') { | ||
return value ? [key, ''] : null | ||
} | ||
return [key, value] | ||
}) | ||
.filter((item): item is [string, string] => item != null) | ||
.sort(([keyA], [keyB]) => keyA.localeCompare(keyB)), | ||
content, | ||
]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters