From af636aae9699ff95885c72c3a81060ab225e92b5 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Thu, 4 Aug 2016 14:57:31 +0200 Subject: [PATCH 1/2] Add a parameter to the `isName` function that enables checking not just that something is a `Name`, but also that the actual `name` properties matches This is similar to the existing `isCmd` and `isDict` functions, which already support similar kind of checks. With the updated `isName` function, we'll be able to simplify many callsites from: `isName(someVariable) && someVariable.name === 'someName'` to: `isName(someVariable, 'someName')`. --- src/core/primitives.js | 14 ++++---------- test/unit/primitives_spec.js | 6 ++++++ 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/core/primitives.js b/src/core/primitives.js index ee4d5e4ea..6a17e990a 100644 --- a/src/core/primitives.js +++ b/src/core/primitives.js @@ -267,8 +267,8 @@ var RefSetCache = (function RefSetCacheClosure() { return RefSetCache; })(); -function isName(v) { - return v instanceof Name; +function isName(v, name) { + return v instanceof Name && (name === undefined || v.name === name); } function isCmd(v, cmd) { @@ -276,14 +276,8 @@ function isCmd(v, cmd) { } function isDict(v, type) { - if (!(v instanceof Dict)) { - return false; - } - if (!type) { - return true; - } - var dictType = v.get('Type'); - return isName(dictType) && dictType.name === type; + return v instanceof Dict && + (type === undefined || isName(v.get('Type'), type)); } function isRef(v) { diff --git a/test/unit/primitives_spec.js b/test/unit/primitives_spec.js index 68cd86542..5e344b759 100644 --- a/test/unit/primitives_spec.js +++ b/test/unit/primitives_spec.js @@ -244,6 +244,12 @@ describe('primitives', function() { var name = Name.get('Font'); expect(isName(name)).toEqual(true); }); + + it('handles names with name check', function () { + var name = Name.get('Font'); + expect(isName(name, 'Font')).toEqual(true); + expect(isName(name, 'Subtype')).toEqual(false); + }); }); describe('isCmd', function () { From 83ce6f0b6de3ca33d4a6add556e66edf8d5033ea Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Thu, 4 Aug 2016 15:13:37 +0200 Subject: [PATCH 2/2] Adjust the (applicable) existing `isName` callsites to use the new `isName(v, name)` version of the function --- src/core/annotation.js | 7 +++---- src/core/crypto.js | 2 +- src/core/evaluator.js | 10 ++++------ src/core/obj.js | 11 +++++------ 4 files changed, 13 insertions(+), 17 deletions(-) diff --git a/src/core/annotation.js b/src/core/annotation.js index 6540e0841..d6aa45d96 100644 --- a/src/core/annotation.js +++ b/src/core/annotation.js @@ -93,7 +93,7 @@ AnnotationFactory.prototype = /** @lends AnnotationFactory.prototype */ { case 'Widget': var fieldType = Util.getInheritableProperty(dict, 'FT'); - if (isName(fieldType) && fieldType.name === 'Tx') { + if (isName(fieldType, 'Tx')) { return new TextWidgetAnnotation(parameters); } return new WidgetAnnotation(parameters); @@ -338,10 +338,9 @@ var Annotation = (function AnnotationClosure() { } if (borderStyle.has('BS')) { var dict = borderStyle.get('BS'); - var dictType; + var dictType = dict.get('Type'); - if (!dict.has('Type') || (isName(dictType = dict.get('Type')) && - dictType.name === 'Border')) { + if (!dictType || isName(dictType, 'Border')) { this.borderStyle.setWidth(dict.get('W')); this.borderStyle.setStyle(dict.get('S')); this.borderStyle.setDashArray(dict.getArray('D')); diff --git a/src/core/crypto.js b/src/core/crypto.js index dfbadae4d..f30d9a69a 100644 --- a/src/core/crypto.js +++ b/src/core/crypto.js @@ -1909,7 +1909,7 @@ var CipherTransformFactory = (function CipherTransformFactoryClosure() { function CipherTransformFactory(dict, fileId, password) { var filter = dict.get('Filter'); - if (!isName(filter) || filter.name !== 'Standard') { + if (!isName(filter, 'Standard')) { error('unknown encryption method'); } this.dict = dict; diff --git a/src/core/evaluator.js b/src/core/evaluator.js index 904ae6cd2..dcfeb2771 100644 --- a/src/core/evaluator.js +++ b/src/core/evaluator.js @@ -287,7 +287,7 @@ var PartialEvaluator = (function PartialEvaluatorClosure() { var groupSubtype = group.get('S'); var colorSpace; - if (isName(groupSubtype) && groupSubtype.name === 'Transparency') { + if (isName(groupSubtype, 'Transparency')) { groupOptions.isolated = (group.get('I') || false); groupOptions.knockout = (group.get('K') || false); colorSpace = (group.has('CS') ? @@ -582,7 +582,7 @@ var PartialEvaluator = (function PartialEvaluatorClosure() { gStateObj.push([key, value]); break; case 'SMask': - if (isName(value) && value.name === 'None') { + if (isName(value, 'None')) { gStateObj.push([key, false]); break; } @@ -890,8 +890,7 @@ var PartialEvaluator = (function PartialEvaluatorClosure() { assert(isStream(xobj), 'XObject should be a stream'); var type = xobj.dict.get('Subtype'); - assert(isName(type), - 'XObject should have a Name subtype'); + assert(isName(type), 'XObject should have a Name subtype'); if (type.name === 'Form') { stateManager.save(); @@ -1602,8 +1601,7 @@ var PartialEvaluator = (function PartialEvaluatorClosure() { assert(isStream(xobj), 'XObject should be a stream'); var type = xobj.dict.get('Subtype'); - assert(isName(type), - 'XObject should have a Name subtype'); + assert(isName(type), 'XObject should have a Name subtype'); if ('Form' !== type.name) { xobjsCache.key = name; diff --git a/src/core/obj.js b/src/core/obj.js index b104f58fe..ba9f72f42 100644 --- a/src/core/obj.js +++ b/src/core/obj.js @@ -94,8 +94,7 @@ var Catalog = (function CatalogClosure() { var type = stream.dict.get('Type'); var subtype = stream.dict.get('Subtype'); - if (isName(type) && isName(subtype) && - type.name === 'Metadata' && subtype.name === 'XML') { + if (isName(type, 'Metadata') && isName(subtype, 'XML')) { // XXX: This should examine the charset the XML document defines, // however since there are currently no real means to decode // arbitrary charsets, let's just hope that the author of the PDF @@ -304,7 +303,7 @@ var Catalog = (function CatalogClosure() { assert(isDict(labelDict), 'The PageLabel is not a dictionary.'); var type = labelDict.get('Type'); - assert(!type || (isName(type) && type.name === 'PageLabel'), + assert(!type || isName(type, 'PageLabel'), 'Invalid type in PageLabel dictionary.'); var s = labelDict.get('S'); @@ -382,7 +381,7 @@ var Catalog = (function CatalogClosure() { var javaScript = []; function appendIfJavaScriptDict(jsDict) { var type = jsDict.get('S'); - if (!isName(type) || type.name !== 'JavaScript') { + if (!isName(type, 'JavaScript')) { return; } var js = jsDict.get('JS'); @@ -410,11 +409,11 @@ var Catalog = (function CatalogClosure() { var openactionDict = this.catDict.get('OpenAction'); if (isDict(openactionDict, 'Action')) { var actionType = openactionDict.get('S'); - if (isName(actionType) && actionType.name === 'Named') { + if (isName(actionType, 'Named')) { // The named Print action is not a part of the PDF 1.7 specification, // but is supported by many PDF readers/writers (including Adobe's). var action = openactionDict.get('N'); - if (isName(action) && action.name === 'Print') { + if (isName(action, 'Print')) { javaScript.push('print({});'); } } else {