Skip to content

Commit

Permalink
fix(FileToken): framed or manual thumb image is unscale
Browse files Browse the repository at this point in the history
  • Loading branch information
bhsd-harry committed Jul 8, 2024
1 parent f34a196 commit 9d82f34
Show file tree
Hide file tree
Showing 18 changed files with 70 additions and 80 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
- [`ImageParameterToken.size`](https://github.com/bhsd-harry/wikiparser-node/wiki/ImageParameterToken#size) when the parameter ends with `pxpx`
- [`ImageParameterToken.link`](https://github.com/bhsd-harry/wikiparser-node/wiki/ImageParameterToken#link) when containing [`QuoteToken`](https://github.com/bhsd-harry/wikiparser-node/wiki/QuoteToken)
- Image parameter parsing for `link=` followed by an invalid external link
- Framed images and manual thumbnails are always unscaled

**Changed**

Expand Down
2 changes: 1 addition & 1 deletion i18n/zh-hans.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"invalid content in <$1>": "<$1>内的无效内容",
"invalid conversion flag": "无效的转换标记",
"invalid gallery image": "无效的图库图片",
"invalid gallery image parameter": "无效的图库图片参数",
"invalid image parameter": "无效的图片参数",
"invalid ISBN": "无效的ISBN",
"invalid link in <imagemap>": "无效的<imagemap>链接",
"invalid parameter of <$1>": "<$1>的无效参数",
Expand Down
2 changes: 1 addition & 1 deletion i18n/zh-hant.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"invalid content in <$1>": "<$1>內的無效內容",
"invalid conversion flag": "無效的轉換標記",
"invalid gallery image": "無效的圖庫圖片",
"invalid gallery image parameter": "無效的圖庫圖片參數",
"invalid image parameter": "無效的圖片參數",
"invalid ISBN": "無效的ISBN",
"invalid link in <imagemap>": "無效的<imagemap>連結",
"invalid parameter of <$1>": "<$1>的無效參數",
Expand Down
5 changes: 1 addition & 4 deletions lib/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,10 +297,7 @@ export class AstText extends AstNode {
];
} else if (char === ']' && previousType === 'free-ext-link' && severity === 'error') {
const i = start - previousSibling!.toString().length;
e.fix = {
range: [i, i],
text: '[',
};
e.fix = {range: [i, i], text: '['};
}
errors.push(e);
}
Expand Down
5 changes: 1 addition & 4 deletions src/arg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,7 @@ export abstract class ArgToken extends Token {
if (!this.getAttribute('include')) {
const e = generateForSelf(this, {start}, 'no-arg', 'unexpected template argument');
if (argDefault) {
e.fix = {
range: [start, e.endIndex],
text: argDefault.text(),
};
e.fix = {range: [start, e.endIndex], text: argDefault.text()};
}
return [e];
}
Expand Down
5 changes: 1 addition & 4 deletions src/attribute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,10 +381,7 @@ export abstract class AttributeToken extends Token {
);
e.startIndex--;
e.startCol--;
const fix: LintError.Fix = {
range: [e.endIndex, e.endIndex],
text: this.#quotes[0]!,
};
const fix: LintError.Fix = {range: [e.endIndex, e.endIndex], text: this.#quotes[0]!};
if (lastChild.childNodes.some(({type: t, data}) => t === 'text' && /\s/u.test(data))) {
e.suggestions = [
{
Expand Down
7 changes: 2 additions & 5 deletions src/converterFlags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,12 @@ export abstract class ConverterFlagsToken extends Token {
) {
const e = generateForChild(child, rect, 'no-ignored', 'invalid conversion flag');
if (variantFlags.size === 0 && definedFlags.has(flag.toUpperCase())) {
e.fix = {
range: [e.startIndex, e.endIndex],
text: flag.toUpperCase(),
};
e.fix = {range: [e.startIndex, e.endIndex], text: flag.toUpperCase()};
} else {
e.suggestions = [
{
desc: 'remove',
range: [e.startIndex, e.endIndex],
range: [e.startIndex - (i && 1), e.endIndex],
text: '',
},
];
Expand Down
7 changes: 4 additions & 3 deletions src/gallery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,25 +111,26 @@ export abstract class GalleryToken extends Token {
startLine = top + i,
startCol = i ? 0 : left;
if (child.type === 'noinclude' && trimmed && !/^<!--.*-->$/u.test(trimmed)) {
const endIndex = start + length;
errors.push({
rule: 'no-ignored',
message: Parser.msg('invalid content in <$1>', 'gallery'),
severity: trimmed.startsWith('|') ? 'warning' : 'error',
startIndex: start,
endIndex: start + length,
endIndex,
startLine,
endLine: startLine,
startCol,
endCol: startCol + length,
suggestions: [
{
desc: 'remove',
range: [start, start + length],
range: [start, endIndex],
text: '',
},
{
desc: 'comment',
range: [start, start + length],
range: [start, endIndex],
text: `<!--${str}-->`,
},
],
Expand Down
5 changes: 1 addition & 4 deletions src/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,7 @@ export abstract class HtmlToken extends Token {
} else if (msg === 'tag that is both closing and self-closing') {
const {html: [,, voidTags]} = this.getAttribute('config');
if (voidTags.includes(this.name)) {
error.fix = {
range: [start + 1, start + 2],
text: '',
};
error.fix = {range: [start + 1, start + 2], text: ''};
}
}
errors.push(error);
Expand Down
7 changes: 2 additions & 5 deletions src/imageParameter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,8 @@ export abstract class ImageParameterToken extends Token {
const errors = super.lint(start, re),
{link, name} = this;
if (name === 'invalid') {
const e = generateForSelf(this, {start}, 'invalid-gallery', 'invalid gallery image parameter');
e.fix = {
range: [start, start + e.endIndex],
text: '',
};
const e = generateForSelf(this, {start}, 'invalid-gallery', 'invalid image parameter');
e.fix = {range: [start - 1, e.endIndex], text: ''};
errors.push(e);
} else if (typeof link === 'object' && link.encoded) {
errors.push(generateForSelf(this, {start}, 'url-encoding', 'unnecessary URL encoding in an internal link'));
Expand Down
8 changes: 3 additions & 5 deletions src/link/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,12 @@ export abstract class LinkBaseToken extends Token {
if (type === 'link' || type === 'category') {
const textNode = linkText?.childNodes.find((c): c is AstText => c.type === 'text' && c.data.includes('|'));
if (textNode) {
const e = generateForChild(linkText!, rect, 'pipe-like', 'additional "|" in the link text', 'warning');
const e = generateForChild(linkText!, rect, 'pipe-like', 'additional "|" in the link text', 'warning'),
i = e.startIndex + textNode.getRelativeIndex();
e.suggestions = [
{
desc: 'escape',
range: [
e.startIndex + textNode.getRelativeIndex(),
e.startIndex + textNode.getRelativeIndex() + textNode.data.length,
],
range: [i, i + textNode.data.length],
text: textNode.data.replace(/\|/gu, '&#124;'),
},
];
Expand Down
46 changes: 33 additions & 13 deletions src/link/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {

sanitizeAlt,
} from '../../util/string';
import {font} from '../../util/html';
import {generateForChild, generateForSelf} from '../../util/lint';
import {BoundingRect} from '../../lib/rect';
import {Title} from '../../lib/title';
Expand Down Expand Up @@ -92,7 +93,8 @@ export abstract class FileToken extends LinkBaseToken {

/** 图片大小 */
get size(): {width: string, height: string} | undefined {
return this.getArg('width')?.size;
const fr = this.getFrame();
return fr === 'framed' || fr instanceof Title ? undefined : this.getArg('width')?.size;
}

set size(size) {
Expand Down Expand Up @@ -159,14 +161,23 @@ export abstract class FileToken extends LinkBaseToken {
const visibleNodes = childNodes.filter(node => node.text().trim());
return visibleNodes.length !== 1 || visibleNodes[0]!.type !== 'arg';
}),
keys = [...new Set(args.map(({name}) => name))].filter(key => key !== 'invalid'),
keys = [...new Set(args.map(({name}) => name))],
frameKeys = keys.filter(key => frame.has(key)),
horizAlignKeys = keys.filter(key => horizAlign.has(key)),
vertAlignKeys = keys.filter(key => vertAlign.has(key)),
[fr] = frameKeys,
unscaled = fr === 'framed' || fr === 'manualthumb',
rect = new BoundingRect(this, start);
if (this.closest('ext-link-text') && (this.getValue('link') as string | undefined)?.trim() !== '') {
errors.push(generateForSelf(this, rect, 'nested-link', 'internal link in an external link'));
}
if (unscaled) {
for (const arg of args.filter(({name}) => name === 'width')) {
const e = generateForChild(arg, rect, 'invalid-gallery', 'invalid image parameter');
e.fix = {range: [e.startIndex - 1, e.endIndex], text: ''};
errors.push(e);
}
}
if (
args.length === keys.length
&& frameKeys.length < 2
Expand All @@ -185,6 +196,9 @@ export abstract class FileToken extends LinkBaseToken {
(arg: ImageParameterToken): LintError =>
generateForChild(arg, rect, 'no-duplicate', Parser.msg(`${msg} image $1 parameter`, p1));
for (const key of keys) {
if (key === 'invalid' || key === 'width' && unscaled) {
continue;
}
let relevantArgs = args.filter(({name}) => name === key);
if (key === 'caption') {
relevantArgs = [...relevantArgs.slice(0, -1).filter(arg => arg.text()), ...relevantArgs.slice(-1)];
Expand Down Expand Up @@ -248,19 +262,13 @@ export abstract class FileToken extends LinkBaseToken {
*/
#getTypedArgs(keys: Set<string> | Map<string, string>, type: string): ImageParameterToken[] {
const args = this.getAllArgs().filter(({name}) => keys.has(name));

/* NOT FOR BROWSER */

if (args.length > 1) {
Parser.warn(
`The image ${this.name} has ${args.length} ${type} parameters. Only the last ${
args[0]!.name
} will take effect!`,
);
}

/* NOT FOR BROWSER END */

return args;
}

Expand Down Expand Up @@ -400,18 +408,30 @@ export abstract class FileToken extends LinkBaseToken {
if (link) {
try {
href = typeof link === 'string' ? this.getArg('link')!.getUrl()! : link.getUrl();
if (link === file) {
const lang = this.getValue('lang') as string | undefined,
page = this.getValue('page') as string | undefined;
if (lang) {
href += `?lang=${lang}`;
} else if (page) {
href += `?page=${page}`;
}
}
} catch {}
}
const a = link
? `<a${href && ` href="${href}"`}${hasLink ? '' : ` class="mw-file-description"`}${titleAttr}${
typeof link === 'string' ? ` rel="nofollow"` : ''
}>${img}</a>`
: `<span${titleAttr}>${img}</span>`;
return horiz || vert || visibleCaption
? `<figure${classAttr} typeof="mw:File${
fr ? `/${manual ? 'Thumb' : frame.get(fr)}` : ''
}">${a}<figcaption>${caption}</figcaption></figure>`
: `<span${classAttr}>${a}</span>`;
return font(
this,
horiz || vert || visibleCaption
? `<figure${classAttr} typeof="mw:File${
fr ? `/${manual ? 'Thumb' : frame.get(fr)}` : ''
}">${a}<figcaption>${caption}</figcaption></figure>`
: `<span${classAttr}>${a}</span>`,
);
}
}

Expand Down
5 changes: 1 addition & 4 deletions src/link/redirectTarget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,7 @@ export abstract class RedirectTargetToken extends LinkBaseToken {
const e = generateForChild(this.lastChild, {start}, 'no-ignored', 'useless link text');
e.startIndex--;
e.startCol--;
e.fix = {
range: [e.startIndex, e.endIndex],
text: '',
};
e.fix = {range: [e.startIndex, e.endIndex], text: ''};
errors.push(e);
}
return errors;
Expand Down
2 changes: 1 addition & 1 deletion src/nested.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export abstract class NestedToken extends Token {
},
{
desc: 'comment',
range: [e.startIndex, e.startIndex],
range: [e.startIndex, e.endIndex],
text: `<!--${child.toString()}-->`,
},
];
Expand Down
5 changes: 1 addition & 4 deletions src/nowiki/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,7 @@ export abstract class CommentToken extends NowikiBaseToken {
return [];
}
const e = generateForSelf(this, {start}, 'unclosed-comment', Parser.msg('unclosed $1', 'HTML comment'));
e.fix = {
range: [e.endIndex, e.endIndex],
text: '-->',
};
e.fix = {range: [e.endIndex, e.endIndex], text: '-->'};
return [e];
}

Expand Down
5 changes: 1 addition & 4 deletions src/nowiki/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ export abstract class NowikiToken extends NowikiBaseToken {
const {name, firstChild: {data}} = this;
if ((name === 'templatestyles' || name === 'section') && data) {
const e = generateForSelf(this, {start}, 'void-ext', Parser.msg('nothing should be in <$1>', name));
e.fix = {
range: [start - 1, e.endIndex + name.length + 3],
text: '/>',
};
e.fix = {range: [start, e.endIndex], text: ''};
return [e];
}
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
Expand Down
5 changes: 1 addition & 4 deletions src/parameter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,7 @@ export abstract class ParameterToken extends Token {
e.startCol = e.endCol;
e.endIndex++;
e.endCol++;
e.fix = {
range: [e.startIndex, e.endIndex],
text: '{{=}}',
};
e.fix = {range: [e.startIndex, e.endIndex], text: '{{=}}'};
errors.push(e);
}
return errors;
Expand Down
Loading

0 comments on commit 9d82f34

Please sign in to comment.