exifreader 4.26.2 → 4.27.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/README.md +41 -11
- package/dist/exif-reader.js +1 -1
- package/dist/exif-reader.js.map +1 -1
- package/package.json +3 -2
- package/src/dom-parser.js +5 -1
- package/src/exif-reader.js +13 -3
- package/src/xmp-namespaces.js +93 -0
- package/src/xmp-tags.js +35 -14
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "exifreader",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.27.0",
|
|
4
4
|
"description": "Library that parses Exif metadata in images.",
|
|
5
5
|
"author": "Mattias Wallander <mattias@wallander.eu>",
|
|
6
6
|
"repository": {
|
|
@@ -37,6 +37,7 @@
|
|
|
37
37
|
"eslint": "^8.52.0",
|
|
38
38
|
"eslint-plugin-cypress": "^3.0.2",
|
|
39
39
|
"husky": "^9.0.11",
|
|
40
|
+
"linkedom": "^0.18.9",
|
|
40
41
|
"mocha": "^11.0.1",
|
|
41
42
|
"npm-run-all": "^4.1.5",
|
|
42
43
|
"nyc": "^17.0.0",
|
|
@@ -70,7 +71,7 @@
|
|
|
70
71
|
"check-coverage": true,
|
|
71
72
|
"statements": 93,
|
|
72
73
|
"branches": 88,
|
|
73
|
-
"functions":
|
|
74
|
+
"functions": 97,
|
|
74
75
|
"lines": 93,
|
|
75
76
|
"reporter": [
|
|
76
77
|
"lcov",
|
package/src/dom-parser.js
CHANGED
package/src/exif-reader.js
CHANGED
|
@@ -200,7 +200,17 @@ function getDataView(data) {
|
|
|
200
200
|
|
|
201
201
|
export function loadView(
|
|
202
202
|
dataView,
|
|
203
|
-
{
|
|
203
|
+
{
|
|
204
|
+
expanded = false,
|
|
205
|
+
async = false,
|
|
206
|
+
includeUnknown = false,
|
|
207
|
+
domParser = undefined,
|
|
208
|
+
} = {
|
|
209
|
+
expanded: false,
|
|
210
|
+
async: false,
|
|
211
|
+
includeUnknown: false,
|
|
212
|
+
domParser: undefined,
|
|
213
|
+
}
|
|
204
214
|
) {
|
|
205
215
|
let foundMetaData = false;
|
|
206
216
|
let tags = {};
|
|
@@ -267,7 +277,7 @@ export function loadView(
|
|
|
267
277
|
}
|
|
268
278
|
|
|
269
279
|
if (Constants.USE_TIFF && Constants.USE_XMP && readTags['ApplicationNotes'] && !hasXmpData(xmpChunks)) {
|
|
270
|
-
const readXmpTags = XmpTags.read(getStringValueFromArray(readTags['ApplicationNotes'].value));
|
|
280
|
+
const readXmpTags = XmpTags.read(getStringValueFromArray(readTags['ApplicationNotes'].value), undefined, domParser);
|
|
271
281
|
if (expanded) {
|
|
272
282
|
tags.xmp = readXmpTags;
|
|
273
283
|
} else {
|
|
@@ -330,7 +340,7 @@ export function loadView(
|
|
|
330
340
|
|
|
331
341
|
if (Constants.USE_XMP && hasXmpData(xmpChunks)) {
|
|
332
342
|
foundMetaData = true;
|
|
333
|
-
const readTags = XmpTags.read(dataView, xmpChunks);
|
|
343
|
+
const readTags = XmpTags.read(dataView, xmpChunks, domParser);
|
|
334
344
|
if (expanded) {
|
|
335
345
|
tags.xmp = readTags;
|
|
336
346
|
} else {
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
export function isMissingNamespaceError(error) {
|
|
2
|
+
const missingNamespaceStrings = [
|
|
3
|
+
// @xmldom/xmldom
|
|
4
|
+
'prefix is non-null and namespace is null',
|
|
5
|
+
// Firefox
|
|
6
|
+
'prefix not bound to a namespace', // en
|
|
7
|
+
'prefix inte bundet till en namnrymd', // sv
|
|
8
|
+
// Chrome
|
|
9
|
+
/Namespace prefix .+ is not defined/
|
|
10
|
+
];
|
|
11
|
+
|
|
12
|
+
for (let i = 0; i < missingNamespaceStrings.length; i++) {
|
|
13
|
+
const regexp = new RegExp(missingNamespaceStrings[i]);
|
|
14
|
+
if (regexp.test(error.message)) {
|
|
15
|
+
return true;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// This should fix most missing namespace errors, but it's not a complete solution.
|
|
23
|
+
export function addMissingNamespaces(xmlString) {
|
|
24
|
+
const rootTagMatch = xmlString.match(/<([A-Za-z_][A-Za-z0-9._-]*)([^>]*)>/);
|
|
25
|
+
if (!rootTagMatch) {
|
|
26
|
+
return xmlString;
|
|
27
|
+
}
|
|
28
|
+
const rootTagName = rootTagMatch[1];
|
|
29
|
+
|
|
30
|
+
const declaredPrefixes = getAllDeclaredNamespacePrefixes(xmlString);
|
|
31
|
+
const usedPrefixes = getUsedNamespacePrefixes(xmlString);
|
|
32
|
+
const missingPrefixes = usedPrefixes.filter((prefix) => declaredPrefixes.indexOf(prefix) === -1);
|
|
33
|
+
if (missingPrefixes.length === 0) {
|
|
34
|
+
return xmlString;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const namespaceDeclarations = createNamespaceDeclarations(missingPrefixes);
|
|
38
|
+
return insertDeclarationsIntoRoot(xmlString, rootTagName, namespaceDeclarations);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function getAllDeclaredNamespacePrefixes(xmlContent) {
|
|
42
|
+
const prefixes = [];
|
|
43
|
+
const namespaceDeclarationRegex = /xmlns:([\w-]+)=["'][^"']+["']/g;
|
|
44
|
+
let match;
|
|
45
|
+
while ((match = namespaceDeclarationRegex.exec(xmlContent)) !== null) {
|
|
46
|
+
if (prefixes.indexOf(match[1]) === -1) {
|
|
47
|
+
prefixes.push(match[1]);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return prefixes;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function getUsedNamespacePrefixes(xmlContent) {
|
|
54
|
+
const prefixes = [];
|
|
55
|
+
const prefixUsageRegex = /\b([A-Za-z_][A-Za-z0-9._-]*):[A-Za-z_][A-Za-z0-9._-]*\b/g;
|
|
56
|
+
let match;
|
|
57
|
+
while ((match = prefixUsageRegex.exec(xmlContent)) !== null) {
|
|
58
|
+
const prefix = match[1];
|
|
59
|
+
if (prefix === 'xmlns' || prefix === 'xml') {
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
if (prefixes.indexOf(prefix) === -1) {
|
|
63
|
+
prefixes.push(prefix);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return prefixes;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const KNOWN_NAMESPACE_URIS = {
|
|
70
|
+
xmp: 'http://ns.adobe.com/xap/1.0/',
|
|
71
|
+
tiff: 'http://ns.adobe.com/tiff/1.0/',
|
|
72
|
+
exif: 'http://ns.adobe.com/exif/1.0/',
|
|
73
|
+
dc: 'http://purl.org/dc/elements/1.1/',
|
|
74
|
+
xmpMM: 'http://ns.adobe.com/xap/1.0/mm/',
|
|
75
|
+
stEvt: 'http://ns.adobe.com/xap/1.0/sType/ResourceEvent#',
|
|
76
|
+
stRef: 'http://ns.adobe.com/xap/1.0/sType/ResourceRef#',
|
|
77
|
+
photoshop: 'http://ns.adobe.com/photoshop/1.0/'
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
function createNamespaceDeclarations(prefixes) {
|
|
81
|
+
const declarations = [];
|
|
82
|
+
for (let i = 0; i < prefixes.length; i++) {
|
|
83
|
+
const prefix = prefixes[i];
|
|
84
|
+
const uri = KNOWN_NAMESPACE_URIS[prefix] || 'http://fallback.namespace/' + prefix;
|
|
85
|
+
declarations.push(' xmlns:' + prefix + '="' + uri + '"');
|
|
86
|
+
}
|
|
87
|
+
return declarations.join('');
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function insertDeclarationsIntoRoot(xmlString, rootTagName, declarations) {
|
|
91
|
+
const rootTagPattern = new RegExp('<' + rootTagName + '([^>]*)>');
|
|
92
|
+
return xmlString.replace(rootTagPattern, '<' + rootTagName + '$1' + declarations + '>');
|
|
93
|
+
}
|
package/src/xmp-tags.js
CHANGED
|
@@ -5,25 +5,33 @@
|
|
|
5
5
|
import {getStringFromDataView, objectAssign} from './utils.js';
|
|
6
6
|
import XmpTagNames from './xmp-tag-names.js';
|
|
7
7
|
import DOMParser from './dom-parser.js';
|
|
8
|
+
import {isMissingNamespaceError, addMissingNamespaces} from './xmp-namespaces.js';
|
|
8
9
|
|
|
9
10
|
export default {
|
|
10
11
|
read
|
|
11
12
|
};
|
|
12
13
|
|
|
13
|
-
|
|
14
|
+
class ParseError extends Error {
|
|
15
|
+
constructor(message) {
|
|
16
|
+
super(message);
|
|
17
|
+
this.name = 'ParseError';
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function read(dataView, chunks, domParser) {
|
|
14
22
|
const tags = {};
|
|
15
23
|
|
|
16
24
|
if (typeof dataView === 'string') {
|
|
17
|
-
readTags(tags, dataView);
|
|
25
|
+
readTags(tags, dataView, domParser);
|
|
18
26
|
return tags;
|
|
19
27
|
}
|
|
20
28
|
|
|
21
29
|
const [standardXmp, extendedXmp] = extractCompleteChunks(dataView, chunks);
|
|
22
30
|
|
|
23
|
-
const hasStandardTags = readTags(tags, standardXmp);
|
|
31
|
+
const hasStandardTags = readTags(tags, standardXmp, domParser);
|
|
24
32
|
|
|
25
33
|
if (extendedXmp) {
|
|
26
|
-
const hasExtendedTags = readTags(tags, extendedXmp);
|
|
34
|
+
const hasExtendedTags = readTags(tags, extendedXmp, domParser);
|
|
27
35
|
|
|
28
36
|
if (!hasStandardTags && !hasExtendedTags) {
|
|
29
37
|
// Some writers are not spec-compliant in that they split an XMP
|
|
@@ -31,7 +39,7 @@ function read(dataView, chunks) {
|
|
|
31
39
|
// XMP block. If we failed parsing both of the XMPs in the regular
|
|
32
40
|
// way, we try to combine them to see if that works better.
|
|
33
41
|
delete tags._raw;
|
|
34
|
-
readTags(tags, combineChunks(dataView, chunks));
|
|
42
|
+
readTags(tags, combineChunks(dataView, chunks), domParser);
|
|
35
43
|
}
|
|
36
44
|
}
|
|
37
45
|
|
|
@@ -69,9 +77,9 @@ function combineChunks(dataView, chunks) {
|
|
|
69
77
|
return new DataView(combinedChunks.buffer);
|
|
70
78
|
}
|
|
71
79
|
|
|
72
|
-
function readTags(tags, chunkDataView) {
|
|
80
|
+
function readTags(tags, chunkDataView, domParser) {
|
|
73
81
|
try {
|
|
74
|
-
const {doc, raw} = getDocument(chunkDataView);
|
|
82
|
+
const {doc, raw} = getDocument(chunkDataView, domParser);
|
|
75
83
|
tags._raw = (tags._raw || '') + raw;
|
|
76
84
|
const rdf = getRDF(doc);
|
|
77
85
|
|
|
@@ -82,19 +90,15 @@ function readTags(tags, chunkDataView) {
|
|
|
82
90
|
}
|
|
83
91
|
}
|
|
84
92
|
|
|
85
|
-
function getDocument(chunkDataView) {
|
|
86
|
-
const domParser = DOMParser.get();
|
|
93
|
+
function getDocument(chunkDataView, _domParser) {
|
|
94
|
+
const domParser = DOMParser.get(_domParser);
|
|
87
95
|
if (!domParser) {
|
|
88
96
|
console.warn('Warning: DOMParser is not available. It is needed to be able to parse XMP tags.'); // eslint-disable-line no-console
|
|
89
97
|
throw new Error();
|
|
90
98
|
}
|
|
91
99
|
|
|
92
100
|
const xmlString = typeof chunkDataView === 'string' ? chunkDataView : getStringFromDataView(chunkDataView, 0, chunkDataView.byteLength);
|
|
93
|
-
const doc =
|
|
94
|
-
|
|
95
|
-
if (doc.documentElement.nodeName === 'parsererror') {
|
|
96
|
-
throw new Error(doc.documentElement.textContent);
|
|
97
|
-
}
|
|
101
|
+
const doc = parseFromString(domParser, trimXmlSource(xmlString));
|
|
98
102
|
|
|
99
103
|
return {
|
|
100
104
|
doc,
|
|
@@ -106,6 +110,23 @@ function trimXmlSource(xmlSource) {
|
|
|
106
110
|
return xmlSource.replace(/^.+(<\?xpacket begin)/, '$1').replace(/(<\?xpacket end=".*"\?>).+$/, '$1');
|
|
107
111
|
}
|
|
108
112
|
|
|
113
|
+
function parseFromString(domParser, xmlString, isRetry = false) {
|
|
114
|
+
try {
|
|
115
|
+
const doc = domParser.parseFromString(xmlString, 'application/xml');
|
|
116
|
+
const errors = doc.getElementsByTagName('parsererror');
|
|
117
|
+
if (errors.length > 0) {
|
|
118
|
+
throw new ParseError(errors[0].textContent);
|
|
119
|
+
}
|
|
120
|
+
return doc;
|
|
121
|
+
} catch (error) {
|
|
122
|
+
if (error.name === 'ParseError' && isMissingNamespaceError(error) && !isRetry) {
|
|
123
|
+
// Retry once after trying to fix the invalid XML.
|
|
124
|
+
return parseFromString(domParser, addMissingNamespaces(xmlString), true);
|
|
125
|
+
}
|
|
126
|
+
throw error;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
109
130
|
function getRDF(node) {
|
|
110
131
|
for (let i = 0; i < node.childNodes.length; i++) {
|
|
111
132
|
if (node.childNodes[i].tagName === 'x:xmpmeta') {
|