Fix display_svg_spec tests.

- Mark the test as async, and don't swallow exceptions.
- Fix the DOMElement polyfill to behave closer to the actual getAttributeNS
  method, which excludes the namespace prefix.
This commit is contained in:
Rob Wu 2017-07-14 18:55:17 +02:00
parent ca3c08f12b
commit 18566091aa
2 changed files with 43 additions and 21 deletions

View File

@ -60,7 +60,22 @@ function DOMElement(name) {
DOMElement.prototype = { DOMElement.prototype = {
getAttributeNS: function DOMElement_getAttributeNS(NS, name) { getAttributeNS: function DOMElement_getAttributeNS(NS, name) {
return name in this.attributes ? this.attributes[name] : null; // Fast path
if (name in this.attributes) {
return this.attributes[name];
}
// Slow path - used by test/unit/display_svg_spec.js
// Assuming that there is only one matching attribute for a given name,
// across all namespaces.
if (NS) {
var suffix = ':' + name;
for (var fullName in this.attributes) {
if (fullName.slice(-suffix.length) === suffix) {
return this.attributes[fullName];
}
}
}
return null;
}, },
setAttributeNS: function DOMElement_setAttributeNS(NS, name, value) { setAttributeNS: function DOMElement_setAttributeNS(NS, name, value) {

View File

@ -20,6 +20,8 @@ import { buildGetDocumentParams } from './test_utils';
import { getDocument } from '../../src/display/api'; import { getDocument } from '../../src/display/api';
import { SVGGraphics } from '../../src/display/svg'; import { SVGGraphics } from '../../src/display/svg';
const XLINK_NS = 'http://www.w3.org/1999/xlink';
// withZlib(true, callback); = run test with require('zlib') if possible. // withZlib(true, callback); = run test with require('zlib') if possible.
// withZlib(false, callback); = run test without require('zlib').deflateSync. // withZlib(false, callback); = run test without require('zlib').deflateSync.
// The return value of callback is returned as-is. // The return value of callback is returned as-is.
@ -41,14 +43,18 @@ function withZlib(isZlibRequired, callback) {
var zlib = __non_webpack_require__('zlib'); var zlib = __non_webpack_require__('zlib');
var deflateSync = zlib.deflateSync; var deflateSync = zlib.deflateSync;
zlib.deflateSync = function() { zlib.deflateSync = disabledDeflateSync;
function disabledDeflateSync() {
throw new Error('zlib.deflateSync is explicitly disabled for testing.'); throw new Error('zlib.deflateSync is explicitly disabled for testing.');
}; }
try { function restoreDeflateSync() {
return callback(); if (zlib.deflateSync === disabledDeflateSync) {
} finally {
zlib.deflateSync = deflateSync; zlib.deflateSync = deflateSync;
} }
}
var promise = callback();
promise.then(restoreDeflateSync, restoreDeflateSync);
return promise;
} }
describe('SVGGraphics', function () { describe('SVGGraphics', function () {
@ -86,12 +92,13 @@ describe('SVGGraphics', function () {
}; };
// This points to the XObject image in xobject-image.pdf. // This points to the XObject image in xobject-image.pdf.
var xobjectObjId = { ref: 4, gen: 0, }; var xobjectObjId = 'img_p0_1';
if (isNodeJS()) { if (isNodeJS()) {
setStubs(global); setStubs(global);
} }
try { try {
svgGfx.paintImageXObject(xobjectObjId, elementContainer); var imgData = svgGfx.objs.get(xobjectObjId);
svgGfx.paintInlineImageXObject(imgData, elementContainer);
} finally { } finally {
if (isNodeJS()) { if (isNodeJS()) {
unsetStubs(global); unsetStubs(global);
@ -101,35 +108,35 @@ describe('SVGGraphics', function () {
}); });
} }
it('should produce a reasonably small svg:image', function() { it('should produce a reasonably small svg:image', function(done) {
if (!isNodeJS()) { if (!isNodeJS()) {
pending('zlib.deflateSync is not supported in non-Node environments.'); pending('zlib.deflateSync is not supported in non-Node environments.');
} }
withZlib(true, getSVGImage).then(function(svgImg) { withZlib(true, getSVGImage).then(function(svgImg) {
expect(svgImg.nodeName).toBe('svg:image'); expect(svgImg.nodeName).toBe('svg:image');
expect(svgImg.getAttribute('width')).toBe('200px'); expect(svgImg.getAttributeNS(null, 'width')).toBe('200px');
expect(svgImg.getAttribute('height')).toBe('100px'); expect(svgImg.getAttributeNS(null, 'height')).toBe('100px');
var imgUrl = svgImg.getAttribute('xlink:href'); var imgUrl = svgImg.getAttributeNS(XLINK_NS, 'href');
// forceDataSchema = true, so the generated URL should be a data:-URL. // forceDataSchema = true, so the generated URL should be a data:-URL.
expect(imgUrl).toMatch(/^data:image\/png;base64,/); expect(imgUrl).toMatch(/^data:image\/png;base64,/);
// Test whether the generated image has a reasonable file size. // Test whether the generated image has a reasonable file size.
// I obtained a data URL of size 366 with Node 8.1.3 and zlib 1.2.11. // I obtained a data URL of size 366 with Node 8.1.3 and zlib 1.2.11.
// Without zlib (uncompressed), the size of the data URL was excessive // Without zlib (uncompressed), the size of the data URL was excessive
// (80247). // (80246).
expect(imgUrl.length).toBeLessThan(367); expect(imgUrl.length).toBeLessThan(367);
}); }).then(done, done.fail);
}); });
it('should produce a svg:image even if zlib is unavailable', function() { it('should be able to produce a svg:image without zlib', function(done) {
withZlib(false, getSVGImage).then(function(svgImg) { withZlib(false, getSVGImage).then(function(svgImg) {
expect(svgImg.nodeName).toBe('svg:image'); expect(svgImg.nodeName).toBe('svg:image');
expect(svgImg.getAttribute('width')).toBe('200px'); expect(svgImg.getAttributeNS(null, 'width')).toBe('200px');
expect(svgImg.getAttribute('height')).toBe('100px'); expect(svgImg.getAttributeNS(null, 'height')).toBe('100px');
var imgUrl = svgImg.getAttribute('xlink:href'); var imgUrl = svgImg.getAttributeNS(XLINK_NS, 'href');
expect(imgUrl).toMatch(/^data:image\/png;base64,/); expect(imgUrl).toMatch(/^data:image\/png;base64,/);
// The size of our naively generated PNG file is excessive :( // The size of our naively generated PNG file is excessive :(
expect(imgUrl.length).toBe(80247); expect(imgUrl.length).toBe(80246);
}); }).then(done, done.fail);
}); });
}); });
}); });