exifreader 4.43.0 → 4.44.1
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 +7 -6
- package/dist/exif-reader.js +1 -1
- package/dist/exif-reader.js.map +1 -1
- package/package.json +10 -9
- package/src/exif-reader.js +16 -15
- package/src/file-tags.js +2 -1
- package/src/image-header-jpeg.js +7 -1
- package/src/jfif-tags.js +2 -1
- package/src/photoshop-tags.js +6 -3
- package/src/png-text-tags.js +1 -1
- package/src/utils.js +18 -1
- package/src/xmp-namespaces.js +95 -13
- package/src/xmp-tags.js +45 -13
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "exifreader",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.44.1",
|
|
4
4
|
"description": "Library that parses Exif metadata in images.",
|
|
5
5
|
"author": "Mattias Wallander <mattias@wallander.eu>",
|
|
6
6
|
"repository": {
|
|
@@ -30,27 +30,27 @@
|
|
|
30
30
|
"devDependencies": {
|
|
31
31
|
"@babel/core": "^7.29.7",
|
|
32
32
|
"@babel/preset-env": "^7.29.7",
|
|
33
|
-
"@cypress/webpack-preprocessor": "^
|
|
33
|
+
"@cypress/webpack-preprocessor": "^8.0.0",
|
|
34
34
|
"@eslint/js": "^9.39.5",
|
|
35
|
-
"@types/node": "^26.
|
|
35
|
+
"@types/node": "^26.4.0",
|
|
36
36
|
"babel-loader": "^10.0.0",
|
|
37
37
|
"c8": "^12.0.0",
|
|
38
38
|
"chai": "^6.2.2",
|
|
39
39
|
"cross-env": "^10.0.0",
|
|
40
|
-
"cypress": "^15.
|
|
40
|
+
"cypress": "^15.21.1",
|
|
41
41
|
"eslint": "^9.39.4",
|
|
42
42
|
"eslint-plugin-cypress": "^6.4.4",
|
|
43
|
-
"globals": "^17.
|
|
43
|
+
"globals": "^17.12.0",
|
|
44
44
|
"husky": "^9.0.11",
|
|
45
45
|
"linkedom": "^0.18.13",
|
|
46
|
-
"mocha": "^
|
|
46
|
+
"mocha": "^12.0.0",
|
|
47
47
|
"npm-run-all2": "^9.0.3",
|
|
48
48
|
"rimraf": "^5.0.5",
|
|
49
49
|
"string-replace-loader": "^3.1.0",
|
|
50
50
|
"terser-webpack-plugin": "^5.6.1",
|
|
51
51
|
"typescript": "^7.0.2",
|
|
52
|
-
"webpack": "5.
|
|
53
|
-
"webpack-cli": "^7.2.
|
|
52
|
+
"webpack": "5.110.2",
|
|
53
|
+
"webpack-cli": "^7.2.3",
|
|
54
54
|
"webpack-dev-server": "^5.2.6"
|
|
55
55
|
},
|
|
56
56
|
"scripts": {
|
|
@@ -85,7 +85,8 @@
|
|
|
85
85
|
"text"
|
|
86
86
|
],
|
|
87
87
|
"exclude": [
|
|
88
|
-
"test/**"
|
|
88
|
+
"test/**",
|
|
89
|
+
".mocharc.cjs"
|
|
89
90
|
]
|
|
90
91
|
},
|
|
91
92
|
"browserslist": [
|
package/src/exif-reader.js
CHANGED
|
@@ -230,6 +230,7 @@ export function loadView(
|
|
|
230
230
|
Constants.USE_TIFF
|
|
231
231
|
&& Constants.USE_XMP
|
|
232
232
|
&& parsedExifTags['ApplicationNotes']
|
|
233
|
+
&& Array.isArray(parsedExifTags['ApplicationNotes'].value)
|
|
233
234
|
&& !hasXmpData(xmpChunks)
|
|
234
235
|
&& tagFilter.shouldParseGroup('xmp')
|
|
235
236
|
) {
|
|
@@ -259,6 +260,7 @@ export function loadView(
|
|
|
259
260
|
Constants.USE_PHOTOSHOP
|
|
260
261
|
&& parsedExifTags['ImageSourceData']
|
|
261
262
|
&& parsedExifTags['PhotoshopSettings']
|
|
263
|
+
&& Array.isArray(parsedExifTags['PhotoshopSettings'].value)
|
|
262
264
|
&& tagFilter.shouldParseGroup('photoshop')
|
|
263
265
|
) {
|
|
264
266
|
const readPhotoshopTags = PhotoshopTags.read(
|
|
@@ -412,11 +414,7 @@ export function loadView(
|
|
|
412
414
|
&& tagFilter.shouldParseGroup('exif')
|
|
413
415
|
&& async
|
|
414
416
|
) {
|
|
415
|
-
const compressedExifData =
|
|
416
|
-
dataView.buffer,
|
|
417
|
-
dataView.byteOffset + brobExifChunk.dataOffset,
|
|
418
|
-
brobExifChunk.length
|
|
419
|
-
);
|
|
417
|
+
const compressedExifData = getBrobDataView(dataView, brobExifChunk);
|
|
420
418
|
deferredPromises.push(
|
|
421
419
|
decompress(compressedExifData, COMPRESSION_METHOD_BROTLI, undefined, 'dataview', decompressConfig)
|
|
422
420
|
.then((decompressedDataView) => {
|
|
@@ -451,11 +449,7 @@ export function loadView(
|
|
|
451
449
|
&& tagFilter.shouldParseGroup('xmp')
|
|
452
450
|
&& async
|
|
453
451
|
) {
|
|
454
|
-
const compressedXmpData =
|
|
455
|
-
dataView.buffer,
|
|
456
|
-
dataView.byteOffset + brobXmpChunk.dataOffset,
|
|
457
|
-
brobXmpChunk.length
|
|
458
|
-
);
|
|
452
|
+
const compressedXmpData = getBrobDataView(dataView, brobXmpChunk);
|
|
459
453
|
deferredPromises.push(
|
|
460
454
|
decompress(compressedXmpData, COMPRESSION_METHOD_BROTLI, undefined, 'dataview', decompressConfig)
|
|
461
455
|
.then((decompressedDataView) => {
|
|
@@ -476,8 +470,7 @@ export function loadView(
|
|
|
476
470
|
}
|
|
477
471
|
|
|
478
472
|
if (
|
|
479
|
-
|
|
480
|
-
&& Constants.USE_ICC
|
|
473
|
+
Constants.USE_ICC
|
|
481
474
|
&& hasIccData(iccChunks)
|
|
482
475
|
&& tagFilter.shouldParseGroup('icc')
|
|
483
476
|
) {
|
|
@@ -735,6 +728,14 @@ export function loadView(
|
|
|
735
728
|
}
|
|
736
729
|
}
|
|
737
730
|
|
|
731
|
+
function getBrobDataView(dataView, brobChunk) {
|
|
732
|
+
// The box length is untrusted and comes through negative for a box shorter
|
|
733
|
+
// than its header, so bound the view by the bytes present. The JXL header
|
|
734
|
+
// parser never reports a dataOffset past the end of the view.
|
|
735
|
+
const length = Math.max(0, Math.min(brobChunk.length, dataView.byteLength - brobChunk.dataOffset));
|
|
736
|
+
return new DataView(dataView.buffer, dataView.byteOffset + brobChunk.dataOffset, length);
|
|
737
|
+
}
|
|
738
|
+
|
|
738
739
|
function readExifTagsSafely(dataView, tiffHeaderOffset, includeUnknown, computed, tagFilter) {
|
|
739
740
|
try {
|
|
740
741
|
return Tags.read(dataView, tiffHeaderOffset, includeUnknown, computed, tagFilter);
|
|
@@ -839,8 +840,7 @@ function hasPotentialMetaData({
|
|
|
839
840
|
&& hasXmpData(xmpChunks)
|
|
840
841
|
)
|
|
841
842
|
|| (
|
|
842
|
-
|
|
843
|
-
&& Constants.USE_ICC
|
|
843
|
+
Constants.USE_ICC
|
|
844
844
|
&& hasIccData(iccChunks)
|
|
845
845
|
)
|
|
846
846
|
|| (
|
|
@@ -936,7 +936,8 @@ function hasCanonData(tags) {
|
|
|
936
936
|
|
|
937
937
|
function hasPentaxType1Data(tags) {
|
|
938
938
|
const PENTAX_ID_STRING = 'PENTAX ';
|
|
939
|
-
return tags['MakerNote'].value
|
|
939
|
+
return Array.isArray(tags['MakerNote'].value)
|
|
940
|
+
&& tags['MakerNote'].value.length > PENTAX_ID_STRING.length
|
|
940
941
|
&& getStringValueFromArray(tags['MakerNote'].value.slice(0, PENTAX_ID_STRING.length)) === PENTAX_ID_STRING
|
|
941
942
|
&& tags['MakerNote'].__offset;
|
|
942
943
|
}
|
package/src/file-tags.js
CHANGED
|
@@ -21,7 +21,8 @@ function read(dataView, fileDataOffset) {
|
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
function getLength(dataView, fileDataOffset) {
|
|
24
|
-
|
|
24
|
+
// The declared length is untrusted, so bound it by the bytes present.
|
|
25
|
+
return Math.min(Types.getShortAt(dataView, fileDataOffset), dataView.byteLength - fileDataOffset);
|
|
25
26
|
}
|
|
26
27
|
|
|
27
28
|
function getDataPrecision(dataView, fileDataOffset, length) {
|
package/src/image-header-jpeg.js
CHANGED
|
@@ -247,7 +247,10 @@ function isSOF2Marker(dataView, appMarkerPosition) {
|
|
|
247
247
|
function isApp2ICCMarker(dataView, appMarkerPosition) {
|
|
248
248
|
const markerIdLength = APP2_ICC_IDENTIFIER.length;
|
|
249
249
|
|
|
250
|
+
// The ICC branch reads the two chunk bytes that follow the identifier, so a
|
|
251
|
+
// segment without them must not be recognized here.
|
|
250
252
|
return (dataView.getUint16(appMarkerPosition) === APP2_MARKER)
|
|
253
|
+
&& (appMarkerPosition + ICC_TOTAL_CHUNKS_OFFSET < dataView.byteLength)
|
|
251
254
|
&& (getStringFromDataView(dataView, appMarkerPosition + APP_ID_OFFSET, markerIdLength) === APP2_ICC_IDENTIFIER);
|
|
252
255
|
}
|
|
253
256
|
|
|
@@ -314,7 +317,10 @@ function isApp13PhotoshopMarker(dataView, appMarkerPosition) {
|
|
|
314
317
|
const resourceIdentifierOffset = appMarkerPosition + APP_ID_OFFSET
|
|
315
318
|
+ markerIdLength + 1;
|
|
316
319
|
|
|
317
|
-
|
|
320
|
+
// The null byte and the 8BIM identifier lie past the bytes the marker walk
|
|
321
|
+
// guarantees, so a segment without them must not be recognized here.
|
|
322
|
+
return (resourceIdentifierOffset + resourceIdentifierLength <= dataView.byteLength)
|
|
323
|
+
&& (dataView.getUint16(appMarkerPosition) === APP13_MARKER)
|
|
318
324
|
&& (getStringFromDataView(dataView, appMarkerPosition + APP_ID_OFFSET, markerIdLength) === APP13_IPTC_IDENTIFIER)
|
|
319
325
|
&& (dataView.getUint8(appMarkerPosition + APP_ID_OFFSET + markerIdLength) === 0x00)
|
|
320
326
|
&& (getStringFromDataView(dataView, resourceIdentifierOffset, resourceIdentifierLength) === APP13_RESOURCE_BLOCK_IDENTIFIER);
|
package/src/jfif-tags.js
CHANGED
|
@@ -38,7 +38,8 @@ function read(dataView, jfifDataOffset) {
|
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
function getLength(dataView, jfifDataOffset) {
|
|
41
|
-
|
|
41
|
+
// The declared length is untrusted, so bound it by the bytes present.
|
|
42
|
+
return Math.min(Types.getShortAt(dataView, jfifDataOffset), dataView.byteLength - jfifDataOffset);
|
|
42
43
|
}
|
|
43
44
|
|
|
44
45
|
function getVersion(dataView, jfifDataOffset, length) {
|
package/src/photoshop-tags.js
CHANGED
|
@@ -22,25 +22,28 @@ const TAG_NAME_MIN_SIZE = 2;
|
|
|
22
22
|
const RESOURCE_BLOCK_MIN_HEADER_SIZE = SIGNATURE_SIZE + TAG_ID_SIZE + TAG_NAME_MIN_SIZE + RESOURCE_LENGTH_SIZE;
|
|
23
23
|
|
|
24
24
|
function read(bytes, includeUnknown, tagFilter = NOOP_TAG_FILTER) {
|
|
25
|
+
if (!Array.isArray(bytes)) {
|
|
26
|
+
return {};
|
|
27
|
+
}
|
|
25
28
|
const dataView = getDataView(new Uint8Array(bytes).buffer);
|
|
26
29
|
const tags = {};
|
|
27
30
|
let offset = 0;
|
|
28
31
|
|
|
29
|
-
while (offset + RESOURCE_BLOCK_MIN_HEADER_SIZE <=
|
|
32
|
+
while (offset + RESOURCE_BLOCK_MIN_HEADER_SIZE <= dataView.byteLength) {
|
|
30
33
|
const signature = getStringFromDataView(dataView, offset, SIGNATURE_SIZE);
|
|
31
34
|
offset += SIGNATURE_SIZE;
|
|
32
35
|
const tagId = Types.getShortAt(dataView, offset);
|
|
33
36
|
offset += TAG_ID_SIZE;
|
|
34
37
|
const {tagName, tagNameSize} = getTagName(dataView, offset);
|
|
35
38
|
offset += tagNameSize;
|
|
36
|
-
if (offset + RESOURCE_LENGTH_SIZE >
|
|
39
|
+
if (offset + RESOURCE_LENGTH_SIZE > dataView.byteLength) {
|
|
37
40
|
break;
|
|
38
41
|
}
|
|
39
42
|
const declaredResourceSize = Types.getLongAt(dataView, offset);
|
|
40
43
|
offset += RESOURCE_LENGTH_SIZE;
|
|
41
44
|
// The declared size comes from the file and is not trusted, so bound
|
|
42
45
|
// it by the bytes that are actually present.
|
|
43
|
-
const resourceSize = Math.min(declaredResourceSize,
|
|
46
|
+
const resourceSize = Math.min(declaredResourceSize, dataView.byteLength - offset);
|
|
44
47
|
if (signature === SIGNATURE) {
|
|
45
48
|
const resolvedTagName = getTagNameForFiltering(tagId, tagName, includeUnknown);
|
|
46
49
|
if (!tagFilter.shouldParseTag('photoshop', resolvedTagName, tagId)) {
|
package/src/png-text-tags.js
CHANGED
|
@@ -147,7 +147,7 @@ function getNameAndValue(dataView, offset, length, type, async, decompressConfig
|
|
|
147
147
|
|
|
148
148
|
function getCompressionMethod({type, dataView, offset}) {
|
|
149
149
|
if (type === TYPE_ITXT) {
|
|
150
|
-
if (dataView.getUint8(offset) === COMPRESSION_FLAG_COMPRESSED) {
|
|
150
|
+
if (offset + 1 < dataView.byteLength && dataView.getUint8(offset) === COMPRESSION_FLAG_COMPRESSED) {
|
|
151
151
|
return dataView.getUint8(offset + 1);
|
|
152
152
|
}
|
|
153
153
|
} else if (type === TYPE_ZTXT) {
|
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]
|
|
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, {
|
package/src/xmp-namespaces.js
CHANGED
|
@@ -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
|
|
25
|
-
if (
|
|
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
|
|
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*["'][^"']
|
|
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
|
-
|
|
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
|
|
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
|
|
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
|
-
|
|
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
|
|
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
|
|
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
|
|
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
|
-
|
|
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) {
|