X Tutup
Skip to content

return if src is null or undefined in resolveImages#103

Merged
azu merged 3 commits intohonkit:masterfrom
ktat:ignore-src-error-in-resolveImages
Jul 31, 2020
Merged

return if src is null or undefined in resolveImages#103
azu merged 3 commits intohonkit:masterfrom
ktat:ignore-src-error-in-resolveImages

Conversation

@ktat
Copy link
Contributor

@ktat ktat commented Jul 29, 2020

in resolveImages, if src is null or undefined, LocationUtils.toAbsolute occurs error.

This occurs the following content and using gitbook-plugin-changelog.

<img
src="....">

This file's diff is like the following

+<img
+src="...">

In this case, resolveImages occurs error because LocationUtils.toAbsolute used in resolveImages expects src is string.

@azu azu added the Type: Bug Bug or Bug fixes label Jul 30, 2020
@azu
Copy link
Member

azu commented Jul 30, 2020

Thanks for Pull Request!

Is gitbook-plugin-changelog https://github.com/antonlegoo/gitbook-plugin-changelog?
I can not found <img> task on gitbook-plugin-changelog?
The <img src="...."> come from your commit message?

Mine understand that

commit message → gitbook-plugin-changelog → HonKit's resolveImages (bug) → invalid result

I think that src value is .... of your example. (not undefined and not null).
Does This fix work it correctly?

Also, Can you add test for resolveImages like resolveLinks.js?

@ktat
Copy link
Contributor Author

ktat commented Jul 30, 2020

@azu

The come from your commit message?

Yes, that's right.

When img tag has break return and src is started with line head, in the git diff, src is started with plus sign like this.

+<img
+src="...">

In this case, resolveImages can find img tag but cannot find src..
I confirmed this change fixes my case, but sorry, I didn't confirm src is whether null or undefined in this case.

But if src is null or undefined, the following line must be error.

src = LocationUtils.toAbsolute(src, currentDirectory, ".");

LocationUtils.toAbsolute calls toAbsorute and it calls normalize in location.js, this function expect string as argument.

So, I think resolveImages should check src is whether string or not.

@ktat
Copy link
Contributor Author

ktat commented Jul 30, 2020

@azu ah, I haven't detected which is real problem whether src with plus sign is problem or img tag with break line is problem

@ktat
Copy link
Contributor Author

ktat commented Jul 30, 2020

@azu I'll write test later, but it'll take a few days.

Also, Can you add test for resolveImages like resolveLinks.js?

@azu
Copy link
Member

azu commented Jul 30, 2020

In this case, resolveImages can find img tag but cannot find src..

Ah, I see.

This PR is reasonable to fix <img /> handling.

@azu ah, I haven't detected which is real problem whether src with plus sign is problem or img tag with break line is problem

Me too.
I expect that HonKit uses old version of cheerio and this bug come from old version cheerio, maybe.

In html, following img tag is valid syntax.

<img
src=x
>

@azu I'll write test later, but it'll take a few days.

Yes!

@ktat
Copy link
Contributor Author

ktat commented Jul 31, 2020

@azu I add new test.

I confirmed the test is aborted on master branch.

FAIL src/output/modifiers/__tests__/resolveImages.js
  ● resolveLinks › img tag with src with plus sign › no error occurs and return undefined

    TypeError: The "path" argument must be of type string. Received undefinedTypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined

      37 | // Normalize a path to be a link
      38 | function normalize(s) {
    > 39 |     return path.normalize(s).replace(/\\/g, "/");
         |                 ^
      40 | }
      41 | 
      42 | /**

      at normalize (src/utils/location.js:39:17)
      at Object.toAbsolute (src/utils/location.js:72:13)
      at src/output/modifiers/resolveImages.js:24:29
      at src/output/modifiers/editHTMLElement.js:11:16
      at src/utils/promise.js:36:16
      at src/utils/promise.js:22:20
      at _fulfilled (../../node_modules/q/q.js:854:54)
      at ../../node_modules/q/q.js:883:30

as a result, break return is no problem.
src with plus sign is problem. ex <img +src="..."> occurs error.

Copy link
Member

@azu azu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good! Thanks.

LGTM

@azu azu merged commit bedbf56 into honkit:master Jul 31, 2020
@azu
Copy link
Member

azu commented Aug 1, 2020

Relase v3.5.3

@ktat
Copy link
Contributor Author

ktat commented Aug 1, 2020

It seems that this fix is not included v3.5.3.
Something goes worng...?

The following is install log and result of grep.

npm WARN deprecated request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142
/home/ktat/.nodebrew/node/v14.5.0/bin/honkit -> /home/ktat/.nodebrew/node/v14.5.0/lib/node_modules/honkit/bin/honkit.js
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@~2.1.2 (node_modules/honkit/node_modules/chokidar/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@2.1.3: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

+ honkit@3.5.3
updated 1 package in 3.737s
[ktat@katt:(git)master] grep src /home/ktat/.nodebrew/node/v14.5.0/lib/node_modules/honkit/lib/output/modifiers/resolveImages.js
        let src = $img.attr("src");
        if (LocationUtils.isExternal(src) || LocationUtils.isDataURI(src)) {
        src = LocationUtils.toAbsolute(src, currentDirectory, ".");
        src = LocationUtils.relative(currentDirectory, src);
        $img.attr("src", src);

@azu
Copy link
Member

azu commented Aug 1, 2020

@ktat Ah, Nice catch!

I've added prepublish script and re-publish it as 3.5.4
810fcc7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Type: Bug Bug or Bug fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

X Tutup