From: <asa...@us...> - 2012-11-09 12:58:45
|
Revision: 7706 http://sourceforge.net/p/htmlunit/code/7706 Author: asashour Date: 2012-11-09 12:58:42 +0000 (Fri, 09 Nov 2012) Log Message: ----------- Implement DOMStringMap Modified Paths: -------------- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/configuration/JavaScriptConfiguration.java trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSStyleDeclaration.java trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/dom/DOMStringMap.java Added Paths: ----------- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/dom/DOMStringMapTest.java Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/configuration/JavaScriptConfiguration.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/configuration/JavaScriptConfiguration.java 2012-11-09 11:12:24 UTC (rev 7705) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/configuration/JavaScriptConfiguration.java 2012-11-09 12:58:42 UTC (rev 7706) @@ -116,6 +116,7 @@ import com.gargoylesoftware.htmlunit.javascript.host.dom.DOMException; import com.gargoylesoftware.htmlunit.javascript.host.dom.DOMImplementation; import com.gargoylesoftware.htmlunit.javascript.host.dom.DOMParser; +import com.gargoylesoftware.htmlunit.javascript.host.dom.DOMStringMap; import com.gargoylesoftware.htmlunit.javascript.host.dom.DOMTokenList; import com.gargoylesoftware.htmlunit.javascript.host.geo.Coordinates; import com.gargoylesoftware.htmlunit.javascript.host.geo.Geolocation; @@ -281,7 +282,8 @@ CSSRuleList.class, CSSStyleDeclaration.class, CSSStyleRule.class, CSSStyleSheet.class, CSSValue.class, CanvasRenderingContext2D.class, CharacterDataImpl.class, ClientRect.class, Comment.class, ComputedCSSStyleDeclaration.class, Console.class, Coordinates.class, DataView.class, DOMException.class, - DOMImplementation.class, DOMParser.class, DOMTokenList.class, Document.class, DocumentFragment.class, + DOMImplementation.class, DOMParser.class, DOMStringMap.class, + DOMTokenList.class, Document.class, DocumentFragment.class, DocumentType.class, Element.class, Enumerator.class, Event.class, EventNode.class, External.class, Float32Array.class, Float64Array.class, FormChild.class, FormField.class, Geolocation.class, History.class, Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSStyleDeclaration.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSStyleDeclaration.java 2012-11-09 11:12:24 UTC (rev 7705) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSStyleDeclaration.java 2012-11-09 12:58:42 UTC (rev 7706) @@ -706,6 +706,7 @@ * to camel-cased (e.g. <tt>fontSize</tt>). * @param string the string to camelize * @return the transformed string + * @see com.gargoylesoftware.htmlunit.javascript.host.dom.DOMStringMap#decamelize(String) */ protected static String camelize(final String string) { if (string == null) { @@ -719,7 +720,7 @@ // not found in CamelizeCache_; convert and store in cache final int pos = string.indexOf('-'); - if (pos == -1 || pos >= string.length() - 1) { + if (pos == -1 || pos == string.length() - 1) { // cache also this strings for performance CamelizeCache_.put(string, string); return string; Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/dom/DOMStringMap.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/dom/DOMStringMap.java 2012-11-09 11:12:24 UTC (rev 7705) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/dom/DOMStringMap.java 2012-11-09 12:58:42 UTC (rev 7706) @@ -14,9 +14,16 @@ */ package com.gargoylesoftware.htmlunit.javascript.host.dom; +import net.sourceforge.htmlunit.corejs.javascript.Context; +import net.sourceforge.htmlunit.corejs.javascript.Scriptable; +import net.sourceforge.htmlunit.corejs.javascript.ScriptableObject; + +import com.gargoylesoftware.htmlunit.html.DomElement; +import com.gargoylesoftware.htmlunit.html.HtmlElement; import com.gargoylesoftware.htmlunit.javascript.SimpleScriptable; import com.gargoylesoftware.htmlunit.javascript.configuration.JsxClass; import com.gargoylesoftware.htmlunit.javascript.host.Node; +import com.gargoylesoftware.htmlunit.javascript.host.Window; /** * A JavaScript object for DOMStringMap. @@ -43,4 +50,57 @@ setPrototype(getPrototype(getClass())); } + /** + * {@inheritDoc} + */ + @Override + public Object get(final String name, final Scriptable start) { + final HtmlElement e = getDomNodeOrNull(); + if (e != null) { + final String value = e.getAttribute("data-" + decamelize(name)); + if (value != DomElement.ATTRIBUTE_NOT_DEFINED) { + return value; + } + } + return NOT_FOUND; + } + + /** + * {@inheritDoc} + */ + public void put(final String name, final Scriptable start, final Object value) { + if (!(ScriptableObject.getTopLevelScope(this) instanceof Window) || getWindow().getWebWindow() == null) { + super.put(name, start, value); + } + else { + final HtmlElement e = getDomNodeOrNull(); + e.setAttribute("data-" + decamelize(name), Context.toString(value)); + } + } + + /** + * Transforms the specified string from camel-cased (e.g. <tt>fontSize</tt>) + * to delimiter-separated (e.g. <tt>font-size</tt>). + * to camel-cased . + * @param string the string to decamelize + * @return the transformed string + * @see com.gargoylesoftware.htmlunit.javascript.host.css.CSSStyleDeclaration#camelize + */ + public static String decamelize(final String string) { + if (string == null || string.isEmpty()) { + return string; + } + + final StringBuilder buffer = new StringBuilder(); + for (int i = 0; i < string.length(); i++) { + final char ch = string.charAt(i); + if (Character.isUpperCase(ch)) { + buffer.append('-').append(Character.toLowerCase(ch)); + } + else { + buffer.append(ch); + } + } + return buffer.toString(); + } } Added: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/dom/DOMStringMapTest.java =================================================================== --- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/dom/DOMStringMapTest.java (rev 0) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/dom/DOMStringMapTest.java 2012-11-09 12:58:42 UTC (rev 7706) @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2002-2012 Gargoyle Software Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.gargoylesoftware.htmlunit.javascript.host.dom; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import com.gargoylesoftware.htmlunit.BrowserRunner; +import com.gargoylesoftware.htmlunit.BrowserRunner.Alerts; +import com.gargoylesoftware.htmlunit.WebDriverTestCase; + +/** + * Tests for {@link DOMStringMap}. + * + * @version $Revision$ + * @author Ahmed Ashour + */ +@RunWith(BrowserRunner.class) +public class DOMStringMapTest extends WebDriverTestCase { + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts(FF = { "undefined", "there" }, FF3_6 = { }) + public void get() throws Exception { + final String html + = "<html><head><title>First</title><script>\n" + + "function test() {\n" + + " if (document.body.dataset) {\n" + + " alert(document.body.dataset.hi);\n" + + " alert(document.body.dataset.hello);\n" + + " }\n" + + "}\n" + + "</script></head><body onload='test()' data-hello='there'>\n" + + "</body></html>"; + + loadPageWithAlerts2(html); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts(FF = { "old", "old", "null", "null" }, FF3_6 = { }) + public void put() throws Exception { + final String html + = "<html><head><title>First</title><script>\n" + + "function test() {\n" + + " if (document.body.dataset) {\n" + + " document.body.dataset.dateOfBirth = 'old';\n" + + " alert(document.body.dataset.dateOfBirth);\n" + + " alert(document.body.getAttribute('data-date-of-birth'));\n" + + " document.body.dataset.dateOfBirth = null;\n" + + " alert(document.body.dataset.dateOfBirth);\n" + + " alert(document.body.getAttribute('data-date-of-birth'));\n" + + " }\n" + + "}\n" + + "</script></head><body onload='test()'>\n" + + "</body></html>"; + + loadPageWithAlerts2(html); + } +} Property changes on: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/dom/DOMStringMapTest.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native |