-
Notifications
You must be signed in to change notification settings - Fork 273
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into suggestions-horizon
- Loading branch information
Showing
51 changed files
with
693 additions
and
430 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 |
---|---|---|
@@ -1 +1 @@ | ||
NaS6y8wA0e7VTQNYOV0o6eBd+fU= | ||
i/Y2atBKmmJfJWfPYtjAghmFTKw= |
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 @@ | ||
const roots = new Map(); | ||
let useLinks = false; | ||
let preloadLinks = true; | ||
|
||
/** | ||
* Use this function to provide the path to the directory where the css resources for the given package will be served from | ||
* | ||
* @public | ||
* @param packageName name of the package that is being configured | ||
* @param root path, accessible by the server that will serve the css resources | ||
*/ | ||
const setPackageCSSRoot = (packageName, root) => { | ||
roots.set(packageName, root); | ||
}; | ||
|
||
const getUrl = (packageName, path) => { | ||
return `${roots.get(packageName)}${path}`; | ||
}; | ||
|
||
/** | ||
* Call this function to enable or disable the usage of <link> tags instead of <style> tags to achieve CSP compliance | ||
* Example: "setUseLinks(true)" will unconditionally use <link> tags for all browsers; | ||
* Example: "setUseLinks(!document.adoptedStyleSheets) will only enable the usage of <link> tags for browsers that do not support constructable stylesheets. | ||
* | ||
* @public | ||
* @param use whether links will be used | ||
*/ | ||
const setUseLinks = use => { | ||
useLinks = use; | ||
}; | ||
|
||
/** | ||
* Call this function to enable or disable the preloading of <link> tags. | ||
* Note: only taken into account when <link> tags are being used. | ||
* Note: links are being preloaded by default, so call "setPreloadLinks(false)" to opt out of this. | ||
* | ||
* @public | ||
* @param preload | ||
*/ | ||
const setPreloadLinks = preload => { | ||
preloadLinks = preload; | ||
}; | ||
|
||
const shouldUseLinks = () => { | ||
return useLinks; | ||
}; | ||
|
||
const shouldPreloadLinks = () => { | ||
return preloadLinks; | ||
}; | ||
|
||
export { | ||
setPackageCSSRoot, | ||
getUrl, | ||
setUseLinks, | ||
setPreloadLinks, | ||
shouldUseLinks, | ||
shouldPreloadLinks, | ||
}; |
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
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,20 @@ | ||
import { getUrl } from "../CSP.js"; | ||
|
||
const flatten = arr => { | ||
return arr.reduce((acc, val) => acc.concat(Array.isArray(val) ? flatten(val) : val), []); | ||
}; | ||
|
||
const getEffectiveLinksHrefs = (ElementClass, forStaticArea = false) => { | ||
let stylesData = ElementClass[forStaticArea ? "staticAreaStyles" : "styles"]; | ||
if (!stylesData) { | ||
return; | ||
} | ||
|
||
if (!Array.isArray(stylesData)) { | ||
stylesData = [stylesData]; | ||
} | ||
|
||
return flatten(stylesData).filter(data => !!data).map(data => getUrl(data.packageName, data.fileName)); | ||
}; | ||
|
||
export default getEffectiveLinksHrefs; |
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,23 @@ | ||
import getEffectiveLinksHrefs from "./getEffectiveLinksHrefs.js"; | ||
import createLinkInHead from "../util/createLinkInHead.js"; | ||
import { shouldUseLinks, shouldPreloadLinks } from "../CSP.js"; | ||
|
||
const preloaded = new Set(); | ||
|
||
const preloadLinks = ElementClass => { | ||
if (!shouldUseLinks() || !shouldPreloadLinks()) { | ||
return; | ||
} | ||
|
||
const linksHrefs = getEffectiveLinksHrefs(ElementClass, false) || []; | ||
const staticAreaLinksHrefs = getEffectiveLinksHrefs(ElementClass, true) || []; | ||
|
||
[...linksHrefs, ...staticAreaLinksHrefs].forEach(href => { | ||
if (!preloaded.has(href)) { | ||
createLinkInHead(href, { rel: "preload", as: "style" }); | ||
preloaded.add(href); | ||
} | ||
}); | ||
}; | ||
|
||
export default preloadLinks; |
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,26 +1,30 @@ | ||
import executeTemplate from "./renderer/executeTemplate.js"; | ||
import getConstructableStyle from "./theming/getConstructableStyle.js"; | ||
import getEffectiveStyle from "./theming/getEffectiveStyle.js"; | ||
import getEffectiveLinksHrefs from "./theming/getEffectiveLinksHrefs.js"; | ||
import isLegacyBrowser from "./isLegacyBrowser.js"; | ||
import { shouldUseLinks } from "./CSP.js"; | ||
|
||
/** | ||
* Updates the shadow root of a UI5Element or its static area item | ||
* @param element | ||
* @param forStaticArea | ||
*/ | ||
const updateShadowRoot = (element, forStaticArea = false) => { | ||
let styleToPrepend; | ||
let styleStrOrHrefsArr; | ||
const template = forStaticArea ? "staticAreaTemplate" : "template"; | ||
const shadowRoot = forStaticArea ? element.staticAreaItem.shadowRoot : element.shadowRoot; | ||
const renderResult = executeTemplate(element.constructor[template], element); | ||
|
||
if (document.adoptedStyleSheets) { // Chrome | ||
if (shouldUseLinks()) { | ||
styleStrOrHrefsArr = getEffectiveLinksHrefs(element.constructor, forStaticArea); | ||
} else if (document.adoptedStyleSheets) { // Chrome | ||
shadowRoot.adoptedStyleSheets = getConstructableStyle(element.constructor, forStaticArea); | ||
} else if (!isLegacyBrowser()) { // FF, Safari | ||
styleToPrepend = getEffectiveStyle(element.constructor, forStaticArea); | ||
styleStrOrHrefsArr = getEffectiveStyle(element.constructor, forStaticArea); | ||
} | ||
|
||
element.constructor.render(renderResult, shadowRoot, styleToPrepend, { host: element }); | ||
element.constructor.render(renderResult, shadowRoot, styleStrOrHrefsArr, { host: element }); | ||
}; | ||
|
||
export default updateShadowRoot; |
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,19 @@ | ||
/** | ||
* Creates a <link> tag in the <head> tag | ||
* @param href - the CSS | ||
* @param attributes - optional attributes to add to the tag | ||
* @returns {HTMLElement} | ||
*/ | ||
const createLinkInHead = (href, attributes = {}) => { | ||
const link = document.createElement("link"); | ||
link.type = "text/css"; | ||
link.rel = "stylesheet"; | ||
|
||
Object.entries(attributes).forEach(pair => link.setAttribute(...pair)); | ||
|
||
link.href = href; | ||
document.head.appendChild(link); | ||
return link; | ||
}; | ||
|
||
export default createLinkInHead; |
Oops, something went wrong.