Repository: wicket Updated Branches: refs/heads/wicket-6.x 54431eb0c -> e9f116aa8
WICKET-5577 Generation of wicket ids with prefix / suffix (cherry picked from commit ccff0802a729bc10d84ed876ec08d408efc377af) Project: http://git-wip-us.apache.org/repos/asf/wicket/repo Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/e9f116aa Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/e9f116aa Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/e9f116aa Branch: refs/heads/wicket-6.x Commit: e9f116aa871cdccbc72c8329468154efef638e3c Parents: 54431eb Author: Martin Tzvetanov Grigorov <[email protected]> Authored: Mon May 19 18:38:01 2014 +0300 Committer: Martin Tzvetanov Grigorov <[email protected]> Committed: Sun May 25 21:59:35 2014 +0200 ---------------------------------------------------------------------- .../java/org/apache/wicket/Application.java | 10 +++ .../main/java/org/apache/wicket/Component.java | 53 +----------- .../apache/wicket/DefaultMarkupIdGenerator.java | 80 ++++++++++++++++++ .../org/apache/wicket/IMarkupIdGenerator.java | 32 ++++++++ .../apache/wicket/MarkupIdGeneratorTest.java | 85 ++++++++++++++++++++ 5 files changed, 209 insertions(+), 51 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/wicket/blob/e9f116aa/wicket-core/src/main/java/org/apache/wicket/Application.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/Application.java b/wicket-core/src/main/java/org/apache/wicket/Application.java index a2bf907..514cf2e 100644 --- a/wicket-core/src/main/java/org/apache/wicket/Application.java +++ b/wicket-core/src/main/java/org/apache/wicket/Application.java @@ -1596,6 +1596,16 @@ public abstract class Application implements UnboundListener, IEventSink this.requestCycleProvider = requestCycleProvider; } + /** + * @deprecated In Wicket 7.x this method is part of {@link org.apache.wicket.settings.def.MarkupSettings} + * @return The generator of markup ids for components + */ + @Deprecated + public IMarkupIdGenerator getMarkupIdGenerator() + { + return new DefaultMarkupIdGenerator(); + } + private static class DefaultExceptionMapperProvider implements IProvider<IExceptionMapper> { @Override http://git-wip-us.apache.org/repos/asf/wicket/blob/e9f116aa/wicket-core/src/main/java/org/apache/wicket/Component.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/Component.java b/wicket-core/src/main/java/org/apache/wicket/Component.java index a9d3ce9..a3fdc75 100644 --- a/wicket-core/src/main/java/org/apache/wicket/Component.java +++ b/wicket-core/src/main/java/org/apache/wicket/Component.java @@ -1485,57 +1485,8 @@ public abstract class Component */ public String getMarkupId(boolean createIfDoesNotExist) { - Object storedMarkupId = getMarkupIdImpl(); - if (storedMarkupId instanceof String) - { - return (String)storedMarkupId; - } - - if (storedMarkupId == null && createIfDoesNotExist == false) - { - return null; - } - - int generatedMarkupId = storedMarkupId instanceof Integer ? (Integer)storedMarkupId - : getSession().nextSequenceValue(); - - if (generatedMarkupId == 0xAD) - { - // WICKET-4559 skip suffix 'ad' because some ad-blocking solutions may hide the -// component - generatedMarkupId = getSession().nextSequenceValue(); - } - - if (storedMarkupId == null) - { - setMarkupIdImpl(generatedMarkupId); - } - - String markupIdPrefix = "id"; - if (getApplication().usesDevelopmentConfig()) - { - // in non-deployment mode we make the markup id include component id - // so it is easier to debug - markupIdPrefix = getId(); - } - - String markupIdPostfix = Integer.toHexString(generatedMarkupId).toLowerCase(); - - String markupId = markupIdPrefix + markupIdPostfix; - - // make sure id is compliant with w3c requirements (starts with a letter) - char c = markupId.charAt(0); - if (!Character.isLetter(c)) - { - markupId = "id" + markupId; - } - - // escape some noncompliant characters - markupId = Strings.replaceAll(markupId, "_", "__").toString(); - markupId = markupId.replace('.', '_'); - markupId = markupId.replace('-', '_'); - markupId = markupId.replace(' ', '_'); - + IMarkupIdGenerator markupIdGenerator = getApplication().getMarkupIdGenerator(); + String markupId = markupIdGenerator.generateMarkupId(this); return markupId; } http://git-wip-us.apache.org/repos/asf/wicket/blob/e9f116aa/wicket-core/src/main/java/org/apache/wicket/DefaultMarkupIdGenerator.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/DefaultMarkupIdGenerator.java b/wicket-core/src/main/java/org/apache/wicket/DefaultMarkupIdGenerator.java new file mode 100644 index 0000000..4f5d90f --- /dev/null +++ b/wicket-core/src/main/java/org/apache/wicket/DefaultMarkupIdGenerator.java @@ -0,0 +1,80 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.wicket; + +import org.apache.wicket.util.string.Strings; + +/** + * An implementation of IMarkupIdGenerator that uses the Session to generate + * sequence numbers for the component markup ids. + * As a prefix for the generated markup id in development mode it uses the component id + * and in production mode the string <em>id</em>. + */ +public class DefaultMarkupIdGenerator implements IMarkupIdGenerator +{ + @Override + public String generateMarkupId(Component component) + { + Object storedMarkupId = component.getMarkupIdImpl(); + if (storedMarkupId instanceof String) + { + return (String)storedMarkupId; + } + + Session session = component.getSession(); + int generatedMarkupId = storedMarkupId instanceof Integer ? (Integer)storedMarkupId + : session.nextSequenceValue(); + + if (generatedMarkupId == 0xAD) + { + // WICKET-4559 skip suffix 'ad' because some ad-blocking solutions may hide the component + generatedMarkupId = session.nextSequenceValue(); + } + + if (storedMarkupId == null) + { + component.setMarkupIdImpl(generatedMarkupId); + } + + String markupIdPrefix = "id"; + if (component.getApplication().usesDevelopmentConfig()) + { + // in non-deployment mode we make the markup id include component id + // so it is easier to debug + markupIdPrefix = component.getId(); + } + + String markupIdPostfix = Integer.toHexString(generatedMarkupId).toLowerCase(); + + String markupId = markupIdPrefix + markupIdPostfix; + + // make sure id is compliant with w3c requirements (starts with a letter) + char c = markupId.charAt(0); + if (!Character.isLetter(c)) + { + markupId = "id" + markupId; + } + + // escape some noncompliant characters + markupId = Strings.replaceAll(markupId, "_", "__").toString(); + markupId = markupId.replace('.', '_'); + markupId = markupId.replace('-', '_'); + markupId = markupId.replace(' ', '_'); + + return markupId; + } +} http://git-wip-us.apache.org/repos/asf/wicket/blob/e9f116aa/wicket-core/src/main/java/org/apache/wicket/IMarkupIdGenerator.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/IMarkupIdGenerator.java b/wicket-core/src/main/java/org/apache/wicket/IMarkupIdGenerator.java new file mode 100644 index 0000000..6bd2e30 --- /dev/null +++ b/wicket-core/src/main/java/org/apache/wicket/IMarkupIdGenerator.java @@ -0,0 +1,32 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.wicket; + +/** + * Generates markup ids for components + */ +public interface IMarkupIdGenerator +{ + /** + * Generates markup id for the given component + * + * @param component + * The component for which to generate a markup id + * @return The generated markup id + */ + String generateMarkupId(Component component); +} http://git-wip-us.apache.org/repos/asf/wicket/blob/e9f116aa/wicket-core/src/test/java/org/apache/wicket/MarkupIdGeneratorTest.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/test/java/org/apache/wicket/MarkupIdGeneratorTest.java b/wicket-core/src/test/java/org/apache/wicket/MarkupIdGeneratorTest.java new file mode 100644 index 0000000..9d4f67f --- /dev/null +++ b/wicket-core/src/test/java/org/apache/wicket/MarkupIdGeneratorTest.java @@ -0,0 +1,85 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.wicket; + +import org.apache.wicket.markup.html.link.Link; +import org.apache.wicket.mock.MockApplication; +import org.apache.wicket.util.tester.WicketTester; +import org.junit.Test; + +/** + * Tests for IMarkupIdGenerator + * + * @since 6.16.0 + * @see <a href="https://issues.apache.org/jira/browse/WICKET-5577">Generation of wicket ids with prefix / suffix</a> + */ +public class MarkupIdGeneratorTest extends WicketTestCase +{ + @Test + public void defaultMarkupIdGenerator() + { + MockPageWithLink page = new MockPageWithLink(); + Link link = new Link(MockPageWithLink.LINK_ID) + { + @Override + public void onClick() + { + } + }; + link.setOutputMarkupId(true); + page.add(link); + + assertEquals("link1", link.getMarkupId()); + } + + @Test + public void customMarkupIdGenerator() + { + final String customMarkupId = "custom"; + final IMarkupIdGenerator generator = new IMarkupIdGenerator() + { + @Override + public String generateMarkupId(Component component) + { + return customMarkupId; + } + }; + + MockApplication application = new MockApplication() + { + @Override + public IMarkupIdGenerator getMarkupIdGenerator() + { + return generator; + } + }; + tester.destroy(); + tester = new WicketTester(application); + MockPageWithLink page = new MockPageWithLink(); + Link link = new Link(MockPageWithLink.LINK_ID) + { + @Override + public void onClick() + { + } + }; + link.setOutputMarkupId(true); + page.add(link); + + assertEquals(customMarkupId, link.getMarkupId()); + } +}
