Files
WebODF/javascriptzip/style2css.js
T

232 lines
7.4 KiB
JavaScript
Raw Normal View History

2010-06-14 17:31:36 +02:00
function style2css(stylesheet, stylesxmldom) {
// helper constants
var xlinkns = 'http://www.w3.org/1999/xlink';
2010-06-14 17:31:36 +02:00
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 = {
xlink: xlinkns,
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' ],
2010-06-15 00:20:55 +02:00
[ fons, 'font-size', 'font-size' ]
];
var bgImageSimpleMapping = [
[ xlinkns, 'href', 'background-image' ]
];
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 18:36:19 +02:00
[ fons, 'border-left', 'border-left' ],
[ fons, 'border-right', 'border-right' ],
[ fons, 'border-top', 'border-top' ],
[ fons, 'border-bottom', 'border-bottom' ],
[ fons, 'margin-left', 'margin-left' ],
[ fons, 'margin-right', 'margin-right' ],
[ fons, 'margin-top', 'margin-top' ],
[ fons, 'margin-bottom', 'margin-bottom' ],
[ fons, 'border', 'border' ],
2010-06-24 11:40:05 -07:00
[ fons, 'background-color', 'background-color' ],
];
2010-06-17 14:54:54 +02:00
var tablecellPropertySimpleMapping = [
[ fons, 'background-color', 'background-color' ],
[ fons, 'border-left', 'border-left' ],
[ fons, 'border-right', 'border-right' ],
[ fons, 'border-top', 'border-top' ],
[ fons, 'border-bottom', 'border-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
for (var prefix in namespaces) {
var rule = '@namespace ' + prefix + ' url(' + namespaces[prefix] + ')';
try {
2010-06-14 17:36:04 +02:00
stylesheet.insertRule(rule, stylesheet.cssRules.length);
} catch (e) {
// WebKit can throw an exception here, but it will have retained the
// namespace declarations anyway.
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-17 14:54:54 +02:00
properties = style.getElementsByTagNameNS(stylens, 'table-cell-properties');
if (properties.length > 0) {
rule += getTableCellProperties(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 + '}';
}
2010-06-14 18:36:19 +02:00
function applySimpleMapping(props, mapping) {
var rule = '';
for (var r in mapping) {
r = mapping[r];
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 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;';
}
2010-06-15 00:20:55 +02:00
value = props.getAttributeNS(stylens, 'font-name');
if (value) {
value = getFontDeclaration(value);
if (value) {
rule += 'font-family: ' + value + ';';
}
}
2010-06-14 17:31:36 +02:00
return rule;
}
function getParagraphProperties(props) {
var rule = '';
rule += applySimpleMapping(props, paragraphPropertySimpleMapping);
var imageProps = props.getElementsByTagNameNS(stylens, 'background-image');
if (imageProps.length > 0) {
//var url = imageProps.item(0).getAttributeNS(xlinkns, 'href');
var url = "http://chani.ca/avatar.png";
rule += "background-image: url('" + url + "');";
rule += "background-repeat: repeat;"; //FIXME test
}
2010-06-14 17:31:36 +02:00
return rule;
}
2010-06-17 14:54:54 +02:00
function getTableCellProperties(props) {
var rule = '';
rule += applySimpleMapping(props, tablecellPropertySimpleMapping);
return rule;
}
2010-06-15 00:20:55 +02:00
function getFontDeclaration(name) {
return '"'+name+'"';
}
2010-06-14 17:31:36 +02:00
// 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
}