exifreader 4.46.0 → 4.48.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,16 +2,45 @@
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 {decodeUtf8ByteString, getStringFromDataView, objectAssign, setProperty, tryDecodeUtf8ByteString} from './utils.js';
5
+ import {decodeUtf8ByteString, getByteString, getTagKey, objectAssign, setProperty, tryDecodeUtf8ByteString} from './utils.js';
6
6
  import XmpTagNames from './xmp-tag-names.js';
7
7
  import DOMParser from './dom-parser.js';
8
8
  import TextDecoder from './text-decoder.js';
9
9
  import {isMissingNamespaceError, addMissingNamespaces} from './xmp-namespaces.js';
10
+ import {exceedsMarkupBounds} from './xmp-element-depth.js';
10
11
 
11
12
  export default {
12
13
  read
13
14
  };
14
15
 
16
+ // Node names differ between parsers (#cdata-section vs #cdatasection), so
17
+ // nodes are told apart by their DOM nodeType.
18
+ const ELEMENT_NODE = 1;
19
+ const TEXT_NODE = 3;
20
+ const CDATA_SECTION_NODE = 4;
21
+ const PROCESSING_INSTRUCTION_NODE = 7;
22
+ const COMMENT_NODE = 8;
23
+
24
+ const PACKET_TRAILER_START = '<?xpacket end="';
25
+ const PACKET_TRAILER_END = '"?>';
26
+
27
+ // Each level repeats the descriptions of everything below it, so any part of
28
+ // the packet's text is copied into at most twice this many descriptions.
29
+ const MAX_NESTING_DEPTH = 16;
30
+
31
+ // xmldom 0.9.12 walks one link per enclosing declaring element on every prefix
32
+ // lookup, and the DOM conversion here recurses once per level.
33
+ const MAX_ELEMENT_DEPTH = 256;
34
+
35
+ // Each markup node takes about 2 KB of peak heap in xmldom 0.9.12. With short
36
+ // names and values, reading a packet within this bound into tags peaks below
37
+ // about 800 MB.
38
+ const MAX_MARKUP_NODES = 250000;
39
+
40
+ // Parsing is synchronous and oneLevelDeeper always restores the counter, so
41
+ // one counter serves every read.
42
+ let nestingDepth = 0;
43
+
15
44
  class ParseError extends Error {
16
45
  constructor(message) {
17
46
  super(message);
@@ -82,6 +111,8 @@ function combineChunks(dataView, chunks) {
82
111
  function getBoundedChunkBytes(dataView, chunk) {
83
112
  // The bound is the view's own extent, not the buffer's, since the slice
84
113
  // starts at the view's byteOffset.
114
+ // dataView can be a DataViewWrapper whose buffer is a Buffer, which a
115
+ // Uint8Array view constructor would copy whole, so the bytes come from slice.
85
116
  const bufferLength = dataView.byteLength;
86
117
  const byteOffset = dataView.byteOffset || 0;
87
118
  const start = Math.min(Math.max(chunk.dataOffset, 0), bufferLength);
@@ -95,7 +126,14 @@ function readTags(tags, chunkDataView, domParser) {
95
126
  tags._raw = (tags._raw || '') + raw;
96
127
  const rdf = getRDF(doc);
97
128
 
98
- objectAssign(tags, parseXMPObject(convertToObject(rdf, true, decodeValue)));
129
+ const xmpTags = {};
130
+ parseXMPObject(convertToObject(rdf, true, decodeValue), xmpTags, Object.create(null));
131
+ // A parsed tag named _raw must not replace the packet string.
132
+ delete xmpTags._raw;
133
+ const names = Object.keys(xmpTags);
134
+ for (let i = 0; i < names.length; i++) {
135
+ setProperty(tags, getTagKey(names[i]), xmpTags[names[i]]);
136
+ }
99
137
  return true;
100
138
  } catch (error) {
101
139
  return false;
@@ -141,7 +179,8 @@ function decodeXmlSource(source) {
141
179
  // Not valid UTF-8.
142
180
  }
143
181
  }
144
- return decodeByteString(getStringFromDataView(source, 0, source.byteLength));
182
+ const bytes = new Uint8Array(source.buffer, source.byteOffset, source.byteLength);
183
+ return decodeByteString(getByteString(bytes));
145
184
  }
146
185
 
147
186
  function decodeByteString(byteString) {
@@ -157,10 +196,33 @@ function keepValue(value) {
157
196
  }
158
197
 
159
198
  function trimXmlSource(xmlSource) {
160
- return xmlSource.replace(/^.+(<\?xpacket begin)/, '$1').replace(/(<\?xpacket end=".*"\?>).+$/, '$1');
199
+ return trimAfterPacketTrailer(xmlSource.replace(/^.+(<\?xpacket begin)/, '$1'));
200
+ }
201
+
202
+ // Gives the same result as the former /(<\?xpacket end=".*"\?>).+$/ in linear
203
+ // time: only text after a trailer on the last line is cut off.
204
+ function trimAfterPacketTrailer(xmlSource) {
205
+ const lastLineStart = Math.max(
206
+ xmlSource.lastIndexOf('\n'),
207
+ xmlSource.lastIndexOf('\r'),
208
+ xmlSource.lastIndexOf('\u2028'),
209
+ xmlSource.lastIndexOf('\u2029')
210
+ ) + 1;
211
+ const trailerStart = xmlSource.indexOf(PACKET_TRAILER_START, lastLineStart);
212
+ if (trailerStart === -1) {
213
+ return xmlSource;
214
+ }
215
+ const trailerEnd = xmlSource.lastIndexOf(PACKET_TRAILER_END, xmlSource.length - PACKET_TRAILER_END.length - 1);
216
+ if (trailerEnd < trailerStart + PACKET_TRAILER_START.length) {
217
+ return xmlSource;
218
+ }
219
+ return xmlSource.slice(0, trailerEnd + PACKET_TRAILER_END.length);
161
220
  }
162
221
 
163
222
  function parseFromString(domParser, xmlString, isRetry = false) {
223
+ if (exceedsMarkupBounds(xmlString, MAX_ELEMENT_DEPTH, MAX_MARKUP_NODES)) {
224
+ throw new ParseError(`XMP elements nested deeper than ${MAX_ELEMENT_DEPTH} levels or more than ${MAX_MARKUP_NODES} markup nodes.`);
225
+ }
164
226
  try {
165
227
  const doc = domParser.parseFromString(xmlString, 'application/xml');
166
228
  const errors = doc.getElementsByTagName('parsererror');
@@ -178,12 +240,14 @@ function parseFromString(domParser, xmlString, isRetry = false) {
178
240
  }
179
241
 
180
242
  function getRDF(node) {
181
- for (let i = 0; i < node.childNodes.length; i++) {
182
- if (node.childNodes[i].tagName === 'x:xmpmeta') {
183
- return getRDF(node.childNodes[i]);
243
+ const childNodes = node.childNodes;
244
+
245
+ for (let i = 0; i < childNodes.length; i++) {
246
+ if (childNodes[i].tagName === 'x:xmpmeta') {
247
+ return getRDF(childNodes[i]);
184
248
  }
185
- if (node.childNodes[i].tagName === 'rdf:RDF') {
186
- return node.childNodes[i];
249
+ if (childNodes[i].tagName === 'rdf:RDF') {
250
+ return childNodes[i];
187
251
  }
188
252
  }
189
253
 
@@ -197,28 +261,41 @@ function convertToObject(node, isTopNode, decodeValue) {
197
261
  if (isTopNode) {
198
262
  return {};
199
263
  }
200
- return decodeValue(getTextValue(childNodes[0]));
264
+ return decodeValue(getTextValue(childNodes));
201
265
  }
202
266
 
203
267
  return getElementsFromNodes(childNodes, decodeValue);
204
268
  }
205
269
 
206
270
  function getChildNodes(node) {
207
- const elements = [];
271
+ const childNodes = node.childNodes;
272
+ const nodes = [];
208
273
 
209
- for (let i = 0; i < node.childNodes.length; i++) {
210
- elements.push(node.childNodes[i]);
274
+ for (let i = 0; i < childNodes.length; i++) {
275
+ if (!isCommentOrProcessingInstruction(childNodes[i])) {
276
+ nodes.push(childNodes[i]);
277
+ }
211
278
  }
212
279
 
213
- return elements;
280
+ return nodes;
281
+ }
282
+
283
+ function isCommentOrProcessingInstruction(node) {
284
+ return node.nodeType === COMMENT_NODE || node.nodeType === PROCESSING_INSTRUCTION_NODE;
214
285
  }
215
286
 
216
287
  function hasTextOnlyContent(nodes) {
217
- return (nodes.length === 1) && (nodes[0].nodeName === '#text');
288
+ return nodes.length > 0 && nodes.every(isText);
289
+ }
290
+
291
+ function isText(node) {
292
+ return node.nodeType === TEXT_NODE || node.nodeType === CDATA_SECTION_NODE;
218
293
  }
219
294
 
220
- function getTextValue(node) {
221
- return node.nodeValue;
295
+ // linkedom splits text at every entity or character reference, and a CDATA
296
+ // section is a node of its own, so the text of an element can span several nodes.
297
+ function getTextValue(nodes) {
298
+ return nodes.map((node) => node.nodeValue).join('');
222
299
  }
223
300
 
224
301
  // The elements object must not have a prototype. The element names come from
@@ -246,7 +323,7 @@ function getElementsFromNodes(nodes, decodeValue) {
246
323
  }
247
324
 
248
325
  function isElement(node) {
249
- return (node.nodeName) && (node.nodeName !== '#text');
326
+ return node.nodeType === ELEMENT_NODE;
250
327
  }
251
328
 
252
329
  function getElementFromNode(node, decodeValue) {
@@ -257,22 +334,17 @@ function getElementFromNode(node, decodeValue) {
257
334
  }
258
335
 
259
336
  function getAttributes(element, decodeValue) {
337
+ const elementAttributes = element.attributes;
260
338
  const attributes = {};
261
339
 
262
- for (let i = 0; i < element.attributes.length; i++) {
263
- setProperty(attributes, element.attributes[i].nodeName, decodeValue(element.attributes[i].value));
340
+ for (let i = 0; i < elementAttributes.length; i++) {
341
+ setProperty(attributes, elementAttributes[i].nodeName, decodeValue(elementAttributes[i].value));
264
342
  }
265
343
 
266
344
  return attributes;
267
345
  }
268
346
 
269
- function parseXMPObject(xmpObject) {
270
- const tags = {};
271
-
272
- if (typeof xmpObject === 'string') {
273
- return xmpObject;
274
- }
275
-
347
+ function parseXMPObject(xmpObject, tags, plainDescriptions) {
276
348
  for (const nodeName in xmpObject) {
277
349
  let nodes = xmpObject[nodeName];
278
350
 
@@ -281,34 +353,24 @@ function parseXMPObject(xmpObject) {
281
353
  }
282
354
 
283
355
  nodes.forEach((node) => {
284
- objectAssign(tags, parseNodeAttributesAsTags(node.attributes));
356
+ parseNodeAttributesAsTags(node.attributes, tags, plainDescriptions);
285
357
  if (typeof node.value === 'object') {
286
- objectAssign(tags, parseNodeChildrenAsTags(node.value));
358
+ parseNodeChildrenAsTags(node.value, tags, plainDescriptions);
287
359
  }
288
360
  });
289
361
  }
290
-
291
- return tags;
292
362
  }
293
363
 
294
- function parseNodeAttributesAsTags(attributes) {
295
- const tags = {};
296
-
364
+ function parseNodeAttributesAsTags(attributes, tags, plainDescriptions) {
297
365
  for (const name in attributes) {
298
366
  try {
299
367
  if (isTagAttribute(name)) {
300
- setProperty(tags, getLocalName(name), {
301
- value: attributes[name],
302
- attributes: {},
303
- description: getDescription(attributes[name], name)
304
- });
368
+ setParsedTag(tags, plainDescriptions, getLocalName(name), createParsedTag(attributes[name], attributes[name], {}, name));
305
369
  }
306
370
  } catch (error) {
307
371
  // Keep going and try to parse the rest of the tags.
308
372
  }
309
373
  }
310
-
311
- return tags;
312
374
  }
313
375
 
314
376
  function isTagAttribute(name) {
@@ -319,52 +381,56 @@ function isNamespaceDefinition(name) {
319
381
  return name.split(':')[0] === 'xmlns';
320
382
  }
321
383
 
384
+ function setParsedTag(tags, plainDescriptions, name, parsedTag) {
385
+ setProperty(tags, name, parsedTag.tag);
386
+ setProperty(plainDescriptions, name, parsedTag.plainDescription);
387
+ }
388
+
322
389
  function getLocalName(name) {
323
390
  if (/^MicrosoftPhoto(_\d+_)?:Rating$/i.test(name)) {
324
391
  return 'RatingPercent';
325
392
  }
326
- return name.split(':')[1];
393
+ const localName = name.split(':')[1];
394
+ // No prefix (the name can be in a default namespace) or an empty local part: keep the whole name.
395
+ return localName || name;
327
396
  }
328
397
 
329
- function getDescription(value, name = undefined) {
330
- if (Array.isArray(value)) {
331
- const arrayDescription = getDescriptionOfArray(value);
332
- if (hasTagNameFunction(name)) {
333
- // A description is a string, so a description function written for
334
- // a plain value is ignored when it throws on a list or returns
335
- // something other than a string.
336
- try {
337
- const description = XmpTagNames[name](value, arrayDescription);
338
- if (typeof description === 'string') {
339
- return description;
340
- }
341
- } catch (error) {
342
- // Fall back to the descriptions of the items.
343
- }
344
- }
345
- return arrayDescription;
346
- }
347
- if (typeof value === 'object') {
348
- return getDescriptionOfObject(value);
349
- }
398
+ // A parent describes its members without their description functions, so
399
+ // each parsed tag carries that plain description next to the tag itself.
400
+ function createParsedTag(value, plainDescription, attributes, name) {
401
+ return {
402
+ tag: {
403
+ value,
404
+ attributes,
405
+ description: getDescription(value, plainDescription, name)
406
+ },
407
+ plainDescription
408
+ };
409
+ }
350
410
 
351
- if (hasTagNameFunction(name)) {
411
+ function getDescription(value, plainDescription, name) {
412
+ if (!hasTagNameFunction(name) || isObject(value)) {
413
+ return plainDescription;
414
+ }
415
+ if (Array.isArray(value)) {
416
+ // A description is a string, so a description function written for
417
+ // a plain value is ignored when it throws on a list or returns
418
+ // something other than a string.
352
419
  try {
353
- return XmpTagNames[name](value);
420
+ const description = XmpTagNames[name](value, plainDescription);
421
+ if (typeof description === 'string') {
422
+ return description;
423
+ }
354
424
  } catch (error) {
355
- return value;
425
+ // Fall back to the descriptions of the items.
356
426
  }
427
+ return plainDescription;
428
+ }
429
+ try {
430
+ return XmpTagNames[name](value);
431
+ } catch (error) {
432
+ return value;
357
433
  }
358
- return value;
359
- }
360
-
361
- function getDescriptionOfArray(value) {
362
- return value.map((item) => {
363
- if (item.value !== undefined) {
364
- return getDescription(item.value);
365
- }
366
- return getDescription(item);
367
- }).join(', ');
368
434
  }
369
435
 
370
436
  // The name comes from the image, so an inherited property of the tag name table
@@ -373,56 +439,36 @@ function hasTagNameFunction(name) {
373
439
  return Object.prototype.hasOwnProperty.call(XmpTagNames, name) && (typeof XmpTagNames[name] === 'function');
374
440
  }
375
441
 
376
- function getDescriptionOfObject(value) {
377
- const descriptions = [];
378
-
379
- for (const key in value) {
380
- descriptions.push(`${getClearTextKey(key)}: ${getDescription(value[key].value)}`);
381
- }
382
-
383
- return descriptions.join('; ');
442
+ function isObject(value) {
443
+ return (typeof value === 'object') && !Array.isArray(value);
384
444
  }
385
445
 
386
- function getClearTextKey(key) {
387
- if (key === 'CiAdrCity') {
388
- return 'CreatorCity';
389
- }
390
- if (key === 'CiAdrCtry') {
391
- return 'CreatorCountry';
392
- }
393
- if (key === 'CiAdrExtadr') {
394
- return 'CreatorAddress';
395
- }
396
- if (key === 'CiAdrPcode') {
397
- return 'CreatorPostalCode';
398
- }
399
- if (key === 'CiAdrRegion') {
400
- return 'CreatorRegion';
401
- }
402
- if (key === 'CiEmailWork') {
403
- return 'CreatorWorkEmail';
404
- }
405
- if (key === 'CiTelWork') {
406
- return 'CreatorWorkPhone';
407
- }
408
- if (key === 'CiUrlWork') {
409
- return 'CreatorWorkUrl';
410
- }
411
- return key;
446
+ function parseNodeChildrenAsTags(children, tags, plainDescriptions) {
447
+ oneLevelDeeper(() => {
448
+ for (const name in children) {
449
+ try {
450
+ if (!isNamespaceDefinition(name)) {
451
+ setParsedTag(tags, plainDescriptions, getLocalName(name), parseNodeAsTag(children[name], name));
452
+ }
453
+ } catch (error) {
454
+ // Keep going and try to parse the rest of the tags.
455
+ }
456
+ }
457
+ });
412
458
  }
413
459
 
414
- function parseNodeChildrenAsTags(children, tags = {}) {
415
- for (const name in children) {
416
- try {
417
- if (!isNamespaceDefinition(name)) {
418
- setProperty(tags, getLocalName(name), parseNodeAsTag(children[name], name));
419
- }
420
- } catch (error) {
421
- // Keep going and try to parse the rest of the tags.
460
+ function oneLevelDeeper(callback) {
461
+ nestingDepth++;
462
+ try {
463
+ if (nestingDepth > MAX_NESTING_DEPTH) {
464
+ // Lands in the catch of the nearest parseNodeChildrenAsTags, which
465
+ // drops the tag.
466
+ throw new ParseError(`XMP value nested deeper than ${MAX_NESTING_DEPTH} levels.`);
422
467
  }
468
+ return callback();
469
+ } finally {
470
+ nestingDepth--;
423
471
  }
424
-
425
- return tags;
426
472
  }
427
473
 
428
474
  function parseNodeAsTag(node, name) {
@@ -430,7 +476,7 @@ function parseNodeAsTag(node, name) {
430
476
  return parseNodeAsDuplicateTag(node, name);
431
477
  }
432
478
  if (isEmptyResourceTag(node)) {
433
- return {value: '', attributes: {}, description: ''};
479
+ return {tag: {value: '', attributes: {}, description: ''}, plainDescription: ''};
434
480
  }
435
481
  if (hasNestedSimpleRdfDescription(node)) {
436
482
  return parseNodeAsSimpleRdfDescription(node, name);
@@ -475,13 +521,9 @@ function parseNodeAsSimpleRdfDescription(node, name) {
475
521
 
476
522
  objectAssign(attributes, parseNodeAttributes(node), parseNodeChildrenAsAttributes(node));
477
523
 
478
- const value = parseRdfValue(node);
524
+ const {value, plainDescription} = parseRdfValue(node);
479
525
 
480
- return {
481
- value,
482
- attributes,
483
- description: getDescription(value, name)
484
- };
526
+ return createParsedTag(value, plainDescription, attributes, name);
485
527
  }
486
528
 
487
529
  function parseNodeAttributes(node) {
@@ -499,6 +541,10 @@ function parseNodeAttributes(node) {
499
541
  function parseNodeChildrenAsAttributes(node) {
500
542
  const attributes = {};
501
543
 
544
+ if (typeof node.value !== 'object') {
545
+ return attributes;
546
+ }
547
+
502
548
  for (const name in node.value) {
503
549
  if ((name !== 'rdf:value') && (!isNamespaceDefinition(name))) {
504
550
  setProperty(attributes, getLocalName(name), node.value[name].value);
@@ -510,7 +556,11 @@ function parseNodeChildrenAsAttributes(node) {
510
556
 
511
557
  function parseRdfValue(node) {
512
558
  const rdfValueNode = getLastNode(node.value['rdf:value']);
513
- return getURIValue(rdfValueNode) || parseRdfValueContent(rdfValueNode);
559
+ const uri = getURIValue(rdfValueNode);
560
+ if (uri) {
561
+ return {value: uri, plainDescription: uri};
562
+ }
563
+ return parseRdfValueContent(rdfValueNode);
514
564
  }
515
565
 
516
566
  // Repeated elements are collected into an array, and the last one wins, which
@@ -526,14 +576,56 @@ function getLastNode(node) {
526
576
  // the value of the tag rather than a tag of its own.
527
577
  function parseRdfValueContent(rdfValueNode) {
528
578
  if (isArray(rdfValueNode)) {
529
- return parseNodeAsArray(rdfValueNode).value;
579
+ // Lists nested in rdf:value elements never pass parseNodeChildrenAsTags, so each one is a level.
580
+ return oneLevelDeeper(() => parseArrayItems(rdfValueNode));
530
581
  }
531
582
  if (typeof rdfValueNode.value === 'object') {
532
583
  // The child names come from the image, so a child named __proto__ must
533
584
  // not be able to replace this object's prototype.
534
- return parseNodeChildrenAsTags(rdfValueNode.value, Object.create(null));
585
+ const tags = Object.create(null);
586
+ const plainDescriptions = Object.create(null);
587
+ parseNodeChildrenAsTags(rdfValueNode.value, tags, plainDescriptions);
588
+ return {value: tags, plainDescription: getDescriptionOfObject(tags, plainDescriptions)};
535
589
  }
536
- return rdfValueNode.value;
590
+ return {value: rdfValueNode.value, plainDescription: rdfValueNode.value};
591
+ }
592
+
593
+ function getDescriptionOfObject(tags, plainDescriptions) {
594
+ const descriptions = [];
595
+
596
+ for (const key in tags) {
597
+ descriptions.push(`${getClearTextKey(key)}: ${plainDescriptions[key]}`);
598
+ }
599
+
600
+ return descriptions.join('; ');
601
+ }
602
+
603
+ function getClearTextKey(key) {
604
+ if (key === 'CiAdrCity') {
605
+ return 'CreatorCity';
606
+ }
607
+ if (key === 'CiAdrCtry') {
608
+ return 'CreatorCountry';
609
+ }
610
+ if (key === 'CiAdrExtadr') {
611
+ return 'CreatorAddress';
612
+ }
613
+ if (key === 'CiAdrPcode') {
614
+ return 'CreatorPostalCode';
615
+ }
616
+ if (key === 'CiAdrRegion') {
617
+ return 'CreatorRegion';
618
+ }
619
+ if (key === 'CiEmailWork') {
620
+ return 'CreatorWorkEmail';
621
+ }
622
+ if (key === 'CiTelWork') {
623
+ return 'CreatorWorkPhone';
624
+ }
625
+ if (key === 'CiUrlWork') {
626
+ return 'CreatorWorkUrl';
627
+ }
628
+ return key;
537
629
  }
538
630
 
539
631
  function hasNestedStructureRdfDescription(node) {
@@ -542,38 +634,44 @@ function hasNestedStructureRdfDescription(node) {
542
634
  }
543
635
 
544
636
  function parseNodeAsStructureRdfDescription(node, name) {
545
- const tag = {
546
- value: {},
547
- attributes: {}
548
- };
637
+ const value = {};
638
+ const plainDescriptions = Object.create(null);
639
+ const attributes = {};
549
640
 
550
641
  if (node.value['rdf:Description'] !== undefined) {
551
- objectAssign(tag.value, parseNodeAttributesAsTags(node.value['rdf:Description'].attributes));
552
- objectAssign(tag.attributes, parseNodeAttributes(node));
642
+ parseNodeAttributesAsTags(node.value['rdf:Description'].attributes, value, plainDescriptions);
643
+ objectAssign(attributes, parseNodeAttributes(node));
553
644
  node = node.value['rdf:Description'];
554
645
  }
555
646
 
556
- objectAssign(tag.value, parseNodeChildrenAsTags(node.value));
557
-
558
- tag.description = getDescription(tag.value, name);
647
+ if (typeof node.value === 'object') {
648
+ parseNodeChildrenAsTags(node.value, value, plainDescriptions);
649
+ }
559
650
 
560
- return tag;
651
+ return createParsedTag(value, getDescriptionOfObject(value, plainDescriptions), attributes, name);
561
652
  }
562
653
 
563
654
  function isCompactStructure(node) {
564
- return (Object.keys(node.value).length === 0)
655
+ return isEmptyValue(node.value)
565
656
  && (node.attributes['xml:lang'] === undefined)
566
657
  && (node.attributes['rdf:resource'] === undefined);
567
658
  }
568
659
 
660
+ // A text-only element has a string value; enumerating it would visit every character.
661
+ function isEmptyValue(value) {
662
+ if (typeof value === 'string') {
663
+ return value === '';
664
+ }
665
+ return Object.keys(value).length === 0;
666
+ }
667
+
569
668
  function parseNodeAsCompactStructure(node, name) {
570
- const value = parseNodeAttributesAsTags(node.attributes);
669
+ const value = {};
670
+ const plainDescriptions = Object.create(null);
571
671
 
572
- return {
573
- value,
574
- attributes: {},
575
- description: getDescription(value, name)
576
- };
672
+ parseNodeAttributesAsTags(node.attributes, value, plainDescriptions);
673
+
674
+ return createParsedTag(value, getDescriptionOfObject(value, plainDescriptions), {}, name);
577
675
  }
578
676
 
579
677
  function isArray(node) {
@@ -585,9 +683,16 @@ function getArrayChild(value) {
585
683
  }
586
684
 
587
685
  function parseNodeAsArray(node, name) {
588
- let items = getArrayChild(node.value).value['rdf:li'];
589
686
  const attributes = parseNodeAttributes(node);
687
+ const {value, plainDescription} = parseArrayItems(node);
688
+
689
+ return createParsedTag(value, plainDescription, attributes, name);
690
+ }
691
+
692
+ function parseArrayItems(node) {
693
+ let items = getArrayChild(node.value).value['rdf:li'];
590
694
  const value = [];
695
+ const itemDescriptions = [];
591
696
 
592
697
  if (items === undefined) {
593
698
  items = [];
@@ -596,38 +701,53 @@ function parseNodeAsArray(node, name) {
596
701
  }
597
702
 
598
703
  items.forEach((item) => {
599
- value.push(parseArrayValue(item));
704
+ const parsedItem = parseArrayValue(item);
705
+ value.push(parsedItem.value);
706
+ itemDescriptions.push(parsedItem.plainDescription);
600
707
  });
601
708
 
602
- return {
603
- value,
604
- attributes,
605
- description: getDescription(value, name)
606
- };
709
+ return {value, plainDescription: itemDescriptions.join(', ')};
607
710
  }
608
711
 
609
712
  function parseArrayValue(item) {
610
713
  if (hasNestedSimpleRdfDescription(item)) {
611
- return parseNodeAsSimpleRdfDescription(item);
714
+ return getParsedTagAsItem(parseNodeAsSimpleRdfDescription(item));
612
715
  }
613
716
  if (hasNestedStructureRdfDescription(item)) {
614
- return parseNodeAsStructureRdfDescription(item).value;
717
+ return getParsedStructureAsItem(parseNodeAsStructureRdfDescription(item));
615
718
  }
616
719
  if (isCompactStructure(item)) {
617
- return parseNodeAsCompactStructure(item).value;
720
+ return getParsedStructureAsItem(parseNodeAsCompactStructure(item));
618
721
  }
619
722
 
620
- return parseNodeAsSimpleValue(item);
723
+ return getParsedTagAsItem(parseNodeAsSimpleValue(item));
724
+ }
725
+
726
+ function getParsedTagAsItem(parsedTag) {
727
+ return {value: parsedTag.tag, plainDescription: parsedTag.plainDescription};
728
+ }
729
+
730
+ function getParsedStructureAsItem(parsedStructure) {
731
+ return {value: parsedStructure.tag.value, plainDescription: parsedStructure.plainDescription};
621
732
  }
622
733
 
623
734
  function parseNodeAsSimpleValue(node, name) {
624
- const value = getURIValue(node) || parseXMPObject(node.value);
735
+ const {value, plainDescription} = parseSimpleValue(node);
736
+ return createParsedTag(value, plainDescription, parseNodeAttributes(node), name);
737
+ }
625
738
 
626
- return {
627
- value,
628
- attributes: parseNodeAttributes(node),
629
- description: getDescription(value, name)
630
- };
739
+ function parseSimpleValue(node) {
740
+ const uri = getURIValue(node);
741
+ if (uri) {
742
+ return {value: uri, plainDescription: uri};
743
+ }
744
+ if (typeof node.value === 'string') {
745
+ return {value: node.value, plainDescription: node.value};
746
+ }
747
+ const tags = {};
748
+ const plainDescriptions = Object.create(null);
749
+ parseXMPObject(node.value, tags, plainDescriptions);
750
+ return {value: tags, plainDescription: getDescriptionOfObject(tags, plainDescriptions)};
631
751
  }
632
752
 
633
753
  function getURIValue(node) {