exifreader 4.42.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.
@@ -21,45 +21,145 @@ 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 declaredPrefixes = getAllDeclaredNamespacePrefixes(xmlString);
33
+ const declaredPrefixLookup = getDeclaredNamespacePrefixLookup(xmlString, rootTagStartIndex, insertionIndex, attributeValueSpans);
31
34
  const usedPrefixes = getUsedNamespacePrefixes(xmlString);
32
- const missingPrefixes = usedPrefixes.filter((prefix) => declaredPrefixes.indexOf(prefix) === -1);
35
+ const missingPrefixes = usedPrefixes.filter((prefix) => declaredPrefixLookup[prefix] === undefined);
33
36
  if (missingPrefixes.length === 0) {
34
37
  return 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);
39
42
  }
40
43
 
41
- function getAllDeclaredNamespacePrefixes(xmlContent) {
42
- const prefixes = [];
43
- const namespaceDeclarationRegex = /xmlns:([\w-]+)=["'][^"']+["']/g;
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;
102
+ }
103
+
104
+ // The lookup must not have a prototype. The prefixes come from the image, so a
105
+ // prefix named e.g. __proto__ or constructor would otherwise be found among the
106
+ // inherited properties and be treated as declared.
107
+ function getDeclaredNamespacePrefixLookup(xmlContent, rootTagStartIndex, insertionIndex, attributeValueSpans) {
108
+ const prefixes = Object.create(null);
109
+ // Must not miss a name getUsedNamespacePrefixes finds: one missed on the
110
+ // root element is redeclared there, and the retry fails on the duplicate.
111
+ // XML allows whitespace on either side of the equals sign (Eq ::= S? '=' S?).
112
+ const namespaceDeclarationRegex = /xmlns:([A-Za-z_][A-Za-z0-9._-]*)\s*=\s*["']([^"']*)["']/g;
44
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;
45
117
  while ((match = namespaceDeclarationRegex.exec(xmlContent)) !== null) {
46
- if (prefixes.indexOf(match[1]) === -1) {
47
- prefixes.push(match[1]);
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;
48
135
  }
49
136
  }
50
137
  return prefixes;
51
138
  }
52
139
 
