Files
WebODF/javascriptzip/style2css.js
T

179 lines
5.5 KiB
JavaScript
Raw Normal View History

2010-06-14 17:31:36 +02:00
function style2css(stylesheet, stylesxmldom) {
// helper constants
var stylens = "urn:oasis:names:tc:opendocument:xmlns:style:1.0";
var officens = "urn:oasis:names:tc:opendocument:xmlns:office:1.0";
var textns = "urn:oasis:names:tc:opendocument:xmlns:text:1.0";
var fons="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0";
2010-06-14 17:48:48 +02:00
var drawns="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0";
var tablens="urn:oasis:names:tc:opendocument:xmlns:table:1.0";
2010-06-14 17:31:36 +02:00
var namespaces = {
2010-06-14 17:48:48 +02:00
draw: drawns,
fo: fons,
2010-06-14 17:31:36 +02:00
office: officens,
2010-06-14 17:48:48 +02:00
style: stylens,
table: tablens,
2010-06-14 17:31:36 +02:00
text: textns,
};
var familynamespaceprefixes = {
graphic: 'draw',
paragraph: 'text',
ruby: 'text',
section: 'text',
table: 'table',
'table-cell': 'table',
'table-column': 'table',
'table-row': 'table',
text: 'text'
};
var familytagnames = {
graphic: ['frame'],
paragraph: ['alphabetical-index-entry-template', 'h',
'illustration-index-entry-template', 'index-source-style',
'object-index-entry-template', 'p', 'table-index-entry-template',
'table-of-content-entry-template', 'user-index-entry-template'],
ruby: ['ruby', 'ruby-text'],
section: ['alphabetical-index', 'bibliography', 'illustration-index',
'index-title', 'object-index', 'section', 'table-of-content',
'table-index', 'user-index'],
table: ['background', 'table'],
'table-cell': ['body', 'covered-table-cell', 'even-columns', 'even-rows',
'first-column', 'first-row', 'last-column', 'last-row', 'odd-columns',
'odd-rows', 'table-cell'],
'table-column': ['table-column'],
'table-row': ['table-row'],
text: ['a', 'index-entry-chapter', 'index-entry-link-end',
'index-entry-link-start', 'index-entry-page-number', 'index-entry-span',
'index-entry-tab-stop', 'index-entry-text', 'index-title-template',
'linenumbering-configuration', 'list-level-style-number',
'list-level-style-bullet', 'outline-level-style', 'span']
};
var textPropertySimpleMapping = [
[ fons, 'color', 'color' ],
// this sets the element background, not just the text background
[ fons, 'background-color', 'background-color' ],
[ fons, 'font-weight', 'font-weight' ],
[ fons, 'font-style', 'font-style' ],
];
var paragraphPropertySimpleMapping = [
[ fons, 'text-align', 'text-align' ],
[ fons, 'padding-left', 'padding-left' ],
[ fons, 'padding-right', 'padding-right' ],
[ fons, 'padding-top', 'padding-top' ],
[ fons, 'padding-bottom', 'padding-bottom' ],
];
2010-06-14 17:31:36 +02:00
// make stylesheet empty
while (stylesheet.cssRules.length) {
stylesheet.deleteRule(stylesheet.cssRules.length-1);
}
var doc = stylesxmldom.ownerDocument;
// add @namespace rules
var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
for (var prefix in namespaces) {
var rule = '@namespace ' + prefix + ' url(' + namespaces[prefix] + ')';
if (!is_chrome) { // TODO find workaround for chrome
2010-06-14 17:36:04 +02:00
stylesheet.insertRule(rule, stylesheet.cssRules.length);
2010-06-14 17:31:36 +02:00
}
}
var namespaceResolver = function(prefix) {
return namespaces[prefix];
}
var iter = doc.evaluate("style:style",
stylesxmldom, namespaceResolver, XPathResult.ANY_TYPE, null);
var i = iter.iterateNext();
while (i) {
var rule = createRule(i);
2010-06-14 17:38:52 +02:00
if (rule) {
2010-06-14 17:36:04 +02:00
try {
stylesheet.insertRule(rule, stylesheet.cssRules.length);
} catch (e) {
throw e + ' ' + rule;
}
2010-06-14 17:31:36 +02:00
}
i = iter.iterateNext();
}
// alert(stylesheet.cssRules.length + ' = count');
return;
// helper functions
function createSelector(family, name) {
var prefix = familynamespaceprefixes[family];
if (prefix == null) return null;
var namepart = '['+prefix+'|style-name="'+name+'"]';
var selector = '';
var first = true;
return prefix+'|'+familytagnames[family].join(namepart+','+prefix+'|')
+ namepart;
}
function createRule(style) {
var name = style.getAttributeNS(stylens, 'name');
if (name == null) return null;
var family = style.getAttributeNS(stylens, 'family');
var selector = createSelector(family, name);
if (selector == null) return null;
var rule = '';
var properties = style.getElementsByTagNameNS(stylens, 'text-properties');
if (properties.length > 0) {
rule += getTextProperties(properties.item(0));
}
properties = style.getElementsByTagNameNS(stylens, 'paragraph-properties');
if (properties.length > 0) {
rule += getParagraphProperties(properties.item(0));
}
2010-06-14 17:38:52 +02:00
if (rule.length == 0) {
return null;
}
2010-06-14 17:31:36 +02:00
return selector + '{' + rule + '}';
}
function getTextProperties(props) {
var rule = '';
rule += applySimpleMapping(props, textPropertySimpleMapping);
2010-06-14 17:31:36 +02:00
var value;
value = props.getAttributeNS(stylens, 'text-underline-style');
if (value == 'solid') {
rule += 'text-decoration: underline;';
}
return rule;
}
function applySimpleMapping(props, mapping) {
var rule = '';
for (var r in mapping) {
var value = props.getAttributeNS(r[0], r[1]);
if (value) {
rule += r[2] + ':' + value + ';';
}
}
return rule;
}
2010-06-14 17:31:36 +02:00
function getParagraphProperties(props) {
var rule = '';
rule += applySimpleMapping(props, paragraphPropertySimpleMapping);
2010-06-14 17:31:36 +02:00
return rule;
}
// css vs odf styles
// ODF styles occur in families. A family is a group of odf elements to
// which an element applies. ODF families can be mapped to a group of css elements
// initial implemenation doing just the text family
}