diff options
author | David A. Madore <david+git@madore.org> | 2011-10-02 17:07:55 +0200 |
---|---|---|
committer | David A. Madore <david+git@madore.org> | 2011-10-02 17:07:55 +0200 |
commit | 16f86e7bf5f22806c2a64f9a9bc501a2f13e72e5 (patch) | |
tree | 2c0953f87fd023dad6a61707b7d69dee2e6863bf | |
parent | 6e0587364ea813c1762a313feed942ff68e18e58 (diff) | |
download | damlengine-16f86e7bf5f22806c2a64f9a9bc501a2f13e72e5.tar.gz damlengine-16f86e7bf5f22806c2a64f9a9bc501a2f13e72e5.tar.bz2 damlengine-16f86e7bf5f22806c2a64f9a9bc501a2f13e72e5.zip |
Handle HTML <style> and <script> elements by adding CDATA sections as appropriate.
Previously, only the (automatically inserted) <style> and <script>
elements in the HTML <head> were handled this way. This now extends
to these elements anywhere in the document.
-rw-r--r-- | org/madore/damlengine/TodoDamlElement.java | 10 | ||||
-rw-r--r-- | org/madore/damlengine/TodoElement.java | 10 | ||||
-rw-r--r-- | org/madore/damlengine/TodoStyleOrScript.java | 78 |
3 files changed, 82 insertions, 16 deletions
diff --git a/org/madore/damlengine/TodoDamlElement.java b/org/madore/damlengine/TodoDamlElement.java index 93275d9..599e49a 100644 --- a/org/madore/damlengine/TodoDamlElement.java +++ b/org/madore/damlengine/TodoDamlElement.java @@ -163,11 +163,13 @@ public final class TodoDamlElement extends TodoDefaultElement { this.ownerDeque.registerAtStart(toProcess); this.ownerDeque.registerAtStart(toProcessFirst); this.ownerDeque. - registerAtEnd(new TodoStyleOrScript(TodoStyleOrScript.Type.STYLE, - this.ctx, this)); + registerAtEnd(new TodoStyleOrScript. + HeadStyleOrScript(TodoStyleOrScript.Type.STYLE, + this.ctx, this)); this.ownerDeque. - registerAtEnd(new TodoStyleOrScript(TodoStyleOrScript.Type.SCRIPT, - this.ctx, this)); + registerAtEnd(new TodoStyleOrScript. + HeadStyleOrScript(TodoStyleOrScript.Type.SCRIPT, + this.ctx, this)); } } diff --git a/org/madore/damlengine/TodoElement.java b/org/madore/damlengine/TodoElement.java index 12810c0..4f15afa 100644 --- a/org/madore/damlengine/TodoElement.java +++ b/org/madore/damlengine/TodoElement.java @@ -76,6 +76,12 @@ public abstract class TodoElement extends TodoItem { protected final static Factory killAcronymFactory = new TodoKillAcronymElement.Factory(); + protected final static Factory styleFactory + = new TodoStyleOrScript.Factory(TodoStyleOrScript.Type.STYLE); + + protected final static Factory scriptFactory + = new TodoStyleOrScript.Factory(TodoStyleOrScript.Type.SCRIPT); + protected final Element node; public TodoElement(Element node, @@ -97,6 +103,10 @@ public abstract class TodoElement extends TodoItem { factory = killAFactory; else if ( node.getLocalName().equals("acronym") ) factory = killAcronymFactory; + else if ( node.getLocalName().equals("style") ) + factory = styleFactory; + else if ( node.getLocalName().equals("script") ) + factory = scriptFactory; } if ( factory == null ) factory = defaultFactory; diff --git a/org/madore/damlengine/TodoStyleOrScript.java b/org/madore/damlengine/TodoStyleOrScript.java index 44741e4..553366d 100644 --- a/org/madore/damlengine/TodoStyleOrScript.java +++ b/org/madore/damlengine/TodoStyleOrScript.java @@ -2,7 +2,7 @@ package org.madore.damlengine; import org.w3c.dom.*; -public final class TodoStyleOrScript extends TodoItem { +public final class TodoStyleOrScript extends TodoDefaultElement { public enum Type { STYLE("style", "text/css", "/* ", " */\n"), @@ -20,31 +20,85 @@ public final class TodoStyleOrScript extends TodoItem { } } + public static class Factory extends TodoElement.Factory { + final Type t; + public Factory(Type t) { + super(); + this.t = t; + } + @Override + public TodoStyleOrScript newItem(Element node, + Context ctx, + TodoItem caller) { + return new TodoStyleOrScript(t, node, ctx, caller); + } + } + final Type t; + final CharSequence useThisContent; + + public TodoStyleOrScript(Type t, + Element node, + Context ctx, + TodoItem caller, + CharSequence useThisContent) { + super(node, ctx, caller); + this.t = t; + this.useThisContent = useThisContent; + } + public TodoStyleOrScript(Type t, + Element node, Context ctx, TodoItem caller) { - super(ctx, caller); + super(node, ctx, caller); this.t = t; + this.useThisContent = null; } @Override - public void handle() { - if ( ctx.gc.headNode == null ) - throw new IllegalStateException("head node is null when doing style or script"); - Element node - = ctx.doc.createElementNS(DamlEngine.XHTML_NS, t.eltName); - node.setAttributeNS(null, "type", t.mimeType); - ctx.gc.headNode.appendChild(node); - ctx.gc.headNode.appendChild(ctx.doc.createTextNode("\n")); + public void handleNodeOnly() { + String content; + if ( useThisContent != null ) + content = useThisContent.toString(); + else + content = node.getTextContent(); + while ( node.getLastChild() != null ) { + node.removeChild(node.getLastChild()); + } node.appendChild(ctx.doc.createTextNode("\n"+t.preCdata)); - StringBuffer content - = (t==Type.SCRIPT)?ctx.gc.scriptContent:ctx.gc.styleContent; node.appendChild(ctx.doc. createCDATASection(t.postCdata+content +t.preCdata)); node.appendChild(ctx.doc.createTextNode(t.postCdata)); } + public static final class HeadStyleOrScript extends TodoItem { + + final Type t; + + public HeadStyleOrScript(Type t, Context ctx, TodoItem caller) { + super(ctx, caller); + this.t = t; + } + + @Override + public void handle() { + if ( ctx.gc.headNode == null ) + throw new IllegalStateException("head node is null when doing style or script"); + Element node + = ctx.doc.createElementNS(DamlEngine.XHTML_NS, t.eltName); + node.setAttributeNS(null, "type", t.mimeType); + ctx.gc.headNode.appendChild(node); + ctx.gc.headNode.appendChild(ctx.doc.createTextNode("\n")); + StringBuffer content + = (t==Type.SCRIPT)?ctx.gc.scriptContent:ctx.gc.styleContent; + this.ownerDeque. + registerAtStart(new TodoStyleOrScript(t, node, this.ctx, this, + content)); + } + + } + } |