140
+ function isInRootStartTag(index, rootTagStartIndex, insertionIndex) {
141
+ return index > rootTagStartIndex && index < insertionIndex;
142
+ }
143
+
53
144
  function getUsedNamespacePrefixes(xmlContent) {
54
145
  const prefixes = [];
55
- const prefixUsageRegex = /\b([A-Za-z_][A-Za-z0-9._-]*):[A-Za-z_][A-Za-z0-9._-]*\b/g;
146
+ const seenPrefixes = Object.create(null); // Must not have a prototype, the prefixes come from the image.
147
+ // The token must start the string or follow a consumed delimiter, i.e. a
148
+ // character that cannot be part of a prefix. A word boundary must not be
149
+ // used here: with \b, each letter following a dot or dash in a long
150
+ // colon-free run of prefix-like characters ("a.a.a...") starts a match
151
+ // attempt that reads the rest of the run, which makes the scan quadratic.
152
+ // (Lookbehind would read better but is not supported by the oldest
153
+ // targeted runtimes.)
154
+ const prefixUsageRegex = /(?:^|[^A-Za-z0-9._-])([A-Za-z_][A-Za-z0-9._-]*):[A-Za-z_][A-Za-z0-9._-]*/g;
56
155
  let match;
57
156
  while ((match = prefixUsageRegex.exec(xmlContent)) !== null) {
58
157
  const prefix = match[1];
59
158
  if (prefix === 'xmlns' || prefix === 'xml') {
60
159
  continue;
61
160
  }
62
- if (prefixes.indexOf(prefix) === -1) {
161
+ if (seenPrefixes[prefix] === undefined) {
162
+ seenPrefixes[prefix] = true;
63
163
  prefixes.push(prefix);
64
164
  }
65
165
  }
@@ -81,13 +181,14 @@ function createNamespaceDeclarations(prefixes) {
81
181
  const declarations = [];
82
182
  for (let i = 0; i < prefixes.length; i++) {
83
183
  const prefix = prefixes[i];
84
- const uri = KNOWN_NAMESPACE_URIS[prefix] || 'http://fallback.namespace/' + prefix;
85
- declarations.push(' xmlns:' + prefix + '="' + uri + '"');
184
+ declarations.push(' xmlns:' + prefix + '="' + getNamespaceUri(prefix) + '"');
86
185
  }
87
186
  return declarations.join('');
88
187
  }
89
188
 
90
- function insertDeclarationsIntoRoot(xmlString, rootTagName, declarations) {
91
- const rootTagPattern = new RegExp('<' + rootTagName + '([^>]*)>');
92
- return xmlString.replace(rootTagPattern, '<' + rootTagName + '$1' + declarations + '>');
189
+ function getNamespaceUri(prefix) {
190
+ if (Object.prototype.hasOwnProperty.call(KNOWN_NAMESPACE_URIS, prefix)) {
191
+ return KNOWN_NAMESPACE_URIS[prefix];
192
+ }
193
+ return 'http://fallback.namespace/' + prefix;
93
194
  }
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';
@@ -179,8 +179,11 @@ function getTextValue(node) {
179
179
  return node.nodeValue;
180
180
  }
181
181
 
182
+ // The elements object must not have a prototype. The element names come from
183
+ // the image, so an element named e.g. constructor would be found among the
184
+ // inherited properties, and one named __proto__ would replace the prototype.
182
185
  function getElementsFromNodes(nodes) {
183
- const elements = {};
186
+ const elements = Object.create(null);
184
187
 
185
188
  nodes.forEach((node) => {
186
189
  if (isElement(node)) {
@@ -215,7 +218,7 @@ function getAttributes(element) {
215
218
  const attributes = {};
216
219
 
217
220
  for (let i = 0; i < element.attributes.length; i++) {
218
- attributes[element.attributes[i].nodeName] = decodeURIComponent(escape(element.attributes[i].value));
221
+ setProperty(attributes, element.attributes[i].nodeName, decodeURIComponent(escape(element.attributes[i].value)));
219
222
  }
220
223
 
221
224
  return attributes;
@@ -252,11 +255,11 @@ function parseNodeAttributesAsTags(attributes) {
252
255
  for (const name in attributes) {
253
256
  try {
254
257
  if (isTagAttribute(name)) {
255
- tags[getLocalName(name)] = {
258
+ setProperty(tags, getLocalName(name), {
256
259
  value: attributes[name],
257
260
  attributes: {},
258
261
  description: getDescription(attributes[name], name)
259
- };
262
+ });
260
263
  }
261
264
  } catch (error) {
262
265
  // Keep going and try to parse the rest of the tags.
@@ -284,8 +287,18 @@ function getLocalName(name) {
284
287
  function getDescription(value, name = undefined) {
285
288
  if (Array.isArray(value)) {
286
289
  const arrayDescription = getDescriptionOfArray(value);
287
- if ((name) && (typeof XmpTagNames[name] === 'function')) {
288
- return XmpTagNames[name](value, arrayDescription);
290
+ if (hasTagNameFunction(name)) {
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
+ }
289
302
  }
290
303
  return arrayDescription;
291
304
  }
@@ -294,7 +307,7 @@ function getDescription(value, name = undefined) {
294
307
  }
295
308
 
296
309
  try {
297
- if ((name) && (typeof XmpTagNames[name] === 'function')) {
310
+ if (hasTagNameFunction(name)) {
298
311
  return XmpTagNames[name](value);
299
312
  }
300
313
  return decodeURIComponent(escape(value));
@@ -312,6 +325,12 @@ function getDescriptionOfArray(value) {
312
325
  }).join(', ');
313
326
  }
314
327
 
328
+ // The name comes from the image, so an inherited property of the tag name table
329
+ // must not be mistaken for a description function.
330
+ function hasTagNameFunction(name) {
331
+ return Object.prototype.hasOwnProperty.call(XmpTagNames, name) && (typeof XmpTagNames[name] === 'function');
332
+ }
333
+
315
334
  function getDescriptionOfObject(value) {
316
335
  const descriptions = [];
317
336
 
@@ -350,13 +369,11 @@ function getClearTextKey(key) {
350
369
  return key;
351
370
  }
352
371
 
353
- function parseNodeChildrenAsTags(children) {
354
- const tags = {};
355
-
372
+ function parseNodeChildrenAsTags(children, tags = {}) {
356
373
  for (const name in children) {
357
374
  try {
358
375
  if (!isNamespaceDefinition(name)) {
359
- tags[getLocalName(name)] = parseNodeAsTag(children[name], name);
376
+ setProperty(tags, getLocalName(name), parseNodeAsTag(children[name], name));
360
377
  }
361
378
  } catch (error) {
362
379
  // Keep going and try to parse the rest of the tags.
@@ -430,7 +447,7 @@ function parseNodeAttributes(node) {
430
447
 
431
448
  for (const name in node.attributes) {
432
449
  if ((name !== 'rdf:parseType') && (name !== 'rdf:resource') && (!isNamespaceDefinition(name))) {
433
- attributes[getLocalName(name)] = node.attributes[name];
450
+ setProperty(attributes, getLocalName(name), node.attributes[name]);
434
451
  }
435
452
  }
436
453
 
@@ -442,7 +459,7 @@ function parseNodeChildrenAsAttributes(node) {
442
459
 
443
460
  for (const name in node.value) {
444
461
  if ((name !== 'rdf:value') && (!isNamespaceDefinition(name))) {
445
- attributes[getLocalName(name)] = node.value[name].value;
462
+ setProperty(attributes, getLocalName(name), node.value[name].value);
446
463
  }
447
464
  }
448
465
 
@@ -450,7 +467,31 @@ function parseNodeChildrenAsAttributes(node) {
450
467
  }
451
468
 
452
469
  function parseRdfValue(node) {
453
- 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;
454
495
  }
455
496
 
456
497
  function hasNestedStructureRdfDescription(node) {
@@ -498,7 +539,7 @@ function isArray(node) {
498
539
  }
499
540
 
500
541
  function getArrayChild(value) {
501
- return value['rdf:Bag'] || value['rdf:Seq'] || value['rdf:Alt'];
542
+ return getLastNode(value['rdf:Bag'] || value['rdf:Seq'] || value['rdf:Alt']);
502
543
  }
503
544
 
504
545
  function parseNodeAsArray(node, name) {