Skip to content

Commit

Permalink
Regex mode now supports acts
Browse files Browse the repository at this point in the history
  • Loading branch information
PRO-2684 committed Aug 8, 2024
1 parent 6ede58b commit a5131b3
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ This table shows supported parameters for each mode:
| ---------- | -- | --- | -- | --- | -- | --- | -- |
| `std` ||||||||
| `params` ||||||||
| `acts` |||| ||||
| `acts` |||| ||||
| `regex` ||||||||
| `replace` ||||||||
| ~~`ua`~~ ||||||||
Expand Down Expand Up @@ -264,6 +264,7 @@ Under Specific Parameter mode, pURLfy will:

| Param | Type | Default |
| --- | --- | --- |
| `acts` | `string[]` | `[]` |
| `regex` | `string[]` | Required |
| `replace` | `string[]` | Required |
| `continue` | `Boolean` | `true` |
Expand All @@ -272,6 +273,7 @@ Under Regex mode, pURLfy will, for each `regex`-`replace` pair:

1. Match the RegExp pattern specified in `regex` against the URL.
2. Replace all matched parts with the "replacement string" specified in `replace`.
3. Decode the result using the [processors](#-processors) specified in the `acts` array in order (if any `acts` value is invalid or throws an error, it is considered a failure and the original URL is returned).

If you'd like to learn more about the syntax of the "replacement string", please refer to the [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#specifying_a_string_as_the_replacement).

Expand Down
4 changes: 3 additions & 1 deletion README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ new Purlfy({
| ---------- | -- | --- | -- | --- | -- | --- | -- |
| `std` ||||||||
| `params` ||||||||
| `acts` |||| ||||
| `acts` |||| ||||
| `regex` ||||||||
| `replace` ||||||||
| ~~`ua`~~ ||||||||
Expand Down Expand Up @@ -265,6 +265,7 @@ new Purlfy({

| 参数 | 类型 | 默认值 |
| --- | --- | --- |
| `acts` | `string[]` | `[]` |
| `regex` | `string[]` | 必须 |
| `replace` | `string[]` | 必须 |
| `continue` | `Boolean` | `true` |
Expand All @@ -273,6 +274,7 @@ new Purlfy({

1. 在 URL 中匹配 `regex` 中指定的正则表达式
2. 替换所有匹配到的部分为 `replace` 中指定的“替换字符串”
3. 使用 `acts` 数组中指定的 [处理器](#-处理器) 依次对结果进行解码 (若任一 `acts` 值无效或执行出错,则认定失败,返回原 URL)

若您想要了解“替换字符串”的语法,请参考 [MDN 文档](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/replace#%E6%8C%87%E5%AE%9A%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%BD%9C%E4%B8%BA%E6%9B%BF%E6%8D%A2%E9%A1%B9)

Expand Down
5 changes: 3 additions & 2 deletions purlfy.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,8 @@ class Purlfy extends EventTarget {
const replace = rule.replace[i];
newUrl = newUrl.replaceAll(regex, replace);
}
if (URL.canParse(newUrl, urlObj.href)) { // Valid URL
newUrl = Purlfy.#applyActs(newUrl, rule.acts ?? [], logFunc);
if (newUrl && URL.canParse(newUrl, urlObj.href)) { // Valid URL
urlObj = new URL(newUrl, urlObj.href);
} else { // Invalid URL
logFunc("Invalid URL:", newUrl);
Expand Down Expand Up @@ -475,7 +476,7 @@ class Purlfy extends EventTarget {
let urlObj;
this.#log("Purifying URL:", originalUrl);
const optionalLocation = typeof location !== 'undefined' ? location.href : undefined;
if (URL.canParse(originalUrl, optionalLocation)) {
if (originalUrl && URL.canParse(originalUrl, optionalLocation)) {
urlObj = new URL(originalUrl, optionalLocation);
} else {
this.#log(`Cannot parse URL ${originalUrl}`);
Expand Down

0 comments on commit a5131b3

Please sign in to comment.