-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BREAKING] Only rewrite image paths for RTL-variant when files exist (#…
…162) Only rewrite img path to img-RTL in RTL (right-to-left) CSS if image file is present in img-RTL folder. Don't rewrite img paths with a protocol (http/https/data/...) or starting with a slash (`/`). BREAKING CHANGE: This affects the output of the `rtl` (right-to-left) variant that is created by applying several mechanisms to create a mirrored variant of the CSS to be used in locales that use a right to left text direction. One aspect is adopting urls which contain an `img` folder in the path. Before this change, all urls have been changed to use a `img-RTL` folder instead. This allows mirrored images to be used, but it also requires an images to be available on that path even when the original image should be used (e.g. for a logo). With this change: - Urls are only adopted in case an `img-RTL` variant of that file exists - Urls with a protocol (http/https/data/...) or starting with a slash (`/`) are not adopted anymore Co-authored-by: Matthias Osswald <[email protected]>
- Loading branch information
1 parent
41b9ba6
commit 88e7a74
Showing
16 changed files
with
519 additions
and
49 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
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,67 @@ | ||
"use strict"; | ||
|
||
const path = require("path"); | ||
const url = require("url"); | ||
const less = require("../thirdparty/less"); | ||
|
||
const urlNodeNames = { | ||
"background": true, | ||
"background-image": true, | ||
"content": true, | ||
"cursor": true, | ||
"icon": true, | ||
"list-style-image": true | ||
}; | ||
|
||
/** | ||
* @constructor | ||
*/ | ||
const UrlCollectorPlugin = module.exports = function() { | ||
/* eslint-disable-next-line new-cap */ | ||
this.oVisitor = new less.tree.visitor(this); | ||
this.urls = new Map(); | ||
}; | ||
|
||
UrlCollectorPlugin.prototype = { | ||
isReplacing: false, | ||
isPreEvalVisitor: false, | ||
run: function(root) { | ||
return this.oVisitor.visit(root); | ||
}, | ||
visitRule: function(ruleNode) { | ||
if (urlNodeNames[ruleNode.name]) { | ||
this.visitUrl(ruleNode); | ||
} | ||
}, | ||
visitUrl: function(ruleNode) { | ||
for (const valueObject of ruleNode.value.value) { | ||
if (valueObject.type === "Url") { | ||
this.addUrlFromNode(valueObject); | ||
} else if (valueObject.type === "Expression") { | ||
for (const node of valueObject.value) { | ||
this.addUrlFromNode(node); | ||
} | ||
} | ||
} | ||
}, | ||
addUrlFromNode: function(node) { | ||
if (node.type === "Url") { | ||
const relativeUrl = node.value.value; | ||
|
||
const parsedUrl = url.parse(relativeUrl); | ||
// Ignore urls with protocol (also includes data urls) | ||
// Ignore server absolute urls | ||
if (parsedUrl.protocol || relativeUrl.startsWith("/")) { | ||
return; | ||
} | ||
|
||
const {currentDirectory} = node.currentFileInfo; | ||
|
||
const resolvedUrl = path.posix.join(currentDirectory, relativeUrl); | ||
this.urls.set(resolvedUrl, {currentDirectory, relativeUrl}); | ||
} | ||
}, | ||
getUrls: function() { | ||
return Array.from(this.urls.values()); | ||
} | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Oops, something went wrong.