Browse Source

load: trim xml string if necessary (#790)

close #790

xml strings with extra whitespace / newlines at the beginning weren't loaded correctly.
this is technically not a bugfix, since there should be now whitespace at the beginning
of xml strings, but it's a favor to users, to prevent their errors.
sschmid 5 năm trước cách đây
mục cha
commit
bdddc2a399
1 tập tin đã thay đổi với 11 bổ sung7 xóa
  1. 11 7
      src/OpenSheetMusicDisplay/OpenSheetMusicDisplay.ts

+ 11 - 7
src/OpenSheetMusicDisplay/OpenSheetMusicDisplay.ts

@@ -112,21 +112,25 @@ export class OpenSheetMusicDisplay {
                 // UTF with BOM detected, truncate first three bytes and pass along
                 return self.load(str.substr(3));
             }
-            if (str.substr(0, 6).includes("<?xml")) { // first character is sometimes null, making first five characters '<?xm'.
-                log.debug("[OSMD] Finally parsing XML content, length: " + str.length);
+            let trimmedStr: string = str;
+            if (/^\s/.test(trimmedStr)) { // only trim if we need to. (end of string is irrelevant)
+                trimmedStr = trimmedStr.trim(); // trim away empty lines at beginning etc
+            }
+            if (trimmedStr.substr(0, 6).includes("<?xml")) { // first character is sometimes null, making first five characters '<?xm'.
+                log.debug("[OSMD] Finally parsing XML content, length: " + trimmedStr.length);
                 // Parse the string representing an xml file
                 const parser: DOMParser = new DOMParser();
-                content = parser.parseFromString(str, "application/xml");
-            } else if (str.length < 2083) { // TODO do proper URL format check
-                log.debug("[OSMD] Retrieve the file at the given URL: " + str);
+                content = parser.parseFromString(trimmedStr, "application/xml");
+            } else if (trimmedStr.length < 2083) { // TODO do proper URL format check
+                log.debug("[OSMD] Retrieve the file at the given URL: " + trimmedStr);
                 // Assume now "str" is a URL
                 // Retrieve the file at the given URL
-                return AJAX.ajax(str).then(
+                return AJAX.ajax(trimmedStr).then(
                     (s: string) => { return self.load(s); },
                     (exc: Error) => { throw exc; }
                 );
             } else {
-                console.error("[OSMD] osmd.load(string): Could not process string. Missing else branch?");
+                console.error("[OSMD] osmd.load(string): Could not process string. Did not find <?xml at beginning.");
             }
         }