1################################################################ 2# 3# Licensed to the Apache Software Foundation (ASF) under one 4# or more contributor license agreements. See the NOTICE file 5# distributed with this work for additional information 6# regarding copyright ownership. The ASF licenses this file 7# to you under the Apache License, Version 2.0 (the 8# "License"); you may not use this file except in compliance 9# with the License. You may obtain a copy of the License at 10# 11# http://www.apache.org/licenses/LICENSE-2.0 12# 13# Unless required by applicable law or agreed to in writing, 14# software distributed under the License is distributed on an 15# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16# KIND, either express or implied. See the License for the 17# specific language governing permissions and limitations 18# under the License. 19# 20################################################################ 21 22"""foreign.strip_foreign(): removal of foreign elements and attributes (ODF 1.2 Part 1 2.2.2 B.1, 3.17).""" 23import os 24 25import pytest 26 27from aootest.checkers import foreign 28 29pytestmark = pytest.mark.selftest 30 31HDR = ('<office:text xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" ' 32 'xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:foo="urn:foo" xmlns:dc="http://purl.org/dc/elements/1.1/" ' 33 'xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xf="http://www.w3.org/2002/xforms"') 34CASES = [ 35 ("foreign attribute removed", '<text:p foo:x="1" text:style-name="a">t</text:p>', '<text:p text:style-name="a">t</text:p>'), 36 ("foreign element outside a paragraph dropped with its content", '<foo:x>stuff<text:p>q</text:p></foo:x><text:p>t</text:p>', '<text:p>t</text:p>'), 37 ("foreign element in a paragraph: tags removed, content kept", '<text:p>a<foo:x>b<text:span>c</text:span></foo:x>d</text:p>', '<text:p>ab<text:span>c</text:span>d</text:p>'), 38 ("foreign element in a span in a paragraph: tags removed", '<text:p><text:span>a<foo:x>b</foo:x></text:span></text:p>', '<text:p><text:span>ab</text:span></text:p>'), 39 ("foreign element below a frame (no character data): dropped", '<text:p><draw:frame><foo:x>b</foo:x></draw:frame></text:p>', '<text:p><draw:frame></draw:frame></text:p>'), 40 ("namespace declared on an unwrapped element is kept for its children", 41 '<text:p><foo:x xmlns:t2="urn:oasis:names:tc:opendocument:xmlns:text:1.0"><t2:span>a</t2:span></foo:x></text:p>', 42 '<text:p><t2:span xmlns:t2="urn:oasis:names:tc:opendocument:xmlns:text:1.0">a</t2:span></text:p>'), 43 ("elements and attributes of table 4 namespaces kept", '<text:p xlink:href="u"><dc:title>t</dc:title></text:p>', '<text:p xlink:href="u"><dc:title>t</dc:title></text:p>'), 44 ("content below a table 4 element kept (wildcard content)", '<xf:model><foo:data><bar/></foo:data></xf:model>', '<xf:model><foo:data><bar></bar></foo:data></xf:model>'), 45 ("text is escaped again", '<text:p>a & b < c</text:p>', '<text:p>a & b < c</text:p>'), 46] 47 48 49@pytest.fixture(scope="module") 50def mixed(schema_dir): 51 return foreign.mixed_elements(os.path.join(schema_dir, "OpenDocument-v1.2-os-schema.rng")) 52 53 54@pytest.mark.parametrize("src, want", [pytest.param(s, w, id=n) for n, s, w in CASES]) 55def test_strip_foreign(src, want, mixed): 56 got = foreign.strip_foreign(HDR + ">" + src + "</office:text>", mixed).split("\n", 1)[1].strip() 57 inner = got[got.index(">") + 1:-len("</office:text>")] if got.startswith("<office:text") else got 58 assert inner == want 59