diff --git a/webodf/lib/odf/OdfCanvas.js b/webodf/lib/odf/OdfCanvas.js index c44143aa..323360be 100644 --- a/webodf/lib/odf/OdfCanvas.js +++ b/webodf/lib/odf/OdfCanvas.js @@ -891,29 +891,61 @@ /** * @param {!Document} document - * @return {!HTMLStyleElement} + * @return {!Element} */ function addWebODFStyleSheet(document) { + /** + * @param {!string} css + * @param {!HTMLHeadElement} head + * @return {!Element} + */ + function addCSS(css, head) { + var e = document.createElementNS(head.namespaceURI, 'style'); + e.setAttribute('media', 'screen, print, handheld, projection'); + e.setAttribute('type', 'text/css'); + e.appendChild(document.createTextNode(css)); + head.appendChild(e); + return e; + } var head = /**@type{!HTMLHeadElement}*/(document.getElementsByTagName('head')[0]), + /**@type{?Element}*/ style, - href; + href, + count = document.styleSheets.length; + // make sure this is only added once per HTML document, e.g. in case of + // multiple odfCanvases + style = head.firstElementChild; + while (style) { + if (style.getAttribute("webodfcss")) { + return style; + } + style = style.nextElementSibling; + } if (String(typeof webodf_css) !== "undefined") { - style = document.createElementNS(head.namespaceURI, 'style'); - style.setAttribute('media', 'screen, print, handheld, projection'); - style.appendChild(document.createTextNode(webodf_css)); + style = addCSS(webodf_css, head); } else { style = document.createElementNS(head.namespaceURI, 'link'); href = "webodf.css"; if (runtime.currentDirectory) { - href = runtime.currentDirectory() + "/../" + href; + href = runtime.currentDirectory(); + if (href.length > 0 && href.substr(-1) !== "/") { + href += "/"; + } + href += "../webodf.css"; } style.setAttribute('href', href); style.setAttribute('rel', 'stylesheet'); + style.setAttribute('type', 'text/css'); + head.appendChild(style); + // if the css was not loaded, with , read and add the css + // this is a workaround for behaviour of qtjsruntime + if (count === document.styleSheets.length) { + head.removeChild(style); + style = addCSS(/**@type{!string}*/(runtime.readFileSync(href, "utf-8")), head); + } } - style.setAttribute('type', 'text/css'); - // TODO: make sure this is only added once per HTML document, e.g. in case of multiple odfCanvases - head.appendChild(style); - return /**@type {!HTMLStyleElement}*/(style); + style.setAttribute('webodfcss', 'true'); + return style; } /** * @param {!Document} document Put and ODF Canvas inside this element. @@ -966,7 +998,7 @@ showAnnotationRemoveButton = false, /**@type{gui.AnnotationViewManager}*/ annotationViewManager = null, - /**@type{!HTMLStyleElement}*/ + /**@type{!Element}*/ webodfcss, /**@type{!HTMLStyleElement}*/ fontcss, diff --git a/webodf/tests/CMakeLists.txt b/webodf/tests/CMakeLists.txt index 60c9bd59..66730aed 100644 --- a/webodf/tests/CMakeLists.txt +++ b/webodf/tests/CMakeLists.txt @@ -22,10 +22,12 @@ add_custom_target(nodetest ALL if (QT4_FOUND) COPY_FILES(tests_qtjsruntimetest ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}/_qtjsruntimetest ${TESTS_TESTFILES} ) + COPY_FILES(tests_qtjsruntimetest2 ${CMAKE_CURRENT_SOURCE_DIR}/.. + ${CMAKE_CURRENT_BINARY_DIR} webodf.css) add_custom_target(qtjsruntimetest ALL COMMAND qtjsruntime ${RUNTIME} tests.js WORKING_DIRECTORY _qtjsruntimetest - DEPENDS NodeJS ${tests_qtjsruntimetest} qtjsruntime manifest.json-target + DEPENDS NodeJS ${tests_qtjsruntimetest} ${tests_qtjsruntimetest2} qtjsruntime manifest.json-target ) endif (QT4_FOUND) diff --git a/webodf/tests/ops/OdtDocumentTests.js b/webodf/tests/ops/OdtDocumentTests.js index edf70997..96b98526 100644 --- a/webodf/tests/ops/OdtDocumentTests.js +++ b/webodf/tests/ops/OdtDocumentTests.js @@ -209,8 +209,8 @@ ops.OdtDocumentTests = function OdtDocumentTests(runner) { r.shouldBe(t, "t.steps", "6"); } function testCountLinesStepsDown_OverWrap() { - createOdtDocument("ABCD FGHIJ"); - appendCssRule("text|p[text|style-name=width30px] { width: 30px; }\n"); // Width calculated to wrap at first space + createOdtDocument("ABCD FGHIJ"); + appendCssRule("text|p[text|style-name=width4em] { width: 4em; }\n"); // Width calculated to wrap at first space setCursorPosition(4); t.steps = t.counter.countLinesSteps(1, t.filter); r.shouldBe(t, "t.steps", "6"); @@ -252,8 +252,8 @@ ops.OdtDocumentTests = function OdtDocumentTests(runner) { r.shouldBe(t, "t.steps", "3"); } function testCountStepsToLineBoundary_Forward_OverWrapping() { - createOdtDocument("ABC DEF"); - appendCssRule("text|p[text|style-name=width30px] { width: 30px; }\n"); // Width calculated to wrap at first space + createOdtDocument("ABC DEF"); + appendCssRule("text|p[text|style-name=width3em] { width: 3em; }\n"); // Width calculated to wrap at first space setCursorPosition(0); t.steps = t.counter.countStepsToLineBoundary(1, t.filter); r.shouldBe(t, "t.steps", "3"); @@ -302,8 +302,8 @@ ops.OdtDocumentTests = function OdtDocumentTests(runner) { r.shouldBe(t, "t.steps", "-3"); } function testCountStepsToLineBoundary_Backward_OverWrapping() { - createOdtDocument("ABC DEF"); - appendCssRule("text|p[text|style-name=width30px] { width: 30px; }\n"); // Width calculated to wrap at first space + createOdtDocument("ABC DEF"); + appendCssRule("text|p[text|style-name=width3em] { width: 3em; }\n"); // Width calculated to wrap at first space setCursorPosition(6); t.steps = t.counter.countStepsToLineBoundary(-1, t.filter); r.shouldBe(t, "t.steps", "-2"); diff --git a/webodf/tests/tests.js b/webodf/tests/tests.js index 212e63b4..4fad55bb 100644 --- a/webodf/tests/tests.js +++ b/webodf/tests/tests.js @@ -62,13 +62,6 @@ runtime.loadClass("odf.TextStyleApplicatorTests"); runtime.loadClass("xmldom.LSSerializerTests"); runtime.loadClass("xmldom.XPathTests"); -// qtjsruntimetest tests break if OdfCanvas is allowed to insert a dynamic element. -// If a dynamic link is inserted, subsequent dynamic style sheets (document.createElement(...)) fail to result in -// new entries being added to document.styleSheets. -// This can easily be seen by commenting out the following and running ops.OdtCursorTests followed by ops.OdtDocumentTests. -// As ops.OdtDocumentTests is the only test suite to rely on style behaviour functioning correctly and will report failures. -webodf_css = "/* Need to prevent OdfCanvas from inserting it's own element. See tests.js for more information */"; - /** * Holds the unit tests to run. * @type {!Array.}