Skip to content

throw errors when invalid values passed to dash(...) - #969

Merged
blikblum merged 3 commits into
foliojs:masterfrom
floyd-may:throw-on-bad-dash
May 7, 2019
Merged

blikblum merged 3 commits into
foliojs:masterfrom
floyd-may:throw-on-bad-dash

Conversation

@floyd-may

@floyd-may floyd-may commented May 3, 2019 •

Copy link
Copy Markdown
Contributor

The dash(...) function now throws errors when passed invalid values

Fixes #949. Current behavior is that dash(...) accepts values such as 0 for length, which breaks rendering in some viewers. There is a demo in the linked issue of the impact of the problem.

Checklist:

  • Tests (preference for unit tests)
  • Documentation
  • Update CHANGELOG.md
  • Ready to be merged

@floyd-may

Copy link
Copy Markdown
Contributor Author

@danthegoodman feedback appreciated 😄

@danthegoodman danthegoodman left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the PR! 😁

Comment thread lib/mixins/vector.js Outdated
return this.addContent(`[${length}] ${number(phase)} d`);
} else {
if (!Array.isArray(length) && !Number.isInteger(length) || length === 0) {
throw new Error(`dash(${length}) invalid, must be number greater than zero`);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

  1. !Array.isArray(length) has already been checked and will always return true. I think it should be omitted here.
  2. The length doesn't have to be an integer. The PDF spec allows floating point numbers. Number.isFinite(length) should get you your null check while also covering cases like NaN and strings and infinities.
  3. Your check is only for zero. You should also check for negative numbers.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed, nice catches. I went ahead and consolidated the parallel paths in the if and else - seems cleaner now.

Comment thread lib/mixins/vector.js Outdated
return this;
}
if (Array.isArray(length)) {
const hasZeros = length.filter(x => x === 0).length > 0;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

  1. Using either the every or some function here will be a little more efficient, since they can short-circuit and doesn't build up an intermediate array.
  2. Like the non-array check, this check should reject negative numbers, NaN, nulls and other invalid values.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

👍

@floyd-may

Copy link
Copy Markdown
Contributor Author

@danthegoodman I think it's ready for another look.

@danthegoodman danthegoodman left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice!

@blikblum / @devongovett I think this is good to go!

@blikblum

blikblum commented May 3, 2019

Copy link
Copy Markdown
Member

I'm afraid this will break rendering since does not handle space option.

Will do some tests.

@danthegoodman

Copy link
Copy Markdown
Contributor

@blikblum You're right. I missed that in my second review. Good catch.

@floyd-may

floyd-may commented May 4, 2019 via email

Copy link
Copy Markdown
Contributor Author

@blikblum

blikblum commented May 5, 2019 •

Copy link
Copy Markdown
Member

I added dash unit tests to ensure no regressions are made with f486d11

You should rebase and add the validation tests in vector.spec.js. As long as unit tests are running all is fine

@floyd-may
floyd-may force-pushed the throw-on-bad-dash branch from 035dd2f to fd29fe2 Compare May 6, 2019 13:52
@floyd-may

Copy link
Copy Markdown
Contributor Author

@blikblum thanks for those unit tests for dash(...) - that really illustrated how badly I had broken things 😅

I moved the new tests to vector.spec.js and fixed the issues that your unit tests revealed. I went ahead and rewrote the PR as a single commit as well.

@blikblum blikblum left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Some tweaks

Comment thread tests/unit/document.spec.js Resolved
Comment thread lib/mixins/vector.js Resolved
Comment thread lib/mixins/vector.js Resolved
blikblum and others added 2 commits May 7, 2019 08:29
Co-Authored-By: floyd-may <floyd.may@gmail.com>
@floyd-may

Copy link
Copy Markdown
Contributor Author

@blikblum one more look please?

@blikblum

blikblum commented May 7, 2019

Copy link
Copy Markdown
Member

Many thanks

@blikblum
blikblum merged commit d91b890 into foliojs:master May 7, 2019
@floyd-may
floyd-may deleted the throw-on-bad-dash branch May 7, 2019 16:15
@floyd-may

Copy link
Copy Markdown
Contributor Author

Any guidance on when this fix will be available in a published release?

bendemboski added a commit to chadkirby/SVG-to-PDFKit that referenced this pull request Apr 1, 2022
In PDFKit >=0.10.0, calling dash() with any 0s in the dash array/length will throw an error. For more info see

foliojs/pdfkit#1060
foliojs/pdfkit#949
foliojs/pdfkit#969

A number of SVG editors, I believe including inkscape, will happily generate `stroke-dasharray` attributes with 0s in them, and then running them through SVG-to-PDFKit will cause the conversion to fail completely. So, this commit does a bunch of math to rewrite dash arrays containing 0s to eliminate the zeros, but still achieve the same result.

This commit also updates the version of PDFKit the examples use to the latest (0.13.0) and adds a test that exercises a bunch of `stroke-dasharray` scenarios with 0s in them.
bendemboski added a commit to chadkirby/SVG-to-PDFKit that referenced this pull request Apr 1, 2022
In PDFKit >=0.10.0, calling dash() with any 0s in the dash array/length will throw an error. For more info see

foliojs/pdfkit#1060
foliojs/pdfkit#949
foliojs/pdfkit#969

A number of SVG editors, I believe including inkscape, will happily generate `stroke-dasharray` attributes with 0s in them, and then running them through SVG-to-PDFKit will cause the conversion to fail completely. So, this commit does a bunch of math to rewrite dash arrays containing 0s to eliminate the zeros, but still achieve the same result.

This commit also updates the version of PDFKit the examples use to the latest (0.13.0) and adds a test that exercises a bunch of `stroke-dasharray` scenarios with 0s in them.
bendemboski added a commit to chadkirby/SVG-to-PDFKit that referenced this pull request Apr 1, 2022
In PDFKit >=0.10.0, calling dash() with any 0s in the dash array/length will throw an error. For more info see

foliojs/pdfkit#1060
foliojs/pdfkit#949
foliojs/pdfkit#969

A number of SVG editors, I believe including inkscape, will happily generate `stroke-dasharray` attributes with 0s in them, and then running them through SVG-to-PDFKit will cause the conversion to fail completely. So, this commit does a bunch of math to rewrite dash arrays containing 0s to eliminate the zeros, but still achieve the same result.

This commit also updates the version of PDFKit the examples use to the latest (0.13.0) and adds a test that exercises a bunch of `stroke-dasharray` scenarios with 0s in them.
bendemboski added a commit to chadkirby/SVG-to-PDFKit that referenced this pull request Apr 1, 2022
In PDFKit >=0.10.0, calling dash() with any 0s in the dash array/length will throw an error. For more info see

foliojs/pdfkit#1060
foliojs/pdfkit#949
foliojs/pdfkit#969

A number of SVG editors, I believe including inkscape, will happily generate `stroke-dasharray` attributes with 0s in them, and then running them through SVG-to-PDFKit will cause the conversion to fail completely. So, this commit does a bunch of math to rewrite dash arrays containing 0s to eliminate the zeros, but still achieve the same result.

This commit also updates the version of PDFKit the examples use to the latest (0.13.0) and adds a test that exercises a bunch of `stroke-dasharray` scenarios with 0s in them.
bendemboski added a commit to chadkirby/SVG-to-PDFKit that referenced this pull request Apr 4, 2022
In PDFKit >=0.10.0, calling dash() with any 0s in the dash array/length will throw an error. For more info see

foliojs/pdfkit#1060
foliojs/pdfkit#949
foliojs/pdfkit#969

A number of SVG editors, I believe including inkscape, will happily generate `stroke-dasharray` attributes with 0s in them, and then running them through SVG-to-PDFKit will cause the conversion to fail completely. So, this commit does a bunch of math to rewrite dash arrays containing 0s to eliminate the zeros, but still achieve the same result.

This commit also updates the version of PDFKit the examples use to the latest (0.13.0) and adds a test that exercises a bunch of `stroke-dasharray` scenarios with 0s in them.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use of dash(0) emits PDF that some readers interpret as corrupt

3 participants