diff --git a/webodf/lib/core/DomUtils.js b/webodf/lib/core/DomUtils.js index 43e76170..730ff9e1 100644 --- a/webodf/lib/core/DomUtils.js +++ b/webodf/lib/core/DomUtils.js @@ -142,6 +142,26 @@ return browserQuirks; } + /** + * Return the first child element with the given namespace and name. + * If the parent is null, or if there is no child with the given name and + * namespace, null is returned. + * @param {?Element} parent + * @param {!string} ns + * @param {!string} name + * @return {?Element} + */ + function getDirectChild(parent, ns, name) { + var node = parent ? parent.firstElementChild : null; + while (node) { + if (node.localName === name && node.namespaceURI === ns) { + return /**@type{!Element}*/(node); + } + node = node.nextElementSibling; + } + return null; + } + /** * A collection of Dom utilities * @constructor @@ -813,6 +833,8 @@ } this.mapObjOntoNode = mapObjOntoNode; + this.getDirectChild = getDirectChild; + /** * @param {!core.DomUtils} self */ diff --git a/webodf/lib/odf/Formatting.js b/webodf/lib/odf/Formatting.js index 07c51c21..ea829250 100644 --- a/webodf/lib/odf/Formatting.js +++ b/webodf/lib/odf/Formatting.js @@ -108,22 +108,6 @@ odf.Formatting = function Formatting() { this.setOdfContainer = function (odfcontainer) { odfContainer = odfcontainer; }; - /** - * @param {?Element} node - * @param {!string} ns - * @param {!string} name - * @return {?Element} - */ - function getDirectChild(node, ns, name) { - var e = node && node.firstElementChild; - while (e) { - if (e.namespaceURI === ns && e.localName === name) { - break; - } - e = e.nextElementSibling; - } - return e; - } /** * Returns a font face declarations map, where the key is the style:name and * the value is the svg:font-family or null, if none set but a svg:font-face-uri @@ -676,10 +660,10 @@ odf.Formatting = function Formatting() { pageLayoutElement = getPageLayoutStyleElement(styleName, styleFamily); if (!pageLayoutElement) { - pageLayoutElement = getDirectChild(odfContainer.rootElement.styles, + pageLayoutElement = domUtils.getDirectChild(odfContainer.rootElement.styles, stylens, "default-page-layout"); } - props = getDirectChild(pageLayoutElement, stylens, + props = domUtils.getDirectChild(pageLayoutElement, stylens, "page-layout-properties"); if (props) { printOrientation = props.getAttributeNS(stylens, "print-orientation") || "portrait"; diff --git a/webodf/lib/odf/OdfContainer.js b/webodf/lib/odf/OdfContainer.js index d7ec05e3..e7960733 100644 --- a/webodf/lib/odf/OdfContainer.js +++ b/webodf/lib/odf/OdfContainer.js @@ -70,22 +70,6 @@ @type{!string}*/ documentContentScope = "document-content"; - /** - * @param {?Node} node - * @param {!string} ns - * @param {!string} name - * @return {?Element} - */ - function getDirectChild(node, ns, name) { - node = node ? node.firstChild : null; - while (node) { - if (node.localName === name && node.namespaceURI === ns) { - return /**@type{!Element}*/(node); - } - node = node.nextSibling; - } - return null; - } /** * Return the position the node should get according to the ODF flat format. * @param {!Node} child @@ -642,7 +626,7 @@ * Any processing instructions are removed, since importing them * gives an exception. * @param {Document|undefined} xmldoc - * @return {Node|undefined} + * @return {!Element|undefined} */ function importRootNode(xmldoc) { var doc = self.rootElement.ownerDocument, @@ -652,7 +636,7 @@ if (xmldoc) { removeProcessingInstructions(xmldoc.documentElement); try { - node = doc.importNode(xmldoc.documentElement, true); + node = /**@type{!Element}*/(doc.importNode(xmldoc.documentElement, true)); } catch (ignore) { } } @@ -678,12 +662,12 @@ function setRootElement(root) { contentElement = null; self.rootElement = /**@type{!odf.ODFDocumentElement}*/(root); - root.fontFaceDecls = getDirectChild(root, officens, 'font-face-decls'); - root.styles = getDirectChild(root, officens, 'styles'); - root.automaticStyles = getDirectChild(root, officens, 'automatic-styles'); - root.masterStyles = getDirectChild(root, officens, 'master-styles'); - root.body = getDirectChild(root, officens, 'body'); - root.meta = getDirectChild(root, officens, 'meta'); + root.fontFaceDecls = domUtils.getDirectChild(root, officens, 'font-face-decls'); + root.styles = domUtils.getDirectChild(root, officens, 'styles'); + root.automaticStyles = domUtils.getDirectChild(root, officens, 'automatic-styles'); + root.masterStyles = domUtils.getDirectChild(root, officens, 'master-styles'); + root.body = domUtils.getDirectChild(root, officens, 'body'); + root.meta = domUtils.getDirectChild(root, officens, 'meta'); linkAnnotationStartAndEndElements(root); } /** @@ -713,16 +697,16 @@ setState(OdfContainer.INVALID); return; } - root.fontFaceDecls = getDirectChild(node, officens, 'font-face-decls'); + root.fontFaceDecls = domUtils.getDirectChild(node, officens, 'font-face-decls'); setChild(root, root.fontFaceDecls); - n = getDirectChild(node, officens, 'styles'); + n = domUtils.getDirectChild(node, officens, 'styles'); root.styles = n || xmldoc.createElementNS(officens, 'styles'); setChild(root, root.styles); - n = getDirectChild(node, officens, 'automatic-styles'); + n = domUtils.getDirectChild(node, officens, 'automatic-styles'); root.automaticStyles = n || xmldoc.createElementNS(officens, 'automatic-styles'); setAutomaticStylesScope(root.automaticStyles, documentStylesScope); setChild(root, root.automaticStyles); - node = getDirectChild(node, officens, 'master-styles'); + node = domUtils.getDirectChild(node, officens, 'master-styles'); root.masterStyles = node || xmldoc.createElementNS(officens, 'master-styles'); setChild(root, root.masterStyles); @@ -748,14 +732,14 @@ return; } root = self.rootElement; - fontFaceDecls = getDirectChild(node, officens, 'font-face-decls'); + fontFaceDecls = domUtils.getDirectChild(node, officens, 'font-face-decls'); if (root.fontFaceDecls && fontFaceDecls) { fontFaceNameChangeMap = mergeFontFaceDecls(root.fontFaceDecls, fontFaceDecls); } else if (fontFaceDecls) { root.fontFaceDecls = fontFaceDecls; setChild(root, fontFaceDecls); } - automaticStyles = getDirectChild(node, officens, 'automatic-styles'); + automaticStyles = domUtils.getDirectChild(node, officens, 'automatic-styles'); setAutomaticStylesScope(automaticStyles, documentContentScope); if (fontFaceNameChangeMap) { styleInfo.changeFontFaceNames(automaticStyles, fontFaceNameChangeMap); @@ -770,7 +754,7 @@ root.automaticStyles = automaticStyles; setChild(root, automaticStyles); } - node = getDirectChild(node, officens, 'body'); + node = domUtils.getDirectChild(node, officens, 'body'); if (node === null) { throw " tag is mising."; } @@ -789,7 +773,7 @@ return; } root = self.rootElement; - root.meta = getDirectChild(node, officens, 'meta'); + root.meta = domUtils.getDirectChild(node, officens, 'meta'); setChild(root, root.meta); } /** @@ -804,7 +788,7 @@ return; } root = self.rootElement; - root.settings = getDirectChild(node, officens, 'settings'); + root.settings = domUtils.getDirectChild(node, officens, 'settings'); setChild(root, root.settings); } /** @@ -914,7 +898,7 @@ var header = '\n', xml = '', manifest = /**@type{!Document}*/(runtime.parseXML(xml)), - manifestRoot = getDirectChild(manifest, manifestns, 'manifest'), + manifestRoot = manifest.documentElement, serializer = new xmldom.LSSerializer(), /**@type{string}*/ fullPath; @@ -1042,9 +1026,9 @@ body; if (!contentElement) { body = self.rootElement.body; - contentElement = getDirectChild(body, officens, "text") - || getDirectChild(body, officens, "presentation") - || getDirectChild(body, officens, "spreadsheet"); + contentElement = domUtils.getDirectChild(body, officens, "text") + || domUtils.getDirectChild(body, officens, "presentation") + || domUtils.getDirectChild(body, officens, "spreadsheet"); } if (!contentElement) { throw "Could not find content element in ."; diff --git a/webodf/lib/odf/Style2CSS.js b/webodf/lib/odf/Style2CSS.js index f81300e8..cae50196 100644 --- a/webodf/lib/odf/Style2CSS.js +++ b/webodf/lib/odf/Style2CSS.js @@ -84,6 +84,7 @@ odf.Style2CSS = function Style2CSS() { /**@const @type{!string}*/ presentationns = odf.Namespaces.presentationns, + domUtils = new core.DomUtils(), /**@const @type{!Object.}*/ @@ -451,22 +452,6 @@ odf.Style2CSS = function Style2CSS() { } return selectors; } - /** - * @param {?Element} node - * @param {!string} ns - * @param {!string} name - * @return {?Element} - */ - function getDirectChild(node, ns, name) { - var e = node && node.firstElementChild; - while (e) { - if (e.namespaceURI === ns && e.localName === name) { - break; - } - e = e.nextElementSibling; - } - return e; - } /** * Make sure border width is no less than 1px wide; otherwise border is not rendered. * Only have problems with point unit at the moment. Please add more rule if needed. @@ -523,7 +508,7 @@ odf.Style2CSS = function Style2CSS() { * @return {?{value: !number, unit: !string}} */ function getFontSize(styleNode) { - var props = getDirectChild(styleNode, stylens, 'text-properties'); + var props = domUtils.getDirectChild(styleNode, stylens, 'text-properties'); if (props) { return utils.parseFoFontSize(props.getAttributeNS(fons, 'font-size')); } @@ -628,7 +613,7 @@ odf.Style2CSS = function Style2CSS() { function getParagraphProperties(props) { var rule = '', bgimage, url, lineHeight; rule += applySimpleMapping(props, paragraphPropertySimpleMapping); - bgimage = getDirectChild(props, stylens, 'background-image'); + bgimage = domUtils.getDirectChild(props, stylens, 'background-image'); if (bgimage) { url = bgimage.getAttributeNS(xlinkns, 'href'); if (url) { @@ -791,41 +776,41 @@ odf.Style2CSS = function Style2CSS() { selector = selectors.join(','), rule = '', properties; - properties = getDirectChild(node.element, stylens, 'text-properties'); + properties = domUtils.getDirectChild(node.element, stylens, 'text-properties'); if (properties) { rule += getTextProperties(properties); } - properties = getDirectChild(node.element, + properties = domUtils.getDirectChild(node.element, stylens, 'paragraph-properties'); if (properties) { rule += getParagraphProperties(properties); } - properties = getDirectChild(node.element, + properties = domUtils.getDirectChild(node.element, stylens, 'graphic-properties'); if (properties) { rule += getGraphicProperties(properties); } - properties = getDirectChild(node.element, + properties = domUtils.getDirectChild(node.element, stylens, 'drawing-page-properties'); if (properties) { rule += getDrawingPageProperties(properties); } - properties = getDirectChild(node.element, + properties = domUtils.getDirectChild(node.element, stylens, 'table-cell-properties'); if (properties) { rule += getTableCellProperties(properties); } - properties = getDirectChild(node.element, + properties = domUtils.getDirectChild(node.element, stylens, 'table-row-properties'); if (properties) { rule += getTableRowProperties(properties); } - properties = getDirectChild(node.element, + properties = domUtils.getDirectChild(node.element, stylens, 'table-column-properties'); if (properties) { rule += getTableColumnProperties(properties); } - properties = getDirectChild(node.element, + properties = domUtils.getDirectChild(node.element, stylens, 'table-properties'); if (properties) { rule += getTableProperties(properties); @@ -898,9 +883,9 @@ odf.Style2CSS = function Style2CSS() { level = node.getAttributeNS(textns, "level"), itemSelector, listItemRule, - listLevelProps = getDirectChild(node, stylens, + listLevelProps = domUtils.getDirectChild(node, stylens, "list-level-properties"), - listLevelLabelAlign = getDirectChild(listLevelProps, stylens, + listLevelLabelAlign = domUtils.getDirectChild(listLevelProps, stylens, "list-level-label-alignment"), bulletIndent, listIndent, @@ -968,7 +953,7 @@ odf.Style2CSS = function Style2CSS() { var rule = '', imageProps, url, contentLayoutRule = '', pageSizeRule = '', - props = getDirectChild(node, stylens, 'page-layout-properties'), + props = domUtils.getDirectChild(node, stylens, 'page-layout-properties'), stylename, masterStyles, e, @@ -979,7 +964,7 @@ odf.Style2CSS = function Style2CSS() { stylename = node.getAttributeNS(stylens, 'name'); rule += applySimpleMapping(props, pageContentPropertySimpleMapping); - imageProps = getDirectChild(props, stylens, 'background-image'); + imageProps = domUtils.getDirectChild(props, stylens, 'background-image'); if (imageProps) { url = imageProps.getAttributeNS(xlinkns, 'href'); if (url) { @@ -990,7 +975,7 @@ odf.Style2CSS = function Style2CSS() { } if (documentType === 'presentation') { - masterStyles = getDirectChild(/**@type{!Element}*/(node.parentNode.parentNode), officens, 'master-styles'); + masterStyles = domUtils.getDirectChild(/**@type{!Element}*/(node.parentNode.parentNode), officens, 'master-styles'); e = masterStyles && masterStyles.firstElementChild; while (e) { // Generate CSS for all the pages that use the master page that use this page-layout