exifreader 4.45.2 → 4.47.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/src/xmp-tags.js CHANGED
@@ -2,15 +2,35 @@
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, setProperty} from './utils.js';
5
+ import {decodeUtf8ByteString, getStringFromDataView, objectAssign, setProperty, tryDecodeUtf8ByteString} from './utils.js';
6
6
  import XmpTagNames from './xmp-tag-names.js';
7
7
  import DOMParser from './dom-parser.js';
8
+ import TextDecoder from './text-decoder.js';
8
9
  import {isMissingNamespaceError, addMissingNamespaces} from './xmp-namespaces.js';
9
10
 
10
11
  export default {
11
12
  read
12
13
  };
13
14
 
15
+ // Node names differ between parsers (#cdata-section vs #cdatasection), so
16
+ // nodes are told apart by their DOM nodeType.
17
+ const ELEMENT_NODE = 1;
18
+ const TEXT_NODE = 3;
19
+ const CDATA_SECTION_NODE = 4;
20
+ const PROCESSING_INSTRUCTION_NODE = 7;
21
+ const COMMENT_NODE = 8;
22
+
23
+ const PACKET_TRAILER_START = '<?xpacket end="';
24
+ const PACKET_TRAILER_END = '"?>';
25
+
26
+ // Each nested value repeats the descriptions of everything below it, so the
27
+ // description text grows with the square of the nesting depth.
28
+ const MAX_NESTING_DEPTH = 32;
29
+
30
+ // Parsing is synchronous and oneLevelDeeper always restores the counter, so
31
+ // one counter serves every read.
32
+ let nestingDepth = 0;
33
+
14
34
  class ParseError extends Error {
15
35
  constructor(message) {
16
36
  super(message);
@@ -90,11 +110,13 @@ function getBoundedChunkBytes(dataView, chunk) {
90
110
 
91
111
  function readTags(tags, chunkDataView, domParser) {
92
112
  try {
93
- const {doc, raw} = getDocument(chunkDataView, domParser);
113
+ const {doc, raw, decodeValue} = getDocument(chunkDataView, domParser);
94
114
  tags._raw = (tags._raw || '') + raw;
95
115
  const rdf = getRDF(doc);
96
116
 
97
- objectAssign(tags, parseXMPObject(convertToObject(rdf, true)));
117
+ const xmpTags = {};
118
+ parseXMPObject(convertToObject(rdf, true, decodeValue), xmpTags, Object.create(null));
119
+ objectAssign(tags, xmpTags);
98
120
  return true;
99
121
  } catch (error) {
100
122
  return false;
@@ -108,17 +130,75 @@ function getDocument(chunkDataView, _domParser) {
108
130
  throw new Error();
109
131
  }
110
132
 
111
- const xmlString = typeof chunkDataView === 'string' ? chunkDataView : getStringFromDataView(chunkDataView, 0, chunkDataView.byteLength);
133
+ const {xmlString, decodeValue} = decodeXmlSource(chunkDataView);
112
134
  const doc = parseFromString(domParser, trimXmlSource(xmlString));
113
135
 
114
136
  return {
115
137
  doc,
116
138
  raw: xmlString,
139
+ decodeValue
117
140
  };
118
141
  }
119
142
 
143
+ // The packet has to be decoded before it is parsed. When it is parsed one
144
+ // character per byte, the UTF-8 encoding of e.g. Å (0xC3 0x85) contains a lone
145
+ // U+0085, which xmldom turns into a line feed before parsing (an XML 1.1 line
146
+ // ending). That corrupts element text, and attribute value normalization then
147
+ // makes it a space. XMP is UTF-8 by specification, but some writers store a
148
+ // single-byte encoding. Such a packet is parsed one character per byte and
149
+ // each value is decoded on its own afterwards where it is valid UTF-8.
150
+ function decodeXmlSource(source) {
151
+ if (typeof source === 'string') {
152
+ return decodeByteString(source);
153
+ }
154
+ const Decoder = TextDecoder.get();
155
+ if (Decoder !== undefined) {
156
+ try {
157
+ return {
158
+ xmlString: new Decoder('utf-8', {fatal: true}).decode(source),
159
+ decodeValue: keepValue
160
+ };
161
+ } catch (error) {
162
+ // Not valid UTF-8.
163
+ }
164
+ }
165
+ return decodeByteString(getStringFromDataView(source, 0, source.byteLength));
166
+ }
167
+
168
+ function decodeByteString(byteString) {
169
+ const decoded = tryDecodeUtf8ByteString(byteString);
170
+ if (decoded !== undefined) {
171
+ return {xmlString: decoded, decodeValue: keepValue};
172
+ }
173
+ return {xmlString: byteString, decodeValue: decodeUtf8ByteString};
174
+ }
175
+
176
+ function keepValue(value) {
177
+ return value;
178
+ }
179
+
120
180
  function trimXmlSource(xmlSource) {
121
- return xmlSource.replace(/^.+(<\?xpacket begin)/, '$1').replace(/(<\?xpacket end=".*"\?>).+$/, '$1');
181
+ return trimAfterPacketTrailer(xmlSource.replace(/^.+(<\?xpacket begin)/, '$1'));
182
+ }
183
+
184
+ // Gives the same result as the former /(<\?xpacket end=".*"\?>).+$/ in linear
185
+ // time: only text after a trailer on the last line is cut off.
186
+ function trimAfterPacketTrailer(xmlSource) {
187
+ const lastLineStart = Math.max(
188
+ xmlSource.lastIndexOf('\n'),
189
+ xmlSource.lastIndexOf('\r'),
190
+ xmlSource.lastIndexOf('\u2028'),
191
+ xmlSource.lastIndexOf('\u2029')
192
+ ) + 1;
193
+ const trailerStart = xmlSource.indexOf(PACKET_TRAILER_START, lastLineStart);
194
+ if (trailerStart === -1) {
195
+ return xmlSource;
196
+ }
197
+ const trailerEnd = xmlSource.lastIndexOf(PACKET_TRAILER_END, xmlSource.length - PACKET_TRAILER_END.length - 1);
198
+ if (trailerEnd < trailerStart + PACKET_TRAILER_START.length) {
199
+ return xmlSource;
200
+ }
201
+ return xmlSource.slice(0, trailerEnd + PACKET_TRAILER_END.length);
122
202
  }
123
203
 
124
204
  function parseFromString(domParser, xmlString, isRetry = false) {
@@ -139,58 +219,73 @@ function parseFromString(domParser, xmlString, isRetry = false) {
139
219
  }
140
220
 
141
221
  function getRDF(node) {
142
- for (let i = 0; i < node.childNodes.length; i++) {
143
- if (node.childNodes[i].tagName === 'x:xmpmeta') {
144
- return getRDF(node.childNodes[i]);
222
+ const childNodes = node.childNodes;
223
+
224
+ for (let i = 0; i < childNodes.length; i++) {
225
+ if (childNodes[i].tagName === 'x:xmpmeta') {
226
+ return getRDF(childNodes[i]);
145
227
  }
146
- if (node.childNodes[i].tagName === 'rdf:RDF') {
147
- return node.childNodes[i];
228
+ if (childNodes[i].tagName === 'rdf:RDF') {
229
+ return childNodes[i];
148
230
  }
149
231
  }
150
232
 
151
233
  throw new Error();
152
234
  }
153
235
 
154
- function convertToObject(node, isTopNode = false) {
236
+ function convertToObject(node, isTopNode, decodeValue) {
155
237
  const childNodes = getChildNodes(node);
156
238
 
157
239
  if (hasTextOnlyContent(childNodes)) {
158
240
  if (isTopNode) {
159
241
  return {};
160
242
  }
161
- return getTextValue(childNodes[0]);
243
+ return decodeValue(getTextValue(childNodes));
162
244
  }
163
245
 
164
- return getElementsFromNodes(childNodes);
246
+ return getElementsFromNodes(childNodes, decodeValue);
165
247
  }
166
248
 
167
249
  function getChildNodes(node) {
168
- const elements = [];
250
+ const childNodes = node.childNodes;
251
+ const nodes = [];
169
252
 
170
- for (let i = 0; i < node.childNodes.length; i++) {
171
- elements.push(node.childNodes[i]);
253
+ for (let i = 0; i < childNodes.length; i++) {
254
+ if (!isCommentOrProcessingInstruction(childNodes[i])) {
255
+ nodes.push(childNodes[i]);
256
+ }
172
257
  }
173
258
 
174
- return elements;
259
+ return nodes;
260
+ }
261
+
262
+ function isCommentOrProcessingInstruction(node) {
263
+ return node.nodeType === COMMENT_NODE || node.nodeType === PROCESSING_INSTRUCTION_NODE;
175
264
  }
176
265
 
177
266
  function hasTextOnlyContent(nodes) {
178
- return (nodes.length === 1) && (nodes[0].nodeName === '#text');
267
+ return nodes.length > 0 && nodes.every(isText);
268
+ }
269
+
270
+ function isText(node) {
271
+ return node.nodeType === TEXT_NODE || node.nodeType === CDATA_SECTION_NODE;
179
272
  }
180
273
 
181
- function getTextValue(node) {
182
- return node.nodeValue;
274
+ // linkedom splits text at every entity or character reference, and a CDATA
275
+ // section is a node of its own, so the text of an element can span several nodes.
276
+ function getTextValue(nodes) {
277
+ return nodes.map((node) => node.nodeValue).join('');
183
278
  }
184
279
 
185
280
  // The elements object must not have a prototype. The element names come from
186
281
  // the image, so an element named e.g. constructor would be found among the
187
282
  // inherited properties, and one named __proto__ would replace the prototype.
188
- function getElementsFromNodes(nodes) {
283
+ function getElementsFromNodes(nodes, decodeValue) {
189
284
  const elements = Object.create(null);
190
285
 
191
286
  nodes.forEach((node) => {
192
287
  if (isElement(node)) {
193
- const nodeElement = getElementFromNode(node);
288
+ const nodeElement = getElementFromNode(node, decodeValue);
194
289
 
195
290
  if (elements[node.nodeName] !== undefined) {
196
291
  if (!Array.isArray(elements[node.nodeName])) {
@@ -207,33 +302,28 @@ function getElementsFromNodes(nodes) {
207
302
  }
208
303
 
209
304
  function isElement(node) {
210
- return (node.nodeName) && (node.nodeName !== '#text');
305
+ return node.nodeType === ELEMENT_NODE;
211
306
  }
212
307
 
213
- function getElementFromNode(node) {
308
+ function getElementFromNode(node, decodeValue) {
214
309
  return {
215
- attributes: getAttributes(node),
216
- value: convertToObject(node)
310
+ attributes: getAttributes(node, decodeValue),
311
+ value: convertToObject(node, false, decodeValue)
217
312
  };
218
313
  }
219
314
 
220
- function getAttributes(element) {
315
+ function getAttributes(element, decodeValue) {
316
+ const elementAttributes = element.attributes;
221
317
  const attributes = {};
222
318
 
223
- for (let i = 0; i < element.attributes.length; i++) {
224
- setProperty(attributes, element.attributes[i].nodeName, decodeURIComponent(escape(element.attributes[i].value)));
319
+ for (let i = 0; i < elementAttributes.length; i++) {
320
+ setProperty(attributes, elementAttributes[i].nodeName, decodeValue(elementAttributes[i].value));
225
321
  }
226
322
 
227
323
  return attributes;
228
324
  }
229
325
 
230
- function parseXMPObject(xmpObject) {
231
- const tags = {};
232
-
233
- if (typeof xmpObject === 'string') {
234
- return xmpObject;
235
- }
236
-
326
+ function parseXMPObject(xmpObject, tags, plainDescriptions) {
237
327
  for (const nodeName in xmpObject) {
238
328
  let nodes = xmpObject[nodeName];
239
329
 
@@ -242,34 +332,24 @@ function parseXMPObject(xmpObject) {
242
332
  }
243
333
 
244
334
  nodes.forEach((node) => {
245
- objectAssign(tags, parseNodeAttributesAsTags(node.attributes));
335
+ parseNodeAttributesAsTags(node.attributes, tags, plainDescriptions);
246
336
  if (typeof node.value === 'object') {
247
- objectAssign(tags, parseNodeChildrenAsTags(node.value));
337
+ parseNodeChildrenAsTags(node.value, tags, plainDescriptions);
248
338
  }
249
339
  });
250
340
  }
251
-
252
- return tags;
253
341
  }
254
342
 
255
- function parseNodeAttributesAsTags(attributes) {
256
- const tags = {};
257
-
343
+ function parseNodeAttributesAsTags(attributes, tags, plainDescriptions) {
258
344
  for (const name in attributes) {
259
345
  try {
260
346
  if (isTagAttribute(name)) {
261
- setProperty(tags, getLocalName(name), {
262
- value: attributes[name],
263
- attributes: {},
264
- description: getDescription(attributes[name], name)
265
- });
347
+ setParsedTag(tags, plainDescriptions, getLocalName(name), createParsedTag(attributes[name], attributes[name], {}, name));
266
348
  }
267
349
  } catch (error) {
268
350
  // Keep going and try to parse the rest of the tags.
269
351
  }
270
352
  }
271
-
272
- return tags;
273
353
  }
274
354
 
275
355
  function isTagAttribute(name) {
@@ -280,6 +360,11 @@ function isNamespaceDefinition(name) {
280
360
  return name.split(':')[0] === 'xmlns';
281
361
  }
282
362
 
363
+ function setParsedTag(tags, plainDescriptions, name, parsedTag) {
364
+ setProperty(tags, name, parsedTag.tag);
365
+ setProperty(plainDescriptions, name, parsedTag.plainDescription);
366
+ }
367
+
283
368
  function getLocalName(name) {
284
369
  if (/^MicrosoftPhoto(_\d+_)?:Rating$/i.test(name)) {
285
370
  return 'RatingPercent';
@@ -287,103 +372,80 @@ function getLocalName(name) {
287
372
  return name.split(':')[1];
288
373
  }
289
374
 
290
- function getDescription(value, name = undefined) {
375
+ // A parent describes its members without their description functions, so
376
+ // each parsed tag carries that plain description next to the tag itself.
377
+ function createParsedTag(value, plainDescription, attributes, name) {
378
+ return {
379
+ tag: {
380
+ value,
381
+ attributes,
382
+ description: getDescription(value, plainDescription, name)
383
+ },
384
+ plainDescription
385
+ };
386
+ }
387
+
388
+ function getDescription(value, plainDescription, name) {
389
+ if (!hasTagNameFunction(name) || isObject(value)) {
390
+ return plainDescription;
391
+ }
291
392
  if (Array.isArray(value)) {
292
- const arrayDescription = getDescriptionOfArray(value);
293
- if (hasTagNameFunction(name)) {
294
- // A description is a string, so a description function written for
295
- // a plain value is ignored when it throws on a list or returns
296
- // something other than a string.
297
- try {
298
- const description = XmpTagNames[name](value, arrayDescription);
299
- if (typeof description === 'string') {
300
- return description;
301
- }
302
- } catch (error) {
303
- // Fall back to the descriptions of the items.
393
+ // A description is a string, so a description function written for
394
+ // a plain value is ignored when it throws on a list or returns
395
+ // something other than a string.
396
+ try {
397
+ const description = XmpTagNames[name](value, plainDescription);
398
+ if (typeof description === 'string') {
399
+ return description;
304
400
  }
401
+ } catch (error) {
402
+ // Fall back to the descriptions of the items.
305
403
  }
306
- return arrayDescription;
307
- }
308
- if (typeof value === 'object') {
309
- return getDescriptionOfObject(value);
404
+ return plainDescription;
310
405
  }
311
-
312
406
  try {
313
- if (hasTagNameFunction(name)) {
314
- return XmpTagNames[name](value);
315
- }
316
- return decodeURIComponent(escape(value));
407
+ return XmpTagNames[name](value);
317
408
  } catch (error) {
318
409
  return value;
319
410
  }
320
411
  }
321
412
 
322
- function getDescriptionOfArray(value) {
323
- return value.map((item) => {
324
- if (item.value !== undefined) {
325
- return getDescription(item.value);
326
- }
327
- return getDescription(item);
328
- }).join(', ');
329
- }
330
-
331
413
  // The name comes from the image, so an inherited property of the tag name table
332
414
  // must not be mistaken for a description function.
333
415
  function hasTagNameFunction(name) {
334
416
  return Object.prototype.hasOwnProperty.call(XmpTagNames, name) && (typeof XmpTagNames[name] === 'function');
335
417
  }
336
418
 
337
- function getDescriptionOfObject(value) {
338
- const descriptions = [];
339
-
340
- for (const key in value) {
341
- descriptions.push(`${getClearTextKey(key)}: ${getDescription(value[key].value)}`);
342
- }
343
-
344
- return descriptions.join('; ');
419
+ function isObject(value) {
420
+ return (typeof value === 'object') && !Array.isArray(value);
345
421
  }
346
422
 
347
- function getClearTextKey(key) {
348
- if (key === 'CiAdrCity') {
349
- return 'CreatorCity';
350
- }
351
- if (key === 'CiAdrCtry') {
352
- return 'CreatorCountry';
353
- }
354
- if (key === 'CiAdrExtadr') {
355
- return 'CreatorAddress';
356
- }
357
- if (key === 'CiAdrPcode') {
358
- return 'CreatorPostalCode';
359
- }
360
- if (key === 'CiAdrRegion') {
361
- return 'CreatorRegion';
362
- }
363
- if (key === 'CiEmailWork') {
364
- return 'CreatorWorkEmail';
365
- }
366
- if (key === 'CiTelWork') {
367
- return 'CreatorWorkPhone';
368
- }
369
- if (key === 'CiUrlWork') {
370
- return 'CreatorWorkUrl';
371
- }
372
- return key;
423
+ function parseNodeChildrenAsTags(children, tags, plainDescriptions) {
424
+ oneLevelDeeper(() => {
425
+ for (const name in children) {
426
+ try {
427
+ if (!isNamespaceDefinition(name)) {
428
+ setParsedTag(tags, plainDescriptions, getLocalName(name), parseNodeAsTag(children[name], name));
429
+ }
430
+ } catch (error) {
431
+ // Keep going and try to parse the rest of the tags.
432
+ }
433
+ }
434
+ });
373
435
  }
374
436
 
375
- function parseNodeChildrenAsTags(children, tags = {}) {
376
- for (const name in children) {
377
- try {
378
- if (!isNamespaceDefinition(name)) {
379
- setProperty(tags, getLocalName(name), parseNodeAsTag(children[name], name));
380
- }
381
- } catch (error) {
382
- // Keep going and try to parse the rest of the tags.
437
+ function oneLevelDeeper(callback) {
438
+ nestingDepth++;
439
+ try {
440
+ if (nestingDepth > MAX_NESTING_DEPTH) {
441
+ // Lands in the catch of the nearest parseNodeChildrenAsTags, which
442
+ // drops the tag.
443
+ throw new ParseError(`XMP value nested deeper than ${MAX_NESTING_DEPTH} levels.`);
383
444
  }
445
+ return callback();
446
+ } finally {
447
+ nestingDepth--;
384
448
  }
385
-
386
- return tags;
387
449
  }
388
450
 
389
451
  function parseNodeAsTag(node, name) {
@@ -391,7 +453,7 @@ function parseNodeAsTag(node, name) {
391
453
  return parseNodeAsDuplicateTag(node, name);
392
454
  }
393
455
  if (isEmptyResourceTag(node)) {
394
- return {value: '', attributes: {}, description: ''};
456
+ return {tag: {value: '', attributes: {}, description: ''}, plainDescription: ''};
395
457
  }
396
458
  if (hasNestedSimpleRdfDescription(node)) {
397
459
  return parseNodeAsSimpleRdfDescription(node, name);
@@ -436,13 +498,9 @@ function parseNodeAsSimpleRdfDescription(node, name) {
436
498
 
437
499
  objectAssign(attributes, parseNodeAttributes(node), parseNodeChildrenAsAttributes(node));
438
500
 
439
- const value = parseRdfValue(node);
501
+ const {value, plainDescription} = parseRdfValue(node);
440
502
 
441
- return {
442
- value,
443
- attributes,
444
- description: getDescription(value, name)
445
- };
503
+ return createParsedTag(value, plainDescription, attributes, name);
446
504
  }
447
505
 
448
506
  function parseNodeAttributes(node) {
@@ -460,6 +518,10 @@ function parseNodeAttributes(node) {
460
518
  function parseNodeChildrenAsAttributes(node) {
461
519
  const attributes = {};
462
520
 
521
+ if (typeof node.value !== 'object') {
522
+ return attributes;
523
+ }
524
+
463
525
  for (const name in node.value) {
464
526
  if ((name !== 'rdf:value') && (!isNamespaceDefinition(name))) {
465
527
  setProperty(attributes, getLocalName(name), node.value[name].value);
@@ -471,7 +533,11 @@ function parseNodeChildrenAsAttributes(node) {
471
533
 
472
534
  function parseRdfValue(node) {
473
535
  const rdfValueNode = getLastNode(node.value['rdf:value']);
474
- return getURIValue(rdfValueNode) || parseRdfValueContent(rdfValueNode);
536
+ const uri = getURIValue(rdfValueNode);
537
+ if (uri) {
538
+ return {value: uri, plainDescription: uri};
539
+ }
540
+ return parseRdfValueContent(rdfValueNode);
475
541
  }
476
542
 
477
543
  // Repeated elements are collected into an array, and the last one wins, which
@@ -487,14 +553,56 @@ function getLastNode(node) {
487
553
  // the value of the tag rather than a tag of its own.
488
554
  function parseRdfValueContent(rdfValueNode) {
489
555
  if (isArray(rdfValueNode)) {
490
- return parseNodeAsArray(rdfValueNode).value;
556
+ // Lists nested in rdf:value elements never pass parseNodeChildrenAsTags, so each one is a level.
557
+ return oneLevelDeeper(() => parseArrayItems(rdfValueNode));
491
558
  }
492
559
  if (typeof rdfValueNode.value === 'object') {
493
560
  // The child names come from the image, so a child named __proto__ must
494
561
  // not be able to replace this object's prototype.
495
- return parseNodeChildrenAsTags(rdfValueNode.value, Object.create(null));
562
+ const tags = Object.create(null);
563
+ const plainDescriptions = Object.create(null);
564
+ parseNodeChildrenAsTags(rdfValueNode.value, tags, plainDescriptions);
565
+ return {value: tags, plainDescription: getDescriptionOfObject(tags, plainDescriptions)};
496
566
  }
497
- return rdfValueNode.value;
567
+ return {value: rdfValueNode.value, plainDescription: rdfValueNode.value};
568
+ }
569
+
570
+ function getDescriptionOfObject(tags, plainDescriptions) {
571
+ const descriptions = [];
572
+
573
+ for (const key in tags) {
574
+ descriptions.push(`${getClearTextKey(key)}: ${plainDescriptions[key]}`);
575
+ }
576
+
577
+ return descriptions.join('; ');
578
+ }
579
+
580
+ function getClearTextKey(key) {
581
+ if (key === 'CiAdrCity') {
582
+ return 'CreatorCity';
583
+ }
584
+ if (key === 'CiAdrCtry') {
585
+ return 'CreatorCountry';
586
+ }
587
+ if (key === 'CiAdrExtadr') {
588
+ return 'CreatorAddress';
589
+ }
590
+ if (key === 'CiAdrPcode') {
591
+ return 'CreatorPostalCode';
592
+ }
593
+ if (key === 'CiAdrRegion') {
594
+ return 'CreatorRegion';
595
+ }
596
+ if (key === 'CiEmailWork') {
597
+ return 'CreatorWorkEmail';
598
+ }
599
+ if (key === 'CiTelWork') {
600
+ return 'CreatorWorkPhone';
601
+ }
602
+ if (key === 'CiUrlWork') {
603
+ return 'CreatorWorkUrl';
604
+ }
605
+ return key;
498
606
  }
499
607
 
500
608
  function hasNestedStructureRdfDescription(node) {
@@ -503,38 +611,44 @@ function hasNestedStructureRdfDescription(node) {
503
611
  }
504
612
 
505
613
  function parseNodeAsStructureRdfDescription(node, name) {
506
- const tag = {
507
- value: {},
508
- attributes: {}
509
- };
614
+ const value = {};
615
+ const plainDescriptions = Object.create(null);
616
+ const attributes = {};
510
617
 
511
618
  if (node.value['rdf:Description'] !== undefined) {
512
- objectAssign(tag.value, parseNodeAttributesAsTags(node.value['rdf:Description'].attributes));
513
- objectAssign(tag.attributes, parseNodeAttributes(node));
619
+ parseNodeAttributesAsTags(node.value['rdf:Description'].attributes, value, plainDescriptions);
620
+ objectAssign(attributes, parseNodeAttributes(node));
514
621
  node = node.value['rdf:Description'];
515
622
  }
516
623
 
517
- objectAssign(tag.value, parseNodeChildrenAsTags(node.value));
518
-
519
- tag.description = getDescription(tag.value, name);
624
+ if (typeof node.value === 'object') {
625
+ parseNodeChildrenAsTags(node.value, value, plainDescriptions);
626
+ }
520
627
 
521
- return tag;
628
+ return createParsedTag(value, getDescriptionOfObject(value, plainDescriptions), attributes, name);
522
629
  }
523
630
 
524
631
  function isCompactStructure(node) {
525
- return (Object.keys(node.value).length === 0)
632
+ return isEmptyValue(node.value)
526
633
  && (node.attributes['xml:lang'] === undefined)
527
634
  && (node.attributes['rdf:resource'] === undefined);
528
635
  }
529
636
 
637
+ // A text-only element has a string value; enumerating it would visit every character.
638
+ function isEmptyValue(value) {
639
+ if (typeof value === 'string') {
640
+ return value === '';
641
+ }
642
+ return Object.keys(value).length === 0;
643
+ }
644
+
530
645
  function parseNodeAsCompactStructure(node, name) {
531
- const value = parseNodeAttributesAsTags(node.attributes);
646
+ const value = {};
647
+ const plainDescriptions = Object.create(null);
532
648
 
533
- return {
534
- value,
535
- attributes: {},
536
- description: getDescription(value, name)
537
- };
649
+ parseNodeAttributesAsTags(node.attributes, value, plainDescriptions);
650
+
651
+ return createParsedTag(value, getDescriptionOfObject(value, plainDescriptions), {}, name);
538
652
  }
539
653
 
540
654
  function isArray(node) {
@@ -546,9 +660,16 @@ function getArrayChild(value) {
546
660
  }
547
661
 
548
662
  function parseNodeAsArray(node, name) {
549
- let items = getArrayChild(node.value).value['rdf:li'];
550
663
  const attributes = parseNodeAttributes(node);
664
+ const {value, plainDescription} = parseArrayItems(node);
665
+
666
+ return createParsedTag(value, plainDescription, attributes, name);
667
+ }
668
+
669
+ function parseArrayItems(node) {
670
+ let items = getArrayChild(node.value).value['rdf:li'];
551
671
  const value = [];
672
+ const itemDescriptions = [];
552
673
 
553
674
  if (items === undefined) {
554
675
  items = [];
@@ -557,38 +678,53 @@ function parseNodeAsArray(node, name) {
557
678
  }
558
679
 
559
680
  items.forEach((item) => {
560
- value.push(parseArrayValue(item));
681
+ const parsedItem = parseArrayValue(item);
682
+ value.push(parsedItem.value);
683
+ itemDescriptions.push(parsedItem.plainDescription);
561
684
  });
562
685
 
563
- return {
564
- value,
565
- attributes,
566
- description: getDescription(value, name)
567
- };
686
+ return {value, plainDescription: itemDescriptions.join(', ')};
568
687
  }
569
688
 
570
689
  function parseArrayValue(item) {
571
690
  if (hasNestedSimpleRdfDescription(item)) {
572
- return parseNodeAsSimpleRdfDescription(item);
691
+ return getParsedTagAsItem(parseNodeAsSimpleRdfDescription(item));
573
692
  }
574
693
  if (hasNestedStructureRdfDescription(item)) {
575
- return parseNodeAsStructureRdfDescription(item).value;
694
+ return getParsedStructureAsItem(parseNodeAsStructureRdfDescription(item));
576
695
  }
577
696
  if (isCompactStructure(item)) {
578
- return parseNodeAsCompactStructure(item).value;
697
+ return getParsedStructureAsItem(parseNodeAsCompactStructure(item));
579
698
  }
580
699
 
581
- return parseNodeAsSimpleValue(item);
700
+ return getParsedTagAsItem(parseNodeAsSimpleValue(item));
701
+ }
702
+
703
+ function getParsedTagAsItem(parsedTag) {
704
+ return {value: parsedTag.tag, plainDescription: parsedTag.plainDescription};
705
+ }
706
+
707
+ function getParsedStructureAsItem(parsedStructure) {
708
+ return {value: parsedStructure.tag.value, plainDescription: parsedStructure.plainDescription};
582
709
  }
583
710
 
584
711
  function parseNodeAsSimpleValue(node, name) {
585
- const value = getURIValue(node) || parseXMPObject(node.value);
712
+ const {value, plainDescription} = parseSimpleValue(node);
713
+ return createParsedTag(value, plainDescription, parseNodeAttributes(node), name);
714
+ }
586
715
 
587
- return {
588
- value,
589
- attributes: parseNodeAttributes(node),
590
- description: getDescription(value, name)
591
- };
716
+ function parseSimpleValue(node) {
717
+ const uri = getURIValue(node);
718
+ if (uri) {
719
+ return {value: uri, plainDescription: uri};
720
+ }
721
+ if (typeof node.value === 'string') {
722
+ return {value: node.value, plainDescription: node.value};
723
+ }
724
+ const tags = {};
725
+ const plainDescriptions = Object.create(null);
726
+ parseXMPObject(node.value, tags, plainDescriptions);
727
+ return {value: tags, plainDescription: getDescriptionOfObject(tags, plainDescriptions)};
592
728
  }
593
729
 
594
730
  function getURIValue(node) {