1*2cb6d6f1SJim Jagielski################################################################ 2*2cb6d6f1SJim Jagielski# 3*2cb6d6f1SJim Jagielski# Licensed to the Apache Software Foundation (ASF) under one 4*2cb6d6f1SJim Jagielski# or more contributor license agreements. See the NOTICE file 5*2cb6d6f1SJim Jagielski# distributed with this work for additional information 6*2cb6d6f1SJim Jagielski# regarding copyright ownership. The ASF licenses this file 7*2cb6d6f1SJim Jagielski# to you under the Apache License, Version 2.0 (the 8*2cb6d6f1SJim Jagielski# "License"); you may not use this file except in compliance 9*2cb6d6f1SJim Jagielski# with the License. You may obtain a copy of the License at 10*2cb6d6f1SJim Jagielski# 11*2cb6d6f1SJim Jagielski# http://www.apache.org/licenses/LICENSE-2.0 12*2cb6d6f1SJim Jagielski# 13*2cb6d6f1SJim Jagielski# Unless required by applicable law or agreed to in writing, 14*2cb6d6f1SJim Jagielski# software distributed under the License is distributed on an 15*2cb6d6f1SJim Jagielski# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*2cb6d6f1SJim Jagielski# KIND, either express or implied. See the License for the 17*2cb6d6f1SJim Jagielski# specific language governing permissions and limitations 18*2cb6d6f1SJim Jagielski# under the License. 19*2cb6d6f1SJim Jagielski# 20*2cb6d6f1SJim Jagielski################################################################ 21*2cb6d6f1SJim Jagielski 22*2cb6d6f1SJim Jagielski"""foreign.strip_foreign(): removal of foreign elements and attributes (ODF 1.2 Part 1 2.2.2 B.1, 3.17).""" 23*2cb6d6f1SJim Jagielskiimport os 24*2cb6d6f1SJim Jagielski 25*2cb6d6f1SJim Jagielskiimport pytest 26*2cb6d6f1SJim Jagielski 27*2cb6d6f1SJim Jagielskifrom aootest.checkers import foreign 28*2cb6d6f1SJim Jagielski 29*2cb6d6f1SJim Jagielskipytestmark = pytest.mark.selftest 30*2cb6d6f1SJim Jagielski 31*2cb6d6f1SJim JagielskiHDR = ('<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*2cb6d6f1SJim Jagielski '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*2cb6d6f1SJim Jagielski 'xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xf="http://www.w3.org/2002/xforms"') 34*2cb6d6f1SJim JagielskiCASES = [ 35*2cb6d6f1SJim Jagielski ("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*2cb6d6f1SJim Jagielski ("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*2cb6d6f1SJim Jagielski ("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*2cb6d6f1SJim Jagielski ("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*2cb6d6f1SJim Jagielski ("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*2cb6d6f1SJim Jagielski ("namespace declared on an unwrapped element is kept for its children", 41*2cb6d6f1SJim Jagielski '<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*2cb6d6f1SJim Jagielski '<text:p><t2:span xmlns:t2="urn:oasis:names:tc:opendocument:xmlns:text:1.0">a</t2:span></text:p>'), 43*2cb6d6f1SJim Jagielski ("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*2cb6d6f1SJim Jagielski ("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*2cb6d6f1SJim Jagielski ("text is escaped again", '<text:p>a & b < c</text:p>', '<text:p>a & b < c</text:p>'), 46*2cb6d6f1SJim Jagielski] 47*2cb6d6f1SJim Jagielski 48*2cb6d6f1SJim Jagielski 49*2cb6d6f1SJim Jagielski@pytest.fixture(scope="module") 50*2cb6d6f1SJim Jagielskidef mixed(schema_dir): 51*2cb6d6f1SJim Jagielski return foreign.mixed_elements(os.path.join(schema_dir, "OpenDocument-v1.2-os-schema.rng")) 52*2cb6d6f1SJim Jagielski 53*2cb6d6f1SJim Jagielski 54*2cb6d6f1SJim Jagielski@pytest.mark.parametrize("src, want", [pytest.param(s, w, id=n) for n, s, w in CASES]) 55*2cb6d6f1SJim Jagielskidef test_strip_foreign(src, want, mixed): 56*2cb6d6f1SJim Jagielski got = foreign.strip_foreign(HDR + ">" + src + "</office:text>", mixed).split("\n", 1)[1].strip() 57*2cb6d6f1SJim Jagielski inner = got[got.index(">") + 1:-len("</office:text>")] if got.startswith("<office:text") else got 58*2cb6d6f1SJim Jagielski assert inner == want 59