From 7741d55f2e85ca3e2ebff72ae3738cd14dfc1be4 Mon Sep 17 00:00:00 2001 From: "Friedrich W. H. Kossebau" Date: Wed, 17 Apr 2013 16:22:52 +0200 Subject: [PATCH] Create new class odf.Namespaces --- CMakeLists.txt | 1 + programs/touchui/app/views/FileDetail.js | 8 +- webodf/lib/gui/PresenterUI.js | 8 +- webodf/lib/gui/SessionController.js | 7 +- webodf/lib/manifest.js | 1 + webodf/lib/odf/FontLoader.js | 19 ++-- webodf/lib/odf/Formatting.js | 44 ++++---- webodf/lib/odf/Namespaces.js | 122 +++++++++++++++++++++++ webodf/lib/odf/OdfCanvas.js | 38 +++---- webodf/lib/odf/OdfContainer.js | 34 +++---- webodf/lib/odf/Style2CSS.js | 67 ++++--------- webodf/tests/xmldom/XPathTests.js | 16 +-- 12 files changed, 228 insertions(+), 137 deletions(-) create mode 100644 webodf/lib/odf/Namespaces.js diff --git a/CMakeLists.txt b/CMakeLists.txt index deab32f9..ec160e2b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -236,6 +236,7 @@ set(LIBJSFILES lib/packages.js lib/runtime.js lib/core/Base64.js lib/xmldom/OperationalTransformDOM.js lib/xmldom/XPath.js + lib/odf/Namespaces.js lib/odf/StyleInfo.js lib/odf/Style2CSS.js lib/odf/FontLoader.js diff --git a/programs/touchui/app/views/FileDetail.js b/programs/touchui/app/views/FileDetail.js index 91e9fc7a..f3708893 100644 --- a/programs/touchui/app/views/FileDetail.js +++ b/programs/touchui/app/views/FileDetail.js @@ -33,11 +33,11 @@ */ /*global Ext, app, runtime, xmldom, odf*/ runtime.loadClass("xmldom.XPath"); -runtime.loadClass("odf.Style2CSS"); +runtime.loadClass("odf.Namespaces"); Ext.define('WebODFApp.view.FileDetail', (function () { "use strict"; var panel, - style2CSS = new odf.Style2CSS(), + namespaces = new odf.Namespaces(), xpath = new xmldom.XPath(), fileDetail, title, @@ -48,13 +48,13 @@ Ext.define('WebODFApp.view.FileDetail', (function () { var ps, title; ps = xpath.getODFElementsWithXPath(body, - ".//text:h", style2CSS.namespaceResolver); + ".//text:h", namespaces.resolvePrefix); title = ""; if (ps && ps.length) { title = ps[0].nodeValue; } else { ps = xpath.getODFElementsWithXPath(body, - ".//text:p", style2CSS.namespaceResolver); + ".//text:p", namespaces.resolvePrefix); if (ps && ps.length) { title = ps[0].nodeValue; } diff --git a/webodf/lib/gui/PresenterUI.js b/webodf/lib/gui/PresenterUI.js index 0373c93f..90227e0c 100644 --- a/webodf/lib/gui/PresenterUI.js +++ b/webodf/lib/gui/PresenterUI.js @@ -33,13 +33,11 @@ /*global runtime, gui, odf, core, xmldom, document, window*/ runtime.loadClass("xmldom.XPath"); -runtime.loadClass("odf.Style2CSS"); +runtime.loadClass("odf.Namespaces"); gui.PresenterUI = (function () { "use strict"; - var s2css = new odf.Style2CSS(), - xpath = new xmldom.XPath(), - nsResolver = s2css.namespaceResolver; + var xpath = new xmldom.XPath(); return function PresenterUI(odf_element) { var self = this; @@ -136,7 +134,7 @@ gui.PresenterUI = (function () { // return a list of tuples (pagename, pagenode) self.getPages = function (root) { - var pagenodes = root.getElementsByTagNameNS(nsResolver('draw'), 'page'), + var pagenodes = root.getElementsByTagNameNS(odf.Namespaces.drawns, 'page'), pages = [], i; for (i = 0; i < pagenodes.length; i += 1) { diff --git a/webodf/lib/gui/SessionController.js b/webodf/lib/gui/SessionController.js index 917fcedb..36b6c10a 100644 --- a/webodf/lib/gui/SessionController.js +++ b/webodf/lib/gui/SessionController.js @@ -57,8 +57,7 @@ gui.SessionController = (function () { * @return {?} */ gui.SessionController = function SessionController(session, inputMemberId) { - var self = this, - /**@type{Object}*/ namespaces = new odf.Style2CSS().namespaces; + var self = this; function listenEvent(eventTarget, eventType, eventHandler) { if (eventTarget.addEventListener) { @@ -214,8 +213,8 @@ gui.SessionController = (function () { /* if (isAtEndOfParagraph) { paragraphNode = odtDocument.getParagraphElement(odtDocument.getCursor(inputMemberId).getNode()); - styleName = paragraphNode.getAttributeNS(namespaces.text, 'style-name'); - nextStyleName = odtDocument.getFormatting().getParagraphStyleAttribute(styleName, namespaces.style, 'next-style-name'); + styleName = paragraphNode.getAttributeNS(odf.Namespaces.textns, 'style-name'); + nextStyleName = odtDocument.getFormatting().getParagraphStyleAttribute(styleName, odf.Namespaces.stylens, 'next-style-name'); if (nextStyleName && nextStyleName !== styleName) { op = new ops.OpSetParagraphStyle(); diff --git a/webodf/lib/manifest.js b/webodf/lib/manifest.js index 684390cc..af665b35 100644 --- a/webodf/lib/manifest.js +++ b/webodf/lib/manifest.js @@ -64,6 +64,7 @@ "odf/CommandLineTools.js", "odf/FontLoader.js", "odf/Formatting.js", + "odf/Namespaces.js", "odf/OdfCanvas.js", "odf/OdfContainer.js", "odf/Style2CSS.js", diff --git a/webodf/lib/odf/FontLoader.js b/webodf/lib/odf/FontLoader.js index 1dfeec71..6a296c25 100644 --- a/webodf/lib/odf/FontLoader.js +++ b/webodf/lib/odf/FontLoader.js @@ -34,7 +34,7 @@ /*global runtime, odf, core, document, xmldom*/ runtime.loadClass("core.Base64"); runtime.loadClass("xmldom.XPath"); -runtime.loadClass("odf.Style2CSS"); +runtime.loadClass("odf.Namespaces"); /** * This class loads embedded fonts into the CSS * @constructor @@ -42,9 +42,9 @@ runtime.loadClass("odf.Style2CSS"); **/ odf.FontLoader = (function () { "use strict"; - var style2CSS = new odf.Style2CSS(), - xpath = new xmldom.XPath(), - base64 = new core.Base64(); + var xpath = new xmldom.XPath(), + base64 = new core.Base64(), + namespaces = new odf.Namespaces(); /** * @param {!Element} fontFaceDecls * @return {!Object.} @@ -56,17 +56,16 @@ odf.FontLoader = (function () { } fonts = xpath.getODFElementsWithXPath(fontFaceDecls, "style:font-face[svg:font-face-src]", - style2CSS.namespaceResolver); + namespaces.resolvePrefix); for (i = 0; i < fonts.length; i += 1) { font = fonts[i]; - name = font.getAttributeNS(style2CSS.namespaces.style, "name"); - family = font.getAttributeNS(style2CSS.namespaces.svg, "font-family"); + name = font.getAttributeNS(odf.Namespaces.stylens, "name"); + family = font.getAttributeNS(odf.Namespaces.svgns, "font-family"); uris = xpath.getODFElementsWithXPath(font, "svg:font-face-src/svg:font-face-uri", - style2CSS.namespaceResolver); + namespaces.resolvePrefix); if (uris.length > 0) { - href = uris[0].getAttributeNS(style2CSS.namespaces["xlink"], - "href"); + href = uris[0].getAttributeNS(odf.Namespaces.xlinkns, "href"); decls[name] = {href: href, family: family}; } } diff --git a/webodf/lib/odf/Formatting.js b/webodf/lib/odf/Formatting.js index 7242f643..76af6342 100644 --- a/webodf/lib/odf/Formatting.js +++ b/webodf/lib/odf/Formatting.js @@ -33,6 +33,8 @@ */ /*global odf, runtime, console*/ +runtime.loadClass("odf.Style2CSS"); + /** * @constructor */ @@ -41,7 +43,8 @@ odf.Formatting = function Formatting() { var /**@type{odf.OdfContainer}*/ odfContainer, /**@type{odf.StyleInfo}*/ styleInfo = new odf.StyleInfo(), /**@type{odf.Style2CSS}*/ style2CSS = new odf.Style2CSS(), - /**@type{Object}*/ namespaces = style2CSS.namespaces; + /**@const@type {!string}*/ stylens = odf.Namespaces.stylens; + /** * Class that iterates over all elements that are part of the range. * @constructor @@ -193,12 +196,12 @@ odf.Formatting = function Formatting() { style; while (node) { if (node.nodeType === 1 && node.localName === "style" - && node.namespaceURI === namespaces.style) { + && node.namespaceURI === stylens) { style = node; - p_family = style.getAttributeNS(namespaces.style, 'family'); + p_family = style.getAttributeNS(stylens, 'family'); if (p_family === "paragraph") { - p_name = style.getAttributeNS(namespaces.style, 'name'); - p_displayName = style.getAttributeNS(namespaces.style, 'display-name') || p_name; + p_name = style.getAttributeNS(stylens, 'name'); + p_displayName = style.getAttributeNS(stylens, 'display-name') || p_name; if (p_name && p_displayName) { paragraphStyles.push({ name: p_name, @@ -218,9 +221,10 @@ odf.Formatting = function Formatting() { * @return {boolean} */ this.isStyleUsed = function (styleElement) { - var hasDerivedStyles, isUsed; - - hasDerivedStyles = styleInfo.hasDerivedStyles(odfContainer.rootElement, style2CSS.namespaceResolver, styleElement); + var hasDerivedStyles, isUsed, + namespaces = new odf.Namespaces(); + + hasDerivedStyles = styleInfo.hasDerivedStyles(odfContainer.rootElement, namespaces.resolvePrefix, styleElement); isUsed = new styleInfo.UsedStyleList(odfContainer.rootElement.styles).uses(styleElement) || new styleInfo.UsedStyleList(odfContainer.rootElement.automaticStyles).uses(styleElement) @@ -234,9 +238,9 @@ odf.Formatting = function Formatting() { while (node) { if (node.nodeType === 1 - && node.namespaceURI === namespaces.style + && node.namespaceURI === stylens && node.localName === "default-style" - && node.getAttributeNS(namespaces.style, 'family') === family) { + && node.getAttributeNS(stylens, 'family') === family) { return node; } node = node.nextSibling; @@ -249,10 +253,10 @@ odf.Formatting = function Formatting() { while (node) { if (node.nodeType === 1 - && node.namespaceURI === namespaces.style + && node.namespaceURI === stylens && node.localName === "style" - && node.getAttributeNS(namespaces.style, 'family') === family - && node.getAttributeNS(namespaces.style, 'name') === styleName) { + && node.getAttributeNS(stylens, 'family') === family + && node.getAttributeNS(stylens, 'name') === styleName) { return node; } node = node.nextSibling; @@ -273,7 +277,7 @@ odf.Formatting = function Formatting() { propertiesNode = styleNode.firstChild; while (propertiesNode) { - if (propertiesNode.nodeType === 1 && propertiesNode.namespaceURI === namespaces.style) { + if (propertiesNode.nodeType === 1 && propertiesNode.namespaceURI === stylens) { propertiesMap[propertiesNode.nodeName] = {}; for (i = 0; i < propertiesNode.attributes.length; i += 1) { propertiesMap[propertiesNode.nodeName][propertiesNode.attributes[i].name] = propertiesNode.attributes[i].value; @@ -305,16 +309,16 @@ odf.Formatting = function Formatting() { propertiesMap = getStyleAttributes(node); inheritedPropertiesMap = mergeRecursive(propertiesMap, inheritedPropertiesMap); - parentStyleName = node.getAttributeNS(namespaces.style, 'parent-style-name'); + parentStyleName = node.getAttributeNS(stylens, 'parent-style-name'); if (parentStyleName) { - node = getStyleElement(styleListElement, parentStyleName, styleNode.getAttributeNS(namespaces.style, 'family')); + node = getStyleElement(styleListElement, parentStyleName, styleNode.getAttributeNS(stylens, 'family')); } else { node = null; } } // Now incorporate attributes from the default style - propertiesMap = getStyleAttributes(getDefaultStyleElement(styleListElement, styleNode.getAttributeNS(namespaces.style, 'family'))); + propertiesMap = getStyleAttributes(getDefaultStyleElement(styleListElement, styleNode.getAttributeNS(stylens, 'family'))); inheritedPropertiesMap = mergeRecursive(propertiesMap, inheritedPropertiesMap); return inheritedPropertiesMap; @@ -335,7 +339,7 @@ odf.Formatting = function Formatting() { // first look for automatic style with the name while ((styleElement = getStyleElement(automaticStyleElementList, styleName, "paragraph")) !== null) { - styleName = styleElement.getAttributeNS(namespaces.style, 'parent-style-name'); + styleName = styleElement.getAttributeNS(stylens, 'parent-style-name'); } // then see if that style is in named styles styleElement = getStyleElement(styleElementList, styleName, "paragraph"); @@ -376,7 +380,7 @@ odf.Formatting = function Formatting() { if (attributeValue) { return attributeValue; } - styleName = styleElement.getAttributeNS(namespaces.style, 'parent-style-name'); + styleName = styleElement.getAttributeNS(stylens, 'parent-style-name'); } // then see if that style is in named styles while ((styleElement = getStyleElement(styleElementList, styleName, "paragraph")) !== null) { @@ -384,7 +388,7 @@ odf.Formatting = function Formatting() { if (attributeValue) { return attributeValue; } - styleName = styleElement.getAttributeNS(namespaces.style, 'parent-style-name'); + styleName = styleElement.getAttributeNS(stylens, 'parent-style-name'); } return null; }; diff --git a/webodf/lib/odf/Namespaces.js b/webodf/lib/odf/Namespaces.js new file mode 100644 index 00000000..9f6c84ac --- /dev/null +++ b/webodf/lib/odf/Namespaces.js @@ -0,0 +1,122 @@ +/** + * @license + * Copyright (C) 2012-2013 KO GmbH + * + * @licstart + * The JavaScript code in this page is free software: you can redistribute it + * and/or modify it under the terms of the GNU Affero General Public License + * (GNU AGPL) as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. The code is distributed + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU AGPL for more details. + * + * As additional permission under GNU AGPL version 3 section 7, you + * may distribute non-source (e.g., minimized or compacted) forms of + * that code without the copy of the GNU GPL normally required by + * section 4, provided you include this license notice and a URL + * through which recipients can access the Corresponding Source. + * + * As a special exception to the AGPL, any HTML file which merely makes function + * calls to this code, and for that purpose includes it by reference shall be + * deemed a separate work for copyright law purposes. In addition, the copyright + * holders of this code give you permission to combine this code with free + * software libraries that are released under the GNU LGPL. You may copy and + * distribute such a system following the terms of the GNU AGPL for this code + * and the LGPL for the libraries. If you modify this code, you may extend this + * exception to your version of the code, but you are not obligated to do so. + * If you do not wish to do so, delete this exception statement from your + * version. + * + * This license applies to this entire compilation. + * @licend + * @source: http://www.webodf.org/ + * @source: http://gitorious.org/webodf/webodf/ + */ + +/*global odf*/ + +/** + * @constructor + * @return {?} + */ +odf.Namespaces = (function () { + "use strict"; + + var drawns = "urn:oasis:names:tc:opendocument:xmlns:drawing:1.0", + fons = "urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0", + officens = "urn:oasis:names:tc:opendocument:xmlns:office:1.0", + presentationns = "urn:oasis:names:tc:opendocument:xmlns:presentation:1.0", + stylens = "urn:oasis:names:tc:opendocument:xmlns:style:1.0", + svgns = "urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0", + tablens = "urn:oasis:names:tc:opendocument:xmlns:table:1.0", + textns = "urn:oasis:names:tc:opendocument:xmlns:text:1.0", + xlinkns = 'http://www.w3.org/1999/xlink', + xmlns = "http://www.w3.org/XML/1998/namespace"; + + /** + * @constructor + * @return {?} + */ + odf.Namespaces = function Namespaces() { + var self = this, + /** @const@type {!Object.} */ + standardNamespaceMap = { + "draw": drawns, + "fo": fons, + "office": officens, + "presentation": presentationns, + "style": stylens, + "svg": svgns, + "table": tablens, + "text": textns, + "xlink": xlinkns, + "xml": xmlns + }; + + /** + * @param {function(string,string)} cb + * @return {undefined} + */ + this.forEachPrefix = function (cb) { + var prefix; + + for (prefix in standardNamespaceMap) { + if (standardNamespaceMap.hasOwnProperty(prefix)) { + cb(prefix, standardNamespaceMap[prefix]); + } + } + }; + + /** + * @return {!Object.} + */ + this.getNamespaceMap = function () { + return standardNamespaceMap; + }; + + /** + * @param {!string} prefix + * @return {?string} + */ + this.resolvePrefix = function (prefix) { + return standardNamespaceMap[prefix] || null; + }; + + // TODO: document where and why this is needed + this.resolvePrefix.lookupNamespaceURI = this.resolvePrefix; + }; + + // add all namespaces as global properties + /**@const@type {!string}*/ odf.Namespaces.drawns = drawns; + /**@const@type {!string}*/ odf.Namespaces.fons = fons; + /**@const@type {!string}*/ odf.Namespaces.officens = officens; + /**@const@type {!string}*/ odf.Namespaces.presentationns = presentationns; + /**@const@type {!string}*/ odf.Namespaces.stylens = stylens; + /**@const@type {!string}*/ odf.Namespaces.svgns = svgns; + /**@const@type {!string}*/ odf.Namespaces.tablens = tablens; + /**@const@type {!string}*/ odf.Namespaces.textns = textns; + /**@const@type {!string}*/ odf.Namespaces.xlinkns = xlinkns; + /**@const@type {!string}*/ odf.Namespaces.xmlns = xmlns; + + return odf.Namespaces; +}()); diff --git a/webodf/lib/odf/OdfCanvas.js b/webodf/lib/odf/OdfCanvas.js index d02a8778..72ab521c 100644 --- a/webodf/lib/odf/OdfCanvas.js +++ b/webodf/lib/odf/OdfCanvas.js @@ -275,18 +275,17 @@ odf.OdfCanvas = (function () { listenEvent(element, "keyup", checkSelection); listenEvent(element, "keydown", checkSelection); } - var style2CSS = new odf.Style2CSS(), - namespaces = style2CSS.namespaces, - drawns = namespaces.draw, - fons = namespaces.fo, - officens = namespaces.office, - stylens = namespaces.style, - svgns = namespaces.svg, - tablens = namespaces.table, - textns = namespaces.text, - xlinkns = namespaces.xlink, - xmlns = namespaces.xml, - window = runtime.getWindow(), + var /**@const@type {!string}*/drawns = odf.Namespaces.drawns, + /**@const@type {!string}*/fons = odf.Namespaces.fons, + /**@const@type {!string}*/officens = odf.Namespaces.officens, + /**@const@type {!string}*/stylens = odf.Namespaces.stylens, + /**@const@type {!string}*/svgns = odf.Namespaces.svgns, + /**@const@type {!string}*/tablens = odf.Namespaces.tablens, + /**@const@type {!string}*/textns = odf.Namespaces.textns, + /**@const@type {!string}*/xlinkns = odf.Namespaces.xlinkns, + /**@const@type {!string}*/xmlns = odf.Namespaces.xmlns, + /**@const@type {!string}*/window = runtime.getWindow(), + namespaces = new odf.Namespaces(), xpath = new xmldom.XPath(); /** @@ -416,7 +415,7 @@ odf.OdfCanvas = (function () { i, nodes = xpath.getODFElementsWithXPath(odfbody, ".//*[*[@text:anchor-type='paragraph']]", - style2CSS.namespaceResolver); + namespaces.resolvePrefix); for (i = 0; i < nodes.length; i += 1) { n = nodes[i]; if (n.setAttributeNS) { @@ -495,9 +494,6 @@ odf.OdfCanvas = (function () { frames, i, images; - function namespaceResolver(prefix) { - return namespaces[prefix]; - } // find all the frame elements frames = []; node = odfbody.firstChild; @@ -640,7 +636,6 @@ odf.OdfCanvas = (function () { function loadLists(container, odffragment, stylesheet) { var i, lists, - svgns = namespaces.svg, node, id, continueList, @@ -749,12 +744,9 @@ odf.OdfCanvas = (function () { prefix; style.setAttribute('type', 'text/css'); style.setAttribute('media', 'screen, print, handheld, projection'); - for (prefix in namespaces) { - if (namespaces.hasOwnProperty(prefix) && prefix) { - text += "@namespace " + prefix + " url(" + namespaces[prefix] - + ");\n"; - } - } + namespaces.forEachPrefix(function(prefix, ns) { + text += "@namespace " + prefix + " url(" + ns + ");\n"; + }); style.appendChild(document.createTextNode(text)); head.appendChild(style); return style; diff --git a/webodf/lib/odf/OdfContainer.js b/webodf/lib/odf/OdfContainer.js index 75a6d08b..607537a2 100644 --- a/webodf/lib/odf/OdfContainer.js +++ b/webodf/lib/odf/OdfContainer.js @@ -35,7 +35,7 @@ runtime.loadClass("core.Base64"); runtime.loadClass("core.Zip"); runtime.loadClass("xmldom.LSSerializer"); runtime.loadClass("odf.StyleInfo"); -runtime.loadClass("odf.Style2CSS"); +runtime.loadClass("odf.Namespaces"); runtime.loadClass("odf.FontLoader"); /** * The OdfContainer class manages the various parts that constitues an ODF @@ -48,8 +48,7 @@ runtime.loadClass("odf.FontLoader"); odf.OdfContainer = (function () { "use strict"; var styleInfo = new odf.StyleInfo(), - style2CSS = new odf.Style2CSS(), - namespaces = style2CSS.namespaces, + namespaces = new odf.Namespaces(), officens = "urn:oasis:names:tc:opendocument:xmlns:office:1.0", manifestns = "urn:oasis:names:tc:opendocument:xmlns:manifest:1.0", webodfns = "urn:webodf:names:origin", @@ -581,13 +580,12 @@ odf.OdfContainer = (function () { }); }); } - function documentElement(name, map) { + function createDocumentElement(name, namespaces) { var s = "", i; - for (i in map) { - if (map.hasOwnProperty(i)) { - s += " xmlns:" + i + "=\"" + map[i] + "\""; - } - } + + namespaces.forEachPrefix(function(prefix, ns) { + s += " xmlns:" + prefix + "=\"" + ns + "\""; + }); return ""; } @@ -595,9 +593,9 @@ odf.OdfContainer = (function () { * @return {!string} */ function serializeMetaXml() { - var nsmap = style2CSS.namespaces, + var nsmap = namespaces.getNamespaceMap(), serializer = new xmldom.LSSerializer(), - /**@type{!string}*/ s = documentElement("document-meta", nsmap); + /**@type{!string}*/ s = createDocumentElement("document-meta", namespaces); serializer.filter = new OdfNodeFilter(self.rootElement); s += serializer.writeToString(self.rootElement.meta, nsmap); s += ""; @@ -632,15 +630,15 @@ odf.OdfContainer = (function () { } } serializer.filter = new OdfNodeFilter(self.rootElement); - return header + serializer.writeToString(manifest, style2CSS.namespaces); + return header + serializer.writeToString(manifest, namespaces.getNamespaceMap()); } /** * @return {!string} */ function serializeSettingsXml() { - var nsmap = style2CSS.namespaces, + var nsmap = namespaces.getNamespaceMap(), serializer = new xmldom.LSSerializer(), - /**@type{!string}*/ s = documentElement("document-settings", nsmap); + /**@type{!string}*/ s = createDocumentElement("document-settings", namespaces); serializer.filter = new OdfNodeFilter(self.rootElement); s += serializer.writeToString(self.rootElement.settings, nsmap); s += ""; @@ -650,12 +648,12 @@ odf.OdfContainer = (function () { * @return {!string} */ function serializeStylesXml() { - var nsmap = style2CSS.namespaces, + var nsmap = namespaces.getNamespaceMap(), serializer = new xmldom.LSSerializer(), // select the styles by the tag, only needed until smart saving of styles is implemented automaticStyles = cloneStylesByOrigin(self.rootElement.automaticStyles, "styles.xml"), masterStyles = self.rootElement.masterStyles && self.rootElement.masterStyles.cloneNode(true), - /**@type{!string}*/ s = documentElement("document-styles", nsmap); + /**@type{!string}*/ s = createDocumentElement("document-styles", namespaces); // TODO: care about automatic styles only referenced by other automatic styles // once that is done, add self.rootElement.masterStyles as second parameter again // For now just the complete existing automatic styles are written @@ -680,11 +678,11 @@ odf.OdfContainer = (function () { * @return {!string} */ function serializeContentXml() { - var nsmap = style2CSS.namespaces, + var nsmap = namespaces.getNamespaceMap(), serializer = new xmldom.LSSerializer(), // select the styles by the tag, only needed until smart saving of styles is implemented automaticStyles = cloneStylesByOrigin(self.rootElement.automaticStyles, "content.xml"), - /**@type{!string}*/ s = documentElement("document-content", nsmap); + /**@type{!string}*/ s = createDocumentElement("document-content", namespaces); // TODO: care about automatic styles only referenced by other automatic styles // once that is done, add self.rootElement.body as second parameter again // For now just the complete existing automatic styles are written diff --git a/webodf/lib/odf/Style2CSS.js b/webodf/lib/odf/Style2CSS.js index 7b91d97e..d692d51e 100644 --- a/webodf/lib/odf/Style2CSS.js +++ b/webodf/lib/odf/Style2CSS.js @@ -30,35 +30,25 @@ * @source: http://www.webodf.org/ * @source: http://gitorious.org/webodf/webodf/ */ + /*global odf, runtime*/ + +runtime.loadClass("odf.Namespaces"); + /** * @constructor */ odf.Style2CSS = function Style2CSS() { "use strict"; - // helper constants - var xlinkns = 'http://www.w3.org/1999/xlink', - drawns = "urn:oasis:names:tc:opendocument:xmlns:drawing:1.0", - fons = "urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0", - officens = "urn:oasis:names:tc:opendocument:xmlns:office:1.0", - presentationns = "urn:oasis:names:tc:opendocument:xmlns:presentation:1.0", - stylens = "urn:oasis:names:tc:opendocument:xmlns:style:1.0", - svgns = "urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0", - tablens = "urn:oasis:names:tc:opendocument:xmlns:table:1.0", - textns = "urn:oasis:names:tc:opendocument:xmlns:text:1.0", - xmlns = "http://www.w3.org/XML/1998/namespace", - namespaces = { - "draw": drawns, - "fo": fons, - "office": officens, - "presentation": presentationns, - "style": stylens, - "svg": svgns, - "table": tablens, - "text": textns, - "xlink": xlinkns, - "xml": xmlns - }, + var namespaces = new odf.Namespaces(), + + // helper constants + /**@const@type{!string}*/ drawns = odf.Namespaces.drawns, + /**@const@type{!string}*/ fons = odf.Namespaces.fons, + /**@const@type{!string}*/ stylens = odf.Namespaces.stylens, + /**@const@type{!string}*/ svgns = odf.Namespaces.svgns, + /**@const@type{!string}*/ textns = odf.Namespaces.textns, + /**@const@type{!string}*/ xlinkns = odf.Namespaces.xlinkns, familynamespaceprefixes = { 'graphic': 'draw', @@ -176,13 +166,6 @@ odf.Style2CSS = function Style2CSS() { fontFaceDeclsMap = {}; // helper functions - /** - * @param {string} prefix - * @return {string} - */ - function namespaceResolver(prefix) { - return namespaces[prefix] || null; - } /** * @param {!Document} doc * @param {!Element} stylesnode @@ -750,10 +733,6 @@ odf.Style2CSS = function Style2CSS() { // which an element applies. ODF families can be mapped to a group of css // elements - this.namespaces = namespaces; - this.namespaceResolver = namespaceResolver; - this.namespaceResolver.lookupNamespaceURI = this.namespaceResolver; - // Font face declarations map this.createFontFaceDeclsMap = createFontFaceDeclsMap; @@ -782,18 +761,16 @@ odf.Style2CSS = function Style2CSS() { return; } // add @namespace rules - for (prefix in namespaces) { - if (namespaces.hasOwnProperty(prefix)) { - rule = '@namespace ' + prefix + ' url(' + namespaces[prefix] + ');'; - try { - stylesheet.insertRule(rule, stylesheet.cssRules.length); - } catch (e) { - // WebKit can throw an exception here, but it will have - // retained the namespace declarations anyway. - } + namespaces.forEachPrefix(function(prefix, ns) { + rule = '@namespace ' + prefix + ' url(' + ns + ');'; + try { + stylesheet.insertRule(rule, stylesheet.cssRules.length); + } catch (e) { + // WebKit can throw an exception here, but it will have + // retained the namespace declarations anyway. } - } - + }); + // Populate the font face declarations map fontFaceDeclsMap = createFontFaceDeclsMap(fontFaceDecls); // add the various styles diff --git a/webodf/tests/xmldom/XPathTests.js b/webodf/tests/xmldom/XPathTests.js index f8cdda77..263de187 100644 --- a/webodf/tests/xmldom/XPathTests.js +++ b/webodf/tests/xmldom/XPathTests.js @@ -32,7 +32,7 @@ */ /*global runtime, core, xmldom, odf, XMLSerializer*/ runtime.loadClass("xmldom.XPath"); -runtime.loadClass("odf.Style2CSS"); +runtime.loadClass("odf.Namespaces"); /** * @constructor @@ -42,15 +42,15 @@ runtime.loadClass("odf.Style2CSS"); xmldom.XPathTests = function XPathTests(runner) { "use strict"; var r = runner, - style2CSS = new odf.Style2CSS(), + namespaces = new odf.Namespaces(), t; function setupDoc() { - var stylens = style2CSS.namespaceResolver("style"), - svgns = style2CSS.namespaceResolver("svg"), - drawns = style2CSS.namespaceResolver("draw"), - presentationns = style2CSS.namespaceResolver("presentation"), - textns = style2CSS.namespaceResolver("text"), + var stylens = odf.Namespaces.stylens, + svgns = odf.Namespaces.svgns, + drawns = odf.Namespaces.drawns, + presentationns = odf.Namespaces.presentationns, + textns = odf.Namespaces.textns, doc = runtime.getDOMImplementation().createDocument("", "a", null), r = doc.documentElement, fontFace = doc.createElementNS(stylens, "font-face"), @@ -82,7 +82,7 @@ xmldom.XPathTests = function XPathTests(runner) { for (x in xpaths) { if (xpaths.hasOwnProperty(x)) { t.result = xpath.getODFElementsWithXPath(t.doc.documentElement, - x, style2CSS.namespaceResolver); + x, namespaces.resolvePrefix); r.shouldBe(t, "t.result.length", "1"); r.shouldBe(t, "t.result[0]", xpaths[x]); }