-
Notifications
You must be signed in to change notification settings - Fork 172
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/compatible anglur12 #368
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
81ed0bf
feat: 🎸 compatible angular12
maoxiaoke cab2c98
Merge branch 'master' into feat/compatible-anglur12
maoxiaoke 4defed5
feat: 🎸 handle base element
maoxiaoke ae88f58
feat: 🎸 handle .html
maoxiaoke 43e42ea
feat: 🎸 parse base & tests
maoxiaoke b1082cd
feat: 🎸 remove location mock
maoxiaoke 54e842e
refactor: 💡 refact code
maoxiaoke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,12 +9,14 @@ import type { Fetch } from './globalConfiguration'; | |
import type { ScriptAttributes } from '../apps'; | ||
|
||
const COMMENT_REGEX = /<!--.*?-->/g; | ||
const BASE_LOOSE_REGEX = /<base\s[^>]*href=['"]?([^'"]*)['"]?[^>]*>/; | ||
|
||
const EMPTY_STRING = ''; | ||
const STYLESHEET_LINK_TYPE = 'stylesheet'; | ||
|
||
const cachedScriptsContent: object = {}; | ||
const cachedStyleContent: object = {}; | ||
const cachedProcessedContent: object = {}; | ||
|
||
const defaultFetch = window?.fetch.bind(window); | ||
|
||
|
@@ -340,11 +342,29 @@ export function processHtml(html: string, entry?: string): ProcessedContent { | |
|
||
const domContent = (new DOMParser()).parseFromString(html.replace(COMMENT_REGEX, ''), 'text/html'); | ||
|
||
/* | ||
* When using DOMParser,the origin of relative path of `<script />` 和 `<link />` is Framwork's origin. | ||
* To escape this error, append <base /> element and then remove them to avoid conflict. | ||
*/ | ||
if (entry) { | ||
// add base URI for absolute resource. see more https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base | ||
const base = document.createElement('base'); | ||
base.href = entry; | ||
domContent.getElementsByTagName('head')[0].appendChild(base); | ||
const baseElementMatch = html.match(BASE_LOOSE_REGEX); | ||
|
||
const baseElements = domContent.getElementsByTagName('base'); | ||
const hasBaseElement = baseElements.length; | ||
|
||
if (baseElementMatch && hasBaseElement) { | ||
// Only take the first one into consideration. | ||
const baseElement = baseElements[0]; | ||
|
||
const [, baseHerf] = baseElementMatch; | ||
baseElement.href = isAbsoluteUrl(baseHerf) ? baseHerf : getUrl(entry, baseHerf); | ||
} else { | ||
// add base URI for absolute resource. | ||
// see more https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base | ||
const base = document.createElement('base'); | ||
base.href = entry; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 说明下 base 设置即便是 /xx/index.html 加载也可以 |
||
domContent.getElementsByTagName('head')[0].appendChild(base); | ||
} | ||
} | ||
|
||
// process js assets | ||
|
@@ -388,9 +408,13 @@ export function processHtml(html: string, entry?: string): ProcessedContent { | |
]; | ||
|
||
if (entry) { | ||
// remove base node | ||
const baseNode = domContent.getElementsByTagName('base')[0]; | ||
baseNode?.parentNode.removeChild(baseNode); | ||
/* | ||
* Remove all child's <base /> element to avoid conflict with parent's. | ||
*/ | ||
const baseNodes = domContent.getElementsByTagName('base'); | ||
for (let i = 0; i < baseNodes.length; ++i) { | ||
baseNodes[i]?.parentNode.removeChild(baseNodes[i]); | ||
} | ||
} | ||
|
||
return { | ||
|
@@ -402,8 +426,6 @@ export function processHtml(html: string, entry?: string): ProcessedContent { | |
}; | ||
} | ||
|
||
const cachedProcessedContent: object = {}; | ||
|
||
export async function getEntryAssets({ | ||
root, | ||
entry, | ||
|
@@ -420,9 +442,10 @@ export async function getEntryAssets({ | |
fetch?: Fetch; | ||
assertsCached?: boolean; | ||
}) { | ||
let cachedContent = cachedProcessedContent[assetsCacheKey]; | ||
const cachedContent = cachedProcessedContent[assetsCacheKey]; | ||
let htmlContent = entryContent; | ||
|
||
if (!cachedContent) { | ||
let htmlContent = entryContent; | ||
if (!htmlContent && entry) { | ||
if (!fetch) { | ||
warn('Current environment does not support window.fetch, please use custom fetch'); | ||
|
@@ -431,20 +454,18 @@ export async function getEntryAssets({ | |
); | ||
} | ||
|
||
const res = await fetch(entry); | ||
htmlContent = await res.text(); | ||
htmlContent = await fetch(entry).then((res) => res.text()); | ||
} | ||
cachedContent = processHtml(htmlContent, entry || href); | ||
cachedProcessedContent[assetsCacheKey] = cachedContent; | ||
cachedProcessedContent[assetsCacheKey] = htmlContent; | ||
} | ||
|
||
const { html } = cachedContent; | ||
const { html, assets } = processHtml(cachedContent ?? htmlContent, entry || href); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 执行结果上目前是没问题的,看下 cachedContent 设置为 false/ '' 的情况下 是拿 htmlContent 作为兜底还是以设置的值为准 |
||
|
||
if (root) { | ||
root.appendChild(html); | ||
} | ||
|
||
return cachedContent.assets; | ||
return assets; | ||
} | ||
|
||
export function getAssetsNode(): Array<HTMLStyleElement|HTMLScriptElement> { | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
baseElements.length > 0