exifreader 4.43.0 → 4.44.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "exifreader",
3
- "version": "4.43.0",
3
+ "version": "4.44.0",
4
4
  "description": "Library that parses Exif metadata in images.",
5
5
  "author": "Mattias Wallander <mattias@wallander.eu>",
6
6
  "repository": {
@@ -40,7 +40,7 @@
40
40
  "cypress": "^15.20.1",
41
41
  "eslint": "^9.39.4",
42
42
  "eslint-plugin-cypress": "^6.4.4",
43
- "globals": "^17.9.0",
43
+ "globals": "^17.11.0",
44
44
  "husky": "^9.0.11",
45
45
  "linkedom": "^0.18.13",
46
46
  "mocha": "^11.8.0",
package/src/utils.js CHANGED
@@ -76,13 +76,30 @@ export function assertPromiseSupport() {
76
76
  export function objectAssign() {
77
77
  for (let i = 1; i < arguments.length; i++) {
78
78
  for (const property in arguments[i]) {
79
- arguments[0][property] = arguments[i][property];
79
+ setProperty(arguments[0], property, arguments[i][property]);
80
80
  }
81
81
  }
82
82
 
83
83
  return arguments[0];
84
84
  }
85
85
 
86
+ /**
87
+ * Sets a property whose name may come from an image. A plain assignment to
88
+ * `__proto__` replaces the object's prototype instead of storing the value,
89
+ * which loses the entry and makes the prototype's properties look like content.
90
+ *
91
+ * @param {object} object The object to set the property on.
92
+ * @param {string} key The property name.
93
+ * @param {*} value The property value.
94
+ */
95
+ export function setProperty(object, key, value) {
96
+ if (key === '__proto__') {
97
+ Object.defineProperty(object, key, {value, enumerable: true, writable: true, configurable: true});
98
+ return;
99
+ }
100
+ object[key] = value;
101
+ }
102
+
86
103
  export function deferInit(object, key, initializer) {
87
104
  let initialized = false;
88
105
  Object.defineProperty(object, key, {
@@ -21,13 +21,16 @@ export function isMissingNamespaceError(error) {
21
21
 
22
22
  // This should fix most missing namespace errors, but it's not a complete solution.
23
23
  export function addMissingNamespaces(xmlString) {
24
- const rootTagMatch = xmlString.match(/<([A-Za-z_][A-Za-z0-9._-]*)([^>]*)>/);
25
- if (!rootTagMatch) {
24
+ const rootTagStartIndex = findRootTagStartIndex(xmlString);
25
+ if (rootTagStartIndex === -1) {
26
+ return xmlString;
27
+ }
28
+ const {insertionIndex, attributeValueSpans} = scanStartTag(xmlString, rootTagStartIndex + 1);
29
+ if (insertionIndex === -1) {
26
30
  return xmlString;
27
31
  }
28
- const rootTagName = rootTagMatch[1];
29
32
 
30
- const declaredPrefixLookup = getDeclaredNamespacePrefixLookup(xmlString);
33
+ const declaredPrefixLookup = getDeclaredNamespacePrefixLookup(xmlString, rootTagStartIndex, insertionIndex, attributeValueSpans);
31
34
  const usedPrefixes = getUsedNamespacePrefixes(xmlString);
32
35
  const missingPrefixes = usedPrefixes.filter((prefix) => declaredPrefixLookup[prefix] === undefined);
33
36
  if (missingPrefixes.length === 0) {
@@ -35,25 +38,109 @@ export function addMissingNamespaces(xmlString) {
35
38
  }
36
39
 
37
40
  const namespaceDeclarations = createNamespaceDeclarations(missingPrefixes);
38
- return insertDeclarationsIntoRoot(xmlString, rootTagName, namespaceDeclarations);
41
+ return xmlString.slice(0, insertionIndex) + namespaceDeclarations + xmlString.slice(insertionIndex);
42
+ }
43
+
44
+ function findRootTagStartIndex(xmlString) {
45
+ let index = 0;
46
+ while (index < xmlString.length) {
47
+ index = xmlString.indexOf('<', index);
48
+ if (index === -1) {
49
+ return -1;
50
+ }
51
+ if (/[A-Za-z_]/.test(xmlString.charAt(index + 1))) {
52
+ return index;
53
+ }
54
+ if (hasSubstringAt(xmlString, '<!--', index)) {
55
+ index = skipPast(xmlString, index + '<!--'.length, '-->');
56
+ } else if (hasSubstringAt(xmlString, '<![CDATA[', index)) {
57
+ index = skipPast(xmlString, index + '<![CDATA['.length, ']]>');
58
+ } else if (xmlString.charAt(index + 1) === '?') {
59
+ index = skipPast(xmlString, index + '<?'.length, '?>');
60
+ } else {
61
+ index++;
62
+ }
63
+ }
64
+ return -1;
65
+ }
66
+
67
+ // Each attribute value span covers the value's content, excluding the quotes.
68
+ function scanStartTag(xmlString, fromIndex) {
69
+ const attributeValueSpans = [];
70
+ let quoteCharacter;
71
+ let valueStartIndex;
72
+ for (let i = fromIndex; i < xmlString.length; i++) {
73
+ const character = xmlString.charAt(i);
74
+ if (quoteCharacter !== undefined) {
75
+ if (character === quoteCharacter) {
76
+ attributeValueSpans.push({start: valueStartIndex, end: i});
77
+ quoteCharacter = undefined;
78
+ }
79
+ } else if (character === '"' || character === '\'') {
80
+ quoteCharacter = character;
81
+ valueStartIndex = i + 1;
82
+ } else if (character === '>') {
83
+ if (xmlString.charAt(i - 1) === '/') {
84
+ return {insertionIndex: i - 1, attributeValueSpans};
85
+ }
86
+ return {insertionIndex: i, attributeValueSpans};
87
+ }
88
+ }
89
+ return {insertionIndex: -1, attributeValueSpans};
90
+ }
91
+
92
+ function hasSubstringAt(xmlString, substring, index) {
93
+ return xmlString.slice(index, index + substring.length) === substring;
94
+ }
95
+
96
+ function skipPast(xmlString, fromIndex, endMarker) {
97
+ const endIndex = xmlString.indexOf(endMarker, fromIndex);
98
+ if (endIndex === -1) {
99
+ return xmlString.length;
100
+ }
101
+ return endIndex + endMarker.length;
39
102
  }
40
103
 
41
104
  // The lookup must not have a prototype. The prefixes come from the image, so a
42
105
  // prefix named e.g. __proto__ or constructor would otherwise be found among the
43
106
  // inherited properties and be treated as declared.
44
- function getDeclaredNamespacePrefixLookup(xmlContent) {
107
+ function getDeclaredNamespacePrefixLookup(xmlContent, rootTagStartIndex, insertionIndex, attributeValueSpans) {
45
108
  const prefixes = Object.create(null);
46
109
  // Must not miss a name getUsedNamespacePrefixes finds: one missed on the
47
110
  // root element is redeclared there, and the retry fails on the duplicate.
48
111
  // XML allows whitespace on either side of the equals sign (Eq ::= S? '=' S?).
49
- const namespaceDeclarationRegex = /xmlns:([A-Za-z_][A-Za-z0-9._-]*)\s*=\s*["'][^"']+["']/g;
112
+ const namespaceDeclarationRegex = /xmlns:([A-Za-z_][A-Za-z0-9._-]*)\s*=\s*["']([^"']*)["']/g;
50
113
  let match;
114
+ // The spans and the matches are both in document order, so one forward
115
+ // pointer finds the span that could hold each match.
116
+ let spanIndex = 0;
51
117
  while ((match = namespaceDeclarationRegex.exec(xmlContent)) !== null) {
52
- prefixes[match[1]] = true;
118
+ while (spanIndex < attributeValueSpans.length && attributeValueSpans[spanIndex].end <= match.index) {
119
+ spanIndex++;
120
+ }
121
+ if (spanIndex < attributeValueSpans.length && attributeValueSpans[spanIndex].start <= match.index) {
122
+ // Text inside a quoted attribute value is not an attribute, so it
123
+ // declares nothing to the XML parser either, and skipping it
124
+ // cannot cause the duplicate. Resuming at the value's end keeps a
125
+ // match inside it from consuming the declaration that follows.
126
+ namespaceDeclarationRegex.lastIndex = attributeValueSpans[spanIndex].end;
127
+ continue;
128
+ }
129
+ // An empty URI (illegal in Namespaces 1.0, an undeclaration in 1.1)
130
+ // counts only in the root start tag, the one element a duplicate can
131
+ // land on. Elsewhere it has to stay missing, so that the repair still
132
+ // declares the prefix on the root and binds the usages it can.
133
+ if (match[2] !== '' || isInRootStartTag(match.index, rootTagStartIndex, insertionIndex)) {
134
+ prefixes[match[1]] = true;
135
+ }
53
136
  }
54
137
  return prefixes;
55
138
  }
56
139
 
140
+ function isInRootStartTag(index, rootTagStartIndex, insertionIndex) {
141
+ return index > rootTagStartIndex && index < insertionIndex;
142
+ }
143
+
57
144
  function getUsedNamespacePrefixes(xmlContent) {
58
145
  const prefixes = [];
59
146
  const seenPrefixes = Object.create(null); // Must not have a prototype, the prefixes come from the image.
@@ -105,8 +192,3 @@ function getNamespaceUri(prefix) {
105
192
  }
106
193
  return 'http://fallback.namespace/' + prefix;
107
194
  }
108
-
109
- function insertDeclarationsIntoRoot(xmlString, rootTagName, declarations) {
110
- const rootTagPattern = new RegExp('<' + rootTagName + '([^>]*)>');
111
- return xmlString.replace(rootTagPattern, '<' + rootTagName + '$1' + declarations + '>');
112
- }
package/src/xmp-tags.js CHANGED
@@ -2,7 +2,7 @@
2
2
  * License, v. 2.0. If a copy of the MPL was not distributed with this
3
3
  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
4
 
5
- import {getStringFromDataView, objectAssign} from './utils.js';
5
+ import {getStringFromDataView, objectAssign, setProperty} from './utils.js';
6
6
  import XmpTagNames from './xmp-tag-names.js';
7
7
  import DOMParser from './dom-parser.js';
8
8
  import {isMissingNamespaceError, addMissingNamespaces} from './xmp-namespaces.js';
@@ -218,7 +218,7 @@ function getAttributes(element) {
218
218
  const attributes = {};
219
219
 
220
220
  for (let i = 0; i < element.attributes.length; i++) {
221
- attributes[element.attributes[i].nodeName] = decodeURIComponent(escape(element.attributes[i].value));
221
+ setProperty(attributes, element.attributes[i].nodeName, decodeURIComponent(escape(element.attributes[i].value)));
222
222
  }
223
223
 
224
224
  return attributes;
@@ -255,11 +255,11 @@ function parseNodeAttributesAsTags(attributes) {
255
255
  for (const name in attributes) {
256
256
  try {
257
257
  if (isTagAttribute(name)) {
258
- tags[getLocalName(name)] = {
258
+ setProperty(tags, getLocalName(name), {
259
259
  value: attributes[name],
260
260
  attributes: {},
261
261
  description: getDescription(attributes[name], name)
262
- };
262
+ });
263
263
  }
264
264
  } catch (error) {
265
265
  // Keep going and try to parse the rest of the tags.
@@ -288,7 +288,17 @@ function getDescription(value, name = undefined) {
288
288
  if (Array.isArray(value)) {
289
289
  const arrayDescription = getDescriptionOfArray(value);
290
290
  if (hasTagNameFunction(name)) {
291
- return XmpTagNames[name](value, arrayDescription);
291
+ // A description is a string, so a description function written for
292
+ // a plain value is ignored when it throws on a list or returns
293
+ // something other than a string.
294
+ try {
295
+ const description = XmpTagNames[name](value, arrayDescription);
296
+ if (typeof description === 'string') {
297
+ return description;
298
+ }
299
+ } catch (error) {
300
+ // Fall back to the descriptions of the items.
301
+ }
292
302
  }
293
303
  return arrayDescription;
294
304
  }
@@ -359,13 +369,11 @@ function getClearTextKey(key) {
359
369
  return key;
360
370
  }
361
371
 
362
- function parseNodeChildrenAsTags(children) {
363
- const tags = {};
364
-
372
+ function parseNodeChildrenAsTags(children, tags = {}) {
365
373
  for (const name in children) {
366
374
  try {
367
375
  if (!isNamespaceDefinition(name)) {
368
- tags[getLocalName(name)] = parseNodeAsTag(children[name], name);
376
+ setProperty(tags, getLocalName(name), parseNodeAsTag(children[name], name));
369
377
  }
370
378
  } catch (error) {
371
379
  // Keep going and try to parse the rest of the tags.
@@ -439,7 +447,7 @@ function parseNodeAttributes(node) {
439
447
 
440
448
  for (const name in node.attributes) {
441
449
  if ((name !== 'rdf:parseType') && (name !== 'rdf:resource') && (!isNamespaceDefinition(name))) {
442
- attributes[getLocalName(name)] = node.attributes[name];
450
+ setProperty(attributes, getLocalName(name), node.attributes[name]);
443
451
  }
444
452
  }
445
453
 
@@ -451,7 +459,7 @@ function parseNodeChildrenAsAttributes(node) {
451
459
 
452
460
  for (const name in node.value) {
453
461
  if ((name !== 'rdf:value') && (!isNamespaceDefinition(name))) {
454
- attributes[getLocalName(name)] = node.value[name].value;
462
+ setProperty(attributes, getLocalName(name), node.value[name].value);
455
463
  }
456
464
  }
457
465
 
@@ -459,7 +467,31 @@ function parseNodeChildrenAsAttributes(node) {
459
467
  }
460
468
 
461
469
  function parseRdfValue(node) {
462
- return getURIValue(node.value['rdf:value']) || node.value['rdf:value'].value;
470
+ const rdfValueNode = getLastNode(node.value['rdf:value']);
471
+ return getURIValue(rdfValueNode) || parseRdfValueContent(rdfValueNode);
472
+ }
473
+
474
+ // Repeated elements are collected into an array, and the last one wins, which
475
+ // is how a repeated tag is handled too.
476
+ function getLastNode(node) {
477
+ if (isDuplicateTag(node)) {
478
+ return node[node.length - 1];
479
+ }
480
+ return node;
481
+ }
482
+
483
+ // An rdf:value element stands in for the tag element, so a list inside it is
484
+ // the value of the tag rather than a tag of its own.
485
+ function parseRdfValueContent(rdfValueNode) {
486
+ if (isArray(rdfValueNode)) {
487
+ return parseNodeAsArray(rdfValueNode).value;
488
+ }
489
+ if (typeof rdfValueNode.value === 'object') {
490
+ // The child names come from the image, so a child named __proto__ must
491
+ // not be able to replace this object's prototype.
492
+ return parseNodeChildrenAsTags(rdfValueNode.value, Object.create(null));
493
+ }
494
+ return rdfValueNode.value;
463
495
  }
464
496
 
465
497
  function hasNestedStructureRdfDescription(node) {
@@ -507,7 +539,7 @@ function isArray(node) {
507
539
  }
508
540
 
509
541
  function getArrayChild(value) {
510
- return value['rdf:Bag'] || value['rdf:Seq'] || value['rdf:Alt'];
542
+ return getLastNode(value['rdf:Bag'] || value['rdf:Seq'] || value['rdf:Alt']);
511
543
  }
512
544
 
513
545
  function parseNodeAsArray(node, name) {