Fix photo upload from wikidata.
[redakcja.git] / src / redakcja / static / js / wiki / xslt.js
index ab90e0c..a38ce6b 100644 (file)
@@ -17,43 +17,19 @@ function withStylesheets(code_block, onError)
     if (!xml2htmlStylesheet) {
        $.blockUI({message: 'Ładowanie arkuszy stylów...'});
        $.ajax({
-               url: STATIC_URL + 'xsl/wl2html_client.xsl?20171106',
-               dataType: 'xml',
-               timeout: 10000,
-               success: function(data) {
+            url: '/wlxml/wl2html.xsl',
+            dataType: 'xml',
+            timeout: 10000,
+            success: function(data) {
                xml2htmlStylesheet = createXSLT(data);
                 $.unblockUI();
-                               code_block();
-
-            },
-                       error: onError
-        })
-    }
-       else {
                code_block();
-       }
-}
-
-
-// Wykonuje block z załadowanymi kanonicznymi motywami
-function withThemes(code_block, onError)
-{
-    if (typeof withThemes.canon == 'undefined') {
-        $.ajax({
-            url: '/editor/themes',
-            dataType: 'text',
-            success: function(data) {
-                withThemes.canon = data.split('\n');
-                code_block(withThemes.canon);
             },
-            error: function() {
-                withThemes.canon = null;
-                code_block(withThemes.canon);
-            }
+           error: onError
         })
     }
     else {
-        code_block(withThemes.canon);
+       code_block();
     }
 }
 
@@ -69,7 +45,6 @@ function xml2html(options) {
 
         if (error.length == 0) {
             doc = xml2htmlStylesheet.transformToFragment(doc, document);
-            console.log(doc.firstChild);
 
         if(doc.firstChild === null) {
             options.error("Błąd w przetwarzaniu XML.");
@@ -85,9 +60,23 @@ function xml2html(options) {
             source.text('');
             options.error(error.text(), source_text);
         } else {
+            let galleryUrl = new URL(
+                options.base,
+                window.location.href
+            );
+            $("img", $(doc.childNodes)).each(function() {
+                $(this).attr(
+                    'src',
+                    new URL(
+                        $(this).attr('src'),
+                        galleryUrl
+                    )
+                );
+            })
+
             options.success(doc.childNodes);
 
-            withThemes(function(canonThemes) {
+            $.themes.withCanon(function(canonThemes) {
                 if (canonThemes != null) {
                     $('.theme-text-list').addClass('canon').each(function(){
                         var themes = $(this).html().split(',');
@@ -118,6 +107,7 @@ const DOCUMENT_TYPE_NODE             = 10;
 const DOCUMENT_FRAGMENT_NODE         = 11;
 const NOTATION_NODE                  = 12;
 const XATTR_RE = /^x-attr-name-(.*)$/;
+const XATTR_KNOWN_RE = /^x-a-([a-z]+)-(.*)$/;
 
 const ELEM_START = 1;
 const ELEM_END = 2;
@@ -130,6 +120,13 @@ const NAMESPACES = {
        "http://www.w3.org/XML/1998/namespace": "xml"
 };
 
+NS_PREFIXES = {
+    'wl': ''
+};
+for (prefix in NAMESPACES) {
+    NS_PREFIXES[NAMESPACES[prefix]] = prefix
+};
+
 function HTMLSerializer() {
        // empty constructor
 }
@@ -181,7 +178,7 @@ HTMLSerializer.prototype._verseBefore = function(node) {
 }
 
 HTMLSerializer.prototype._nodeIgnored = function(node) {
-    return node.getAttribute('x-node') == 'wers';
+    return node.getAttribute('x-auto-node') == 'true';
 }
 
 HTMLSerializer.prototype._ignoredWithWhitespace = function(node) {
@@ -338,32 +335,61 @@ HTMLSerializer.prototype._serializeElement = function(node) {
 
        /* retrieve attributes */
        var attributeIDs = [];
+        var attributes = [];
        for (var i = 0; i < node.attributes.length; i++) {
-               var attr = node.attributes.item(i);
-
-               // check if name starts with "x-attr-name"
-               var m = attr.name.match(XATTR_RE);
-               if (m !== null)
-                       attributeIDs.push(m[1]);
+           var attr = node.attributes.item(i);
+
+            m = attr.name.match(XATTR_KNOWN_RE);
+            if (m !== null) {
+                prefix = m[1];
+                tag = m[2];
+                attributes.push([
+                    NS_PREFIXES[prefix],
+                    tag,
+                    attr.value
+                ]);
+            } else {
+               // check if name starts with "x-attr-name"
+               var m = attr.name.match(XATTR_RE);
+               if (m !== null) {
+                   attributeIDs.push(m[1]);
+                }
+            }
        };
 
        /* print out */
 
        self.result += '<' + tagName;
 
-       $.each(attributeIDs, function() {
-               var nsData = self._assignNamespace(node.getAttribute('x-attr-ns-'+this));
-
+        function writeAttr(ns, tag, value) {
+            if (ns) {
+               var nsData = self._assignNamespace(ns);
                if(nsData.fresh) {
-                       newNamespaces.push(nsData);
-                       self.stack.push({
-                               "type": NS_END,
-                               "namespace": nsData
-                       });
+                   newNamespaces.push(nsData);
+                   self.stack.push({
+                       "type": NS_END,
+                       "namespace": nsData
+                   });
                };
+                tag = self._join(nsData.prefix, tag);
+            }
 
-               self.result += ' ' + self._join(nsData.prefix, node.getAttribute('x-attr-name-'+this));
-               self.result += '="'+node.getAttribute('x-attr-value-'+this) +'"';
+           self.result += ' ' + tag;
+           self.result += '="' + value.replace(/&/g, '&amp;').replace(/"/g, '&quot;') + '"';
+        }
+        
+        $.each(attributes, function() {
+            writeAttr(
+                this[0], this[1], this[2]
+            );
+        });
+        
+       $.each(attributeIDs, function() {
+            writeAttr(
+                node.getAttribute('x-attr-ns-'+this),
+               node.getAttribute('x-attr-name-'+this),
+               node.getAttribute('x-attr-value-'+this)
+            );
        });
 
        /* print new namespace declarations */